
// Init checkshipping(); and filterstates();
var texts = new Array();
var values = new Array();
var countries = new Array();

var allStates = [];

// Special drop down options with custom rules
var custaccount = -1;	// "Ship on Customer's Account"
var othership = -1;
var intl = -1;		// "International Mail"
var intlexpress = -1;	// "International Express Mail"
var calculated = "";
var strstate       = document.getElementById("strstate");
var shipstate      = document.getElementById("shipstate");
var shipmethodtype = document.getElementById("shipmethodType");
var shipcountry    = document.getElementById("shipcountry");
var strcountry     = document.getElementById("strcountry");
var shipzip = document.getElementById("zip");
if (shipzip) shipzip.onchange = checkshipping;
var glbustime = "3-5";

if (strstate)
{
	for (var i = 0; i < strstate.options.length; i++) {
		allStates[i] = { text: strstate.options[i].text, value: strstate.options[i].value };	
	}
}
if (shipstate || strstate)
{
	filterStates();
	strcountry.onchange = filterStates;
}
//if (shipmethodtype) shipmethodtype.onchange = UpdateShippingJs;
if (shipcountry && shipmethodtype.options) {
	if (shipcountry.value == "CA") glbustime = "2-4";
	if (window.glexpedited === undefined) {
		window.glexpedited = true;
	}
	// Might be set earlier for multiple languages.
	if (window.glselectcountry === undefined) {
		window.glselectcountry = "A country needs to be selected first.";
	}
	if (window.glselectsmethod === undefined) {
		window.glselectsmethod = "Please select a shipping method.";
	}

	var selected_smt = shipmethodtype.options[shipmethodtype.selectedIndex].value;
	var count = 0;
	var hawaska = "";
	var gforce = "";
	for (var i = 0; i < shipmethodtype.options.length; i++) {
		if (shipmethodtype.options[i].value != "Select") {
			texts[count] = shipmethodtype.options[i].text;
			values[count] = shipmethodtype.options[i].value;
			if (texts[count].indexOf("Hawaii & Alaska") > 0) hawaska = count;
			if (texts[count].indexOf("APO/AE") > 0) gforce = count;
			count++;
		}
	}

	for (var i = 0; i < texts.length; i++) {
		var c = texts[i];

		var pos = c.indexOf("UPS ");
		if (pos > -1) {
			c = c.substr(pos + 4);
		}
		pos = c.indexOf("USPS ");
		if (pos > -1) {
			c = c.substr(pos + 5);
		}
		pos = c.indexOf(" - ");
		if (pos > -1) {
			c = c.substr(pos+3);
		}
		pos = c.indexOf(" (");
		if (pos > -1) {
			c = c.substr(0, pos);
		}
		
		pos = c.indexOf("FedEx International");
		if (pos > -1) {
			c = "FedEx International";
		}

		if (c == "Ship on Customer's Account") {
			custaccount = i;
		} 
		else if (c == "Other Shipping Method") {
			othership = i
		} else if (c == "FedEx International") {
			intl = i;
		} else if (c == "International Express Mail") {
			intlexpress = i;
		} 
		else if (window.glexpedited && (c == "2 Day" || c == "3 Day" || c == "Overnight" && window.glexpedited)) {
			c = "United States";
		} else if (c == "Ground") {
			c = "United States";
		} else {
			pos = c.indexOf("Mail ");
			if (pos > -1) {
				c = c.substr(pos + 5);
			}
		}
		
		

		if (window.glintlmail !== undefined && texts[i].indexOf("FedEx International") > -1) {
			texts[i] = texts[i].replace("FedEx International", window.glintlmail);
		}
		if (window.glbusdays !== undefined && texts[i].indexOf("Business Days") > -1) {
			texts[i] = texts[i].replace("Business Days", window.glbusdays);
		}

		countries[i] = c;
	}
	checkshipping();
	shipcountry.onchange = checkshipping;
}

function addShipMethodType(text, value, selected)
{
	var count = shipmethodtype.options.length;
	if (selected == text) {
		shipmethodtype.options[count] = new Option(text, value, true, true);
	} else {
		shipmethodtype.options[count] = new Option(text, value);
	}
}

function checkshipping()
{
	var calcbtn = document.getElementById("GO");
	var glmsg_othership = document.getElementById("glmsg_othership");
	if (glmsg_othership) {
		glmsg_othership.style.display = "none";
	}

	var country = shipcountry.options[shipcountry.selectedIndex];

	if (country.value == "") {
		country = "Select";
	} else {
		country = country.text;
	}

	if (calcbtn)
	{
		if (country == "United States" || country == "Puerto Rico") calcbtn.value = "Add to Order";
		else calcbtn.value = langcalculate;
	}
	
	if (country == "United States" || country == "Puerto Rico" || country == "Select")
		calcbtn.style.display = "none";
	else
		calcbtn.style.display = "";
		
	var glmsg_shipfrom = document.getElementById("glmsg_shipfrom");
	if (glmsg_shipfrom) {
		if (country == "United States" || country == "Puerto Rico") {
			glmsg_shipfrom.style.display = "none";
		} else {
			glmsg_shipfrom.style.display = "";
		}
	}

	// if (shipmethodtype == null)
		// return;
	var selected = null;
	if (shipmethodtype.selectedIndex > -1) {
		selected = shipmethodtype.options[shipmethodtype.selectedIndex].text;
	}
	shipmethodtype.options.length = 0; // Clear all drop down options

	if (country == "Select") {
		addShipMethodType(window.glselectcountry, "Select", selected);
	} else {
		addShipMethodType(window.glselectsmethod, "Select", selected);
		
		if (country == "Virgin Islands (U.S.)")
			country = "Virgin Islands";
			
		for (var i = 0; i < countries.length; i++) {
			if (countries[i] == country) {
				addShipMethodType(texts[i], values[i], selected);
			}
		}

		// Add the following to countries without anything specific
		if (shipmethodtype.options.length == 1) 
		{
			glbustime = "3-5";
			if (country == "Canada") glbustime = "2-4";
			shipmethodtype.options.length = 0; // Clear all drop down options
			if (intl > -1) {
				addShipMethodType(texts[intl].replace("FedEx International Mail",window.glintlmail + " ("+glbustime + " " + glbusdays + ")"), values[intl], selected.replace("FedEx International", window.glintlmail));
			}
			if (intlexpress > -1) {
				addShipMethodType(texts[intlexpress], values[intlexpress], selected.replace("FedEx International", window.glintlmail));
			}
		}
		//check zip code for Hawaii and Alaska
		var zipstr = shipzip.value.substring(0,3);
		if ((zipstr == 967 || zipstr == 968 || zipstr == 995 || zipstr == 996 || zipstr == 997 || zipstr == 998 || zipstr == 999) && country == "United States")
		{
			shipmethodtype.options.length = 0; // Clear all drop down options
			addShipMethodType(window.glselectsmethod, "Select", selected);
			addShipMethodType(texts[hawaska], values[hawaska], selected);
		}
		if ((zipstr == 090 || zipstr == 092 || zipstr == 093 || zipstr == 094 || zipstr == 095 || zipstr == 096 || zipstr == 097 || zipstr == 098 || zipstr == 962 || zipstr == 963 || zipstr == 964 || zipstr == 965 || zipstr == 340) && country == "United States")
		{
			shipmethodtype.options.length = 0; //Clean all drop down options
			addShipMethodType(window.glselectsmethod, "Select", selected);
			addShipMethodType(texts[gforce], values[gforce], selected);
		}
		// Add to everything
		if (custaccount > -1) {
			addShipMethodType(texts[custaccount], values[custaccount], selected);
		}
		if (othership > -1) {
			addShipMethodType(texts[othership], values[othership], selected);
		}
	}

	checkcutofftime();
	UpdateShippingJs();
}

function trim(text) {
	return text.replace(/^\s+|\s+$/g,"");
}

// Init checkcutofftime();
var glmsg_cutofftime = document.getElementById("glmsg_cutofftime");
var glmsg_2day = document.getElementById("glmsg_2day");
var glmsg_3day = document.getElementById("glmsg_3day");
if (glmsg_cutofftime && shipmethodtype.options) {
	shipmethodtype.onchange = checkcutofftime;
	checkcutofftime();
}
function checkcutofftime()
{
	UpdateShippingJs();
	var v = shipmethodtype.options[shipmethodtype.selectedIndex].value;
	if (glmsg_cutofftime) {
		if (v == "UPS 2 Day" || v == "UPS 3 Day" || v == "UPS Overnight") {
			glmsg_cutofftime.style.display = "";
		} else {
			glmsg_cutofftime.style.display = "none";
		}
	}
	if (glmsg_2day) {
		if (v == "UPS 2 Day") {
			glmsg_2day.style.display = "";
		} else {
			glmsg_2day.style.display = "none";
		}
	}
	if (glmsg_3day) {
		if (v == "UPS 3 Day") {
			glmsg_3day.style.display = "";
		} else {
			glmsg_3day.style.display = "none";
		}
	}
}

function UpdateShippingJs()
{
	var v = shipmethodtype.options[shipmethodtype.selectedIndex].value;
	var t = shipmethodtype.options[shipmethodtype.selectedIndex].text;
	var shipcost = t.substring(0,t.indexOf("-")-1)
	var shipcosttd = document.getElementById("shipcosttd");
	var shipcosthdr = document.getElementById("shipcosthdr");
	if ((shipcountry.value == "US" || shipcountry.value == "PR") && t != "Select")
	{
		shipcosttd.style.color = "green";
		shipcosthdr.style.color = "green";
		shipcosttd.innerHTML = shipcost;
	}
	else
	{
		if (calculated == "false")
		{
			shipcosttd.style.color = "";
			shipcosthdr.style.color = "";
			shipcosttd.innerHTML = "";
		}
	}
}

function statesForCountry(country) {
	if (country == "") {
		return allStates;
	}
	var states = [];
	var inCountry = (country == "US");
	for (var i = 2; i < allStates.length; i++) {
		if (!inCountry) {
			var text = trim(allStates[i].text.replace(/-/g, ""));
			if (text == country) {
				inCountry = true;
			}
		} else {
			if (allStates[i].text.substring(0, 1) == "-") {
				break;
			}
			states.push(allStates[i]);
		}
	}
	if (states.length > 0) {
		// Put the "Select" option in the beginning
		states.unshift(allStates[0]);
	} else {
		// No matching country found, add "None"
		states[0] = allStates[1];
	}
	return states;
}

function showStates(select, states) {
	var haveSelected = false;
	var previous = select.options[select.selectedIndex].value;
	select.options.length = 0;
	for (var i=0; i < states.length; i++) {
		var selected = (!haveSelected && previous == states[i].value);
		select.options[i] = new Option(states[i].text, states[i].value, selected, selected);
		if (selected) {
			haveSelected = true;
		}
	}
}

function filterStates() {
	var bCountry = strcountry.options[strcountry.selectedIndex].value;
	if (bCountry != "") {
		//bCountry = strcountry.options[strcountry.selectedIndex].text;
		//bCountry = document.getElementById("strcountry").value;
	}
	//var sCountry = shipcountry.options[shipcountry.selectedIndex].value;
	var sCountry = shipcountry.value;
	if (sCountry != "") {
		//sCountry = shipcountry.options[shipcountry.selectedIndex].text;
		//sCountry = shipcountry.value;
	}
	showStates(strstate, statesForCountry(bCountry));
	showStates(shipstate, statesForCountry(sCountry));
}

