// JavaScript Document
function ajaxFunction(queryString,file,output){
	var ajaxRequest; 
	try{
		// Opera 8.0+ & Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// IE Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				alert("browser exception 1.1");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById(output);
			//alert(ajaxRequest.responseText);
			//ajaxDisplay.innerHTML = "<option value='1'>Please Wait</option>";
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
		//else{
		//wait function will b called here...
		//document.getElementById(output).innerHTML = "<option>Please Wait...</option>";
		//}
	}
	ajaxRequest.open("GET", file + queryString, true);
	ajaxRequest.send(null);
}
function updateList(type,elemId){
	var queryString = "";
	var file = "ajaxPHPcall.php";
	var output = "div_"+elemId; //specify div id where menu to b created
	if(type == "AC"){
		queryString = "?func=getData4Combo&id="+elemId+"&type=lairport";	
	}else{
		queryString = "?func=getData4Combo&id="+elemId;
	}
	document.getElementById(output).innerHTML = "";
	ajaxFunction(queryString,file,output);
}
function updateRecord4Admin(id,elemId,response){
	var fares = document.getElementById(elemId).value;
	var queryString = "?func=updateRecord4Admin&id="+id+"&fares="+fares;
	var file = "ajaxPHPcall.php";
	var output = "message";
	ajaxFunction(queryString,file,output);
}
function deleteRecord4Admin(id,rowId){
	var queryString = "?func=deleteRecord4Admin&id="+id;
	var file = "ajaxPHPcall.php";
	var output = "message";
	ajaxFunction(queryString,file,output);	
	document.getElementById('myTable').deleteRow(rowId);
}
function appendOptionList(elemId,value){
  var newElem = document.createElement('option');
  newElem.text = value;
  newElem.value = value;
  var elemSel = document.getElementById(elemId);

  try {
    elemSel.add(newElem, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elemSel.add(newElem); // IE only
  }
}
function addRecord4CarType(){
	document.getElementById('message').style.color = "#006";
	var data = document.getElementById('txt_carType').value;
	var elemId = "carType2";
	var file = "ajaxPHPcall.php";
	var output = "message";
	var name = document.getElementById(elemId)[document.getElementById(elemId).selectedIndex].value;
	if(data == ""){
		document.getElementById('message').innerHTML = "Please fill in the required field";
		document.getElementById('message').style.color = "#900";
		return false;	
	}
	var queryString = "?func=addRecord4CarType&name="+data+"&table=cartypes";
	ajaxFunction(queryString,file,output);
	appendOptionList(elemId,data);//need to fix this 1 .. updae list even if the record exist
//working here
}
function editRecord4CarType(){
	//reset all
	document.getElementById('message').innerHTML = "";
	document.getElementById('message').style.color = "#006";

	var data = document.getElementById('txt_carType').value;
	var elemId = "carType2";
	var file = "ajaxPHPcall.php";
	var output = "message";
	var selValue = document.getElementById(elemId)[document.getElementById(elemId).selectedIndex].value;
	if(selValue == 0){
		document.getElementById('message').innerHTML = "please select cartype to be edit";
		return false;	
	}if(data == ""){
		document.getElementById('message').innerHTML = "Please fill in the required field";
		document.getElementById('message').style.color = "#900";
		//document.getElementById('txt_carType').style.border = "1px solid #900";
		return false;	
	}
	var queryString = "?func=editRecord4CarType&name="+selValue+"&newValue="+data+"&table=cartypes";
	ajaxFunction(queryString,file,output);
	document.getElementById(elemId)[document.getElementById(elemId).selectedIndex].value = data;
	document.getElementById(elemId)[document.getElementById(elemId).selectedIndex].text = data;
}
function delRecord4CarType(){
	document.getElementById('message').style.color = "#006";
	var elemId = "carType2";
	var file = "ajaxPHPcall.php";
	var output = "message";
	var name = document.getElementById(elemId)[document.getElementById(elemId).selectedIndex].value;
	if(name == 0){
		document.getElementById('message').innerHTML = "please select cartype to be deleted";
		return false;	
	}
	var queryString = "?func=deleteRecord4Admin&name="+name+"&table=cartypes";
	ajaxFunction(queryString,file,output);
	removeSelectedOption("carType2");
}
function removeSelectedOption(id)
{
  var selElem = document.getElementById(id);
  for (var i = selElem.length-1; i>=0; i--) {
    if (selElem.options[i].selected) {
      selElem.remove(i);
    }
  }
}
function addRecord4ToFrom(list,txtValue,locType){
	document.getElementById('message').style.color = "#006";
	var data = document.getElementById(txtValue).value;
	var elemId = list;
	var file = "ajaxPHPcall.php";
	var output = "message";
	var name = document.getElementById(elemId)[document.getElementById(elemId).selectedIndex].value;
	if(data == ""){
		document.getElementById('message').innerHTML = "Please fill in the required field";
		document.getElementById('message').style.color = "#900";
		return false;	
	}
	var queryString = "?func=addRecord4ToFrom&name="+data+"&table=airportcport"+"&locType="+locType;
	ajaxFunction(queryString,file,output);
	appendOptionList(elemId,data);//need to fix this 1 .. updae list even if the record exist
}
function editRecord4ToFrom(list,txtValue,locType){
	//reset all
	document.getElementById('message').innerHTML = "";
	document.getElementById('message').style.color = "#006";

	var data = document.getElementById(txtValue).value;
	var elemId = list;
	var file = "ajaxPHPcall.php";
	var output = "message";
	var selValue = document.getElementById(elemId)[document.getElementById(elemId).selectedIndex].value;
	if(selValue == 0){
		document.getElementById('message').innerHTML = "please select cartype to be edit";
		return false;	
	}if(data == ""){
		document.getElementById('message').innerHTML = "Please fill in the required field";
		document.getElementById('message').style.color = "#900";
		//document.getElementById('txt_carType').style.border = "1px solid #900";
		return false;	
	}
	var queryString = "?func=editRecord4CarType&name="+selValue+"&newValue="+data+"&table=airportcport"+"&locType="+locType;
	ajaxFunction(queryString,file,output);
	document.getElementById(elemId)[document.getElementById(elemId).selectedIndex].value = data;
	document.getElementById(elemId)[document.getElementById(elemId).selectedIndex].text = data;
}
function delRecord4ToFrom(list,txtValue,locType){
	document.getElementById('message').style.color = "#006";
	var elemId = list;
	var file = "ajaxPHPcall.php";
	var output = "message";
	var name = document.getElementById(elemId)[document.getElementById(elemId).selectedIndex].value;
	if(name == 0){
		document.getElementById('message').innerHTML = "please select cartype to be deleted";
		return false;	
	}
	var queryString = "?func=deleteRecord4Admin&name="+name+"&table=airportcport"+"&locType="+locType;
	ajaxFunction(queryString,file,output);
	removeSelectedOption(list);
}

function addUpdateFares(){ //frmLoc,toLoc,carType,txt_fares
	var frmLoc = document.getElementById('frmLoc')[document.getElementById('frmLoc').selectedIndex].value;
	var toLoc = document.getElementById('toLoc')[document.getElementById('toLoc').selectedIndex].value;
	var carType = document.getElementById('carType')[document.getElementById('carType').selectedIndex].value;
	var txt_fares = document.getElementById('txt_fares').value;
	
	var file = "ajaxPHPcall.php";
	var output = "message";
	var queryString = "?func=addUpdateFares&frmLoc="+frmLoc+"&toLoc="+toLoc+"&carType="+carType+"&txt_fares="+txt_fares;
	ajaxFunction(queryString,file,output);
	
}
function openNewWindow4Password(sid){
	var win = window.open('changepwd.php?sid='+sid,'changepassword','width=400,height=300,toolbar=no,resizable=no,menubar=no,location=no');
	win.focus();
}
function checkAllBoxes(){
	var total = document.getElementById('total_chkbox').value;
	if(document.getElementById('chkAll').checked == true){
		for(var i=0;i<total;i++){
			document.getElementById('chkAll_'+i).checked =true;
		}
	}else{
		for(var i=0;i<total;i++){
			document.getElementById('chkAll_'+i).checked =false;
		}
	}
}
function setStatusValue(val){
	var flag = 0;
	var total = document.getElementById('total_chkbox').value;
	document.getElementById('btnPressed').value = val;
	//validate inputs
	var frmLoc = document.getElementById('frmLoc').value;
	var toLoc = document.getElementById('toLoc').value;
	var carType = document.getElementById('carType').value;
	if(frmLoc == '0' || toLoc == '0' || carType == '0' || frmLoc == 'e' || toLoc == 'e' || carType == 'e'){
		//document.getElementById('message').innerHTML = "Please make the valide Selection"	
	}
	
	//--END---
	if(val == "delete"){
		for(var i=0;i<total;i++){
			if(document.getElementById('chkAll_'+i).checked ==true){
				flag = 1;
				break;
			}
		}
		if(flag>0){
			document.form1.submit();
		}else{
			document.getElementById('message').innerHTML = "No Record Selected";	
		}
	}else{
		document.form1.submit();
	}
}
function sendMailToClient(type){
	var carType = document.getElementById('carType').value;
	var hid_selectedFares = document.getElementById('hid_selectedFares').value;
	var orderdate = document.getElementById('orderdate').value;
	var nop = document.getElementById('nop').value;
	var locFrom = document.getElementById('locFrom').value;
	var locTo = document.getElementById('locTo').value;
	var txt_name = document.getElementById('txt_name').value;
	var txt_mNumber = document.getElementById('txt_mNumber').value;
	var sp_email = document.getElementById('sp_email').value;
	var txt_flightNum = document.getElementById('txt_flightNum').value;
	var ta_addressPickUp = document.getElementById('ta_addressPickUp').value;
	var txt_pc_from = document.getElementById('txt_pc_from').value;
	var ta_addressGoingTo = document.getElementById('ta_addressGoingTo').value;
	var txt_pc_to = document.getElementById('txt_pc_to').value;
	var gender = document.getElementById('gender').value;
	var ta_si = document.getElementById('ta_si').value;
	var process = 0;
	while(ta_si.search("&")!= -1){
		ta_si = ta_si.replace("&"," and ");
		process++;
		if(process>30)  //text to get out of infinite loop if something goes wrong
			break;
	}
	if(type == 'mail'){
		var file = "mail.php";
		var output = "message";
		var queryString = "?locFrom="+locFrom+"&locTo="+locTo+"&carType="+carType+"&hid_selectedFares="+hid_selectedFares+"&orderdate="+orderdate+"&nop="+nop+"&txt_name="+txt_name+"&txt_mNumber="+txt_mNumber+"&sp_email="+sp_email+"&txt_flightNum="+txt_flightNum+"&ta_addressPickUp="+ta_addressPickUp+"&txt_pc_from="+txt_pc_from+"&ta_addressGoingTo="+ta_addressGoingTo+"&txt_pc_to="+txt_pc_to+"&gender="+gender+"&ta_si="+ta_si;
	}else{
		var file = "ajaxPHPcall.php";
		var output = "message";
		var queryString = "?func=bookCar&locFrom="+locFrom+"&locTo="+locTo+"&carType="+carType+"&hid_selectedFares="+hid_selectedFares+"&orderdate="+orderdate+"&nop="+nop+"&txt_name="+txt_name+"&txt_mNumber="+txt_mNumber+"&sp_email="+sp_email+"&txt_flightNum="+txt_flightNum+"&ta_addressPickUp="+ta_addressPickUp+"&txt_pc_from="+txt_pc_from+"&ta_addressGoingTo="+ta_addressGoingTo+"&txt_pc_to="+txt_pc_to+"&gender="+gender+"&ta_si="+ta_si;

	}
	ajaxFunction(queryString,file,output);
}
function changeClass(num){
		if(num == 1)
			document.getElementById('submitForm').style.backgroundColor = "#AAB9DF";
		else
			document.getElementById('submitForm').style.backgroundColor = "#CFCFCF";
	}
function validateFields(){
/*		if(document.getElementById('selBox1').disabled == true || document.getElementById('selBox2').disabled == true){
			return false;
		}
*/		if(document.getElementById('locFrom').value == 0  || document.getElementById('locTo').value == 0){
			return false;	
		}
}
function validateFormSP(type){//validate fields
		var txt_name = document.getElementById('txt_name').value;
		var txt_mNumber = document.getElementById('txt_mNumber').value;
		var sp_email = document.getElementById('sp_email').value;
		var ta_addressPickUp = document.getElementById('ta_addressPickUp').value;
		var txt_pc_from = document.getElementById('txt_pc_from').value;
		var ta_addressGoingTo = document.getElementById('ta_addressGoingTo').value;
		var txt_pc_to = document.getElementById('txt_pc_to').value;
		var orderdate = document.getElementById('orderdate').value;
		if(txt_name == "" || txt_mNumber == "" || sp_email == "" || ta_addressPickUp == "" || txt_pc_from == "" || ta_addressGoingTo == "" || txt_pc_to == "" || orderdate == ""){
			var handle = document.getElementById('message');
			handle.innerHTML = "Please fill in all the required fields";
			handle.style.color = "#900";
			return false;
		}
		
		//merge date and time
		try{
			var dateTime1 = document.getElementById('sel_pickupTime_H').value;
			var dateTime2 = document.getElementById('sel_pickupTime_M').value;
			var dateTime3 = document.getElementById('sel_pickupTime_AMPM').value;
			var dateTime = orderdate+" "+dateTime1+":"+dateTime2+dateTime3;
			}catch(e){
				var temp1 = document.getElementById('pickupTime').value;
				var dateTime =  orderdate+" "+temp1;
			}
			document.getElementById('orderdate').value = dateTime;
		
		document.getElementById('bookSelected').disabled = "disabled";
		document.getElementById('ViewDetails').disabled = false;		
		document.getElementById('bookSelected').disabled = true;
		sendMailToClient('mail');
		//document.sp_form.submit;
		return ;
		//sendMailToClient('book');
}
function getSelectValue(fares,carType){
	document.getElementById('hid_selectedFares').value = fares;
	document.getElementById('carType').value = carType;
	
}
function setHiddenVar(price,carType){
		document.getElementById('hid_selectedFares').value = price;	
		document.getElementById('carType').value = carType;	
}
