// JavaScript Document

function set_cookie(id, callback) {

	var callback = function () {
		window.location.reload();
	}

	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
			
	XObj.open('POST','php/set_cookie.php?id='+id,true);
	XObj.send(null);
}

function subLogin (tag) {
	var email = document.getElementById('inputEmail'+tag).value;
	var pass = document.getElementById('inputPass'+tag).value;
	
	if(!email || !pass) {


	loginFail('login_message_'+tag, 'You must enter your email AND password to login.');
	return false;
	}

	autoLogin(email, pass, tag);
}

function loginFail (domName, message) {
	$(domName).style.display = "block";
	$(domName).innerHTML = message;
}

function resetForm(tag) {
	var html = "confirm email:<input id='resetEmail" + tag + "'/><br />new password:<input id='resetPass" + tag + "' type='password' /><br /><input type='button' style='background-color: #FFF; width: 60px; cursor: pointer' value='Reset' onClick='sendPassword(" + tag + ")'/>";
	$('login_input_'+tag).innerHTML = html;
	$('login_message_'+tag).innerHTML = "";
}


function sendPassword (tag) {
	var email = document.getElementById('resetEmail'+tag).value;
	var pass = document.getElementById('resetPass'+tag).value;
	
	if(!email || !pass) {	
		loginFail('login_message_'+tag, 'You must confirm your email AND enter a new password to login.');
		return false;
	}

	var callback = function (txt) {
		if(txt.reponseText) { loginFail('login_message_'+tag, 'Sorry, the email you provided did not match our records...'); return false; }
		$('login_input_'+tag).innerHTML = "Check inbox to confirm your email and login with your new password.";
		$('login_message_'+tag).innerHTML = "";
	}

	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }

	XObj.onreadystatechange = function () {	
	if(XObj.readyState == 4) {
			if(callback) {
				callback(XObj);
			}
		}		
	}		
	
	XObj.open('POST','php/reg.php?reset=1&email='+email+'&passwd='+pass,true);
	XObj.send(null);
}

function autoLogin (email, pass, tag) {
	var callback = function (txt) {
		if(txt.responseText) {
			var ERROR = txt.responseText + " <a onClick='resetForm(\"" + tag + "\")'>Forgot Password?</a>";
	                loginFail('login_message_'+tag, ERROR);
			return false;
		}
		window.location="./profile.php";
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {	
	if(XObj.readyState == 4) {
			if(callback) {
				callback(XObj);
			}
		}		
	}		
	
	XObj.open('POST','php/login.php?email='+email+'&passwd='+pass,true);
	XObj.send(null);
}

function submitLogout () {
	var callback = function () {
		window.location.reload();
	}
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {	
	if(XObj.readyState == 4) {
			if(callback) {
				callback();
			}
		}		
	}		
	
	XObj.open('POST','php/login.php?logout=true', true);
	XObj.send(null);	
}
