/***************************************************************************************************************************************************
 	Author                  :           PA
	Version		     	    :	        1.0
	Date Created	        :
   
 	Change history 
 	
	Task/Bug 			Date 		     Author 		       Change Description
	
	TracNo: 1065		26-05-2008	     SNasina		       This file is added for to display only Country DropDown in jsp pages
	
	Global Shipping 	28-05-2008   	 SGopidinne            initializeCatalogPage() is added to remove countryStatesDropDownForCatalog.js.This file is 
															   duplicate of countryStatesDropDown.js file.This is used in request catalog page to hide UK 
															   from country dropdown. 
															
	Global Shipping     28-05-2008       SGopidinne            Modified to add Comments for Global Shipping.	
	
	
	Trac 1097           09-Jun-2008       Pveedu         	   initializeshippingpage()has modified to add  some code for hiding giftmessage check box.
	
	Trac 1054           18-June-2008      Pveedu         	   Added code for displaying list of countries in country dropdown of profile page. 
	
	Trac 1054           19-June-2008      Pveedu         	   Modified code for displaying list of countries in the alphabetical order for country names in country dropdown of profile page.  											
	  											
	Allurent Code		08-09-2008		  CLohr				   Allurent added a reference to an older version of the Prototype JS library; needed to update code to work with this version
	Conflict
	
	Bug 219<Scottish    22-Sep-2008       PSahu                Modified for not to display Scottish Highlands in the country dropdown list in the international.jsp
	Highlands should 
	not be listed in 
	the country 
	dropdown in the 
	international.jsp>

		
****************************************************************************************************************************************************/

function CountryObject(pObject){
	this.mCountryCode = null;
	this.mCountryName = null;
}

function StateObject(pObject){
	this.mStateCode = null;
	this.mStateName = null;
}

/*
Object.prototype.getKeys = function(){
	var Keys = new Array();
	for(var i in this){
		if(i in Object)
			continue;
		Keys[Keys.length] = i; //get the key
	}
	return Keys;
}

Object.prototype.getValues = function(){
	var Values = new Array();
	for(var i in this){
		if(i in Object)
			continue;
		Values[Values.length] = this[i]; //get the value of that key
	}
	return Values;
}
*/

// to populate list of countries in country dropdown
// United Kingdom and its country code are added in Array for Global Shipping.
mCountryArray = new Array("United States", "Canada", "United Kingdom","Australia"); 
mCountryArrayWithCodes = {"0000" : "United States", "0001" : "Canada", "1072" : "United Kingdom", "1007" : "Australia"};

// code modified for the Trac 1065 
// for to display only Country DropDown in jsp pages
// to populate Shipping countries in country dropdown
//Code modified for the  Trac 1204
//code modified for Bug 219  "Scottish Highlands should not be listed in the country dropdown list in the international.jsp"  
mShippingCountryArrayWithCodes = {"SELE":"Please select a country","AFG":"Afghanistan", "ALB":"Albania", "DZA":"Algeria", "ASM":"American Samoa", "AND":"Andorra", "AGO":"Angola", "AIA":"Anguilla", "ATG":"Antigua and Barbuda", "ARG":"Argentina", "ARM":"Armenia", "ABW":"Aruba", "AUS":"Australia", "AUT":"Austria", "AZE":"Azerbaijan", "BHS":"Bahamas", "BHR":"Bahrain", "BGD":"Bangladesh", "BRB":"Barbados", "BLR":"Belarus", "BEL":"Belgium", "BLZ":"Belize", "BEN":"Benin", "BMU":"Bermuda", "BTN":"Bhutan", "BOL":"Bolivia", "BIH":"Bosnia and Herzegovina", "BWA":"Botswana", "BRA":"Brazil", "VGB":"British Virgin Islands", "BRN":"Brunei Darussalam", "BGR":"Bulgaria", "BFA":"Burkina Faso", "BDI":"Burundi", "KHM":"Cambodia", "CMR":"Cameroon", "CPV":"Cape Verde", "CYM":"Cayman Islands", "CAF":"Central African Republic", "TCD":"Chad", "CHL":"Chile", "CHN":"China", "COL":"Colombia", "COM":"Comoros", "COG":"Congo", "COK":"Cook Islands", "CRI":"Costa Rica", "CIV":"Cote d'Ivoire", "HRV":"Croatia", "CUB":"Cuba", "CYP":"Cyprus", "CZE":"Czech Republic", "PRK":"Democratic People's Republic of Korea", "COD":"Democratic Republic of the Congo", "DNK":"Denmark", "DJI":"Djibouti", "DMA":"Dominica", "DOM":"Dominican Republic", "ECU":"Ecuador", "EGY":"Egypt", "SLV":"El Salvador", "GNQ":"Equatorial Guinea", "ERI":"Eritrea", "EST":"Estonia", "ETH":"Ethiopia", "FRO":"Faeroe Islands", "FLK":"Falkland Islands (Malvinas)", "FJI":"Fiji", "FIN":"Finland", "FRA":"France", "GUF":"French Guiana", "PYF":"French Polynesia", "GAB":"Gabon", "GMB":"Gambia", "GEO":"Georgia", "DEU":"Germany", "GHA":"Ghana", "GIB":"Gibraltar", "GRC":"Greece", "GRL":"Greenland", "GRD":"Grenada", "GLP":"Guadeloupe", "GUM":"Guam", "GTM":"Guatemala","GGY":"Guernsey", "GIN":"Guinea", "GNB":"Guinea-Bissau", "GUY":"Guyana", "HTI":"Haiti", "VAT":"Holy See", "HND":"Honduras", "HKG":"Hong Kong Special Administrative Region of China", "HUN":"Hungary", "ISL":"Iceland", "IND":"India", "IDN":"Indonesia", "IRN":"Iran (Islamic Republic of)", "IRQ":"Iraq", "IRL":"Ireland","IMN":"Isle of Man", "ISR":"Israel", "ITA":"Italy", "JAM":"Jamaica", "JPN":"Japan","JEY":"Jersey", "JOR":"Jordan", "KAZ":"Kazakhstan", "KEN":"Kenya", "KIR":"Kiribati", "KWT":"Kuwait", "KGZ":"Kyrgyzstan", "LAO":"Lao People's Democratic Republic", "LVA":"Latvia", "LBN":"Lebanon", "LSO":"Lesotho", "LBR":"Liberia", "LBY":"Libyan Arab Jamahiriya", "LIE":"Liechtenstein", "LTU":"Lithuania", "LUX":"Luxembourg", "MAC":"Macao Special Administrative Region of China", "MDG":"Madagascar", "MWI":"Malawi", "MYS":"Malaysia", "MDV":"Maldives", "MLI":"Mali", "MLT":"Malta", "MHL":"Marshall Islands", "MTQ":"Martinique", "MRT":"Mauritania", "MUS":"Mauritius", "MYT":"Mayotte", "MEX":"Mexico", "FSM":"Micronesia (Federated States of)", "MCO":"Monaco", "MNG":"Mongolia", "MSR":"Montserrat", "MAR":"Morocco", "MOZ":"Mozambique", "MMR":"Myanmar", "NAM":"Namibia", "NRU":"Nauru", "NPL":"Nepal", "NLD":"Netherlands", "ANT":"Netherlands Antilles", "NCL":"New Caledonia", "NZL":"New Zealand", "NIC":"Nicaragua", "NER":"Niger", "NGA":"Nigeria", "NIU":"Niue", "NFK":"Norfolk Island", "MNP":"Northern Mariana Islands", "NOR":"Norway", "PSE":"Occupied Palestinian Territory", "OMN":"Oman", "PAK":"Pakistan", "PLW":"Palau", "PAN":"Panama", "PNG":"Papua New Guinea", "PRY":"Paraguay", "PER":"Peru", "PHL":"Philippines", "PCN":"Pitcairn", "POL":"Poland", "PRT":"Portugal", "PRI":"Puerto Rico", "QAT":"Qatar", "KOR":"Republic of Korea", "MDA":"Republic of Moldova", "REU":"Reunion", "ROU":"Romania", "RUS":"Russian Federation", "RWA":"Rwanda", "SHN":"Saint Helena", "KNA":"Saint Kitts and Nevis", "LCA":"Saint Lucia", "SPM":"Saint Pierre and Miquelon", "VCT":"Saint Vincent and the Grenadines", "WSM":"Samoa", "SMR":"San Marino", "STP":"Sao Tome and Principe", "SAU":"Saudi Arabia", "SEN":"Senegal", "YUG":"Serbia and Montenegro", "SYC":"Seychelles", "SLE":"Sierra Leone", "SGP":"Singapore", "SVK":"Slovakia", "SVN":"Slovenia", "SLB":"Solomon Islands", "SOM":"Somalia", "ZAF":"South Africa", "ESP":"Spain", "LKA":"Sri Lanka", "SDN":"Sudan", "SUR":"Suriname", "SJM":"Svalbard and Jan Mayen Islands", "SWZ":"Swaziland", "SWE":"Sweden", "CHE":"Switzerland", "SYR":"Syrian Arab Republic", "TWN":"Taiwan Province of China", "TJK":"Tajikistan", "THA":"Thailand", "MKD":"The former Yugoslav Republic of Macedonia", "TLS":"Timor-Leste", "TGO":"Togo", "TKL":"Tokelau", "TON":"Tonga", "TTO":"Trinidad and Tobago", "TUN":"Tunisia", "TUR":"Turkey", "TKM":"Turkmenistan", "TCA":"Turks and Caicos Islands", "TUV":"Tuvalu", "UGA":"Uganda", "UKR":"Ukraine", "ARE":"United Arab Emirates", "TZA":"United Republic of Tanzania", "VIR":"United States Virgin Islands", "URY":"Uruguay", "UZB":"Uzbekistan", "VUT":"Vanuatu", "VEN":"Venezuela", "VNM":"Viet Nam", "WLF":"Wallis and Futuna Islands", "ESH":"Western Sahara", "YEM":"Yemen", "ZMB":"Zambia", "ZWE":"Zimbabwe"};
//end of code modified for Bug 219  "Scottish Highlands should not be listed in the country dropdown list in the international.jsp"  
//End of code modified for the  Trac 1204			
// End of code modified for the Trac 1065 

//TODO: (Felix) dropdown in Request Catalog page for Australia?
//mCountryArrayWithCodesForCatalog has added to hide United Kingdom country from dropdown in Request Catalog page.
mCountryArrayWithCodesForCatalog = {"0000" : "United States", "0001" : "Canada"};

mStatesArrayWithCodesUSA = { "AK" : "AK - Alaska","AL" : "AL - Alabama", "AZ" : "AZ - Arizona", "AR" : "AR - Arkansas", "CA" : "CA - California", 
				"CO" : "CO - Colorado","CT" : "CT - Connecticut", "DE" : "DE - Delaware", "DC" : "DC - District of Columbia", "FL" : "FL - Florida", 
				"GA" : "GA - Georgia","HI" : "HI - Hawaii", "ID" : "ID - Idaho", "IL" : "IL - Illinois", "IN" : "IN - Indiana", "IA" : "IA - Iowa", 
				"KS" : "KS - Kansas", "KY" : "KY - Kentucky","LA" : "LA - Louisiana", "ME" : "ME - Maine", "MD" : "MD - Maryland", "MA" : "MA - Massachusetts", 
				"MI" : "MI - Michigan","MN" : "MN - Minnesota", "MS" : "MS - Mississippi", "MO" : "MO - Missouri", "MT" : "MT - Montana", 
				"NE" : "NE - Nebraska", "NV" : "NV - Nevada","NH" : "NH - New Hampshire", "NJ" : "NJ - New Jersey", "NM" : "NM - New Mexico", "NY" : "NY - New York", 
				"NC" : "NC - North Carolina","ND" : "ND - North Dakota", "OH" : "OH - Ohio", "OK" : "OK - Oklahoma", "OR" : "OR - Oregon", 
				"PA" : "PA - Pennsylvania", "RI" : "RI - Rhode Island","SC" : "SC - South Carolina", "SD" : "SD - South Dakota", "TN" : "TN - Tennessee", "TX" : "TX - Texas", 
				"UT" : "UT - Utah", "VT" : "VT - Vermont", "VA" : "VA - Virginia", "WA" : "WA - Washington", "WV" : "WV - West Virginia", "WI" : "WI - Wisconsin", 
				"WY" : "WY - Wyoming","AA" : "AA - Armed Forces - America", "AE" : "AE - Armed Forces - Europe", "AP" : "AP - Armed Forces - Pacific",
				"AS" : "AS - American Samoa", "FM" : "FM - Fed. States of Micronesia", "GU" : "GU - Guam", "MH" : "MH - Marshall Islands", 
				"MP" : "MP - Northern Mariana Islands", "PW" : "PW - Palau", "PR" : "PR - Puerto Rico", "VI" : "VI - Virgin Islands"};

mStatesArrayWithCodesCanada = {"AB" : "AB - Alberta", "BC" : "BC - British Columbia", "MB" : "MB - Manitoba", "NB" : "NB - New Brunswick", 
					"NL" : "NL - Newfoundland and Labrador", "NT" : "NT - Northwest Territories", "NS" : "NS - Nova Scotia","NU" : "NU - Nunavut",
					"ON" : "ON - Ontario", "PE" : "PE - Prince Edward Islands", "QC" : "QC - Quebec", "SK" : "SK - Saskatchewan","YT" : "YT - Yukon Territory"};

mStatesArrayWithCodesAustralia = {"ACT" : "ACT - Australian Capital Territory", "NSW" : "NSW - New South Wales", "NT" : "NT - Northern Territory", "QLD" : "QLD - Queensland", 
					"SA" : "SA - South Australia", "TAS" : "TAS - Tasmania", "VIC" : "VIC - Victoria","WA" : "WA - Western Australia"};

// array of state belong to United Kingdom for Global Shipping.
mStatesArrayWithCodesUK = { "FN" : "FN - Foreign Nations"};

mCountryArrayWithCodes.getKeys = function(){
	var Keys = new Array();
	for(var i in this){
            if(i in Object)
                continue;
            Keys[Keys.length] = i; //get the key
	}
	return Keys;
}
mCountryArrayWithCodes.getValues = function(){
	var Values = new Array();
	for(var i in this){
		if(i in Object)
			continue;
                if ( typeof this[i] != "function" ) 
		Values[Values.length] = this[i]; //get the value of that key
	}
	return Values;
}
mStatesArrayWithCodesUSA.getKeys = function(){
	var Keys = new Array();
	for(var i in this){
		if(i in Object)
			continue;
		Keys[Keys.length] = i; //get the key
	}
	return Keys;
}
mStatesArrayWithCodesUSA.getValues = function(){
	var Values = new Array();
	for(var i in this){
		if(i in Object)
			continue;
                if ( typeof this[i] != "function" ) 
		Values[Values.length] = this[i]; //get the value of that key
	}
	return Values;
}
mStatesArrayWithCodesCanada.getKeys = function(){
	var Keys = new Array();
	for(var i in this){
		if(i in Object)
			continue;
		Keys[Keys.length] = i; //get the key
	}
	return Keys;
}
mStatesArrayWithCodesCanada.getValues = function(){
	var Values = new Array();
	for(var i in this){
		if(i in Object)
			continue;
                if ( typeof this[i] != "function" ) 
		Values[Values.length] = this[i]; //get the value of that key
	}
	return Values;
}
mStatesArrayWithCodesAustralia.getKeys = function(){
	var Keys = new Array();
	for(var i in this){
		if(i in Object)
			continue;
		Keys[Keys.length] = i; //get the key
	}
	return Keys;
}
mStatesArrayWithCodesAustralia.getValues = function(){
	var Values = new Array();
	for(var i in this){
		if(i in Object)
			continue;
                if ( typeof this[i] != "function" ) 
		Values[Values.length] = this[i]; //get the value of that key
	}
	return Values;
}

mStatesArrayWithCodesUK.getKeys = function(){
	var Keys = new Array();
	for(var i in this){
		if(i in Object)
			continue;
		Keys[Keys.length] = i; //get the key
	}
	return Keys;
}
mStatesArrayWithCodesUK.getValues = function(){
	var Values = new Array();
	for(var i in this){
		if(i in Object)
			continue;
                if ( typeof this[i] != "function" ) 
		Values[Values.length] = this[i]; //get the value of that key
	}
	return Values;
}
mShippingCountryArrayWithCodes.getKeys = function(){
	var Keys = new Array();
	for(var i in this){
		if(i in Object)
			continue;
		Keys[Keys.length] = i; //get the key
	}
	return Keys;
}
mShippingCountryArrayWithCodes.getValues = function(){
	var Values = new Array();
	for(var i in this){
		if(i in Object)
			continue;
                if ( typeof this[i] != "function" ) 
		Values[Values.length] = this[i]; //get the value of that key
	}
	return Values;
}
mCountryArrayWithCodesForCatalog.getKeys = function(){
	var Keys = new Array();
	for(var i in this){
		if(i in Object)
			continue;
		Keys[Keys.length] = i; //get the key
	}
	return Keys;
}
mCountryArrayWithCodesForCatalog.getValues = function(){
	var Values = new Array();
	for(var i in this){
		if(i in Object)
			continue;
                if ( typeof this[i] != "function" ) 
		Values[Values.length] = this[i]; //get the value of that key
	}
	return Values;
}

var mCountryKeys = new Array();
mCountryKeys = mCountryArrayWithCodes.getKeys();

var mCountryValues = new Array();
mCountryValues = mCountryArrayWithCodes.getValues();

var mStateKeysUSA = new Array();
mStateKeysUSA = mStatesArrayWithCodesUSA.getKeys();

var mStateValuesUSA = new Array();
mStateValuesUSA = mStatesArrayWithCodesUSA.getValues();

var mStateKeysCanada = new Array();
mStateKeysCanada = mStatesArrayWithCodesCanada.getKeys();

var mStateValuesCanada = new Array();
mStateValuesCanada = mStatesArrayWithCodesCanada.getValues();

var mStateKeysAustralia = new Array();
mStateKeysAustralia = mStatesArrayWithCodesAustralia.getKeys();

var mStateValuesAustralia = new Array();
mStateValuesAustralia = mStatesArrayWithCodesAustralia.getValues();

//arrays to store UK state's value and key for Global shipping.
var mStateKeysUK = new Array();
mStateKeysUK = mStatesArrayWithCodesUK.getKeys();

var mStateValuesUK = new Array();
mStateValuesUK = mStatesArrayWithCodesUK.getValues();

var mStateKeys = new Object();
mStateKeys[mCountryArray[0]] = mStateKeysUSA;   //for United States
mStateKeys[mCountryArray[1]] = mStateKeysCanada;   //for Canada
mStateKeys[mCountryArray[2]] = mStateKeysUK;		//for United Kingdom.
mStateKeys[mCountryArray[3]] = mStateKeysAustralia;   //for Australia

mStatesAndCountries = new CountryObject();

mStatesAndCountries[mCountryArray[0]] = mStateValuesUSA;

mStatesAndCountries[mCountryArray[1]] = mStateValuesCanada;

mStatesAndCountries[mCountryArray[2]] = mStateValuesUK;

mStatesAndCountries[mCountryArray[3]] = mStateValuesAustralia;


// code modified for the Trac 1065 
// for to display only Country DropDown in jsp pages

var mShippingCountryKeys = new Array();
mShippingCountryKeys = mShippingCountryArrayWithCodes.getKeys();

var mShippingCountryValues = new Array();
mShippingCountryValues = mShippingCountryArrayWithCodes.getValues();
function initializeShippingCountry(pCountry){
	initializeCountrydropdown(mShippingCountryValues, 'shippingcountries', pCountry);
	
}
// to display the default selection for country
function initializeCountrydropdown(pArray, pCountryElem, pSelected){
	var countryElement = getElement(pCountryElem);
	var selCountryAgainstCode = null;

	if(pSelected!=null && pSelected!=''){
		if(contains(mShippingCountryArrayWithCodes.getKeys(), pSelected)){
			var index = inArray(mShippingCountryArrayWithCodes.getKeys(), pSelected);
			selCountryAgainstCode = mShippingCountryArrayWithCodes.getValues()[index];			
		}
	}
	else
		selCountryAgainstCode = pArray[0];  

	for(i = 0; i < pArray.length; i++){			
		if(selCountryAgainstCode == pArray[i]){			
			countryElement.options[i] = new Option(pArray[i], mShippingCountryKeys[i]);
			countryElement.options[i].selected = "true";						
		}
		else{
			countryElement.options[countryElement.options.length] = new Option(pArray[i], mShippingCountryKeys[i]);
		}
	}
}
// End of code modified for the Trac 1065 


//This function is added to hide United Kingdom country from dropdown in Request Catalog page.
//Here we are getting country values without UK country if pCatalogPage is CatalogPage. 
function initializeCatalogPage(pCatalogPage){
	if(pCatalogPage == "CatalogPage"){
		mCountryKeys = mCountryArrayWithCodesForCatalog.getKeys();
		mCountryValues = mCountryArrayWithCodesForCatalog.getValues();
	}
}

function initialize(pCountry, pState){
	initializeCountry(mCountryValues, 'countries', pCountry);
	//code to hide state dropdown .
	if(pCountry == "1072"){
		initializeStates(mCountryValues, 'states', pState);
		getElement( "states" ).style.display = "none";
		getElement( "Province" ).style.display = "none";
		getElement( "states" ).value = "FN";
		//Here we are hiding the pobox field.								
		getElement( "PoBoxAddress" ).style.display = "none";
	} else if(pCountry == "1007") {
		initializeStates(mCountryValues, 'states', pState);
		//Here we are hiding the pobox field.
		//Are we relying on the fact that it is de-selected by default?
		getElement( "PoBoxAddress" ).style.display = "none";
	} else {
		initializeStates(mCountryValues, 'states', pState);
	}
}

// function to hide state dropdown and gift card checkbox in checkout shipping page.
function initializeshippingpage(pCountry, pState){
	initializeCountry(mCountryValues, 'countries', pCountry);
	     
	      // code is  modified for the  Trac1097. Here it will display Giftmessage check box if the country is US from 
	      //the country drop down and other countrys from the country drop down it will hide the Giftmessage check box.
	     if(pCountry =='' || pCountry == null || pCountry == "0000" )
			{ 
			getElement( "giftmessagecheckbox" ).style.display = "";  
			 }
		else {
			getElement( "giftmessagecheckbox" ).style.display = "none"; 
			}
	     //End of code modified for the Trac1097
	    
	if(pCountry == "1072") {
		initializeStates(mCountryValues, 'states', pState);
		getElement( "states" ).style.display = "none";
		getElement( "Province" ).style.display = "none";
		getElement( "states" ).value = "FN";
		//Here we are hiding the pobox field.								
		getElement( "PoBoxAddress" ).style.display = "none";
	} else if(pCountry == "1007") {
		initializeStates(mCountryValues, 'states', pState);
		getElement( "PoBoxAddress" ).style.display = "none";
	} else {
		initializeStates(mCountryValues, 'states', pState);
	}
}

// to display the default selection for country and state values. first Elem in the country Array and its 
//states will be displayed
function initializeCountry(pArray, pCountryElem, pSelected){
	var countryElement = getElement(pCountryElem);
	var selCountryAgainstCode = null;

	if(pSelected!=null && pSelected!=''){
		if(contains(mCountryArrayWithCodes.getKeys(), pSelected)){
			var index = inArray(mCountryArrayWithCodes.getKeys(), pSelected);
			selCountryAgainstCode = mCountryArrayWithCodes.getValues()[index];			
		}
	}
	else
		selCountryAgainstCode = pArray[0];  //default value United States

	for(i = 0; i < pArray.length; i++){			
		if(selCountryAgainstCode == pArray[i]){			
			countryElement.options[i] = new Option(pArray[i], mCountryKeys[i]);
			countryElement.options[i].selected = "true";						
			mStatesAndCountries.mCountryName = pArray[i];
		}
		else{
			countryElement.options[countryElement.options.length] = new Option(pArray[i], mCountryKeys[i]);
		}
	}
}

function initializeStates(pArray, pStateElem, pSelected){
	var stateElement = getElement(pStateElem);
	var defaultCountry = null;
	var key=null;	
	var selectedValAgainstKey = null;
	
	// set default country
	if (document.getElementById("countries")) {
		defaultCountry = document.getElementById("countries").options[document.getElementById("countries").selectedIndex].text;
	}
	
	if(pSelected != null && pSelected != ''){

		/* overwriting selectedValAgainstKey and index values; upgraded to if else conditionals
		if(contains(mStatesArrayWithCodesUSA.getKeys(), pSelected)){							
			var index = inArray(mStatesArrayWithCodesUSA.getKeys(), pSelected);
			selectedValAgainstKey = mStatesArrayWithCodesUSA.getValues()[index];
		}
		if(contains(mStatesArrayWithCodesCanada.getKeys(), pSelected)){
			var index = inArray(mStatesArrayWithCodesCanada.getKeys(), pSelected);
			selectedValAgainstKey = mStatesArrayWithCodesCanada.getValues()[index];
		}
		if(contains(mStatesArrayWithCodesAustralia.getKeys(), pSelected)){
			var index = inArray(mStatesArrayWithCodesAustralia.getKeys(), pSelected);
			selectedValAgainstKey = mStatesArrayWithCodesAustralia.getValues()[index];
		}
		if(contains(mStatesArrayWithCodesUK.getKeys(), pSelected)){							
			var index = inArray(mStatesArrayWithCodesUK.getKeys(), pSelected);
			selectedValAgainstKey = mStatesArrayWithCodesUK.getValues()[index];
		}
		*/
		
		if ((defaultCountry == "United States") && (contains(mStatesArrayWithCodesUSA.getKeys(), pSelected))) {							
			var index = inArray(mStatesArrayWithCodesUSA.getKeys(), pSelected);
			selectedValAgainstKey = mStatesArrayWithCodesUSA.getValues()[index];
		} else if ((defaultCountry == "Canada") && (contains(mStatesArrayWithCodesCanada.getKeys(), pSelected))) {
			var index = inArray(mStatesArrayWithCodesCanada.getKeys(), pSelected);
			selectedValAgainstKey = mStatesArrayWithCodesCanada.getValues()[index];
		} else if ((defaultCountry == "United Kingdom") && (contains(mStatesArrayWithCodesUK.getKeys(), pSelected))) {							
			var index = inArray(mStatesArrayWithCodesUK.getKeys(), pSelected);
			selectedValAgainstKey = mStatesArrayWithCodesUK.getValues()[index];
		} else if ((defaultCountry == "Australia") && (contains(mStatesArrayWithCodesAustralia.getKeys(), pSelected))) {
			var index = inArray(mStatesArrayWithCodesAustralia.getKeys(), pSelected);
			selectedValAgainstKey = mStatesArrayWithCodesAustralia.getValues()[index];
		}	
		
		/* obsolete way of setting defaultCountry variable
		for(i=0; i<pArray.length; i++){			
			if(contains(mStatesAndCountries[pArray[i]], selectedValAgainstKey)){				
				defaultCountry = pArray[i];			
			}
			else{
				if(mStatesAndCountries.mCountryName!=null)
					defaultCountry = mStatesAndCountries.mCountryName; // for cases when incorrect state Code is passed
				else
					defaultCountry = pArray[0];  //giving default value when state n ccountry code is null
			}
		}
		*/
		
		var statesDisplay = mStatesAndCountries[defaultCountry];	//previous selection is firstElem in the states
		
		for(i=0, len = statesDisplay.length; i<len; i++){
			if(selectedValAgainstKey == statesDisplay[i]){								
				stateElement.options[i] = new Option(statesDisplay[i], mStateKeys[defaultCountry][i]);
				stateElement.options[i].selected = "true";
			}
			else
				stateElement.options[stateElement.options.length] = new Option(statesDisplay[i], mStateKeys[defaultCountry][i]);
		}
	}
	else{		
		if(mStatesAndCountries.mCountryName!=null){
			var statesDisplay = mStatesAndCountries[mStatesAndCountries.mCountryName]; // countryElem passed is taken into consideration
			key = mStatesAndCountries.mCountryName;            //key to make sure if state is not passed, states for the respective 
													            //country is populated			
		}
		else{
			var statesDisplay = mStatesAndCountries[pArray[0]];	//key is firstElem incase both state n country is not passed		
			key = pArray[0];			
		}
		for(i=0, len = statesDisplay.length; i<len; i++){			
			stateElement.options[stateElement.options.length] = new Option(statesDisplay[i], mStateKeys[key][i]);		
		}
	}
}

function showStates(pCountry, pElem){
	var stateElem = getElement(pElem);
	if(!stateElem)
		return;
	var selectedCountry = pCountry.options[pCountry.selectedIndex].text;
	var statesDisplay = mStatesAndCountries[selectedCountry];
	//statesDisplay.unshift('');  //enter null value as the firstElem incase of any change
	//mStateKeys[selectedCountry].unshift('');  //enter null for code array also
	while(stateElem.firstChild){
		stateElem.removeChild(stateElem.firstChild);
	}
	for(i=0, len = statesDisplay.length; i<len; i++){
		stateElem.options[stateElem.options.length] = new Option(statesDisplay[i], mStateKeys[selectedCountry][i]);		
	}
	//statesDisplay.shift(); // roll back the entry of null element
	//mStateKeys[selectedCountry].shift();
}

function getElement(pElementId){
	return document.all? document.all[pElementId] : document.getElementById(pElementId);
}

function contains (pArray, pValue){
	for(var i=0; i<pArray.length; i++){
		if(pArray[i] == pValue){
			return true;
		}
	}
	return false;
}

function inArray(pArray, pValue){
	for(var i=0; i<pArray.length; i++){
		if(pArray[i] == pValue){
			return i;
		}
	}
	return null;
}
// code is  added for the  Trac 1054

// to populate list of countries in the alphabetical order for country names  in country dropdown of profile page
function initializeProfileCountries(pCountry){

	var mShippingCountryArrayWithCodes = new Array();
	mShippingCountryArrayWithCodes = { "USA":"United States","AFG":"Afghanistan", "ALB":"Albania", "DZA":"Algeria", "ASM":"American Samoa", "AND":"Andorra", "AGO":"Angola", "AIA":"Anguilla", "ATG":"Antigua and Barbuda", "ARG":"Argentina", "ARM":"Armenia", "ABW":"Aruba", "AUS":"Australia", "AUT":"Austria", "AZE":"Azerbaijan", "BHS":"Bahamas", "BHR":"Bahrain", "BGD":"Bangladesh", "BRB":"Barbados", "BLR":"Belarus", "BEL":"Belgium", "BLZ":"Belize", "BEN":"Benin", "BMU":"Bermuda", "BTN":"Bhutan", "BOL":"Bolivia", "BIH":"Bosnia and Herzegovina", "BWA":"Botswana", "BRA":"Brazil", "VGB":"British Virgin Islands", "BRN":"Brunei Darussalam", "BGR":"Bulgaria", "BFA":"Burkina Faso", "BDI":"Burundi", "KHM":"Cambodia", "CMR":"Cameroon", "CAN":"Canada", "CPV":"Cape Verde", "CYM":"Cayman Islands", "CAF":"Central African Republic", "TCD":"Chad", "CHL":"Chile", "CHN":"China", "COL":"Colombia", "COM":"Comoros", "COG":"Congo", "COK":"Cook Islands", "CRI":"Costa Rica", "CIV":"Cote d'Ivoire", "HRV":"Croatia", "CUB":"Cuba", "CYP":"Cyprus", "CZE":"Czech Republic", "PRK":"Democratic People's Republic of Korea", "COD":"Democratic Republic of the Congo", "DNK":"Denmark", "DJI":"Djibouti", "DMA":"Dominica", "DOM":"Dominican Republic", "ECU":"Ecuador", "EGY":"Egypt", "SLV":"El Salvador", "GNQ":"Equatorial Guinea", "ERI":"Eritrea", "EST":"Estonia", "ETH":"Ethiopia", "FRO":"Faeroe Islands", "FLK":"Falkland Islands (Malvinas)", "FJI":"Fiji", "FIN":"Finland", "FRA":"France", "GUF":"French Guiana", "PYF":"French Polynesia", "GAB":"Gabon", "GMB":"Gambia", "GEO":"Georgia", "DEU":"Germany", "GHA":"Ghana", "GIB":"Gibraltar", "GRC":"Greece", "GRL":"Greenland", "GRD":"Grenada", "GLP":"Guadeloupe", "GUM":"Guam", "GTM":"Guatemala", "GIN":"Guinea", "GNB":"Guinea-Bissau", "GUY":"Guyana", "HTI":"Haiti", "VAT":"Holy See", "HND":"Honduras", "HKG":"Hong Kong Special Administrative Region of China", "HUN":"Hungary", "ISL":"Iceland", "IND":"India", "IDN":"Indonesia", "IRN":"Iran (Islamic Republic of)", "IRQ":"Iraq", "IRL":"Ireland", "ISR":"Israel", "ITA":"Italy", "JAM":"Jamaica", "JPN":"Japan", "JOR":"Jordan", "KAZ":"Kazakhstan", "KEN":"Kenya", "KIR":"Kiribati", "KWT":"Kuwait", "KGZ":"Kyrgyzstan", "LAO":"Lao People's Democratic Republic", "LVA":"Latvia", "LBN":"Lebanon", "LSO":"Lesotho", "LBR":"Liberia", "LBY":"Libyan Arab Jamahiriya", "LIE":"Liechtenstein", "LTU":"Lithuania", "LUX":"Luxembourg", "MAC":"Macao Special Administrative Region of China", "MDG":"Madagascar", "MWI":"Malawi", "MYS":"Malaysia", "MDV":"Maldives", "MLI":"Mali", "MLT":"Malta", "MHL":"Marshall Islands", "MTQ":"Martinique", "MRT":"Mauritania", "MUS":"Mauritius", "MYT":"Mayotte", "MEX":"Mexico", "FSM":"Micronesia (Federated States of)", "MCO":"Monaco", "MNG":"Mongolia", "MSR":"Montserrat", "MAR":"Morocco", "MOZ":"Mozambique", "MMR":"Myanmar", "NAM":"Namibia", "NRU":"Nauru", "NPL":"Nepal", "NLD":"Netherlands", "ANT":"Netherlands Antilles", "NCL":"New Caledonia", "NZL":"New Zealand", "NIC":"Nicaragua", "NER":"Niger", "NGA":"Nigeria", "NIU":"Niue", "NFK":"Norfolk Island", "MNP":"Northern Mariana Islands", "NOR":"Norway", "PSE":"Occupied Palestinian Territory", "OMN":"Oman", "PAK":"Pakistan", "PLW":"Palau", "PAN":"Panama", "PNG":"Papua New Guinea", "PRY":"Paraguay", "PER":"Peru", "PHL":"Philippines", "PCN":"Pitcairn", "POL":"Poland", "PRT":"Portugal", "PRI":"Puerto Rico", "QAT":"Qatar", "KOR":"Republic of Korea", "MDA":"Republic of Moldova", "REU":"Reunion", "ROU":"Romania", "RUS":"Russian Federation", "RWA":"Rwanda", "SHN":"Saint Helena", "KNA":"Saint Kitts and Nevis", "LCA":"Saint Lucia", "SPM":"Saint Pierre and Miquelon", "VCT":"Saint Vincent and the Grenadines", "WSM":"Samoa", "SMR":"San Marino", "STP":"Sao Tome and Principe", "SAU":"Saudi Arabia", "SEN":"Senegal", "YUG":"Serbia and Montenegro", "SYC":"Seychelles", "SLE":"Sierra Leone", "SGP":"Singapore", "SVK":"Slovakia", "SVN":"Slovenia", "SLB":"Solomon Islands", "SOM":"Somalia", "ZAF":"South Africa", "ESP":"Spain", "LKA":"Sri Lanka", "SDN":"Sudan", "SUR":"Suriname", "SJM":"Svalbard and Jan Mayen Islands", "SWZ":"Swaziland", "SWE":"Sweden", "CHE":"Switzerland", "SYR":"Syrian Arab Republic", "TWN":"Taiwan", "TJK":"Tajikistan", "THA":"Thailand", "MKD":"The former Yugoslav Republic of Macedonia", "TLS":"Timor-Leste", "TGO":"Togo", "TKL":"Tokelau", "TON":"Tonga", "TTO":"Trinidad and Tobago", "TUN":"Tunisia", "TUR":"Turkey", "TKM":"Turkmenistan", "TCA":"Turks and Caicos Islands", "TUV":"Tuvalu", "UGA":"Uganda", "UKR":"Ukraine", "ARE":"United Arab Emirates", "GBR":"United Kingdom of Great Britain and Northern Island", "TZA":"United Republic of Tanzania","VIR":"United States Virgin Islands", "URY":"Uruguay", "UZB":"Uzbekistan", "VUT":"Vanuatu", "VEN":"Venezuela", "VNM":"Viet Nam", "WLF":"Wallis and Futuna Islands", "ESH":"Western Sahara", "YEM":"Yemen", "ZMB":"Zambia", "ZWE":"Zimbabwe"};
	
	mShippingCountryArrayWithCodes.getKeys = function(){
		var Keys = new Array();
		for(var i in this){
			if(i in Object)
				continue;
			Keys[Keys.length] = i; //get the key
		}
		return Keys;
	}
	mShippingCountryArrayWithCodes.getValues = function(){
		var Values = new Array();
		for(var i in this){
			if(i in Object)
				continue;
					if ( typeof this[i] != "function" ) 
			Values[Values.length] = this[i]; //get the value of that key
		}
		return Values;
	}

	 var mShippingCountryKeys = new Array();
	 mShippingCountryKeys = mShippingCountryArrayWithCodes.getKeys();
	 var mShippingCountryValues = new Array();
	 mShippingCountryValues = mShippingCountryArrayWithCodes.getValues();

	 initializeProfileCountrydropdown(mShippingCountryValues, 'shippingcountries', pCountry, mShippingCountryArrayWithCodes, mShippingCountryKeys);

}

function initializeProfileCountrydropdown(pArray, pCountryElem, pSelected, mShippingCountryArrayWithCodes, mShippingCountryKeys )
{
	var countryElement = getElement(pCountryElem);
	var selCountryAgainstCode = null;
	if(pSelected!=null && pSelected!=''){
		if(contains(mShippingCountryArrayWithCodes.getKeys(), pSelected))
		{
			var index = inArray(mShippingCountryArrayWithCodes.getKeys(), pSelected);
			selCountryAgainstCode = mShippingCountryArrayWithCodes.getValues()[index];                                            
		}
	} else
	selCountryAgainstCode = pArray[0];  
	for(i = 0; i < pArray.length; i++){                                                
		if(selCountryAgainstCode == pArray[i]) {                                               
			countryElement.options[i] = new Option(pArray[i], mShippingCountryKeys[i]);
			countryElement.options[i].selected = "true";                                                                                     
		} else	{
			countryElement.options[countryElement.options.length] = new Option(pArray[i], mShippingCountryKeys[i]);
		}
	}
}
// End of code added for the Trac 1054

function changeState(pCountry,pElem)
{
	showStates(pCountry, pElem);
	// Code modified for the Trac1097.
	// The following code will execute if you select UK or Canada or Australia
	// from country drop down and it will hide the giftmessage check box. 
	if(pCountry.options[pCountry.selectedIndex].text != "United States") { 
		if (document.getElementById( "giftmessagecheckbox" )) {
			document.getElementById( "giftmessagecheckbox" ).style.display = "none";  
		}
	}
	// End of code modified for the Trac1097   

	if(pCountry.options[pCountry.selectedIndex].text == "United Kingdom"){
		document.getElementById( "states" ).style.display = "none";
		document.getElementById( "Province" ).style.display = "none";
		document.getElementById( "states" ).value = "FN";
		//Here we are deselecting the check box of pobox field.		
		if (document.getElementById( "pobox" )) {
			document.getElementById( "pobox" ).checked = false;
		} else if (document.getElementById( "poBox" )) {
			document.getElementById( "poBox" ).checked = false;
		}
		//Here we are hiding the pobox field.				
		document.getElementById( "PoBoxAddress" ).style.display = "none";
		updateState('refreshShipMethods');
		if (document.shippingForm) {
			document.shippingForm.postalCode.attributes["maxLength"].value = "8";
		} else if (document.newAddressForm) {
			document.newAddressForm.postalCode.attributes["maxLength"].value = "8";
		} else if (document.changeAddressForm) {
			document.changeAddressForm.postalCode.attributes["maxLength"].value = "8";
		} else if (document.billingform) {
			document.billingform.postalCode.attributes["maxLength"].value = "8";
		}
	} else if(pCountry.options[pCountry.selectedIndex].text == "United States")	{
		if (document.getElementById( "pobox" )) {
			document.getElementById( "pobox" ).checked = false;
		} else if (document.getElementById( "poBox" )) {
			document.getElementById( "poBox" ).checked = false;
		}
		document.getElementById( "PoBoxAddress" ).style.display = "";
		document.getElementById( "states" ).style.display = "";
		document.getElementById( "Province" ).style.display = "";
		if (document.getElementById( "giftmessagecheckbox" )) {
			document.getElementById( "giftmessagecheckbox" ).style.display = "";
		}
		document.getElementById( "states" ).value = "AK";
		updateState('refreshShipMethods');
		if (document.shippingForm) {
			document.shippingForm.postalCode.attributes["maxLength"].value = "5";
		} else if (document.newAddressForm) {
			document.newAddressForm.postalCode.attributes["maxLength"].value = "5";
		} else if (document.changeAddressForm) {
			document.changeAddressForm.postalCode.attributes["maxLength"].value = "5";
		} else if (document.billingform) {
			document.billingform.postalCode.attributes["maxLength"].value = "5";
		}
	} else if(pCountry.options[pCountry.selectedIndex].text == "Canada") {
		if (document.getElementById( "pobox" )) {
			document.getElementById( "pobox" ).checked = false;
		} else if (document.getElementById( "poBox" )) {
			document.getElementById( "poBox" ).checked = false;
		}
		document.getElementById( "PoBoxAddress" ).style.display = "";
		document.getElementById( "states" ).style.display = "";
		document.getElementById( "Province" ).style.display = "";
		document.getElementById( "states" ).value = "AB";
		updateState('refreshShipMethods');
		if (document.shippingForm) {
			document.shippingForm.postalCode.attributes["maxLength"].value = "7";
		} else if (document.newAddressForm) {
			document.newAddressForm.postalCode.attributes["maxLength"].value = "7";
		} else if (document.changeAddressForm) {
			document.changeAddressForm.postalCode.attributes["maxLength"].value = "7";
		} else if (document.billingform) {
			document.billingform.postalCode.attributes["maxLength"].value = "7";
		}
	} else if (pCountry.options[pCountry.selectedIndex].text == "Australia") {
		document.getElementById( "states" ).style.display = "";
		document.getElementById( "Province" ).style.display = "";
		document.getElementById( "states" ).value = "ACT";
		//Here we are deselecting the check box of pobox field.		
		if (document.getElementById( "pobox" )) {
			document.getElementById( "pobox" ).checked = false;
		} else if (document.getElementById( "poBox" )) {
			document.getElementById( "poBox" ).checked = false;
		}
		//Here we are hiding the pobox field.				
		document.getElementById( "PoBoxAddress").style.display = "none";
		updateState('refreshShipMethods');
		if (document.shippingForm) {
			document.shippingForm.postalCode.attributes["maxLength"].value = "4";
		} else if (document.newAddressForm) {
			document.newAddressForm.postalCode.attributes["maxLength"].value = "4";
		} else if (document.changeAddressForm) {
			document.changeAddressForm.postalCode.attributes["maxLength"].value = "4";
		} else if (document.billingform) {
			document.billingform.postalCode.attributes["maxLength"].value = "4";
		}
	}
 }


function updateState(actionval)
{
	if (document.shippingForm) {
		document.shippingForm.formAction.value=actionval;
		document.shippingForm.submit();
	}
}

function setUsingprofileAddress(){
	if (document.shippingForm) {
		if(document.shippingForm.isUsingPA != undefined) {
  			document.shippingForm.isUsingPA.value="false";
		}
	}
}

function movetobilling(action){
	if (document.shippingForm) {
		document.shippingForm.formAction.value=action;
		document.shippingForm.submit();
	}
}

function updateaddress(actionval){
	if (document.shippingForm) {
		if(document.shippingForm.isUsingPA != undefined) {
			document.shippingForm.isUsingPA.value="true";
			document.shippingForm.formAction.value=actionval;
			document.shippingForm.submit();
		}
	}
}

function giftoptioncheck(){
	if (document.shippingForm) {
		if (document.shippingForm.giftcheck.checked) {
			document.shippingForm.elements["/uo/commerce/order/purchase/ShippingFormHandler.moveToBillingSuccessURL"].value="../checkout_giftoptions.jsp";
		} else {
			document.shippingForm.elements["/uo/commerce/order/purchase/ShippingFormHandler.moveToBillingSuccessURL"].value="../billing.jsp";
		}
	}
}