var xmlHttp
var statusflag
var carbs = 0;
var carbstats
var price
var costfield
var tmpprice

function gencostcalc(){
	entered = document.getElementById("amountb").value;
	calcthis = Math.round(entered*100)/100;
	document.getElementById("vpc_Amount").value = calcthis * 100;
	//document.getElementById("Client_Name").value = calcthis * 100;
}

function baseCosts(vals){
	tmpprice = vals;
}

//get carbon
function getCarbon(status){ 

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var orig = document.getElementById("origin").value;
var dest = document.getElementById("dest").value;
var url="includes/utilities_misc.php"
url=url+"?dest="+dest
url=url+"&origin="+orig
url=url+"&stats="+status
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=showCarbon
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}


function showCarbon() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
eval(xmlHttp.responseText);
	document.getElementById("mocarb").innerHTML= "$ "+carbs;
	document.getElementById("ccreds").value= carbs;
}

}

//find trip by code
function tripCode(codes){

if (codes == 'x'){
var code = document.getElementById("find_trip").value;
} else {
var code = "";
var loadcode = codes;
}


xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="includes/utilities_order.php"
url=url+"?findtrip="+code
url=url+"&loadcode="+loadcode
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=showtrip
xmlHttp.open("GET",url,true)
xmlHttp.send(null)


}

var tripdetail;
var tripcost;
var tripided;
function showtrip() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
eval(xmlHttp.responseText);

document.getElementById("reptrip").innerHTML = tripdetail;

document.getElementById("hidetrip").value = tripided;

price = tripcost;

Costs();

}
	
}


//this functions stores the form data in the tmp tables
function loadTmp(fields,values){
	
if (response != 1){

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="includes/utilities_order.php"
url=url+"?fields="+fields
url=url+"&values="+values
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=loadValues
xmlHttp.open("GET",url,true)
xmlHttp.send(null)

}

}
var tripidform;
function loadValues() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	eval(xmlHttp.responseText);

	document.getElementById("hidetrip").value = tripidform;
	price = tripcost;
	
	if (price > 0){
	tmpprice = 0;
	}
	
	Costs();

}
	
}

//calculate cost here
function Costs(){
	if (document.getElementById("Personal_Porter_Required").value == 2){
	var porters = '660';
	} else {
	var porters = '0';
	}
	if (tmpprice > 0){
	price = tmpprice;
	}
	costfield = eval(document.getElementById("amountb").value);
	var running = eval(eval(price)+eval(porters)+eval(carbs));
	var rounding = eval(running*0.2);
	var basecosts = Math.round(rounding*100)/100;
	//if (costfield >= basecosts){
	//document.getElementById("amountb").value = costfield;
	//document.getElementById("vpc_Amount").value = costfield * 100;
	//} else {
	document.getElementById("amountb").value = basecosts;
	document.getElementById("vpc_Amount").value = basecosts * 100;
	//}

}

//move to next field
function moveNext(field,nextfield,ev){
  	if(field.value.length >= field.maxLength){
		document.getElementById(nextfield).focus();
	}
}

//set the date
function setsDate(){
	var months = document.getElementById("month").value;
	if (document.getElementById("month").value.length == "1"){
	var newmonth = "0"+months;
	} else {
	var newmonth = months;
	}
	var years = document.getElementById("year").value;
	if (document.getElementById("year").value.length == "1"){
	var newyear = "0"+years;
	} else {
	var newyear = years;
	}
	document.getElementById("vpc_CardExp").value = newyear+newmonth;
}

//check the cc count
function checkCount(field,ev){
  	if(field.value.length != field.maxLength){
		alert('You must enter 16 digits in the credit card number field!');
	}
}

//check the cc count
function doubleCheck(){
  	if(document.getElementById("vpc_CardNum").value.length != 16){
		document.getElementById("vpc_CardNum").focus();
	}
}



function tripids(status){
	document.getElementById("trip_id").disabled = status;
}


function goHere(path){
window.open(path, '_blank');
  return false;
}

function countDate(field,integ){
	if (document.getElementById(field).value.length == "1"){
	document.getElementById(field).value = "0"+integ;
	}
}

 function confirmDelete(delUrl,name_cat) { 
 if (confirm("Are you sure you want to delete this item from the database")) { 
 document.location = delUrl; 
 }
 }
 
 function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = "";
}


//load selected element type for selector
function getTrips(xpds,tripid)
{ 

if (tripid != 'x'){
	tripidform = tripid;
}

statusflag = xpds;

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="includes/utilities_order.php"
url=url+"?xpd="+xpds
url=url+"&tripid="+tripid
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=loadtrips
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function loadtrips() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 

	document.getElementById("iehack2367").innerHTML = xmlHttp.responseText;
	
	document.getElementById("Expedition").disabled = false;
	
	document.getElementById("Expedition").style.width = "300px";
	
	document.getElementById("hidetrip").value = tripidform;
	
	if (statusflag != 'x'){
	getReverse(statusflag);
	}
}

}

//ta check
function checkAgent(agentid)
{ 

if (agentid == 'x'){
var key = document.getElementById("find_agent").value;
} else {
var key = agentid;
}

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="includes/utilities_order.php"
url=url+"?key="+key
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=loadAgent
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

var agentid;
var agentitle;
var response;
function loadAgent() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	eval(xmlHttp.responseText);
	
	if (response == 1){
	document.getElementById("agent_id").value = agentid;

	document.getElementById("agent_name").innerHTML = '<b><font color="#009900">'+agentitle+'</font></b>';
	
	document.getElementById("payments").innerHTML = '<input type="button" name="Submit3" value="Place Booking &gt;&gt;" class="specbutton" onClick="formSubmit()"/>';
	
	} else {
	document.getElementById("agent_name").innerHTML = '<b><font color="#FF0000">No Account Found</font></b>';
	}
	
}

}


//load selected element type for selector
function getReverse(trips)
{ 

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="includes/utilities_order.php"
url=url+"?trips="+trips
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=loadXPDs
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function loadXPDs() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 

	document.getElementById("xpd").innerHTML = xmlHttp.responseText;
}

}

function SearchTrips(country){ 

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="includes/utilities_misc.php"
url=url+"?country="+country
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=findtrip
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function findtrip() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
	document.getElementById("trips").disabled = false;
	document.getElementById("grr").innerHTML = xmlHttp.responseText;
}

}





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;
}

//submit the form
function formSubmit(){
	if ((document.getElementById("termsbx").checked == true) && (document.getElementById("Client_Name").value != "") && (document.getElementById("em_name").value != "") && (document.getElementById("amountb").value > 0)  && (document.getElementById("vpc_Amount").value > 0)){
		document.getElementById("newbook").submit();
	} else {
		if (document.getElementById("termsbx").checked == false){
		alert('You must accept our terms and conditions to place a booking!');
		} else if (document.getElementById("Client_Name").value == ""){
		alert('You must include your name!');
		} else if (document.getElementById("em_name").value == ""){
		alert('You must include an emergency contact!');
		} else if ((document.getElementById("amountb").value < 1) || (document.getElementById("vpc_Amount").value < 1)){
		alert('Please re-select your trip, the correct values have not been set via Javascript!');
		} 
	}
}

//submit the form
function formSubmitgen(){
		gencostcalc();
		document.getElementById("genbook").submit();
}

function payAlert(type){
		if (type == 'cardfail'){
		alert('Your payment was rejected. Please try again!');
		} else if (type == 'agent'){
		alert('Your booking was successful!');
		}
}
