// Init checkother();
var hearaboutus_other = document.getElementById("hearaboutus_other");
var hearaboutus = document.getElementById("hearaboutus");
if (hearaboutus_other && hearaboutus) {
	hearaboutus.onchange = checkother;
	checkother();
}

function checkother()
{
	var container_hearaboutus_other = document.getElementById("container_hearaboutus_other");
	if (hearaboutus.options[hearaboutus.selectedIndex].text == "Other") {
		container_hearaboutus_other.style.display = "";
		hearaboutus_other.name = "hearaboutus";
		hearaboutus.name       = "hearaboutus_ignore";
	} else {
		container_hearaboutus_other.style.display = "none";
		hearaboutus_other.name = "hearaboutus_ignore";
		hearaboutus.name       = "hearaboutus";
	}
}


// 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 intl = -1;		// "International Mail"
var intlexpress = -1;	// "International Express Mail"

var shipmethodtype = document.getElementById("shipmethodType");
var strcountry     = document.getElementById("strcountry");
var strstate       = document.getElementById("strstate");
var shipcountry    = document.getElementById("shipcountry");
var shipstate      = document.getElementById("shipstate");
if (shipmethodtype && strcountry && strstate && shipcountry && shipstate) {
	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;
	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;
			count++;
		}
	}

	for (var i = 0; i < strstate.options.length; i++) {
		allStates[i] = { text: strstate.options[i].text, value: strstate.options[i].value };
	}

	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);
		}

		if (c == "Ship on Customer's Account") {
			custaccount = i;
		} else if (c == "International Mail") {
			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("International Mail") > -1) {
			texts[i] = texts[i].replace("International Mail", window.glintlmail);
		}
		if (window.glbusdays !== undefined && texts[i].indexOf("Business Days") > -1) {
			texts[i] = texts[i].replace("Business Days", window.glbusdays);
		}

		countries[i] = c;
	}
	
	
	strcountry.onchange     = checkshipping;
	strstate.onchange       = checkshipping;
	shipcountry.onchange    = checkshipping;
	shipstate.onchange      = checkshipping;
	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()
{
	filterStates();

	var glmsg_othership = document.getElementById("glmsg_othership");
	if (glmsg_othership) {
		glmsg_othership.style.display = "none";
	}

	var state = shipstate.options[shipstate.selectedIndex];
	if (state.value == "") {
		state = strstate.options[strstate.selectedIndex];
	}
	state = state.text;

	var country = shipcountry.options[shipcountry.selectedIndex];
	if (country.value == "") {
		country = strcountry.options[strcountry.selectedIndex];
	}
	if (country.value == "") {
		country = "Select";
	} else {
		country = country.text;
	}

	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 = "";
		}
	}

	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 == "United States" && (state == "Hawaii" || state == "Alaska")) {
			country = "Hawaii & Alaska";
		} else if (state.substr(0,12) == "Armed Forces") {
			country = "Armed Forces";
		}

		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) {
			if (intl > -1) {
				addShipMethodType(texts[intl], values[intl], selected);
			}
			if (intlexpress > -1) {
				addShipMethodType(texts[intlexpress], values[intlexpress], selected);
			}
		}

		// Add to everything
		if (custaccount > -1) {
			addShipMethodType(texts[custaccount], values[custaccount], selected);
		}
	}

	checkcutofftime();
}


function filterStates() {
	var bCountry = strcountry.options[strcountry.selectedIndex].value;
	if (bCountry != "") {
		bCountry = strcountry.options[strcountry.selectedIndex].text;
	}
	var sCountry = shipcountry.options[shipcountry.selectedIndex].value;
	if (sCountry != "") {
		sCountry = shipcountry.options[shipcountry.selectedIndex].text;
	}

	showStates(strstate, statesForCountry(bCountry));
	showStates(shipstate, statesForCountry(sCountry));
}

function trim(text) {
	return text.replace(/^\s+|\s+$/g,"");
}

function statesForCountry(country) {
	if (country == "") {
		return allStates;
	}
	var states = [];
	var inCountry = (country == "United States");
	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;
		}
	}
}



// 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) {
	shipmethodtype.onchange = checkcutofftime;
	checkcutofftime();
}
function checkcutofftime()
{
	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";
		}
	}
}
