/*

	$Id: 2way-login.js,v 1.3 2006/07/19 17:20:11 scottd Exp $

*/



var cookieName = "slwc-auth-cookie";
var MSG_LOGON_ERROR = "";


function slwc_processHandlerLogon () {
	/* onreadystate change function for login handler */
	/* this function is designed to work with a loginForm, or not
	   incase it's being used naked via a call to slwc_logonUsr */

	var statusOut = document.getElementById ( "requestStatus" );
	var _status = "";
	switch ( xmlhttp.readyState ) {
		case 4:
			if ( xmlhttp.status == "200") {
				// reload the document to load the next step.
				
				xml = xmlhttp.responseXML;
				if ( xml.getElementsByTagName("redirect")[0] ) {
					// need to redirect!
					document.location.href = slwc_getXMLNodeValue (  xml.getElementsByTagName("redirect")[0] );
					return;
				}
				if ( xml.getElementsByTagName("alias")[0] || xml.getElementsByTagName("destaddress")[0] ) { 
					_status = "OK";
				}
				else if ( xml.getElementsByTagName("errormsg")[0] ) {
					_status = "ERROR";
					
					MSG_LOGON_ERROR = slwc_getXMLNodeValue ( xml.getElementsByTagName("errormsg")[0] );	
					
				}
				else
					_status = "INVALID";

				statusOut.innerHTML = eval ( "MSG_LOGON_" + _status );;

				if ( _status == "OK" ) {
					// set the logon remember me cookie if requested.
					if ( document.getElementById ( "slwc_logonForm" ) ) {
						if ( document.getElementById ( "slwc_logonForm" ).elements["__LRM"].checked )
							slwc_setRmCookie ( document.getElementById ( "slwc_logonForm" ).elements["__LID"].value, 
											document.getElementById ( "slwc_logonForm" ).elements["__LPW"].value );
						document.location.href = nextUrl;
						//document.location.reload();
					}	

				}
				else {
					if ( document.getElementById ( "slwc_logonForm" ) ) {
						document.getElementById ("slwc_logonForm").reset();
						document.getElementById ("slwc_logonForm").elements["__LID"].focus();
						setTimeout ( "document.getElementById('requestStatus').innerHTML=MSG_WAITINGRESPONSE;document.getElementById('requestStatus').style.display='none'", 4000 ) ;
					}
				}

				if ( xmlhttp.nextFunction )
                    xmlhttp.nextFunction();
				
			}
			else {
				switch ( xmlhttp.status.toString() ) {
					case "0":		//this doesn't work!  IE is f*&ced....
					case "302":
						var cookies = xmlhttp.getResponseHeader ( "Set-Cookie" ).split ( ";" );
						var c = 0;
						var cookie, data;
						while ( cookie = cookies[c] ) {
							data = cookie.split ( "=" );
							if ( data[0] == "redirect" ) {
								document.location.href = SERVER_URL + data[1];
								break;
							}
							c++;
						} 
						break;
					case "404":
						statusOut.innerHTML = ERROR_RPC_HTTP + " - " + ERROR_RPC_HTTP_404
							+ "<br>" + ERROR_USR_INSTR;
						break;
					default:
						statusOut.innerHTML = ERROR_RPC_HTTP + " - " + xmlhttp.status
							+ "<br>" + ERROR_USR_INSTR;
						break;
				}
				return;
			}
			break;
		case 3:
			statusOut.innerHTML = MSG_WAITINGRESPONSE;	
			break;
		default:
			break;
	}
}

function slwc_logonUsr ( strPostData, fnNextFunction ) {
	if ( strPostData ) {
		xmlhttp.open ( "POST", RPC_URL, true );
        xmlhttp.setRequestHeader ( "Content-Type", "application/x-www-form-urlencoded" );
        xmlhttp.onreadystatechange = slwc_processHandlerLogon;
		if ( fnNextFunction )
			xmlhttp.nextFunction = fnNextFunction;

        xmlhttp.send( strPostData );
	}
}

function slwc_onLogonSubmit( logonForm ) {
	if ( slwc_validateCreds ( logonForm ) ) {

		document.getElementById ( "requestStatus" ).style.display = "block";
		slwc_logonUsr ( slwc_makePostString ( logonForm ) );

		return false;				
	}
	//alert ( "returning" );
	return false;
}

function slwc_deleteRmCookie ( cBox ) {
	if ( !cBox.checked ) {
		del_cookie ( cookieName, "", 0 );
	}
}

function slwc_setRmCookie ( logonId, passwd ) {
	var _cookieDomain = false;
	
	// see if we have an override set for cookie domain.
	// if we do, we need to set it on the call to set_cookie
	// otherwise the default (page.ca) will be used
	try {
		if ( COOKIE_DOMAIN ) {
			_cookieDomain = COOKIE_DOMAIN;
		}
	}
	catch ( e ) {

	}
	set_cookie ( cookieName, "__LID=" + logonId,  
			expiry_date ( 365 ), 
			false, 
			( _cookieDomain ? _cookieDomain : false ) );
}

