function IsEmpty(aTextField) {
	if ((aTextField.value.length==0) ||
 		(aTextField.value==null)) {
    	return true;
 		}
 	else { return false; }
}

function CheckIfTextIsEmpty(str, msg) {
 	if (str == '') {
  	return msg;
 	}
	return '';
}

function checkDropdown(choice, msg) {
  if (choice == 0) {
     return msg;
  }
	return '';
}

function CheckIfPhoneIsLegit(str) {
 	if (str == '') {
  	return "Please enter a phone number.<br>";
 	}
 	
 	var stripped = str.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	   return "The phone number contains illegal characters.<br>";
	}
 	
	return '';
}

function CheckEmail (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) 
	{
		// alert("Email address seems incorrect (check @ and .'s)");
		return "Invalid e-mail address.<br>";
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) 
	{
		if (user.charCodeAt(i)>127) 
		{
			// alert("Ths username contains invalid characters.");
			return "Invalid e-mail address.<br>";
		}
	}
	for (i=0; i<domain.length; i++) 
	{
		if (domain.charCodeAt(i)>127) 
		{
			// alert("Ths domain name contains invalid characters.");
			return "Invalid e-mail address.<br>";
		}
	}

	if (user.match(userPat)==null) 
	{
		// alert("The username doesn't seem to be valid.");
		return "Invalid e-mail address.<br>";
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) 
	{
		for (var i=1;i<=4;i++) 
		{
			if (IPArray[i]>255) 
			{
				// alert("Destination IP address is invalid!");
				return "Invalid e-mail address.<br>";
			}
		}
		return '';
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) 
	{
		if (domArr[i].search(atomPat)==-1) 
		{
			// alert("The domain name does not seem to be valid.");
			return "Invalid e-mail address.<br>";
		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) 
	{
		// alert("The address must end in a well-known domain or two letter " + "country.");
		return "Invalid e-mail address.<br>";
	}

	if (len<2) 
	{
		// alert("This address is missing a hostname!");
		return "Invalid e-mail address.<br>";
	}

	return '';
}

function remove_non_numeric(o)
{
	o.value=o.value.replace(/([^0-9])/g,'');
}

function remove_bad_email_chars(o)
{
	if(o.value.length>0)
		o.value=o.value.replace(/[^a-z0123456789_.@-A-Z]+/g, '').toLowerCase();
}

function remove_non_alphanumeric(o)
{
	if(o.value.length>0)
	o.value=o.value.replace(/[^a-z0123456789_A-Z]+/g, '').toLowerCase();
}

function Validate_Form(group_id, include_payment) {
  var why = '';
  var why_contact = '';
  var why_school = '';
  var why_district = '';
  var why_library = '';
  var why_billing = '';
  var why_payment = '';
  // USER AND CONTACT INFORMATION
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.username.value, "Please enter a username.<br>")
  var usr_mess = document.getElementById("username_message").innerHTML
  if(usr_mess.length > 40)
  {
  	why_contact = why_contact + "Username already exists. Please choose another.<br>"
  }
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.pass.value, "Please enter a password.<br>")
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.firstname.value, "Please enter a first name.<br>")
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.lastname.value, "Please enter a last name.<br>")
  why_contact = why_contact + CheckIfPhoneIsLegit(document.frm_details.phone.value)
  why_contact = why_contact + CheckEmail(document.frm_details.email.value)
  why_contact = why_contact + checkDropdown(document.frm_details.referred_by.value, "Please select referrer.<br>")
  // END OF USER AND CONTACT INFORMATION
  
  // SCHOOL INFORMATION
  if ((group_id==1)||(group_id==20)||(group_id==6)||(group_id==16)) // School=1, Virtual School Access=20, lab=6, Teacher Access=16
  {
	  why_school = why_school + checkDropdown(document.frm_details.school_country.value, "Please select your country.<br>")
	  why_school = why_school + CheckIfTextIsEmpty(document.frm_details.school_zip.value, "Please enter a zip code.<br>")
	  if (document.frm_details.school_name.value == 'other')
	  {
	  	why_school = why_school + CheckIfTextIsEmpty(document.frm_details.school_name_man.value, "Please enter your school's name.<br>")
	  }
		else
		{
			why_school = why_school + checkDropdown(document.frm_details.school_name.value, "Please enter your school's name.<br>")
		}
		why_school = why_school + CheckIfTextIsEmpty(document.frm_details.school_address.value, "Please enter your school's address.<br>")
		why_school = why_school + CheckIfTextIsEmpty(document.frm_details.school_city.value, "Please enter your school's city.<br>")

		if (document.frm_details.school_state != undefined)
	  {
	  	why_school = why_school + checkDropdown(document.frm_details.school_state.value, "Please select your schools state.<br>")
	  }
	  
		why_school = why_school + CheckIfPhoneIsLegit(document.frm_details.school_phone.value)
	}
  // END OF SCHOOL INFORMATION
  
  // DISTRICT INFORMATION
  if (group_id==11) // District=11
  {
	  why_district = why_district + checkDropdown(document.frm_details.district_country.value, "Please select your country.<br>")
	  why_district = why_district + CheckIfTextIsEmpty(document.frm_details.district_zip.value, "Please enter a zip code.<br>")
		why_district = why_district + checkDropdown(document.frm_details.district_name.value, "Please enter your district's name.<br>")
		why_district = why_district + CheckIfTextIsEmpty(document.frm_details.district_address.value, "Please enter your district's address.<br>")
		why_district = why_district + CheckIfTextIsEmpty(document.frm_details.district_city.value, "Please enter your district's city.<br>")
	  if (document.frm_details.district_state != undefined)
	  {
	  	why_district = why_district + checkDropdown(document.frm_details.district_state.value, "Please select your district's state.<br>")
	  }
		why_district = why_district + CheckIfPhoneIsLegit(document.frm_details.district_phone.value)
	}
  // END OF DISTRICT INFORMATION
  
  // PUBLIC LIBRARY INFORMATION
  if (group_id==17) // Library=17
  {
	  why_library = why_library + checkDropdown(document.frm_details.library_country.value, "Please select your country.<br>")
	  why_library = why_library + CheckIfTextIsEmpty(document.frm_details.library_zip.value, "Please enter a zip code.<br>")
		why_library = why_library + checkDropdown(document.frm_details.library_name.value, "Please enter your library's name.<br>")
		why_library = why_library + CheckIfTextIsEmpty(document.frm_details.library_address.value, "Please enter your library's address.<br>")
		why_library = why_library + CheckIfTextIsEmpty(document.frm_details.library_city.value, "Please enter your library's city.<br>")
		 if (document.frm_details.library_state != undefined)
	  {
	  	why_library = why_library + checkDropdown(document.frm_details.library_state.value, "Please select your library's state.<br>")
	  }
		why_library = why_library + CheckIfPhoneIsLegit(document.frm_details.library_phone.value)
	}
  // END OF PUBLIC LIBRARY INFORMATION
  
  // BILLING INFORMATION
  why_billing = why_billing + CheckIfTextIsEmpty(document.frm_details.bill_first_name.value, "Please enter a first name for billing.<br>")
  why_billing = why_billing + CheckIfTextIsEmpty(document.frm_details.bill_last_name.value, "Please enter a last name for billing.<br>")
  why_billing = why_billing + checkDropdown(document.frm_details.bill_country.value, "Please select your country.<br>")
  if (document.frm_details.bill_country.value == 'USA')
  {
  	why_billing = why_billing + checkDropdown(document.frm_details.bill_state.value, "Please select a billing state.<br>")
  }
  why_billing = why_billing + CheckIfTextIsEmpty(document.frm_details.bill_address.value, "Please enter your billing address.<br>")
  why_billing = why_billing + CheckIfTextIsEmpty(document.frm_details.bill_city.value, "Please enter your city.<br>")
  why_billing = why_billing + CheckIfPhoneIsLegit(document.frm_details.bill_phone.value)
  why_billing = why_billing + CheckEmail(document.frm_details.bill_email.value)
  why_billing = why_billing + CheckIfTextIsEmpty(document.frm_details.bill_zip.value, "Please enter a zip code for billing.<br>")
  // END OF BILLING INFORMATION
  
  // PAYMENT INFO
  if(include_payment==true)
  {
	  why_payment = why_payment + CheckIfTextIsEmpty(document.frm_details.cardname.value, "Please enter the name on the credit card.<br>")
	  why_payment = why_payment + CheckIfTextIsEmpty(document.frm_details.cardnumber.value, "Please enter the card number.<br>")
	  why_payment = why_payment + checkDropdown(document.frm_details.cardtype.value, "Please select the card type.<br>")
	  why_payment = why_payment + checkDropdown(document.frm_details.cardexpiremonth.value, "Please select the expiration month.<br>")
	  why_payment = why_payment + checkDropdown(document.frm_details.cardexpireyear.value, "Please select the expiration year.<br>")
	  why_payment = why_payment + checkDropdown(document.frm_details.csc.value, "Please enter the card security code.<br>")
	  // END OF PAYMENT INFO
	}
  
  if (document.frm_details.terms.checked==false)
  {
  	why = why + "Please read and accept the Terms and Conditions.<br>"
  }
  
  if ((why != '')||(why_contact != '')||(why_school != '')||(why_billing != '')||(why_payment != '')||(why_district != '')) {
   var status = "<div style=\"color: red;\"><b>PLEASE CHECK THE FOLLOWING:</b><br><br>"
   if (why_contact != ''){
   		status = status + "<b>CONTACT</b><br>" + why_contact + "<br>"
   }
   if ((group_id==1)||(group_id==20)||(group_id==6)||(group_id==16)) // School=1, Virtual School Access=20, lab=6, Teacher Access=16
   {
	   if (why_school != ''){
	   		status = status + "<b>SCHOOL</b><br>" + why_school + "<br>"
	   }
	 }
	 if (group_id==11) // District=11
   {
	   if (why_district != ''){
	   		status = status + "<b>DISTRICT</b><br>" + why_district + "<br>"
	   }
	 }
	 if (group_id==17) // Library=17
   {
	   if (why_library != ''){
	   		status = status + "<b>PUBLIC LIBRARY</b><br>" + why_library + "<br>"
	   }
	 }
   if (why_billing != ''){
   		status = status + "<b>BILLING</b><br>" + why_billing + "<br>"
   }
   if(include_payment==true){
	   if (why_payment != ''){
	   		status = status + "<b>PAYMENT</b><br>" + why_payment + "<br>"
	   }
	 }
   if (why != ''){
   		status = status + "<b>TERMS AND CONDITIONS</b><br>" + why + "<br>"
   }
   var status = status + "</div>"
   document.getElementById("status_place_holder").style.display = '';
   document.getElementById("status").innerHTML = status;
   return false;
  }
	return true;
}

function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

function checkNumber(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);
 

  /* Check if the keyed in character is a number
     do you want alphabetic UPPERCASE only ?
     or lower case only just check their respective
     codes and replace the 48 and 57 */

  if (cCode < 48 || cCode > 57 ) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}

function Validate_Trial_Form() {
  var why_contact = '';
  // USER AND CONTACT INFORMATION
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.username.value, "Please enter a username.<br>")
  var usr_mess = document.getElementById("username_message").innerHTML
  if(usr_mess.length > 40)
  {
  	why_contact = why_contact + "Username already exists. Please choose another.<br>"
  }
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.pass.value, "Please enter a password.<br>")
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.firstname.value, "Please enter a first name.<br>")
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.lastname.value, "Please enter a last name.<br>")
  //why_contact = why_contact + CheckIfPhoneIsLegit(document.frm_details.phone.value)
  why_contact = why_contact + CheckEmail(document.frm_details.email.value)
  if (document.frm_details.bill_state != undefined)
    {
    	why_contact = why_contact + checkDropdown(document.frm_details.bill_state.value, "Please select your state.<br>")
  }
  why_contact = why_contact + checkDropdown(document.frm_details.referred_by.value, "Please select referrer.<br>")
  why_contact = why_contact + checkDropdown(document.frm_details.user_type.value, "Please select what best describes you.<br>")
  // END OF USER AND CONTACT INFORMATION
  //if (document.frm_details.terms.checked==false)
  {
  	//why_contact = why_contact + "Please read and accept the Terms and Conditions.<br>"
  }
  
  if (why_contact != '') {
   var status = "<div style=\"color: red;\"><b>PLEASE CHECK THE FOLLOWING:</b><br><br>"
   if (why_contact != ''){
   		status = status + why_contact + "<br>"
   }
   var status = status + "</div>"
   document.getElementById("status_place_holder").style.display = '';
   document.getElementById("status").innerHTML = status;
   return false;
  }
	return true;
}

function Validate_Trial_Form_5_Day() {
  var why_contact = '';
  // USER AND CONTACT INFORMATION
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.username.value, "Please enter a username.<br>")
  var usr_mess = document.getElementById("username_message").innerHTML
  if(usr_mess.length > 40)
  {
  	why_contact = why_contact + "Username already exists. Please choose another.<br>"
  }
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.password.value, "Please enter a password.<br>")
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.first_name.value, "Please enter a first name.<br>")
  why_contact = why_contact + CheckIfTextIsEmpty(document.frm_details.last_name.value, "Please enter a last name.<br>")
  why_contact = why_contact + CheckEmail(document.frm_details.email.value)
  if (document.frm_details.email.value != document.frm_details.email2.value)
  {
  	why_contact = why_contact + 'Your email addresses do not match<br>'; 
  }
  if (document.frm_details.country != undefined)
      {
      	why_contact = why_contact + checkDropdown(document.frm_details.country.value, "Please select your country.<br>")
  }
  if (document.frm_details.state != undefined)
    {
    	why_contact = why_contact + checkDropdown(document.frm_details.state.value, "Please select your state.<br>")
  }
  // END OF USER AND CONTACT INFORMATION
  /*
  if (document.frm_details.terms.checked==false)
  {
  	why_contact = why_contact + "Please read and accept our privacy policy and our user agreement.<br>"
  }
  */
  if (why_contact != '') {
   var status = "<div style=\"color: red;\"><b>PLEASE CHECK THE FOLLOWING:</b><br><br>"
   if (why_contact != ''){
   		status = status + why_contact + "<br>"
   }
   var status = status + "</div>"
   document.getElementById("status_place_holder").style.display = '';
   document.getElementById("status").innerHTML = status;
   return false;
  }
	return true;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}