// State table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var US = { "state": [
		{"stateCode": "", "stateText": "Select State"},
		{"stateCode": "US", "stateText": "---US States---"},
		{"stateCode": "AK", "stateText": "Alaska"},
		{"stateCode": "AL", "stateText": "Alabama"},
		{"stateCode": "AR", "stateText": "Arkansas"},
		{"stateCode": "AS", "stateText": "American Samoa"},
		{"stateCode": "AZ", "stateText": "Arizona"},
		{"stateCode": "CA", "stateText": "California"},
		{"stateCode": "CO", "stateText": "Colorado"},
		{"stateCode": "CT", "stateText": "Connecticut"},
		{"stateCode": "DC", "stateText": "D.C."},
		{"stateCode": "DE", "stateText": "Delaware"},
		{"stateCode": "FL", "stateText": "Florida"},
		{"stateCode": "FM", "stateText": "Micronesia"},
		{"stateCode": "GA", "stateText": "Georgia"},
		{"stateCode": "GU", "stateText": "Guam"},
		{"stateCode": "HI", "stateText": "Hawaii"},
		{"stateCode": "IA", "stateText": "Iowa"},
		{"stateCode": "ID", "stateText": "Idaho"},
		{"stateCode": "IL", "stateText": "Illinois"},
		{"stateCode": "IN", "stateText": "Indiana"},
		{"stateCode": "KS", "stateText": "Kansas"},
		{"stateCode": "KY", "stateText": "Kentucky"},
		{"stateCode": "LA", "stateText": "Louisiana"},
		{"stateCode": "MA", "stateText": "Massachusetts"},
		{"stateCode": "MD", "stateText": "Maryland"},
		{"stateCode": "ME", "stateText": "Maine"},
		{"stateCode": "MH", "stateText": "Marshall Islands"},
		{"stateCode": "MI", "stateText": "Michigan"},
		{"stateCode": "MN", "stateText": "Minnesota"},
		{"stateCode": "MO", "stateText": "Missouri"},
		{"stateCode": "MP", "stateText": "Marianas"},
		{"stateCode": "MS", "stateText": "Mississippi"},
		{"stateCode": "MT", "stateText": "Montana"},
		{"stateCode": "NC", "stateText": "North Carolina"},
		{"stateCode": "ND", "stateText": "North Dakota"},
		{"stateCode": "NH", "stateText": "New Hampshire"},
		{"stateCode": "NJ", "stateText": "New Jersey"},
		{"stateCode": "NM", "stateText": "New Mexico"},
		{"stateCode": "NV", "stateText": "Nevada"},
		{"stateCode": "NY", "stateText": "New York"},
		{"stateCode": "OH", "stateText": "Ohio"},
		{"stateCode": "OK", "stateText": "Oklahoma"},
		{"stateCode": "OR", "stateText": "Oregon"},
		{"stateCode": "PA", "stateText": "Pennsylvania"},
		{"stateCode": "PR", "stateText": "Puerto Rico"},
		{"stateCode": "PW", "stateText": "Palau"},
		{"stateCode": "RI", "stateText": "Rhode Island"},
		{"stateCode": "SC", "stateText": "South Carolina"},
		{"stateCode": "SD", "stateText": "South Dakota"},
		{"stateCode": "TN", "stateText": "Tennessee"},
		{"stateCode": "TX", "stateText": "Texas"},
		{"stateCode": "UT", "stateText": "Utah"},
		{"stateCode": "VA", "stateText": "Virginia"},
		{"stateCode": "VI", "stateText": "Virgin Islands"},
		{"stateCode": "VT", "stateText": "Vermont"},
		{"stateCode": "WA", "stateText": "Washington"},
		{"stateCode": "WI", "stateText": "Wisconsin"},
		{"stateCode": "WV", "stateText": "West Virginia"},
		{"stateCode": "WY", "stateText": "Wyoming"},
		{"stateCode": "AA", "stateText": "Military Americas"},
		{"stateCode": "AE", "stateText": "Military Europe/ME/Canada"},
		{"stateCode": "AP", "stateText": "Military Pacific"}
	]
}; // END object: US

var CA = { "state": [
		{"stateCode": "", "stateText": "Select State"},
		{"stateCode": "CA", "stateText": "---Canada Provinces---"},
		{"stateCode": "AB", "stateText": "Alberta"},
		{"stateCode": "BC", "stateText": "British Columbia"},
		{"stateCode": "MB", "stateText": "Manitoba"},
		{"stateCode": "NB", "stateText": "New Brunswick"},
		{"stateCode": "NL", "stateText": "Newfoundland and Labrador"},
		{"stateCode": "NS", "stateText": "Nova Scotia"},
		{"stateCode": "NT", "stateText": "Northwest Territories"},
		{"stateCode": "NU", "stateText": "Nunavut"},
		{"stateCode": "ON", "stateText": "Ontario"},
		{"stateCode": "PE", "stateText": "Prince Edward Island"},
		{"stateCode": "QC", "stateText": "Quebec"},
		{"stateCode": "SK", "stateText": "Saskatchewan"},
		{"stateCode": "YT", "stateText": "Yukon Territory"}
	]
}; // END object: CA

// Country data table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var country = { "state": [
		{"stateCode": "", "stateText": "Select Country"},
		{"stateCode": "US", "stateText": "USA"},
		{"stateCode": "CA", "stateText": "Canada"},
		{"stateCode": "NA", "stateText": "Other Country"}
	]
}; // END object: country

var postState = "";
var postCountry = "";

function TrimString(sInString)
{
	if ( sInString )
	{
		sInString = sInString.replace( /^\s+/g, "" );// strip leading
		return sInString.replace( /\s+$/g, "" );// strip trailing
	}
} // END function: TrimString

//Billing Specific: Populates the country selected with the counties from the country list

function populateCountry( countryFieldID, defaultCountryCode )
{
	var countryField = document.getElementById( countryFieldID );
	
	if ( postCountry != "" )
		defaultCountry = postCountry;
		
	countryField.selectedIndex = 0;
	
	for ( var i = 0; i < country.state.length; i++ )
	{
		countryField[i] = new Option( country.state[i].stateText, country.state[i].stateCode );
		
		if ( defaultCountryCode.toLowerCase() == country.state[i].stateCode.toLowerCase() )
			countryField.selectedIndex = i;
	}
} // END function: populateCountry

function populateState( countryFieldID, stateFieldID )
{
	var countryField = document.getElementById( countryFieldID );
	var stateField = document.getElementById( stateFieldID );
	var stateFieldParent = stateField.parentNode;
	var newStateField;
	
	if ( countryField.value == "US" || countryField.value == "CA" )
	{
		var stateListObject;
		
		newStateField = document.createElement( "select" );
		newStateField.setAttribute( "name", stateField.name );
		newStateField.setAttribute( "id", stateField.id );
		newStateField.setAttribute( "class", "region" );
		
		if ( countryField.value == "US" )
			stateListObject = US;
		else if ( countryField.value == "CA" )
			stateListObject = CA;
		else
			stateListObject = null;
		
		newStateField.selectedIndex = 0;
		
		for ( var i = 0; i < stateListObject.state.length; i++ )
		{
			newStateField[i] = new Option( stateListObject.state[i].stateText, stateListObject.state[i].stateCode );
		
			if ( postState.toLowerCase() == stateListObject.state[i].stateCode.toLowerCase() )
				newStateField.selectedIndex = i;
		}
	}
	else
	{
		newStateField = document.createElement( "input" );
		
		newStateField.setAttribute( "id", stateField.id );
		newStateField.setAttribute( "name", stateField.name );
		newStateField.setAttribute( "type", "text" );
		
		if ( countryField.value == "" )
			newStateField.setAttribute( "value", "Select State" );
		else if ( countryField.value == "NA" )
			newStateField.setAttribute( "value", "NA-International" );
			
		newStateField.setAttribute( "readonly", "");
		newStateField.setAttribute( "style", "background-color:#CCCCCC" );
	}

	stateFieldParent.removeChild( stateField );
	stateFieldParent.appendChild( newStateField );
} // END function: populateState

function initCountry( countryFieldID, defaultCountryCode, stateFieldID )
{
	populateCountry( countryFieldID, defaultCountryCode );
	populateState( countryFieldID, stateFieldID );
} // END function: initCountry



