// JavaScript Document
var xmlHttp_obj=null;
var return_data;
var return_status;
function GetxmlHttp_object(){
	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 sendRequest(page,method,formname,divId){
	xmlHttp_obj=GetxmlHttp_object()
	if (xmlHttp_obj==null){
	  alert ("Your browser does not support AJAX!");
	  return false;
	} 
	var url=page;
	xmlHttp_obj.onreadystatechange=function(){stateChanged_fnc(divId)};
	//xmlHttp_obj.open("GET",url,true);
	//xmlHttp_obj.send(null);
	if(method == "POST"){
		xmlHttp_obj.open('POST',page,true);
		// set form http header
		xmlHttp_obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		// get form values and send http request
		xmlHttp_obj.send(getFormValues(formname));
	}else if(method == "GET"){
		xmlHttp_obj.open('GET',page,true);
		xmlHttp_obj.send(null);						
	}
}
function stateChanged_fnc(divId){ 
	if (xmlHttp_obj.readyState==1){
		if(divId=='txt'){	
			document.getElementById(divId).align='center';	
			document.getElementById(divId).style.color='red';	
			document.getElementById(divId).innerHTML='Processsing Please Wait';	
		}
	}
	if (xmlHttp_obj.readyState==4){ 
	 	document.getElementById(divId).innerHTML=xmlHttp_obj.responseText;	
	}
}
function getFormValues(fobjname){
	var fobj=document.getElementById(fobjname);
    var str='';
    for(var i=0;i< fobj.elements.length;i++){
       if(fobj.elements[i].type=="checkbox" ){
			if(fobj.elements[i].checked==true){
				str+=fobj.elements[i].name+'='+escape(fobj.elements[i].value)+'&';//escape		
			}
		}else{
        	str+=fobj.elements[i].name+'='+escape(fobj.elements[i].value)+'&';//escape
		}
    }
    str=str.substr(0,(str.length-1));
	//alert(str); 
    return str;
}
function displayServerResponse(){
	var output = xmlHttp_obj.responseText;	
	document.location.href = output;	
}
function next1(page){
	var modelVal = document.getElementById("model_id").options[document.getElementById("model_id").selectedIndex].value;
	if(modelVal == 0){
		alert("Please select a vehicle model");
		return;
	}else{
		url=relativeslash+'action.php?act=1&model='+document.getElementById('model_id').value;
		sendRequest(url,'GET','','tex');
	}	
}
function next2(){
	var id='';
	var arr=document.getElementById("service_id").value.split("_");
	var x =document.getElementById('chk').getElementsByTagName('input');
	for (var i=0; i < x.length; i++){
		if(x[i].id!='' && x[i].type=='checkbox' && x[i].checked==true){
			id=id+x[i].id+',';
		}
	}
	if(id=="" && arr[0]==0){
		alert("Please select atleast one servicing parameter");
	}else{
		document.getElementById("id").value=id;
		url=relativeslash+'action.php?act=3';
		sendRequest(url,'POST','frm1','main-area');
	}
}
function checkLength(o, len){
	if(o.value.length > len)
		o.value=o.value.substr(0,len);
}
function getQuote(){
	var id='';
	var x =document.getElementById('chk').getElementsByTagName('input');
	for (var i=0; i < x.length; i++){
		if(x[i].id!='' && x[i].type=='checkbox' && x[i].checked==true){
				id=id+x[i].id+',';
		}
	}	
	url=relativeslash+'action.php?act=2&service='+document.getElementById("service_id").value+'&id='+id;
	sendRequest(url,'GET','','txt');
}
function valid(){
	var errString="Your quote could not be submitted.\n Please correct the errors below and re-submit.\n";
	var retval=true;
	var theForm=document.frm1;
	if (theForm.registration.value=='') {
            retval=false;
            errString+= "\n\rvehicle registration missing";
    }	
	if (theForm.name.value==''){
            retval=false;
            errString+= "\n\rname missing";
    } 		
	if (theForm.email.value==''){
            retval=false;
            errString+= "\n\remail missing";
    }else{
		if((/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value)) == false){
            retval=false;
            errString+= "\n\remail address not valid";
		}
	}		
	if (theForm.phone.value==''){
            retval=false;
            errString+= "\n\rtelephone missing";
    }else{
		if((/^((\+[1-9][ \-]*)|(\([0-9]\)[ \-]*)|([0-9])[ \-]*)*?[0-9]?[ \-]*[0-9]?$/.test(theForm.phone.value)) == false){
            retval=false;
            errString+= "\n\rtelephone number not valid";
		}
	}	
	if (theForm.address1.value==''){
            retval=false;
            errString+= "\n\raddress missing";
    }
	if (theForm.county.value==''){
            retval=false;
            errString+= "\n\rcounty missing";
    }
	if (theForm.postcode.value==''){
            retval=false;
            errString+= "\n\rpostcode missing";
    }		
	if (retval) {
		return true;
	}else{
        alert(errString);
		return false;
	}	

}
function submitfrm(){
	if(valid()==true){
		url=relativeslash+'action.php?act=4';
		sendRequest(url,'POST','frm1','main-area');
	}
}
function validC(){
	var errString="Your quote could not be submitted.\n Please correct the errors below and re-submit.\n";
	var retval=true;
	var theForm=document.frm1;

	if (theForm.name.value==''){
            retval=false;
            errString+= "\n\rname missing";
    } 		
	if (theForm.email.value==''){
            retval=false;
            errString+= "\n\remail missing";
    }else{
		if((/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(theForm.email.value)) == false){
            retval=false;
            errString+= "\n\remail address not valid";
		}
	}		
	if (theForm.phone.value==''){
           /* retval=false;
            errString+= "\n\rtelephone missing";*/
    }else{
		if((/^((\+[1-9][ \-]*)|(\([0-9]\)[ \-]*)|([0-9])[ \-]*)*?[0-9]?[ \-]*[0-9]?$/.test(theForm.phone.value)) == false){
            retval=false;
            errString+= "\n\rtelephone number not valid";
		}
	}	
	if (retval) {
		return true;
	}else{
        alert(errString);
		return false;
	}	

}
function submitContact(){
	if(validC()==true){
		url=relativeslash+'action.php?act=5';
		sendRequest(url,'POST','frm1','msg');
	}
}
