/*	formcheck.js
	###########################################################################
	### WWW.HOTLAPS.DE - HOTLAP COMPETITIONS
	### Developed by Hansjoerg Beck, 61440 Oberursel, Germany
	### http://www.hotlaps.de
	###########################################################################
*/

//=== Set All CheckBoxes
function SetAllCheckBoxes(FormName, FieldName, CheckValue) {
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	
	if(objCheckBoxes[0].checked)
		CheckValue = false;
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}

//=== Set Cookie
function SetCookie (url,cname,cvalue,mode,exp) {
	var found = 0;
	var cexpire = "";
	ca = readCookie(cname);
	if (ca)	{
		va = ca.split('&');
	}
	else {
		va = new Array();
	}
	for(var i=0;i<va.length;i=i+2) {
		if (va[i] == cvalue) {
			va[i+1] = mode;
			found = 1;
		}
	}
	if (found == 0) {
		va.push(cvalue,mode);
	}
	var cvars = "";
	for(var i=0;i<va.length;i++) {
		cvars = cvars + va[i] + "&";
	}
	cvars = cvars.substring(0,cvars.length - 1);

	//cookie expires ?;
	if (exp == 0) {
		cexpire = ";expires=Thu, 01-Jan-2050 00:00:01 GMT"
	}
	if (exp < 0) {
		cexpire = ";expires=Sat, 01-Jan-2000 00:00:01 GMT"
	}
	cookiestring = cname + "=" + cvars + ";path=/" + cexpire;
	document.cookie = cookiestring;
	window.open(url,target="_self");
}

//=== ReadCookie
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//========== SELECT AND GO
function surfto(form,fld) {
	var myindex=form.elements[fld].selectedIndex;
	window.open(form.elements[fld].options[myindex].value,target="_self");
}

//========== USER SELECT
function usrquery(form) {
	var usrquery0=form.QUERY0.options[form.QUERY0.selectedIndex].value;
	if (form.QUERY1)
		var usrquery1=form.QUERY1.options[form.QUERY1.selectedIndex].value;
	else
		var usrquery1="";
	if (form.QUERY2)
		var usrquery2=form.QUERY2.options[form.QUERY2.selectedIndex].value;
	else
		var usrquery2="";
	if (form.QUERY3)
		var usrquery3=form.QUERY3.options[form.QUERY3.selectedIndex].value;
	else
		var usrquery3="";
		window.open(usrquery0,target="_self");
	}

//========== KEYPRESS CHECK
function maskKeyPress(objEvent) {
	var iKeyCode;
  if (navigator.userAgent.indexOf('Firefox')!=-1) {
    iKeyCode = objEvent.which;
	}
  else {
	   iKeyCode = objEvent.keyCode;
  }
  if(iKeyCode>=48 && iKeyCode<=58 || iKeyCode==46 || iKeyCode==8 || iKeyCode==0) return true;
  return false;
}


//=== HANDLE ADMIN-SELECTION
function handleadm(b_action,b_lapid,b_trackid,b_drvname) {
	document.ADMINFORM.ADMACTION.value = b_action;
	document.ADMINFORM.LAPID.value = b_lapid;
	document.ADMINFORM.TRACKID.value = b_trackid;
	document.ADMINFORM.ADMCOMMENT.value = "";
	if (b_action == 'del') {
		Check = window.prompt("Please enter a comment why you DELETE the hotlap of '" + b_drvname + "'");
		if (Check) {
			document.ADMINFORM.ADMCOMMENT.value = Check;
		}
		else return false;
	}
	if (b_action == 'flg') {
		Check = window.prompt("Please enter a comment why you FLAG the hotlap of '" + b_drvname + "' to be investigated by a webadmin");
	    if (Check) {
			document.ADMINFORM.ADMCOMMENT.value = Check;
		}
		else return false;
	}
	if (b_action == 'edt') {
	}

	//Appoint LAPAdmin
	if (b_action == 'ala') {
		var answer = confirm ("Do you want to appoint driver '" + b_drvname + "' as a LAPAdmin ?")
		if (!(answer)) {
			return false;
		}
	}
	//Revoke LAPAdmin
	if (b_action == 'rla') {
		var answer = confirm ("Do you want to revoke LAPAdmin permissions from driver '" + b_drvname + "' ?")
		if (!(answer)) {
			return false;
		}
	}
	//Appoint LAPChecker
	if (b_action == 'alc') {
		var answer = confirm ("Do you want to appoint driver '" + b_drvname + "' as a LAPChecker ?")
		if (!(answer)) {
			return false;
		}
	}
	//Revoke LAPChecke
	if (b_action == 'rlc') {
		var answer = confirm ("Do you want to revoke LAPChecker permissions from driver '" + b_drvname + "' ?")
		if (!(answer)) {
			return false;
		}
	}
	
	
	document.ADMINFORM.submit();
}

//=== CHECK FORM: Registration
function checkregister(form,USRAction) {
	//.CHECK Driver Name
	if (document.DRVForm.F_REALNAME.value == "") {
		alert("You have to enter a driver-name !");
		return false;
	}

	//.CHECK EMail Address
	var ats = 0;
	var valid;
	var email = document.DRVForm.M_EMAIL.value;
	if (email == "") {
		alert("You have to enter an EMail address !");
		return false;
	}
	for(i=0; i<email.length; i++) {
		if(email.substr(i, 1) == "@") {
			ats++;
		}
	}
	if(ats != 1) {
		valid = false;
	}
	else {
		var preSuffix = email.split("@");
		var prefix = preSuffix[0];
		var suffix = preSuffix[1];
		if(prefix.length == 0 || suffix.indexOf(".") == -1) {
			valid = false;
		}
		else {
			var domain = suffix.split(".");
			var topLevelDomain = domain.pop();
			if((topLevelDomain.length > 4) || (topLevelDomain.length < 1)){
				valid = false;
			}
		}
	}
	if (valid == false)	{
		alert("ERROR: Not a valid EMail address!");
		return false;
	}

	//.CHECK Country selected
	if (document.DRVForm.NATION.value == "") {
		alert("You have to select a country !");
		return false;
	}

	//.CHECK Password
	psw1 = document.DRVForm.M_PASSW1.value;
	psw2 = document.DRVForm.M_PASSW2.value;
	if (psw1 == "") {
		if (USRAction != "E") {
			alert("You have to enter a password !");
			return false;
		}
	}
	else {
		if (psw1.length < 3) {
			alert("ERROR: Your password must have at least 3 characters !");
			return false;
		}
		else {
			if (psw1 != psw2 ) {
				alert("ERROR: Passwords do not match !");
				return false;
			}
		}
	}
}

//=== Hide/Unhide area
function HideArea(ehide,eshow) {
	document.getElementById(ehide).style.display = 'none';
	document.getElementById(eshow).style.display = 'block';
}

//=== CHECK FORM: Activation
function checkactivation(form) {
	//.CHECK Activation Code
	acode = document.DRVACTIVATE.M_ACODE.value;

	if (acode == "") {
		alert("You have to enter your activation code !");
		return false;
	}
	else {
		if (acode.length != 10) {
			alert("ERROR: Your activation code must be 10 digits !");
			return false;
		}
		else {
			for(i=0; i<acode.length; i++) {
				if ((acode.charCodeAt(i)<48) || (acode.charCodeAt(i)>57)) {
					alert("ERROR: Your activation code only allows numbers !");
					return false;
				}
			}
		}
	}
}

//=== CHECK FORM: AddVote
function checkaddvote(form) {
	if (document.DRVADDVOTE.F_VOGTITLE.value == "") {
		alert("You have to enter a GAME TITLE !");
		return false;
	}
	if (document.DRVADDVOTE.F_VOGTYPE.value == "") {
		alert("You have to select a GAME-TYPE !");
		return false;
	}
}

//=== CHECK FORM: Login
function checklogin(form) {
	if ((document.USRLOGIN.USRPASSW.value == "") && (document.USRLOGIN.USRLOGIN.value == "")) {
		alert("You have to enter your DRIVER ACCOUNT and PASSWORD !");
		return false;
	}
	else {
		if (document.USRLOGIN.USRLOGIN.value == "") {
			alert("You have to enter valid DRIVER ACCOUNT !");
			return false;
		}
		if (document.USRLOGIN.USRPASSW.value == "") {
			alert("You have to enter your PASSWORD !");
			return false;
		}
	}
}

//=== CHECK FORM: SendPW
function checksendpw(form) {
	//.CHECK Driver Name
	if (document.DRVSENDPW.F_REALNAME.value == "") {
		alert("You have to enter a driver-name !");
		return false;
	}

	//.CHECK EMail Address
	var ats = 0;
	var valid;
	var email = document.DRVSENDPW.M_EMAIL.value;
	if (email == "") {
		alert("You have to enter an EMail address !");
		return false;
	}
	for(i=0; i<email.length; i++) {
		if(email.substr(i, 1) == "@") {
			ats++;
		}
	}
	if(ats != 1) {
		valid = false;
	}
	else {
		var preSuffix = email.split("@");
		var prefix = preSuffix[0];
		var suffix = preSuffix[1];
		if(prefix.length == 0 || suffix.indexOf(".") == -1) {
			valid = false;
		}
		else {
			var domain = suffix.split(".");
			var topLevelDomain = domain.pop();
			if((topLevelDomain.length > 4) || (topLevelDomain.length < 1)){
				valid = false;
			}
		}
	}
	if (valid == false)	{
		alert("ERROR: Not a valid EMail address!");
		return false;
	}
}