/******************************
	Arco Address Chooser
	by Tom Selke 22/04/2005
******************************/

var acPREFIX = "";
hide("regiondiv") ;

/**
	This function should be called by the button under an
	address section. It will initiate the address chooser
	popup window.
	The BV url prefix (session id and engine number) should 
	be supplied.
	The prefix of the address fields should be supplied.
*/
function chooseAddress(bvUrlPrefix, fieldPrefix){
	
	// The page to open in the popup chooser.
	var acADDRESS_PAGE = bvUrlPrefix+'&event=pafchooseaddress';
	var acCHOOSER_WIDTH = 600;
	var acCHOOSER_HEIGHT = 500;
	acPREFIX = fieldPrefix;
	
	// Open a new window with the chooser in it.
	newWin = window.open(acADDRESS_PAGE,'addresschooser','scrollbars=yes,menubar=no,height='+acCHOOSER_HEIGHT+',width='+acCHOOSER_WIDTH+',resizable=no,toolbar=no,location=no,status=yes');
	newWin.focus();
	
}

function hide(id) {
   var e = document.getElementById(id);
   if( e )e.style.display = 'none';
   
}

function show(id) {
   var e = document.getElementById(id);
   if( e ) e.style.display = 'block';
}

/**
	Takes address lines as parameters.
	This function assumes that fields with the following ids exist in the page:
			<prefix> + address1
			<prefix> + address2
			<prefix> + address3
			<prefix> + city
			<prefix> + postcode
			<prefix> + country
			<prefix> + countryCode
			<prefix> + isvalid
	Country code should be a hidden field that is for Arco's use only.
	isvalid is also a hidden field used when validating required addresses.
	Spans with the following ids are also assumed to exist. These contain
	the values that the user sees on the page.
			<prefix> + address1span
			<prefix> + address2span
			<prefix> + address3span
			<prefix> + cityspan
			<prefix> + postcodespan
			<prefix> + countryspan
	
	Null strings are converted into empty strings, even though this is not
	really a problem in JavaScript.
*/
function updateAddressFields(mailstop, add1, add2, add3, city, postcode, country, countryCode, region, regionCode, residential){
	
	this.mailstop  = (mailstop) ? mailstop : "" ;
	this.add1        = (add1) ? add1 : "";
	this.add2        = (add2) ? add2 : "";
	this.add3        = (add3) ? add3 : "";
	this.city        = (city) ? city : "";
	//this.postcode    = (postcode) ? postcode : ""; 		// 6535 - SXB - 09/10/2006 - Change to line below
	this.postcode    = (postcode) ? postcode.toUpperCase() : ""; 
	this.country     = (country) ? country : "";
	this.countryCode = (countryCode) ? countryCode : "";
	this.region     = (region) ? region : "";
	this.regionCode = (regionCode) ? regionCode : "";
	hide("regiondiv");
	if( this.region != null && this.region != "" ) {
			show("regiondiv") ;
		}
	
	// First check: see if line 1 and country exist. These are the bear
	// minimum fields for a valid address. If they don't exist, assume no
	// address entered.
	if(this.add1 == "" || this.country == "" || this.countryCode == ""){
		if( document.getElementById(acPREFIX + "_mailstopspan") ) {
			document.getElementById(acPREFIX + "_mailstopspan").innerHTML = "" ;
			document.getElementById(acPREFIX + "_mailstop").value = "" ;
		}
		document.getElementById(acPREFIX + "_address1span").innerHTML = "Please choose an address";
		document.getElementById(acPREFIX + "_address1").value = "";
		document.getElementById(acPREFIX + "_address2span").innerHTML = "";
		document.getElementById(acPREFIX + "_address2").value = "";
		document.getElementById(acPREFIX + "_address3span").innerHTML = "";
		document.getElementById(acPREFIX + "_address3").value = "";
		document.getElementById(acPREFIX + "_cityspan").innerHTML = "";
		document.getElementById(acPREFIX + "_city").value = "";
		document.getElementById(acPREFIX + "_postcodespan").innerHTML = "";
		document.getElementById(acPREFIX + "_postcode").value = "";
		document.getElementById(acPREFIX + "_countryspan").innerHTML = "";
		document.getElementById(acPREFIX + "_country").value = "";
		document.getElementById(acPREFIX + "_countryCode").value = "";
		
		
			document.getElementById(acPREFIX + "_region").value = "";
			document.getElementById(acPREFIX + "_regionspan").innerHTML = "";
			document.getElementById(acPREFIX + "_regionCode").value = "";
		
		document.getElementById(acPREFIX + "_isvalid").value = "false";
		if( document.getElementById(acPREFIX + "_addresschoose") ) // added so that we dont have to link the address button with the actual address as in cart delivery
			document.getElementById(acPREFIX + "_addresschoose").value = "Enter Address";
	}
	else{
		// If we have the minimum information, then we can populate the fields
		// with the passed information.
		if( document.getElementById(acPREFIX + "_mailstopspan") ) {
			document.getElementById(acPREFIX + "_mailstopspan").innerHTML = (this.mailstop != null && this.mailstop != "" && this.mailstop.length > 0 ? "<div style=\"margin-bottom:4px;\">"+ this.mailstop + "</div>" : "") ;
			document.getElementById(acPREFIX + "_mailstop").value = this.mailstop;
		}
		document.getElementById(acPREFIX + "_address1span").innerHTML = this.add1;
		document.getElementById(acPREFIX + "_address1").value = this.add1;
		document.getElementById(acPREFIX + "_address2span").innerHTML = this.add2;
		document.getElementById(acPREFIX + "_address2").value = this.add2;
		document.getElementById(acPREFIX + "_address3span").innerHTML = this.add3;
		document.getElementById(acPREFIX + "_address3").value = this.add3;
		document.getElementById(acPREFIX + "_cityspan").innerHTML = this.city;
		document.getElementById(acPREFIX + "_city").value = this.city;
		document.getElementById(acPREFIX + "_postcodespan").innerHTML = this.postcode;
		document.getElementById(acPREFIX + "_postcode").value = this.postcode;
		document.getElementById(acPREFIX + "_countryspan").innerHTML = this.country;
		document.getElementById(acPREFIX + "_country").value = this.country;
		document.getElementById(acPREFIX + "_countryCode").value = this.countryCode;
		
			document.getElementById(acPREFIX + "_region").value = this.region;
			document.getElementById(acPREFIX + "_regionspan").innerHTML = this.region;
			document.getElementById(acPREFIX + "_regionCode").value = this.regionCode;
		
		document.getElementById(acPREFIX + "_isvalid").value = "true";
		if( document.getElementById(acPREFIX + "_addresschoose") )
			document.getElementById(acPREFIX + "_addresschoose").value = "Change Address";
	}
	
	if(document.getElementById(acPREFIX + "_isres")){
		document.getElementById(acPREFIX + "_isres").value = residential;
		if(document.getElementById(acPREFIX + "_isvalid").value == 'false')
			document.getElementById(acPREFIX + "_resspan").innerHTML = "-";
		else if(residential)
			document.getElementById(acPREFIX + "_resspan").innerHTML = "Yes";
		else
			document.getElementById(acPREFIX + "_resspan").innerHTML = '<input type="checkbox" name="'+acPREFIX+'_reschk" id="'+acPREFIX+'_reschk" />';
	}
	
	if(typeof addressUpdatedImplemented == 'boolean')
		addressUpdated(acPREFIX);
}

/**
	This function checks to see if the address with the
	supplied prefix is a valid address.
*/
function isAddressValid(prefix){
	var isValid = document.getElementById(prefix + "_isvalid");
	if(isValid && isValid.value == "true")
		return true;
	else
		return false;
}

function mailstop(prefix, val){
	
	document.write('<span id="'+prefix+'_mailstopspan">');
	var isValid = document.getElementById(prefix + "_isvalid");	
	if(!isValid || isValid.value == "false")
		 document.write('');
	else
		document.write(( val!= null && val != "" ? "<div style=\"margin-bottom:4px;\">" + val + "</div>" : ""));
	document.write('</span>');
	document.write('<input type="hidden" name="'+prefix+'_mailstop" id="'+prefix+'_mailstop" value="'+val+'">');
	
}

/**
	This function will write the HTML required displaying and storing the
	first line of an address within a page. The prefix (eg 'invoice' or 'delivery') should
	be specified.
	The parameter val is the the value that should be displayed.
*/
function address1(prefix, val){
	
	document.write('<span id="'+prefix+'_address1span">');
	var isValid = document.getElementById(prefix + "_isvalid");	
	if(!isValid || isValid.value == "false")
		 document.write('');
	else
		document.write(val);
	document.write('</span>');
	document.write('<input type="hidden" name="'+prefix+'_address1" id="'+prefix+'_address1" value="'+val+'">');
	
}

/**
	This function will write the HTML required displaying and storing the
	second line of an address within a page. The prefix (eg 'invoice' or 'delivery') should
	be specified.
	The parameter val is the the value that should be displayed.
*/
function address2(prefix, val){	
	document.write('<span id="'+prefix+'_address2span">');
	var isValid = document.getElementById(prefix + "_isvalid");
	if(isValid && isValid.value == "true")
		document.write(val);
	document.write('</span>');
	document.write('<input type="hidden" name="'+prefix+'_address2" id="'+prefix+'_address2" value="'+val+'">');	
}

/**
	This function will write the HTML required displaying and storing the
	third line of an address within a page. The prefix (eg 'invoice' or 'delivery') should
	be specified.
	The parameter val is the the value that should be displayed.
*/
function address3(prefix, val){	
	document.write('<span id="'+prefix+'_address3span">');
	var isValid = document.getElementById(prefix + "_isvalid");
	if(isValid && isValid.value == "true")
		document.write(val);
	document.write('</span>');
	document.write('<input type="hidden" name="'+prefix+'_address3" id="'+prefix+'_address3" value="'+val+'">');	
}

/**
	This function will write the HTML required displaying and storing the
	city line of an address within a page. The prefix (eg 'invoice' or 'delivery') should
	be specified.
	The parameter val is the the value that should be displayed.
*/
function city(prefix, val){	
	document.write('<span id="'+prefix+'_cityspan">');
	var isValid = document.getElementById(prefix + "_isvalid");
	if(isValid && isValid.value == "true")
		document.write(val);
	document.write('</span>');
	document.write('<input type="hidden" name="'+prefix+'_city" id="'+prefix+'_city" value="'+val+'">');	
}

/**
	This function will write the HTML required displaying and storing the
	postcode line of an address within a page. The prefix (eg 'invoice' or 'delivery') should
	be specified.
	The parameter val is the the value that should be displayed.
*/
function postcode(prefix, val){	
	document.write('<span id="'+prefix+'_postcodespan">');
	var isValid = document.getElementById(prefix + "_isvalid");
	if(isValid && isValid.value == "true")
		document.write(val);
	document.write('</span>');
	document.write('<input type="hidden" name="'+prefix+'_postcode" id="'+prefix+'_postcode" value="'+val+'">');	
}

/**
	This function will write the HTML required displaying and storing the
	country line of an address within a page. The prefix (eg 'invoice' or 'delivery') should
	be specified.
	The parameter val is the the value that should be displayed.
*/
function country(prefix, val, code){	
	document.write('<span id="'+prefix+'_countryspan">');
	var isValid = document.getElementById(prefix + "_isvalid");
	if(isValid && isValid.value == "true")
		document.write(val);
	document.write('</span>');
	document.write('<input type="hidden" name="'+prefix+'_country" id="'+prefix+'_country" value="'+val+'">');	
	document.write('<input type="hidden" name="'+prefix+'_countryCode" id="'+prefix+'_countryCode" value="'+code+'" />');
}

/**
	This function will write the HTML required displaying and storing the
	region line of an address within a page. The prefix (eg 'invoice' or 'delivery') should
	be specified.
	The parameter val is the the value that should be displayed.
*/
function region(prefix, val, code){	
		document.write('<span id="'+prefix+'_regionspan">');
		var isValid = document.getElementById(prefix + "_isvalid");
		if(isValid && isValid.value == "true")
			document.write(val);
		document.write('</span>');
		document.write('<input type="hidden" name="'+prefix+'_region" id="'+prefix+'_region" value="'+val+'">');	
		document.write('<input type="hidden" name="'+prefix+'_regionCode" id="'+prefix+'_regionCode" value="'+code+'" />');
}

function addressResidential(prefix, qasval, userval){
	document.write('<span id="'+prefix+'_resspan">');
	if(!qasval) {
		document.write('<input type="checkbox" name="'+prefix+'_reschk" id="'+prefix+'_reschk" '+(userval ? "checked" : "" )+'>');
	} else {
		document.write('Yes');
	}

	document.write('</span>');
	document.write('<input type="hidden" name="'+prefix+'_isres" id="'+prefix+'_isres" />');
	document.getElementById(prefix+'_isres').value = qasval;
}

function addressHidden(prefix, val){
	acPREFIX = prefix;
	document.write('<input type="hidden" name="'+prefix+'_isvalid" id="'+prefix+'_isvalid" />');
	document.getElementById(prefix+'_isvalid').value = val;
}

/**
	Simple object to hold address information.
*/
function addressObject(){
	this.address1 = "";
	this.address2 = "";
	this.address3 = "";
	this.city = "";
	this.postcode = "";
	this.region = "";
	this.regionCode = "";
	this.country = "";
	this.countryCode = "";
	this.unresolvable = false;
	this.moniker = "";
}

/**
	Simple object to hold information about a country.
*/
function countryOb(code, name){
	this.code = code;
	this.name = name;
}

/**
	Returns an array of country objects representing all ISO countries.
*/
function getCountryArray(){
	var countries = new Array(
			       new countryOb("GB", "United Kingdom"),
			       new countryOb("IE", "Ireland")
                   , new countryOb("AF", "Afghanistan")
                   , new countryOb("AL", "Albania")
                   , new countryOb("DZ", "Algeria")
                   , new countryOb("VI", "Amer.Virgin Is.")
                   , new countryOb("AD", "Andorra")
                   , new countryOb("AO", "Angola")
                   , new countryOb("AI", "Anguilla")
                   , new countryOb("AQ", "Antarctica")
                   , new countryOb("AG", "Antigua/Barbads")
                   , new countryOb("AR", "Argentina")
                   , new countryOb("AM", "Armenia")
                   , new countryOb("AW", "Aruba")
                   , new countryOb("AU", "Australia")
                   , new countryOb("AT", "Austria")
                   , new countryOb("AZ", "Azerbaijan")
                   , new countryOb("BS", "Bahamas")
                   , new countryOb("BD", "Bangladesh")
                   , new countryOb("BB", "Barbados")
                   , new countryOb("BE", "Belgium")
                   , new countryOb("BZ", "Belize")
                   , new countryOb("BJ", "Benin")
                   , new countryOb("BM", "Bermuda")
                   , new countryOb("BH", "Bharain")
                   , new countryOb("BT", "Bhutan")
                   , new countryOb("BO", "Bolivia")
                   , new countryOb("BA", "Bosnia-Herz.")
                   , new countryOb("BW", "Botswana")
                   , new countryOb("BV", "Bouvet Island")
                   , new countryOb("BR", "Brazil")
                   , new countryOb("IO", "Brit.Ind.Oc.Ter")
                   , new countryOb("VG", "Brit.Virgin Is.")
                   , new countryOb("BN", "Brunei Dar-es-S")
                   , new countryOb("BG", "Bulgaria")
                   , new countryOb("BF", "Burkina-Faso")
                   , new countryOb("BI", "Burundi")
                   , new countryOb("KH", "Cambodia")
                   , new countryOb("CM", "Cameroon")
                   , new countryOb("CA", "Canada")
                   , new countryOb("CV", "Cape Verde")
                   , new countryOb("KY", "Cayman Islands")
                   , new countryOb("CF", "Central Afr.Rep")
                   , new countryOb("TD", "Chad")
                   , new countryOb("CL", "Chile")
                   , new countryOb("CN", "China")
                   , new countryOb("CX", "Christmas Islnd")
                   , new countryOb("CC", "Coconut Islands")
                   , new countryOb("CO", "Columbia")
                   , new countryOb("KM", "Comoros")
                   , new countryOb("CG", "Congo")
                   , new countryOb("CK", "Cook Islands")
                   , new countryOb("CR", "Costa Rica")
                   , new countryOb("HR", "Croatia")
                   , new countryOb("CU", "Cuba")
                   , new countryOb("CY", "Cyprus")
                   , new countryOb("CZ", "Czech Republic")
                   , new countryOb("ZR", "Democ.Rep.Congo")
                   , new countryOb("DK", "Denmark")
                   , new countryOb("DJ", "Djibouti")
                   , new countryOb("DM", "Dominica")
                   , new countryOb("DO", "Dominican Rep.")
                   , new countryOb("AN", "Dutch Antilles")
                   , new countryOb("TP", "East Timor")
                   , new countryOb("EC", "Ecuador")
                   , new countryOb("EG", "Egypt")
                   , new countryOb("SV", "El Salvador")
                   , new countryOb("GQ", "EquatorialGuin")
                   , new countryOb("ER", "Eritrea")
                   , new countryOb("EE", "Estonia")
                   , new countryOb("ET", "Ethiopia")
                   , new countryOb("FO", "Faeroe")
                   , new countryOb("FK", "Falkland Islnds")
                   , new countryOb("FJ", "Fiji")
                   , new countryOb("FI", "Finland")
                   , new countryOb("FR", "France")
                   , new countryOb("PF", "Frenc.Polynesia")
                   , new countryOb("GF", "FrenchGuayana")
                   , new countryOb("GA", "Gabon")
                   , new countryOb("GM", "Gambia")
                   , new countryOb("GE", "Georgia")
                   , new countryOb("DE", "Germany")
                   , new countryOb("GH", "Ghana")
                   , new countryOb("GI", "Gibraltar")
                   , new countryOb("GR", "Greece")
                   , new countryOb("GL", "Greenland")
                   , new countryOb("GD", "Grenada")
                   , new countryOb("GP", "Guadeloupe")
                   , new countryOb("GU", "Guam")
                   , new countryOb("GT", "Guatemala")
                   , new countryOb("GN", "Guinea")
                   , new countryOb("GW", "Guinea-Bissau")
                   , new countryOb("GY", "Guyana")
                   , new countryOb("HT", "Haiti")
                   , new countryOb("HM", "Heard/McDon.Isl")
                   , new countryOb("HN", "Honduras")
                   , new countryOb("HK", "Hong Kong")
                   , new countryOb("HU", "Hungary")
                   , new countryOb("IS", "Iceland")
                   , new countryOb("IN", "India")
                   , new countryOb("ID", "Indonesia")
                   , new countryOb("IR", "Iran")
                   , new countryOb("IQ", "Iraq")
                   , new countryOb("IE", "Ireland")
                   , new countryOb("IL", "Israel")
                   , new countryOb("IT", "Italy")
                   , new countryOb("CI", "Ivory Coast")
                   , new countryOb("JM", "Jamaica")
                   , new countryOb("JP", "Japan")
                   , new countryOb("JO", "Jordan")
                   , new countryOb("KZ", "Kazakhstan")
                   , new countryOb("KE", "Kenya")
                   , new countryOb("KI", "Kiribati")
                   , new countryOb("KW", "Kuwait")
                   , new countryOb("KG", "Kyrgyzstan")
                   , new countryOb("LA", "Laos")
                   , new countryOb("LV", "Latvia")
                   , new countryOb("LB", "Lebanon")
                   , new countryOb("LS", "Lesotho")
                   , new countryOb("LR", "Liberia")
                   , new countryOb("LY", "Libya")
                   , new countryOb("LI", "Liechtenstein")
                   , new countryOb("LT", "Lithuania")
                   , new countryOb("LU", "Luxembourg")
                   , new countryOb("MO", "Macau")
                   , new countryOb("MK", "Macedonia")
                   , new countryOb("MG", "Madagascar")
                   , new countryOb("MW", "Malawi")
                   , new countryOb("MY", "Malaysia")
                   , new countryOb("MV", "Maldives")
                   , new countryOb("ML", "Mali")
                   , new countryOb("MT", "Malta")
                   , new countryOb("MH", "Marshall Islnds")
                   , new countryOb("MQ", "Martinique")
                   , new countryOb("MR", "Mauretania")
                   , new countryOb("MU", "Mauritius")
                   , new countryOb("YT", "Mayotte")
                   , new countryOb("MX", "Mexico")
                   , new countryOb("FM", "Micronesia")
                   , new countryOb("UM", "Minor Outl.Isl.")
                   , new countryOb("MD", "Moldavia")
                   , new countryOb("MC", "Monaco")
                   , new countryOb("MN", "Mongolia")
                   , new countryOb("MS", "Montserrat")
                   , new countryOb("MA", "Morocco")
                   , new countryOb("MZ", "Mozambique")
                   , new countryOb("MM", "Myanmar")
                   , new countryOb("MP", "N.Mariana Islnd")
                   , new countryOb("NA", "Namibia")
                   , new countryOb("NR", "Nauru")
                   , new countryOb("NP", "Nepal")
                   , new countryOb("NL", "Netherlands")
                   , new countryOb("NC", "New Caledonia")
                   , new countryOb("NZ", "New Zealand")
                   , new countryOb("NI", "Nicaragua")
                   , new countryOb("NE", "Niger")
                   , new countryOb("NG", "Nigeria")
                   , new countryOb("NU", "Niue Islands")
                   , new countryOb("NF", "Norfolk Island")
                   , new countryOb("KP", "North Korea")
                   , new countryOb("NO", "Norway")
                   , new countryOb("OM", "Oman")
                   , new countryOb("PK", "Pakistan")
                   , new countryOb("PW", "Palau")
                   , new countryOb("PA", "Panama")
                   , new countryOb("PG", "Pap. NewGuinea")
                   , new countryOb("PY", "Paraguay")
                   , new countryOb("PE", "Peru")
                   , new countryOb("PH", "Philippines")
                   , new countryOb("PN", "Pitcairn Islnds")
                   , new countryOb("PL", "Poland")
                   , new countryOb("PT", "Portugal")
                   , new countryOb("PR", "Puerto Rico")
                   , new countryOb("QA", "Qatar")
                   , new countryOb("RE", "Reunion")
                   , new countryOb("RO", "Rumania")
                   , new countryOb("RU", "Russian Fed.")
                   , new countryOb("RW", "Rwanda")
                   , new countryOb("ST", "S.Tome,Principe")
                   , new countryOb("AS", "Samoa,American")
                   , new countryOb("SM", "San Marino")
                   , new countryOb("SA", "Saudi Arabia")
                   , new countryOb("SN", "Senegal")
                   , new countryOb("SC", "Seychelles")
                   , new countryOb("SL", "Sierra Leone")
                   , new countryOb("SG", "Singapore")
                   , new countryOb("SK", "Slovakia")
                   , new countryOb("SI", "Slovenia")
                   , new countryOb("SB", "Solomon Islands")
                   , new countryOb("SO", "Somalia")
                   , new countryOb("ZA", "South Africa")
                   , new countryOb("KR", "South Korea")
                   , new countryOb("ES", "Spain")
                   , new countryOb("LK", "Sri Lanka")
                   , new countryOb("KN", "St Kitts&Nevis")
                   , new countryOb("SH", "St. Helena")
                   , new countryOb("LC", "St. Lucia")
                   , new countryOb("VC", "St. Vincent")
                   , new countryOb("PM", "St.Pier,Miquel.")
                   , new countryOb("STL", "stateless")
                   , new countryOb("GS", "Sth Sandwich Is")
                   , new countryOb("SD", "Sudan")
                   , new countryOb("SR", "Suriname")
                   , new countryOb("SJ", "Svalbard")
                   , new countryOb("SZ", "Swaziland")
                   , new countryOb("SE", "Sweden")
                   , new countryOb("CH", "Switzerland")
                   , new countryOb("SY", "Syria")
                   , new countryOb("TW", "Taiwan")
                   , new countryOb("TJ", "Tajikstan")
                   , new countryOb("TZ", "Tanzania")
                   , new countryOb("TH", "Thailand")
                   , new countryOb("TG", "Togo")
                   , new countryOb("TK", "Tokelau Islands")
                   , new countryOb("TO", "Tonga")
                   , new countryOb("TT", "Trinidad,Tobago")
                   , new countryOb("TN", "Tunisia")
                   , new countryOb("TR", "Turkey")
                   , new countryOb("TM", "Turkmenistan")
                   , new countryOb("TC", "Turksh Caicosin")
                   , new countryOb("TV", "Tuvalu")
                   , new countryOb("UG", "Uganda")
                   , new countryOb("UA", "Ukraine")
                   , new countryOb("GB", "United Kingdom")
                   , new countryOb("UY", "Uruguay")
                   , new countryOb("US", "USA")
                   , new countryOb("AE", "Utd.Arab.Emir.")
                   , new countryOb("UZ", "Uzbekistan")
                   , new countryOb("VU", "Vanuatu")
                   , new countryOb("VA", "Vatican City")
                   , new countryOb("VE", "Venezuela")
                   , new countryOb("VN", "Vietnam")
                   , new countryOb("WF", "Wallis,Futuna")
                   , new countryOb("WS", "Western Samoa")
                   , new countryOb("BY", "White Russia")
                   , new countryOb("YE", "Yemen")
                   , new countryOb("YU", "Yugoslavia")
                   , new countryOb("ZM", "Zambia")
                   , new countryOb("ZW", "Zimbabwe" ));
	return countries;
}

/**
	Function to return the full, user-friendly name of the
	country specified by its ISO code.
*/
function getCountryName(isoCode){
	var countries = getCountryArray();
    for(z = 0; z < countries.length; z++){
    	if(isoCode == countries[z].code){
    		return countries[z].name;
    	}
    }
    return "" ;
}

function getRegionArray(){
	var regions = new Array(
	new countryOb("AO", "Angola")
, new countryOb("CK", "Cork")
, new countryOb("CL", "Clare")
, new countryOb("CV", "Cavan")
, new countryOb("CW", "Carlow")
, new countryOb("DB", "Dublin")
, new countryOb("DG", "Donegal")
, new countryOb("GC", "Galway City")
, new countryOb("GW", "Galway")
, new countryOb("KD", "Kildare")
, new countryOb("KK", "Kilkenny")
, new countryOb("KY", "Kerry")
, new countryOb("LC", "Limerick City")
, new countryOb("LF", "Longford")
, new countryOb("LI", "Limerick")
, new countryOb("LM", "Leitrim")
, new countryOb("LS", "Laois")
, new countryOb("LT", "Louth")
, new countryOb("MH", "Monaghan")
, new countryOb("MT", "Meath")
, new countryOb("MY", "Mayo")
, new countryOb("OF", "Offaly")
, new countryOb("RC", "Rosscommon")
, new countryOb("SG", "Sligo")
, new countryOb("TN", "Tipperary N")
, new countryOb("TP", "Tipperary")
, new countryOb("TS", "Tipperary S")
, new countryOb("WC", "Waterford Cty")
, new countryOb("WF", "Waterford")
, new countryOb("WK", "Wicklow")
, new countryOb("WM", "Westmeath")
, new countryOb("WX", "Wexford"));
	return regions;
}


function getRegionName(isoCode){
	var regions = getRegionArray();
    for(z = 0; z < regions.length; z++){
    	if(isoCode == regions[z].code){
    		return regions[z].name;
    	}
    }
    return "" ;
}

/**
	This function was moved into this file to allow it to be
	with the other address functionality.
	The function draws a country dropdown menu where it is called
	within the document. The dropdown has the name and id supplied
	in fieldName and defaults the selected option to the country code 
	passed in toBeSelected. The onchange parameter, if supplied, will
	specify the action the select performs when the value is changed.
*/
function drawCountryBox(toBeSelected, fieldName, onchange){
			
	    var countries = getCountryArray();
	    document.write("<select name=\"" + fieldName +"\" id=\"" + fieldName + "\" onchange=\""+onchange+"\">");
	    for(z = 0; z < countries.length; z++){
	    	document.write("<option value='" + countries[z].code + "'");
	    	if(toBeSelected == countries[z].code){
	    		document.write(" selected");
	    	}
	    	document.write(">" + countries[z].name + "</option>");
	    }
	    document.write("</select>");

}

/**
	Function to validate the a postcode entered into the address
	chooser screens.
	Take a postcode and a country code as arguments and only 
	carries out real validation for UK country code.
	Returns true if the postcode is valid for the specified counrty.
*/
function isValidPostcode(postcode, countrycode){
	if(countrycode == "GB"){		
		if( postcode.match("^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$") && postcode.length < 10 ){
			return true;
		}
		else{
			return false;
		}
	}
	else{
		if(postcode != ""){
			return true;
		}
		else{
			return false;
		}			
	}
}

/**
	Validates line 1 of an address and returns true if it
	starts with an alphanumeric character and then only consists
	of alphnumeric characters or @&!£:'.,()/ characters.
	Spaces are also allowed as long as they are not the first
	character.
*/
function isValidAddress1(address1){
	if(address1.match("^[a-zA-Z0-9][a-zA-Z0-9- @&!£:'.,()\/]+$"))
		return true
	
	return false;
}

/**
	Validate the city field of an address
*/
function isValidCity(address1){
	return address1.match("^[a-zA-Z0-9- &,+.']+$");
}

/**
	Validates the passed variable to make sure it fits
	the format of a foreign postcode.
*/
function isValidForeignPostcode(postcode){
	if(postcode != "")
		return true;
		
	return false;
}

/**
	This function greys out the address field with the specified
	prefix. Greying out of an address field only means it cannot
	be changed - the address information should still be passed by
	the browser in HTTP requests.
*/
function greyOutAddress(addPrefix){
	var addButton = document.getElementById(addPrefix + "_addresschoose");
	if(addButton){
		addButton.disabled = true;
		addButton.className = "disabledAddButton";
	}
}

/**
	This function enables the address field with the specified
	prefix.
*/
function enableAddress(addPrefix){
	var addButton = document.getElementById(addPrefix + "_addresschoose");
	if(addButton){
		addButton.disabled = false;
		addButton.className = "proceedbutton";
	}
}
