var xmlHttp

function clearStatusMsg($obj) {
	if($('#result-msg').html() != "") {
		$('#result-msg').html('');
	}
}

function includeData(url) {	
	var element = document.getElementById('wrapper');
	xmlHttp = GetXmlHttpObject();
	xmlHttp.open('GET', url, false);
    xmlHttp.send(null);
	element.innerHTML = xmlHttp.responseText;
  
}
function checkSubmit(v){
	var val = v;
	
	if (val=='null'){
		
		submitForm('quotation_action.php');	
		
	}else{
		
		v1	= checkFields('selCountry');
		v2	= checkFields('selCountry2');
		v3	= checkFields('txtFname');
		//v4	= checkFields('txtLname');
		v5	= checkFields('txtEmail');
		v6  = checkFields('txtContact1');
		v7  = checkFields('txtContact2');
		v8  = checkFields('txtAddress1');
		v9	= checkFields('selCity');
		v10	= checkFields('selCity2');
		v11	= checkFields('selKm');
		v12	= checkFields('selKm2');
		v13	= checkFields('txtMoveDate');
		
	
		if(v1 && v2 && v3 && v5 && v6 && v7 && v8 && v9 && v10 && v11 && v12 && v13){
			
			returnmuna('true');
			
		}else{
			returnmuna('false');
		}
		
		
	}
	
}
function returnmuna(retval){
	var returnval = document.getElementById('emukuKukunan');
	returnval.value = retval;
}

function mailIt(url){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return;
	}
	xmlHttp.open('GET', url, false);
	xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.send(null);
}

function submitForm(url)
{
	v1	= checkFields('selCountry');
	v2	= checkFields('selCountry2');
	v3	= checkFields('txtFname');
	//v4	= checkFields('txtLname');
	v5	= checkFields('txtEmail');
	v6  = checkFields('txtContact1');
	v7  = checkFields('txtContact2');
	v8  = checkFields('txtAddress1');
	v9	= checkFields('selCity');
	v10	= checkFields('selCity2');
	v11	= checkFields('selKm');
	v12	= checkFields('selKm2');
	v13	= checkFields('txtMoveDate');
	v14	= checkFields('txtPremoveDate');
	
	if(v1 && v2 && v3 && v5 && v6 && v7 && v8 && v9 && v10 && v11 && v12 && v13 && v14){
		returnmuna('true');
		var data	= " ";
	
		var elmInput	= document.getElementsByTagName('input');
		for (var i=0;i<elmInput.length;i++){
			if (elmInput[i].type!='button'){
				if(elmInput[i].type=='radio'){
					if(elmInput[i].checked)
						data	+= elmInput[i].name+"="+elmInput[i].value+"&";
						
				}else{
					data	+= elmInput[i].name+"="+elmInput[i].value+"&";	
				}
			}
		}
		
		var elmSelect	= document.getElementsByTagName('select');
		
		for (var j=0;j<elmSelect.length;j++){
			data	+= elmSelect[j].name+"="+encodeURIComponent(elmSelect[j].value)+"&";
		}
		
		var elmTextarea	= document.getElementsByTagName('textarea');
		for (var k=0;k<elmTextarea.length;k++){
			data	+= elmTextarea[k].name+"="+elmTextarea[k].value+"&";
		}
		if(typeof(addedparam)!='undefined' && typeof(fromtemp)!='undefined'){
			data += 'from='+fromtemp+'&adpar='+addedparam;
		}
		
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
	 	{
	 		alert ("Browser does not support HTTP Request")
	 		return;
	 	}
		var url=url;
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", data.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.send(data);
		
	}
}

function stateChanged() 
{ 
	var lang = $('#lang').val();	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 	{ 
 		
		
 		if (xmlHttp.responseText!='failed') {
 			window.location.href='thankyou_survey.php?lang='+lang;
 			document.getElementById('result-msg').innerHTML='';
 		
 			
 		}else{
 			alert(xmlHttp.responseText);	
 		}
 		
 	} 
}


function btnshow(){

	document.getElementById('result-msg').style.display = "none";
	document.getElementById('btnsnd').style.display 	= "";
}
// onclick submit button validator
function checkFields(id){	
	
	var errormsg	= document.getElementById('result-msg');
	//alert(id);
		var elmV	= document.getElementById(id);
		var emlValue = elmV.value;
		var elmid		= id;
		
		var elmErrorHL	= document.getElementById(elmid);
		if(emlValue==''){
			elmErrorHL.style.border='1px solid #FF8F8F';
			errMsg	= 'Please fill out the Required Field(s).';
			errormsg.innerHTML = errMsg;
			returnmuna('false');
			return false;
		} else {		
			
			if(elmid=='txtContact1' ||  elmid=='txtContact2') {
				if(isNaN(document.getElementById(elmid).value)){
					elmErrorHL.style.border='1px solid #FF8F8F';
					errMsg	= 'Invalid entry';
					errormsg.innerHTML = errMsg;
					return false;
				}
			}
			
			elmErrorHL.style.border='';
			errormsg.innerHTML = '';
		}
	
	return true;
}
// onblur event validator
function valueChecker(){	
	
	var emlValue	= this.value;
	var elmid		= this.id;
	
	var elmErrorHL	= document.getElementById(elmid);
	
	var errormsg	= document.getElementById('result-msg');
	if(emlValue==''){
		elmErrorHL.style.border='1px solid #FF8F8F';
		elmErrorHL.style.opacity='0.999999';
		errMsg	= 'Please fill out the Required Field(s).';
		errormsg.innerHTML = errMsg;
		return false;
	} else {	
		
		if(elmid == 'txtPremoveDate' || elmid == 'txtMoveDate'){
			
			elmErrorHL.style.border='';
			errormsg.innerHTML = '';
		}
		
		if(elmid=='txtContact1' ||  elmid=='txtContact2') {
				if(isNaN(document.getElementById(elmid).value)){
					elmErrorHL.style.border='1px solid #FF8F8F';
					errMsg	= 'Contact: Invalid entry';
					errormsg.innerHTML = errMsg;
					return false;
				}
			}
			else{
			elmErrorHL.style.border='';
			errormsg.innerHTML = '';
			
			}
					
		elmErrorHL.style.border='';
		errormsg.innerHTML = '';
	}
	return true;
}

function checkmail() {
	
	var noerr		= true;	
	var emailfilter	= /^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
	var elmVal		= this.value;

	var elmid		= this.id;

	if(elmVal==''){
		noerr	= false;
		document.getElementById("result-msg").innerHTML	= "Please fill out the Required Field(s).";
		document.getElementById(elmid).style.border='1px solid #FF8F8F';
	} 
	
	if(elmVal!=''){
		if (elmVal.search(emailfilter) == -1) 
		{
			document.getElementById("result-msg").innerHTML	= "Invalid e-mail address.";
			document.getElementById(elmid).style.border='1px solid #FF8F8F';
			noerr	= false;
		} else {
			document.getElementById("result-msg").innerHTML	= '';
			document.getElementById(elmid).style.border = '';
		}
	}
	
	return noerr;

}

addEvent(window,'load',function()
{
    initForm();    
	
});

