// JavaScript Document

function authorLogin(type){
	//Make the XMLHttpRequest Object
	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null){
		alert ("Your browser does not support AJAX!");
		return;
	}
	
	password = document.loginform.password.value;
	
	if(type == "login"){
		username = document.loginform.username.value;
		url="/includes/author-login-code.php";
		params = "username="+username+"&password="+password;
	}
	else if(type == "forgotpass"){
		username = document.forgotpassform.username.value;
		url="/includes/author-forgotpass-code.php";
		params = "username="+username;
	}
	
	if(type == "login"){ document.getElementById('logging-in').innerHTML = "<div class=\"login-loader\"><p><img src=\"/img/login_text.gif\" width=\"92\" height=\"5\"><br><img src=\"/img/login_loader.gif\" width=\"220\" height=\"19\"></p></div>"; }
	if(type == "forgotpass"){ document.getElementById('logging-in').innerHTML = "<div class=\"login-loader\"><p><img src=\"/img/login_forgotpass.gif\"><br><img src=\"/img/login_loader.gif\" width=\"220\" height=\"19\"></p></div>"; }
	
	xmlHttp.onreadystatechange = handleLoginResponse;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
}

function handleLoginResponse(type){
	document.getElementById('logging-in').style.display = "block";
	document.getElementById('login-errors').style.display = "none";
	document.getElementById('login-form').style.display = "none";
	document.getElementById('login-forgotpass').style.display = "none";
	if(xmlHttp.readyState == 4){
		response = xmlHttp.responseText;
		if(response){
			if(response == "success"){ window.location = "/author/admin/index.php"; }
			else if(response == "forgotpass"){ window.location = "/author/"; }
			else{
				responsearr = response.split("|");
				errors = responsearr[0];
				errortext = responsearr[1];
				if(errortext == "" && type == "login"){ errortext = "* There was an error and you could not be logged in, please email <a href=\"#\" onclick=\"insertmailto('info','nzs.com','','');\">info@nzs.com</a> stating this error."; }
				if(errortext == "" && type == "forgotpass"){ errortext = "* There was an error and your password could not be reset, please email <a href=\"#\" onclick=\"insertmailto('info','nzs.com','','');\">info@nzs.com</a> stating this error."; }
				
				document.getElementById('login-errors').innerHTML = "<p><strong>" + errors + " error(s) found</strong><br>" + errortext + "<br></p><p><a href=\"javascript:logindiv('login-form','login-errors');\">Click here</a> to try again</p>";
				document.getElementById('login-errors').style.display = "block";
				document.getElementById('logging-in').style.display = "none";
				document.getElementById('login-form').style.display = "none";
				document.getElementById('login-forgotpass').style.display = "none";
			}
		}
	}
}

function GetXmlHttpObject(){
	var xmlHttp = null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
			try{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

/***************************************************************************************************
This function shows or hide the login divs
*****************************************************************************************************/
function logindiv(showdiv,hidediv){
	targetshow = document.getElementById(showdiv);
	targethide = document.getElementById(hidediv);
	targetshow.style.display = "block";
	targethide.style.display = "none";
}