var xmlHttp
var problemArray = new Array();

function dispSubmit(field,action){
    if(problemArray.length != 0){
        for(var i = 0; i < problemArray.length; i++){
            if(problemArray[i] == field){    
                place = i;
                break;
            }else{
                place = -12;
            }
        }
    }else{
        place = -50;
    }
    if(action == "add" && place < 0){
        problemArray.splice(problemArray.length,0,field);		
    }else if(action == "remove" && place >= 0){
        problemArray.splice(place,1);
    }
}

function submitForm(){
	if(problemArray.length != 0){
        alert("Pleasse fill in all required fields.")
    }else{
		if (validateAll()== 0){
			if (confirm('Submit details?') == true) {
        		document.forms['quoteRequestform'].submit()
			}
		}else {
			alert("Pleasse fill in all required fields.")
		}
    }	
	return false
}

function validateAll(){	
	validate_field_All("surname")
	validate_field_All("firstname")
	validate_field_All("policy")
	validate_field_All("postal")
	validate_field_All("email")
	validate_field_All("mobile")
	
	return problemArray.length
}

function validate_field(txtfield)
{  
    var url2=""
	if (txtfield == "surname"){
		var x = document.getElementById('surname').value
        url2 = "field="+txtfield+unescape("%26surname=")+x			
    }else if (txtfield == "firstname"){
		var x = document.getElementById('firstname').value
        url2 = "field="+txtfield+unescape("%26firstname=")+x
    }else if (txtfield == "email"){
		var x = document.getElementById('email').value
        url2 = "field="+txtfield+unescape("%26email=")+x			
    }else if (txtfield == "mobile"){
		var x = document.getElementById('mobile').value
        url2 = "field="+txtfield+unescape("%26mobile=")+x			
    }else if (txtfield == "postal"){
		var x = document.getElementById('postal').value
        url2 = "field="+txtfield+unescape("%26postal=")+x			
    }else if (txtfield == "policy"){
		var x = document.getElementById('policyno1').value
		var y = document.getElementById('policyno2').value
		var z = document.getElementById('policyno3').value
		url2 = "field=policy"+unescape("%26policy1=")+x+unescape("%26policy2=")+y+unescape("%26policy3=")+z
    }	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) 	{
  		alert ("Browser does not support HTTP Request")
  		return
  	} 
	var url="./includes/checkValidN.php?"
	url+=url2
	url+="&sid="+Math.random()	
	//alert(url)
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
} 

function stateChanged() 
{  
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		
   		var response = xmlHttp.responseText;
        var UPDATE = new Array();
   		<!--LOOP THROUGH THE RESPONSES-->
		//alert(response)		
        if(response.indexOf('|' != -1)) {
   		    UPDATE = response.split('|');
       		if(UPDATE[1] != '!error'){                		
           		document.getElementById(UPDATE[0]+'_container').innerHTML = UPDATE[1];
           		dispSubmit(UPDATE[0],'add');
       		}else if(UPDATE[1] == '!error'){ 
			    document.getElementById(UPDATE[0]+'_container').innerHTML = "";
				dispSubmit(UPDATE[0],'remove');				
       		}
   		}
	}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

/*******************************************************************/
function validate_field_All(txtfield)
{ 
	if (txtfield == "surname" || txtfield == "firstname" || txtfield == "email" || txtfield == "postal" || txtfield == "mobile"){
		var x = document.getElementById(txtfield).value
        if(x == ""){  
        	document.getElementById(txtfield+'_container').innerHTML = "Required";
        	dispSubmit(txtfield,'add');
       	}
    }else if (txtfield == "policy"){
		var x = document.getElementById('policyno1').value
		var y = document.getElementById('policyno2').value
		var z = document.getElementById('policyno3').value
		if (x == "" || y == "" || z ==""){
			document.getElementById('policy_container').innerHTML = "Invalid policy number";
        	dispSubmit('policy','add');
		}
    }
} 