// This set of function are general includes for validation
// They are designed in pairs the validation and the event function
// the event function will call the validation with the event src

var sumAlert="";
var isValid=true;
var strMandatory = "Following Fields are Mandatory <br><ul>";
var strNumeric = "Following Fields Must Be a Valid Numeric <br><ul>";
var strPhone="Following Fields Must Be a Valid Phone Number<br><ul>";
var strValid="Following Fields are Not Valid<br><ul>";


        
//For tree Node Checked
			
/*var status=new Boolean(false);//For avoid recursive
function NodeChecked(treeId, nodeId, bChecked)
{		
	var selectedNode = igtree_getNodeById(nodeId);
	var childNodes = selectedNode.getChildNodes();	    	
	
	if(status==false)
	{
		if (bChecked)//Have a parentnode is checked then, Select all child nodes in webtree
		{
			checkParent(selectedNode);	
			for (n in childNodes)
			{		
			childNodes[n].setChecked(true);
			}
		}
		if (!bChecked) //Have a parentnode is un checked then, Un Select all child nodes in webtree
		{		
			uncheckParent(selectedNode);
			for (n in childNodes)
			{
			childNodes[n].setChecked(false);
			}        
		}
	}
}
function uncheckParent(nodeId) //to uncheck the parent nodes when node is uncheck 
{	
	if(nodeId.getChecked()==false)
	{	
		if(nodeId.getParent())
		{
			var doParent=new Boolean(true);
			if(nodeId.getParent())
			{
				var nw=nodeId.getParent();
			}		
			while(doParent)
			{				
				status=true;
				nw.setChecked(false);
				status=false;
				if(nw.getParent())
				{	
					nw=nw.getParent();//to move next parent
					doParent=true;
				}
				else
				{
					doParent=false;
				}			
			}			
		}
	}		
}
function checkParent(nodeId)//to check the parent when all childs are checked.
{	
	var temp=nodeId;
	while(temp.getParent())
	{
		var nw=temp.getParent();	
		var toset=temp.getParent();	
		nw=nw.getFirstChild();		
		if(nw.getChecked())
		{				
			if(nw.getNextSibling())
			{	
				while(nw.getNextSibling())
				{							
					nw=nw.getNextSibling();				
					if(nw.getChecked()==true)
					{				
						status=true;
						continue;
					}
					if(nw.getChecked()==false)
					{
						status=false;
						break;
					}
				}
			}
			else
			{
				status=true;
				toset.setChecked(true);				
				status=false;
			}
			if (status==true)
			{				
				toset.setChecked(true);				
				status=false;
			}			
		}		
		if(temp.getParent())
		{
			temp=temp.getParent();//to move next parent
		}
	}
}
*/
function resetErrorLabel()
{
document.getElementById("lblErrorMessage").innerHTML="";
}


function display_name(item) {
	var strDisplay = item.getAttribute("DisplayName");
	if (strDisplay==null || strDisplay=="")
		strDisplay="Field";
	return strDisplay;
}

function default_value(item) {
	var strDefault = item.defaultValue;
	if (strDefault==null || strDefault=="")
		strDefault="";
	return strDefault;
}

function trim_string() {
	var ichar, icount;
	var strValue = this;
	ichar = strValue.length - 1;
	icount = -1;
	while (strValue.charAt(ichar)==' ' && ichar > icount)
		--ichar;
	if (ichar!=(strValue.length-1))
		strValue = strValue.slice(0,ichar+1);
	ichar = 0;
	icount = strValue.length - 1;
	while (strValue.charAt(ichar)==' ' && ichar < icount)
		++ichar;
	if (ichar!=0)
		strValue = strValue.slice(ichar,strValue.length);
	return strValue;
}

function date_toSimpleForm() {
	var toSimpleForm = new String;
	toSimpleForm = this.toLocaleString();
	toSimpleForm = toSimpleForm.substring(0,toSimpleForm.indexOf(' '));
	return toSimpleForm;
}

function es_non_blank() {
	var item = event.srcElement;
	event.returnValue = vs_non_blank(item);
}

function vs_non_blank(item) {
    if( item == null )
    return false;
    
	item.value=item.value.Trim();
	if (item.value.length==0) {
		item.focus();
		sumAlert=" <li>" +  display_name(item) + sumAlert;
		isValid= false;
	}
}

function es_non_selected() {
	var item = event.srcElement;
	event.returnValue = vs_non_selected(item);
}

function vs_non_selected(item) {
	var strSelectItem;
	strSelectItem=item.options[item.selectedIndex].value;
	strSelectItem=strSelectItem.Trim();
	if (strSelectItem=='') {
		item.focus();
		sumAlert=" <li>" +  display_name(item) + sumAlert
		isValid= false;
	}
}
function es_Item_Length() {
	var item = event.srcElement;
	event.returnValue = vs_Item_Length(item,ItemLength);
}


function vs_Item_Length(item,ItemLength) {
	var strErrorMsg = display_name(item) + " Must be a " + ItemLength + " digits";
	item.value=item.value.Trim();
	if (item.value.length!=ItemLength) {
		item.focus();
		sumAlert= strErrorMsg + " <li>" + sumAlert
		isValid=false;
	}

}

function es_valid_MaxLength() {
	var item = event.srcElement;
	event.returnValue = vs_valid_MaxLength(item,MaxLength);
}


function vs_valid_MaxLength(item,MaxLength) {
	var strErrorMsg = display_name(item) + " Must be a less then " + MaxLength + " Characters";
	//item.value=item.value.Trim();
	if (item.value.length>MaxLength) {
		item.focus();		
		sumAlert=" <li>" +  strErrorMsg + sumAlert
		isValid=false;
	}
}



function es_valid_number() {
	var item = event.srcElement;
	event.returnValue = vs_valid_number(item);
}
function vs_valid_number(item) {
	strDefault = item.value
	var strErrorMsg = display_name(item) + " must be a valid numeric";
	//var strDefault = default_value(item);
	if (strDefault.length==0) {
		strDefault="";
		//strDefault="0";
	}
	item.value=item.value.Trim();
	if (item.value.length==0)
		item.value=strDefault;
	var num = ".0123456789";
	for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
		if (num.indexOf(item.value.charAt(intLoop)) == -1) {
			item.focus();
			sumAlert= " <li>" + display_name(item) + sumAlert
			isValid=false;
			return false;
		}
	}
	// Check only one "." will be present
	if (item.value.indexOf(".")!=item.value.lastIndexOf(".")) {				
		item.focus();
		alert(strErrorMsg);
		return false;
	}
	return true;
}

function es_valid_hours() {
	var item = event.srcElement;
	event.returnValue = vs_valid_hours(item);
}

function vs_valid_hours(item) {
	var strErrorMsg = display_name(item);
	if (!vs_valid_number(item))
		return false;
	var itemValue = new Number(item.value);
	if ((itemValue < 0 || itemValue > 80)) {
		item.focus();
		//alert(strErrorMsg + " must have a value from 0 to 80 hours");
		return false;
	}
	itemValue *= 4;
	if ((itemValue)!=Math.ceil(itemValue)) {
		item.focus();
		//alert(strErrorMsg + " must be a valid quartely increment");
		return false;
	}
	return true;
}


//   Chek For Integer(ie, without ".")
function es_valid_Integer() {
	var item = event.srcElement;
	event.returnValue = vs_valid_Integer(item);
}
function vs_valid_Integer(item) {
    if( item.value == null )
        return;
    
	strDefault = item.value
	if (strDefault.length==0) {
		strDefault="";
	}
	item.value=item.value.Trim();
	if (item.value.length==0)
		item.value=strDefault;
	var num = "0123456789";
	for (var intLoop = 0; intLoop < item.value.length; intLoop++) {
		if (num.indexOf(item.value.charAt(intLoop)) == -1) {
			item.focus();
			sumAlert= " <li>" + display_name(item) + sumAlert
			isValid=false;
			return false;
		}
	}
	
	return true;
}


function es_valid_date() {
	var item = event.srcElement;
	event.returnValue = vs_valid_date(item);
}

function vs_valid_date(item) {
	var strErrorMsg = display_name(item);
	if (isNaN(Date.parse(item.value))) {
		item.focus();
		//alert(strErrorMsg + " must be a valid Date");
		return false;
	}
	var dtItem = new Date(Date.parse(item.value));
	//item.value = dtItem.toSimpleForm();
	//item.value = dtItem.toSimpleForm();
	return true;
}


function es_item_selected() {
	var item = event.srcElement;
	event.returnValue = vs_item_selected(item);
}
function vs_item_selected(item) {
	var strErrorMsg = display_name(item) + " must be a valid selection";
	if (item.selectedIndex==0) {
		item.focus();
		//alert(strErrorMsg);
		return false;
	}
	return true;
}

function es_valid_zip() {
	var item = event.srcElement;
	event.returnValue = vs_valid_zip(item);
}
function vs_valid_zip(item) {
	var strErrorMsg = display_name(item) + " must be of the form 99999-9999";
	item.value=item.value.Trim();
	if (!(/^\d{5}$/.test(item.value) || /^\d{5}-\d{4}$/.test(item.value))) {
		item.focus();
		//alert(strErrorMsg);
		return false;
	}
	return true;
}

function es_valid_ssnbr() {
	var item = event.srcElement;
	event.returnValue = vs_valid_ssnbr(item);
}
function vs_valid_ssnbr(item) {
	var strErrorMsg = display_name(item) + " must be of the form 999-99-9999";
	item.value=item.value.Trim();
	if (!(/^\d{3}-\d{2}-\d{4}$/.test(item.value))) {
		item.focus();
		//alert(strErrorMsg);
		return false;
	}
	return true;
}

function es_valid_email() {
	var item = event.srcElement;
	event.returnValue = vs_valid_email(item);
}
function vs_valid_email(item) {
	var strErrorMsg = "Enter a valid  " + display_name(item);
	item.value=item.value.Trim();
	if (!(/^[\w\.]+@[\w\.]+[\W\.][\w\.]+$/.test(item.value))) {
		item.focus();
		sumAlert= " <li>" +  strErrorMsg;
		isValid=false;
	}
}

function es_valid_select() {
	var item = event.srcElement;
	event.returnValue = vs_valid_select(item);
}
function vs_valid_select(item) {
	var strErrorMsg = "Enter  valid  " + display_name(item);
	item.value=item.value.Trim();
	///^[\w\.]+@[a-z\.]+$/.
	if (!(/^[\w\.]+@[\w\.]+[\W\.][\w\.]+$/.test(item.value))) {
		item.focus();
		//alert(strErrorMsg);
		return false;
	}
	return true;
}


// build the validation object
function validation_setup() {
	this.eventNonBlank = es_non_blank;
	this.nonBlank = vs_non_blank;
	this.eventNonSelected =es_non_selected;
	this.nonSelected = vs_non_selected;
	this.eventLength = es_Item_Length;
	this.validLength = vs_Item_Length;	
	this.eventValidNumber = es_valid_Integer;
	this.validInteger = vs_valid_Integer;
	this.eventValidNumber = es_valid_number;
	this.validNumber = vs_valid_number;
	this.eventValidHours = es_valid_hours;
	this.validHours = vs_valid_hours;
	this.eventValidDate = es_valid_date;
	this.validDate = vs_valid_date;
	this.eventItemSelected = es_item_selected;
	this.itemSelected = vs_item_selected;
	this.eventValidZip = es_valid_zip;
	this.validZip = vs_valid_zip;
	this.eventValidSSNbr = es_valid_ssnbr;
	this.validSSNbr = vs_valid_ssnbr;
	this.eventValidEmail = es_valid_email;
	this.validEmail = vs_valid_email;
	this.eventValidSelect = es_valid_select;
	this.validSelect = vs_valid_select;	
	this.eventValidMaxLength = es_valid_MaxLength;
	this.validMaxLength = vs_valid_MaxLength;
	return this;
}

// Extend the string object to include a trim function
String.prototype.Trim = trim_string;
// Extend the date object to include a simple form string conversion
Date.prototype.toSimpleForm = date_toSimpleForm;

// Construct the validation object
var validation = new Object;
validation = validation_setup();


// This set of function are for processing the key press event
// Used to restrict input on numerics and pure textual fields

function kp_integer() {
	if ((event.keyCode < 48 || event.keyCode > 57))
		event.returnValue = false;
}
function kp_numeric() {
	if ((event.keyCode != 46) && (event.keyCode < 48 || event.keyCode > 57))
		event.returnValue = false;
	if (event.keyCode == 46) {
		if (event.srcElement.value.indexOf(".") > -1)
			event.returnValue = false;
	}
}


function kp_character() {
	if ((event.keyCode < 65 || event.keyCode > 90) && (event.keyCode < 97 || event.keyCode > 122))
		event.returnValue = false;
}
function kp_convert_upper() {
	if ((event.keyCode >= 97 && event.keyCode <= 122))
		event.keyCode -= 32;
}
function kp_convert_lower() {
	if ((event.keyCode >= 65 && event.keyCode <= 90))
		event.keyCode += 32;
}


function kp_setup() {
	this.Integer = kp_integer;
	this.Numeric = kp_numeric;
	this.Character = kp_character;
	this.ConvertUpper = kp_convert_upper;
	this.ConvertLower = kp_convert_lower;
	return this;
}

var keyPressInput = new Object;
keyPressInput = kp_setup();	

function ThumbtoLarge( ImgFileName, ImgTitle ) {
// specify window paramaters	
	photoWin = window.open( "", "photo", "width=525,height=570,status=no,scrollbars=yes,resizable=yes,left=100,top=0");
// wrote content to window
	photoWin.document.write('<html><head><title>' + ImgTitle + '</title></head>');	
	photoWin.document.write('<BODY style="background-image:url(images/bg.jpg);" ondragstart="return false" onselectstart="return false">');
	photoWin.document.write("<table height=100% align='center'><tr><td align='center'>");
	photoWin.document.write('<img src="' + ImgFileName + '" galleryimg="no"><p>');
	photoWin.document.write("</td></tr>");
	photoWin.document.write("<tr><td align='center'>");
	photoWin.document.write("<font face='verdana' size='3' color='#182eb2'><b>");	
	photoWin.document.write(ImgTitle=ImgTitle.replace('<','&lt;').replace('>','&gt;'));
	photoWin.document.write("</b></font></td></tr></table>");
	photoWin.document.write('</body></html>');
	//if (navigator.appName.substring(0,8) == "Netscape") photoWin.focus();
	photoWin.document.close();	
}

// Included on 13 Dec 2006.
function AfterNodeSelectedChange(treeId,nodeId)
{
	var type=igtree_getElementById("cboType");
	var selectedNode=igtree_getNodeById(nodeId);
	var tag =selectedNode.DataKey.ToString();
	alert(tag);
	if(tag != 0)
	{	
		type.value=tag;
	}	
}

