// JavaScript Document
var form_type = "bronze";

function submit_free_signup () {
	form_type = "silver";
	submit_signup();
}

function submit_silver_signup () {
	form_type = "silver";
	submit_silver_payment();
}

function submit_bronze_signup () {
	form_type = "bronze";
	submit_signup();
}

function submit_jobs_signup () {
	form_type = "jobs";
	submit_signup();
}

function submit_signup () {
	var email = _get('email').value;
	var password = _get('password').value;
	var c_password = _get('c_password').value;
	var agree_box = _get('agree_box').checked;
	
	if(!email.match('@')) {
		alert("You must enter a valid email.");
		return false;
	}
	
	if(!password || password.length < 6 || password.length > 12) {
		alert("You did not enter a valid password. Your password must be 6-12 charcters long.");
		return false;
	}
	
	if(password != c_password) {
		alert("Your passwords did not match, you may want to right this down");
		return false;
	}
	
	if(!agree_box) {
		alert("Please Review the Terms of Service");
		return false;
	}
	
	var callback = send_new_account_info;
	encode_p(email, password, callback);
}
	
function submit_silver_payment () {
	var email = temp_form.email;
	var password = temp_form.password;
	var payment_amount = _get('payment_amount').value;
	
	/* var card_name = _get('card_name').value;
	var card_number = _get('card_number_a').value + _get('card_number_b').value + _get('card_number_c').value + _get('card_number_d').value;
	var expiration = _get('expiration_month').value + "/20" + _get('expiration_year').value;
	
	if(!card_name) {
		alert("You must enter your name as it appears on your card.");
		return false;
	}
	
	if(!card_number || card_number.length != 16) {
		alert("You must provide the account number as it appears on your card.");
		return false;
	}
	
	if(!expiration) {
		alert("You must provide the expiration date of your card.");
		return false;
	}*/
	
	//verify card information
	//submit card information for payment
	
	var callback = function(email, pass) {
		window.location = 'pay.php?email=' + email + '&am=' + payment_amount + '&pass=' + pass;	
	}
	encode_p(email, password, callback);
}

function send_new_account_info(email, password) {
	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;>", 2);
		
			if(data[0] == "error") {
				alert(data[1]);
			} else {
				alert(data[1]);
				confirm_signup(email, password);
			}
		}
	}
	var type = form_type;
	if(type == "silver") {
		type = "1";
	} else if (type == "gold") {
		type = "2";
	} else if (type == "jobs") {
		type = "3";
	} else {
		type = "0";
	}
	
	XObj.open('POST','php/user.php?email='+email+'&pass='+password+'&type='+type,true);
	XObj.send(null);
}

function check_account_info(email, password, 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;>", 2);
		
			if(data[0] == "error") {
				alert(data[1]);
			} else {
				alert(data[1]);
				if(callback) {
					callback();
				}
			}
		}
	}
	var type = "check";
	
	XObj.open('POST','php/user.php?email='+email+'&pass='+password+'&type='+type,true);
	XObj.send(null);
}