var lastSelectedUSState = 0;
var lastSelectedCAState = 0;
var lastSelectedCountry = 0;
var numstatesincanadonlysite = 15;  // Canada only has 13 states, so if the state list is this small, we know it's canada.

function IsCanada( country, numstates ) {
    return country.indexOf('Canada') >= 0 || numstates <= numstatesincanadonlysite
}

function IsUS(country) {
    return country.indexOf('United States of America') >= 0;
}

function IsCanadaState(state, numstates) {
    return state.indexOf('(Canada)') >= 0 || numstates <= numstatesincanadonlysite
}

function IsUSState(state) {
    return state.indexOf('(US)') >= 0;
}

function SelectAState(state_ctl, cntry_ctl) {
    if (cntry_ctl.selectedIndex == 0 || IsUS(cntry_ctl.options[cntry_ctl.selectedIndex].text) == true || IsCanada(cntry_ctl.options[cntry_ctl.selectedIndex].text, state_ctl.length) == true) {
	    if (cntry_ctl.selectedIndex == 0) {
			state_ctl.selectedIndex = 0;
		} else if( IsUS(cntry_ctl.options[cntry_ctl.selectedIndex].text) == true ) {
			state_ctl.selectedIndex = lastSelectedUSState;
		} else if( IsCanada(cntry_ctl.options[cntry_ctl.selectedIndex].text, state_ctl.length) == true ) {
			state_ctl.selectedIndex = lastSelectedCAState;
		}
		return;
	} else {
		lastSelectedCountry = cntry_ctl.selectedIndex;
		state_ctl.selectedIndex = 1;
	}
}

function SelectACountry( state_ctl, cntry_ctl ) {
	var si = state_ctl.selectedIndex;
	if( si > 1 || state_ctl.length <= numstatesincanadonlysite) {
	    if (IsCanadaState(state_ctl.options[si].text, state_ctl.length) == true) {
			lastSelectedCAState = si;
			if (state_ctl.length <= numstatesincanadonlysite)
			    cntry_ctl.selectedIndex = 1;
			else
			    cntry_ctl.selectedIndex = 2;
		} else {
			lastSelectedUSState = si;
			cntry_ctl.selectedIndex = 1;
		}
	} else {
		if( si == 0 ) {
			cntry_ctl.selectedIndex = 0;
		} else {
			cntry_ctl.selectedIndex = lastSelectedCountry;
		}
	}
}

function ToggleFields( sameas ) {
	document.forms[0].ship_firstname.disabled = sameas;
	document.forms[0].ship_lastname.disabled = sameas;
	document.forms[0].ship_addressline1.disabled = sameas;
	document.forms[0].ship_addressline2.disabled = sameas;
	document.forms[0].ship_city.disabled = sameas;
	document.forms[0].ship_state.disabled = sameas;
	document.forms[0].ship_postalcode.disabled = sameas;
	document.forms[0].ship_country.disabled = sameas;
	document.forms[0].ship_phonenumber.disabled = sameas;
	document.forms[0].ship_otherinfo.disabled = sameas;
}

function PaymentOptions( ctl ) {
	if( ctl.selectedIndex > 0 ) {
		PaymenetOptions_SH( true );
		document.getElementById("credit_card_type").style.backgroundColor      = "#d3d3d3";
		document.getElementById("credit_card_number").style.backgroundColor    = "#d3d3d3";
		document.getElementById("credit_card_cvv2").style.backgroundColor      = "#d3d3d3";
		document.getElementById("credit_card_exp_month").style.backgroundColor = "#d3d3d3";
		document.getElementById("credit_card_exp_year").style.backgroundColor  = "#d3d3d3";
	} else {
		PaymenetOptions_SH( false );
		document.getElementById("credit_card_type").style.backgroundColor      = "#ffffff";
		document.getElementById("credit_card_number").style.backgroundColor    = "#ffffff";
		document.getElementById("credit_card_cvv2").style.backgroundColor      = "#ffffff";
		document.getElementById("credit_card_exp_month").style.backgroundColor = "#ffffff";
		document.getElementById("credit_card_exp_year").style.backgroundColor  = "#ffffff";
	}
}

function PaymenetOptions_SH( enable ) {
	document.forms[0].credit_card_type.disabled      = enable;
	document.forms[0].credit_card_number.disabled    = enable;;
	document.forms[0].credit_card_cvv2.disabled      = enable;
	document.forms[0].credit_card_exp_month.disabled = enable;
	document.forms[0].credit_card_exp_year.disabled  = enable;
}
