/**
 Function sets login form hidden variable values such as the user id and password and submits the form
 **/
var userLoggedIn = false;
var loginRefresh = false;
var menuLinkUrlContext = null; // to retain correct links need to provide url context

var loggedInStatusOnLoad = window.onload;
window.onload = function(){
    getStatus();

    if (loggedInStatusOnLoad!=null){ // if previous onload event was set, call it here.
        loggedInStatusOnLoad();
    }
}

function getStatus(){
	callGetJSON(new Array(), (menuLinkUrlContext?menuLinkUrlContext:'') + "GetStatus",function(o,t,e){
        //alert("status done");
		if ( o != null ){
            if ( o.name.length > 0 ){
                userLoggedIn = true;
                loadMemberMenu(o.name,o.permissionList);
                im_load_menu();
            }
            else { // if user is not logged in, create seed and load the login screen.
				var _href = window.location.href;
                if ( _href.indexOf('index.html') != -1 || _href.charAt(_href.length-1) == '/'){
                    loadCounter();
                }

                loadLoginMenu();
                $('seed').value=o.seed;
            }
        }
		else {
			alert("bla");	
		}
    });
}

function login() {
    var uid = $("inputuid").value;
    var pwd = $("inputpwd").value;
    var seed = $("seed").value;    

    if ( uid.length == 0 || pwd.length == 0 ) return;

    persistUid(); // save uid into a cookie

    var paramList = new Array();
    paramList[0] = new RequestParam("uid",uid);
    paramList[1] = new RequestParam("uids",SHA1(SHA1(pwd) + trim(seed)));
    paramList[2] = new RequestParam("seed", seed);

    callGetJSON(paramList,(menuLinkUrlContext?menuLinkUrlContext:'') +"Login",function(o,t,e){
        if ( o != null ){
            if ( o.error && !o.critical ){
                if ( window.location.href.indexOf('login.html')>0){
                    $('errorspan').innerHTML = o.error;
                    $("seed").value = o.seed; // update the seed
                    $('inputpwd').value = ''; // clear pwd field
                }
                else {
                    window.location = 'login.html?error=' + o.error;
                }
            }
            else {  // login successful. if login in from login.html simply go to index.html
                if ( window.location.href.indexOf('login.html')>0){
                    var redirect = getUrlParameter("redirect");
                    // if redirect param is specified, redirect to that page
                    if ( trim(redirect).length > 0  ) {
                        window.location = redirect;
                    }
                    else {
                        window.location = 'index.html';
                    }
                }
                else if ( loginRefresh ){ // instead of inserting a callback, simply put in a refresh functionality
                    window.location = window.location.href;
                }
                else { // load member menu
                    loadMemberMenu(o.name, o.permissionList);
                    im_load_menu();
                }
            }
        }
    });
}

function persistUid(){
    var expdate = new Date();
    FixCookieDate(expdate);
    // Correct for Mac date bug - call only once for given Date object!
    expdate.setTime(expdate.getTime() + (24 * 7 * 60 * 60 * 1000));
    // 24*7 hrs from now or a week
    var inputtxtbox = $("inputuid");
    if (inputtxtbox != null) {
        SetCookie("inputuid", inputtxtbox.value, expdate); //,null,"www.hvfire.org",true);
    }
}

/**
    Submits a form based on a keydown event of an "enter" key. Works
    for both IE and Netscape
**/
function keyDown(event) {
    var isNS4 = (navigator.appName=="Netscape")?1:0;
    var e = event ? event : window.event;
    var key = isNS4 ? e.which : e.keyCode;
    var id = e.target ? e.target.id : e.srcElement.id;

    if ( key == 13 ){
        if ( id == "loginsubmittd" || id == "inputpwd"){
            // first clear the errors if any from errorspan element ( if found ), then login
            var errspan = $('errorspan');
            if ( errspan != null ) errspan.innerHTML = '&nbsp;';
            login();
        }
    }
    return true;
}

// set onkeydown to invoke a isKeydown function
document.onkeydown = keyDown;
// for netscape browsers
if (document.layers) {
    document.captureEvents(Event.KEYPRESS);
}