// Check if logged in
function checkLogin() {
	var logged_in = readCookie('login')
    if (! logged_in) {
        showWarning();	
    } 
}

function showWarning() { 
     $('#dialog').jqm({modal:true, overlay: 87, toTop:true});  
	 $('#dialog').jqmShow();
}

//Cookie functions from Peter Paul Koch http://www.quirksmode.org/js/cookies.html */
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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// Validation functions
function validateForm() { 
var errors = '';
var submitFlag = true;

	if (!valPassword())
	{
		errors += 'Invalid Member ID Number \n';
	} 
	
    if (errors) 
	{
		alert('The following error(s) occurred:\n'+errors);
    	submitFlag = false;
    }
	
	if (submitFlag) {
		if (document.getElementById('auto_login').checked) {
			//save cookie for 1 year
			createCookie('login','true',183);
		} else {
			//save cookie until browser is closed
		    createCookie('login','true',0);
		}
	    $('#dialog').jqmHide();

	} else {
		document.getElementById('password').value = '';
		document.getElementById('password').focus();
	}
 }

function valPassword() {
    var pass = document.frmLogin.password.value;
	if (isNaN(pass) || pass.length != 9 || pass < 0) {
		return false;
	} else {
		return true;
	}
}

//adds functions to the windows onload event
function addOnload(newFunction) {
    var oldOnload = window.onload;
    // does window.onload exist
    if (typeof oldOnload == "function") {
        //overwriting existing onload
        window.onload = function() {
        if (oldOnload) {oldOnload();}
           newFunction();
        }
    } else {
        window.onload = newFunction;
    }
} 

addOnload(checkLogin);