
function makePOSTRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest){
	http_request =	new	XMLHttpRequest();
	if	(http_request.overrideMimeType)	{
		// set type	accordingly	to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	}
}else if(window.ActiveXObject){ 
	try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try	{
				http_request	= new ActiveXObject("Microsoft.XMLHTTP");
			} catch	(e)	{}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return	false;
	}
	//http_request.onreadystatechange = alertPostContents;
	http_request.onreadystatechange	= function() { alertPostContents(http_request);	};
	http_request.open('POST',	url, true);
	http_request.setRequestHeader("Content-type",	"application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function alertPostContents(http_request)	{
	if (http_request.readyState == 4)	{
		if	(http_request.status ==	200) {
			result = http_request.responseText;
			//alert(result);
			if(result.indexOf("EMAIL") == 1){
				//alert(result.indexOf("EMAIL"));
				result = result.slice(6);
				document.getElementById('EmailError').innerHTML = result;
				//alert(result);
			}else if(result.indexOf("FISH") == 1){
				//alert(result.indexOf("FISH"));
				result = result.slice(5);
				document.getElementById('UsernameE').innerHTML = result;
				if(result.indexOf("system") > 0)document.getElementById("myUsername").value = "";
				//alert(result);
			}else if(result.indexOf("CLINICS") == 1){
				//alert(result.indexOf("CLINICS"));
				//var clinicSpan = document.getElementById("clinics");
				if(result.length > 10){  //add the thing to the dom if a good answer comes back
					result = result.slice(8);
					var clinicSpan = document.getElementById('clinics');//.innerHTML = result;
					// create the new select for the DOM
					var clinics = new Array();
					clinics = result.split("!");
					for(i=1;i<clinics.length;i++){
						var clinic =  new Array();
						clinic = clinics[i].split("|");
						//add the option to the select   //!$clinicID|$clinicName|$clinicDistance"
						var clinicID = clinic[0];
						var clinicName = clinic[1];
						var clinicDistance = clinic[2];
						var clinicOption = document.getElementById('clinic'+i);
						clinicOption.setAttribute("value",clinicID);
						var clinicText=document.createTextNode(clinicName+" " + clinicDistance + " Miles");
						if(clinicOption.hasChildNodes())clinicOption.removeChild(clinicOption.firstChild);
						clinicOption.appendChild(clinicText);
					}
					document.getElementById('clinics').style.display = 'block';
				}else{ //remove the thing from the dom if it exists
					document.getElementById('clinics').style.display = 'none';
					clearZip();
				}
			}
		} else	{
			alert('There was a problem checking your email address.');
		}
	}
}

function checkEmail(){
	if(document.getElementById("Email").value != ""){
		var test = "Email=" +  encodeURI( document.getElementById("Email").value );
		makePOSTRequest("checkEmailAddress.php", test);
	}
}

function checkUser(){
	//alert('checking2');
	//alert(document.getElementById("myUsername").value);
	if(document.getElementById("myUsername").value != ""){
		var test = "myUsername=" + encodeURI( document.getElementById("myUsername").value );
		makePOSTRequest("checkUsername.php", test);
		//alert(test);
	}
}

function IsNumeric(sText){
var ValidChars = "0123456789";
var IsNumber=true;
var Char;
for(i = 0; i < sText.length && IsNumber == true; i++){ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1){
			IsNumber = false;
		}
	}
	return IsNumber;
}

function clearZip(){
	for(i=1;i<4;i++){
		var clinicOption = document.getElementById('clinic'+i);
		clinicOption.setAttribute("value",'2');
		if(clinicOption.hasChildNodes())clinicOption.removeChild(clinicOption.firstChild);
	}
	document.getElementById('clinics').style.display = 'none';
}

function checkZip(){
	if(document.getElementById("PostalCode").value != "" && IsNumeric(document.getElementById("PostalCode").value)){
		var zip = document.getElementById("PostalCode").value;
		if(zip.length == 5){ // is it actually a zip?
			var test = "PostalCode=" + encodeURI( document.getElementById("PostalCode").value );
			makePOSTRequest("checkZip.php", test);
		}else{
			clearZip();
		}
	}else{
		clearZip();
	}
}

function isInteger(s)
{   var i;
		var intcnt;
		intcnt = 0;
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) {
		} else {
					intcnt++;
		}
	}
	// All characters are numbers.
	if (intcnt > 6) {
			return true;   
	} else {
			return false;    
	}
}


	function CheckPassword(){
		if (document.Customers.Password.value != ""){
			if (document.Customers.Password.value == document.Customers.Password2.value){
				return true;
			}
			else{
				return false;
			}
		}
		else return false;
	}	


	function checkChar(charOK, fieldvalue, msg) 
	{

	  var charOK;
	  var fieldvalue = fieldvalue;

	  var usernameValid = true;

	  for (i = 0;  i < fieldvalue.length;  i++)
	  {
		buffer = fieldvalue.charAt(i);
		for (j = 0;  j < charOK.length;  j++)
		  if (buffer == charOK.charAt(j))
			break;
		if (j == charOK.length)
		{
		  usernameValid = false;
		  break;
		}
	  }
	  if (!usernameValid)
	  {
		alert(msg);
	  }
	  return (usernameValid);
	}

	function CheckElement(ElementName, BlankFieldsString, UserFriendlyName){
		if(document.Customers.elements[ElementName].value == ""){
			if(BlankFieldsString == ""){
				BlankFieldsString = UserFriendlyName;
			}
			else{
				BlankFieldsString = BlankFieldsString + ", " + UserFriendlyName;
			}
		}
		return BlankFieldsString;
	}	
/*
	function (CallingObject){
		var intNumRows;
		var strName;
		var intDirtyRowIndex;

		//Get the name of the calling object
	   strName = CallingObject.name;

		//Get the number of rows we are writing by counting the number of IsDirty Flags
		intNumRows = document.Customers.IsDirty.length;

		//Get the array index of the calling object by looping thru each row and
		//checking to see if the object in that row with the Calling Object's name
		//is the actual calling object.
		//That array index represents the dirty row index
		if(intNumRows>1){
			 for(i=0;i<intNumRows;i++){
				 if(document.Customers.elements[strName][i]==CallingObject){
					 intDirtyRowIndex = i
				 }
			 }
			 //Set the value of the IsDirty element in this row to "Yes".
			 document.Customers.IsDirty[intDirtyRowIndex].value="Yes"
		 }
		else {
			 document.Customers.IsDirty.value="Yes"
		 }
	}
*/
	function DeleteItem(){
						 document.Customers.IsDirty.value="Yes";
						 document.forms("Customers").submit();
					   }


	function SubmitForm(){
		var BlankFieldsString = "";
		var i = 0;
		var username = document.Customers.elements["myUsername"].value;
		var invalid_char = username.indexOf(' '); 

		if (checkChar("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890", document.Customers.myUsername.value, "Your Username contains invalid characters.  Please use only letters and numbers.")) {
			if (isInteger(document.Customers.DayPhone.value)==true) {
				if (CheckPassword()==true)	{

					BlankFieldsString = CheckElement("FirstName", BlankFieldsString, "First Name");
					BlankFieldsString = CheckElement("LastName", BlankFieldsString, "Last Name");
					BlankFieldsString = CheckElement("myUsername", BlankFieldsString, "myUsername");
					BlankFieldsString = CheckElement("Email", BlankFieldsString, "Email Address");

					BlankFieldsString = CheckElement("DayPhone", BlankFieldsString, "Daytime Phone");

					BlankFieldsString = CheckElement("Address1", BlankFieldsString, "Address 1");
					BlankFieldsString = CheckElement("City", BlankFieldsString, "City");
					BlankFieldsString = CheckElement("State", BlankFieldsString, "State");
					BlankFieldsString = CheckElement("PostalCode", BlankFieldsString, "Zipcode");
					BlankFieldsString = CheckElement("Country", BlankFieldsString, "Country");

					if(BlankFieldsString != ""){
						alert("You must provide information for the following fields: " + BlankFieldsString);
					}
					else{
						document.forms["Customers"].submit();
					}
				}
				else{
					alert("Your passwords do not match.");
				}
			}
			else{
				alert("Enter a valid phone number");
			}
		}
	}

