function updateTopBarLoginMsg() {
	$.post("/secure/ajaxLogin.php",{ step: 'getLoginMessage' }, function(xml) {
		statusmsg = $("text",xml).text();
		$("#wrpAjaxMessageSection").html( statusmsg );
	});
}
function getLoginStatusLooper(sBaseUrl, sCalledFrom) {
	sGlobalBaseUrl			= sBaseUrl;
	sGlobalCalledFromUrl	= escape(sCalledFrom);
	getLoginStatus();
	var check = setTimeout('getLoginStatus()',60000);
}
function updateTopBars() {
	$.post("/secure/ajaxLogin.php",{ step: 'getLoginMessage' }, function(xml) {
		//alertXml(xml);//return(false);
		var status		= $('status',xml).text();
		var statusmsg	= $('text',xml).text();
		var miscmsg		= $('misc',xml).text();
		updateNavLoginSections(status, statusmsg, miscmsg);
	});
	var check = setTimeout('updateTopBars()',600000);
}
function setDivLoading(p_sDivId) {
	$("#"+p_sDivId).html( '<span style="color:#444444;"><img src="/images/loading.gif">&nbsp;&nbsp;Loading...</span>' );
}

function updateNavLoginSections(status, statusmsg, miscmsg) {
	if(status == "1" ) {
		$("#wrpAjaxLoginForm").hide();

		var array1		= statusmsg.split("\|\|\|");
		var welcomemsg	= array1[0];
		var loginmsg	= array1[1];

		$("#wrpAjaxLoginInfo").show();
		$("#wrpAjaxLoginInfo").html( loginmsg );
		$("#wrpAjaxLoginWelcome").html( welcomemsg );

		if (miscmsg.length) {
			$("#wrpAjaxMessageSection").show();
			$("#wrpAjaxMessageSection").html( miscmsg );
		} else {
			$("#wrpAjaxMessageSection").hide();
		}
	} else if(status == "0" ) {
		$("#wrpAjaxLoginForm").show();

		$("#wrpAjaxLoginInfo").hide();
		$("#wrpAjaxLoginInfo").html( statusmsg );

		$("#wrpAjaxMessageSection").hide();
		//$("#wrpAjaxMessageSection").show();
		//$("#wrpAjaxMessageSection").html( 'Please sign in by filling out the top left form.' );
	}
}

function setInit() {
	$("form#frmNavLogin").submit(function(){
		//alert('frmNavLogin called');
		//setDivLoading('wrpAjaxMessageSection');

		var keepval = ($("#keep").is(":checked")) ? '1' : '0';

		$.get("/secure/ajaxLogin.php",{step: 'executeLogin',username: $("#username").val(),password: $("#password").val(),loginmode: $("#loginmode").val(), keep: keepval},function(xml) {
			//alertXml(xml);
			var status		= $("status",xml).text();
			var statusmsg	= $("text",xml).text();
			var miscmsg		= $('misc',xml).text();
			var debugmsg	= $('debug',xml).text();

			//alert('DEBUG: ' + debugmsg);

			//alert(xml);
			//return(false);

			if ( status == "1" ) {
				if($("softrefresh",xml).text() == "1" ) {
					//alert('softrefresh');
					window.location.reload(true); return(false);
				}
				if($("hardrefresh",xml).text() == "1" ) {
					//alert('hardrefresh');
					window.location.reload(true); return(false);
				}
				if($("custrefresh",xml).text() == "1" ) {
					//alert('custrefresh');
		            //alert('REF: ' + do_refresh);
		            var m_sUrl = window.location.href;
		            var m_sUrl = m_sUrl.toLowerCase();
		            if ( m_sUrl.indexOf("users.php") < 0 ) { // only if the user isnt here already
		                if ( m_sUrl.indexOf("www.thorograph.com") >= 0 ) {
		                    window.location.href = 'https://www.thorograph.com/secure/users.php?step=edit&ccreq=1';
		                } else {
		                    window.location.href = 'http://beta.thorograph.com/secure/users.php?step=edit&ccreq=1';
		                }
		            }
				}
				updateNavLoginSections(status, statusmsg, miscmsg);
			} else {
				alert('Invalid login and password combination.  Please try again.');
			}
		});
		return false;
	});
}

function setLoginModeNormal() {
	$("#loginmode").val('normal');
}

/*
function checkPasswordRetrievalForm(){
	$("#loginmode").val('password');
	if ( $("#username").val() == 'username' || $("#username").val() == '' ) {
		alert('Please provide your username in the top right form');
	} else {
		$("form#frmNavLogin").submit();
	}
}
*/

function checkLoginForm(){
	if ( $("#username").val() == 'username' ) {
		$("#username").val('');
		$("#password").val('');
	}
}

function alertXml(xmlData) {
	if (window.ActiveXObject) {
		var string = xmlData.xml;
	} else {
		var string = (new XMLSerializer()).serializeToString(xmlData);
	}
	alert(string);
}

/*
var iGlobalCustomerID=0;
var sGlobalBaseUrl			= '';
var sGlobalCalledFromUrl	='';
var bIsLoggedIn				= false;
var bLastReportedLoggedIn	= false;

function getLoginStatusLooper(sBaseUrl, sCalledFrom) {
	sGlobalBaseUrl			= sBaseUrl;
	sGlobalCalledFromUrl	= escape(sCalledFrom);
	getLoginStatus();
	var check = setTimeout('getLoginStatus()',60000);
}

function getLoginStatus() {
	var sUrl	= sGlobalBaseUrl+'?step=getLoginStatus&from='+sGlobalCalledFromUrl;
	//alert('making request');
	makeRequestThreaded(sUrl,'set_login_status');
}

function set_login_status(rawdata) {
	//var rawdata = handleRequest();
	if ( rawdata != undefined ) {
		if ( rawdata == "" ) {
			return(false);
		} else {
			//alert('handling request: '+rawdata);
			var array1		= rawdata.split("\|\|\|");
			var result		= array1[0];
			var msg			= array1[1];
			var alert_msg	= array1[2];
			var do_refresh	= array1[3];

			if ( result != '0' ) {
                //alert('REF: ' + do_refresh);
                if ( do_refresh == '9' ) {
                    var m_sUrl = window.location.href;
                    var m_sUrl = m_sUrl.toLowerCase();
                    if ( m_sUrl.indexOf("users.php") < 0 ) { // only if the user isnt here already
                        if ( m_sUrl.indexOf("www.thorograph.com") >= 0 ) {
                            window.location.href = 'https://www.thorograph.com/secure/users.php?step=edit&ccreq=1';
                        } else {
                            window.location.href = 'http://dev.thorograph.com/secure/users.php?step=edit&ccreq=1';
                        }
                    }
                }
                if ( do_refresh == '1' ) {
                    window.location.reload(true);
                }
				setLoggedInStatus(true);
				if ( document.getElementById( 'wrpAjaxMessageSection' ) ) document.getElementById( 'wrpAjaxMessageSection' ).innerHTML = msg;
				if ( document.getElementById( 'wrpAjaxLoginSection' ) ) document.getElementById( 'wrpAjaxLoginSection' ).style.visibility = 'hidden';
				ajaxCallAfterSuccessLogin();
			} else {
				setLoggedInStatus(false);
				if ( document.getElementById( 'wrpAjaxMessageSection' ) ) document.getElementById( 'wrpAjaxMessageSection' ).innerHTML = '<span style="font-size:8pt;font-face:tahoma;">Please sign in below</span>';
				if ( document.getElementById( 'wrpAjaxLoginSection' ) ) document.getElementById( 'wrpAjaxLoginSection' ).innerHTML = msg;
				if ( document.getElementById( 'wrpAjaxLoginLoading' ) ) document.getElementById( 'wrpAjaxLoginLoading' ).innerHTML = '<img id="ajax" name="ajax" src="/images/space.gif" width="10" height="10" hspace="10" border="0">';
				ajaxCallAfterSuccessLogout();
			}
		}
	}
}

function logout(sBaseUrl) {
	sGlobalBaseUrl = sBaseUrl;
	setLoggedInStatus(false);
	var sUrl	= sBaseUrl+'?step=executeLogout&from='+sGlobalCalledFromUrl;
	//alert(sUrl);
	makeRequestThreaded(sUrl,'login_results');
}

function login(sBaseUrl) {
	sGlobalBaseUrl = sBaseUrl;
	var login	= document.getElementById('username').value;
	var pwd		= document.getElementById('password').value;
	var keep	= '0';

	if ( document.getElementById('cbKeepLoggedIn') ) {
		if ( document.getElementById('cbKeepLoggedIn').checked )
			keep = '1';
	}

	if ( login != "" && pwd != "" ) {
		var sUrl	= sBaseUrl+'?step=executeLogin&vcLogin='+login+'&vcPassword='+pwd+'&keep='+keep+'&from='+sGlobalCalledFromUrl;
		if ( document.getElementById( 'wrpAjaxLoginLoading' ) )
			document.getElementById( 'wrpAjaxLoginLoading' ).innerHTML = '<img id="ajax" name="ajax" src="/images/ajax.gif" width="10" height="10"  hspace="10" border="0">';
		makeRequestThreaded(sUrl,'login_results');
	} else {
		alert('Please provide a username and password');
		return(false);
	}
}
function login_results(rawdata) {
	//var rawdata = handleRequest();
	if ( rawdata != undefined ) {
		if ( rawdata == "" ) {
			return(false);
		} else {
			var array1 = rawdata.split("\|\|\|");
			var result			= array1[0];
			var msg				= array1[1];
			var alert_msg		= array1[2];
			var do_refresh		= array1[3];
			iGlobalCustomerID	= result;
			if ( alert_msg ) {
				alert(alert_msg);
			}
			if ( do_refresh == '1' ) {
				window.location.reload(true);
			} else if ( do_refresh == '2' ) {
				document.location=document.location;
			}

			//alert(msg.match( new RegExp( "<script\\s+?type=['\"]text/javascript['\"]>(.+?)</script>", "i" ) ));
			if ( result != '0' ) {
				if ( document.getElementById( 'wrpAjaxMessageSection' ) )
					document.getElementById( 'wrpAjaxMessageSection' ).innerHTML = msg;

				if ( document.getElementById( 'wrpAjaxLoginLoading' ) )
					document.getElementById( 'wrpAjaxLoginLoading' ).innerHTML = '<img id="ajax" name="ajax" src="/images/space.gif" width="10" height="10" hspace="10" border="0">';

				// clear otu the login piece
				if ( document.getElementById( 'wrpAjaxLoginSection' ) ) {
					document.getElementById( 'wrpAjaxLoginSection' ).style.visibility = 'hidden';
					document.getElementById( 'wrpAjaxLoginSection' ).style.display = 'none';
				}

				// ensure no duplicate calls
				setLoggedInStatus(true);
				if ( bLastReportedLoggedIn != true ) { ajaxCallAfterSuccessLogin(); }
			} else {
				if ( document.getElementById( 'wrpAjaxLoginSection' ) ) {
					document.getElementById( 'wrpAjaxLoginSection' ).innerHTML = msg;
					document.getElementById( 'wrpAjaxLoginSection' ).style.visibility = 'visible';
					document.getElementById( 'wrpAjaxLoginSection' ).style.display = 'block';
				}
				if ( document.getElementById( 'wrpAjaxMessageSection' ) )
					document.getElementById( 'wrpAjaxMessageSection' ).innerHTML = '<span style="font-size:8pt;font-face:tahoma;">Please sign in below</span>';

				// ensure no duplicate calls
				setLoggedInStatus(false);
				if ( bLastReportedLoggedIn != true ) { ajaxCallAfterSuccessLogout(); }
			}
			//alert(rawdata);
		}
	}
}
function setLoggedInStatus(status) {
	bIsLoggedIn = status;
}
function execLoginFormEnterKey(myform, myfield, e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
    {
       var m_sHost = window.location.hostname;
       if ( m_sHost.indexOf('www.thorograph.com') !=-1 ) {
            login('https://'+m_sHost+'/secure/ajaxLogin.php');
       } else {
            login('http://'+m_sHost+'/secure/ajaxLogin.php');
       }
       return false;
    } else return true;
}
*/