// JavaScript Document
function logoff(formurl){
//alert(data);
$.ajax({ 
   type: "POST", 
   url: "logoff.php", 
   data: "op=logoff", 
   success: function(msg){ 
     //alert( "Data Saved: " + msg ); 
	 //alert(data);
	 //$("#tr_payoption").append(msg);
	 $("#error_label_login").html('logging you off ...').show();			
	 $("#form1").attr("action",formurl);
	 $("#form1").submit();
   } 
 });
}

function getdefaultcurrency(){
//alert(data);
$.ajax({ 
   type: "POST", 
   url: "utilities.php", 
   data: "op=currency", 
   success: function(msg){ 
     //alert( "Data Saved: " + msg ); 
	 //alert(data);
	 //$("#tr_payoption").append(msg);
	 $("#currency_code").html(msg);
	 $("#currency_code2").html(msg);
	 $("#currency_code3").html(msg);
	 $("#currency").get(0).value = msg;
   } 
 });
}
function getstates(){
var data = $("#countries").val();
//alert(data);
$.ajax({ 
   type: "POST", 
   url: "utilities.php", 
   data: "op=countries&country_code="+data, 
   success: function(msg){ 
     //alert( "Data Saved: " + msg ); 
	 //alert(data);
	 //$("#tr_payoption").append(msg);
	 $("#states").html(msg);
	 $("#country_name").get(0).value = $("#countries option:selected").text();
   } 
 });

}
function getstatename(){
	$("#state_name").get(0).value = $("#states option:selected").text();
}

function getnumrooms(){
var data = $("#roomt_type").val();
$.ajax({ 
   type: "POST", 
   url: "utilities.php", 
   data: "op=numrooms&roomt_type="+data, 
   success: function(msg){ 
     //alert( "Data Saved: " + msg ); 
	 //alert(data);
	 //$("#tr_payoption").append(msg);
	 $("#num_rooms").html(msg);
   } 
 });

}

function getpackageamount(){

$("#loading").ajaxStart(function(){ 
   $("#loading").css("loading_gif");
   $("#loading").html("please wait...");
 });

var data = $("#package_sel").val();
$.ajax({ 
   type: "POST", 
   url: "utilities.php", 
   data: "op=pkgamount&package_sel="+data, 
   success: function(msg){ 
     //alert( "Data Saved: " + msg ); 
	 //alert(msg);
	 //$("#tr_payoption").append(msg);
	 var pkgdata = msg.split(",");
	 //alert(pkgdata[0]+pkgdata[1]+pkgdata[2]+pkgdata[3]+pkgdata[4]+pkgdata[5]+pkgdata[6]);
	 	// Checking to see num of days if 0 means no rooms should be booked
		if(pkgdata[2]=='0'){
		 $("#roomt_type").hide();
		 $("#num_rooms").hide();
		 $("#roomtypespan").html("Room option not needed");
		 $("#roomnospan").html("");
		}else{
		 $("#roomt_type").show();
		 $("#num_rooms").show();
		 $("#roomtypespan").html("Select Room option");
		 $("#roomnospan").html("Select No. of Rooms");
		}
		
		// checking to see if only weekend booking is allowed
	 $("#pkg_amount").html(pkgdata[0]);
	 $("#pkg_amount_h").val(pkgdata[0]);
	 $("#package_num_days").val(pkgdata[1]);
	 $("#allow_room_option").val(pkgdata[2]);
	 $("#allow_only_weekend").val(pkgdata[3]);
	 $("#allow_friday").val(pkgdata[4]);
	 $("#allow_saturday").val(pkgdata[5]);
	 $("#allow_sunday").val(pkgdata[6]);
	 
   } 
 });

$("#loading").ajaxComplete(function(request, settings){ 
   $(this).html(""); 
 });

}

function getroomtypefee(){
var data = $("#roomt_type").val();
var data2 = $("#num_rooms").val();

$("#loading").ajaxStart(function(){ 
   $("#loading").css("loading_gif");
   $("#loading").html("please wait...");
 });

$.ajax({ 
   type: "POST", 
   url: "utilities.php", 
   data: "op=roomtypefee&roomt_type="+data+"&num_rooms="+data2, 
   success: function(msg){ 
     //alert( "Data Saved: " + msg ); 
	 //alert(msg);
	 //$("#tr_payoption").append(msg);
	 $("#roomfeedisplay").html(msg);
	 $("#roomt_type_fees").val(msg);
	 $("#total_amount").html(parseFloat($("#roomt_type_fees").val()) + parseFloat($("#pkg_amount_h").val()));
   } 
 });

$("#loading").ajaxComplete(function(request, settings){ 
   $(this).html(""); 
 });
}

function getmembercatamount(){
var data = $("#member_cat").val();
$.ajax({ 
   type: "POST", 
   url: "utilities.php", 
   data: "op=membercatamount&member_cat="+data, 
   success: function(msg){ 
     //alert( "Data Saved: " + msg ); 
	 //alert(msg);
	 //$("#tr_payoption").append(msg);
	 $("#memcat_amount").html(msg);
	 $("#memcat_amount_h").get(0).value = msg;
   } 
 });
}

function checkpackageexception(){
	if($("#package_num_days").val()=='3' && $("#allow_only_weekend").val()=='1'){
		if(checkpackagedateinoption()=='Friday' && checkpackagedateoutoption()=='Monday'){
			return true;
		}else{
			$("#error_label").html('This package can only be booked Weekends (Friday - Monday {3 Days in All})').show();
			$("#checkin_date").focus()
			return false;
		}
	}
	else if($("#package_num_days").val()=='2' && $("#allow_only_weekend").val()=='1'){
		if(checkpackagedateinoption()=='Friday' && checkpackagedateoutoption()=='Sunday'){
			return true;
		}else{
			$("#error_label").html('This package can only be booked Weekends (Friday - Sunday {2 Days in All})').show();
			$("#checkin_date").focus()
			return false;
		}
	}
	else{
		return true;
	}
}
function movenext(formurl){
//$("#form1").action = 'reservation_pdetails.php';
/*
$("#form1").validate({ 
  rules: { 
    checkin_date: { 
      required: true, 
      date: true 
    } 
  } 
});
*/
if($("#checkin_date").val()==''){
	$("#error_label").html('This field is required').show();
	$("#checkin_date").focus()
	return false;
}
if($("#checkout_date").val()==''){
	$("#error_label").html('This field is required').show();
	$("#checkout_date").focus()
	return false;
}
if(formurl=='reservation_pdetails.php'){
	if(!checkpackageexception()) return false;
}
if($("#package_sel").val()==''){
	$("#error_label").html('This field is required').show();
	$("#package_sel").focus()
	return false;
}
if($("#roomt_type").val()=='' && $("#allow_room_option").val()=='1'){
	$("#error_label").html('This field is required').show();
	$("#roomt_type").focus()
	return false;
}
if($("#num_rooms").val()=='' && $("#allow_room_option").val()=='1'){
	calldialog();
	$("#error_label").html('This field is required').show();
	return false;
}
if($("#lastname").val()==''){
	$("#error_label").html('This field is required').show();
	$("#lastname").focus()
	return false;
}
if($("#firstname").val()==''){
	$("#error_label").html('This field is required').show();
	$("#firstname").focus()
	return false;
}
if($("#middlename").val()==''){
	$("#error_label").html('This field is required').show();
	$("#middlename").focus()
	return false;
}
if($("#address").val()==''){
	$("#error_label").html('This field is required').show();
	$("#address").focus()
	return false;
}
if($("#city").val()==''){
	$("#error_label").html('This field is required').show();
	return false;
}
if($("#city").val()==''){
	$("#error_label").html('This field is required').show();
	return false;
}if($("#countries").val()==''){
	$("#error_label").html('This field is required').show();
	return false;
}
if($("#email").val()==''){
	$("#error_label").html('This field is required').show();
	return false;
}
if($("#mobile_no").val()==''){
	$("#error_label").html('This field is required').show();
	return false;
}
else {
	$("#form1").attr("action",formurl);
	$("#form1").submit();
}
}

function moveback(formurl){
	$("#form1").attr("action",formurl);
	$("#form1").submit();
}

function movenext_bookroom(formurl){
//$("#form1").action = 'reservation_pdetails.php';
/*
$("#form1").validate({ 
  rules: { 
    checkin_date: { 
      required: true, 
      date: true 
    } 
  } 
});
*/
if($("#checkin_date").val()==''){
	$("#error_label").html('This field is required').show();
	$("#checkin_date").focus()
	return false;
}
if($("#checkout_date").val()==''){
	$("#error_label").html('This field is required').show();
	$("#checkout_date").focus()
	return false;
}

if($("#roomt_type").val()==''){
	$("#error_label").html('This field is required').show();
	$("#roomt_type").focus()
	return false;
}
if($("#num_rooms").val()==''){
	calldialog();
	$("#error_label").html('This field is required').show();
	return false;
}
if($("#lastname").val()==''){
	$("#error_label").html('This field is required').show();
	$("#lastname").focus()
	return false;
}
if($("#firstname").val()==''){
	$("#error_label").html('This field is required').show();
	$("#firstname").focus()
	return false;
}
if($("#middlename").val()==''){
	$("#error_label").html('This field is required').show();
	$("#middlename").focus()
	return false;
}
if($("#address").val()==''){
	$("#error_label").html('This field is required').show();
	$("#address").focus()
	return false;
}
if($("#city").val()==''){
	$("#error_label").html('This field is required').show();
	return false;
}
if($("#city").val()==''){
	$("#error_label").html('This field is required').show();
	return false;
}if($("#countries").val()==''){
	$("#error_label").html('This field is required').show();
	return false;
}
if($("#email").val()==''){
	$("#error_label").html('This field is required').show();
	return false;
}
if($("#mobile_no").val()==''){
	$("#error_label").html('This field is required').show();
	return false;
}
else {
	$("#form1").attr("action",formurl);
	$("#form1").submit();
}
}

function checklogin(formurl){
		var data = $("#email").val();
		var data2 = $("#password").val();
		//alert(formurl);
		$.ajax({ 
		   type: "POST", 
		   url: "utilities.php", 
		   data: "op=checklogin&email="+data+"&password="+data2, 
		   success: function(msg){  
			 //alert(msg); 
			 $("#error_label_login").html('logging you in ...').show();
				if(msg=='0'){
					$("#error_label_login").html('Your Member profile has not been activated').show();
				}
				else if(msg!=''){	
					$("#member_data").val(msg);
					//alert($("#member_data").val());
				 $("#form1").attr("action",formurl);
				 $("#form1").submit();
				}
				else{
					$("#error_label_login").html('Invalid email or password').show();
				}
		   } 
		 });
		//alert(data);
}

function doregister(formurl){
if($("#lastname").val()==''){
	$("#error_label").html('Lastname is required').show();
	$("#lastname").focus();
	return false;
}
if($("#firstname").val()==''){
	$("#error_label").html('Firstname is required').show();
	$("#firstname").focus();
	return false;
}
if($("#middlename").val()==''){
	$("#error_label").html('Middlename is required').show();
	$("#middlename").focus();
	return false;
}
if($("#address").val()==''){
	$("#error_label").html('Address is required').show();
	$("#address").focus();
	return false;
}
if($("#city").val()==''){
	$("#error_label").html('City is required').show();
	$("#city").focus();
	return false;
}
if($("#state").val()==''){
	$("#error_label").html('State is required').show();
	$("#state").focus();
	return false;
}if($("#country").val()==''){
	$("#error_label").html('Country is required').show();
	return false;
}
if($("#email_address").val()==''){
	$("#error_label").html('Email address is required').show();
	$("#email_address").focus();
	return false;
}
if($("#sex").val()==''){
	$("#error_label").html('Select your gender').show();
	$("#sex").focus();
	return false;
}
if($("#chosen_password").val()==''){
	$("#error_label").html('Type in your prefered password').show();
	$("#error_label").focus();
	return false;
}
if($("#confirm_password").val()!=$("#chosen_password").val()){
	$("#error_label").html('Password is not the same').show();
	$("#confirm_password").focus();
	return false;
}
else {
	$("#form1").attr("action",formurl);
	$("#form1").submit();
}
}


function docurrency(formurl){
if($("#currency_code").val()==''){
	$("#error_label").html('Currency Code is required').show();
	$("#currency_code").focus();
	return false;
}
if($("#currency_id").val()==''){
	$("#error_label").html('Currency ID is required').show();
	$("#currency_id").focus();
	return false;
}
if($("#currency_name").val()==''){
	$("#error_label").html('Currency Name is required').show();
	$("#currency_name").focus();
	return false;
}
else {
	$("#form1").attr("action",formurl);
	$("#form1").submit();
}
}

function goFirst(){
	var lpage = parseInt($("#tpages").val());
	var fpage = parseInt($("#fpage").val());
	if(fpage!=1){
		$("#fpage").get(0).value = '1';
		$("#pageNo").get(0).value = 1;
		$("#form1").submit();
	}else{
		return false;
	}

}

function goLast(){
	var lpage = parseInt($("#tpages").val());
	var fpage = parseInt($("#fpage").val());
	if(lpage!=fpage){
		$("#fpage").get(0).value = lpage;
		$("#pageNo").get(0).value = lpage;
		$("#form1").submit();
	}else{
		return false;
	}

}
function goPrevious(){
	var lpage = parseInt($("#tpages").val());
	var fpage = parseInt($("#fpage").val());
	if(fpage !=1){
		$("#fpage").get(0).value = fpage-1;
		$("#pageNo").get(0).value = fpage-1;
		$("#form1").submit();
	}else{
		return false;
	}

}

function goNext(){
	var lpage = parseInt($("#tpages").val());
	var fpage = parseInt($("#fpage").val());
	if((lpage > fpage)){
		$("#fpage").get(0).value = fpage+1;
		$("#pageNo").get(0).value = fpage+1;
		$("#form1").submit();
	}else{
		return false;
	}

}

function doSearch(){
	$("#form1").submit();
}

function calldialog(){
var roomtypeval = 'Sorry there are no available rooms for '+$("#roomt_type option:selected").text();
$("#roomdialog").html(roomtypeval);
$("#roomdialog").dialog();
}

function calldialog_receipt(data){
//var roomtypeval = 'Sorry there are no available rooms for '+$("#roomt_type option:selected").text();
$("#receipt_div").html(data);
$("#receipt_div").dialog();
}

function calldialog_session(){
var data = 'You do not have a valid logon session';
$("#sessiondialog").html(data);
$("#sessiondialog").dialog({                                                           
							width: 600,                        
							modal: true,                        
							close: function(event, ui) {window.location.href = 'membership.php';}                        
							});
//window.location.href = 'membership.php';
}
function makepayment(formurl){
	var totalamt = $("#total_fee").val();
	var order_id = $("#TRANSACTION_ID").val();
	var descr = $("#descr").val();
	var payopt = $("input[name='payoption']:checked").val();
	alert(payopt);
	if(payopt=='etranzact'){
	$("#form1").attr("action",formurl);
	$("#form1").submit();
	}
	else if(payopt=='interswitch'){
	$("#form1").attr("action",'paymentgateway_isw.php');
	$("#form1").submit();
	}
	else if(payopt=='visa'){
	$("#form1").attr("action",'paymentgateway_visa.php');
	$("#form1").submit();
	}
	else{
		return false;
	}
}

function displayoption(){
	$("#container").addClass('hidediv');
}

function checkform(){
	var allInputs = $(":input"); 
	//alert(allInputs.length);
	//return false;
	var error_message = "";
	
	$(":input").each(
		function(){
			//access to form element via $(this)
			//alert($(this).attr('name'));
			if($(this).val()=='')error_message = error_message + $(this).attr('name')+" is required <br>";
		});
	if(error_message!=''){
		$("#error_label").html(error_message);
		return false;
	}
}

function checkpackagedateinoption(){
	var checkinvar = $("#checkin_date").val();
	var datearr = checkinvar.split("-");
	//alert(datearr[0].substring(0,4));
	var chkddatein = new Date(datearr[0],datearr[1]-1,datearr[2]);
	return getdayoftheweek(chkddatein.getDay());
	//alert(new Date().getDay());
}

function checkpackagedateoutoption(){
	var checkoutvar = $("#checkout_date").val();
	var datearr = checkoutvar.split("-");
	//alert(datearr[0].substring(0,4));
	var chkddateout = new Date(datearr[0],datearr[1]-1,datearr[2]);
	return getdayoftheweek(chkddateout.getDay());
}
function getdayoftheweek(day){
	var myDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	return myDays[day];
}

	$(function() {
		//$("#checkin_date").datepicker();
		var d = new Date();
		var curr_date = d.getDate();
		var curr_month = d.getMonth();
		var curr_year = d.getFullYear();
		var myStartDate = new Date(curr_year,curr_month,curr_date); 
		$("#checkin_date").datepicker({ showOn: 'both', buttonImageOnly: true, buttonImage: 'images/ew_calendar.gif', buttonText: 'date', dateFormat: $.datepicker.W3C, minDate: myStartDate});
	});
	
	$(function() {
		var d = new Date();
		var curr_date = d.getDate();
		var curr_month = d.getMonth();
		var curr_year = d.getFullYear();
		var myStartDate2 = new Date(curr_year,curr_month,curr_date); 
		$("#checkout_date").datepicker({ showOn: 'both', buttonImageOnly: true, buttonImage: 'images/ew_calendar.gif', buttonText: 'date', dateFormat: $.datepicker.W3C, minDate: myStartDate2 });
	});
	
	$(function() {
		//$("#checkin_date").datepicker();
		var d = new Date();
		var curr_date = d.getDate();
		var curr_month = d.getMonth();
		var curr_year = d.getFullYear();
		var myStartDate = new Date(curr_year,curr_month,curr_date); 
		$("#option_date").datepicker({ showOn: 'both', buttonImageOnly: true, buttonImage: 'images/ew_calendar.gif', buttonText: 'datepicker', dateFormat: $.datepicker.W3C, minDate: myStartDate });
	});