var bError = false;
var strErrorMessage = "";

function getValueById(fieldName) {
  var obj = gel(fieldName);
  if (obj.options == null) {
    return obj.value;
  } else {
    return obj.options[ obj.selectedIndex ].value;
  }
}



function checkZip(countryName) {
  return (aryRequireZip[countryName] == 1);
}



function loadStates(formName,sCountryName) {
  gel('requirezip').value = aryRequireZip[sCountryName];
  if (aryRequireZip[sCountryName] == 1) {
    gel("ziprequired").style.visibility = 'visible';
  } else {
    gel("ziprequired").style.visibility = 'hidden';
  }
  ajax_get("/store/store.asp?action=do&doaction=get_states_by_country&country_name="+escape(sCountryName),callback_gotStates);
}

function callback_gotStates(bSuccess,sStateData)
{
  if (bSuccess)
  {
    configureCountyField("select","state");
    gel('state').options.length = 0;
    gel('state').options[ gel('state').options.length  ] = new Option( sDropDownSelectString,"");

    var arrStates = eval("{" + sStateData +"}");
    for (i in arrStates)
      if (arrStates[i].state_name)
        gel('state').options[ gel('state').options.length  ] = new Option( arrStates[i].state_name, arrStates[i].state_code );
    if (gel('state').options.length <= 1)
      configureCountyField("input","state");
  }
}

function capitalizeMe(obj) {
  val = obj.value;
  newVal = '';
  val = val.split(' ');
  for(var c=0; c < val.length; c++) {
    newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
  }
  obj.value = newVal.substring(0,newVal.length-1);
}

function configureCounties(stateName,countyFieldName) {
  if (countyFieldName == '') {
    return false;
  }
  //var stateName = gel('state').value;
  var countyName = gel(countyFieldName).value;
  if (aryCounties[stateName] == null) {
    configureCountyField('input',countyFieldName);
  } else {
    configureCountyField('select',countyFieldName);
    var countyObj = gel(countyFieldName);
    var b = 0;
    var i = 0;
    for (var i in aryCounties[stateName]) {
      countyObj.options[b] = new Option(aryCounties[stateName][i],aryCounties[stateName][i]);
      b++;
    }
  }
  // now set the value back to what it was
  gel(countyFieldName).value = countyName;
}

function configureCountyField(type,countyFieldName) {
  objOldElement = gel(countyFieldName);

  if(!objOldElement || !objOldElement.parentNode || 
    !document.getElementById || !document.createElement) return;
  var objNewElement = document.createElement(type);

  if (type == 'input') 
    objNewElement.type = "text";
  objNewElement.hasFocus = false;
  objNewElement.setAttribute('id', countyFieldName);
  objNewElement.setAttribute('name', countyFieldName);
  objOldElement.parentNode.replaceChild(objNewElement, objOldElement);

  window.objTempElement = objNewElement;

  if (type == 'select') 
    gel(countyFieldName).className = 'select';

  return true;

}
