// JAVASCRIPT FUNCTIONS FOR FORMATTING WEB REGISTRATIONS

// list of country names to translate. must be in caps.

  countrychange = new Array();

  countrychange["DEUTSCHLAND"]="GERMANY"
  countrychange["U.S.A."]="USA"
  countrychange["U.S.A"]="USA"
  countrychange["U S A"]="USA"
  countrychange["U. S. A."]="USA"
  countrychange["SVERIGE"]="SWEDEN"
  countrychange["ESPANA"]="SPAIN"
  countrychange["ENGLAND"]=""
  countrychange["WALES"]=""
  countrychange["SCOTLAND"]=""
  countrychange["ITALIA"]="ITALY"
  countrychange["UNITED STATES"]="USA"
  countrychange["UNITED STATES OF AMERICA"]="USA"
  countrychange["NORGE"]="NORWAY"
  countrychange["DANMARK"]="DENMARK"
  countrychange["EIRE"]="IRELAND"
  countrychange["TURKIYE"]="TURKEY"
  countrychange["BELGIE"]="BELGIUM"
  countrychange["BELGE"]="BELGIUM"
  countrychange["LA SUISSE"]="SWITZERLAND"
  countrychange["SUISSE"]="SWITZERLAND"
  countrychange["SCHWEIZ"]="SWITZERLAND"
  countrychange["NEDERLAND"]="NETHERLANDS"
  countrychange["OSTERREICH"]="AUSTRIA"
  countrychange["MAGYAR"]="HUNGARY"
  countrychange["BRASIL"]="BRAZIL"
  countrychange["HOLLAND"]="NETHERLANDS"
  
// list of london boroughs. must be in caps.

londonboroughs = ["CAMDEN","WESTMINSTER","FULHAM","TOTTENHAM","HAMMERSMITH","CHELSEA","BOW","HACKNEY","CHINGFORD","HIGHGATE","BLACKHEATH","SHEEN","EAST SHEEN","WIMBLEDON","WANDSWORTH","CLAPHAM","PUTNEY","KNIGHTSBRIDGE","WEST KENSINGTON","PARK ROYAL","CHISWICK","COVENT GARDEN"]




function titlecase(s) {

	s=trim(ltrim(s));

	// If no alpha chars, leave as it is
	if ( !ContainsUpperChars(s) && !ContainsLowerChars(s)) {
		return s;
	}

	// If already mixed case, leave it as it is
	if ( ContainsUpperChars(s) && ContainsLowerChars(s)) {
		return s;
	}
	
	
	// Split word by word and deal with each, joining back together at the end
	
	var cResult="";
	var aWord=s.split(" ");
	for (var nWord=0; nWord < aWord.length; nWord++) {
		var cThisWord=Lower(aWord[nWord])
		var cNewWord="";
		var cNextAction="";
		for (var nLetter=0; nLetter < cThisWord.length; nLetter++) {
		
		
			// Mc
			if (nLetter==0 && cThisWord.substr(0,2)=="mc") {
				cNewWord="Mc";
				nLetter++;
				cNextAction="U";
			} else
			
			// Mac
			if (nLetter==0 && cThisWord.substr(0,3)=="mac" && cThisWord.length > 5 && 
					!cThisWord.match(/macabr|macaron|macerat|machete|machiav|machin|machis|mackerel|mackint|macram|macro/)) {
				cNewWord="Mac";
				nLetter++;
				nLetter++;
				cNextAction="U";
			} else
			
			// O'
			if (nLetter==0 && cThisWord.substr(0,2)=="o'") {
				cNewWord="O'";
				nLetter++;
				cNextAction="U";
			} else

			// D'
			if (nLetter==0 && cThisWord.substr(0,2)=="d'") {
				cNewWord="d'";
				nLetter++;
				cNextAction="U";
			} else 

			// L'
			if (nLetter==0 && cThisWord.substr(0,2)=="d'") {
				cNewWord="l'";
				nLetter++;
				cNextAction="U";
			} else 


			// Small word exception


			if (nLetter==0 && cThisWord.match(/^de$|^la$|^le$|^du$|^des$|^van$|^von$|^der$|^den$|^vder$/) ) {
				cNewWord=Lower(cThisWord);
				nLetter=cThisWord.length;

			} else {




			// Otherwise
				cThisLetter=cThisWord.charAt(nLetter)
				if (cNextAction=="U" || nLetter==0) {
					cNewWord=cNewWord+Upper(cThisLetter)
				} else {
					cNewWord=cNewWord+Lower(cThisLetter)
				}
				if (cThisLetter.match(/[\.\-\,]/)) {
					cNextAction="U";
				} else {
					cNextAction="";
				}
			}
		}

		cResult=cResult+cNewWord+" "
	}
	cResult=trim(cResult);
	
	return cResult;
}



// format a first name: If it is a short string with only consonants and 
// case is constant, then space out the letters. Otherwise titlecase as usual

function firstnamecase(s) {

	s=trim(ltrim(s));
	
	if (s.length<4 && ! (ContainsUpperChars(s) && ContainsLowerChars(s)) && ContainsOnlyConsonants(s) ) {
		var cNewString=""
		for (k=0; k<s.length; k++) {
			cNewString=cNewString+s.charAt(k)+" ";
		}
		s=cNewString;
	}
	
	return titlecase(s);
	
}

// format a job title:
// ordinary title case, then expand standard abbreviations


function jobcase(s) {

	s=titlecase(s);

	s=s.replace(/\bMD\b/i,'Managing Director');
	s=s.replace(/\bFD\b/i,'Financial Director');
	s=s.replace(/\bSr\b/i,'Senior');
	s=s.replace(/\bJr\b/i,'Junior');
	s=s.replace(/\bAcct\b/i,'Account');
	s=s.replace(/\bMgr\b/i,'Manager');
	s=s.replace(/\bExec\b/i,'Executive');
	s=s.replace(/\bVP\b/i,'Vice President');
	s=s.replace(/\bCEO\b/i,'Chief Executive Officer');
	s=s.replace(/\bPA\b/i,'Personal Assistant');
	s=s.replace(/\bHR\b/i,'Human Resources');
	s=s.replace(/\bIT\b/i,'I T');
	s=s.replace(/\bAsst\b/i,'Assistant');
	return s;
}	


// format a company name: 
// - if looks like a web name, and constant case, force lower
// - Otherwise, if all entered upper, keep upper
// - If any word is all cons, force upper, otherwise normal titlecase
// - Replace common exceptions: plc GmbH Ltd UK USA
function companycase(s) {

	if (LooksLikeWebAddress(s) && ! (ContainsUpperChars(s) && ContainsLowerChars(s)) ) {
		return Lower(s);
	}

	// If already mixed case, leave it as it is
	if ( ContainsUpperChars(s) && ContainsLowerChars(s)) {
		return s;
	}
		
	s=titlecase(s);
	
	var cResult="";
	var aWord=s.split(" ");
	for (var nWord=0; nWord < aWord.length; nWord++) {
		var cThisWord=aWord[nWord]

		if (ContainsOnlyConsonants(cThisWord)) {
			cResult=cResult+Upper(cThisWord)+" ";
		} else {
			cResult=cResult+cThisWord+" ";
		}
	}
	s=trim(cResult);

	s=s.replace(/\bUK\b/i,'UK');
	s=s.replace(/\bUSA\b/i,'USA');
	s=s.replace(/\bplc\b/i,'plc');
	s=s.replace(/\bgmbh\b/i,'GmbH');
	s=s.replace(/\bSA\b/i,'SA');
	s=s.replace(/\bBLDG\b/i,'Bldg');
	s=s.replace(/\bLTD\b/i,'Ltd');
	s=s.replace(/\bBLDGs\b/i,'Bldgs');
	
	return s;
}

// address: first word lowercase: ul ulica rue
function addresscase(s) {

	// If already mixed case, leave it as it is
	if ( ContainsUpperChars(s) && ContainsLowerChars(s)) {
		return s;
	}

	s=titlecase(s);

	s=s.replace(/^rue\b/i,'rue');
	s=s.replace(/^ul\b/i,'ul');
	s=s.replace(/^ulica\b/i,'ulica');

	return s;
}	



// format a UK phone number with spaces and brackets
function phoneformat(s) {
	var nDigCount=0, nStrCount=0, cNewStr="", cFormatType="", temp1, temp2
	s=trim(ltrim(s))
	while (nStrCount < s.length ) {
		temp1=s.substr(nStrCount,1)
		if (isAlpha(temp1) || temp1 == "+") {
			cNewStr=cNewStr+s.substr(nStrCount)
			nStrCount=s.length-1
		} else {
			if (isDigit(temp1)) {
				cNewStr=cNewStr+temp1
				nDigCount=nDigCount+1
			}
		}
		++nStrCount
	}
	s=cNewStr
	if (nDigCount==10 && s.substr(0,2)!="01" && s.substr(0,2)!="02") {
		cFormatType="46"
	} else if (nDigCount==11 && s.substr(0,2)=="08") {
		cFormatType="434"
	} else if (nDigCount==11 && s.substr(0,2)=="07") {
		cFormatType="56"
	} else if (nDigCount==11 && s.substr(0,2)=="02") {
		cFormatType="344"
	} else if (nDigCount==11 && (s.substr(0,3)=="011" || (s.substr(0,2)=="01" && s.substr(3,1)=="1"))) {
		cFormatType="434"
	} else if (nDigCount==11 && s.substr(0,2)=="01") {
		cFormatType="56"
	}
	switch (cFormatType) {
		case "46":
			s=cNewStr.substr(0,4)+" "+cNewStr.substr(4,6)
			if (cNewStr.length > nDigCount) {
				s=s+" "+cNewStr.substr(nDigCount)
			}
			break;
		case "434":
			s=cNewStr.substr(0,4)+" "+cNewStr.substr(4,3)+" "+cNewStr.substr(7,4)
			if (cNewStr.length > nDigCount) {
				s=s+" "+cNewStr.substr(nDigCount)
			}
			break;
		case "344":			
			s=cNewStr.substr(0,3)+" "+cNewStr.substr(3,4)+" "+cNewStr.substr(7,4)
			if (cNewStr.length > nDigCount) {
				s=s+" "+cNewStr.substr(nDigCount)
			}
			break;
		case "56":			
			s=cNewStr.substr(0,5)+" "+cNewStr.substr(5,6)
			if (cNewStr.length > nDigCount) {
				s=s+" "+cNewStr.substr(nDigCount)
			}
			break;
	}
	return s
}

// format UK postcode with spaces. Correct O to 0.

function pcodeformat(s) {
	s=s.toUpperCase()
	var temp1=chrtran(chrtran(s,".","")," ","")
	var temp2=""
	if (temp1.substr((temp1.length)-3,1)=="O") {
		temp1=temp1.substr(0,(temp1.length)-3)+"0"+temp1.substr((temp1.length)-2,2)
	}
	for (var i=0; i<temp1.length; i++) {
		if (isAlpha(temp1.substr(i,1))) {
			temp2=temp2+"X"
		} else if (isDigit(temp1.substr(i,1))) {
			temp2=temp2+"9"
		} else {
			temp2=temp2+"?"
		}
	}
	switch (temp2) {
		case "XX99XX":
		case "X9X9XX":
		case "X999XX":
			s=temp1.substr(0,3)+" "+temp1.substr(3)
			break;
		case "XX999XX":
		case "XX9X9XX":
			s=temp1.substr(0,4)+" "+temp1.substr(4)
			break;
		case "X99XX":
			s=temp1.substr(0,2)+" "+temp1.substr(2)
			break;
		default:
			break;
	}
	return s
}

// format email address with capitalisation; remove spaces, etc.

function emailformat(s) {
	var cLeft="", cRight=""
	if (s.indexOf("@")>=0) {
		if (s.indexOf("@")>0) {
			cLeft=s.substr(0,s.indexOf("@"))
		}
		if (s.indexOf("@")<s.length-1) {
			cRight=s.substr(s.indexOf("@")+1)
		}
		if (cLeft == cLeft.toUpperCase()) {
			cLeft=cLeft.toLowerCase()
		}
		cRight=cRight.toLowerCase()
		s=cLeft+"@"+cRight
	}
	
	if (s.length > 0) {
		while (s.charAt(s.length-1) == "." || s.charAt(s.length-1) == ",") {
			s=s.substr(0,s.length-1)
		}
	}

	s=ltrim(s)
	
	if (!validateemail(s) && validateemail(chrtran(s," ",""))) {
		s=chrtran(s," ","")
	}

	if (!validateemail(s) && validateemail(chrtran(s,",","."))) {
		s=chrtran(s,",",".")
	}

	if (!validateemail(s) && validateemail(chrtran(chrtran(s," ",""),",","."))) {
		s=chrtran(chrtran(s," ",""),",",".")
	}
	return ltrim(s)
}

// standard function to translate a character string

function chrtran(s,a,b) {
	var RetVal=""
	for (var i=0; i<s.length; i++) {
		if (s.substr(i,1) == a) {
			RetVal=RetVal+b
		} else {
			RetVal=RetVal+s.substr(i,1)
		}
	}
	return RetVal
}

// general function to format a field based on formattype property

function formatfield(fld) {
	var RetVal=fld.value
	
	switch (fld.formattype) {
		case "lower":
			RetVal=ltrim(RetVal);
			RetVal=RetVal.toLowerCase();
			break;
		case "upper":
			RetVal=ltrim(RetVal);
			RetVal=RetVal.toUpperCase();
			break;
		case "title":
			RetVal=titlecase(RetVal);
			break;
		case "name":
			RetVal=titlecase(RetVal);
			break;
		case "firstname":
			RetVal=firstnamecase(RetVal);
			break;
		case "job":
			RetVal=jobcase(RetVal);
			break;
		case "company":
			RetVal=companycase(RetVal);
			break;
		case "address":
			RetVal=addresscase(RetVal);
			break;
		case "phone":
			RetVal=phoneformat(RetVal);
			break;
		case "number":
			RetVal=numberformat(RetVal);
			break;
		case "money":
			RetVal=currencyformat(RetVal);
			break;
		case "years":
			RetVal=yearsformat(RetVal);
			break;
		case "email":
			RetVal=emailformat(RetVal);
			break;
		case "pcode":
			RetVal=pcodeformat(RetVal);
			break;
	}
	return RetVal;
}

// convert a recognised alternative country name to the proper one

function countryconvert(s) {
	if (typeof(countrychange[s.toUpperCase()]) != "undefined") {
		s=countrychange[s.toUpperCase()]
	}
	return s.toUpperCase()
}

// check that a country is correct/alternative. Return the correct version

function validcountry(s) {
	if (typeof(countrychange[s.toUpperCase()]) != "undefined") {
		return true
	}
	for (var i=0; i<document.qform.er_country.length; i++) {
		if (s.toUpperCase()==document.qform.er_country[i].value.toUpperCase()) {
			return true
		}
	}
	return false	
}	


// reformat address based on set rules - see the text line in each rule

function addformat(frm) {
	var add1=trim(ltrim(frm.er_add1.value))
	var add2=trim(ltrim(frm.er_add2.value))
	var town=trim(ltrim(frm.er_town.value))
	var county=trim(ltrim(frm.er_county.value))
	var pcode=trim(ltrim(frm.er_pcode.value))
	var country=trim(ltrim(frm.er_country[frm.er_country.selectedIndex].value))
 	var o=""
	
 	if (add1.toUpperCase()==add2.toUpperCase() && add1.length) {
		o=o+"removing address line 2 (matches add1)\n"
		add2=""
	}

	if (countryconvert(town.toUpperCase())==country.toUpperCase() && town.length) {
		o=o+"removing town - matches country\n"
		town=""
	}
	
	if (add2.toUpperCase()==town.toUpperCase() && add2.length) {
		o=o+"removing address line 2 (matches town)\n"
		add2=""
	}
	
	if (countryconvert(county.toUpperCase())==country.toUpperCase() && county.length) {
		o=o+"removing county - matches country\n"
		county=""
	}

	if (county.toUpperCase()==town.toUpperCase() && county.length) {
		o=o+"removing county - matches town\n"
		county=""
	}
	
	if (countryconvert(pcode.toUpperCase())==country.toUpperCase() && pcode.length) {
		o=o+"removing pcode - matches country\n"
		pcode=""
	}

	if (pcode.toUpperCase()==county.toUpperCase() && pcode.length) {
		o=o+"removing county - matches pcode\n"
		county=""
	}

	
	// if the county (or town if county is blank) is a valid country name,
	// and the country has not been set,
	// or recognised abbreviation or foreign spelling, then blank it out and
	// set the country appropriately

	if (county.length && !country.length && validcountry(county)) {
		country=countryconvert(county)
		county=""
		o=o+"County moved to country\n"
	}
	
	if (pcode.length && !country.length && validcountry(pcode)) {
		country=countryconvert(pcode)
		pcode=""
		o=o+"Postcode moved to country\n"
	}
	
	if (town.length && !country.length && validcountry(town)) {
		country=countryconvert(town)
		town=""
		o=o+"Town moved to country\n"
	}

	
	// If the county has a valid-looking UK postcode in it, and the postcode
	// field is blank, move the postcode
	
	if (county.length && !pcode.length && !country.length && validpcode(county)) {
		pcode=pcodeformat(county)
		county=""
		o=o+"County moved to postcode\n"
	}
	
	if (town.length && !pcode.length && !country.length && validpcode(town)) {
		pcode=pcodeformat(town)
		town=""
		o=o+"Town moved to postcode\n"
	}
	
	if (add2.length && !town.length) {
		o=o+"Moving add2 to town\n"
		town=add2
		add2=""	
	}

	if (county.toUpperCase()=="LONDON") {
	
		for (var i=0; i<londonboroughs.length; i++) {
			if (town.toUpperCase()==londonboroughs[i].toUpperCase()) {
				if (add2=="") {
					add2=titlecase(town)
				}
				town="LONDON"
				county=""
				i=londonboroughs.length
				o=o+"Moved London to town\n"
			}
		}				
	
	}
	
	if (frm.er_add1.value != add1)
		frm.er_add1.value=add1
	if (frm.er_add2.value != add2)
		frm.er_add2.value=add2
	if (frm.er_town.value != town)
		frm.er_town.value=town
	if (frm.er_county.value != county)
		frm.er_county.value=county
	if (frm.er_pcode.value != pcode)
		frm.er_pcode.value=pcode
	
	for (var j=0; j<frm.er_country.length; j++) {
		if (frm.er_country.options[j].value==country) {
			frm.er_country.selectedIndex=j
			j=frm.er_country.length
		}
	}		

	
//        if (o.length) 
//                alert("Changes made:\n\n"+o)

}

function LTrim(str) {
 for (var i=0; ((str.charAt(i)<=" ")&&(str.charAt(i)!="")); i++);
 return str.substring(i,str.length);
}
function RTrim(str) {
 for (var i=str.length-1; ((str.charAt(i)<=" ")&&(str.charAt(i)!="")); i--);
 return str.substring(0,i+1);
}



dial= new Array();
dial["FRANCE"]="33"
dial["POLAND"]="48"
dial["GERMANY"]="49"
dial["USA"]="1"

dial["AFGANISTAN"]="93"
dial["ALBANIA"]="355"
dial["ALGERIA"]="213"
dial["ANDORRA"]="376"
dial["ANGOLA"]="244"
dial["ANGUILLA"]="1 809"
dial["ANTARTIC AUSTRALIAN TERRITORY"]="672"
dial["ANTIGUA & BARBUDA"]="1 268"
dial["ARGENTINA"]="54"
dial["ARMENIA"]="374"
dial["ARUBA"]="297"
dial["ASCENSION ISLAND"]="247"
dial["AUSTRALIA"]="61"
dial["AUSTRIA"]="43"
dial["AZERBAIJAN"]="994"
dial["BAHAMAS"]="1 242"
dial["BAHRAIN"]="973"
dial["BANGLADESH"]="880"
dial["BARBADOS"]="1 246"
dial["BELARUS"]="375"
dial["BELAU"]="680"
dial["BELGIUM"]="32"
dial["BELIZE"]="501"
dial["BENIN"]="229"
dial["BERMUDA"]="1 441"
dial["BHUTAN"]="975"
dial["BOLIVIA"]="591"
dial["BOSNIA-HERZEGOVINA"]="387"
dial["BOTSWANA"]="267"
dial["BRAZIL"]="55"
dial["BRUNEI"]="673"
dial["BULGARIA"]="359"
dial["BURKINA FASO"]="226"
dial["BURUNDI"]="257"
dial["CAMBODIA"]="855"
dial["CAMEROON"]="237"
dial["CANADA"]="1"
dial["CANARY ISLANDS"]="34"
dial["CAPE VERDE"]="238"
dial["CAYMAN ISLANDS"]="1 345"
dial["CENTRAL AFRICAN REPUBLIC"]="236"
dial["CHAD"]="235"
dial["CHILE"]="56"
dial["CHINA"]="86"
dial["CHRISTMAS ISLAND"]="6 724"
dial["COCOS ISLAND"]="6 722"
dial["COLOMBIA"]="57"
dial["COMOROS"]="269"
dial["CONGO"]="242"
dial["CONGO DEMOCRATIC REPUBLIC"]="243"
dial["COOK ISLANDS"]="682"
dial["COSTA RICA"]="506"
dial["COTE D'IVOIRE"]="225"
dial["CROATIA"]="385"
dial["CUBA"]="53"
dial["CURACAO"]="599"
dial["CYPRUS"]="357"
dial["CZECH REPUBLIC"]="420"
dial["DENMARK"]="45"
dial["DJIBOUTI"]="253"
dial["DOMINICA"]="1 809"
dial["DOMINICAN REPUBLIC"]="1 809"
dial["EAST TIMOR"]="62"
dial["ECUADOR"]="593"
dial["EGYPT"]="20"
dial["EL SALVADOR"]="503"
dial["EQUATORIAL GUINEA"]="240"
dial["ERITREA"]="291"
dial["ESTONIA"]="372"
dial["ETHIOPIA"]="251"
dial["FALKLAND ISLANDS"]="500"
dial["FAROE ISLANDS"]="298"
dial["FIJI"]="679"
dial["FINLAND"]="358"
dial["FRANCE"]="33"
dial["FRENCH GUIANA"]="594"
dial["FRENCH POLYNESIA"]="689"
dial["GABON"]="241"
dial["GAMBIA"]="220"
dial["GEORGIA"]="995"
dial["GERMANY"]="49"
dial["GHANA"]="233"
dial["GIBRALTAR"]="350"
dial["GREECE"]="30"
dial["GREENLAND"]="299"
dial["GRENADA"]="1 809"
dial["GUADELOUPE"]="590"
dial["GUAM"]="671"
dial["GUATEMALA"]="502"
dial["GUINEA"]="224"
dial["GUINEA-BISSAU"]="245"
dial["GUYANA"]="592"
dial["HAITI"]="509"
dial["HONDURAS"]="504"
dial["HONG KONG"]="852"
dial["HUNGARY"]="36"
dial["ICELAND"]="354"
dial["INDIA"]="91"
dial["INDONESIA"]="62"
dial["IRAN"]="98"
dial["IRAQ"]="964"
dial["IRELAND"]="353"
dial["ISRAEL"]="972"
dial["ITALY"]="39"
dial["IVORY COAST"]="225"
dial["JAMAICA"]="1 876"
dial["JAPAN"]="81"
dial["JORDAN"]="962"
dial["KAZAKHASTAN"]="7"
dial["KENYA"]="254"
dial["KIRIBATI"]="686"
dial["KOREA NORTH (PDR)"]="850"
dial["KOREA SOUTH (REP OF)"]="82"
dial["KOSOVO"]=""
dial["KUWAIT"]="965"
dial["KYRGYZSTAN"]="996"
dial["LAOS"]="856"
dial["LATVIA"]="371"
dial["LEBANON"]="961"
dial["LESOTHO"]="266"
dial["LIBERIA"]="231"
dial["LIBYA"]="218"
dial["LIECHTENSTEIN"]="41 75"
dial["LITHUANIA"]="370"
dial["LUXEMBOURG"]="352"
dial["MACAO"]="853"
dial["MACEDONIA"]="389"
dial["MADAGASCAR"]="261"
dial["MADEIRA"]="351 91"
dial["MALAWI"]="265"
dial["MALAYSIA"]="60"
dial["MALDIVES"]="960"
dial["MALI"]="223"
dial["MALTA"]="356"
dial["MARSHALL ISLANDS"]="692"
dial["MARTINIQUE"]="596"
dial["MAURITANIA"]="222"
dial["MAURITIUS"]="230"
dial["MEXICO"]="52"
dial["MICRONESIA"]="691"
dial["MOLDOVA"]="373"
dial["MONACO"]="377"
dial["MONGOLIA"]="976"
dial["MONTENEGRO"]="381"
dial["MONTSERRAT"]="1 664"
dial["MOROCCO"]="212"
dial["MOZAMBIQUE"]="258"
dial["MYANMAR"]="95"
dial["NAMIBIA"]="264"
dial["NAURU"]="674"
dial["NEPAL"]="977"
dial["NETHERLANDS"]="31"
dial["NETHERLANDS ANTILLES"]="599"
dial["NEW CALEDONIA"]="687"
dial["NEW ZEALAND"]="64"
dial["NICARAGUA"]="505"
dial["NIGER"]="227"
dial["NIGERIA"]="234"
dial["NIUE"]="683"
dial["NORFOLK ISLAND"]="6 72"
dial["NORTHERN MARIANAS"]="670"
dial["NORWAY"]="47"
dial["OMAN"]="968"
dial["PAKISTAN"]="92"
dial["PALAU"]="680"
dial["PANAMA"]="507"
dial["PAPUA NEW GUINEA"]="675"
dial["PARAGUAY"]="595"
dial["PERU"]="51"
dial["PHILIPPINES"]="63"
dial["PITCAIRN ISLAND"]="69 82"
dial["POLAND"]="48"
dial["PORTUGAL"]="351"
dial["PUERTO RICO"]="1 787"
dial["QATAR"]="974"
dial["REUNION"]="262"
dial["ROMANIA"]="40"
dial["RUSSIAN FEDERATION"]="7"
dial["RWANDA"]="250"
dial["SAMOA"]="684"
dial["SAN MARINO"]="378"
dial["SAO TOM AND PRINCIPE"]="239"
dial["SAUDI ARABIA"]="966"
dial["SENEGAL"]="221"
dial["SERBIA"]="381"
dial["SEYCHELLES"]="248"
dial["SIERRA LEONE"]="232"
dial["SINGAPORE"]="65"
dial["SLOVAKIA"]="421"
dial["SLOVENIA"]="386"
dial["SOLOMON ISLANDS"]="677"
dial["SOMALIA"]="252"
dial["SOUTH AFRICA"]="27"
dial["SOUTH GEORGIA"]="672"
dial["SOUTH SANDWICH ISLANDS"]="672"
dial["SPAIN"]="34"
dial["SPITZBERGEN"]=""
dial["SRI LANKA"]="94"
dial["ST PIERRE & MIQUELON"]="508"
dial["ST. HELENA"]="290"
dial["ST. KITTS & NEVIS"]="1 869"
dial["ST. LUCIA"]="1 758"
dial["ST. VINCENT & THE GRENADINES"]="1 809"
dial["SUDAN"]="249"
dial["SURINAME"]="597"
dial["SWAZILAND"]="268"
dial["SWEDEN"]="46"
dial["SWITZERLAND"]="41"
dial["SYRIA"]="963"
dial["TAIWAN"]="886"
dial["TAJIKISTAN"]="7"
dial["TANZANIA"]="255"
dial["THAILAND"]="66"
dial["TOGO"]="228"
dial["TONGA"]="676"
dial["TRINIDAD & TOBAGO"]="1 809"
dial["TUNISIA"]="216"
dial["TURKEY"]="90"
dial["TURKMENISTAN"]="993"
dial["TURKS AND CAICOS ISLANDS"]="1 809"
dial["TUVALU"]="688"
dial["UGANDA"]="256"
dial["UKRAINE"]="380"
dial["UNITED ARAB EMIRATES"]="971"
dial["URUGUAY"]="598"
dial["USA"]="1"
dial["UZBEKISTAN"]="7"
dial["VANUATU"]="678"
dial["VATICAN CITY STATE"]="39"
dial["VENEZUELA"]="58"
dial["VIETNAM"]="84"
dial["VIRGIN ISLANDS (UK)"]="1 809 49"
dial["VIRGIN ISLANDS (US)"]="1 809"
dial["WAKE ISLAND"]="69 93"
dial["WALLIS & FUTUNA ISLANDS"]="681"
dial["WESTERN SAMOA"]="685"
dial["WESTERN SAMOA"]="685"
dial["YEMEN"]="967"
dial["YUGOSLAVIA"]="381"
dial["ZAMBIA"]="260"
dial["ZAIRE"]="243"
dial["ZIMBABWE"]="263"


function setdial(fld, countryfld) {
	var ctry=Upper(countryfld.value)
	var cDial=dial[ctry]
	if (fld.value.length==0 && typeof(cDial) != "undefined")  {
			fld.value="+"+cDial+" ";
			setCaretToEnd(fld);

	}
}


function cleardial(fld, countryfld) {
	var ctry=Upper(countryfld.value)
	var cDial=dial[ctry]
	if (typeof(cDial) != "undefined")  {
			if (trim(fld.value)=="+"+cDial) {
				fld.value=""
			}
	}
	
}


function setCaretToEnd (control) {
    if (control.createTextRange) {
        var range = control.createTextRange();
        range.collapse(false);
        range.select();
    }
    else if (control.setSelectionRange) {
        control.focus();
        var length = control.value.length;
        control.setSelectionRange(length, length);
    }
}

