stateValue = document.getElementById("state").value;
updateState();
document.getElementById("state").value = stateValue;
var mathErr = false;
if (document.getElementById("state").selectedIndex < 0) {
  document.getElementById("state").selectedIndex = 0;
}
function updateState() {
  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.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";
	document.getElementById("lblState").innerHTML = "" 		
  }
  else {
    document.getElementById("spnState").style.display = "";  
  }
  if (selectedCountryID == "2") {
    document.getElementById("caChallenge").style.display = "";
    if (mathErr) {
        document.getElementById("errCaChallenge").style.display = "";
    }
  }
  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 getXmlHttpObject() { 
        var objXMLHttp=null
            if (window.XMLHttpRequest) {
                objXMLHttp=new XMLHttpRequest()
            }
            else if (window.ActiveXObject) {
                objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
            }
        return objXMLHttp
    } 

    function logIn() {
        pswd = document.getElementById("pswdCP").value;
        eml = document.getElementById("emailAddressCP").value;
        if (eml == "") {
            alert("Please enter your Email Address");
            return;
        }
        if (pswd == "") {
            alert("Please enter your Password");
            return;
        }
        document.getElementById("btnLoginCP").disabled = true;
        document.getElementById("frmLogIn-cp").submit();
    }  
    
    function createPassword() {
        eml = document.getElementById("emailAddressCP").value;
        pswd = document.getElementById("newPswd").value;
        cnfm = document.getElementById("newPswdCnfrm").value;
        if (eml == "") {
            alert ("Please enter your Email Address");
            return;
        }
        if (pswd == "") {
            alert ("Please enter a New Password");
            return;
        }
        if (pswd != cnfm) {
            alert ("The New Password and Confirm Password must match.");
            return;
        }
        if (pswd != "" && eml != "") {
            xmlHttp=getXmlHttpObject()
            if (xmlHttp==null) {
                alert ("Your browser does not support HTTP Request, we are unable to pull up your information." + 
                String.fromCharCode(10) + String.fromCharCode(10) + 
                'Please enter your information in the "First Time Winner" section or contact us at info@prodege.com.');                
                return;
            }
            tai = document.getElementById("tai").value;
            url="/?cmd=cp-create-pswd&tai=" + tai + "&pswrd=" + escape(pswd).replace(/\+/g,'%2b') + "&eml=" + eml;
            url=url+"&amp;sid="+Math.random();
            xmlHttp.onreadystatechange=checkPswdResults;
            xmlHttp.open("GET",url,true)
            xmlHttp.send(null)
        }
    }
    function checkPswdResults() {
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
            result = xmlHttp.responseText.split('|');
            if (result[0] == 3) {
                document.getElementById("pswdCP").value = document.getElementById("newPswd").value; 
                document.getElementById("frmLogIn").submit();
                //location.href = "/?cmd=cp-complete&tai=" + document.getElementById("tai").value;
            } 
            else if (result[0] == 1) {
                alert ("Your Email Address was not found on our system." + 
                String.fromCharCode(10) + String.fromCharCode(10) + 
                'Please enter a diffrent email address or enter your information in the "First Time Winner" section.');                
            }
            else if (result[0] == 2) {
                alert ("Your Email Address is already associated with a password." + 
                String.fromCharCode(10) + String.fromCharCode(10) + 
                'To retrieve your password click on "Email My Password"');                
            }
            else {
                alert("There was an unexpected error looking up you information." + 
                String.fromCharCode(10) + String.fromCharCode(10) + "Please try again." +
                String.fromCharCode(10) + String.fromCharCode(10) + "or contact us at info@prodege.com");
            }
        }
    }

    function loginEnter(evt) {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
        if(charCode == 13) {
            logIn();
            return false;
        }  
    }   
    function newPswdEnter(evt) {
        evt = (evt) ? evt : event;
        var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
        if(charCode == 13) {
            createPassword();
            return false;
        }  
    }   
    
    function sendEmail() {
        xmlHttp=getXmlHttpObject()
        if (xmlHttp==null) {
            alert ("Your browser does not support HTTP Request. Please contact us at info@prodege.com to get your password.")
            return;
        }
        txtEmail = document.getElementById("emailAddressCP").value;
        if (txtEmail == "") {
            alert ("Please enter your Email Address.")
            return;
        }
        url="/?cmd=sb-pswd-reminder&email=" + txtEmail;
        url=url+"&amp;sid="+Math.random();
        xmlHttp.onreadystatechange=getPResult;
        xmlHttp.open("GET",url,true)
        xmlHttp.send(null)
    }

    function getPResult() {
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
            result = xmlHttp.responseText
            if (result == 1) {
                alert("Your Password has been sent.");
            } 
            else  if (result == 2) {
                alert("Your Email Address was not found on our system.");
            }
            else if (result == 3) {
                alert("Please enter a valid Email Address.");
            } 
            else  {
                alert("There was an unexpected error looking up you information." + 
                String.fromCharCode(10) + String.fromCharCode(10) + "Please try again." +
                String.fromCharCode(10) + String.fromCharCode(10) + "or contact us at info@prodege.com");
            } 
        }
    }

    function getForm() {
      var theForm = document.getElementById(address);     
      if (theForm == undefined)
        theForm = document.getElementsByName(formName)[0];
      return theForm;
    }
    
