allowSubmit = true;

function submitForm(m, y) {
    if (allowSubmit) {
        String.prototype.trim = function(){return this.replace(/^\s*|\s*$/g,"");}
        var errstr = "";

        if (!document.getElementById('tsandcs').checked)
            errstr += "You have not accepted our terms and conditions\n";
        if (document.getElementById('title').value.length < 2)
            errstr += "Please enter your title correctly\n";
        if (document.getElementById('first').value.length < 1)
            errstr += "Please enter your first name correctly\n";
        if (document.getElementById('last').value.length < 1)
            errstr += "Please enter your surname correctly\n";
        if (document.getElementById('house').value.length < 1)
            errstr += "Please enter your house number/name correctly\n";
        if (document.getElementById('address').value.length < 4)
            errstr += "Please enter your address correctly\n";
        if (document.getElementById('city').value.length < 3)
            errstr += "Please enter your city correctly\n";
        if (document.getElementById('post_code').value.length < 2)
            errstr += "You have not entered your postcode correctly\n";
        if (document.getElementById('driverphone').value.length < 8)
            errstr += "You have not entered your phone number correctly\n";
        email = document.getElementById('email').value.trim();
        email2 = document.getElementById('email2').value.trim();
        if (email != email2)
            errstr += "Your email address was entered differently in the two boxes - please check\n";
        else if (!email.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/))
            errstr += "Your email address is invalid\n";
		if (document.getElementById('cardType').value.length <  1)
            errstr += "Please enter your credit card type\n";
        if (document.getElementById('cardNum').value.length < 12)
            errstr += "You have not entered your credit card number correctly\n";
        var theCardType = document.getElementById('cardType').options[document.getElementById('cardType').selectedIndex].text;
		if (theCardType.toLowerCase().indexOf('switch') != -1){
			if (!document.getElementById('issueNum').value.match(/^\d{1,3}$/)){
				errstr += "Please enter your issue number\n";		
			}
		}
		if (document.getElementById('cardName').value.length < 4)
            errstr += "Please enter the name on your credit card correctly\n";
        if (document.getElementById('card_end_month').value == "null" || document.getElementById('card_end_year').value == "null")
            errstr += "Please enter the expiry date on your credit card correctly\n";
        if (document.getElementById('card_end_month').value < m && document.getElementById('card_end_year').value == y)
            errstr += "The expiry date of your credit card is in the past\n";
        if (document.getElementById('ccv').value.length < 3)
            errstr += "Please enter your security code correctly\n";		
/*        if (document.getElementById('extras_ad') && document.getElementById('extras_ad').checked) {
            if (document.getElementById('ad1_title').value.length < 2)
                errstr += "Please enter the additional driver's title correctly\n";
            if (document.getElementById('ad1_first').value.length < 1)
                errstr += "Please enter the additional driver's first name correctly\n";
            if (document.getElementById('ad1_last').value.length < 1)
                errstr += "Please enter the additional driver's surname correctly\n";
        }
        if (document.getElementById('extras_sad') && document.getElementById('extras_sad').checked) {
            if (document.getElementById('ad2_title').value.length < 2)
                errstr += "Please enter the second additional driver's title correctly\n";
            if (document.getElementById('ad2_first').value.length < 1)
                errstr += "Please enter the second additional driver's first name correctly\n";
            if (document.getElementById('ad2_last').value.length < 1)
                errstr += "Please enter the second additional driver's surname correctly\n";
        }*/
        if (errstr.length == 0) {
            allowSubmit = false;
            	elmLoop(2);
            //document.getElementById('form1').action = "https://www.citrox.co.uk/booking.php?book=reference";
//            document.getElementById('form1').submit();
			return true;
        } else
            alert(errstr);
    }
	return false;
}

var xmlHttp
function elmLoop(submitVal)
{

	var theForm = document.forms[0]
	var urlText = ""
	for(i=0; i<theForm.elements.length; i++)
	{
		if(theForm.elements[i].id != "cardType" && theForm.elements[i].id != "cardNum" && theForm.elements[i].id != "issueNum" && theForm.elements[i].id != "cardName" && theForm.elements[i].id != "card_start_month" && theForm.elements[i].id != "card_start_year" && theForm.elements[i].id != "card_end_month" && theForm.elements[i].id != "card_end_year" && theForm.elements[i].id != "ccv")
		{
			urlText += theForm.elements[i].id + "="
			//   urlText += theForm.elements[i].value + "&"
			
			if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button" || theForm.elements[i].type == "hidden")
			{
				urlText += theForm.elements[i].value + "&"
			}
			else if(theForm.elements[i].type == "checkbox")
			{
				urlText += theForm.elements[i].checked + "&"
			}
			else if(theForm.elements[i].type == "select-one")
			{
				urlText += theForm.elements[i].options[theForm.elements[i].selectedIndex].value + "&"
			}
		}
		//alert(alertText)
	}
	//alert(urlText)
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="formAbandon.php"
	url=url+"?"+urlText
	url=url+"sid="+Math.random()
	url=url+"&submit="+submitVal
	xmlHttp.onreadystatechange=stateChangedFormAbandon
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
 
} 

function stateChangedFormAbandon()
{
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  {
    //alert(xmlHttp.responseText)
    document.getElementById('faID').value = xmlHttp.responseText;
  }
}

function GetXmlHttpObject()
{
  var objXMLHttp=null
  if (window.XMLHttpRequest)
  {
    objXMLHttp=new XMLHttpRequest()
  }
  else if (window.ActiveXObject)
  {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  return objXMLHttp
}
