var xmlHttp = false;
var result = "";

function PreAuth() {
	if (!runSubmit(document.Login, document.btnSubmit)) {
		return false;
	}
		
	if (isNaN(document.Login.userNumber.value)) {	    
		top.location = "/BASE/LIVE/LoginError.aspx?msg=INVALID";
		return false;
	}
	
    //window.location = "pleasewait.htm";
	newXMLConnection = getXmlHttpRequestObject();
	if (newXMLConnection != null) {
		try {
			newXMLConnection.open("POST", "/BASE/LIVE/CUIPW_SVC.asmx/Logon", false);
			newXMLConnection.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			
			newXMLConnection.send("an=" + document.Login.userNumber.value + "&pw=" + encodeDec(document.Login.password.value));
			//document.textForm.TextArea1.value = newXMLConnection.responseText;
			xmlResult = ParseXML(newXMLConnection.responseText);
			result = xmlResult.getElementsByTagName("string")[0].childNodes[0].nodeValue;
		}
		catch (e) {
			result = e.message;
		}
	}
	else {
		result = "NULL CONNECTION";
	}
	if (result == "INVALID" || result == "FROZEN") {
		top.location = "/BASE/LIVE/LoginError.aspx?msg=" + result;
		return false;
	}
	else {
		signOnTimer();
		document.Login.submit();
		return true;
	}
}   

function getXmlHttpRequestObject() {
	// check for native XMLHttpRequest object
	if (window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			xmlHttp = new XMLHttpRequest();
		}
		catch (e) {
			xmlHttp = false;
		}
	}

	// check for IE/Windows ActiveX version
	else if (window.ActiveXObject) {
	    try {
		    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch (e) {
		    try {
    			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    		}
    		catch (e) {
    			xmlHttp = false;
    		}
    	}
    }
    return xmlHttp;
}

function ParseXML(text) {
	if (window.DOMParser) {
		parser = new DOMParser();
		xmlDoc = parser.parseFromString(text, "text/xml");
	}
	else // Internet Explorer
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.loadXML(text);
	}
	return xmlDoc;
}

function encodeDec(str) {
    var result = "";
    for (var i = 0; i < str.length; i++) {
        if (i>0) {
            result += "-";
        }
        result += str.charCodeAt(i);
    }
    return result;
}

