// auto tabbing for phone field starts

var phone_field_length=0;
function TabNext(obj,event,len,next_field)
{
	if(event == "down")
	{
	  phone_field_length=obj.value.length;
	}
	else if(event == "up")
	{
		if(obj.value.length != phone_field_length)
		{
			phone_field_length=obj.value.length;
			if(phone_field_length == len)
			{
				next_field.focus();
			}
		}
	}
}	// End: function TabNext(obj,event,len,next_field)

// auto tabbing for phone field ends

//document.onKeyPress = getEnterKey;
if (navigator.appName == 'Netscape') {
    window.captureEvents(Event.KEYPRESS);
    window.onKeyPress = getEnterKey;
}

function getEnterKey(keyStroke) 
{
	// when user presses 'Enter' key 
	isNetscape=(document.layers);
	keyCode = (isNetscape) ? keyStroke.which : event.keyCode;
	if (keyCode==13) 
	{
		validation();
	}

}
//  End -->


function stringvalidateusername(field)
{
	var valid="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
	for (var i=0; i<field.length; i++) 
	{
		if (valid.indexOf(field.charAt(i)) < 0) 
		{
			//alert('invalid characters');
			return false;
		}
	}
}	// End: function stringvalidateusername(field)
	
function stringvalidatepassword(field)
{
	
  var valid="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._!@#$%^&*"
			  
  for (var i=0; i<field.length; i++) 
  {
		if (valid.indexOf(field.charAt(i)) < 0) 
		{
			//alert('invalid characters');
			return false;
		}
  }
  
}
	
// validation for first or last name
function stringvalidatename(field)
{
	
  var valid="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ'."
	
  for (var i=0; i<field.length; i++) 
  {
		if (valid.indexOf(field.charAt(i)) < 0) 
		{
			//alert('invalid characters');
			return false;
		}
  }
}

function stringvalidate	(field)
{
	
  var valid="0123456789abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	
  for (var i=0; i<field.length; i++) 
  {
		if (valid.indexOf(field.charAt(i)) < 0) 
		{
			//alert('invalid characters');
			return false;
		}
  }
			  
}

function stringvalidatecity(field)
{
	
  var valid="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	
  for (var i=0; i<field.length; i++) 
  {
		if (valid.indexOf(field.charAt(i)) < 0) 
		{
			//alert('invalid characters');
			return false;
		}
  }
			  
}

	
function integersonly(field)
{
	
  var valid="0123456789"
	
  for (var i=0; i<field.length; i++) 
  {
		if (valid.indexOf(field.charAt(i)) < 0) 
		{
			//alert('invalid characters');
			return false;
		}
  }
			  
}

function countyload()
 {
	
  document.UpdateForm.action="myprofile_new.php#acounty";
  document.UpdateForm.submit();
 }

	
function validation()
{
	if(trim(document.UpdateForm.txtCurrentPassword.value)!="")
	{
		
		if(trim(document.UpdateForm.txtNewConfirmPassword.value) == "")
		{
			alert("Please confirm your password.");
			document.UpdateForm.txtNewConfirmPassword.focus();
			return false;
        }
			//alert(document.UpdateForm.txtNewPassword.value.length);
			if(document.UpdateForm.txtNewPassword.value.length < 6) 
			{
				alert("Your password must be at least 6 characters.");
				document.UpdateForm.txtNewPassword.focus();
				return false;
			}
			   
			var ret8=stringvalidatepassword(document.UpdateForm.txtNewPassword.value)
					
			if (ret8==false)
			{
				alert("Please don't include any special characters other than -._!@#%^&* in password.");
				document.UpdateForm.txtNewPassword.focus();
				return false;
			}
		
		
		//} 
	  
		if(trim(document.UpdateForm.txtNewConfirmPassword.value) != trim(document.UpdateForm.txtNewPassword.value))
		{
			alert("Please re-enter the same password.");
		    document.UpdateForm.txtNewConfirmPassword.focus();
			return false;
		}
	}

	if (document.UpdateForm.cboPrefix[document.UpdateForm.cboPrefix.selectedIndex].value == "none")
	{
	   	alert("Please select prefix.");
		document.UpdateForm.cboPrefix.focus();
		return false;
	}

	if (trim(document.UpdateForm.txtFirstName.value) == "")
	{
	   	alert("Please enter your first name.");
		document.UpdateForm.txtFirstName.focus();
		return false;
	}
  
/*	var ret = stringvalidatename(trim(document.UpdateForm.txtFirstName.value))
	if (ret==false)
	{
		alert("Please don't include any numbers/special characters other than '. in first name.");
		document.UpdateForm.txtFirstName.focus();
		return false;
	}*/
/*
	if (trim(document.UpdateForm.txtMiddleName.value) == "")
	{
	   	alert("Please enter your middle name.");
		document.UpdateForm.txtMiddleName.focus();
		return false;
	}
  */
/*	var ret = stringvalidatename(trim(document.UpdateForm.txtMiddleName.value))
	if (ret==false)
	{
		alert("Please don't include any numbers/special characters other than '. in first name.");
		document.UpdateForm.txtMiddleName.focus();
		return false;
	}
  */
	if (trim(document.UpdateForm.txtLastName.value) == "")
	{
		alert("Please enter your last name.");
		document.UpdateForm.txtLastName.focus();
		return false;
	}
 /*
	var ret1 = stringvalidatename(trim(document.UpdateForm.txtLastName.value))
	if (ret1==false)
	{
		alert("Please don't include any numbers/special characters other than '. in last name.");
		document.UpdateForm.txtLastName.focus();
		return false;
	}*/
/*  var ret1 = stringvalidatename(trim(document.UpdateForm.txtSchoolName.value))
  if (ret1==false)
	{
		alert("Please don't include any numbers/special characters other than '. in last name.");
		document.UpdateForm.txtSchoolName.focus();
		return false;
	}

  if (trim(document.UpdateForm.txtSchoolName.value) == "")
  {
   	alert("Please enter your School Name.");
	document.UpdateForm.txtSchoolName.focus();
	return false;
  }

  if (trim(document.UpdateForm.txtSchoolAddress.value) == "")
  {
   	alert("Please enter your School Address.");
	document.UpdateForm.txtSchoolAddress.focus();
	return false;
  }

  if (trim(document.UpdateForm.txtDistrictName.value) == "")
  {
   	alert("Please enter District Name.");
	document.UpdateForm.txtDistrictName.focus();
	return false;
  }
*/
  var checkbox_choices=0;

for (counter = 0; counter < document.UpdateForm.chkGrade.length; counter++)
{
    if (document.UpdateForm.chkGrade[counter].checked)
    {

     checkbox_choices = checkbox_choices + 1;
    }
}


if(checkbox_choices==0)
  {
   alert("Please select Grade Level.");
   document.UpdateForm.chkGrade[1].focus();
   return false;
  }


  if (trim(document.UpdateForm.txtSubjectArea.value) == "")
  {
   	alert("Please enter your Subject Area.");
	document.UpdateForm.txtSubjectArea.focus();
	return false;
  }
	
	 if (document.UpdateForm.cboExpertise[document.UpdateForm.cboExpertise.selectedIndex].value == 0)
  {
   	alert("Please select Year of Expertise.");
	document.UpdateForm.cboExpertise.focus();
	return false;
  }

  if (document.UpdateForm.cboUserType[document.UpdateForm.cboUserType.selectedIndex].value == 0)
  {
   	alert("Please select User Type.");
	document.UpdateForm.cboUserType.focus();
	return false;
  }
	if (trim(document.UpdateForm.txtAddress1.value) == "")
	{
		alert("Please enter your address.");
		document.UpdateForm.txtAddress1.focus();
		return false;
	}

	if (trim(document.UpdateForm.txtCity.value) == "")
	{
		alert("Please enter your city.");
		document.UpdateForm.txtCity.focus();
		return false;
	}

	var ret1b = stringvalidatecity(trim(document.UpdateForm.txtCity.value))
	if (ret1b==false)
	{
		alert("Only alphabets and spaces allowed in city field.");
		document.UpdateForm.txtCity.focus();
		return false;
	}
	// Zip code Details
	  
	if(trim(document.UpdateForm.txtZip.value) == "")
	{
	    alert("Please enter your zipcode.");
	    document.UpdateForm.txtZip.focus();
		return false;
	} 

	var strZipCode = trim(document.UpdateForm.txtZip.value);
	if(strZipCode.length>0)			        
	{
		//checks for the integer validation if any string value is present in it
		/*var ret2=integersnhiphen(strZipCode);
		if (ret2==false)
		{
			alert("Please enter only numbers for Zip code")
		    document.UpdateForm.txtZip.focus();
		    return false;
		}*/
		//checks for the existence of zip code for atleast five digits

		          
	    if(strZipCode <= 0)
		{
		    alert("Zip codes should not have all digits zero");
		    document.UpdateForm.txtZip.focus();
		    return false;
		}      		                        
				                 
	}
	// End: Zip Code

	if (document.UpdateForm.cboCountry[document.UpdateForm.cboCountry.selectedIndex].value == 0)
	{
	   	alert("Please select country.");
		document.UpdateForm.cboCountry.focus();
		return false;
	}
	else if(document.UpdateForm.cboCountry[document.UpdateForm.cboCountry.selectedIndex].value > 0 && document.UpdateForm.cboState.disabled == true)
	{
		//check to see if country is Non-US --> if so force user to enter state manually
		if (trim(document.UpdateForm.txtStateOthers.value) == "")
		{
			alert("Please specify state/province.");
			document.UpdateForm.txtStateOthers.focus();
			return false;
		}
	}
	
	if(document.UpdateForm.cboState[document.UpdateForm.cboState.selectedIndex].value == 0 && document.UpdateForm.cboCountry[document.UpdateForm.cboCountry.selectedIndex].value > 0 && document.UpdateForm.cboState.disabled == false)
	{
		alert("Please select state.");
		document.UpdateForm.cboState.focus();
		return false;
	}
	if(document.UpdateForm.txtCounty)
	{
		if(trim(document.UpdateForm.txtCounty.value) == "")
			{
				alert("Please enter your County.");
				document.UpdateForm.txtCounty.focus();
				return false;
			} 

	}
	
	 
	
	// Phone Details
	
  
	if(trim(document.UpdateForm.txtPhone.value) == "")
	{
	    alert("Please enter your phone number.");
	    document.UpdateForm.txtPhone.focus();
		return false;
	} 
	
	// Fax Details 

  	// End: Fax Details
  	
  	// Email Address Details
	if(trim(document.UpdateForm.txtEmailAddress.value) == "")
	{
		alert("Please enter your email address.");
	    document.UpdateForm.txtEmailAddress.focus();
		return false;
	}
  	// End: Email Address Details
  	
	var fieldname="Email";
	var fieldvalue=trim(document.UpdateForm.txtEmailAddress.value)
	var specialcharset="`~!|\#$%^&*():;{}[]'?/<>,+=";
	var ret6 = validemail(fieldvalue,fieldname,specialcharset)
	if (ret6 == "false")
	{
		document.UpdateForm.txtEmailAddress.focus();
		return false;
	}

  
	if(document.UpdateForm.cboCountry[document.UpdateForm.cboCountry.selectedIndex].value == "253")
	{
		//check to see if country is  'US --> CODE 253' if so clear
		document.UpdateForm.txtStateOthers.value = "";	
	}
	
	document.UpdateForm.hdFormSubmitted.value="yes";

	return true;
	
}	// End: function validation()

function showStates()
{
   if (document.UpdateForm.cboCountry.value != "0")
   {
 
	document.UpdateForm.action="myprofile_new.php?loadstate=yes";
	document.UpdateForm.submit();
   }
}



//function to trim preceding and following spaces for text fields
function trim(str)
{
	temp=" ";
		var countspcatfrnt=-1;
		var len;
		i=0;
		len=str.length;
		while(temp==" ")
		{
			temp=str.substring(i,i+1);
			countspcatfrnt+=1;//counting front spaces
			i+=1;	
		}
		temp=" ";
		var countspcatrear=-1;
		var trimvalue=""
		i=0;
		while(temp==" ")
		{
			temp=str.substring(((len-i)-1),(len-i));
			countspcatrear+=1;//counting rear spaces
			i++;
		}
		i=0;
		for(i=countspcatfrnt; i<=(len-countspcatrear-1); i++)
			trimvalue=trimvalue + str.substring(i,i+1);
				
		return trimvalue;
}

function statecheck()
{
	if(document.UpdateForm.cboCountry[document.UpdateForm.cboCountry.selectedIndex].value == "253")
	{
		document.UpdateForm.cboState.disabled = false;
		document.UpdateForm.cboCounty.disabled = false;
		document.UpdateForm.txtStateOthers.disabled = true;
		document.UpdateForm.cboState.focus();
		return true;
	}
	else
	{
		document.UpdateForm.cboState.disabled = true;
		document.UpdateForm.cboCounty.disabled = true;
		document.UpdateForm.txtStateOthers.disabled = false;
		document.UpdateForm.txtStateOthers.focus();
		return true;
	}

}

 function storegrade()
  {
	  
   var checkedval="";
    for (counter = 0; counter < document.UpdateForm.chkGrade.length; counter++)
      {
          if (document.UpdateForm.chkGrade[counter].checked)
          {
             
             if(checkedval=="")
              {
              checkedval=document.UpdateForm.chkGrade[counter].value;
              }
             else
             {
              checkedval=checkedval+","+document.UpdateForm.chkGrade[counter].value;
             }
          }
      }
	 
   document.UpdateForm.txtGrade.value=checkedval;
  
  }


  function cleanfield(obj,varval)
{

	 var valid="abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     field=obj.value;
	  for (var i=0; i<field.length; i++) 
  {
		
		  
		if (valid.indexOf(field.charAt(i)) > 0) 
		{
		
		    obj.value = "";
    	    obj.focus();
	       return true;

		}
  }

	/*if(obj.value == varval)
	{
		 alert("hello");

	
	}*/
}
function fillfield(obj,varval)
{

	if(obj.value == "")
	{
	    obj.value = varval;
	    return true;
	}
}

function formload()
{
	
    document.UpdateForm.action="myprofile_new.php#country";
	document.UpdateForm.hdFormSubmitted.value="yes1";
//	alert(document.UpdateForm.hdFormSubmitted.value);
    document.UpdateForm.submit();
 }

 function integersnhiphen(field)
{
	
  var valid="0123456789-"
	
  for (var i=0; i<field.length; i++) 
  {
		if (valid.indexOf(field.charAt(i)) < 0) 
		{
			//alert('invalid characters');
			return false;
		}
  }
			  
}

//----------------------------------------------------------------
//=======TRIM==========END==============
//----------------------------------------------------------------  
