// This script should now be obsolete. Its functionalities should all be handled
// in GWT now.

// 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 USA = { "state": [
		{"stateCode": "", "stateText": "Select State"},
		{"stateCode": "AL", "stateText": "Alabama"},
		{"stateCode": "AK", "stateText": "Alaska"},
		{"stateCode": "AZ", "stateText": "Arizona"},
		{"stateCode": "AR", "stateText": "Arkansas"},
		{"stateCode": "CA", "stateText": "California"},
		{"stateCode": "CO", "stateText": "Colorado"},
		{"stateCode": "CT", "stateText": "Connecticut"},
		{"stateCode": "DE", "stateText": "Delaware"},
		{"stateCode": "DC", "stateText": "D.C."},
		{"stateCode": "FL", "stateText": "Florida"},
		{"stateCode": "GA", "stateText": "Georgia"},
		{"stateCode": "HI", "stateText": "Hawaii"},
		{"stateCode": "ID", "stateText": "Idaho"},
		{"stateCode": "IL", "stateText": "Illinois"},
		{"stateCode": "IN", "stateText": "Indiana"},
		{"stateCode": "IA", "stateText": "Iowa"},
		{"stateCode": "KS", "stateText": "Kansas"},
		{"stateCode": "KY", "stateText": "Kentucky"},
		{"stateCode": "LA", "stateText": "Louisiana"},
		{"stateCode": "ME", "stateText": "Maine"},
		{"stateCode": "MD", "stateText": "Maryland"},
		{"stateCode": "MA", "stateText": "Massachusetts"},
		{"stateCode": "MI", "stateText": "Michigan"},
		{"stateCode": "MN", "stateText": "Minnesota"},
		{"stateCode": "MS", "stateText": "Mississippi"},
		{"stateCode": "MO", "stateText": "Missouri"},
		{"stateCode": "MT", "stateText": "Montana"},
		{"stateCode": "NE", "stateText": "Nebraska"},
		{"stateCode": "NV", "stateText": "Nevada"},
		{"stateCode": "NH", "stateText": "New Hampshire"},
		{"stateCode": "NJ", "stateText": "New Jersey"},
		{"stateCode": "NM", "stateText": "New Mexico"},
		{"stateCode": "NY", "stateText": "New York"},
		{"stateCode": "NC", "stateText": "North Carolina"},
		{"stateCode": "ND", "stateText": "North Dakota"},
		{"stateCode": "OH", "stateText": "Ohio"},
		{"stateCode": "OK", "stateText": "Oklahoma"},
		{"stateCode": "OR", "stateText": "Oregon"},
		{"stateCode": "PA", "stateText": "Pennsylvania"},
		{"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": "VT", "stateText": "Vermont"},
		{"stateCode": "VA", "stateText": "Virginia"},
		{"stateCode": "WA", "stateText": "Washington"},
		{"stateCode": "WV", "stateText": "West Virginia"},
		{"stateCode": "WI", "stateText": "Wisconsin"},
		{"stateCode": "WY", "stateText": "Wyoming"},
		{"stateCode": "AS", "stateText": "American Samoa"},
		{"stateCode": "GU", "stateText": "Guam"},
		{"stateCode": "MP", "stateText": "Mariana Islands"},
		{"stateCode": "MH", "stateText": "Marshall Islands"},
		{"stateCode": "FM", "stateText": "Micronesia"},
		{"stateCode": "PW", "stateText": "Palau"},
		{"stateCode": "PR", "stateText": "Puerto Rico"},
		{"stateCode": "VI", "stateText": "Virgin Islands"},
		{"stateCode": "AA", "stateText": "Military Americas"},
		{"stateCode": "AE", "stateText": "Military Europe/ME/Canada"},
		{"stateCode": "AP", "stateText": "Military Pacific"}
	]
}; // END object: US

var CAN = { "state": [
		{"stateCode": "", "stateText": "Select State"},
		{"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": "AMS", "stateText": "America Samoa"},
		{"stateCode": "ANT", "stateText": "Antigua"},
		{"stateCode": "AUG", "stateText": "Arabian Gulf"},
		{"stateCode": "ARG", "stateText": "Argentina"},
		{"stateCode": "ARU", "stateText": "Aruba"},
		{"stateCode": "AUS", "stateText": "Australia"},
		{"stateCode": "BAH", "stateText": "Bahamas"},
		{"stateCode": "BHR", "stateText": "Bahrain"},
		{"stateCode": "BNG", "stateText": "Bangladesh"},
		{"stateCode": "BAR", "stateText": "Barbados"},
		{"stateCode": "BLA", "stateText": "Belarus"},
		{"stateCode": "BEL", "stateText": "Belgium"},
		{"stateCode": "BLZ", "stateText": "Belize"},
		{"stateCode": "BEN", "stateText": "Benin South Africa"},
		{"stateCode": "BER", "stateText": "Bermuda"},
		{"stateCode": "BOL", "stateText": "Bolivia"},
		{"stateCode": "BOS", "stateText": "Bosnia-Herzegovina"},
		{"stateCode": "BRZ", "stateText": "Botswana"},
		{"stateCode": "BRU", "stateText": "Brazil"},
		{"stateCode": "BW", "stateText": "British Virgin Islands"},
		{"stateCode": "BWI", "stateText": "British West Indies"},
		{"stateCode": "BVI", "stateText": "Brunei Darussalam"},
		{"stateCode": "BUL", "stateText": "Bulgaria"},		
		{"stateCode": "CAN", "stateText": "Canada"},
		{"stateCode": "CAM", "stateText": "Central America"},
		{"stateCode": "CH", "stateText": "Chile"},
		{"stateCode": "CHI", "stateText": "China"},
		{"stateCode": "COL", "stateText": "Colombia"},
		{"stateCode": "COP", "stateText": "Copenhagen"},
		{"stateCode": "CR", "stateText": "Costa Rica"},
		{"stateCode": "CRO", "stateText": "Croatia (Hrvatska)"},
		{"stateCode": "CUB", "stateText": "Cuba"},
		{"stateCode": "CYP", "stateText": "Cyprus"},
		{"stateCode": "CZH", "stateText": "Czech Republic & Slovakia"},
		{"stateCode": "DEN", "stateText": "Denmark"},
		{"stateCode": "DR", "stateText": "Dominican Republic"},
		{"stateCode": "EAF", "stateText": "East Africa"},
		{"stateCode": "ECU", "stateText": "Ecuador"},
		{"stateCode": "EGP", "stateText": "Egypt"},
		{"stateCode": "ELS", "stateText": "El Salvador"},
		{"stateCode": "ENG", "stateText": "England"},
		{"stateCode": "ETH", "stateText": "Ethiopia"},
		{"stateCode": "FJI", "stateText": "Fiji"},
		{"stateCode": "FIN", "stateText": "Finland"},
		{"stateCode": "FRA", "stateText": "France"},
		{"stateCode": "FRP", "stateText": "French Polynesia"},
		{"stateCode": "FWI", "stateText": "French West Indies"},
		{"stateCode": "GER", "stateText": "Germany"},
		{"stateCode": "GHA", "stateText": "Ghana"},
		{"stateCode": "GIB", "stateText": "Gibraltar"},
		{"stateCode": "GRE", "stateText": "Greece"},
		{"stateCode": "GUA", "stateText": "Guatemala"},
		{"stateCode": "GNA", "stateText": "Guyana"},
		{"stateCode": "HTI", "stateText": "Haiti"},
		{"stateCode": "HOL", "stateText": "Holland"},
		{"stateCode": "HON", "stateText": "Honduras"},
		{"stateCode": "HK", "stateText": "Hong Kong"},
		{"stateCode": "HUN", "stateText": "Hungary"},
		{"stateCode": "ICD", "stateText": "Iceland"},
		{"stateCode": "IDA", "stateText": "India"},
		{"stateCode": "IDO", "stateText": "Indonesia"},
		{"stateCode": "IRA", "stateText": "Iran"},
		{"stateCode": "IRE", "stateText": "Ireland"},
		{"stateCode": "ISR", "stateText": "Israel"},
		{"stateCode": "ITL", "stateText": "Italy"},
		{"stateCode": "IVC", "stateText": "Ivory Coast"},
		{"stateCode": "JAM", "stateText": "Jamaica"},
		{"stateCode": "JAP", "stateText": "Japan"},
		{"stateCode": "JOR", "stateText": "Jordan"},
		{"stateCode": "KYA", "stateText": "Kenya"},
		{"stateCode": "KOR", "stateText": "Korea"},
		{"stateCode": "KWT", "stateText": "Kuwait"},
		{"stateCode": "LAO", "stateText": "Laos"},
		{"stateCode": "LAT", "stateText": "Latvia"},
		{"stateCode": "LEB", "stateText": "Lebanon"},
		{"stateCode": "LCT", "stateText": "Liechtenstein"},
		{"stateCode": "LIT", "stateText": "Lithuania"},
		{"stateCode": "LUX", "stateText": "Luxembourg"},
		{"stateCode": "MAD", "stateText": "Madagascar"},
		{"stateCode": "MAL", "stateText": "Malaysia"},
		{"stateCode": "MLD", "stateText": "Maldives"},
		{"stateCode": "MLT", "stateText": "Malta"},
		{"stateCode": "MAU", "stateText": "Mauritius"},
		{"stateCode": "MEX", "stateText": "Mexico"},
		{"stateCode": "MOL", "stateText": "Moldova"},
		{"stateCode": "MNO", "stateText": "Monaco"},
		{"stateCode": "MON", "stateText": "Mongolia"},
		{"stateCode": "MOR", "stateText": "Morocco"},
		{"stateCode": "NAM", "stateText": "Namibia"},
		{"stateCode": "NET", "stateText": "Netherlands"},
		{"stateCode": "NA", "stateText": "Netherlands Antilles"},
		{"stateCode": "NG", "stateText": "New Guinea"},
		{"stateCode": "NZ", "stateText": "New Zealand"},
		{"stateCode": "NIC", "stateText": "Nicaragua"},
		{"stateCode": "NIG", "stateText": "Nigeria"},
		{"stateCode": "NOR", "stateText": "Norway"},
		{"stateCode": "OMN", "stateText": "Oman"},
		{"stateCode": "PAK", "stateText": "Pakistan"},
		{"stateCode": "PAL", "stateText": "Palau"},
		{"stateCode": "PAN", "stateText": "Panama"},
		{"stateCode": "PNG", "stateText": "Papua New Guinea"},
		{"stateCode": "PGY", "stateText": "Paraguay"},
		{"stateCode": "PRC", "stateText": "People's Rep China"},
		{"stateCode": "PER", "stateText": "Peru"},
		{"stateCode": "PHI", "stateText": "Philippines"},
		{"stateCode": "POL", "stateText": "Poland"},
		{"stateCode": "POR", "stateText": "Portugal"},
		{"stateCode": "QTR", "stateText": "Qatar"},
		{"stateCode": "ROC", "stateText": "Republic of China-Taiwan"},
		{"stateCode": "ROM", "stateText": "Romania"},
		{"stateCode": "RUS", "stateText": "Russia"},
		{"stateCode": "SAI", "stateText": "Saipan"},
		{"stateCode": "SAD", "stateText": "Santo Domino"},
		{"stateCode": "SAR", "stateText": "Saudi Arabia"},
		{"stateCode": "SCO", "stateText": "Scotland"},
		{"stateCode": "SIN", "stateText": "Singapore"},
		{"stateCode": "SLO", "stateText": "Slovenia"},
		{"stateCode": "SOL", "stateText": "Solomon Islands"},
		{"stateCode": "SAF", "stateText": "South Africa"},
		{"stateCode": "KOS", "stateText": "South Korea"},
		{"stateCode": "SPN", "stateText": "Spain"},
		{"stateCode": "SRI", "stateText": "Sri Lanka"},
		{"stateCode": "SCX", "stateText": "St. Croix"},
		{"stateCode": "STL", "stateText": "St. Lucia"},
		{"stateCode": "SWE", "stateText": "Sweden"},
		{"stateCode": "SWZ", "stateText": "Switzerland"},
		{"stateCode": "SYR", "stateText": "Syria"},
		{"stateCode": "THI", "stateText": "Thailand"},
		{"stateCode": "TNG", "stateText": "Tonga"},
		{"stateCode": "TKY", "stateText": "Turkey"},
		{"stateCode": "UGA", "stateText": "Uganda"},
		{"stateCode": "UKR", "stateText": "Ukraine"},
		{"stateCode": "UAE", "stateText": "United Arab Emirates"},
		{"stateCode": "UK", "stateText": "United Kingdom"},
		{"stateCode": "URU", "stateText": "Uruguay"},
		{"stateCode": "UZB", "stateText": "Uzbekistan"},
		{"stateCode": "VAN", "stateText": "Vanuatu South Pacific"},
		{"stateCode": "VEN", "stateText": "Venezuela"},
		{"stateCode": "VTM", "stateText": "Vietnam"},
		{"stateCode": "WAL", "stateText": "Wales"},
		{"stateCode": "WI", "stateText": "West Indies"},
		{"stateCode": "YUG", "stateText": "Yugoslavia"},
		{"stateCode": "ZAI", "stateText": "Zaire"},
		{"stateCode": "ZAM", "stateText": "Zambia"},
		{"stateCode": "ZMB", "stateText": "Zimbabwe"}
	]
}; // END object: country

var postState = "";
var postCountry = "";
var prePopState = "";
var prePopCountry = "";

function prePopulate( country, state )
{
	prePopState = state;
	prePopCountry = country;
}

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;
	
	for ( var i = 0; i < country.state.length; i++ )
	{
		var countryField2 = document.createElement( "option" );	
		countryField2.setAttribute( "value", country.state[i].stateCode );
		if( country.state[i].stateCode == prePopCountry )
		{
			countryField2.setAttribute( "selected", "selected");
		}
		var text = document.createTextNode(country.state[i].stateText);
		countryField2.appendChild(text);
		countryField.appendChild(countryField2);	

	}
} // 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 == "CAN" )
	{	
		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 = USA;
		else if ( countryField.value == "CAN" )
			stateListObject = CAN;
		else
			stateListObject = null;
		
		for ( var i = 0; i < stateListObject.state.length; i++ )
		{
		
			var newStateField2 = document.createElement( "option" );	
			newStateField2.setAttribute( "value", stateListObject.state[i].stateCode );
			if( stateListObject.state[i].stateCode == prePopState )
			{
				newStateField2.setAttribute( "selected", "selected" );
			}
			var text = document.createTextNode(stateListObject.state[i].stateText);
			newStateField2.appendChild(text);
			newStateField.appendChild(newStateField2);	

		}
	}
	else
	{
		newStateField = document.createElement( "select" );
		newStateField.setAttribute( "name", stateField.name );
		newStateField.setAttribute( "id", stateField.id );
		newStateField.setAttribute( "class", "region" );
		newStateField2 = document.createElement( "option" );	
		newStateField2.setAttribute( "value", "NA-International" );
		var text = document.createTextNode("NA-International");
		newStateField2.appendChild(text);
		newStateField.appendChild(newStateField2);			

	}
	
	stateFieldParent.removeChild( stateField );
	stateFieldParent.appendChild( newStateField );
} // END function: populateState

function initCountry( countryFieldID, defaultCountryCode, stateFieldID )
{
	populateCountry( countryFieldID, defaultCountryCode );
	populateState( countryFieldID, stateFieldID );
} // END function: initCountry



