var countryDDLName = "countrySelect";
var stateDDLName = "stateSelect";
var ddlWidth = 200; // default
var textAreaHeight = 80; // default
var countryCodeCanada = "Canada";
var countryCodeJapan = "Japan";
var countryCodeUSA = "USA";
var countryCodeOther = "ZZ";
var postState = "";
var postCountry = "";
var csdJapaneseFlag = false;



function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the country selected with the counties from the country list
function populateCountryDDL(defaultCountry) {

  if ( postCountry != "" ) {
    defaultCountry = postCountry;
  }
  var countryLineArray = country.split("|");  // Split into lines
  var selObj = document.getElementById(countryDDLName);
  var promptText = "";

  if(csdJapaneseFlag) promptText = "国を選んでください";
  else promptText = "Select Country";		


  selObj.style.width = ddlWidth;
  selObj.options[0] = new Option(promptText,"");
  selObj.selectedIndex = 0;

  for (var loop = 0; loop < (countryLineArray.length - 1) ; loop++) {
    lineArray = countryLineArray[loop].split(":");
    countryCode  = TrimString(lineArray[0]);
    countryName  = TrimString(lineArray[1]);
    if ( countryCode != "" ) {
      selObj.options[loop + 1] = new Option(countryName, countryCode);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }  
}

function populateStateDDL() {

  var selObj = document.getElementById(stateDDLName);
  var foundState = false;
  var promptText = "";

  selObj.style.width = ddlWidth;	

  // Determine the first item in dropdown list.

  var selectedCountry = document.getElementById(countryDDLName).value;
  if(selectedCountry == countryCodeCanada){
	if(csdJapaneseFlag) promptText = "州または特別区を選んでください";
	else promptText = "Select a province or territory";
  }else if(selectedCountry == countryCodeJapan){
	if(csdJapaneseFlag) promptText = "都道府県を選んでください";
	else promptText = "Select a prefecture or city";
  }else if(selectedCountry == countryCodeUSA){
	if(csdJapaneseFlag) promptText = "州を選んでください";
	else promptText = "Select a state";
  }else{
	if(csdJapaneseFlag) promptText = "選択してください";
	else promptText = "Please select";
  }	

  // Empty options just in case new drop down is shorter
  if ( selObj.type == "select-one" ) {
   for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    selObj.options[0] = new Option(promptText,"");
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with states from the selected country
  var stateLineArray = state.split("|");  // Split into lines
  var optionCntr = 1;



  for (var loop = 0; loop < (stateLineArray.length - 1); loop++) {
    lineArray = stateLineArray[loop].split(":");
    countryCode  = TrimString(lineArray[0]);
    stateCode    = TrimString(lineArray[1]);
    stateName    = TrimString(lineArray[2]);

    if (document.getElementById(countryDDLName).value == countryCode && countryCode != "" ) {
    // If it's a input element, change it to a select
      if ( selObj.type == "text" ) {
        parentObj = document.getElementById(stateDDLName).parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name",stateDDLName);
        inputSel.setAttribute("id",stateDDLName);
		inputSel.style.width=ddlWidth;
		parentObj.appendChild(inputSel) ;
        selObj = document.getElementById(stateDDLName);
        selObj.options[0] = new Option(promptText,"");
        selObj.selectedIndex = 0;
      }
      if ( stateCode != "" ) {
        selObj.options[optionCntr] = new Option(stateName, stateCode);
      }
      // See if it's selected from a previous post
      if ( stateCode == postState && countryCode == postCountry ) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++
    }
  }

  // If the country has no states, change the select to a text box
  if ( !foundState ) {

    parentObj = document.getElementById(stateDDLName).parentNode;
    parentObj.removeChild(selObj);

    // Create the Input Field
    var inputEl = document.createElement("INPUT");
    inputEl.setAttribute("id", stateDDLName);
    inputEl.setAttribute("name", stateDDLName);
    inputEl.setAttribute("type", "text");
    inputEl.setAttribute("maxLength", "50");	
    inputEl.style.width=ddlWidth;
    inputEl.setAttribute("value", postState);
    parentObj.appendChild(inputEl) ;
  }
}

function getField(fieldName){
	return document.getElementById(fieldName);
};

// Hide fields. Requires an array of field names as an argument.
function hideFields(fieldList){
	for(i=0; i < fieldList.length; i++){
		document.getElementById(fieldList[i]).style.display='none';
	}
}

// Show fields. Requires an array of field names as an argument.
function showFields(fieldList){
	for(i=0; i < fieldList.length; i++){	
		document.getElementById(fieldList[i]).style.display='block';	
	}
}

// Create an INPUT field
function createInput(name, width, height, promptText){
	var field = document.createElement("INPUT");
	field.setAttribute("id", name);
	field.setAttribute("name", name);
    	field.setAttribute("type", "text");
   	field.value=promptText;
    	field.style.width=width;
    	field.style.height=height;
    	return field;
}

// Create an TEXTAREA field
function createTextArea(name, width, height, promptText){
	var field = document.createElement("TEXTAREA");
	field.setAttribute("id", name);
	field.setAttribute("name", name);    
    	field.value=promptText;
    	field.style.width=width;
    	field.style.height=height;
    	return field;
}

// Set fields value blank.
function resetFields(fieldList){
	for(var i=0; i < fieldList.length; i++){
		getField(fieldList[i]).value = "";				
	} 
}

// Set width of a field.
function setWidth(fieldName, width){
	var field = document.getElementById(fieldName);
	field.style.width = width;	
}

// Set height of a field.
function setHeight(fieldName, height){
	var field = document.getElementById(fieldName);
	field.style.height = height;	
}

function initCountryDDL(country) {
  populateCountryDDL(country);
  populateStateDDL();
}

function setCountryDDLName(ddlName){
	countryDDLName = ddlName;
}


function setStateDDLName(ddlName){
	stateDDLName = ddlName;
}
 
function setDDLWidth(width) {
	ddlWidth = width;
}

function setTextAreaHeight(height) {
	textAreaHeight = height;
}

function getDDLWidth() {
	return ddlWidth;
}

function setLabels(){
	
	var country = getField("shipcountry").value;						
		
	if(country != countryCodeOther){
		
		hideFields(new Array("labelcountry2","shipcountry2"));
			
		setLabel("labelcountry", "Country");				
		if(country == countryCodeCanada) setCanadaLabels();						
		if(country == countryCodeJapan) setJapanLabels();		
		if(country == countryCodeUS) setUSLabels();					
						
		return;
	}	
					
	showFields(new Array("labelcountry2","shipcountry2"));
	setOtherLabels();		
}	
	
function setUSLabels(){
	setLabel("labeladdress", "Street:");
	setLabel("labelcity", "City");
	setLabel("labelstate", "State:");	
	setLabel("labelzip", "Zip Code:");	
}
	
function setCanadaLabels(){
	setLabel("labeladdress", "Street:");
	setLabel("labelcity", "City");
	setLabel("labelstate", "Province/Territory:");	
	setLabel("labelzip", "Post Code:");		
}
	
function setJapanLabels(){
	setLabel("labeladdress", "Street:");
	setLabel("labelcity", "City/Town");
	setLabel("labelstate", "Prefecture:");	
	setLabel("labelcountry", "Country:");	
}
	
function setOtherLabels(){
	setLabel("labelcountry", "");
	setLabel("labeladdress", "Street:");
	setLabel("labelcity", "City/Town");
	setLabel("labelstate", "State:");	
	setLabel("labelzip", "Post Code:");	
	setLabel("labelcountry2", "Country:");	
}
	
function setLabel(labelName, val){			
	label = "";
	if(val != null && val != "") label = val;
	td = document.getElementById(labelName);
	td.innerHTML = label;	
}

function setCsdJapaneseFlag(flag){
	if(flag != "")	csdJapaneseFlag = flag;
}


