function backToTop() {

    var x1 = x2 = x3 = 0;

    var y1 = y2 = y3 = 0;



    if (document.documentElement) {

        x1 = document.documentElement.scrollLeft || 0;

        y1 = document.documentElement.scrollTop || 0;

    }



    if (document.body) {

        x2 = document.body.scrollLeft || 0;

        y2 = document.body.scrollTop || 0;

    }



    x3 = window.scrollX || 0;

    y3 = window.scrollY || 0;



    var x = Math.max(x1, Math.max(x2, x3));

    var y = Math.max(y1, Math.max(y2, y3));



    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));



    if (x > 0 || y > 0) {

        window.setTimeout("backToTop()", 25);

    }

}
function calculate()
{
	var items=document.getElementsByName("items[]");
	var quants=document.getElementsByName("quantities[]");
	var total=0;
	var j=0;
	for(j=0;j<items.length;j++)
	{
		var temp=(items[j].value).split(",");
		if(quants[j].value!="")
			total+=temp[1]*100*(quants[j].value);	
	}
	total=(total/100);
	document.getElementById("total").value=total;
	document.getElementById("totalcu").value=total;
	
}
function checkValid()
{
	if(DetailCheck())
	{
		var items=document.getElementsByName("items[]");
		var quants=document.getElementsByName("quantities[]");
		var msg="";
		for(j=0;j<quants.length;j++)
		{
			var temp=(items[j].value).split(",");
			if((isNaN(quants[j].value))&&(quants[j].value!=""))
				msg+="-"+temp[0]+"\n";
		}
		msg+="has to be Numeric!";
		if(msg!="has to be Numeric!")
		{
			alert(msg);
			return false;
		}
		else
			return true;
	}
	else
		return false;
}
function DetailCheck()
{
	var msg="Sorry:\n";
	if(document.Enquiryform.username.value=="")
		msg+="-Name is required.\n";	
	if(document.Enquiryform.telephone.value=="")
		msg+="-Telephone is required.\n";
	if(document.Enquiryform.email.value==null||document.Enquiryform.email.value=="")
		msg+="-Email is required.\n";	
	else
	{
		if(!echeck(document.Enquiryform.email.value))
			msg+="-Please enter a valid email.\n";	
	}
	if(document.Enquiryform.departure.value=="")
		msg+="-Departure is required.\n";
	if(document.Enquiryform.location.value=="")
		msg+="-Location is required.\n";
	if(document.Enquiryform.destination.value=="")
		msg+="-Destination is required.\n";
	if(msg!="Sorry:\n")
	{
		alert(msg);
		return false;
	}
	else
		return true;
}
function Gonext()
{
	if(checkValid())
	{
		calculate();
		window.document.getElementById("cal_btn").style.display="none";
		window.document.getElementById("sub_btn").style.display="block";
		alert("Please click the submit button to send your quotation enquiry.");
	}
}
function Va_onChange()
{
	window.document.getElementById("sub_btn").style.display="none";
	window.document.getElementById("cal_btn").style.display="block";
}
function onSub()
{
	if(document.getElementById("total").value!=0)
	{
		if(checkValid())
		{
			calculate();	
			return true;
		//	return confirm("Are you sure?");
		}
		else
		{
			Va_onChange();
			return false;
		}
	}
	else
	{
		alert("You need to add a quantity for at least one item!");
		Va_onChange();
		return false;
	}
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
function ContactformCheck()
{
	var msg="Sorry:\n";
	if(document.contactForm.username.value=="")
		msg+="-Name is required.\n";	
	if(document.contactForm.email.value==null||document.contactForm.email.value=="")
		msg+="-Email is required.\n";	
	else
	{
		if(!echeck(document.contactForm.email.value))
			msg+="-Please enter a valid email.\n";	
	}
	if(document.contactForm.message.value=="")
		msg+="-Message is required.\n";
	if(document.contactForm.publicKey.value=="")
		msg+="-Security Code is required.\n";
	if(msg!="Sorry:\n")
	{
		alert(msg);
		return false;
	}
	else
		return true;
}
