var http = getHTTPObject();

function submitLogin(){
    //alert('here');
    var username = document.forms[0].user.value;
    var password = document.forms[0].pass.value;
    http.open('get', 'auth/authenticate.html',true,username,password);
    http.onreadystatechange = getAuthResult;
    http.send(null);
}

function getAuthResult() {
    if (http.readyState==4){
	//alert(http.status);
        var messageText=document.getElementById('message');
        if (http.status == 200) {
            messageText.innerText='Login Accepted!';
            createCookie('supportAuth',document.forms[0].user.value,0);
            if(destPage) location = destPage;
			else location = getParameter(location.search,'destination');
        } else {
            messageText.innerText='Invalid Login!';
        }
        return false;
    }
}

function getParameter ( queryString, parameterName ) { 
    // Add "=" to the parameter name (i.e. parameterName=value)
    if ( queryString.length > 0 ) {
        // Find the beginning of the string
        var begin = queryString.indexOf ( parameterName+'=' );
        // If the parameter name is not found, skip it, otherwise return the value
        if ( begin != -1 ) {
            // Add the length (integer) to the beginning
            begin += parameterName.length + 1;
            // Multiple parameters are separated by the "&" sign
            end = queryString.indexOf ( "&" , begin );
            if ( end == -1 ) {
                end = queryString.length;
            }
            // Return the string
            return unescape ( queryString.substring ( begin, end ) );
        }
        // Return LANDING if no parameter has been found
     }
     return 'support.php4';
}

function getHTTPObject() {
    if (typeof XMLHttpRequest != 'undefined') {
        return new XMLHttpRequest();
    }
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
    return false;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
