function URLDecode(psEncodeString) 
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " ")); 
}
function fillSchoolField()
{
	  var query = window.location.search.substring(1);
	  var parms = query.split('&');
	  for (var i=0; i<parms.length; i++) {
	     var pos = parms[i].indexOf('=');
	     if (pos > 0) {
	        var key = parms[i].substring(0,pos);
	        var val = parms[i].substring(pos+1);

	        if (key == "name") var school = URLDecode(val);
	     	if (key == "type") var school_type = URLDecode(val);
	     	if (key == "code") var school_code = URLDecode(val);
			
	     }
	  }
	  if (school_type == "High School") window.opener.document.registration_form.curhighschool.value = school_code+" - "+school;
	  else if (school_type == "College") window.opener.document.registration_form.curcollege.value = school_code+" - "+school;
	  window.close();
}
function validate_initial_special_visit_form()
{
	var numguestsfield=document.initial_special_visit_form.num_visitors;
	if (numguestsfield[numguestsfield.selectedIndex].value=="more")
	{
		document.location="visitorcenter_specialevent_contact.html";
		return false;
	}
}
function validate_registration_form()
{
 theForm = document.registration_form;
 if (theForm.first_name.value == "" || theForm.last_name.value == ""){
       theForm.first_name.focus();
       alert("Please enter both your first and last name into the form.");
       return false;
    }
 if (theForm.address1.value == "" || theForm.city.value == "" || theForm.state.value == "" || theForm.zip.value == ""){
       theForm.address1.focus();
       alert("Please enter your street address, city, state, and zip into the form.");
       return false;
    }
 // Checks if both emails are entered
 if (theForm.email.value == "" || theForm.confirm_email.value == ""){
       theForm.email.focus();
       alert("Please enter your email into both the 'Email' and 'Confirm Email' fields.");
       return false;
    }
 //ONLY VALIDATE EMAILS IF ONE IS ENTERED
 // Checks email format '@' and .
 if (theForm.email.value!="" && (theForm.email.value.indexOf("@") == -1 || theForm.email.value.indexOf(".") == -1)){
       theForm.email.focus();
       alert("Please fill in a valid e-mail address.  The correct form of an e-mail address is 'example@gsu.edu'.");
       return false;
    }
 // Checks to see if confirm email matches email
 if (theForm.email.value != theForm.confirm_email.value)
 {
  theForm.email.focus();
  //alert ("If you enter your email address, it must match the email you enter in the 'Confirm Email' field.");
  alert ("Your email address must match the email you enter in the 'Confirm Email' field.");
  return false;
 }
 if (theForm.phone_number.value == ""){
       theForm.phone_number.focus();
       alert("Please enter your primary phone number into the form.");
       return false;
    }
 if (theForm.isProspectiveStudent.value == "true")
 {
  if (theForm.birth_month.value == "" || theForm.birth_day.value == "" || theForm.birth_year.value == ""){
        theForm.birth_month.focus();
        alert("Please enter your complete date of birth into the form.");
        return false;
     }
	// Make sure user doesn't put 31 for a month that only has 30 days

	if ((theForm.birth_month.value == 4 || theForm.birth_month.value == 6 || theForm.birth_month.value == 9 || theForm.birth_month.value == 11) && theForm.birth_day.value == 31) {
		if (theForm.birth_month.value == 4) myMonth="April";
		if (theForm.birth_month.value == 6) myMonth="June";
		if (theForm.birth_month.value == 9) myMonth="September";
		if (theForm.birth_month.value == 11) myMonth="November";
		alert ("Please check your birth date.  "+myMonth+" doesn't have 31 days!");
		return false;
	}
	
	// Check for February date validity (including leap years) 
	
	if (theForm.birth_month.value == 2) {
	
	// figure out if "year" is a leap year; don't forget that
	// century years are only leap years if divisible by 400
	
	var isleap=(theForm.birth_year.value%4==0 && (theForm.birth_year.value%100!=0 || theForm.birth_year.value%400==0));
	if (theForm.birth_day.value > 29 || (theForm.birth_day.value == 29 && !isleap)) {
		alert ("Please check your birth date.  February " + theForm.birth_year.value + " doesn't have " + theForm.birth_day.value + " days!");
		return false;
	   }
	}

  // Checks to make sure high school and college codes are entered
  /*var hs = theForm.curhighschool.value;alert (1);
  if (hs!="") hscode = parseint(hs, 10);alert(2);
  if (hs == "" || isNaN(hscode))
  {alert(3);
   theForm.curhighschool.focus();
   alert ("Please enter your high school code by looking up your high school using the link to the right.");
   return false;
  }
  if (theForm.curcollege.value == "" || parseint(theForm.curcollege.value)=="NaN")
  {
   theForm.curcollege.focus();
   alert ("Please enter your college code by looking up your college using the link to the right.");
   return false;
  }*/
  
  // set var radio_choice to false
  var radio_choice = false;
  
  // Loop from zero to the one minus the number of radio button selections
  for (counter = 0; counter < theForm.entrance_type.length; counter++)
  {
   // If a radio button has been selected it will return true
   // (If not it will return false)
   if (theForm.entrance_type[counter].checked)
   radio_choice = true; 
  }
  
  if (!radio_choice)
  {
   // If there were no selections made display an alert box 
   alert("Please select your entrance type.")
   return (false);
  }
  
  if (theForm.entrance_type.value == ""){
        theForm.entrance_type.focus();
        alert("Please enter your entrance_type into the form.");
        return false;
     }
 }
 return true;
}
/////////////////////////////////////
//IMPORTED FUNCTIONS FROM INDEX.CFM//
/////////////////////////////////////
function validate_radios(radGrpName, theForm){
 if (theForm=="" || !theForm) theForm = document.visitdate_form;
 var radios = theForm.elements[radGrpName];
 //if ((radios != null && radios.length == null) || radios==null)
 if (radios != null)
 {
    if (radios.length == null)
     {
      var radio_array= new Array(1); 
      radio_array[0]=radios;
      radios=radio_array;
     }
    for (var i=0; i<radios.length; i++){
      if (radios[i].checked){
       return true;
      }
     }
 }
 if (radGrpName == "selected_day")
 {
  radname="day";
  alert ("Please select a " + radname + " before continuing.");
 }
 return false;
}
function validate_form(validate_date)
{
 var theForm = document.visit_form;
 if (theForm.num_visitors.value=="more")
 {
  document.location="visitorcenter_contact.cfm";
  return false;
 }
 /*if (validate_date=="true")
 {
  if (theForm.month[theForm.month.selectedIndex].value=="")
  {
   alert ("Please select the approximate month of your visit.");
   return false;
  }
  if (theForm.year[theForm.year.selectedIndex].value=="")
  {
   alert ("Please select the approximate year of your visit.");
   return false;
  }
 }*/
 // require that at least one checkbox be checked
 if (!theForm.iTour.checked && !theForm.bTour.checked)
 {
  //if none checked, force user to check one
  alert("Please select at least one of the tours you would like to attend.");
  return false;
 }
 if (validate_radios("prospective_student", theForm)==false)
 {
  alert ("Please select whether or not you are a prospective student.");
  return false;
 }
 theForm.send.disabled=true;
 return (true);
}
function validate_registration()
{
 var theForm = document.registration_form;
 if (theForm.first_name.value=="")
 {
  alert ("Please enter your first name.");
  theForm.first_name.focus();
  return false;
 }
 if (theForm.last_name.value=="")
 {
  alert ("Please enter your last name.");
  theForm.last_name.focus();
  return false;
 }
 if (theForm.email.value=="")
 {
  alert ("Please enter your email address.");
  theForm.email.focus();
  return false;
 }
}
function validate_allformradios(thisform) {
 for (x=0;x<thisform.elements.length;x++)
 {
  var myButton = thisform.elements[x];
  if (myButton.type == "radio")
  {
  
   var radioLength = eval("document."+thisform.name+"."+myButton.name+".length");
   myOption = -1;
   for (i=radioLength-1; i > -1; i--) {
    if (eval("document."+thisform.name+"."+myButton.name+"[i].checked")) {
     myOption = i;
    }
   }
   if (myOption == -1) {
    alert("You must select a time for each type of tour.");
    return false;
   }
  }
 }
 //thisform.sendTourTimes.disabled=true;
 return true;
}
function URLDecode(psEncodeString) 
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " ")); 
}
//  Function to open a window
function openWindow(window_url,width,height) {
   helpWin = window.open(window_url,'','toolbar=0,status=0,scrollbars=1,menubar=0,resizable=0,directories=0,location=0,width='+width+',height='+height);
   helpWin.focus();
}
function validate_grouptour_form() {
	if (document.getElementById("date1").value==""){
		document.getElementById("date1").focus();
		alert("Please choose a tour date before continuing");
		return false;
	}
	if (document.contact_welcome_center.requested_time.options[document.contact_welcome_center.requested_time.selectedIndex].value==""){
		document.contact_welcome_center.requested_time.focus();
		alert("Please choose a tour time before continuing");
		return false;
	}
	if (document.contact_welcome_center.requested_time.options[document.contact_welcome_center.requested_time.selectedIndex].value=="other" && document.getElementById("other_requested_time").value==""){
		document.getElementById("other_requested_time").focus();
		alert("Please specify your tour time before continuing");
		return false;
	}
	if (document.getElementById("organization").value==""){
		document.getElementById("organization").focus();
		alert("Please specify your organization before continuing");
		return false;
	}
	if (document.getElementById("group_description").value==""){
		document.getElementById("group_description").focus();
		alert("Please specify your group description before continuing");
		return false;
	}
	if (document.getElementById("num_students").value==""){
		document.getElementById("num_students").focus();
		alert("Please specify the number of students in your group before continuing");
		return false;
	}
	if (document.getElementById("num_chaperones").value==""){
		document.getElementById("num_chaperones").focus();
		alert("Please specify the number of chaperones in your group before continuing");
		return false;
	}
	if (document.getElementById("contact_name").value==""){
		document.getElementById("contact_name").focus();
		alert("Please specify the contact name before continuing");
		return false;
	}
	if (document.getElementById("contact_number").value==""){
		document.getElementById("contact_number").focus();
		alert("Please specify the contact number before continuing");
		return false;
	}
	if (document.getElementById("cell_num").value==""){
		document.getElementById("cell_num").focus();
		alert("Please specify your cell number before continuing");
		return false;
	}
	if (document.getElementById("fax_num").value==""){
		document.getElementById("fax_num").focus();
		alert("Please specify your fax number before continuing");
		return false;
	}
	if (document.getElementById("email").value==""){
		document.getElementById("email").focus();
		alert("Please specify your email before continuing");
		return false;
	}
	if (document.getElementById("email").value!=document.getElementById("confirm_email").value){
		document.getElementById("email").focus();
		alert("The two emails you entered do not match.");
		return false;
	}
	if (document.getElementById("address1").value=="" || document.getElementById("city").value=="" || document.getElementById("zip").value==""){
		document.getElementById("address1").focus();
		alert("Please enter your full address before continuing.");
		return false;
	}
	if (document.getElementById("location").value==""){
		document.getElementById("location").focus();
		alert("Please enter the location your group is traveling from before continuing.");
		return false;
	}
}
////////////////////////////////
//HANDLE JAVASCRIPT ERRORS
function handleError (err, url, line) {
	var agt=navigator.userAgent.toLowerCase();
	/*openWindow("http://webdb.gsu.edu/visit/visitForm/js_error.cfm?err="+err+"&url="+url+"&line="+line+"&browser="+agt,1,1)*/
	var xmlHttp=createResponseObject();
	var count=Math.floor(Math.random()*999999999999999999999);
	var errurl = "http://webdb.gsu.edu/visit/visitForm/js_error.cfm?count="+count+"&err="+err+"&url="+url+"&line="+line+"&browser="+agt;
	//alert (url);
	//xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",errurl,false);
	//xmlHttp.onreadystatechange = handleAjaxResponse;
	xmlHttp.send(null);
	return true;
}
window.onerror = handleError;
////////////////////////////////








function createResponseObject()
{
	var tempobject=GetXmlHttpObject()
	if (tempobject==null)
	{
		alert ("Browser does not support HTTP Request")
		return false;
	}
	return tempobject;
}

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.ActiveXObject)
	{
	//alert("ms");
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest)
	{
	//alert("other");
	objXMLHttp=new XMLHttpRequest()
	}
	return objXMLHttp
}