var xmlHttp
var passed = false;
var userid = "";


function checkEmailAddress(field){
    var varTemp = field;
    if (varTemp.value != ''){
        var value = String(varTemp.value);

        for (index = 0; index < value.length; index++){
            if (value.charAt(index) == '@') {
                passed = true;
            }
        }

        if (passed == false){
         alert('Please use {ie. username@domain.com} format for all email entries.');
         varTemp.value = '';
         varTemp.focus();
        }
    }
}


function validateEmail(str) {
    var tempID = str;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="verify.asp?z="+currentGuid+"&"
    url=url+"q=email&em="+tempID
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}


function validateCAP(str,id) {
    var tempID = str;
    userid = id;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request");
        return;
    }
    var url="verify.asp?"
    url=url+"q=cap&em="+tempID + "&userid="+id
    xmlHttp.onreadystatechange=stateChanged2;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}


function stateChanged() {
    if (xmlHttp.readyState==4) {
    	var resp = xmlHttp.responseText;
    	if (resp.indexOf('BadEmail',0) >= 0) {
    		if(document.getElementById("newuserid")){
    			try{document.getElementById("fred").innerHTML=resp;}catch(ex){alert("Email already in use.");}        	
        		document.getElementById("email").value="";
        		document.getElementById("email").focus();
        	}else{	
        		alert("Email already in use.");
        		document.getElementById("email").value="";
        		document.getElementById("email").focus();        	
        	
        	}
        } else {
        	try{document.getElementById("fred").innerHTML='';}catch(ex){;}
        }
    }
}

var captchaGood=false;
var submitOnGoodCAP = false;
function stateChanged2() {
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
    	var resp = xmlHttp.responseText;
    	if (resp.indexOf('<!--BadCaptcha-->',0) >= 0) {
        	document.getElementById("fred").innerHTML=resp;
        	document.getElementById("capcha1").innerHTML="<img src='aspcaptcha.asp?z="+currentGuid+"&userid=" + userid + "' alt='PassCode' width=86' 'height='21' />";
        	document.getElementById("checktext").value="";
        	//document.getElementById("checktext").focus();
        	captchaGood=false;
        } else {
        	//alert('the captcha succeeded')
        	document.getElementById("fred").innerHTML='<p><img src="images/blank.gif" width="22" height="20" /></p>';
        	captchaGood=true;
        	//alert('should we submit the form? '+submitOnGoodCAP)
        	if(submitOnGoodCAP){
        		//alert('yes, we need to submit');
        		if(document.all){
        			//alert('ie');
        			document.newform.submit();
        		}else{
        			//alert('else');
        			document.forms.newform.submit();
        		}
        	}
        }
    }
}


function GetXmlHttpObject() {
    var objXMLHttp=null
    if (window.XMLHttpRequest) {
        objXMLHttp=new XMLHttpRequest()
    }
    else if (window.ActiveXObject) {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
    return objXMLHttp
}
  
  
// Information from the email field and the CAPTCHA fields comes here
// and then is sent off to their validation functions, which
// are AJAX requests to verify.asp.
function verify(name) {
    var varTemp = name
    if (varTemp.id == "email") {   
        checkEmailAddress(varTemp);
        if (passed == true) {
            validateEmail(varTemp.value);
        }
    }
    if (varTemp.id == "checktext") { 
    	validateCAP(name.value, document.getElementById("newuserid").value);
    //    document.getElementById("capcha1").innerHTML="<img src='aspcaptcha.asp' alt='PassCode' width=86' 'height='21' />";
    }
}
function disableEnterKey(e)
{
     var key;     
     if(window.event)
	  key = window.event.keyCode; //IE
     else
	  key = e.which; //firefox     

     return (key != 13);
}