/*********************************************** Common function for ajax start here **********************************************************************************/
// create http object for ajax use


function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function getHttpObject()
{  var xmlHttp;
   // Firefox, Opera 8.0+, Safari
	try { xmlHttp=new XMLHttpRequest(); } 
	  	catch (e) {    // Internet Explorer    
	  		try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } 
	  			catch (e) { 
	  				try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
						catch (e) { alert("Your browser does not support AJAX! IE is highly recommended to use this feature !!");  return false; 
						} 
					}
	}
	return xmlHttp;
}
// get http response for requested query
function getResponse(URL, Method)
{
	var strResponseText;
	var objHTTP = getHttpObject();
	if (!objHTTP) { // if browser does not support AJAX end program here and return error
			return false;
	}
	
	objHTTP.open(Method, URL, false); 
	objHTTP.send(null); 	 // send http request 	
	if(objHTTP.readyState == 4)
	{				
		 strResponseText = objHTTP.responseText; //return response text	
		 return strResponseText;
	}	
}

/*********************************************** Common function for ajax end here **********************************************************************************/


// General functions //
// add option element to the select element
function addOption(obj, strText, mixValue)
{
	var newOption =document.createElement('option');
		
  		newOption.text 	= strText;
		newOption.value = mixValue;
		try
		{
		obj.add(newOption, null); // standards compliant
		}
	  	catch(ex)
		{
		obj.add(newOption);  // IE only
		}
		
}
// remove a single option from the list of select box
function removeOption(obj, index)
{
	obj.remove(index);
}
// Remove all the elements form the list of select box //
function removeAllOptions(obj)
{	
  var loopLength = obj.length;
  for (var i=0; i < loopLength;i++) {
	  removeOption(obj, obj.options[i]);  
  }
}
// General functions end here //
/*********************************************** functions for search form 'country city list popup' start here **********************************************************************************/
// get list of countries for a region
function getCountries(region)
{
	var Method = "GET"; // set method of request for http
	var URL = "getResponse.php?region=" + region;	// Http request URL	
	var strResponseText = getResponse(URL, Method);	// get Response result from the server for the requested value	
	return strResponseText;

}
// set the list of countries to the list box
function setCountryList(region)
{
	var strOuterHTML 	= "";
	var strCountryList 	= getCountries(region);
	var arryCountryList = strCountryList.split('||');
	var objCountry		= document.getElementById("country");
	removeAllOptions(objCountry); // remove all the options first //
	addOption(objCountry, 'Select Country', "");
	//================================Add by kulvinder ===========================================
	var objcity		= document.getElementById("city");
	removeAllOptions(objcity); // remove all the options first //
	addOption(objcity, 'Select City', "");
	//==========================================================================================
	//strOuterHTML = '<select name="country" class="comboBox1" id="country"  onChange="setCityList(this.value)"><option value=""> Select Country </option>';
	for (var i=0;i < arryCountryList.length; i++) {
		if (arryCountryList[i] != "") {				
			arryCountry = arryCountryList[i].split('|');	
			if (arryCountry.length==2) {
				//strOuterHTML += '<option value="' + arryCountry[0] + '">' + arryCountry[1] + '</option>';
				addOption(objCountry, arryCountry[1], arryCountry[0]);
				
			}
		}		
	}	
	//objCountry.outerHTML = strOuterHTML + '</select>';
}

// get list of cities for a country
function getCities(country)
{
	var Method = "GET"; // set method of request for http
	var URL = "getResponse.php?country=" + country;	// Http request URL
	var strResponseText = getResponse(URL, Method);	// get Response result from the server for the requested value
	return strResponseText;
}
// set the list of cities to the list box
function setCityList(country)
{
	
	var strOuterHTML = "";
	var strCityList  = getCities(country);
	var arryCityList = strCityList.split('||');	
	var objCity		= document.getElementById("city");
	removeAllOptions(objCity); // remove all the options first //	
	//var strOuterHTML = '<select name="city" class="comboBox1" id="city"><option value=""> Select City </option>';
	addOption(objCity, 'Select City', "");
	for (var i=0;i < arryCityList.length; i++) {
		if (arryCityList[i] != "") {
			arryCity = arryCityList[i].split('|');			
			if (arryCity.length==2) {
				//strOuterHTML += '<option value="' + arryCity[0] + '">' + arryCity[1] + '</option>';
				addOption(objCity, arryCity[1], arryCity[0]);
			}
		}		
	}
	
	//objCity.outerHTML = strOuterHTML + '</select>';
}
/*********************************************** functions for search form 'country city list popup' end here **********************************************************************************/

/**************************************************** Search result page AJAX functionalites Start Here ************************/ 


function getTripRateOptions(rate_id, trip, markup, exchange, cur_type, currency)
{
	var Method = "GET"; // set method of request for http
	var URL = "getResponse.php?rate_id=" + rate_id + '&trip=' + trip + '&markup=' + markup + '&exchange=' + exchange + '&cur_type=' + cur_type + '&currency=' + currency;	// Http request URL
	var strResponseText = getResponse(URL, Method);	// get Response result from the server for the requested value
	return strResponseText;
}
function setTripRateOptions(rate_id, trip, markup, exchange, cur_type, currency)
{

	var strOptions 		= "";
	var strTableID 		= "tripOptions_" + trip;
	
	strOptions		  	= getTripRateOptions(rate_id, trip, markup, exchange, cur_type, currency); // get the response result
	
	arryOptions			= strOptions.split('|=|'); // explode the result with this parameter
	strOptions			= arryOptions[0]; // lunch, guide, entry fee options
	
	CheckBoxesArr  = strOptions.split("|");
	mainId = document.getElementById(strTableID);
	if (navigator.appName === "Microsoft Internet Explorer") {
	mainId.innerText = "";
	}
	else
	{
	mainId.innerHTML = "";
	}
	var tbdy = document.createElement("tbody");
	
	for(k=0;k<CheckBoxesArr.length - 1;k++)
	{
		checkBoxElement = CheckBoxesArr[k].split(",");
	//	alert(checkBoxElement[0]);
		if(trim(checkBoxElement[0]) != '')
		{
		var tr = document.createElement("tr");
		var td = document.createElement("td");
		var td2 = document.createElement("td");

		checkBoxElement = CheckBoxesArr[k].split(",");
		var check1 =document.createElement("input");
		check1.type = "checkbox";
		check1.name = trim(checkBoxElement[0]);
		check1.id = trim(checkBoxElement[0]);
//		check1.setAttribute("onClick",checkBoxElement[1]);

		var funct = checkBoxElement[1];
		check1.onclick = function() {eval(funct)}
		check1.value = "1";
		//alert(check1.name);

		var textOfCheckBox = document.createTextNode(checkBoxElement[2]);

		td.appendChild(check1);
		td2.appendChild(textOfCheckBox);

		tr.appendChild(td);
		tr.appendChild(td2);
		tbdy.appendChild(tr);
		}
	}
		
	    mainId.appendChild(tbdy);	

	arryOptions	= arryOptions[1].split('||'); // explode the value to set hidden field value order upto person, price, lunch rate, guide rate, entry fee//
	// set hidden field value according to current options choosed //
	
	var objFrm	= eval("document.frmTrip_" + trip);
	eval('objFrm.upto_persons' + trip).value= Math.round(parseInt(arryOptions[0]));
	eval('objFrm.price' + trip).value 		= Math.round(parseInt(arryOptions[1]));
	eval('objFrm.lunch' + trip).value		= Math.round(parseInt(arryOptions[2]));
	eval('objFrm.guide' + trip).value		= Math.round(parseInt(arryOptions[4]));
	eval('objFrm.entry' + trip).value		= Math.round(parseInt(arryOptions[3]));
	
	// hidden variable value block end here //
	if (navigator.appName === "Microsoft Internet Explorer") {
		//document.getElementById(strTableID).outerHTML = '<table width="100%" cellpadding="0" cellspacing="0" id="tripOptions_' + trip + '" name="tripOptions_' + trip + '">' + strOptions + '</trip>';	
 	} else {
		//document.getElementById(strTableID).innerHTML = strOptions;
	}
	if (calcGroupTotal(trip) == false) {
		var objRad = eval('objFrm.trip_rate_' + trip);
		for (var i=0;i<eval('objFrm.trip_rate_' + trip).length;i++) {			
			if (objRad[i].checked == true) {
				objRad[i].checked = false;
			}
		}		
		return false;
	}
}
/**************************************************** Search result page AJAX functionalites End Here *****************************************/ 


function change_currency(frm, actionURL)
{
	frm.action = actionURL;
	frm.submit();
}
// for person based trip //
function validatePerson(trip, min_price, min_person)
{	
	var frm = eval("document.frmTrip_" + trip);
	var per_person_price = 0;
	var num_person = eval('frm.persons_' + trip).value;
	var cur_type   = eval('frm.cur_mode_' + trip).value;
	if (isNaN(num_person) || num_person <= 0) {
		alert("Please enter a valid number of persons !");
		eval('frm.persons_' + trip).focus();
		return false;
	}
	
	if (min_person > parseInt(num_person)) {
		per_person_price =  Math.round(min_price / num_person);
		if (!confirm("You have entered less than minimum number of person !! Price for per person will be " + per_person_price + " (" + cur_type + ") per person ! Do you want to continue ?")) {
				return false;
		} else {
			frm.action = "book.php";
			eval('frm.total_' + trip).value  = Math.round(per_person_price * num_person);
			//eval('frm.total_' + trip).focus();
		}
	} else {
			per_person_price = eval('frm.per_person_price_' + trip).value;
			frm.action = "book.php";
			eval('frm.total_' + trip).value  = Math.round(per_person_price * num_person);
	}
	frm.submit();
}
// for person based trip
function calcTotal(trip, min_price, min_person)
{	
	var frm = eval("document.frmTrip_" + trip);
	var per_person_price = 0;
	var num_person = eval('frm.persons_' + trip).value;
	var cur_type   = eval('frm.cur_mode_' + trip).value;
	/*if (isNaN(num_person) || num_person <= 0) {
		alert("Please enter a valid number of persons !");
		eval('frm.persons_' + trip).focus();
		return false;
	}*/
	
	if (min_person > parseInt(num_person)) {
		per_person_price =  Math.round(min_price / num_person);
		//if (!confirm("You have entered less than minimum number of person !! Price for per person will be " + per_person_price + " (" + cur_type + ") per person ! Do you want to continue ?")) {
		//		return false;
		//} else {
			
			eval('frm.total_' + trip).value  = Math.round(per_person_price * num_person);
			//eval('frm.total_' + trip).focus();
		//}
	} else {
			per_person_price = eval('frm.per_person_price_' + trip).value;
			eval('frm.total_' + trip).value  = Math.round(per_person_price * num_person);
			//eval('frm.total_' + trip).focus();			
	}
	return false;
}



// for group based trip
function validateGroup(trip)
{
	var frm = eval("document.frmTrip_" + trip);
	var per_person_price = 0;
	var num_person = eval('frm.persons_' + trip).value;
	var cur_type   = eval('frm.cur_mode_' + trip).value;

	var objRad = eval('frm.trip_rate_' + trip);
	if (isNaN(num_person) || num_person <= 0) {
		alert("Please enter a valid number of persons !");
		eval('frm.persons_' + trip).focus();
		if (objRad.checked==true) {
			objRad.checked=false;
		}
		return false;
	}

	var sel = false;
	var objRad = eval('frm.trip_rate_' + trip);
	if (!isNaN(objRad.length)) {
		for (var i=0;i<eval('frm.trip_rate_' + trip).length;i++) {		
			if (objRad[i].checked == true) {
				sel = true;
			}
		}
	} else if (objRad.checked==true) {
		sel = true;
	}
	if (sel === false) {
		alert("Please choose group size !");
		return false;
	}

	var max_person	= parseInt(eval('frm.upto_persons' + trip).value, 0);
	var group_price = parseInt(eval('frm.price' + trip).value, 0);
	var lunch_price = parseInt(eval('frm.lunch' + trip).value, 0);
	var guide_price = parseInt(eval('frm.guide' + trip).value, 0);
	var entry_price = parseInt(eval('frm.entry' + trip).value, 0);
	if (num_person > max_person) {
		alert('You have enter number of persons more than the maximum limit of group please choose suitable group option to continue');
		return false;
	} else {
			var totalPrice = group_price;
						if (navigator.appName === "Microsoft Internet Explorer")
		{
				var compareWith = "[object]";
		}
		else
		{
			 var compareWith = "[object HTMLInputElement]";
		}
			if(document.getElementById("lunch_"+trip) == compareWith) {
				
					totalPrice +=(document.getElementById("lunch_"+trip).checked === true)?(lunch_price*num_person):(0);
				}
			if(document.getElementById("entry_"+trip) == compareWith) {
					totalPrice +=(document.getElementById("entry_"+trip).checked === true)?(entry_price*num_person):(0);
				}
			
			if (document.getElementById("guide_" + trip)== compareWith) {
					totalPrice +=(document.getElementById("guide_" + trip).checked === true)?(guide_price):(0);
				}	
				
			eval('frm.total_' + trip).value = Math.round(totalPrice);
			frm.action = "book.php";			
	}
	frm.submit();
}
// for group based trip
function calcGroupTotal(trip)
{
	var frm = eval("document.frmTrip_" + trip);
	var per_person_price = 0;
	var num_person = eval('frm.persons_' + trip).value;
	var cur_type   = eval('frm.cur_mode_' + trip).value;
	
	var objRad = eval('frm.trip_rate_' + trip);
	if (isNaN(num_person) || num_person <= 0) {
		alert("Please enter a valid number of persons !");
		eval('frm.persons_' + trip).focus();
		if (objRad.checked==true) {
			objRad.checked=false;
		}
		return false;
	} 
	
	var sel = false;
	var objRad = eval('frm.trip_rate_' + trip);
	if (!isNaN(objRad.length)) {
		for (var i=0;i < objRad.length;i++) {		
			if (objRad[i].checked == true) {
				sel = true;			
			}
		}
	} else if (objRad.checked==true) {
		sel = true;
	}

	if (sel === false) {
		alert("Please choose group size !");
		return false;
	}
	
	var max_person	= parseInt(eval('frm.upto_persons' + trip).value, 0);
	var group_price = parseInt(eval('frm.price' + trip).value, 0);
	
		
	if (num_person > max_person) {
		alert('You have enter number of persons more than the maximum limit of group please choose suitable group option to continue');
		return false;
	} else {
			var lunch_price = parseInt(eval('frm.lunch' + trip).value, 0);
			var guide_price = parseInt(eval('frm.guide' + trip).value, 0);
			var entry_price = parseInt(eval('frm.entry' + trip).value, 0);
					
			var totalPrice = Math.round(group_price);
			
			if (navigator.appName === "Microsoft Internet Explorer")
		{
				var compareWith = "[object]";
		}
		else
		{
			 var compareWith = "[object HTMLInputElement]";
		}
			if(document.getElementById("lunch_"+trip) == compareWith) {
				
					totalPrice +=(document.getElementById("lunch_"+trip).checked === true)?(lunch_price*num_person):(0);
				}
			if(document.getElementById("entry_"+trip) == compareWith) {
					totalPrice +=(document.getElementById("entry_"+trip).checked === true)?(entry_price*num_person):(0);
				}
			
			if (document.getElementById("guide_" + trip)== compareWith) {
					totalPrice +=(document.getElementById("guide_" + trip).checked === true)?(guide_price):(0);
				}	
			/*	if (eval("frm.lunch_" + trip) == "[object]") {
					totalPrice +=(eval("frm.lunch_" + trip).checked === true)?(lunch_price*num_person):(0);
				}
				if (eval("frm.entry_" + trip)=="[object]") {
					totalPrice +=(eval("frm.entry_" + trip).checked === true)?(entry_price*num_person):(0);
				}
				if (eval("frm.guide_" + trip)=="[object]") {
					totalPrice +=(eval("frm.guide_" + trip).checked === true)?(guide_price):(0);
				}				*/
			eval('frm.total_' + trip).value = Math.round(totalPrice);
			//eval('frm.total_' + trip).focus();
	}
	return true;
}