
// Control to validate
function ControlToValidate(clientID, type)
{
	this.clientID = clientID;
	this.type = type;
}


// Callout
function Callout(calloutID, message, imageUrl)
{
	this.calloutID = calloutID;
	this.message = message;
	this.imageUrl = imageUrl;
}


// Requred field validator
function RequiredFieldValidator(controlID, controlType, message, imageUrl)
{
	var ctrl;
	var callout;
	
	ctrl = new ControlToValidate(controlID, controlType);
	calloutID = ctrl.clientID + "_requiredVal";
	callout = new Callout(calloutID, message, imageUrl);

	switch(ctrl.type)
	{
		case "TextBox":
			addHandler(ctrl, 'keyup', _onkeyup);
			
			if(IsTextBoxEmpty(ctrl.clientID))
			{
				AppendCallout(ctrl, callout);
				return false
			}
			else
			{
				RemoveCallout(callout);
				return true;
			}
				
			break;
		case "RadioButtonList":
			addHandler(ctrl, 'click', _onclick);
			
			if(IsRadioButtonListSelected(ctrl.clientID))
			{
				RemoveCallout(callout);
				return true;
			}
			else
			{
				AppendCallout(ctrl, callout);
				return false;
			}	
									
			break;
		case "CheckBoxList":
			addHandler(ctrl, 'click', _onclick);
			
			if(IsCheckBoxListSelected(ctrl.clientID))
			{
				RemoveCallout(callout);
				return true;
			}
			else
			{
				AppendCallout(ctrl, callout);
				return false;
			}
				
			break;
		case "CheckBox":
			addHandler(ctrl, 'click', _onclick);
			
			if(IsCheckBoxSelected(ctrl.clientID))
			{
				RemoveCallout(callout);
				return true;
			}
			else
			{
				AppendCallout(ctrl, callout);
				return false;
			}
				
			break;
		case "ListBox":
			addHandler(ctrl, 'change', _onchange);
			if(IsListBoxSelected(ctrl.clientID))
			{
				RemoveCallout(callout);
				return true;
			}
			else
			{
				AppendCallout(ctrl, callout);
				return false;
			}
			
			break;
	}
			
	
	// KeyUp event
	function _onkeyup ()
	{
		if(IsTextBoxEmpty(ctrl.clientID))
		{
			AppendCallout(ctrl, callout);
			return false;
		}
		else
		{
			RemoveCallout(callout);
			return true;
		}
	} 
	
	
	// OnClick event
	function _onclick ()
	{
		switch(ctrl.type)
		{
			case "RadioButtonList":
				if(IsRadioButtonListSelected(ctrl.clientID))
				{
					RemoveCallout(callout);
					return true;
				}
				else
				{
					AppendCallout(ctrl, callout);
					return false;
				}
										
				break;
			case "CheckBoxList":
				if(IsCheckBoxListSelected(ctrl.clientID))
				{
					RemoveCallout(callout);
					//return true;
				}
				else
				{
					AppendCallout(ctrl, callout);
					//return false;
				}
					
				break;
			case "CheckBox":
				if(IsCheckBoxSelected(ctrl.clientID))
				{
					RemoveCallout(callout);
					//return true;
				}
				else
				{
					AppendCallout(ctrl, callout);
					//return false;
				}
					
				break;
		}
	}
		
		
	// OnSelectionChange
	function _onchange()
	{
		if(IsListBoxSelected(ctrl.clientID))
		{
			RemoveCallout(callout);
			return true;
		}
		else
		{
			AppendCallout(ctrl, callout);
			return false;
		}
	}	
}




// Requred field validator
function FormatValidator(controlID, reg, message, imageUrl)
{
	var ctrl = new ControlToValidate(controlID, "TextBox");
	var calloutID = ctrl.clientID + "_requiredVal";
	var callout = new Callout(calloutID, message, imageUrl);

	
	addHandler(ctrl, 'keyup', _onkeyup);
	
	if(IsValidFormat(ctrl.clientID, reg))
	{
		RemoveCallout(callout);
		return true;
	}
	else
	{
		AppendCallout(ctrl, callout);
		return false;
	}
	
		
	// KeyUp event
	function _onkeyup ()
	{
		if(IsValidFormat(ctrl.clientID, reg))
		{
			RemoveCallout(callout);
			return true;
		}
		else
		{
			AppendCallout(ctrl, callout);
			return false;
		}
	} 
}


//////////////////
function DateValidator(controlID, reg, message, imageUrl)
{
	var ctrl = new ControlToValidate(controlID, "TextBox");
	var calloutID = ctrl.clientID + "_requiredVal";
	var callout = new Callout(calloutID, message, imageUrl);

	
	addHandler(ctrl, 'keyup', _onkeyup);
	
	if(IsValidDate(ctrl.clientID, reg))
	{
		RemoveCallout(callout);
		return true;
	}
	else
	{
		AppendCallout(ctrl, callout);
		return false;
	}
	
		
	// KeyUp event
	function _onkeyup ()
	{
		if(IsValidDate(ctrl.clientID, reg))
		{
			RemoveCallout(callout);
			return true;
		}
		else
		{
			AppendCallout(ctrl, callout);
			return false;
		}
	} 
}

function IsValidDate(clientID, objRegExp) 
{
	var tbx = document.getElementById(clientID);
	var strValue = tbx.value;
	
	/************************************************
	DESCRIPTION: Validates that a string contains only
		valid dates with 2 digit month, 2 digit day,
		4 digit year. Date separator can be ., -, or /.
		Uses combination of regular expressions and
		string parsing to validate date.
		Ex. dd/mm/yyyy or dd-mm-yyyy or dd.mm.yyyy

	PARAMETERS:
	strValue - String to be tested for validity

	RETURNS:
	True if valid, otherwise false.

	REMARKS:
	Avoids some of the limitations of the Date.parse()
	method such as the date separator character.
	*************************************************/
	// var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
	 
	//check to see if in correct format
	if(!objRegExp.test(strValue))
		return false; //doesn't match pattern, bad date
	else
	{
		var strSeparator = strValue.substring(2,3); 
		var arrayDate = strValue.split(strSeparator); 
		//create a lookup for months not equal to Feb.
		var arrayLookup = { '01' : 31,'03' : 31, 
							'04' : 30,'05' : 31,
							'06' : 30,'07' : 31,
							'08' : 31,'09' : 30,
							'10' : 31,'11' : 30,'12' : 31}
		var intDay = parseInt(arrayDate[0],10); 

		//check if month value and day value agree
		if(arrayLookup[arrayDate[1]] != null) 
		{
			if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)
				return true; //found in lookup table, good date
		}
	    
		//check for February (bugfix 20050322)
		//bugfix  for parseInt kevin
		//bugfix  biss year  O.Jp Voutat
		var intMonth = parseInt(arrayDate[1],10);
		if (intMonth == 2) 
		{ 
			var intYear = parseInt(arrayDate[2]);
			if (intDay > 0 && intDay < 29) 
			{
				return true;
			}
			else if (intDay == 29) 
			{
				if ((intYear % 4 == 0) && (intYear % 100 != 0) || (intYear % 400 == 0)) 
				{
					// year div by 4 and ((not div by 100) or div by 400) ->ok
					return true;
				}   
			}
		}
	}
	return false; //any other values, bad date
}
//////////////////


// Check for valid format
function IsValidFormat(clientID, reg)
{	
	var tbx = document.getElementById(clientID);
	var str = tbx.value;
	
	return reg.test(str);		
}


// Add handler
function addHandler(el, eventName, handler) 
{
	var target = document.getElementById(el.clientID);
	
	if (target.attachEvent)
	{
		target.attachEvent('on' + eventName, handler);
	} 
	else if (target.addEventListener)
	{
		target.addEventListener(eventName, handler, false);
	}
}


// Check for empty TextBox
function IsTextBoxEmpty(controlID)
{
	var tbx = document.getElementById(controlID);
	
	if(tbx.value == null || tbx.value == "")
		return true;
	else
		return false;
}


// Check for RadioButtonList selection
function IsRadioButtonListSelected(controlID)
{
	var i = 0;
	var currentId = controlID + '_' + i;
	
	while(document.getElementById(currentId) != null)
	{
		if(document.getElementById(currentId).checked)
		{
			selected = true;
			return true;
		}
		
		i++;
		currentId = controlID + '_' + i;
	}
	
	return false;
}


// Check for CheckBoxList selection
function IsCheckBoxListSelected(controlID)
{
	var i = 0;
	var currentId = controlID + '_' + i;

	while(document.getElementById(currentId) != null)
	{
		if(document.getElementById(currentId).checked)
		{
			selected = true;
			return true;
		}
		
		i++;
		currentId = controlID + '_' + i;
	}
	
	return false;
}


// Check for CheckBox selection
function IsCheckBoxSelected(controlID)
{
	if(document.getElementById(controlID).checked)
		return true;
	else	
		return false;
}


// Check for ListBox selection
function IsListBoxSelected(controlID)
{
	var ctrl = document.getElementById(controlID);
	
	for (var i = 0; i < ctrl.options.length; i++)
	{
		if(ctrl.options[i].selected && ctrl.options[i].value != null && ctrl.options[i].value != "")
			return true;
	}
	
	return false;
}


// Append callout
function AppendCallout(controlToValidate, callout)
{
	if(document.getElementById(callout.calloutID) == null)
	{
		var ctrl = document.getElementById(controlToValidate.clientID);
		var parent = ctrl.parentNode;
				
		var div = document.createElement("div");			
		div.id = callout.calloutID;

		parent.appendChild(div);
		
		var append = DrawCallout(callout.message, callout.imageUrl);
		div.appendChild(append);
		
		parent.style.height = "60px";
		
		parent.appendChild(div);
	}
}


// Remove callout
function RemoveCallout(callout)
{
	if(document.getElementById(callout.calloutID) != null)
	{
		var ctrl = document.getElementById(callout.calloutID);
		ctrl.parentNode.style.height = "auto";
		ctrl.parentNode.removeChild(ctrl);
	}	
}


// Draw the callout		
function DrawCallout(message, imageUrl)
{
	var color = "lemonchiffon";
	var fontFamily = "Arial";
	var fontSize = "10px";
	var calloutWidth = "150px";
	var calloutHeight = "50px";
	var calloutImage = imageUrl;	// "Images/alert-small2.gif";
	var calloutTextAlign = "left";
	
	var div = document.createElement("div");
	var table = document.createElement("table");
	var body = document.createElement("tbody");
	
	var tr = document.createElement("tr");
	var td1 = document.createElement("td");
	var td2 = document.createElement("td");
	var div1 = document.createElement("div");

	table.border = "0px";
	table.cellSpacing = "0px";
	table.cellPadding = "0px";
	table.style.fontFamily = fontFamily;
	table.style.fontSize = fontSize;
	
	td1.style.paddingLeft = "5px";		
	td1.style.height = calloutHeight;
	td1.style.paddingTop = "10px";
	td1.style.verticalAlign = "top";
	
	td2.style.verticalAlign = "top";
	td2.style.paddingTop = "5px";
	td2.style.paddingLeft = "5px";
	td2.style.backgroundColor = color;
	td2.style.border = "1px solid black";
	td2.style.height = calloutHeight;
	td2.style.width = calloutWidth;

	div1.style.fontSize = "1px";
	div1.style.position = "relative";
	div1.style.left = "1px";
	div1.style.borderTop = "1px solid black";
	div1.style.width = "15px";
	div1.style.textAlign = "right";
	
	for(var i = 14; i > 0; i--)
	{
		var line = document.createElement("div");
		line.style.cssFloat = "right";
		line.style.clear = "both";
		line.style.width = i.toString() + "px";
		line.style.height = "1px";
		line.style.overflow = "hidden";
		line.style.backgroundColor = color;
		line.style.borderLeft = "1px solid black";
		div1.appendChild(line);
	}

	td1.appendChild(div1);
	
	var img = document.createElement("img")
	img.border = 0;
	img.src = calloutImage;	
		
	var text = document.createElement("div");
	text.appendChild(img);
	text.style.paddingLeft = "5px";
	text.style.textAlign = calloutTextAlign;
	text.innerHTML += "&nbsp;" + message;
	td2.appendChild(text);
	
	tr.appendChild(td1);
	tr.appendChild(td2);
	
	body.appendChild(tr);
	table.appendChild(body);
	div.appendChild(table);

	return div;
}

