  var countryArray = new Array("US|United States","CA|Canada","ME|Mexico","CB|Caribbean","SA|C & S America","EU|Europe","AF|Africa","ME|Middle East","AS|Asia","AU|Australasia");

  function addCountryList(myselect) {
    var codeArray, x;
    for( x=0; x < countryArray.length; x++ ) {
	  codeArray = countryArray[x].split("|");
	  var new_option = document.createElement("OPTION");
	  new_option.value = codeArray[0];
	  new_option.text = codeArray[1];
      //myselect.add(new_option);
	  myselect.options[x+1] = new_option;
	}
  }
  
  function addCountryListSelected(couSelect, couValue, staSelect, staValue) {
    var codeArray, x;
	couSelect.length = 0;
	var new_option = document.createElement("OPTION");
    new_option.value = "";
	new_option.text = "Country";	  
	//couSelect.add(new_option);
	couSelect.options[0] = new_option;
	
    for( x=0; x < countryArray.length; x++ ) {
	  codeArray = countryArray[x].split("|");
	  var new_option = document.createElement("OPTION");
      new_option.value = codeArray[0];
	  new_option.text = codeArray[1];
      //couSelect.add(new_option);
	  couSelect.options[x+1] = new_option;

	  if( codeArray[0] == couValue ) {
	    new_option.selected = true;
		populateRegion(staSelect, couValue, staValue, false);
	  }
	}
  }
  

var zipLength = 0;
var zipLengthShipping = 0;

function initializeRegions( selectedRegion ) {
	if( document.account.country.options[document.account.country.selectedIndex].value != '--NULL--' ) {
		populateRegion(document.account.state_or_province,document.account.country.options[document.account.country.selectedIndex].value,selectedRegion,false);
	}
}

function populateRegion(optionField,selected,selectedRegion,shipping) {

    var selectedArray = new Array( new Array("-", "--------") );
	if (selected == '') {
        selectedArray = XX_Array;
    } else {
        selectedArray = eval(selected + "_Array");
    }
    while (selectedArray.length < optionField.options.length+1) {
        optionField.options[(optionField.options.length - 1)] = null;
    }
    var selectedIx=0;
    for (var i=0; i < selectedArray.length; i++) {
        if( i==selectedArray.length-1 ) {
            if( shipping ) {
                zipLengthShipping = selectedArray[i];
            } else {
                zipLength = selectedArray[i];
            }
        } else {
            var option = new Option(selectedArray[i][1], selectedArray[i][0]);
            if( option.value == selectedRegion ) {
                selectedIx = i;
            }
            eval("optionField.options[i]=option");
            optionField.options[selectedIx].selected=true;
        }
    }
    if (optionField.options[0].value == '') {
        optionField.options[0]= null;
        //if ( navigator.appName == 'Netscape') {
        //    if (parseInt(navigator.appVersion) < 4) {
        //    window.history.go(0);
        //    }else {
        //        if (navigator.platform == 'Win32' || navigator.platform == 'Win16') {
        //            window.history.go(0);
        //        }
        //    }
        //}
    }
}

function verifyZipCode( zipCode, country, shipping ) {

	var mask_CA = "ANA NAN";
	var mask_US = "NNNNN";
	var mask_US2 = "NNNNN-NNNN";
	var mask_SE = "NNN NN";
	var mask_PL = "NN-NNN"
	var mask_NL = "NNNN AA";

	var zipCodeLength = zipLength;

	if( shipping ) {
		zipCodeLength = zipLengthShipping;
	}

	if( zipCodeLength==0 ) {
		return true;
	} else {
		// check the length for all but SPECIAL zip codes
		if( zipCodeLength >= 0 && zipCodeLength != zipCode.length ) {
			window.alert( "The postal code is not the right length" );
			return false;
		}
		// check the special zip codes now
		if( country == "CA" ) {
			if( !checkMask( zipCode, mask_CA ) ) {
				window.alert( "Canadian postal codes are of the following format: ANA NAN - where A is a letter and N is a number" );
				return false;
			}
		}
		if( country == "US" ) {
			if( !checkMask( zipCode, mask_US ) && !checkMask( zipCode, mask_US2 ) ) {
				window.alert( "US postal codes are of the following format: ##### or #####-####" );
				return false;
			}
		}
		if( country == "SE" ) {
			if( !checkMask( zipCode, mask_SE ) ) {
				window.alert( "Sweedish postal codes are of the following format: ### ##" );
				return false;
			}
		}
		if( country == "PL" ) {
			if( !checkMask( zipCode, mask_PL ) ) {
				window.alert( "Polish postal codes are of the following format: ##-###" );
				return false;
			}
		}
		if( country == "NL" ) {
			if( !checkMask( zipCode, mask_NL ) ) {
				window.alert( "Dutch postal codes are of the following format: NNNN AA - where A is a letter and N is a number" );
				return false;
			}
		}
		return true;
	}
}

function checkMask( inStr, mask ) {
	if( inStr.length != mask.length ) {
		return false;
	}

	var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var numbers = "0123456789";

	for (var i=0; i < inStr.length; i++ ) {
		var chr = inStr.charAt(i);
		var msk = mask.charAt(i);
		if( msk=='A' ) {
			if (letters.indexOf(chr) == -1 ) {
          	return false;
  			}
		}
		if( msk=='N' ) {
			if (numbers.indexOf(chr) == -1 ) {
          	return false;
  			}
		}
		if( msk==' ' ) {
			if( chr != ' ' ) {
				return false;
			}
		}
		if( msk=='-' ) {
			if( chr != '-' ) {
				return false;
			}
		}
	}
	return true;
}
