/**  sld - misc js jan 2008  **/
//send to paypal
function GoToPaypal()
{
	alert('Paypal');
	
}
function ShowEntryForm()
{
	document.getElementById('txtform').innerHTML = "";
	document.getElementById('txtmsg').style.display = 'block';
}	


// entry form


function UpdateDB(dowhat){

	var list = document.getElementById("weaponlist").innerHTML;
	var forename = document.getElementById("forename").innerHTML;
	var surname = document.getElementById("surname").innerHTML; 
	var addressone = document.getElementById("addressone").innerHTML;
	var addresstwo = document.getElementById("addresstwo").innerHTML;
	var club = document.getElementById("club").innerHTML;
	var membership = document.getElementById("membership").innerHTML;
	var country = document.getElementById("country").innerHTML;
	var countryname = document.getElementById("countryname").innerHTML;
	var town = document.getElementById("town").innerHTML;
	var postcode = document.getElementById("postcode").innerHTML;
	var phone = document.getElementById("phone").innerHTML;
	var dob = document.getElementById("dob").innerHTML; 
	var email = document.getElementById("email").innerHTML;
	
	var page_request = false;
	var strUrl;
	
	var date = new Date();
	var timestamp = date.getTime();
	
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
				page_request = new XMLHttpRequest()
		else if (window.ActiveXObject){ // if IE
			try {
				page_request = new ActiveXObject("Msxml2.XMLHTTP")
			}
			catch (e){
				try{
					page_request = new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e){
					alert('sorry, your browser does not support ajax - please upgrade to a newer version');
				}
			}
		}else return false
	
		page_request.onreadystatechange=function(){
			if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
				var return_array = page_request.responseText.split(",");
				document.getElementById('paybutton').innerHTML = return_array;
				document.frmPaypal.submit();
			}
		}
	
	strUrl = "themes/chameleon/ajax/ajax.php?dowhat=NEW&forename=" + forename + "&surname=" + surname + "&addressone=" + addressone + "&addresstwo=" + addresstwo + "&membership=" + membership;
	//strUrl += "&countryname=" + countryname + "&town=" + town + "&postcode=" + postcode + "&phone=" + phone + "&dob=" + dob + "&email=" + email + "&country=" + country  + "&club=" +  club + "&events=" + list.slice(0, -1) + "&time=" + timestamp;
	strUrl += "&countryname=" + countryname + "&town=" + town + "&postcode=" + postcode + "&phone=" + phone + "&dob=" + dob + "&email=" + email + "&country=" + country  + "&club=" +  club + "&events=" + list + "&time=" + timestamp;
		
	//document.write(strUrl);
	page_request.open('GET', strUrl, true);
	page_request.send(null);
		

}

//entry form
function SubmitForm(){
	//alert('');
	//return;
	//Get the values
	var list = '';
	var accomodation = '';
	var addresstwo = '';
	var club = '';
	var country = 0;
	var countryname = "";
	var myForm = document.frmEvent;
	var page_request = false;
	var strUrl;
	var accom = 0;
	var date = new Date();
	var timestamp = date.getTime();
	
	
	//Event list
	for(i=0; i<myForm.cboxEvent.length; i++){
		if(myForm.cboxEvent[i].checked){
			list += myForm.cboxEvent[i].value+',';
		}
	}
	if (list == ''){
		alert('Please choose an event');
		return;
	}
	else { // check for foil & sabre togethrer - not allowed
		if ((list.indexOf("4") != -1 && list.indexOf("2") != -1) || (list.indexOf("4") != -1 && list.indexOf("2") != -1)){
			alert('You may not fence foil & epee');
			return;
		}
		else {
			//alert('yep ' + list);
		}
		if ((list.indexOf("3") != -1 && list.indexOf("1") != -1) || (list.indexOf("3") != -1 && list.indexOf("1") != -1)){
			alert('You may not fence foil & epee');
			return;
		}
		else {
			//alert('yep ' + list);
		}
	}
	
	//accomodation
	for(i=0; i<myForm.cboxAccomodation.length; i++){
		if(myForm.cboxAccomodation[i].checked){
			accomodation += myForm.cboxAccomodation[i].value+',';
		}
	}
	
	if ((accomodation.indexOf("1") != -1) && (accomodation.indexOf("2") == -1)) accom = 1; // only fri checked
	if ((accomodation.indexOf("1") == -1) && (accomodation.indexOf("2") != -1)) accom = 2; // only sat checked 
	if ((accomodation.indexOf("1") != -1) && (accomodation.indexOf("2") != -1)) accom = 3; // both checked 
	//alert (accom); return;

	//optional fields
	addresstwo = document.getElementById('txtaddresstwo').value;
	Arrcountry = document.getElementById('country').value.split("|");
	country = Arrcountry[0];
	countryname = Arrcountry[1];
	club = document.getElementById('txtclub').value;
	
	//required fields
	var forename = ValidateForm('txtforename', 'Please enter a forename'); if (!forename) return;
	var surname = ValidateForm('txtsurname', 'Please enter a surname'); if (!surname) return;
	var addressone = ValidateForm('txtaddressone', 'Please enter an address'); if (!addressone) return;
	var town = ValidateForm('txttown', 'Please enter a town'); if (!town) return;
	//chk if uk for postcode
	if (country == 1){
		var postcode = ValidateForm('txtpostcode', 'Please enter a postcode'); if (!postcode) return;
	}
	else {
		var postcode = 'NA';
	}
	var membership = ValidateForm('txtmembership', 'Please enter a membership no'); if (!membership) return;
	var phone = ValidateForm('txtphone', 'Please enter a phone number'); if (!phone) return;
	var dob = ValidateForm('txtdob', 'Please enter a date of birth'); if (!dob) return;
	if (!validateDate(document.getElementById('txtdob'))) return;
	var email = ValidateForm('txtemail', 'Please enter an email address'); if (!email) return;
	if (!validate_email(document.getElementById('txtemail'), 'Please enter a valid email address')) return ;
	//both email's the same - make this better
	if (email != trim(document.getElementById('txtemail2').value)) {alert('emails must match\nplease amend and try again'); document.getElementById('txtemail2').focus(); return false;}
		
	//hide the form
	//document.getElementById('txtmsg').style.display = 'none';
		
	strUrl = "?q=node/8&dowhat=CF&forename=" + forename + "&surname=" + surname + "&addressone=" + addressone + "&addresstwo=" + addresstwo + "&membership=" + membership;
	strUrl += "&countryname=" + countryname + "&town=" + town + "&postcode=" + postcode + "&phone=" + phone + "&dob=" + dob + "&email=" + email + "&country=" + country  + "&club=" +  club + "&events=" + list.slice(0, -1) + "&accomodation=" + accom + "&time=" + timestamp;
	location.href = strUrl;
	//alert (strUrl);
	//document.write(strUrl);	
	
	
}


//validation
function ValidateForm(element, errmsg, type){
	//return "1";
	var validate = (trim(document.getElementById(element).value) != "" && document.getElementById(element).value != null) ? 
			trim(document.getElementById(element).value) : 'x';
	if (validate == 'x') {
		alert(errmsg);
		document.getElementById(element).focus();
		return false;
	}
	else {
		return 	validate;
	}
}
//email validation
function validate_email(field,alerttxt)
{
	return true;
	with (field)
	{
		apos = value.indexOf("@");
		dotpos = value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
	  		{alert(alerttxt);field.focus(); return false;}
		else {return true;}
	}
}
//date validation - lets through 31 where not allowed!
function validateDate(fld) {
	 return true;
    var RegExPattern = /^([1-9]|0[1-9]|[12][0-9]|3[01])\D([1-9]|0[1-9]|1[012])\D(19[0-9][0-9]|20[0-9][0-9])$/;
    var errorMessage = 'Please enter valid date as day, month, and four digit year.\nYou may use a slash, hyphen or period to separate the values.\n\nFormat dd/mm/yyyy.';
    if ((fld.value.match(RegExPattern)) && (trim(fld.value)!='')) {
        //alert('Date is OK'); 
        return true;
    } else {
        alert(errorMessage);
        fld.focus();
        return false;
    } 
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

/** Chose Status **/

var entryid = 0;

function SetLP(entry){
	entryid = entry;
	//alert(entryid);
}


//update status
function ChangeStatus(status){
	
	if (entryid == 0){
		alert('Please choose an enty to edit');
		return false;
	}
	else{
		
		var page_request = false;
		var date = new Date();
		var timestamp = date.getTime();
	
		document.getElementById('txtmsg').innerHTML = 'Please Wait...';
		
		if (window.XMLHttpRequest) // if Mozilla, Safari etc
				page_request = new XMLHttpRequest()
		else if (window.ActiveXObject){ // if IE
			try {
				page_request = new ActiveXObject("Msxml2.XMLHTTP")
			}
			catch (e){
				try{
					page_request = new ActiveXObject("Microsoft.XMLHTTP")
				}
				catch (e){
					alert('sorry, your browser does not support ajax - please upgrade to a newer version');
				}
			}
		}else return false
	
		page_request.onreadystatechange=function(){
			if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
				var return_array = page_request.responseText.split(",");
				document.getElementById('txtmsg').innerHTML = return_array;
			}
		}
		var strUrl = "themes/chameleon/ajax/ajax.php?dowhat=CHANGE&time=" + timestamp + "&status=" + status + "&entryid=" + entryid;
		//alert (strUrl);
		page_request.open('GET', strUrl, true);
		page_request.send(null);
		}
}

// choose which to show
function ChooseStatus(status){
	
	var page_request = false;
	var date = new Date();
	var timestamp = date.getTime();

	document.getElementById('txtmsg').innerHTML = 'Please Wait...';
	
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
			page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){
				alert('sorry, your browser does not support ajax - please upgrade to a newer version');
			}
		}
	}else return false

	page_request.onreadystatechange=function(){
		if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)){
			var return_array = page_request.responseText.split(",");
			document.getElementById('txtmsg').innerHTML = return_array;
		}
	}
	var strUrl = "themes/chameleon/ajax/ajax.php?dowhat=ADMIN&time=" + timestamp + '&status=' + status;
	//alert (strUrl);
	page_request.open('GET', strUrl, true);
	page_request.send(null);
}