var password_ok = false;
var confirmpw_ok = false;
var secretq_ok = false;
var self_secretq = false;
var domain = "pureinbox.com";

function chkPasswdLength() {
        var len = document.getElementById('newpassword').value.length;
        var tips = document.getElementById('passwordTips');
        if (len < min_passwd_len) {
          if (len > 0) {
            tips.innerHTML = "Password is too short";
	    tips.className = "signupwarning";
          }
          else {
            tips.innerHTML = "";
	    tips.className = "";
          }
          password_ok = false;
        }
        else {
          tips.innerHTML = "";
	  tips.className = "hint2";
          password_ok = true;
        }
}

function chkConfirmPassword() {
        var pw1 = document.getElementById('newpassword');
        var pw2 = document.getElementById('confirmpassword');
        var tips = document.getElementById('confirmpasswordTips');
        if (pw2.value.length <= 0) {
          tips.innerHTML = "";
	  tips.className = "";
          confirmpw_ok = false;
        }
        else {
          confirmpw_ok = (pw1.value == pw2.value);
          if (!confirmpw_ok) {
            tips.className = "signupwarning";
            tips.innerHTML = "Passwords do not match";
          }
          else {
            tips.innerHTML = "";
	    tips.className = "hint2";
          }
        }
}


function chkAddress() {
        var address = document.getElementById('newaddress').value;
        var fullAddress = address + "@" + domain;
        var tips = document.getElementById('addressTips');
        var addressOK_text = document.getElementById('address_ok');

        // check for address length
        if (address.length < min_address_len || address.length > 30) {
          if (address.length <= 0) {
            addressOK_text.value = "";
	    tips.className = "";
            tips.innerHTML = "";
          }
          else {
            addressOK_text.value = "";
	    tips.className = 'signupwarning1';
            tips.innerHTML = "Please choose a username between 6 and 30 characters long.";
          }
          return;
        }

        // check for address characters
        var reg = /^\w+((\w+)|(\.\w+))$/;
        if (!reg.test(address)) {
          addressOK_text.value = "";
	  tips.className = 'signupwarning1';
          tips.innerHTML = "Only letters (a-z), numbers (0-9), and periods(.) are allowed.";
          return;
        }

        // check for duplicate users in database
	tips.className = 'signupwarning1';
        tips.innerHTML = "<img src='../images/loading.gif'><font color='" + tipsColors[2] + "'>Checking, please wait...</font>";


        checkMailboxURL = "locatemailbox.php?domain=" + domain + "&mailbox="+encodeURI(fullAddress);
        setTimeout("signuppost.location.href = '" + checkMailboxURL + "'", 1250);
}

function checkSubmit() {
        if (document.getElementById('address_ok').value.length <= 0) {
                document.getElementById('newaddress').focus();
                return false;
        }
        if (!password_ok) {
                document.getElementById('newpassword').focus();
                return false;
        }
        if (!confirmpw_ok) {
                document.getElementById('confirmpassword').focus();
                return false;
        }
        if (!secretq_ok) {
                document.getElementById('secretq').focus();
                return false;
        }
        return true;
}

function chkQuestionAnswer() {
        secretq_ok = (document.signupfrm.secretquestion.value.length > 0 &&  document.signupfrm.secretanswer.value.length > 0);
}

function updateQuestion() {
        var selectbox = document.signupfrm.secretq;
        var tips = document.getElementById('secretqTips');

	tips.className = "";
        if (selectbox.selectedIndex == 0) {
          document.getElementById('wmoq').style.display = 'none';
	  document.getElementById('wmoqtext').value = "";
	  tips.className = "signupwarning";
          tips.innerHTML = "Please select at least one question.";
          document.signupfrm.secretquestion.value = "";
          selectbox.focus();
        }
	else if (selectbox.options[selectbox.selectedIndex].value == "WMOQ") {
		document.signupfrm.secretquestion.value = "";
		document.getElementById('wmoq').style.display = 'block';
		document.getElementById('wmoqtext').focus();
	}
        else {
	  document.getElementById('wmoq').style.display = 'none';
	  document.getElementById('wmoqtext').value = "";
          tips.innerHTML = "";
          document.signupfrm.secretquestion.value = selectbox.options[selectbox.selectedIndex].value;
          document.signupfrm.secretanswer.focus();
          document.signupfrm.secretanswer.select();
	  tips.className = "hint4";
        }
}

function updateWMOQ() {
        var tips = document.getElementById('secretqTips');
	var wmoq = document.getElementById('wmoqtext');

	if (wmoq.value.length > 0) {
          tips.innerHTML = "";
          document.signupfrm.secretquestion.value = wmoq.value; 
          document.signupfrm.secretanswer.focus();
          document.signupfrm.secretanswer.select();
                                tips.className = "hint4";
	}
	else {
                                tips.className = "signupwarning";
          tips.innerHTML = "Please write your own secret question.";
          document.signupfrm.secretquestion.value = "";
          document.getElementById('wmoqtext').focus();
	}
}

function updateAnswer() {
        var answer = document.getElementById("secretanswer");
        var tips = document.getElementById("secretaTips");

        tips.className = "";
        if (answer.value.length <= 0) {
	  		tips.className = "signupwarning";
          tips.innerHTML = "Please provide an answer to the secret question.";
        }
        else {
          tips.innerHTML = "";
	  			tips.className = "hint2";
        }
}

function refreshCaptchaImg() {
	var img = document.getElementById("captchaimg");
	img.src = "/inc/CaptchaSecurityImages.php?width=120&height=60&characters=5&" + Math.random();
}
