
	var browserIE = (navigator.appName == 'Microsoft Internet Explorer');
	var xmlHttp=new Array(10);
	var str_loc=new Array(10);
	var loc_num;
	var xml_index = 0;
	var weight_override = 0;
	var blank_image = '<img width="21" height="21" border="0" src="./images/blank.gif">';

	function GetTopLeft(elm) {
		var x, y = 0;
		x = elm.offsetLeft;
		y = elm.offsetTop;
		elm = elm.offsetParent;
		while(elm != null) {
			x = parseInt(x) + parseInt(elm.offsetLeft);
			y = parseInt(y) + parseInt(elm.offsetTop);
			elm = elm.offsetParent;
		}
		return {Top:y, Left: x};
	}

	function sl() {
		document.getElementById("links").src='./images/icons/social_bm.gif';
	}

	function hl() {
		document.getElementById("links").src='./images/icons/social_bm_bw.gif';
	}

	function nf() {
	}

	function toggle_adv() {
		var adv_div = document.getElementById("adv_inputs");
		var inp_div = document.getElementById("inputs");
		var adv_var = document.getElementById("advanced_interface");
		if (adv_div.style.visibility == "hidden") {
			inp_div.style.visibility = "hidden";
			inp_div.style.display = "none";
			inp_div.disabled = true;
			adv_div.style.visibility = "visible";
			adv_div.style.display = "block";
			adv_div.disabled = false;
			adv_var.value="1";
		} else {
			adv_div.style.visibility = "hidden";
			adv_div.style.display = "none";
			adv_div.disabled = true;
			inp_div.style.visibility = "visible";
			inp_div.style.display = "block";
			inp_div.disabled = false;
			adv_var.value="0";
		}
	}

	function sortNumber(aa,bb) {
		return bb - aa;
	}

	var oldlen = 12;
	var oldwid = 12;
	var oldhgt = 12;
	function dim_weight() {
		var len = parseInt(document.getElementById("length").value);
		var wid = parseInt(document.getElementById("width").value);
		var hgt = parseInt(document.getElementById("height").value);
		if ((len < 1) | (wid < 1) | (hgt < 1)) {
			return;
		} else if ((len == oldlen) & (wid == oldwid) & (hgt == oldhgt)) {
			return;
		}
		oldlen = len;
		oldwid = wid;
		oldhgt = hgt;
		var maxDim = 0;
		var boxDims = new Array(3);
		boxDims[0] = len;
		boxDims[1] = wid;
		boxDims[2] = hgt;
		boxDims = boxDims.sort(sortNumber);
		if (browserIE) {
			maxDim = 90;
		} else {
			maxDim = 120;
		}
		boxDims[1] = parseInt(boxDims[1]/boxDims[0]*maxDim);
		boxDims[2] = parseInt(boxDims[2]/boxDims[0]*maxDim);
		boxDims[0] = maxDim;
		if (browserIE) {
			document.getElementById("3dbox_div").innerHTML='<iframe src="./3die.html?len=' + boxDims[0] + '&wid=' + boxDims[1] + '&hgt=' + boxDims[2] + '" title="3D Box" width="240" height="250" scrolling="no" frameborder="0"></iframe>';
			//document.getElementById("CU").style.width=maxDim + 'px';
			//document.getElementById("CU").style.height=boxDims[1] + 'px';
			//document.getElementById("BE").backdepth=boxDims[2] + 'px';
		} else {
			document.getElementById("3dbox_div").innerHTML='<embed id="3dbox" src="' + './3dbox.svg?len=' + boxDims[0] + '&wid=' + boxDims[1] + '&hgt=' + boxDims[2]+ '" width="200" height="200">';
		}
		var wgt_input = document.getElementById("weight2");
		var wgt_text = document.getElementById("wgt_info");
		var d_mode = document.getElementById("dim_mode");
		var st1 = document.getElementById("state_var3").value;
		var st2 = document.getElementById("state_var4").value;
		var dim_weight_formula = 194;
		var dim_weight_label = "U.S. Domestic";
		d_mode.value="us";
		if ((isCanadian(st1)) | (isCanadian(st2))) {
			dim_weight_formula = 166;
			dim_weight_label = "International";
			d_mode.value="int";
		}
		if ((len > 0) && (wid > 0) && (hgt > 0) && ((len*wid*hgt) > 1728)) {
			d_weight = (len * wid * hgt) / dim_weight_formula;
			d_weight = Math.ceil(d_weight);
			if (d_weight > 150) {
				d_weight = 150;
			}
			wgt_text.innerHTML = "<b>" + dim_weight_label + " Dimensional Weight: " + d_weight + " lbs";
			if ((d_weight >= wgt_input.value) & (weight_override == 1)) {
				wgt_input.value = d_weight;
			}
		} else {
			d_weight = 0;
			wgt_text.innerHTML = "&nbsp;&nbsp;";
		}
		document.getElementById("dimensional_weight").value=d_weight;
	}

	function copy_val (copy1, copy2) {
		try {
			document.getElementById(copy2).value=copy1.value;
		} catch (e) {
		}
	}

	function showCityState(str) {
		var num = str.id;
		num = num.substring(num.length-1,num.length);
		if (str.value.length == 0) {
			document.getElementById("loc"+num).innerHTML=blank_image;
			return;
		} else if (document.getElementById("loc_var"+num).value==document.getElementById("postal"+num).value) {
			return;
		} else if (str.value.length==2) {
			if ((num=='2')|(num == '4')) {
				if (countries[str.value]) {
					document.getElementById("loc"+num).innerHTML=blank_image+'<b>'+countries[str.value]+'</b>';
				} else {
					if ((str.value == 'US')|(str.value == 'CA')) {
						//alert('For the U.S. and Canada, please enter a zip/postal code.');
						document.getElementById("loc"+num).innerHTML=blank_image+'<font color="RED">Country ISO code not found</font>';
					} else {
						document.getElementById("loc"+num).innerHTML=blank_image+'<font color="RED">Country ISO code not found</font>';
					}
				}
			} else {
				document.getElementById("loc"+num).innerHTML=blank_image+'Zip/Postal Code not found';
			}
			return;
		}
		xmlHttp[xml_index]=GetXmlHttpObject();
		if (xmlHttp[xml_index]==null) {
			alert ("Your browser does not support AJAX!");
			return;
		}
		loc_num = num;
		if (num < 3) {
			document.getElementById("loc"+num).innerHTML=blank_image+'<img width="16" height="16" border="0" src="./images/working.gif">'+blank_image+blank_image;
		} else {
			document.getElementById("loc"+num).innerHTML=blank_image+'<img width="16" height="16" border="0" src="./images/working.gif">';
		}
		var url="rates2.php";
		url=url+"?pz="+str.value;
		url=url+"&sid="+Math.random();
		xmlHttp[xml_index].onreadystatechange=stateChanged_CityState;
		xmlHttp[xml_index].open("GET",url,true);
		xmlHttp[xml_index].send(null);
	}

	function stateChanged_CityState() {
		try {
			if (xmlHttp[xml_index].readyState==4) {
				str_loc[xml_index] = xmlHttp[xml_index].responseXML.getElementsByTagName("pztown")[0].childNodes[0].nodeValue;
				document.getElementById("state_var"+loc_num).value=str_loc[xml_index].substring(str_loc[xml_index].length-2,str_loc[xml_index].length);
				if (str_loc[xml_index].substring(1,4)!="Zip") {
					if (str_loc[xml_index].length > 27) {
						str_loc[xml_index] = str_loc[xml_index].substring(0,25)+' '+str_loc[xml_index].substring(str_loc[xml_index].length-2,str_loc[xml_index].length);
					}
					str_loc[xml_index]=format_state(str_loc[xml_index]);
				}
				var loc2 = parseInt(loc_num) + 2;
				if (loc2 > 4) {
					loc2 = loc2 - 4;
				}
				document.getElementById("loc"+loc_num).innerHTML=str_loc[xml_index];
				document.getElementById("loc_var"+loc_num).value=document.getElementById("postal"+loc_num).value;
				document.getElementById("loc"+loc2).innerHTML=blank_image+str_loc[xml_index];
				document.getElementById("loc_var"+loc2).value=document.getElementById("postal"+loc2).value;
				xml_index++;
				if (xml_index > 9) {
					xml_index=0;
				}
			}
		} catch (e) { //alert(e);
		}
	}

	function clearCountry () {
		document.getElementById("country_code").value = '';
		document.getElementById("country_dest").selectedIndex = 0;
	}

	function countryCode (cObj) {
		if (cObj.id == 'country_dest') {
			document.getElementById("country_code").value = cObj[cObj.selectedIndex].value;
			document.getElementById("dest_flag").src="./images/icons/flags/"+cObj[cObj.selectedIndex].value.toLowerCase()+".png";
			document.getElementById("postal4").value='';
			document.getElementById("loc4").innerHTML=blank_image;
		} else {
			var c;
			var cFound = false;
			cObj.value = cObj.value.toUpperCase();
			var cID = cObj.value;
			var cDest = document.getElementById("country_dest");
			for (c=0;c<cDest.length;c++) {
				if (cDest[c].value == cID) {
					cDest.selectedIndex = c;
					cFound = true;
					c = cDest.length + 1;
				}
			}
			if (cFound) {
				document.getElementById("postal4").value='';
				document.getElementById("loc4").innerHTML=blank_image;
				document.getElementById("dest_flag").src="./images/icons/flags/"+cID.toLowerCase()+".png";
			} else {
				cDest.selectedIndex = 0;
				cObj.value = '';
				if ((cID == 'US')|(cID == 'CA')) {
					alert('For the U.S. and Canada, please enter a zip/postal code.');
				} else {
					alert('The 2 letter country ISO code is not recognized.');
				}
				document.getElementById("dest_flag").src="./images/blank.gif";
			}
		}
	}


	function getPostal(myCity, myState, myDisplay) {
//alert(myCity + ', ' + myState + ' ' + myDisplay);
		var cityDiv = document.getElementById("city_div");
		cityDiv.style.visibility='hidden';
		var inputPostal = document.getElementById(last_cityObj);
		inputPostal.value=myDisplay;
		var num = last_cityObj.substring(last_cityObj.length-1,last_cityObj.length);
		document.getElementById("loc"+num).innerHTML=myDisplay;
		if (myCity.length == 2) {
			inputPostal.value=myCity;
			return;
		}

		// Initiate AJAX call for Postal Code
		xmlHttp[xml_index]=GetXmlHttpObject();
		if (xmlHttp[xml_index]==null) {
			alert ("Your browser does not support AJAX!");
			return;
		}
		var url="rates2.php";
		url=url+"?lookup_city="+myCity.replace(/%20/g,"+").replace(/ /g,"+");
		url=url+"&lookup_state="+myState;
		url=url+"&sid="+Math.random();
		xmlHttp[xml_index].onreadystatechange=stateChanged_PostalLookup;
		xmlHttp[xml_index].open("GET",url,true);
		xmlHttp[xml_index].send(null);
	}


	function stateChanged_PostalLookup() {
		try {
			if (xmlHttp[xml_index].readyState==4) {
				var xmlDoc = xmlHttp[xml_index].responseXML.documentElement;
				var error_code = xmlDoc.getElementsByTagName("error_code")[0].childNodes[0].nodeValue;
				if (error_code == '0') {
					document.getElementById(last_cityObj).value = xmlDoc.getElementsByTagName("origin")[0].childNodes[0].nodeValue;
				} else {
					var num = last_cityObj.substring(last_cityObj.length-1,last_cityObj.length);
					document.getElementById(last_cityObj).value = '';
					document.getElementById("loc"+num).innerHTML = 'Zip/Postal code not found';
				}
				xml_index++;
				if (xml_index > 9) {
					xml_index=0;
				}
			}
		} catch (e) { //alert(e);
		}
	}

	var theCountries = new Array();
	theCountries["US"] = 'USA';
	theCountries["CA"] = 'Canada';
	function stateChanged_City() {
		try {
			if (xmlHttp[xml_index].readyState==4) {
				function cnvrt() {
       	     			return arguments[0].toUpperCase();
				}
				var num = last_cityObj.substring(last_cityObj.length-1,last_cityObj.length);
				var cityDiv = document.getElementById("city_div");
				cityDiv.innerHTML = '';
				var c=0;
				var nice_apost = '\\'+"'";
				var city='';
				var xmlDoc = xmlHttp[xml_index].responseXML.documentElement;
				var city_names = xmlDoc.getElementsByTagName('city');
				cityDiv.innerHTML += '<div style="width:100%; line-height:50%; text-align:right; color:#aaaaaa; font-size:10px;">Suggestions</div>';
				if ((num==2)&(document.getElementById(last_cityObj).value.length==2)) {
					if (countries[document.getElementById(last_cityObj).value.toUpperCase()]) {
						cityDiv.innerHTML += '<div style="cursor:pointer; width:100%; color:#444444; background:#fffff0;" onMouseOver="this.style.background=\'#4444ff\'; this.style.color=\'WHITE\';" onMouseOut="this.style.background=\'#fffff0\'; this.style.color=\'#444444\';" onClick="getPostal(\'' + document.getElementById(last_cityObj).value.toUpperCase() + '\',\'\',\'' + countries[document.getElementById(last_cityObj).value.toUpperCase()] + '\');">&nbsp;&nbsp;<b>' + countries[document.getElementById(last_cityObj).value.toUpperCase()] + '</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(country)</div>';
					}
				}
				if (city_names.length > 0) {
					var state_names = xmlDoc.getElementsByTagName('state');
					var country_names = xmlDoc.getElementsByTagName('country');
					for (c=0;c<city_names.length;c++) {
						city = xmlDoc.getElementsByTagName("city")[c].childNodes[0].nodeValue;
						city = city.toLowerCase().replace(/\b[a-z]/g, cnvrt);
						cityDiv.innerHTML += '<div style="cursor:pointer; width:100%; color:#444444; background:#fffff0;" onMouseOver="this.style.background=\'#4444ff\'; this.style.color=\'WHITE\';" onMouseOut="this.style.background=\'#fffff0\'; this.style.color=\'#444444\';" onClick="getPostal(\'' + xmlDoc.getElementsByTagName("city")[c].childNodes[0].nodeValue.replace(/'/g,nice_apost) + '\',\''+xmlDoc.getElementsByTagName("state")[c].childNodes[0].nodeValue + '\',\'' + city.replace(/'/g,nice_apost) + ', ' + states[xmlDoc.getElementsByTagName("state")[c].childNodes[0].nodeValue] + '\');">&nbsp;&nbsp;' + city + ', ' + states[xmlDoc.getElementsByTagName("state")[c].childNodes[0].nodeValue] + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + theCountries[xmlDoc.getElementsByTagName("country")[c].childNodes[0].nodeValue] + '</div>';
					}
				} else {
					cityDiv.innerHTML += '<div style="color:#aaaaaa;">(city not found)</div>';
				}
				cityDiv.style.visibility='visible';
				xml_index++;
				if (xml_index > 9) {
					xml_index=0;
				}
			}
		} catch (e) { //alert(e);
		}
	}

	var last_cityObj = '';
	var last_cityVal = '';

	function ajaxCity (iObj) {
		var cityDiv = document.getElementById("city_div");

		// Exit function if nothing changed since last call
		if ((iObj.id==last_cityObj)&&(iObj.value==last_cityVal)) {
			return;
		}

		if (iObj.value.length < 2) {
			cityDiv.style.visibility='hidden';
			return;
		}

		// Check for U.S. Zip Code - First 2 characters are numbers
		if (!isNaN(parseInt(iObj.value.substring(0,2)))) {
			// Check for U.S. Zip Code - First 5 characters are numbers
			if (iObj.value.length==5) {
				if (!isNaN(parseInt(iObj.value.substring(0,5)))) {
					showCityState(iObj);
				}
			} else if (iObj.value.length > 5) {
				iObj.value = iObj.value.substring(0,5);
				if (!isNaN(parseInt(iObj.value.substring(0,5)))) {
					showCityState(iObj);
				}
			}
			return;
		}
		// Check for Canadian Postal Code - First character is a letter, Second is a number
		if ((isNaN(parseInt(iObj.value.substring(0,1))))&(!isNaN(parseInt(iObj.value.substring(1,2))))) {
			iObj.value = iObj.value.toUpperCase().replace(/ /g,'');
			if (iObj.value.length==6) {
				showCityState(iObj);
			}
			return;
		}

		last_cityObj = iObj.id;
		last_cityVal = iObj.value;

		// Position Drop-down List
		var objXY = GetTopLeft(iObj);
		cityDiv.style.zIndex='9999';
		cityDiv.style.width=parseInt(iObj.offsetWidth) + 'px';
		cityDiv.style.top=objXY.Top + parseInt(iObj.offsetHeight) + 'px';
		cityDiv.style.left=objXY.Left + 'px';


		// Initiate AJAX call for City Name Dropdown List
		xmlHttp[xml_index]=GetXmlHttpObject();
		if (xmlHttp[xml_index]==null) {
			alert ("Your browser does not support AJAX!");
			return;
		}
		var url="rates2.php";
		url=url+"?city_str="+iObj.value.toUpperCase().replace(/ /g,"%20").replace(/\./g,"");
		url=url+"&sid="+Math.random();
		xmlHttp[xml_index].onreadystatechange=stateChanged_City;
		xmlHttp[xml_index].open("GET",url,true);
		xmlHttp[xml_index].send(null);
	}

	function home_init() {
		document.getElementById("postal1").style.color='#999999';
		document.getElementById("postal2").style.color='#999999';
		document.getElementById("postal1").value=defaultExample;
		document.getElementById("postal2").value=defaultExample+defaultExample2;
	}


	function initPostal (pObj) {
		if ((pObj.value==defaultExample)|(pObj.value==defaultExample+defaultExample2)) {
			pObj.value='';
			pObj.style.color='#000777';
		}
	}

	function initPostalAgain (pObj) {
		var num = pObj.id;
		num = num.substring(num.length-1,num.length);
		//document.getElementById("city_div").style.visibility='hidden';
		if (pObj.value=='') {
			pObj.style.color='#999999';
			if (pObj.id=='postal1') {
				pObj.value=defaultExample;
			} else {
				pObj.value=defaultExample+defaultExample2;
			}
			document.getElementById("loc"+num).innerHTML=blank_image;
		}
	}
