
function CountryObject(pObject){
	this.mCountryCode = null;
	this.mCountryName = null;
}

function StateObject(pObject){
	this.mStateCode = null;
	this.mStateName = null;
}

// to populate list of countries in country dropdown
mCountryArray = new Array("United States", "Canada"); 


mCountryArrayWithCodes = {"0000" : "United States"};

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"};



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;
}

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 mStateKeys = new Object();
mStateKeys[mCountryArray[0]] = mStateKeysUSA;   //for United States
mStateKeys[mCountryArray[1]] = mStateKeysCanada;   //for Canada

mStatesAndCountries = new CountryObject();

mStatesAndCountries[mCountryArray[0]] = mStateValuesUSA;

mStatesAndCountries[mCountryArray[1]] = mStateValuesCanada;


function initialize(pCountry, pState){
	//alert("inside intialize");	
	initializeCountry(mCountryValues, 'countries', pCountry);
	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){
	//alert("inside initializeCountry");
	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){
	//alert("inside initializeStates");
	var stateElement = getElement(pStateElem);
	var defaultCountry = null;
	var key=null;	
	var selectedValAgainstKey = null;

	if(pSelected != null && pSelected != ''){

		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];
		}

		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;
}