// JavaScript Document
function ChangeState(focusfield,disfirstfield,dissecfield){
 	if(focusfield.disabled="disabled"){
		focusfield.disabled="";
   	}
   	disfirstfield.disabled="disabled";
   	dissecfield.disabled="disabled";
   	focusfield.focus();	 
}
 

function Formvalidation(frmObj,fieldId,isNull,fieldValidaion,lengthvalidation,Msg){
   	var controlObj,flag,rt;  
   	controlObj=getObject(frmObj,fieldId);   
   	//Null validation 
	if(isNull=="no"){
		flag=checkNull(controlObj,Msg);
		if((flag=="false")||(flag==false)){
			controlObj.focus();
		 	return false;		   
		}
	}
	// length Validation
  	if(lengthvalidation!=""){
	   	flag=checkLength(lengthvalidation,controlObj,Msg)
	   	if((flag=="false")||(flag==false)){
	 		controlObj.focus();
			return false;
	   	}
   	}
	// other fieldvalidation
	if (fieldValidaion!=""){
  		flag=validateValue(fieldValidaion,controlObj,Msg)
	 	if((flag=="false")||(flag==false)){
			controlObj.focus();
			return false;
	 	}
	}
	return true;
}

function checkLength(length,controlObj,Msg){
	if (controlObj.value.length > length){
		alert(Msg +" Value Should not More then "+length )
	   	return false;
   	}
}

function checkNull(field,alerttxt){
//	with (field)
	//alert(field.value.charAt(0)==" ");
	//alert(trim(field.value));
	//alert(fiel);
	field.value=trim(field.value);
	if (field.value==null||field.value==""||field.value.charAt(0)==" "){
		alert("Please Fill "+alerttxt);
		var aa;
		aa="false";
		return aa;
	}

}
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}

//
//function trimAll(sString)
//{
//	var sString;
//	sString.replace(/(^\s*)|(\s*$)|(^\n*)|(\n*$)/g, ""); 
//	return sString;
//}

function getObject(frmObj,fieldId){
	var cntrlobj
	cntrlobj=frmObj[fieldId];
	//cntrlobj=document.getElementById(fieldId)
	return cntrlobj;
}


function validateValue(fieldValidaion,controlObj,Msg){
	switch(fieldValidaion){
		case "char":
	    	flag=checkchar(controlObj,Msg)
			if((flag=="false")||(flag==false))
	 			return false;
			break; 
	 	case "email":
	    	flag=validate_email(controlObj,Msg)
			if((flag=="false")||(flag==false))
	 			return false;
			break;	
		case "numeric":
	    	flag=checknumeric(controlObj,Msg)
			if((flag=="false")||(flag==false))
            	return false;
       		break;
	}
}

function validate_email(field,alerttxt){
	with (field){
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		if (apos<1||dotpos-apos<2){
			alert("Not a valid e-mail address!");
			return false;
		}
	}
}

function checknumeric(field,alerttxt){
	if(isNaN(field.value)){
	    alert(alerttxt+" Must be Numeric");
		return false;
   	}		  
}

function checkchar(field,alerttxt){
	if(!(isNaN(field.value))){
		alert(alerttxt+" Should not Numeric");
	  	return false;
  	}  
}

//function submitForm(formObj,formAction,formMethod,Haction){ 
//	var formObje
//	formObje=document.getElementById(formObj)
//	//alert(formObje)
//	formObje.Haction.value=Haction
//	alert(Haction + " " + formObje.Haction.value);
//	formObje.action=formAction;
//	formObje.method=formMethod;
//	// alert(formObj.method)
//	formObje.submit(); 
//}

function getLength(objId,count){
	var userInput;
  	userInput=objId.value;
  	if (userInput.length > count){
		alert("Can't Enter More Than "+ count + " char ");
		objId.value=objId.value.substring(0,count-1)
		// return false;
	}else{
		//objId.value=objId.value.substring(0,objId.value.length);
			return true;
	}
}

function setOtherValue(obj,label){
	var org;
	if(obj.value.toLowerCase()=="other")
	{
		org=prompt("Please Specify the Name Of "+label+ ' State','');
		if(org==null || org=="" || org.length <1)
		{
			obj.options[0].selected=true
		}
		else
		{
			obj.options[0].text=org;
			obj.options[0].value=org;
			obj.options[0].selected=true;
		}
	}
}

var keybDecimal = new keybEdit('01234567890.','Decimal input only.');
var keybDecimalNM = new keybEdit('01234567890.');
var keybNumeric = new keybEdit('01234567890','Numeric input only.');
var keybNumericNM = new keybEdit('01234567890');
function keybEdit(strValid, strMsg) {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function is to be a constructor for
						the keybEdit object.  keybEdit objects are used by the
						function editKeyBoard to determine which keystrokes are
						valid for form objects.  In addition, if an error occurs,
						they provide the error message.
						
						Please note that the strValid is converted to both
						upper and lower case by this constructor.  Also, that
						the error message is prefixed with 'Error:'.
						
						The properties for this object are the following:
							valid	=	Valid input characters
							message	=	Error message
							
						The methods for this object are the following:
							getValid()	=	Returns a string containing valid
											characters.
							getMessage()=	Returns a string containing the
											error message.

		Update Date:	Programmer:			Description:
	*/

	//	Variables
	var reWork = new RegExp('[a-z]','gi');		//	Regular expression\

	//	Properties
	if(reWork.test(strValid))
		this.valid = strValid.toLowerCase() + strValid.toUpperCase();
	else
		this.valid = strValid;

	if((strMsg == null) || (typeof(strMsg) == 'undefined'))
		this.message = '';
	else
		this.message = strMsg;

	//	Methods
	this.getValid = keybEditGetValid;
	this.getMessage = keybEditGetMessage;
	
	function keybEditGetValid() {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function act as the getValid method
						for the keybEdit object.  Please note that most of the
						following logic is for handling numeric keypad input.

		Update Date:		Programmer:			Description:
	*/

		return this.valid.toString();
	}
	
	function keybEditGetMessage() {
	/*	Function:		keybEdit
		Creation Date:	October 11, 2001
		Programmer:		Edmond Woychowsky
		Purpose:		The purpose of this function act as the getMessage method
						for the keybEdit object.

		Update Date:	Programmer:			Description:
	*/
	
		return this.message;
	}
}

function editKeyBoard(objForm, objKeyb,evn) {
/*	Function:		editKeyBoard
	Creation Date:	October 11, 2001
	Programmer:		Edmond Woychowsky
	Purpose:		The purpose of this function is to edit edit keyboard input
					to determine if the keystrokes are valid.

	Update Date:		Programmer:			Description:
*/
	var keyC
	//alert(navigator.appName)
	if (!evn.keyCode){keyC=evn.which}
	else{keyC=evn.keyCode}
	if (evn.charCode==0){return true;}
		strWork = objKeyb.getValid();
		strMsg = '';							// Error message
		blnValidChar = false;					// Valid character flag

		// Part 1: Validate input
		if(!blnValidChar)
			for(i=0;i < strWork.length;i++)
				if(keyC == strWork.charCodeAt(i)) {
					blnValidChar = true;
					break;
				}

		// Part 2: Build error message
		if(!blnValidChar){
			if(objKeyb.getMessage().toString().length != 0)
				alert('Error: ' + objKeyb.getMessage());

			return  false;		// Clear invalid character
			objForm.focus();						// Set focus
		}
}
 var stateValues = new Array();
function CheckCountry(objValue,comboobj)
{
	var i,j;
	if (objValue!="US")
	{
		
		for(i=comboobj.options.length-1,j=0;i>=1;i--)
		{
			if(comboobj.options[i].text.toLowerCase()!="other")
			{
				stateValues[j]=comboobj.options[i].text + "|" + comboobj.options[i].value
				comboobj.remove(i);
				j++;
			}
		}
		 comboobj.options[comboobj.options.length-1].selected=true;
		 if (objValue!="") setOtherValue(comboobj,"Other");
	}
	else
	{
		
		for(i=stateValues.length-1;i>=1;i--)
			{
				var comt=new Array();				
				comt=stateValues[i].split("|")			
				addOption(comboobj,comt[0],comt[1])
			}	
		comboobj.options[0].text="Select"
		comboobj.options[0].value=""
	}
	
}

function addOption(selectbox,text,value )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}
