/*
function not_lost_pass(){
	var urlstr="/leftbar/leftbar.php?m=not_lost&";


	var request = getAjaxHandle();
	request.open("GET", urlstr , true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var returnAble = request.responseText;
			document.getElementById("leftmenu_User").innerHTML = returnAble;
		}
	};
	request.send(null);
}

function lost_pass(){
	var urlstr="/leftbar/leftbar.php?m=lost&";
	
	
	var request = getAjaxHandle();
	request.open("GET", urlstr , true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var returnAble = request.responseText;
			document.getElementById("leftmenu_User").innerHTML = returnAble;
		}
	};
	request.send(null);
}

function retrieve_pass(){
	var url="/leftbar/leftbar.php?m=retrieve&";
	var urlstr = "e=" + document.getElementById("leftbar_user_loginEmail").value + "&l=" + document.getElementById("leftbar_user_loginID").value;
	
	var request = getAjaxHandle();
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			var returnAble = request.responseText;
			document.getElementById("leftmenu_User").innerHTML = returnAble;
		}
	};
	request.open("POST", url , true);
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", urlstr.length);
 	request.setRequestHeader("Connection", "close");
	request.send(urlstr);

}*/

var timer = null;

function login(){ POST("loginform", "/user/user.php?a=login", process_login_response); }
function logout(){ s_POST("", "/user/user.php?a=logout", process_logout_response); }
function createAccount(){ POST("newaccForm", "/user/user.php?a=create-account", process_account_creation_response); }
function mhmredraw() { h_POST("", "/draw.php?draw=mh", redrawMastheadmenu); }

function redrawMastheadmenu(inc) {
	var masthead = getObj("mastheadmenu");
	masthead.innerHTML = inc;
}

function closeMHMessage() {
	var mhm = getObj("mhmessage");
	mhm.parentNode.removeChild(mhm);
}

function process_login_response(inc){
	var string = new String(inc);
	tokens  = string.split(":");
	switch(tokens[1]){
	case "0":
		alert(tokens[2]);
		window.location.assign("/index.php");
		break;
	case "1":
		closeModal('login');
		mhmredraw();
		//set the message, add links, hide modals if required, and open the message
		var h = "Logged in!";
		var b = "You've been successfully logged in and we're forwarding you to your dashboard!";
		var i = "/ndv9/icon-48_settings.png";
		var lA = new Array();
		lA[0] = new Array();
		lA[0].push("closethismessage");
		lA[0].push("Close this message");
		lA[0].push("");
		lA[0].push("clearTimeout(timer);closeMHMessage()");
		lA[1] = new Array();
		lA[1].push("gotothedashboard");
		lA[1].push("Go to my dashboard");
		lA[1].push("/index.php?m=dashboard");
		lA[1].push("clearTimeout(timer);window.location.assign('/index.php?m=dashboard')");
		var message = new MHMessage(h, b, i, lA);
		setTimer("gotothedashboard", 10);
		break;
	case "2":
	case "3":
		alert(tokens[2]);
		break;
	}
}

function setTimer(link, time) {
	if (link == null) return;
	linkobj = getObj(link);
	var span = linkobj.getElementsByTagName("span");
	if(span.length > 0)
		span = span[0];
	else {
		span = document.createElement("span");
		linkobj.appendChild(span);
	}
	if (time <= 0) {
		eval(linkobj.getAttribute("onclick"));
		return;
	}
	span.innerHTML = " (" + time + ")";
	timer = setTimeout("setTimer('" + link + "', " + (time-1) + ")", 1000);
}

function process_logout_response(inc){
	var string = new String(inc);
	tokens  = string.split(":");
	alert(tokens[2]);
	window.location.assign("/index.php");
}

function process_account_creation_response(inc){
	var string = new String(inc);
	tokens  = string.split(":");
	var error = getObj("naError");
	switch(tokens[1]){
		case "5": break;
		case "20"://email bad
			error.innerHTML = "<img src='/images/warning.png'/><p>The email address you have supplied is not in the correct format.  Please try again or contact beta@newdigs.com for support.</p>";
			error.style.display = "block";
			break;
		case "21"://pass bad
			error.innerHTML = "<img src='/images/warning.png'/><p>The passwords you have supplied do not match.  Please try again.</p>";
			error.style.display = "block";
			break;
		case "22"://pass and email bad
			error.innerHTML = "<img src='/images/warning.png'/><p>The email address you have supplied is not in the correct format and your passwords do not match.  Please try again.</p>";
			error.style.display = "block";
			break;
		case "23"://Bad User/Group Name
			error.innerHTML = "<img src='/images/warning.png'/><p>That account name has already been taken.</p>";
			error.style.display = "block";
			break;
		case "24"://Other
			error.innerHTML = "";
			error.style.display = "block";
		default:
			alert(tokens[2]);
			window.location.assign("/index.php");
			break;
	}
	
	
}

function MHMessage (heading, msg, img, linkArray) {
	if (!getObj("mhmessage")) {
		this.body = document.createElement("div");
		this.body.id = "mhmessage";
		this.body.style.height = "0px";
		getObj("masthead").appendChild(this.body);
	} else {
		this.body = getObj("mhmessage");
		this.body.style.height = "0px";
	}
	
	this.MHiD = document.createElement("div");
	this.MHiD.id = "MHiD";
	this.MHbD = document.createElement("div");
	this.MHbD.id = "MHbD";
	this.MHlD = document.createElement("div");
	this.MHlD.id = "MHlD";
	
	this.body.innerHTML = "";
	this.body.appendChild(this.MHiD);
	this.body.appendChild(this.MHbD);
	var cleaner = document.createElement("div");
	cleaner.className = "cleaner";
	cleaner.innerHTML = "&nbsp;";
	this.body.appendChild(cleaner);
	
	if (heading!=null) this.setHeading(heading);
	if (msg!=null) this.setMessage(msg);
	if (img!=null) this.setImage(img);
	this.MHbD.appendChild(this.MHlD);
	if(linkArray!=null && linkArray.length > 0) { 
		for (var x = 0; x < linkArray.length; x++) { this.addLink(linkArray[x]); }
	}
	this.body.style.height = "auto";
	return this.body;
}

MHMessage.prototype.setHeading = function(inc) {
	if (this.h == undefined || this.h == null) {
		this.h = document.createElement("h1");
		this.MHbD.appendChild(this.h);
	}
	this.h.innerHTML = inc;
};

MHMessage.prototype.setMessage = function(inc) {
	if (this.m == undefined || this.m == null) { 
		this.m = document.createElement("p");
		this.MHbD.appendChild(this.m);
	}
	this.m.innerHTML = inc;
};

MHMessage.prototype.setImage = function(inc, alt) {
	if (this.i == undefined || this.i == null) { 
		this.i = document.createElement("img");
		this.MHiD.appendChild(this.i);
	}
	this.i.setAttribute("src", inc);
	this.i.style.width = "48px";
	this.i.style.height = "48px";
	if (alt != null) this.i.setAttribute("alt", alt);
};

MHMessage.prototype.addLink = function(inc) {
	if (inc == null) return;
	if (this.l == undefined || this.l == null) {
		this.l = document.createElement("div");
		this.MHbD.appendChild(this.l);
	}
	var temp = document.createElement("a");
	if (inc[0] != null && inc[0] != "") temp.id = inc[0];
	temp.innerHTML = inc[1];
	if (inc[2] != null && inc[2] != "") temp.setAttribute("href", inc[2]);
	if (inc[3] != null && inc[3] != "") temp.setAttribute("onclick", inc[3]);
	if (inc[4] != null && inc[4] != "") temp.setAttribute("title", inc[4]);
	else temp.setAttribute("title", inc[1]);
	this.MHlD.appendChild(temp);
};