// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 11;

function validate()
{
	//var rad_val;
	//for (var i=0; i < document.competitionForm.radiobutton.length; i++)
	//{
	//	if (document.competitionForm.radiobutton[i].checked)
	//	{
	//		rad_val = document.competitionForm.radiobutton[i].value;
	//	}
	//}
	//
	//if(rad_val == undefined)
	//{
	//	alert("No answer selected!\nPlease try again!");
	//	return 0;
	//}
	
	var nameText = document.competitionForm.nameText.value;
	var emailText = document.competitionForm.emailText.value;
	var phoneText = document.competitionForm.phoneText.value 
	var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	
	if(nameText == "")
	{
		alert("Please enter your name.");
		return 0;
	}
	
	if(emailText == "")
	{
		alert("Please enter an email address.");
		return 0;
	}
	else if (emailText.search(validRegExp) == -1)
	{
		alert("A valid e-mail address is required.\nPlease check you have entered your details correctly.");
		return 0;
	} 
	
	
	 if (phoneText != "")
	 {
	 	if (checkInternationalPhone(phoneText)==false)
	 	{
	 	alert("A valid phone number is required.\nPlease check you have entered your details correctly.")
	 	document.competitionForm.phoneText.value="";
	 	document.competitionForm.phoneText.focus()
		return false;
	 	}
	 }

	var addCheck;
	if(document.competitionForm.addCheck.checked == true)
	{
		addCheck = true;
	}
	else
	{
		addCheck = false;
	}

	// var interestCheck;
	// if(document.competitionForm.interestCheck.checked == true)
	// {
	// 	interestCheck = true;
	// }
	// else
	// {
	// 	interestCheck = false;
	// }
	
	//alert(rad_val + "\n" +nameText + "\n" + emailText + "\n" + phoneText + "\n" + addCheck + "\n" + interestCheck);
	
	var xmlSubmit = GetXmlHttpObject();
	if (xmlSubmit==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	// create url	
	var url="submitform.php";
	url=url+"?nameText="+ nameText ;
	url=url+"&emailText="+	emailText ;
	url=url+"&phoneText="+ phoneText;
	// url=url+"&answer="+ rad_val;
	url=url+"&addCheck="+	addCheck;
	// url=url+"&interestCheck="+ interestCheck;
	url=url+"&sid="+Math.random();
	
	// fire open
	xmlSubmit.open("GET",url,true);
	xmlSubmit.send(null);
	
	xmlSubmit.onreadystatechange=function()
	{
		if(xmlSubmit.readyState==4)
		{
			document.getElementById("formArea").innerHTML = xmlSubmit.responseText;
		}
	}
	
} 

function isInteger(s)
{
	var i;
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}

function stripCharsInBag(s, bag)
{
	var i;
	var returnString = "";
	
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function checkInternationalPhone(strPhone)
{
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

// function to initialise XML HTTP Request for AJAX
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}