// JavaScript Document

function login (serial, type) {	
	DOM.user_auth = true;
	DOM.user_serial = serial;
	DOM.gallery_index = 0;
	DOM.type = type;
	DOM.owner = true;
	
	var callback = function () {
		window.location = "user";
	}
	
	if(type == '3') {
		callback = jobs_callback;
	}
	setcookie(serial, type, callback);
}

function jobs_callback () {
	window.location = "jobs";
}

function run_login(serial, type, available) {
	DOM.user_auth = true;
	DOM.user_serial = serial;
	DOM.gallery_index = 0;
	DOM.type = type;
	DOM.owner = true;
	DOM.available = available;

	if(type == 0) { my_portfolio(); }
	if(type == 1) { my_gallery(); }
}

function set_login(serial, type, available) {
	DOM.user_auth = true;
	DOM.user_serial = serial;
	DOM.gallery_index = 0;
	DOM.type = type;
	DOM.owner = true;
	DOM.available = available;
}

function confirm_signup (email, pass) {
	var sent = new Confirm_Widget(email, pass, 'content');
	sent.success();
}

function Confirm_Widget (email, pass, root) {
	this.email = email;
	this.pass = pass;
	this.root = root;
}

Confirm_Widget.prototype.success = function () {
	_get(this.root).innerHTML = this.created();
}

Confirm_Widget.prototype.error = function () {
	_get(this.root).innerHTML = this.failed();
}

Confirm_Widget.prototype.created = function () {
	var html = "An email has been sent to <b>" + this.email + "</b><br />"
		+ "<div style=\"background-color: #222; color: #EEE; padding: 14px; width: 460px\">"
		+ "<div style=\"background-color: #111; padding: 14px\">You must confirm your email address to activate your account.<br />" + "1. Visit your mail account.<br />2. Click the provided link.<br />3. Login using email and password.<br />4. Get Started.</div></div>";
	return "<div style=\"padding: 14px; height: 240px\">" + html + "</div>";
}

Confirm_Widget.prototype.failed = function () {
	var html = "An email could not be sent.<br />"
		+ "Is the email you provided accurate ?<br />"
		+ this.email + " <b>failed</b>";
	return html;
}

Confirm_Widget.prototype.safe_test = function () {	
	var old = "An email has been sent.<br />"
		+ "It should look like this:<br />"
		+ "<a href=\"cta.php?ESS=" + email + "&TASL=" + pass + "\">Confirm Your Account</a>";
	return old;
}

function logout (serial, home) {
	DOM.user_auth = false;
	DOM.user_serial = "";
	DOM.gallery = new Array();
	DOM.resume = new Array();
	DOM.gallery_index = 0;
	
	var pre = "../";
	if(home) {
		pre = "";
	}
	
	var callback = function () {
		window.location = pre + "index.php";
	}
	delete_cookie(serial, callback, home);
}

function delete_cookie (serial, callback, home) {
	var pre = "../";
	if(home) {
		pre = "";
	}
	
	try { var XObj = new XMLHttpRequest(); }
	catch(e) { var XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			var message = XObj.responseText;
			if(callback) {
				callback(message);
			}
		}
	}
	XObj.open('POST', pre + 'php/cookie.php?serial='+ serial +'&delete=true',true);
	XObj.send(null);	
}

function getcookie (callback) {
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			var message = XObj.responseText;
			var data = message.split("<&sect;>", 3);
		
			if(callback) {
				callback(data[0], data[1], data[2]);
			}
		}
	}
	XObj.open('POST','php/cookie.php?get=true',true);
	XObj.send(null);
}

function get_user_cookie (callback) {
	
	var XObj;
	try { XObj = new XMLHttpRequest(); }
	catch(e) { XObj = new ActiveXObject(Microsoft.XMLHTTP); }
	
	XObj.onreadystatechange = function () {
		if(XObj.readyState == 4) {
			var message = XObj.responseText;
			var data = message.split("<&sect;>", 3);
		
			if(callback) {
				callback(data[0], data[1], data[2]);
			}
		}
	}
	XObj.open('POST','../php/cookie.php?get=true',true);
	XObj.send(null);
}

function setcookie (serial, type, callback) {
	
	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/cookie.php?serial='+serial+"&type="+type+"&set=true",true);
	XObj.send(null);
}