
/* --------------------------------------------------------------------------------------------- */
/*	FUNCTIONS LIBRARY BY JASON LEIBOW JASON@MASTERSITEDESIGN.COM  */
/* ---------------------------------------------------------------------------------------------*/


// NEEDED FOR AJAX TO RUN NICELY

	var xmlHttp;

	function GetXmlHttpObject(){
		var xmlHttp=null;
		try{// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();}
		catch (e){//Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
		catch (e){
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}}
			
		return xmlHttp;
	}


// ADD A CITY DURING ACCOUNT CREATION

	function addacity(){ 
		
		checkbrowser();

		var city2 = trim(document.getElementById("city2").value);
		var url="/include/ajax.php?field=addcity&q="+city2;
		
		document.getElementById("citiesselected").style.display = "block";
		document.getElementById("citiesselected").innerHTML = "";

		xmlHttp.open("POST",url,true);
		xmlHttp.onreadystatechange = stateaddcity;
		xmlHttp.send(null);
	}

	function stateaddcity() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			theDetails = trim(xmlHttp.responseText);
			document.getElementById("citiesselected").style.visibility = "visible";
			document.getElementById("citiesselected").display = "block";
			document.getElementById("citiesselected").innerHTML = "<br>"+theDetails;
		}
	}
	

// CLEAR CITY LIST

	function clearcitylist(){ 
		
		checkbrowser();

		var url = "/include/ajax.php?field=clearcitylist";

		xmlHttp.open("POST",url,true);
		xmlHttp.onreadystatechange = stateclearcitylist;
		xmlHttp.send(null);
	}

	function stateclearcitylist() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			theDetails = trim(xmlHttp.responseText);
			
			document.getElementById("city2").style.display = "none";
			document.getElementById("btnaddcity").style.display = "none";
			document.getElementById("citiesselected").style.display = "none";
			document.getElementById("clr").style.display = "none";
			document.getElementById("message2").style.display = "block";

		}
	}
	

// CHECK PROMO CODE VALIDITY (ACCOUNT FORM)

	function checkcode(str){ 		

		var codepermit = trim(str);
	
		if (str != '' ){
			checkbrowser();
			var url = "/include/ajax.php?field=code&q="+str;	
			xmlHttp.open("POST",url,true);
			xmlHttp.onreadystatechange = statecode;
			xmlHttp.send(null);
		}
	}

	function statecode() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			theDetails = trim(xmlHttp.responseText);
			
			if ( theDetails == "Not valid"){
				alert ("This is not a valid promo code");
				document.getElementById("codex").value = "";
				document.getElementById("codex").style.backgroundColor = "Yellow";
				document.getElementById("trialdate").style.visibility = "hidden";
				document.getElementById("trialdate").style.display = "none";
				document.getElementById("codex").focus();

			}else{
				document.getElementById("trialdate").style.visibility = "visible";
				document.getElementById("trialdate").style.display = "block";
				document.getElementById("codex").style.backgroundColor = "#99FF66";
				document.getElementById("trialdate").innerHTML = "<font color=yellow><b>Validated! Trial ends "+theDetails+"</b></font>";
			}
		}
	}


// CHECK URL AVAILABILITY (ACCOUNT FORM)

	function checkurl(str){ 		

		str = trim(str);

		if (str != '' ){
			checkbrowser();
			var url = "/include/ajax.php?field=url&q="+str;	
			xmlHttp.open("POST",url,true);
			xmlHttp.onreadystatechange = stateurl;
			xmlHttp.send(null);
		}
	}

	function stateurl() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			theDetails = trim(xmlHttp.responseText);
			
			if ( theDetails == "Not valid"){
				alert ("Sorry, this URL is unavailable or already in use.\nPlease enter a different URL to continue.");
				document.getElementById("url").value = "";
				document.getElementById("url").style.backgroundColor = "Yellow";
				document.getElementById("url").focus();
			}
		}
	}


// CHECK EMAIL AVAILABILITY (ACCOUNT FORM)

	function checkemail(str){ 		

		str = trim(str);

		if (str != '' ){
			checkbrowser();
			var url = "/include/ajax.php?field=email&q="+str;	
			xmlHttp.open("POST",url,true);
			xmlHttp.onreadystatechange = stateemail;
			xmlHttp.send(null);
		}
	}

	function stateemail() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			theDetails = trim(xmlHttp.responseText);
			
			if ( theDetails == "Not valid"){
				alert ("Sorry, this Email is unavailable or already in use.\nPlease enter a different Email to continue.");
				document.getElementById("email").value = "";
				document.getElementById("email").style.backgroundColor = "Yellow";
				document.getElementById("email").focus();
			}
		}
	}


// CHECK IF ANY CITIES PICKED (ACCOUNT FORM)

	function checkcities(str){ 		

		alert ( str );

		str = trim(str);

		if (str != '' ){
			checkbrowser();
			var url = "/include/ajax.php?field=checkcities&q="+str;	
			xmlHttp.open("POST",url,true);
			xmlHttp.onreadystatechange = statecheckcities;
			xmlHttp.send(null);
		}
	}

	function statecheckcities() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			theDetails = trim(xmlHttp.responseText);
			
			if ( theDetails == "Not valid"){
				alert ("* Important : Please add at least 3 Cities.\nThis is primarily how visitors find you.");
				document.getElementById("state").value = "";
				document.getElementById("state").style.backgroundColor = "Yellow";
				document.getElementById("state").focus();
			}
		}
	}


// CHECK LOGIN AVAILABILITY (ACCOUNT FORM)

	function checklogin(str){ 		

		str = trim(str);

		if (str != '' ){
			checkbrowser();
			var url = "/include/ajax.php?field=login&q="+str;	
			xmlHttp.open("POST",url,true);
			xmlHttp.onreadystatechange = statelogin;
			xmlHttp.send(null);
		}
	}

	function statelogin() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			theDetails = trim(xmlHttp.responseText);
			
			if ( theDetails == "Not valid"){
				alert ("Sorry, this Login ID is unavailable or already in use.\nPlease enter a different Login ID to continue.");
				document.getElementById("login").value = "";
				document.getElementById("login").style.backgroundColor = "Yellow";
				document.getElementById("login").focus();
			}
		}
	}


// CLEAR OR ADD CHECKBOXES ON HOME PAGE SEARCH (TRANSPORT TYPE)

	function checkremove(){ 
		
		checkbrowser();
		var val = document.getElementById("transpo").value;

		if(val!=""){			
			document.getElementById("sedans").checked = 0;
			document.getElementById("limos").checked = 0;
			document.getElementById("vans").checked = 0;
			document.getElementById("buses").checked = 0;
		}else{
			document.getElementById("sedans").checked = 1;
			document.getElementById("limos").checked = 1;
			document.getElementById("vans").checked = 1;
			document.getElementById("buses").checked = 1;
		}

	}

// SHOW CITIES -- AFTER STATE SELECTED (SEARCH FORMS)

	function showcities(){ 
		checkbrowser();		

		document.getElementById("introtext").style.visibility = "hidden";
		document.getElementById("introtext").style.display = "none";
		document.getElementById("city").style.visibility = "hidden";
		document.getElementById("city").style.display = "none";
		document.getElementById("message").style.visibility = "visible";
		document.getElementById("message").style.display = "block";
		document.getElementById("message").innerHTML = "Loading Cities...";

		var state = document.getElementById("state").value;
		var url = "/include/ajax.php?field=cities&q="+state;
		xmlHttp.open("POST",url,true);

		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
		xmlHttp.onreadystatechange = stateshowcities;
		xmlHttp.send(null);
	}
	
	function stateshowcities() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			
			theDetails = trim(xmlHttp.responseText);
			
			if (theDetails != ""){
				
				document.getElementById("message").style.visibility = "hidden";
				document.getElementById("message").style.display = "none";
				document.getElementById("city").innerHTML = theDetails; // works great with firefox, safari
				document.getElementById("city").style.visibility = "visible";
				document.getElementById("city").style.display = "block";
				document.getElementById("apDiv46").style.visibility = "visible";
				document.getElementById("apDiv46").style.display = "block";
			
			// this works for internet explorer 6.0+ drop down list
			
				var tokens = theDetails.split(",");			

				var mySelectBox = document.getElementById("city");			
				var newOption = document.createElement("OPTION");
				newOption.text = " -- Select -- ";
				newOption.value = "";
				mySelectBox.options.add(newOption);
				document.getElementById("showcities").value = "";

				for(i=0;i<tokens.length;i++){

					var fulloption = tokens[i];
					var tokens2 = fulloption.split("~");			
					var opts1 = tokens2[0];
					var opts2 = tokens2[1];

					var mySelectBox = document.getElementById("city");			
					var newOption = document.createElement("OPTION");
			
					newOption.text = tokens2[0];
					newOption.value = opts2;
					mySelectBox.options.add(newOption);
					document.getElementById("showcities").value = opts1;
				
				}	
			}
		} 
	}


// SHOW CITIES -- AFTER STATE SELECTED (ACCOUNT SETUP)

	function showcitiesaccount(){ 
		
		checkbrowser();		

		document.getElementById("introtext2").style.display = "none";				
		document.getElementById("city2").style.display = "none";				
		document.getElementById("message2").style.visibility = "visible";
		document.getElementById("message2").style.display = "block";
		document.getElementById("message2").innerHTML = "Loading Cities...";

		var state = document.getElementById("state").value;
		var url = "/include/ajax.php?field=cities&q="+state;

		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
		xmlHttp.onreadystatechange = stateshowcities2;
		xmlHttp.send(null);
	}
	
	function stateshowcities2() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			
			theDetails = trim(xmlHttp.responseText);
			
			if (theDetails != ""){
				
				document.getElementById("message2").style.visibility = "hidden";
				document.getElementById("message2").style.display = "none";
				document.getElementById("city2").innerHTML = theDetails; // works great with firefox, safari
				document.getElementById("city2").style.visibility = "visible";
				document.getElementById("city2").style.display = "block";
				document.getElementById("clr").style.visibility = "visible";
				document.getElementById("clr").style.display = "block";				
				document.getElementById("btnaddcity").style.visibility = "visible";
				document.getElementById("btnaddcity").style.display = "block";				

			// this works for internet explorer 6.0+ drop down list
			
				var tokens = theDetails.split(",");			
				for(i=0;i<tokens.length;i++){

					var fulloption = tokens[i];
					var tokens2 = fulloption.split("~");			
					var opts1 = tokens2[0];
					var opts2 = tokens2[1];

					var mySelectBox = document.getElementById("city2");			
					var newOption = document.createElement("OPTION");
			
					newOption.text = opts1;
					newOption.value = opts2;
					mySelectBox.options.add(newOption);
					document.getElementById("showcities2").value = opts1;
				
				}
			}
		} 
	}
	
	
// SHOW CITIES -- (ACCOUNT SETUP)

	function showcitiesaccount2(){ 
		
		checkbrowser();		

		document.getElementById("city3").style.visibility = "hidden";
		document.getElementById("city3").style.display = "none";				

		var state = document.getElementById("state3").value;
		var url = "/include/ajax.php?field=cities&q="+state;

		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
		xmlHttp.onreadystatechange = stateshowcities3;
		xmlHttp.send(null);
	}
	
	function stateshowcities3() { 
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			
			theDetails = trim(xmlHttp.responseText);
			
			if (theDetails != ""){
				
				document.getElementById("city3").innerHTML = theDetails; // works great with firefox, safari
				document.getElementById("city3").style.visibility = "visible";
				document.getElementById("city3").style.display = "block";

			// this works for internet explorer 6.0+ drop down list
			
				var tokens = theDetails.split(",");			
				for(i=0;i<tokens.length;i++){

					var fulloption = tokens[i];
					var tokens2 = fulloption.split("~");			
					var opts1 = tokens2[0];
					var opts2 = tokens2[1];

					var mySelectBox = document.getElementById("city3");			
					var newOption = document.createElement("OPTION");
			
					newOption.text = opts1;
					newOption.value = opts2;
					mySelectBox.options.add(newOption);
					document.getElementById("city3").value = opts1;
				
				}
			}
		} 
	}



// SUPPORTED BROWSER CHECK

	function checkbrowser(){
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null){
			alert ("Unsupported Browser");
			return;
		}
	}


// TRIM FUNCTION TO CLEAR SPACES

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