// JavaScript Document
function numbersonly(myfield, e, dec){
	var key;
	var keychar;

	if (window.event)
	key = window.event.keyCode;
	else if (e)
	key = e.which;
	else
	return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) ||
	(key==9) || (key==13) || (key==27) )
	return true;

	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	return true;

	// decimal point jump
	else if (dec && (keychar == ".")){
		myfield.form.elements[dec].focus();
		return false;
	}
	else
	return false;
}

function validateUsername(myfield, e){
	var key;
	var keychar;

	if (window.event)
	key = window.event.keyCode;
	else if (e)
	key = e.which;
	else
	return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) ||
	(key==9) || (key==13) || (key==27) ){
		return true;
	}
	// numbers
	//else if ((" `.#@!^&()*~%@'\\/?,-+=|_:;[]{}<>$\"".indexOf(keychar) == -1))
	else if ((("zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP0123456789").indexOf(keychar) > -1))
	return true;
	else
	return false;
}

function selectCountryOnChange(element){
	var value = element.value;
	var otherStateSpan = document.getElementById("otherStateSpan");
	var selectStateSpan = document.getElementById("selectStateSpan");
	if (value == 'US' || value == 'CA'){
		otherStateSpan.style.display = 'none';
		selectStateSpan.style.display = '';
	}
	else{
		otherStateSpan.style.display = '';
		selectStateSpan.style.display = 'none';
	}
}

function checkField(field, whatis, onlyNumber) {
	if(field.value == "") 	{
		alert("Please insert a valid " + whatis);
		field.focus();
		return false;
	}
	else if (onlyNumber==true && !isFinite(field.value))	 	{
		alert(whatis+" numeric characters only");
		field.focus();
		return false;
	}
	else return true;
}

function showOthers(element){
	var value = element.value;
	var showOther = document.getElementById("showOther");

	if(value == 9){
		showOther.style.display = '';
	} else {
		showOther.style.display = 'none';
	}
}


function checkNewUserData(){

	if(checkField(document.regform.fakeUsername,'UserName',false)==false) return false;

	if (document.regform.email.value == "" || document.regform.email.value.indexOf("@") < 1)	{
		alert('Please enter a valid email');
		document.regform.email.focus();
		return false;
	}

	document.regform.submit();

}


function checkAndSubmit()	{
	
	//TITLE
	title = -1;
	for (i=document.regform.title.length-1; i > -1; i--) {
		if (document.regform.title[i].checked) {
			title = i;
		}
	}

	if (title == -1) {
		alert("Please select a valid Title");
		return false;
	}

	//USERNAME
	if(checkField(document.regform.username,'UserName',false)==false) return false;
	//FIRST NAME
	if(checkField(document.regform.firstName,'Fisrt Name',false)==false) return false;
	//LASTNAME
	if(checkField(document.regform.lastName,'Last Name',false)==false) return false;
	//ADDRESS
	if(checkField(document.regform.address1,'Address 1',false)==false) return false;
	//CITY
	if(checkField(document.regform.city,'City',false)==false) return false;
	//COUNTRY
	if(document.regform.country.value == "NA"){
		alert('Country is required!');
		document.regform.country.focus();
		return false;
	}
	//CHECK IF THE COUNTRY IS US AND CHECK THE STATE.
	if(document.regform.country.value == "US"){
		if(document.regform.dbstate.value == "NA"){
			alert('The STATE is required for United States, choose one!');
			document.regform.dbstate.focus();
			return false;
		}
	}
	//CHECK IF THE COUNTRY IS CA AND CHECK THE STATE.
	if(document.regform.country.value == "CA"){
		if(document.regform.dbstate.value == "NA"){
			alert('The PROVINCE is required for Canada, choose one!');
			document.regform.dbstate.focus();
			return false;
		}
	}
	//CHECK ZIP CODE AND SET THE STATE FIELD.
	if((document.regform.country.value == "CA") || (document.regform.country.value == "US")){
		//SET THE STATE FILED
		document.regform.state.value = document.regform.dbstate.value;
		if(document.regform.zipCode.value == ""){
			alert('The Zip Code is required!');
			document.regform.zipCode.focus();
			return false;
		}
	}
	//CHECK IF IS INTERNATIONAL.
	if(document.regform.dbstate.value == "NA"){
		if(document.regform.otherState.value == ""){
			alert('The STATE is required!');
			document.regform.otherState.focus();
			return false;
		} else {
			document.regform.state.value = document.regform.otherState.value;
		}
	}
	//PASSWORD
	if (checkField(document.regform.password,'Password',false)==false) return false;
	if (checkField(document.regform.repassword,'Re-Type Password',false)==false) return false;
	if (checkField(document.regform.passHint,'Password Hint',false)==false) return false;
	if (document.regform.repassword.value != document.regform.password.value){
		alert('Passwords are diferents');
		return false;
	}
	//SECURITY QUESTION.
	if(document.regform.secretQuestion.value == "NA"){
		alert('Secret Question, is a required field');
		document.regform.secretQuestion.focus();
		return false;
	}
	//SECURITY ANSWER.
	if(document.regform.secretAnswer.value == ""){
		alert('Secret Answer, is a required field');
		document.regform.secretAnswer.focus();
		return false;
	}
	//PHONE 1
	if (document.regform.phone1.value == "" || document.regform.phone1.value.length < 10)	{
		alert('Please enter a valid Day phone, must be 10 digits long');
		document.regform.phone1.focus();
		return false;
	}
	//PHONE 2
	//if (document.regform.phone2.value == "" || document.regform.phone2.value.length < 10)	{
	//	alert('Please enter a valid Night phone, must be 10 digits long');
	//	document.regform.phone2.focus();
	//	return false;
	//}
	//EMAIL
	if (document.regform.email.value == "" || document.regform.email.value.indexOf("@") < 1)	{
		alert('Please enter a valid email');
		document.regform.email.focus();
		return false;
	}
	//REFERRAL
	if(document.regform.referral.value == "NA"){
		alert('How did you hear about us? is a required field');
		document.regform.referral.focus();
		return false;
	}
	//OTHER REFERRAL SOURCE
	if(document.regform.referral.value == 9){
		if(trim(document.regform.otherSource.value) == "" || document.regform.otherSource.value == "(Specify)"){
			alert('Specify Source is required.');
			document.regform.otherSource.focus();
			return false;
		} else {
			document.regform.comments.value = "Specific Source: "+document.regform.otherSource.value;
		}
	}
	//CURRENCY CODE
	if(document.regform.currencyCode.value == "NA"){
		alert('Please select a valid Currency');
		document.regform.currencyCode.focus();
		return false;
	}
	//ACCEPT THE TERMS & CONDITIOS.
	if (document.regform.accept.checked == false) {
		alert("You must accept the terms and conditions");
		document.regform.accept.focus();
		return false;
	}
	//AFTER FINISHING.
	if (document.regform.read.checked == false) {
		alert("You must read about the After finishing text.");
		document.regform.read.focus();
		return false;
	}

	document.getElementById('signupLoading').style.display = 'block';
	// Checking the players credentials with merge
	if(checkCredentials() == false) return false;
	 
	document.regform.submit();
}

function updateClientInfo()	{

	//TITLE
	title = -1;
	for (i=document.regform.title.length-1; i > -1; i--) {
		if (document.regform.title[i].checked) {
			title = i;
		}
	}

	if (title == -1) {
		alert("Please select a valid Title");
		return false;
	}

	//USERNAME
	//if(checkField(document.regform.username,'UserName',false)==false) return false;
	//FIRST NAME
	if(checkField(document.regform.firstName,'Fisrt Name',false)==false) return false;
	//LASTNAME
	if(checkField(document.regform.lastName,'Last Name',false)==false) return false;
	//ADDRESS
	if(checkField(document.regform.address1,'Address 1',false)==false) return false;
	//CITY
	if(checkField(document.regform.city,'City',false)==false) return false;
	//COUNTRY
	if(document.regform.country.value == "NA"){
		alert('Country is required!');
		document.regform.country.focus();
		return false;
	}
	//CHECK IF THE COUNTRY IS US AND CHECK THE STATE.
	if(document.regform.country.value == "US"){
		if(document.regform.dbstate.value == "NA"){
			alert('The STATE is required for United States, choose one!');
			document.regform.dbstate.focus();
			return false;
		}
	}
	//CHECK IF THE COUNTRY IS CA AND CHECK THE STATE.
	if(document.regform.country.value == "CA"){
		if(document.regform.dbstate.value == "NA"){
			alert('The PROVINCE is required for Canada, choose one!');
			document.regform.dbstate.focus();
			return false;
		}
	}
	//CHECK ZIP CODE AND SET THE STATE FIELD.
	if((document.regform.country.value == "CA") || (document.regform.country.value == "US")){
		//SET THE STATE FILED
		document.regform.state.value = document.regform.dbstate.value;
		if(document.regform.zipCode.value == ""){
			alert('The Zip Code is required!');
			document.regform.zipCode.focus();
			return false;
		}
	}
	//CHECK IF IS INTERNATIONAL.
	if(document.regform.dbstate.value == "NA"){
		if(document.regform.otherState.value == ""){
			alert('The STATE is required 1!');
			document.regform.otherState.focus();
			return false;
		} else {
			document.regform.state.value = document.regform.otherState.value;
		}
	}
	//PASSWORD
	if (checkField(document.regform.password,'Password',false)==false) return false;
	if (checkField(document.regform.repassword,'Re-Type Password',false)==false) return false;
	if (checkField(document.regform.passHint,'Password Hint',false)==false) return false;
	if (document.regform.repassword.value != document.regform.password.value){
		alert('Passwords are diferents');
		return false;
	}
	//SECURITY QUESTION.
	if(document.regform.secretQuestion.value == "NA"){
		alert('Secret Question, is a required field');
		document.regform.secretQuestion.focus();
		return false;
	}
	//SECURITY ANSWER.
	if(document.regform.secretAnswer.value == ""){
		alert('Secret Answer, is a required field');
		document.regform.secretAnswer.focus();
		return false;
	}
	//PHONE 1
	if (document.regform.phone1.value == "" || document.regform.phone1.value.length < 10)	{
		alert('Please enter a valid Day phone, must be 10 digits long');
		document.regform.phone1.focus();
		return false;
	}
	//PHONE 2
	if (document.regform.phone2.value == "" || document.regform.phone2.value.length < 10)	{
		alert('Please enter a valid Night phone, must be 10 digits long');
		document.regform.phone2.focus();
		return false;
	}
	//EMAIL
	//if (document.regform.email.value == "" || document.regform.email.value.indexOf("@") < 1)	{
	//	alert('Please enter a valid email');
	//	document.regform.email.focus();
	//	return false;
	//}
	//REFERRAL
	if(document.regform.referral.value == "NA"){
		alert('How did you hear about us? is a required field');
		document.regform.referral.focus();
		return false;
	}
	//CURRENCY CODE
	if(document.regform.currencyCode.value == "NA"){
		alert('Please select a valid Currency');
		document.regform.currencyCode.focus();
		return false;
	}
	//ACCEPT THE TERMS & CONDITIOS.
	if (document.regform.accept.checked == false) {
		alert("You must accept the terms and conditions");
		document.regform.accept.focus();
		return false;
	}
}

//Removes leading whitespaces
function LTrim( value ) {

	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");

}

// Removes ending whitespaces
function RTrim( value ) {

	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim( value ) {

	return LTrim(RTrim(value));

}