// JavaScript Document
function GetLayer( name ){
	if (document.layers) {
		eval("layerObj = document." + name + ";");
	} else if (document.getElementById) {
		eval("layerObj = document.getElementById('" + name + "');");
	} else if (document.all) { 
		eval("layerObj = document.all." + name + ";");
	}
	return layerObj;
}

function WindowOpener( url, windowName, width, height, scrollbars ){
	var windowOp = window.open(url, windowName, "width=" + width + ", height=" + height + ", scrollbars=" + scrollbars + ", status=1, toolbar=0, directories=0, menubar = 0, resizable = 1");
}


// JavaScript Document

var loaderGif = "<img src='/ImageOutput/ajax-loader.gif' border=0 align=absmiddle>";
var loaderGifBlue = "<img src='/ImageOutput/ajax-loader-blue.gif' border=0 align=absmiddle>";
var loaderGif2 = "<img src='/ImageOutput/ajax-loader-2.gif' border=0 align=absmiddle>";
var acceptGif = "<img src='/ImageOutput/accept.gif' border=0 align=absmiddle>";

function AJAXConnect( _url, _post, _async, data, type ) {

	if ( window.XMLHttpRequest ) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();

	} else if ( window.ActiveXObject ) {
		// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}
	xmlhttp.open( ((_post)?"POST":"GET") , _url, _async);
	xmlhttp.send(null);
	xmlhttp.onreadystatechange = function() {
		if( xmlhttp.readyState == 4 ) {
			if( type == "Login" ) {
				LoginFinalize(xmlhttp.responseText );	
			}
			if( type == "ForgotUsername" ) {
				UsernameFinalize(xmlhttp.responseText );
			}
			if( type == "ForgotPassword" ) {
				PasswordFinalize(xmlhttp.responseText );
			}
		}
	}
}
	
function LoginUser() {
	GetLayer("username_div").style.display = "none"
	GetLayer("password_div").style.display = "none"
	if( document.formLogin.username.value == "" ) {
		GetLayer("username_div").style.display = ""
		GetLayer("username_div").innerHTML = "Please enter your username!";
		document.formLogin.username.focus();
	} else if( document.formLogin.password.value == "" ) {
		GetLayer("username_div").innerHTML = "";
		GetLayer("username_div").style.display = "none"
		GetLayer("password_div").style.display = ""
		GetLayer("password_div").innerHTML = "Please enter your password!";
		document.formLogin.password.focus();
	} else {
		var data = "username=" + document.formLogin.username.value + "&password=" + document.formLogin.password.value + "&rnd=" + Math.round(Math.random()*2323980238);
		GetLayer("username_div").style.display = "none";
		GetLayer("password_div").style.display = "none";
		GetLayer("ulloader_div").innerHTML = loaderGif;
		AJAXConnect("/AJAX/Login.asp?" + data, false, true, "", "Login");
	}
	return false;
}



function ForgotUser() {
	GetLayer("usernameEmail_div").style.display = "none"
	if( document.formUsername.usernameEmail.value == "" ) {
		GetLayer("usernameEmail_div").style.display = ""
		GetLayer("usernameEmail_div").innerHTML = "Please enter your email address!";
		document.formUsername.usernameEmail.focus();
	} else if( !ValidateEmail(document.formUsername.usernameEmail.value) ) {
		GetLayer("usernameEmail_div").style.display = ""
		GetLayer("usernameEmail_div").innerHTML = "Please enter a valid email address!";
		document.formUsername.usernameEmail.focus();		
	} else {
		var data = "email=" + document.formUsername.usernameEmail.value + "&rnd=" + Math.round(Math.random()*2323980238);
		GetLayer("usernameEmail_div").style.display = "none";
		GetLayer("usernameEmail_div").style.display = "none";
		GetLayer("fuloader_div").innerHTML = loaderGif;
		AJAXConnect("/AJAX/ForgotUsername.asp?" + data, false, true, "", "ForgotUsername");
	}
	return false;
}


function ForgotPass() {
	GetLayer("passwordEmail_div").style.display = "none"
	if( document.formPassword.passwordEmail.value == "" ) {
		GetLayer("passwordEmail_div").style.display = ""
		GetLayer("passwordEmail_div").innerHTML = "Please enter your email address!";
		document.formPassword.passwordEmail.focus();
	} else if( !ValidateEmail(document.formPassword.passwordEmail.value) ) {
		GetLayer("passwordEmail_div").style.display = ""
		GetLayer("passwordEmail_div").innerHTML = "Please enter a valid email address!";
		document.formPassword.passwordEmail.focus();
	} else {
		var data = "email=" + document.formPassword.passwordEmail.value + "&rnd=" + Math.round(Math.random()*2323980238);
		GetLayer("passwordEmail_div").style.display = "none";
		GetLayer("passwordEmail_div").style.display = "none";
		GetLayer("fploader_div").innerHTML = loaderGif;
		AJAXConnect("/AJAX/ForgotPassword.asp?" + data, false, true, "", "ForgotPassword");
	}
	return false;
}
	
function LoginFinalize(returnData){
	GetLayer("ulloader_div").innerHTML = "";
	if( returnData == 1) {
/*		if( document.formLogin.returnURL.value != "" && String(document.formLogin.returnURL.value).indexOf("self.location")  > -1  ) 
		else self.location = "/Index.asp";*/
		self.location = "hashRedirect.asp?url=" + escape(location.href);
	} else {
		GetLayer("error_div").innerHTML = "<img src='/ImageOutput/error.png' align=absmiddle> Invalid username or password";
		document.formLogin.password.value = document.formLogin.username.value = "";
	}	
}

function UsernameFinalize(returnData){
	GetLayer("fuloader_div").innerHTML = "";
	if( returnData != 0) {
		GetLayer("ForgotUsernameFormDiv").style.display = 'none';
		GetLayer("ForgotUsernameFormSuccess").style.display = '';
		GetLayer("ForgotUsernameFormSuccess").innerHTML = "<img src=\'/ImageOutput/success.png\' align=absmiddle> Dear " + returnData + ", <br><br>Please check your inbox, your username has been sent to your email address and after getting back <a href=\'javascript: Login(1);\'>click here</a> to login.";
	} else {
		GetLayer("fuerror_div").innerHTML = "<img src='/ImageOutput/error.png' align=absmiddle> The email address doesn't exist in our database";
		document.formLogin.username.focus();
	}	
}

var errorPNG = "<img src='/ImageOutput/error.png' align=absmiddle>";
var successPNG = "<img src='/ImageOutput/success.png' align=absmiddle>";

function PasswordFinalize(returnData){
	GetLayer("fploader_div").innerHTML = "";
	if( returnData != 0) {
		GetLayer("ForgotPasswordFormDiv").style.display = 'none';
		GetLayer("ForgotPasswordFormSuccess").style.display = '';
		GetLayer("ForgotPasswordFormSuccess").innerHTML = "<img src=\'/ImageOutput/success.png\' align=absmiddle> Dear " + returnData + ", <br><br>Please check your inbox, your password has been sent to your email address and after getting back <a href=\'javascript: Login(1);\'>click here</a> to login.";
	} else {
		GetLayer("fperror_div").innerHTML = "<img src='/ImageOutput/error.png' align=absmiddle> The email address doesn't exist in our database";
		document.formLogin.username.focus();
	}	
}


function ValidateEmail(str){
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return emailRegEx.test(str);
}

function ValidateUser(str){
	var emailRegEx = /^\w+$/;
	return emailRegEx.test(str);
}

function ValidateDomain(str){
	var emailRegEx = /^([a-z][a-z0-9\-]+(\.|\-*\.))+[a-z]{2,6}$/;
	return emailRegEx.test(str);
}


function ValidDate(str){
	var emailRegEx = /(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d/;
	return emailRegEx.test(str);
}


