// JavaScript Document

var tickValid = '<img src="tick.png" alt="valid" />'; // tick images
var crossValid = '<img src="cross.png" alt="invalid" />'; // cross image

var errorLines = new Array(
						   "Invalid e-mail address",
						   "Invalid phone number",
						   "Must contain no spaces",
						   "Username is taken",
						   "Not enough characters",
						   "Must contain 6 characters or more",
						   "Password do not match",
						   "No Special Characters"
						   )

// Registration
var fname = 0; // First Name
var sname = 0; // Last Name
var uname = 0; // Username
var pword = 0; // Password
var cpword = 0; // Confirm Password
var email = 0; // E-Mail
var membership = 0; // Membership

// Check Names
function checkNames(field, fieldList) {
	if(!document.getElementById(field+'name').value) {
		document.getElementById(field+'valid').innerHTML = crossValid;
		if(fieldList == '0') fname = 0;
		if(fieldList == '1') sname = 0;
	} else {
		document.getElementById(field+'valid').innerHTML = tickValid;
		if(fieldList == '0') fname = 1;
		if(fieldList == '1') sname = 1;
	}
}

// Check E-Mail
function checkEmail(field) {
	if(document.getElementById(field).value) {
		var eCheck = /^[\w_\-.]+\@[\w_\-\.]+\.[\w]{2,}$/;
		if(document.getElementById(field).value.match(eCheck) == null) {
			document.getElementById(field+'valid').innerHTML = crossValid + "<span class=\"error\">" + errorLines[0] + "</span>";
			email = 0;
		} else {
			document.getElementById(field+'valid').innerHTML = tickValid;
			email = 1;
		}
	} else {
		document.getElementById(field+'valid').innerHTML = crossValid;
		email = 0;
	}	
}

// Check Username
function checkUname() {
	var xmlHttp;
	try {
    	// Firefox, Opera 8.0+, Safari
	    xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
    	// Internet Explorer
	    try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
    	catch (e) {
		    try {
    	    	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    	}
   			catch (e) {
		        alert("Your browser does not support AJAX!");
//		        return false;
	        }
	    }
	}
	checkUnameA(xmlHttp);
}

// function continued
function checkUnameA(xmlResult) {
	if (!document.getElementById('uname').value) {
		document.getElementById('uvalid').innerHTML = crossValid;
		uname = 0;
	} else {
		var result1 = document.getElementById('uname').value;
		var checkletters = /[^a-zA-Z1-9-_ ]/;
		var checkspace = /[\s]/;
	
		if (checkletters.test(result1)) {
			document.getElementById('uvalid').innerHTML = crossValid + "<span class=\"error\">" + errorLines[7] + "</span>";
			uname = 0;
			return false;
		} else if (checkspace.test(result1)) {
			document.getElementById('uvalid').innerHTML = crossValid + "<span class=\"error\">" + errorLines[2] + "</span>";
			uname = 0;
			return false;
		}
	}
	
	if (document.getElementById('uname').value) {
		xmlResult.onreadystatechange=function() {
		document.getElementById('uvalid').innerHTML = "<strong>Checking...</strong>";
			if(xmlResult.readyState==4) {
				if(xmlResult.responseText) {
	       			document.getElementById('uvalid').innerHTML = xmlResult.responseText;
					if(document.getElementById('validUnameChecker').value == 'notchar') {
		       			document.getElementById('uvalid').innerHTML = crossValid + "<span class=\"error\">" + errorLines[4] + "</span>";
						uname = 0;
					}
					if(document.getElementById('validUnameChecker').value == 'taken') {
		       			document.getElementById('uvalid').innerHTML = crossValid + "<span class=\"error\">" + errorLines[3] + "</span>";
						uname = 0;
					}
					if(document.getElementById('validUnameChecker').value == 'valid') {
		       			document.getElementById('uvalid').innerHTML = tickValid;
						uname = 1;
					}
				}
			}
		}
	}
	xmlResult.open("GET","check.php?name=" + document.getElementById('uname').value,true);
	xmlResult.send(null);
}


// Check Password
function checkPass(field) {
	if(document.getElementById(field).value) {
		if(document.getElementById(field).value.length <= 5) {
			document.getElementById(field+'valid').innerHTML = crossValid + "<span class=\"error\">" + errorLines[5] + "</span>";
			document.getElementById('cpwordvalid').innerHTML = crossValid;	
			pword = 0;
			cpword = 0;
		} else {
			document.getElementById(field+'valid').innerHTML = tickValid;
			document.getElementById('cpwordvalid').innerHTML = crossValid;	
			pword = 1;
			cpword = 0;
		}
	} else {
		document.getElementById(field+'valid').innerHTML = crossValid;
		document.getElementById('cpwordvalid').innerHTML = crossValid;	
		pword = 0;
		cpword = 0;
	}
}

// Check Confirm Password
function checkConPass() {
	if(document.getElementById('pword').value && document.getElementById('cpword')) {
		if(document.getElementById('pword').value == document.getElementById('cpword').value) {
			document.getElementById('cpwordvalid').innerHTML = tickValid;
			cpword = 1;
		} else {
			document.getElementById('cpwordvalid').innerHTML = crossValid + "<span class=\"error\">" + errorLines[6] + "</span>";
			cpword = 0;
		}
	}
}

function checkMembership() {
	if(!document.getElementById('membership1').checked && !document.getElementById('membership2').checked && !document.getElementById('membership3').checked) {
		document.getElementById('membershipvalid').innerHTML = crossValid;
		membership = 0;
	} else {
		document.getElementById('membershipvalid').innerHTML = tickValid;
		membership = 1;
	}
}

// Final Check Return
function vCheck() {
	checkUname();
	checkNames('f', '0');
	checkNames('s', '1');
	checkEmail('email');
	checkPass('pword');
	checkConPass();
	checkMembership();
	
	if(fname == 1 && sname == 1 && email == 1 && uname == 1 && pword == 1 && cpword == 1 && membership == 1) {
		return true;
	} else {
		return false;	
	}
}


/////////////////////////////////////////////

var luname = 0; // Login Username
var lpword = 0; // Login Password

function checkLogin() {
	if(!document.getElementById('luname').value) {
		document.getElementById('lunamevalid').innerHTML = crossValid;
		luname = 0;
	} else {
		document.getElementById('lunamevalid').innerHTML = tickValid;
		luname = 1;
	}

	if(!document.getElementById('lpword').value) {
		document.getElementById('lpwordvalid').innerHTML = crossValid;
		lpword = 0;
	} else {
		document.getElementById('lpwordvalid').innerHTML = tickValid;
		lpword = 1;
	}
}

// Final Check Return
function loginCheck() {
	checkLogin();
	
	if(luname == 1 && lpword == 1) {
		return true;
	} else {
		return false;	
	}
}
