function toggleEnabled(strElem) {
/*
	Purpose:	Toggles the enabled/disabled state of form elements
	Inputs:		strElem 	- string, the element that the window will be open next to
					Sample: 'frmTest.btnSubmit' -- element(s) that match name
							'frmTest.all' -- all elements on form
	Outputs:	None
	Notes:		None
	
	Date		Author			Description
	06/02/2000	Michael Bartha	Created function
*/
	var objElem;
	objElem = eval("document." + strElem);
	if(isNaN(objElem.length)) {
		if(objElem.disabled) {
			objElem.disabled = false;
		}
		else {
			objElem.disabled = true;
		}
	}
	else {
		if(objElem.type) {
			if((objElem.type).indexOf("select",0) >= 0) {
				//treat as single
				if(objElem.disabled) {
				objElem.disabled = false;
				}
				else {
					objElem.disabled = true;
				}
			}
		}
		else {
			for(x=0;x < objElem.length; x++) {
				if(objElem[x].disabled) {
					objElem[x].disabled = false;
				}
				else {
					objElem[x].disabled = true;
				}
			}
		}
	}
}

function openWindow(objSrc,strOptions,strURL) {
	/*
	Purpose:	Opens a new window
	Inputs:		objSrc 		- object, the element that the window will be open next to
				strOptions 	- string, represents the options for a window
					Sample: 'width=500,height=150,scrollbars=yes,menubar=no,location=no,resizable=yes'
				strURL		- string, the URL to open
					Sample: 'addnew.asp?tbl=country&prm=country,name&max=3,30&src=frmSearch.country'
	Outputs:	newly created window
	Notes:		None
	
	Date		Author			Description
	05/10/2000	Michael Bartha	Created function
	*/
	x = objSrc.offsetLeft+76;
	y = objSrc.offsetTop+150;
	w = objSrc.offsetWidth;
	h = objSrc.offsetHeight;
	//alert("x="+x+"|y="+y+"|w="+w+"|h="+h);
	strPos = "left=" + (x+w) + ",top=" + (y+h) + ",";
	strOptions = strPos + strOptions;
	if(strURL != "") {
		newwin = window.open(strURL,"",strOptions);
	}
}

function copyitem(strSource,strTarget) {
	/*
	Purpose:	Creates a new option in a select element
	Inputs:		strSource 	- string, the comma delimited value of item to be inserted
							the 0 position has the value, 1 has the view name
				strTarget 	- string, a string that represents the form name 
							delimited by a "." followed by the element name
	Outputs:	newly created option is selected in element
	Notes:		None
	
	Updates:	Date		Author			Description
				05/10/2000	Michael Bartha	Created function
	*/
	var arrSource
	arrSource = strSource.split(",");
	var objBoxTarget
	objBoxTarget = eval("window.document." + strTarget);
	//alert(objBoxTarget.options.length);
	//0 position hold the value
	//1 position holds the viewed name
	//search for existing value
	if (!exists(arrSource[0],objBoxTarget)) {
		//alert("inserting");
		objoption = new Option(arrSource[1],arrSource[0],false,false);
		objBoxTarget.options[objBoxTarget.options.length] = objoption;
	}
	//select newly inserted item
	objBoxTarget.options[objBoxTarget.options.length-1].selected = true;
	
}

function exists(strItem,objBox) {
	/*
	Purpose:	Check for the existence of item in select box
	Inputs:		strItem	- string, the value of item to be inserted
				objBox 	- object, the select object to check
	Outputs:	true or false
	Notes:		None
	
	Updates:	Date		Author			Description
				05/10/2000	Michael Bartha	Created function
	*/
	//alert("Item: " + strItem);
	for(var i = 0; i< objBox.options.length;i++) {
		//alert("Match: " + item + " | " + objBox.options[i].value);
		if (objBox.options[i].value == strItem) {
			//match found
			return true;
		}
	}
	return false;
}

function maskEdit(objSrc,strMask) {
	/*
	Purpose:	Mask edit for textboxes
	Inputs:		objSrc 	- object, the element that the mask is applied to
				strMask - string, the mask string
	Outputs:	True or false to either pass through the input or not
				Changes contents of eSrc
	Notes:		Valid mask characters are:
					A for uppercase alpha
					a for lowercase alpha
					0 for numeric
				anything else is copied straight through
				Sample: Postal Code 'A0A-0A0'
						Phone		'000 000-0000'
				add to input onkeydown="return maskEdit(this,'000 000-0000');"
						
	Updates:	Date		Author			Description
				05/10/2000	Michael Bartha	Created function
	*/
	var ascCH = event.keyCode;
	//alert(ascCH);
	var strTemp = "";
	var strOutput = "";
	var strInput = objSrc.value;
	var intInputLength = strInput.length;
	var strCH = String.fromCharCode(ascCH);
	//alert(intInputLength);
	//get mask character at character position
	mskCH = strMask.substring(intInputLength,intInputLength+1);
	switch (mskCH) {
		case "A" :
			//uppercase letters only
			//ascii code 65 to 90
			if((ascCH >= 65) && (ascCH <= 90)) {
				//valid concat to output
				strOutput += strCH;
			}
			if((ascCH >= 97) && (ascCH <= 122)) {
				//valid concat to output
				strOutput += String.fromCharCode(ascCH - 32);
			}
			break;
		case "a" :
			//uppercase letters only
			//ascii code 97 to 122
			if((ascCH >= 97) && (ascCH <= 122)) {
				//valid concat to output
				strOutput += strCH;
			}
			if((ascCH >= 65) && (ascCH <= 90)) {
				//valid concat to output
				strOutput += String.fromCharCode(ascCH + 32);
			}
			break;
		case "0" :
			//numbers only
			//ascii code 48 to 57
			//if(((ascCH >= 48) && (ascCH <= 57)) || ((ascCH >= 96) && (ascCH <= 105)) ) {
			if((ascCH >= 48) && (ascCH <= 57) ) {
				//valid concat to output
				strOutput += strCH;
			}
			
			if((ascCH >= 96) && (ascCH <= 105) ) {
				//valid concat to output
				 
				strOutput += String.fromCharCode(ascCH - 48);
			}
			break;
		default :
				//pass through copy mask char to output
				strOutput += mskCH;
	}
	//get mask character at character position + 1 ie look ahead
	blnMatch = false;
	for(i=intInputLength + 1;i < strMask.length;i++) {
		mskCH1 = strMask.substring(i,i+1);		
		switch (mskCH1) {
			case "A" :
				blnMatch = true;
				break;
			case "a" :
				blnMatch = true;
				break;
			case "0" :
				blnMatch = true;
				break;
			default :
				//pass through copy mask char to output
				strOutput += mskCH1;
		}
		if(blnMatch) break;
	}
	//ascii codes of characters to pass through
	if ((ascCH != 8) && (ascCH != 9) && (ascCH != 13) && (ascCH != 35) && (ascCH != 36)
		&& (ascCH != 37) && (ascCH != 38) && (ascCH != 39) && (ascCH != 40)){
		objSrc.value = strInput  + strOutput;
		return false;
	}
	else {
		return true;
	}
}

function IsDirty(eForm)
{   

/*
Purpose:       Checks if any fields of a form's section has been changed.Form is divided
               into sections.Each section has a hidden field.The prefixes of hidden and other
               fields must be same.If any fields of a section has been changed,the value of 
               hidden field is set to True
Inputs:        eForm a form object
Outputs:       False if nothing changed,True if anything changed  
Errors:        
Notes:         


Updates:                Date                   Author                      Description
                        04/20/2000             Michael Bartha              created function
                        04/20/2000             Ramazan Aksoy               added checking for each section
                       
*/
    //a boolean to keep track of changing in the form
	var isFormDirty=false;
	//total number of form elements
	//var eForm = document.forms[eSrc];
	var iNumElems = eForm.elements.length;
	
	//Check each element of form
	for (var i=0; i<iNumElems; i++) {
	   	var eElem = eForm.elements[i];
	   	//Check text and textarea fields
		if ("text" == eElem.type || "TEXTAREA" == eElem.tagName) {
			if (eElem.value != eElem.defaultValue) 
			{  
			   //if changed get prefix of the field
			   var section=(eElem.name).substring(0,(eElem.name).indexOf("_",0));
			   
			   //alert(eElem.name);
			   //find the name of hidden field for this section
			   var isection=section.concat("_IsDirty");
			   //set value of hidden field to true
			   
			   eForm.elements[isection].value="true";
			   
			   //form is changed
			   isFormDirty=true;			   	   
			}
		}
		else if ("checkbox" == eElem.type || "radio" == eElem.type) {
			if (eElem.checked != eElem.defaultChecked) 
			{
			  //if changed get prefix of the field
			   var section=(eElem.name).substring(0,(eElem.name).indexOf("_",0));
			   //alert(eElem.name);
			   //find the name of hidden field for this section
			   var isection=section.concat("_IsDirty");
			   //set value of hidden field to true
			   eForm.elements[isection].value="true";
			   //form is changed
			   isFormDirty=true;		
			}
		}
		else if ("SELECT" == eElem.tagName) {
			var cOpts = eElem.options;
			var iNumOpts = cOpts.length;
			for (var j=0; j<iNumOpts; j++) {
				var eOpt = cOpts[j];
				
				if (eOpt.selected != eOpt.defaultSelected)
				{
					//alert ("selected:"+eOpt.selected+
					//	"\ndefault selected:"+eOpt.defaultSelected
					//	+"\n"+eElem.name);					
				  //if changed get prefix of the field
			      var section=(eElem.name).substring(0,(eElem.name).indexOf("_",0));
			      //alert(eElem.name);
				  //find the name of hidden field for this section
				 var isection=section.concat("_IsDirty");
			      //set value of hidden field to true
			      eForm.elements[isection].value="true";
			      //form is changed
			      isFormDirty=true;		
			      
			    }
			}
		}
	}
	
	//Check if form has been changed
	if(isFormDirty)
	{
	  //if yes,return true
	  return true;
	}else{
	  //if no,return no
	  alert("No changes detected, nothing to update.");
	  return false;
	  
	}
}


