// JavaScript Document

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
 	} catch (e) {
		// Internet Explorer
  		try{
	    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}	
	return xmlHttp;
}

function showPage(str) { 	
	xmlHttp1=GetXmlHttpObject()
	
	if (xmlHttp1==null) {
		alert ("Your browser does not support AJAX!");
  		return;
  }
	
	getURL=(str == 1)?	"banner1.php" : "banner2.php"; 
	xmlHttp1.onreadystatechange=showBanner;
	xmlHttp1.open("GET",getURL,true);
	xmlHttp1.send(null);
	
	if (str == 1)
		getURL="home.php";
	else if (str == 2)
		getURL="culinary_challenge.php";
	else if (str == 3)
		getURL="schedule.php";
	else if (str == 4)
		getURL="education.php";
	else if (str == 5)
		getURL="registration.php";
	else if (str == 6)
		getURL="attraction.php";
	else if (str == 7)
		getURL="committees.php";
	else if (str == 8)
		getURL="hotel.php";
	else if (str == 9)
		getURL="sponsorship.php";
	else if (str == 10)
		getURL="vendor.php";
	else if (str == 11)
		getURL="contactus.php";
	else if (str == 12)
		getURL="paynow.php";
	else if (str == 13)
		getURL="regist_online.php";
		
	xmlHttp2=GetXmlHttpObject()
	xmlHttp2.onreadystatechange=stateChanged;
	xmlHttp2.open("GET",getURL,true);
	xmlHttp2.send(null);
}

function showBanner() { 
	if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete") { 
		document.getElementById("banner").innerHTML=xmlHttp1.responseText;
	}
}

function stateChanged() { 
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete") { 
		document.getElementById("middle_pannel").innerHTML=xmlHttp2.responseText;
	}
}

function getForm(doWhat) { 	
	var userEmail = document.getElementById("userEmail");
	var userPass = document.getElementById("userPass");
	if (doWhat == "getForm") {
		if(!emailCheck(userEmail)) {
			userEmail.focus();
			return false;
		}
			
		if (userPass.value.length == 0) {
			return errorMsg(userPass, "Password");
		}
	}
	
	var values = new Array();
	values.push(doWhat);
	values.push(userEmail.value);
	values.push(userPass.value);
	
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
  	return;
  }

	getURL="_form2.php?values="+values;
	xmlHttp.onreadystatechange=showForm;
	xmlHttp.open("GET",getURL,true);
	xmlHttp.send(null);
}

function showForm() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("formField").innerHTML=xmlHttp.responseText;
	}
}

function printPage(num) {
	var pageURL = "";
	if (num==1)
		pageURL = "schedule.php?q=1";
	
	window.open(pageURL, "_blank");
}

function checkValue(thisForm) {
	
	if(!emailCheck(thisForm.email)) {
		thisForm.email.focus();
		return false;
	}
	
	if (thisForm.message.value.length == 0) {
		alert("\n Please enter message.");
		thisForm.message.focus();
		return false;	
	}
	return true;
}

function checkRegistForm(thisForm, doWhat) {
	var values = new Array();
	values.push(doWhat);
	
	var userEmail = document.getElementById("userEmail");
	var userPass = document.getElementById("userPass");
	if(!emailCheck(userEmail)) {
		userEmail.focus();
		return false;
	}
			
	if (userPass.value.length == 0){ return errorMsg(userPass, "Password"); }
	
	values.push(userEmail.value);
	values.push(userPass.value);	
	values.push(thisForm.name.value);
	values.push(thisForm.title.value);
	values.push(thisForm.badge.value);
	values.push(thisForm.school.value);
	values.push(thisForm.address.value);
	values.push(thisForm.city.value);
	values.push(thisForm.state.value);
	values.push(thisForm.zip.value);	
	values.push(thisForm.phone.value);
	values.push(thisForm.fax.value);
	values.push(thisForm.paypal.value);
	if(thisForm.voting[0].checked)
		values.push(thisForm.voting[0].value);
	else
		values.push(thisForm.voting[1].value);
		
	if(thisForm.first_timer[0].checked)
		values.push(thisForm.first_timer[0].value);
	else
		values.push(thisForm.first_timer[1].value);
		
	if(thisForm.dietary[0].checked)
		values.push(thisForm.dietary[0].value);
	else
		values.push(thisForm.dietary[1].value);
	
	values.push(thisForm.detail.value);
	
	if(thisForm.ambassador[0].checked)
		values.push(thisForm.ambassador[0].value);
	else
		values.push(thisForm.ambassador[1].value);
	
	if (doWhat == "complete") {
		if (thisForm.name.value.length == 0){ return errorMsg(thisForm.name, "Name"); }
		if (thisForm.address.value.length == 0){ return errorMsg(thisForm.address, "Address"); }		
		if (thisForm.city.value.length == 0){ return errorMsg(thisForm.city, "City"); }		
		if (thisForm.state.value.length == 0){ return errorMsg(thisForm.state, "State"); }		
		if (thisForm.zip.value.length == 0){ return errorMsg(thisForm.zip, "Zip Code"); }		
		if (thisForm.phone.value.length == 0){ return errorMsg(thisForm.phone, "Phone Number"); }		
		if (thisForm.paypal.value.length == 0){ return errorMsg(thisForm.paypal, "Paypal Receipt Number"); }
	}
		
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
  	return;
  }

	getURL="_form2.php?values="+values;	
	xmlHttp.onreadystatechange=doForm;
	xmlHttp.open("GET",getURL,true);
	xmlHttp.send(null);
}

function doForm() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		document.getElementById("formField").innerHTML=xmlHttp.responseText;
	}	
}

function errorMsg(thisValue, thisName) {
		alert("\n Missing "+thisName);
		thisValue.focus();
		return false;
}

function emailCheck(theEmail) {
	
	var emailStr = theEmail.value;
	
	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 + ")";
	
	// The following pattern describes the structure of the user
	
	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)");
		theEmail.focus();
		return false;
	}
	
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("The "+ user[i] +" contains invalid characters.");
			theEmail.focus();
			return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("The "+ domain[i] +" contains invalid characters.");
			theEmail.focus();
			return false;
	   }
	}

	// See if "user" is valid 

	if (user.match(userPat)==null) {
	
		// user is not valid
		
		alert("The username doesn't seem to be valid.");
		theEmail.focus();
		return false;
	}
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	
		// this is an IP address		
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				theEmail.focus();
				return false;
		   }
		}
	}

	// Domain is symbolic name.  Check if it's valid.
	 
	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.");
			theEmail.focus();
			return false;
	   }
	}

	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.");
		theEmail.focus();
		return false;
	}

	// Make sure there's a host name preceding the domain.
	
	if (len<2) {
		alert("This address is missing a hostname!");
		theEmail.focus();
		return false;
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}

function showAlert(msg) {
	alert(msg);
}
