//--------------------------------------------------------------------------------------------------------
// Function to display countries in search drop down
//--------------------------------------------------------------------------------------------------------
var url = "trips_ajax_query.php?region="; // The server-side script 
function disp_countries(region, country) 
{  
	//alert(region);
	http.open("GET", url + escape(region) +'&country='+ escape(country)+'&action='+escape('ctry'), true); 
	http.onreadystatechange = handleHttpResponse; 
	http.send(null);
} 
function handleHttpResponse() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText; 
			//alert(results);
			document.getElementById('country_div').innerHTML = results;
		} 
	} 
}



//--------------------------------------------------------------------------------------------------------
// Function to display Duration in search drop down
//--------------------------------------------------------------------------------------------------------
var url2 = "trips_ajax_query.php?country="; // The server-side script 
function disp_duration(country, duration) 
{  
	//alert(country);
	http.open("GET", url2 + escape(country)+'&duration='+escape(duration) +'&action='+ escape('duration'), true); 
	http.onreadystatechange = handleHttpResponse2; 
	http.send(null);
} 
function handleHttpResponse2() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText; 
			//alert(results);
			document.getElementById('dur_div').innerHTML = results;
		} 
	} 
}


//--------------------------------------------------------------------------------------------------------
// Function add trips to members shortlist
//--------------------------------------------------------------------------------------------------------
var url3 = "trips_ajax_query.php?trip_id="; // The server-side script 
function add_shortlist(trip_id) 
{  
	//alert(trip_id);
	http.open("GET", url3 + escape(trip_id) +'&action='+ escape('shortlist'), true); 
	http.onreadystatechange = handleHttpResponse3; 
	http.send(null);
} 
function handleHttpResponse3() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText; 
			//alert(results);
			if(results == 'temptrue')
			{
				alert("Trip added to your shortlist!");
				document.getElementById('trip_det8').innerHTML = "<br />Trip added to your shortlist...";
				disp_div(8, 'trip_det', 8);
			}
			else if(results == 'exists')
			{
				alert ("Trip already in your shortlist!");
				document.getElementById('trip_det8').innerHTML = "<br />Trip already in your shortlist...";
				disp_div(8, 'trip_det', 8);
			}
			else
			{
				alert("Please login to add this trip to your shortlist...");
			}
		} 
	} 
}



//--------------------------------------------------------------------------------------------------------
// Function view details of shortlisted trips 
//--------------------------------------------------------------------------------------------------------
var url4 = "trips_ajax_query.php?trip_id="; // The server-side script 
function shrt_trip_det(trip_id) 
{  
	//alert(trip_id);
	http.open("GET", url4 + escape(trip_id) +'&action='+ escape('shrt_trip_details'), true); 
	http.onreadystatechange = handleHttpResponse4; 
	http.send(null);
} 
function handleHttpResponse4() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText; 
			//alert(results);
			document.getElementById('short_trip').innerHTML = results;
		} 
	} 
}


//--------------------------------------------------------------------------------------------------------
// Function to Delete trips from members shortlist
//--------------------------------------------------------------------------------------------------------
var url_del_trip = "trips_ajax_query.php?trip_id="; // The server-side script 
function del_shrt_trip(trip_id) 
{  
	//alert(trip_id);
	http.open("GET", url_del_trip + escape(trip_id) +'&action='+ escape('delete_trip'), true); 
	http.onreadystatechange = handleHttpResponse6; 
	http.send(null);
} 
function handleHttpResponse6() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText; 
			//alert(results);
			document.getElementById('short_trip_list').innerHTML = results;
		} 
	} 
}

//--------------------------------------------------------------------------------------------------------
// Function to validate a member
//--------------------------------------------------------------------------------------------------------
var url5 = "ajax_query.php?action="; // The server-side script 
function validate(type) 
{  
	var password = document.login_form.password.value;
	var email = document.login_form.email.value;
	//alert(password);
	//alert(email);
	//alert(type);
	http.open("GET", url5 + escape('verify') +'&password='+ escape(password)+'&email='+escape(email)+'&type='+escape(type), true); 
	http.onreadystatechange = handleHttpResponse5; 
	http.send(null);
} 
function handleHttpResponse5() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText; 
			//alert(results);
			if(results == 'invalid')
			{
				alert('Login Unsuccessful, invalid Email ID or Password');
				document.login_form.email.focus();
			}
			else
			{
				document.login_form.submit();
			}
		} 
	} 
}


//--------------------------------------------------------------------------------------------------------
// Function for captcha validation
//--------------------------------------------------------------------------------------------------------

var url7 = "ajax_query.php?entry_code="; // The server-side script 
function valid_code() 
{  
	//alert(document.login_form.entry_code.value);
	var entry_code = document.sign_up.entry_code.value;
	http.open("GET", url7 + escape(entry_code) +'&action='+ escape('chk_captcha'), true); 
	http.onreadystatechange = handleHttpResponse7; 
	http.send(null);
} 
function handleHttpResponse7() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText; 
			//alert(results);
			if(results == 'invalid')
			{
				document.getElementById("captcha").innerHTML = "*Invalid Entry, Please Enter Correct Code.";
			}
			else
			{
				if(document.getElementById("captcha").innerHTML != "")
				{
					document.getElementById("captcha").innerHTML = "";
				}
				document.sign_up.submit();
			}
		} 
	} 
}



//--------------------------------------------------------------------------------------------------------
// Function to check availability of username
//--------------------------------------------------------------------------------------------------------

var url_avail = "ajax_query.php?action="; // The server-side script 
function chk_avail(email) 
{  
	//var email = email;
	//alert(email);
	http.open("GET", url_avail + escape('availability')+'&email='+escape(email), true); 
	http.onreadystatechange = handleHttpResponse_avail; 
	http.send(null);
} 
function handleHttpResponse_avail() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText; 
			//alert(results);
			if(results == 'available')
			{
				//alert('Email Id available');
				document.getElementById("chk").innerHTML = "flag_true";
			}
			else if(results == 'unavailable')
			{
				alert('Email Id is not available. Please enter a different Email Id');
				if(document.getElementById('sign_up')!=null)
				{document.sign_up.email.focus();}
				else
				{document.edit_details.email.focus();}
				/*if(document.sign_up.email)
				{document.sign_up.email.focus();}
				else
				{document.edit_details.email.focus();}*/
				document.getElementById("chk").innerHTML = "flag_false";
			}
			else if(results == 'invalid')
			{
				alert('Invalid Email Id. Please enter full Email ID\nFor eg. username@abc.com');
				if(document.sign_up.email)
				{document.sign_up.email.focus();}
				else
				{document.edit_details.email.focus();}
				document.getElementById("chk").innerHTML = "flag_false";
			}
			
		} 
	} 
} 

//--------------------------------------------------------------------------------------------------------
// Function to Check password in 'Change password' Action
//--------------------------------------------------------------------------------------------------------

var url_chkpassword = "ajax_query.php?action="; // The server-side script 
function chkpassword() 
{  
	var curr_password = document.cng_pwd.curr_password.value;
	var new_password1 = document.cng_pwd.new_password1.value;
	var new_password2 = document.cng_pwd.new_password2.value;
	http.open("GET", url_chkpassword + escape('check_password')+'&curr_password='+escape(curr_password)+'&new_password1='+escape(new_password1)+'&new_password2='+escape(new_password2) , true); 
	http.onreadystatechange = handleHttpResponse_chkpassword; 
	http.send(null);
} 
function handleHttpResponse_chkpassword() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText; 
			//alert(results);
			if(results == 'auth_error')
			{
				alert('Authentication Error, please enter current password correctly');
				document.cng_pwd.curr_password.focus();
			}
			else if(results == 'blank_field')
			{
				alert ('Please enter new password twice');
			}
			else if(results == 'no_match')
			{
				alert ('Please enter same password in \'New Password\' and \'Re-enter new Password\' fields');
				document.cng_pwd.new_password1.value = "";
				document.cng_pwd.new_password2.value = "";
				document.cng_pwd.new_password1.focus();
			}
			else if(results == 'invalid')
			{
				alert('Invalid Password. Password can contain letters and numbers only');
				document.cng_pwd.new_password1.value = "";
				document.cng_pwd.new_password2.value = "";
				document.cng_pwd.new_password1.focus();

			}
			else 
			{
				document.cng_pwd.submit();
			}
		} 
	} 
}


//--------------------------------------------------------------------------------------------------------
// Function To Display Trip Amount
//--------------------------------------------------------------------------------------------------------

var url_trip_amt = "trips_ajax_query.php?action="; // The server-side script 
function view_tripamt() 
{  
	var schedule_id = document.tripbooking_form.schedule_id.value;
	//alert(schedule_id);
	http.open("GET", url_trip_amt + escape('view_trip_amt') +'&schedule_id='+ escape(schedule_id), true); 
	http.onreadystatechange = handleHttpResponse_tripamt; 
	http.send(null);
} 
function handleHttpResponse_tripamt() 
{    
	if (http.readyState == 4) 
	{ 
		if(http.status==200) 
		{ 
			var results=http.responseText; 
			//alert(results);
			document.getElementById("trip_amt").innerHTML = results;
		} 
	} 
}

//--------------------------------------------------------------------------------------------------------
// Functions To Display Divs
//--------------------------------------------------------------------------------------------------------

function disp_div(n, divname, count)
{
	for(i=1;i<=count;i++)
	{
		if (n==i)
		{
			showDiv(divname+n);
		}
		else
		{
			hideDiv(divname+i);
		}
	}
}
function showDiv(divnm)
{
		document.getElementById(divnm).style["display"]="block" ;
		document.getElementById(divnm).style["visibility"]="visible" ;
}
function hideDiv(divnm)
{
		document.getElementById(divnm).style["display"]="none" ;
		document.getElementById(divnm).style["visibility"]="hidden" ;
}



function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function chk_reg()
{
	//alert('chk_reg');
	if(document.search_form.region.value != "")
	{
		disp_countries(document.search_form.region.value, document.search_form.country.value);
	}
	if(document.search_form.country.value != "")
	{
		disp_duration(document.search_form.country.value, document.search_form.duration.value) 	
	}
}

//--------------------------------------------------------------------------------------------------------
// Functions To Validate Trip Booking Form
//--------------------------------------------------------------------------------------------------------

function validate_booking()
{
	var msg = "";
	
	if(document.tripbooking_form.schedule_id)
	{
		if (document.tripbooking_form.schedule_id.value == "")	
		{
			msg = msg+"Select Trip Date\n";
		}	
	}
	var flag = 0;
	for(var i=0; i<document.tripbooking_form.payment.length; i++)
	{
		if(document.tripbooking_form.payment[i].checked)
		{
			flag++;	
		}
	}
	if(flag == 0){msg = msg+"Select Payment Option\n";}
	if (document.tripbooking_form.name.value == "")	{msg = msg+"Name\n";}
	if (document.tripbooking_form.email.value == "")	{msg = msg+"Email\n";}
	if (document.tripbooking_form.address.value == "")	{msg = msg+"Address\n";}
	if (document.tripbooking_form.city.value == "")	{msg = msg+"City\n";}
	if (document.tripbooking_form.state.value == "")	{msg = msg+"State\n";}
	if (document.tripbooking_form.country.value == "")	{msg = msg+"Country\n";}
	if (document.tripbooking_form.pincode.value == "")	{msg = msg+"Pincode\n";}
	if (document.tripbooking_form.phone_no.value == "")	{msg = msg+"Phone no\n";}
	if (document.tripbooking_form.passport_no.value == "")	{msg = msg+"Passport No\n";}
	if (document.tripbooking_form.person_no.value == "")	{msg = msg+"No. of Persons\n";}
	var temp = /^([0-9]+)$/;
	var email_Regxp = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*\.([a-zA-Z]{2,6})$/;
	if(msg != "")
	{
		alert("Please fill following fields\n\n"+msg);
	}
	else if(email_Regxp.test(document.tripbooking_form.email.value) == false)
	{
		alert('Invalid Email Address');
		document.tripbooking_form.email.focus();
	}
	else if(temp.test(document.tripbooking_form.person_no.value) == false) 
	{
		alert('Enter Number of persons');
		document.tripbooking_form.person_no.focus();
	}
	else
	{
		document.tripbooking_form.submit();
	}
}

function flight_booking()
{
	window.open('book_flights.php','BookFlights','statusbar=0,height=500,width=380,resizable=yes,scrollbars=yes');
}

// This function is required to run the AJAX Function.
function getHTTPObject() 
{ 
  var xmlhttp; 
  if(window.XMLHttpRequest)
  { 
    xmlhttp = new XMLHttpRequest(); 
  } 
  else if (window.ActiveXObject)
  { 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    if (!xmlhttp)
	{ 
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
    } 
    
  } 
  return xmlhttp; 
} 
var http = getHTTPObject();
