var CountyList = new Array();
var cIgnore = false;
var detect = navigator.userAgent.toLowerCase();
var isSafari;

if (checkIt('safari')) isSafari = "True";

function checkIt(string)
{
    place = detect.indexOf(string) + 1;
    return place;
}

function catchEnter(e,form) {
    if (!e) {var e = window.event;}
    if (e.keyCode) {
        code = e.keyCode;}
    else if (e.which) {
        code = e.which;
    }
    if (code==13) {
        form.submit();
    }
}
function capitalise(t) {
    var a = t.value.split(/\s+/g); // split the sentence into an array of words
    if (cIgnore == false) {
    for (i = 0 ; i < a.length ; i ++ ) {
		if(t.name != "postcode" && t.name != "deliveryPostcode"){
        	var firstLetter = a[i].substring(0, 1).toUpperCase();
        	var restOfWord = a[i].substring(1, a[i].length).toLowerCase();
        	a[i] = firstLetter + restOfWord; // re-assign it back to the array and move on
            // Special case for Irish/Scottish names starting with "Mc" or "O'";-)
            if(t.name == "surname" || t.name == "deliveryName"){				                    
                    if(a[i].substring(0,2) == "Mc" || a[i].substring(0,2) == "O'"){
                      a[i] = a[i].substring(0,2) + a[i].substring(2,3).toUpperCase() + a[i].substring(3);
                    }
					if (a[i].substring(0,3) == "Mac"){
                      a[i] = a[i].substring(0,3) + a[i].substring(3,4).toUpperCase() + a[i].substring(4);
					}
                    var dash = a[i].indexOf("-"); 
                    if(dash > 0){
                      a[i] = a[i].substring(0,dash+1) + a[i].substring(dash+1,dash+2).toUpperCase() + a[i].substring(dash+2);
                        if(a[i].substring(dash+1,dash+3) == "Mc" || a[i].substring(dash+1,dash+3) == "O'"){
                            a[i] = a[i].substring(0,dash+3) + a[i].substring(dash+3,dash+4).toUpperCase() + a[i].substring(dash+4);
                        }
                        if(a[i].substring(dash+1,dash+4) == "Mac"){
                            a[i] = a[i].substring(0,dash+4) + a[i].substring(dash+4,dash+5).toUpperCase() + a[i].substring(dash+5);
                        }
                    }                                         
            }                
			if((t.name == "company" || t.name == "deliveryCompany") && (a[i].indexOf(".") != -1)){
				a[i] = a[i].toUpperCase();
			}
			if (t.name.indexOf("mail") != -1){
				a[i] = a[i].toLowerCase();
			}
		}
		else {
				a[i] = a[i].toUpperCase();
		}
    }
    t.value = a.join(' '); // join it back together
    }
    cIgnore = false;
    t.style.background = 'white';
}

function validateForm(theForm) {
var result = true;
    for (var i=0; i < theForm.length; i++){
        if (theForm[i].name == "country" || theForm[i].name == "deliveryCountry") {
                    if (theForm[i].name == "deliveryCountry") {
		                var county = theForm.deliveryCounty;
		                var Country = theForm.deliveryCountry;
                    }
	                if (theForm[i].name == "country") {
		                var county = theForm.county;
		                var Country = theForm.country;
	                }	                
	                var sCountry = Country.options[Country.selectedIndex].text;
	                var scounty = county.options[county.selectedIndex].text;
	                county.disabled = false;
					Country.disabled = false;
                if ((sCountry != "United Kingdom" && sCountry != "Ireland" && sCountry != "USA" && sCountry != "Canada" && scounty.indexOf("not applicable") > 0) || (sCountry != "United Kingdom" && sCountry != "Ireland" && sCountry != "USA" && sCountry != "Canada" && scounty.indexOf("not applicable") == (-1))) {
		                Country.disabled = false;
		                county.selectedIndex = 1; 
		                county.options[county.selectedIndex].value = ""; 
		                result = true;
	                } 
                if (sCountry == "United Kingdom" && scounty.indexOf("not applicable") > 0 ) {alert('Please choose United Kingdom County or select country other then United Kingdom'); return false;}
                if (sCountry == "Ireland" && scounty.indexOf("not applicable") > 0 ) {alert('Please choose Irish County or select country other then Ireland'); result = false;}
                if (sCountry == "USA" && scounty.indexOf("not applicable") > 0 ) {alert('Please choose USA State or select country other then USA'); result = false;}
                if (sCountry == "Canada" && scounty.indexOf("not applicable") > 0 ) {alert('Please choose Canadian State or select country other then Canada'); result = false;}
                if (sCountry == "United Kingdom" && scounty.indexOf("--") >= 0 ) {alert('Please select United Kingdom County'); result = false;}
                if (sCountry == "Ireland" && scounty.indexOf("--") >= 0 ) {alert('Please select Irish County'); result = false;}
                if (sCountry == "USA" && scounty.indexOf("--") >= 0 ) {alert('Please select USA State'); result = false;}
                if (sCountry == "Canada" && scounty.indexOf("--") >= 0 ) {alert('Please select Canadian State'); result = false;}
                }
        }
return result;
}

function modifyCounty(theCountry) {
	if (theCountry.name == "country") {
		var county = document.getElementById("county");
	} else {
		var county = document.getElementById("deliveryCounty");
		//document.getElementById("deliveryCountry").disabled = true;
	}
	var sCountry = theCountry.options[theCountry.selectedIndex].text;       
	var aC;
	var f; 
	var iFrom;
	var iTo;
	var newCountyList = new Array();
	var selected = county.selectedIndex
	if (CountyList.length == 0) {
		for (f=0; f < county.options.length; f++) {
			CountyList[f] = county.options[f].text;
		}
	}
	if(document.getElementById("pcaBt")) {
		var pcBt = true;
	}
	switch(sCountry) {
		case "United Kingdom":
		if(pcBt){
			//Show the postcodeanywhere button
			document.getElementById("pcaBt").style.display='';		
			//Clear the list
			//document.getElementById("pcaSelectXR62").length=0;
			//Hide the list
        if (isSafari=="True"){
            while (document.getElementById("pcaSelectXR62").options.length > 0) {
                document.getElementById("pcaSelectXR62").options[0] = null;
                }
            document.getElementById("pcaSelectXR62").style.height='0px';
            document.getElementById("pcaSelectXR62").style.visibility='hidden';
            }
        else{
            document.getElementById("pcaSelectXR62").length=0;
            document.getElementById("pcaDivXR62").style.display='none';
            }
            
		}
			county.disabled = false;//style.display='';
		  for (f=0; f < CountyList.length-1; f++) {
		    if (CountyList[f] == "-- England --") {
				iFrom = f;
			}
			if (CountyList[f] == "-- Republic of Ireland --") {
				iTo = f;
			}
		  }
		  	county.options.length = 0;
			county.options[county.options.length] = new Option("--- Please select UK County ---");
			for (f = 0; f < iTo-iFrom; f++) {
				if (f+iFrom == selected) {
					county.options[county.options.length] = new Option(CountyList[f+iFrom],CountyList[f+iFrom],true,true);
					county.selectedIndex = county.options.length-1;
				} else {
					county.options[county.options.length] = new Option(CountyList[f+iFrom],CountyList[f+iFrom],false,false);
				}
			}
		  break;    
		case "Ireland":
		if(pcBt){
            //Hide the postcodeanywhere button
            document.getElementById("pcaBt").style.display='none';        
            //Clear the list
            //document.getElementById("pcaSelectXR62").length=0;
            //Hide the list
        if (isSafari=="True"){
            while (document.getElementById("pcaSelectXR62").options.length > 0) {
                document.getElementById("pcaSelectXR62").options[0] = null;
                }
            document.getElementById("pcaSelectXR62").style.height='0px';
            document.getElementById("pcaSelectXR62").style.visibility='hidden';
            }
        else{
            document.getElementById("pcaSelectXR62").length=0;
            document.getElementById("pcaDivXR62").style.display='none';
            }
		}
			county.disabled = false;//style.display='';
		  for (f=0; f < CountyList.length-1; f++) {
		    if (CountyList[f] == "-- Republic of Ireland --") {
				iFrom = f;
			}
			if (CountyList[f] == "-- United States of America --") {
				iTo = f;
			}
		  }
		  	county.options.length = 0;
			county.options[county.options.length] = new Option("--- Please select Irish County ---");
			for (f = 0; f < iTo-iFrom; f++) {
				if (f+iFrom == selected) {
					county.options[county.options.length] = new Option(CountyList[f+iFrom],CountyList[f+iFrom],true,true);
					county.selectedIndex = county.options.length-1;
				} else {
					county.options[county.options.length] = new Option(CountyList[f+iFrom],CountyList[f+iFrom],false,false);
				}
			}
		  break;    
		case "USA":
		if(pcBt){
            //Hide the postcodeanywhere button
            document.getElementById("pcaBt").style.display='none';        
            //Clear the list
            //document.getElementById("pcaSelectXR62").length=0;
            //Hide the list
        if (isSafari=="True"){
            while (document.getElementById("pcaSelectXR62").options.length > 0) {
                document.getElementById("pcaSelectXR62").options[0] = null;
                }
            document.getElementById("pcaSelectXR62").style.height='0px';
            document.getElementById("pcaSelectXR62").style.visibility='hidden';
            }
        else{
            document.getElementById("pcaSelectXR62").length=0;
            document.getElementById("pcaDivXR62").style.display='none';
            }
		}
			county.disabled = false;//style.display='';
		  for (f=0; f < CountyList.length-1; f++) {
		    if (CountyList[f] == "-- United States of America --") {
				iFrom = f;
			}
			if (CountyList[f] == "-- Canada --") {
				iTo = f;
			}
		  }
		  	county.options.length = 0;
			county.options[county.options.length] = new Option("--- Please select US State ---");
			for (f = 0; f < iTo-iFrom; f++) {
				if (f+iFrom == selected) {
					county.options[county.options.length] = new Option(CountyList[f+iFrom],CountyList[f+iFrom],true,true);
					county.selectedIndex = county.options.length-1;
				} else {
					county.options[county.options.length] = new Option(CountyList[f+iFrom],CountyList[f+iFrom],false,false);
				}
			}
		  break;
		case "Canada":
		if(pcBt){
            //Hide the postcodeanywhere button
            document.getElementById("pcaBt").style.display='none';        
            //Clear the list
            //document.getElementById("pcaSelectXR62").length=0;
            //Hide the list
        if (isSafari=="True"){
            while (document.getElementById("pcaSelectXR62").options.length > 0) {
                document.getElementById("pcaSelectXR62").options[0] = null;
                }
            document.getElementById("pcaSelectXR62").style.height='0px';
            document.getElementById("pcaSelectXR62").style.visibility='hidden';
            }
        else{
            document.getElementById("pcaSelectXR62").length=0;
            document.getElementById("pcaDivXR62").style.display='none';
            }
		}
			county.disabled = false;//style.display='';
		  for (f=0; f < CountyList.length-1; f++) {
		    if (CountyList[f] == "-- Canada --") {
				 iFrom = f;
			}
		  }
			 iTo = CountyList.length;
		  	county.options.length = 0;
			county.options[county.options.length] = new Option("--- Please select Canadian State ---");
			for (f = 0; f < iTo-iFrom; f++) {
				if (f+iFrom == selected) {
					county.options[county.options.length] = new Option(CountyList[f+iFrom],CountyList[f+iFrom],true,true);
					county.selectedIndex = county.options.length-1;
				} else {
					county.options[county.options.length] = new Option(CountyList[f+iFrom],CountyList[f+iFrom],false,false);
				}
			}
		  break;
		default:
        if(pcBt){ 
            //Hide the postcodeanywhere button
            document.getElementById("pcaBt").style.display='none';        
            //Clear the list
            //document.getElementById("pcaSelectXR62").length=0;
            //Hide the list
        if (isSafari=="True"){
            while (document.getElementById("pcaSelectXR62").options.length > 0) {
                document.getElementById("pcaSelectXR62").options[0] = null;
                }
            document.getElementById("pcaSelectXR62").style.height='0px';
            document.getElementById("pcaSelectXR62").style.visibility='hidden';
            }
        else{
            document.getElementById("pcaSelectXR62").length=0;
            document.getElementById("pcaDivXR62").style.display='none';
            }
        }
			county.options.length = 0;
			county.options[county.options.length] = new Option(CountyList[0]);
			county.options[county.options.length] = new Option(CountyList[1]);				
			county.selectedIndex = 1; 
			county.options[1].value = "";	
			county.disabled = true; //style.display='none';
	}
}  
var al = null;

function openPopUpWindow(add) {
		reload = true;
		if (al) {
			if (al.closed) {
				reload = true;
			} else {
				reload = false;
			}
		}
		if (reload) {
			al = window.open(add,'al','height=620,width=500,scrollbars=yes,status=no,toolbar=no,menubar=no,location=no,resizable=yes');
		}
	al.focus();
}
