var email_valid = false;
var agreed = false;
var invalidEmail = "The email address you entered is not valid.";
var codeValid = "false";

function newFocus(current, next) {
	if (current.value != "") {
		document.getElementById(next).focus();
	}
}

function check_email(email) {
	email = document.getElementById(email);
	//alert('check');
	email_value = email.value;
	if (!email_value.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i)) {
		document.getElementById('error').innerHTML = invalidEmail;
		email.focus();
		email_valid = false;
		return false;
	} else {
		document.getElementById('error').innerHTML = "";
		email_valid = true;
		return true;
	}
}

function check(key, next) {
	if (email_valid == false) {
		document.getElementById('email').focus();
	}
	key = document.getElementById(key);
	key_value = key.value;
	key_id = key.id;
	status = "good";
	next_key = next;
	if (key_value != "") {
		if (!key_value.match(/[1-6]/)) {
			document.getElementById('error').innerHTML = "Enter a number between 1 and 6.";
			key.value = "";
			document.getElementById(key_id).focus();
			status = "bad";
		} else {
			document.getElementById('error').innerHTML = "";
		}
		
		for(i=1; i <=6; i++) {
			if ((key_value == document.getElementById('key' + i).value) && ('key' + i) != key.id) {
				document.getElementById('error').innerHTML = "Number already used, try another.";
				key.value = "";
				key.focus();
				status = "bad";
			}
		}
		
		if (status == "good") {
			document.getElementById(next).focus();
		}
	}
}

function check_form() {
	keys_valid = true;
	for(i=1; i <=6; i++) {
		if (document.getElementById('key' + i).value == "") {
			keys_valid = false;
		}
	}
	
	//alert('test');
	
	check_email('email');
	if (document.getElementById('agreed_to_terms').checked) {
		agreed = true;
	} else {
		agreed = false;
	}
	
	if (keys_valid && email_valid && agreed) {
		return true;
	} else {
		if (!agreed) document.getElementById('error').innerHTML = "You must agree to the Terms &amp; Conditions.";
		if (!keys_valid) document.getElementById('error').innerHTML = "The numbers you entered are not valid.";
		if (!email_valid) document.getElementById('error').innerHTML = invalidEmail;
		return false;
	}
}

function check_refCode() {
	refCodeValid = true;
	
	//checkCode();
	
	for (i = 1; i < 6; i++ ) {
		cBox = document.getElementById('c' + i).value;
		if (!cBox.match(/[a-z1-9]/i)) {
			refCodeValid = false;
		} 
	}
	
	//alert(codeValid);
	
	// && (codeValid == "true")
	if (refCodeValid) {
		return true;
	} else {
		//alert(refCodeValid+" ref code ");
		if (!refCodeValid) document.getElementById('error').innerHTML = "This code is not in the right format.";
		//if (!codeValid) document.getElementById('error').innerHTML = "<br />Code Not Valid.";
		return false;
	}
}

function terms(link) {
	popup=window.open(link.href,link.target,'toolbar=no,scrollbars=yes,location=no,statusbar=no,menubar=no,resizable=no,width=772,height=637');
	popup.focus();
	//window.open(link.href,link.target, -1,-1,'yes','yes',0,0,'',false);
	//window.focus();
	
	return false;
}


//AJAX MAGIC :D

var xmlhttp = false;

try {
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
	try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (E) {
		xmlhttp = false;
	}
}

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') xmlhttp = new XMLHttpRequest();


function checkCode() {
	code = "";
	
	for (i = 5; i > 0 ; i-- ) {
		cBox = document.getElementById('c' + i).value;
		code = cBox.concat(code);
	}
	
	//alert(codeValid+" - Before");
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
				codeValid = xmlhttp.responseText;
		}
	
	xmlhttp.open("GET", '/safe/checkcode.php?code='+code);
	xmlhttp.send(null);
	codeValid = xmlhttp.responseText;
}
