stateValue = document.getElementById("state").value;
document.getElementById("captchaWord").value ="";
updateState();
document.getElementById("state").value = stateValue;
var mathErr = false;
if (document.getElementById("state").selectedIndex < 0) {
  document.getElementById("state").selectedIndex = 0;
}
function updateState() {
  clearProvince(true);
  document.getElementById("state").innerHTML = "";
  drpState = document.getElementById("state");
  selectedCountryID = document.getElementById("country").value;
  if (selectedCountryID == "1") {
	document.getElementById("spnZip").innerHTML = "Zip Code"
	document.getElementById("lblState").innerHTML = "State"   		
  }
  else {
	document.getElementById("spnZip").innerHTML = "Postal Code"   	
	document.getElementById("lblState").innerHTML = "Province"   	
  }
  for (i=0; i<stateIDs.length; ++i) {
    if (stateIDs[i] == selectedCountryID) {
      opt = document.createElement("OPTION");     
      drpState.appendChild(opt);
      //drpState.options.add(opt);
      opt.value = stateValues[i];
      opt.innerHTML = stateNames[i];
    }
  }
  //if (drpState.options.length == 1 && drpState.options[0].text == "") {
  //  document.getElementById("spnState").style.display = "none";
  //}
  //else {
  //  document.getElementById("spnState").style.display = "";  
  //}
  if (selectedCountryID == "2") {
    document.getElementById("caChallenge").style.display = "";
    if (mathErr) {
        document.getElementById("errCaChallenge").style.display = "";
    }
	if (/MSIE 6/i.test(navigator.userAgent)){
		document.styleSheets[0].addRule('.footer ','position:relative;');
	}
  }
  else {
    document.getElementById("caChallenge").style.display = "none";
    document.getElementById("errCaChallenge").style.display = "none";    
  }
}

function submitForm() {
    if (document.getElementById("txtCaChallenge").value != 2 && document.getElementById("country").value == "2") {
        document.getElementById("errCaChallenge").style.display = "";
        mathErr = true;
        return;
    }
	document.getElementById("address").submit();
}

function checkMathError(txtMath) {
    if (mathErr && (txtMath.value == 2 || document.getElementById("country").value != "2")) {
        document.getElementById("errCaChallenge").style.display = "none";
        mathErr = false;
        return;
    }
}    
function zipCodeChange() {
	if ( document.getElementById("country").value < 3 ) {
		xmlHttp=getAjaxObjectTB()
		if (xmlHttp==null) {
		return;
		}
		zipC = document.getElementById("zipcode").value;
		if (zipC.length == 6) {
			zipC = zipC.substring(0,3) + " " + zipC.substring(3,6);
			document.getElementById("zipcode").value = zipC;
		}
		url="/?cmd=sb-register&zipchange="+ zipC + "&sid=" + Math.random();
		xmlHttp.onreadystatechange=zipCodeChangeResult;
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}
}
function zipCodeChangeResult() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
	        result = xmlHttp.responseText
	        if (result.length > 1 && result.length < 50) {
			if ( document.getElementById("country").value < 3 ) {
				resultArray = result.split(",");
				if (document.getElementById("country").value != resultArray[0]) {
					launchStPop('Generic','Whoops...','highlight10',"The zip/postal code ("+ zipC +") entered does not match the country chosen. Please update the zip/postal code and press TAB.");
					clearProvince();
				} else if (resultArray[1] == "QC") {
					launchStPop('Generic','Whoops...','highlight10',"We're sorry, registration is not currently open to Quebec residents.");
					clearProvince();
				} else {
					document.getElementById("state").value = resultArray[1];
				}
			}
	        } else {
			if ( document.getElementById("country").value < 3 ) {
				launchStPop('Generic','Whoops...','highlight10',"The zip/postal code ("+ document.getElementById("zipcode").value +") entered does not match the country chosen. Please update the zip/postal code and press TAB.");
				clearProvince();
			}
	        }
	}
}
  function clearProvince(init) {
    if (init) {
	document.getElementById("fullname").focus();
    } else {
	document.getElementById("zipcode").value = "";
	document.getElementById("zipcode").focus();
    }
  }

