/* Fills the hidden form variables so the dealer data is transmitted to the email routine */
function replaceText(name, loc, country, email, phone, fax, rep_email) {

	document.getElementById("clear").style.visibility = "visible";
	document.getElementById("rep_email").value = rep_email;
	document.getElementById("distp_name").value = name;
	document.getElementById("distp_loc").value = loc;
	document.getElementById("distp_country").value = country;
	document.getElementById("distp_phone").value = phone;
	document.getElementById("distp_fax").value = fax;

	/* If there is no distributor email address, show them the fax number instead */
	if (email === "") {
		document.getElementById("dist_name").innerHTML = " 	<div align=\"center\">" + name + " 	<br>" + loc + ", " + country + " 	<br><a style=\"color: #983A3C;\" >Fax: " + fax + " 	</'+'a> 	<br >" + " 	</'+'div>";
		document.getElementById("warning").innerHTML = " <b >NOTICE: </'+'b > This distributor does not have an email address on file. Either print this request and fax it to the fax number listed, or choose another distributor.";
		document.getElementById("form_submit").value = 'Print Quote Request';
		document.getElementById("main_form").setAttribute("target", "_blank");
		document.getElementById("main_form").setAttribute("action", "index.php?stage=3&print=yes");
		document.getElementById("distp_email").value = '';

	/* Display the distributor in the box to show the user what is selected */
	} else {
		document.getElementById("dist_name").innerHTML = ' <div align="center" >' + name + ' 	<br >' + loc + ', ' + country + '<br>' + '</' + 'div>';
		document.getElementById("warning").innerHTML = '';
		document.getElementById("form_submit").value = 'Submit Quote Request';
		document.getElementById("main_form").setAttribute("target", "");
		document.getElementById("main_form").setAttribute("action", "index.php?stage=3");
		document.getElementById("distp_email").value = email;
	}
}

/* Clears the distributor window and the hidden variables */
function clearDist() {
	document.getElementById("rep_email").value = "";
	document.getElementById("distp_name").value = "";
	document.getElementById("distp_loc").value = "";
	document.getElementById("distp_country").value = "";
	document.getElementById("distp_phone").value = "";
	document.getElementById("distp_fax").value = "";
	document.getElementById("dist_name").innerHTML = "&ensp;&ensp;Enter a valid US zipcode above to display your nearest Distributors";
	document.getElementById("distp_email").value = "";
	document.getElementById("warning").innerHTML = "";
	document.getElementById("clear").style.visibility = "hidden";
}

var xmlHttp;
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 instateChanged() {
	if (xmlHttp.readyState === 4 || xmlHttp.readyState === "complete") {
		if (xmlHttp.responseText) {
			/* there were no distributors, return only the rep for emailing to */
			if(xmlHttp.responseText.substr(0,4) == 'Rep:'){
				var rep = xmlHttp.responseText.split(":");
				rep_dead = rep[0];
				rep_first_name = rep[1];
				rep_last_name = rep[2];
				rep_email = rep[3];
				document.getElementById("zipcode_instate_area").innerHTML = "";
				document.getElementById("dist_name").innerHTML = " 	<div align=\"center\">" + rep_first_name + " " + rep_last_name + "<br>" + rep_email + "</'+'div>";
				document.getElementById("warning").innerHTML = " <b >NOTICE: </'+'b > Sorry there are no Ziggity distributors in your state, please submit your request to " + rep_first_name + " " + rep_last_name + " our representative for your state and a distributor will be contacted.";
				document.getElementById("form_submit").value = 'Submit Quote Request';
				document.getElementById("main_form").setAttribute("target", "");
				document.getElementById("main_form").setAttribute("action", "index.php?stage=3");
				document.getElementById("distp_email").value = rep_email;
				document.getElementById("distp_name").value = rep_first_name + " " + rep_last_name;
			} else {
				document.getElementById("zipcode_instate_area").innerHTML = xmlHttp.responseText;
				var zip_string = document.getElementById("zipcode_raw").value + '';
				/* Disabled by customer */
				/*document.getElementById("zipcode_outstate_link_area").innerHTML = "<small style=\" padding-bottom:25px;\"><a style=\"color: #FFFFFF; font-weight: bold; cursor: pointer; text-decoration: underline;\" onClick=\"zipcode_outstate(\'" + zip_string + "\')\">Show more nearby distributors</a></small>";*/
			}
		}
	}
}

function zipcode_instate(str) {
	clearDist();
	document.getElementById("clear").style.visibility = "hidden";
	document.getElementById("zipcode_outstate_link_area").innerHTML = "";
	document.getElementById("zipcode_outstate_area").innerHTML = "";
	document.getElementById("zipcode_raw").value = str;
	document.getElementById("zipcode_instate_area").innerHTML = "";
	if (str.length === 5) {
		document.getElementById("zipcode_instate_area").innerHTML = "Searching for distributors <img align=\"absmiddle\" src=\"13-0.gif\" />";
	}
	xmlHttp = new GetXmlHttpObject();
	if (xmlHttp === null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "zipcode.func.php?inout=in&data=" + str + "&sid=" + Math.random();

	xmlHttp.onreadystatechange = instateChanged;
		if (str.length === 5) {
		document.getElementById("zipcode_instate_area").innerHTML = "Searching for distributors <img align=\"absmiddle\" src=\"13-0.gif\" />";
	}
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);

}

function outstateChanged() {
	if (xmlHttp.readyState === 4 || xmlHttp.readyState === "complete") {
		if (xmlHttp.responseText) {
			document.getElementById("zipcode_outstate_area").innerHTML = xmlHttp.responseText;
			document.getElementById("zipcode_outstate_link_area").innerHTML = "";
		}
	}
}

function zipcode_outstate(str) {
	str = str + '';
	document.getElementById("zipcode_outstate_link_area").innerHTML = "";
	document.getElementById("zipcode_outstate_area").innerHTML = "Searching for distributors <img align=\"absmiddle\" src=\"13-0.gif\" />";
	xmlHttp = new GetXmlHttpObject();
	if (xmlHttp === null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "zipcode.func.php?inout=out&data=" + str + "&sid=" + Math.random();

	xmlHttp.onreadystatechange = outstateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function intlstateChanged() {
	if (xmlHttp.readyState === 4 || xmlHttp.readyState === "complete") {
		document.getElementById("zipcode_intl_area").innerHTML = xmlHttp.responseText;
		document.getElementById("hide_intl").innerHTML = "<small><a style=\"color: #FFFFFF; cursor: pointer; text-decoration: underline;\" onClick=\"hide_zipcode_intl()\">Hide International Distributors</a></small>";
	}
}

function zipcode_intl() {
	document.getElementById("zipcode_intl_area").innerHTML = "Searching for distributors <img align=\"absmiddle\" src=\"13-0.gif\" />";
	xmlHttp = new GetXmlHttpObject();
	if (xmlHttp === null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "zipcode.func.php?inout=intl&sid=" + Math.random();
	xmlHttp.onreadystatechange = intlstateChanged;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function hide_zipcode_intl() {
	//clearDist();
	document.getElementById("clear").style.visibility = "hidden";
	document.getElementById("zipcode_intl_area").innerHTML = "";
	document.getElementById("hide_intl").innerHTML = "<small><a style=\"color: #FFFFFF; cursor: pointer; text-decoration: underline;\" onClick=\"zipcode_intl()\">Show International Distributors</a></small>";

}

