var xmlhttp = false;
var xmlHttp = false;
	
//Check if we are using IE.
try 
	{
	//If the Javascript version is greater than 5.
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
catch (e) 
	{
	//If not, then use the older active x object.
	try 
		{
		//If we are using Internet Explorer.
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
	catch (E) 
		{
		//Else we must be using a non-IE browser.
		xmlhttp = false;
		}
	}

//If we are using a non-IE browser, create a javascript instance of the object.
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
	xmlhttp = new XMLHttpRequest();
	}


function makerequest(serverPage, objID) {
		
	var date = new Date();
	var timestamp = date.getTime();
	url = serverPage + "?time=" +timestamp;

	var obj = document.getElementById(objID);
//	xmlhttp.open("GET", serverPage);
	xmlhttp.open("GET", url);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			obj.innerHTML = xmlhttp.responseText;
			}
		}
	xmlhttp.send(null);
	}

function getcalendar(objID){
	
	var obj = document.getElementById(objID);
	obj.style.visibility = "visible";
	}

function hidecalendar(){
	var obj = document.getElementById('moveincalendar');
	obj.style.visibility = "hidden";
	}

function createDate(e){

	url = "getrealdate.php?thisday=" + e;
	xmlhttp.open("GET", url);
	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			document.getElementById("registration").birthdate.value = xmlhttp.responseText;
			hidecalendar();
			}
		}
	xmlhttp.send(null);
	}

function checkphone(e){
	var goodphone="";
	var phone=String(e.value);
	phone=phone.replace(/[^0-9]/g,'');
	var vlen=phone.length;

	if (phone)
		{

		if (vlen=='10' && !isNaN(phone))
			{
			goodphone="("+phone.slice(0,3)+") "+phone.slice(3,6)+"-"+phone.slice(6);
			e.value=goodphone;
			}
		else
			{
			alert("The phone number you have entered is invalid. Please enter only 10 numbers including the area code.");
			globalvar = e;
			setTimeout("globalvar.focus();", 1);
			setTimeout("globalvar.select();", 1);
			}
		}
	else
		{
		e.value=phone;
		}
}

function checkemail(e){
	var email=String(e.value);
	if (email){
		if(!email.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) {
			alert("The email address you have entered is invalid.");
			globalvar = e;
			setTimeout("globalvar.focus();", 1);
			setTimeout("globalvar.select();", 1);
		}
	}
}

function submitform (theform, serverPage, objID, valfunc){
	var file = serverPage;
	var str = getformvalues(theform,valfunc);
	//If the validation is ok.
	if (aok == true){
		obj = document.getElementById(objID);
		processajax (serverPage, obj, "post", str);
		}
	}

function getformvalues (fobj, valfunc){
		
	var str = "";
	aok = true;
	var val;
		
	//Run through a list of all objects contained within the form.
	for(var i = 0; i < fobj.elements.length; i++){
		if(valfunc) {
			if (aok == true){
				val = valfunc (fobj.elements[i].value,fobj.elements[i].name); 
				if (val == false){
					aok = false;
					}
				}
			}
		str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
		}
	//Then return the string values.
	return str;
	}

function validate (thevalue, thename){
	var nowcont = true;
	if (thename == "fname"){
		if (trim (thevalue) == ""){
			document.getElementById("error1").innerHTML = "You must enter your FIRST NAME.";
			document.getElementById("registration").fname.focus();
			nowcont = false;
			}
		}
	if (nowcont == true){
		if (thename == "lname"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "You must enter your LAST NAME.";
				document.getElementById("registration").lname.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "birthdate"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "You must enter your BIRTHDAY so that we may determine your age.";
				document.getElementById("registration").birthdate.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "gender"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please select MALE or FEMALE.";
				document.getElementById("registration").gender.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "address"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please tell us your ADDRESS.";
				document.getElementById("registration").address.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "city"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please tell us which CITY.";
				document.getElementById("registration").city.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "state"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please select a STATE.";
				document.getElementById("registration").gender.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "zip"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please enter your ZIPCODE.";
				document.getElementById("registration").zip.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "phone"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please enter your PHONE NUMBER.";
				document.getElementById("registration").phone.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "emailaddr"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please enter your EMAIL ADDRESS.";
				document.getElementById("registration").emailaddr.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "medhis"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "If there are is no medical history, please state NONE in the Medical History box.";
				document.getElementById("registration").medhis.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "goals"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "Please tell us your GOALS.";
				document.getElementById("registration").goals.focus();
				nowcont = false;
				}
			}
		}
	if (nowcont == true){
		if (thename == "agree"){
			if (trim (thevalue) == ""){
				document.getElementById("error1").innerHTML = "You must AGREE to the terms and conditions set forth to be eligible to enroll.";
				document.getElementById("registration").agree.focus();
				nowcont = false;
				}
			}
		}
	return nowcont;
	}

function processajax (serverPage, obj, getOrPost, str){
		//Get an XMLHttpRequest object for use.
		//xmlhttp = getxmlhttp ();
		if (getOrPost == "get"){
			xmlhttp.open("GET", serverPage);
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					obj.innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(null);
		} else {
			xmlhttp.open("POST", serverPage, true);
			xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
			xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
					obj.innerHTML = xmlhttp.responseText;
				}
			}
			xmlhttp.send(str);
		}
	}



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
	} // Ends the "trim" function