// CHECH AVAILABILITY VALIDATOR V1.0
function check_availability_validator(type){
	$('input[type=submit]').attr("disabled", "true");	
	$("#availability_form").bind("keypress", function(e) {	if (e.keyCode == 13) return false;	});
	
	$("#calendar1").html('').fadeIn(1000);
	$("#calendar2").html('').fadeIn(1000);
	var arrival_date 		= $('#arrival_date').val();
	var departure_date 		= $('#departure_date').val();
	var number_of_nights 	= $("#number_of_nights").val();
	
	////////////////////////////////////////////////////////// OUTPUT
	$.get('plug/checkavailability.validator.php', {
		arrival_date: arrival_date,
		departure_date: departure_date,
		number_of_nights: number_of_nights,
	},function(data){
		var success = new String(data);
		var result = success.split(';'); // split on commas
		if(result.length == 1){
			$("#number_of_nights").val(result[0]);
			$("#number_of_nights_summary").text(result[0]);
		}else{
		$("#number_of_nights").val(result[1]);
			$("#number_of_nights_summary").text(result[1]);
			$("#departure_date").val(result[0]);
			$("#departure_date_summary").text(result[0]);
		}				
		$("#arrival_date_summary").text(arrival_date);		
		//$("#departure_date_summary").text(departure_date);		
		check_availability();	
	});
	$('input[type=submit]').removeAttr("disabled");
	$("#availability_form").bind("keypress", function(e) {	if (e.keyCode == 13) return true;	});
}
// CHECH AVAILABILITY V1.0	
function check_availability(){
	$('input[type=submit]').attr("disabled", "true");	
	$("#availability_form").bind("keypress", function(e) {	if (e.keyCode == 13) return false;	});
	
	var arrival_date 		= $("#arrival_date").val();
	var departure_date 		= $("#departure_date").val();
	var number_of_nights 	= parseInt($("#number_of_nights").val());
	var property_id		 	= $("#property_id option:selected").val();
	
	////////////////////////////////////////////////////////// OUTPUT
	$.get('check-availability-ajax.php', {
		arrival_date: arrival_date,
		departure_date: departure_date,
		number_of_nights: number_of_nights,
		property_id: property_id,
	},function(data){
		var result = data.split('%'); // split on commas
		$('#calendar1 td').each(function(){ $(this).style=''; });
		$('#calendar2 td').each(function(){ $(this).style=''; });
		if(result[0] == 0){
			$("#availability_checker_value").val(0);
			var css_position	= "80px 81px";
		}else{
			$("#availability_checker_value").val(1);
			var css_position	= "0px 0px";
		}
		$(".calendar-block").html(result[1]).css("display","none");
		$(".calendar-block").html(result[1]).fadeIn(1000);
		number_of_nights++;
		number_of_nights++;	
		for (ctr=2; ctr<=number_of_nights; ctr++){
			$(result[ctr]).css("background-position",css_position);
			if(result[0] == 0){
				//$(result[ctr]).text('');
			}
		} 
	});
	
	$("#arrival_date_summary").text(arrival_date);
	$("#departure_date_summary").text(departure_date);
	
	$('input[type=submit]').removeAttr("disabled");
	$("#availability_form").bind("keypress", function(e) {	if (e.keyCode == 13) return true;	});
}

function property_icon_viewer(){
	$('input[type=submit]').attr("disabled", "true");	
	$("#availability_form").bind("keypress", function(e) {	if (e.keyCode == 13) return false;	});
	
	var property_id = $('#property_id option:selected').val();
	
	if(property_id==1){
		$('#property_code').val(1);
		$('#property2').fadeOut();
		$('#property1').fadeIn();
	}else if(property_id==2){
	$('#property_code').val(2);
		$('#property1').fadeOut();
		$('#property2').fadeIn();
	}else{
	$('#property_code').val(3);
		$('#property1').fadeOut();
		$('#property2').fadeOut();
	}
	check_availability();
	$('input[type=submit]').removeAttr("disabled");
	$("#availability_form").bind("keypress", function(e) {	if (e.keyCode == 13) return true;	});
}

// GALLERY V1.0
function gallery_viewer(id){
	$('#featured-ads-wrapper').hide();
	$('.gallery-wrapper-inner').each(function() {
		$(this).hide();
	})
	$('#gallery-block'+id).fadeIn();
	
	var galleryheight = $('#gallery-block'+id+' .see-included-table').height() + $('#gallery-block'+id+' .gallery-description').height();
	$('#gallery-block'+id).css('height',+galleryheight+'px');
	
}
function close_gallery(){
	$('.gallery-wrapper-inner').each(function() {
		$(this).hide();
	})
	
	$('.right-panel').width('675px');
	
	$('#featured-ads-wrapper').fadeIn();
	$('.left-panel').fadeIn();
	
	
	
	//$('.left-panel select').attr('disabled',true);
	
	
	
}

// PROCESS RESERVED ITEMS V1.0
function process_reserved_items(){
	$('#infopreloader').fadeIn();
	$('#infopreloader').fadeTo(1000, 0.50);
	$('input[type=submit]').attr("disabled", "true");	
	$("#guest_information_form").bind("keypress", function(e) {	if (e.keyCode == 13) return false;	});
	
	var property_id			= $("#property_id").val();
	var amount_paid			= $("#amount_paid").val();
	var amount_balance		= $("#amount_balance").val();
	var salutation			= $("#salutation option:selected").val();
	var firstname			= $("#firstname").val();
	var lastname			= $("#lastname").val();
	var birthdate			= $("#birthdate").val();
	var gender				= $("#gender:checked").val();
	var phone_number		= $("#phone_number").val();
	var phone_number_ext	= $("#phone_number_ext").val();
	var mobile_number		= $("#mobile_number").val();
	var fax_number			= $("#fax_number").val();
	var email_address		= $("#email_address").val();
	
	var street_address		= $("#street_address").val();
	var address				= $("#address").val();
	var city				= $("#city").val();
	var state_province		= $("#state_province").val();
	var country_id			= $("#country_id option:selected").val();
	var request_comments	= $("#request_comments").val();
	var payment_method		= $("#payment_method").val();
	
	var is_login			= $("#is_login").val();
	
	var password			= $("#password").val();
	
	
	var _terms_agreement	= document.getElementById('terms_agreement').checked; 
	var terms_agreement		= 0;
	
	if(_terms_agreement == true)
		terms_agreement = 1;
	
	$.get('plug/process.reserved.items.php', {
		property_id: property_id,
		amount_paid: amount_paid,
		amount_balance: amount_balance,
		salutation: salutation,
		firstname: firstname,
		lastname: lastname,
		birthdate: birthdate,
		gender: gender,
		phone_number: phone_number,
		phone_number_ext: phone_number_ext,
		mobile_number: mobile_number,
		fax_number: fax_number,
		email_address: email_address,
		street_address: street_address,
		address: address,
		city: city,
		state_province: state_province,
		country_id: country_id,
		request_comments: request_comments,
		payment_method: payment_method,
		password: password,
		terms_agreement: terms_agreement,
		is_login: is_login,
	},function(data){
		if(data=='saved'){
			document.forms["guest_information_form"].submit();
		}else{
			$('#infopreloader').fadeOut();
			return false;
		}
	});
	
	$('input[type=submit]').removeAttr("disabled");
	$("#guest_information_form").bind("keypress", function(e) {	if (e.keyCode == 13) return true;	});
}


// LOGIN CHEKER V1.0
function systemloginchanger(value){
	if ( value == 1 ){
		$('#account_number_block').fadeIn();
	}else{
		$('#account_number_block').fadeOut();
	}
}

function systemloginchecker(){
	$('#infopreloader').fadeIn();
	$('#infopreloader').fadeTo(1000, 0.50);
	
	var tester			= 0;
	var username 		= $('#orsusername').val();
	//var account_number 	= $('#orsaccount_number').val();
	var password 		= $('#orspassword').val();
	//var corporate		= $('input:radio[name=orscorporate]:checked').val();
	
	
	//if(corporate==0){
		if(username!="" || password!=""){	tester++;	}
	//}else{
	//	if(username!="" || password!="" || account_number!=""){	tester++;	}
	//}
	
	if(tester > 0){
		$.get('plug/login.checker.php', {
			username: username,
			account_number: 0,
			password: password,
			corporate: 0,
		},function(data){
			var result = data.split(':'); // split on colon
				_href 	= $(location).attr('href');
	
			if(result[0]== "yes"){
				
				if( result[2] == "0" && _href.search('guest-information.php') != -1 )
					location.href = "check-availability.php";
				else
					location.href = "check-availability.php";
					
				$.facebox.close();
				
			}else{
				$('#loginresponse').html('<b>INVALID USERNAME/PASSWORD</b>');
			}
			$('#infopreloader').fadeOut();
			
		});
	}else{
		$('#loginresponse').html('<b>USERNAME/PASSWORD REQUIRED</b>');
		$('#infopreloader').fadeOut();
	}
}
// PROFILE FETCHER V1.0
function fetchguestprofile(id){
	$.get('plug/fetch.guest.profile.php', {
		id: id,
	},function(data){
		var result = data.split(':'); // split on colon
		$('#salutation').val(result[0]);
		$('#firstname').val(result[1]);
		$('#lastname').val(result[2]);
		$('#birthdate').val(result[3]);
		$('#gender').val(result[4]);
		$('#phone_number').val(result[5]);
		$('#phone_number_ext').val(result[6]);
		$('#mobile_number').val(result[7]);
		$('#fax_number').val(result[8]);
		$('#email_address').val(result[9]);
		$('#confirm_email_address').val(result[10]);
		$('#street_address').val(result[11]);
		$('#address').val(result[12]);
		$('#city').val(result[13]);
		$('#state_province').val(result[14]);
		$('#country_id').val(result[15]);
		
	});
}


// TRANSPORTATION VAL SETTERS V1.0
function transportation_trigger(type){
	var lastarrayctr  = parseInt($('#lastarrayctr').val());
	
	if(type==1){ // IF ARRIVAL
		var checkbox_id = '#avail_pickup';
		var checker_id  = '#avail_pickup_checker';
		var block		= 'arrival';
	}else{  // IF DEPARTURE
		var checkbox_id = '#avail_dropoff';
		var checker_id  = '#avail_dropoff_checker';
		var block		= 'departure';
	}
	
	var check = $(checkbox_id).is(':checked'); 
	if(check==true){
		$(checker_id).val(1);
		$('#'+block+'_input_block :input').removeAttr('disabled');
		$('#'+block+'_input_block :select').removeAttr('disabled');
		$('#transportation_paypal_input_container_for'+block).html('');
		
		lastarrayctr++;
	}else{
		$('#'+block+'_input_block :input').attr('disabled', true);
		$('#'+block+'_input_block :select').attr('disabled', true);
		$('#transportation_paypal_input_container_for_'+block).html('');
		
		$(checker_id).val(0);
		lastarrayctr--;
	}
	$('#lastarrayctr').val(lastarrayctr);
}

function transportation_calculation(block){
	var type = 1;
	var lastarrayctr = $('#lastarrayctr').val();
	if(block==1){ // IF ARRIVAL
		var ref_id = 'arrival';
	}else{  // IF DEPARTURE
		var ref_id = 'departure';
	}
	
	var transportservice_id = $('#'+ref_id+'_transportservice_id option:selected').val();
	var transportvehicle_id = $('#'+ref_id+'_transportvehicle_id option:selected').val();
	var qty 				= $('#'+ref_id+'_vehicle_qty').val();
	var flightno			= $('#'+ref_id+'_flightno').val();
	var time				= $('#'+ref_id+'_time').val();
	
	// PROCESSI INPUTS
	if(transportservice_id > 0 && transportvehicle_id > 0 && qty > 0){
		$('#infopreloader').fadeIn();
		$('#infopreloader').fadeTo(1000, 0.50);
	
		// CALCULATION PROCESS
		$.get('plug/transport.setter.php', {
			transportvehicle_id: transportvehicle_id,
			transportservice_id: transportservice_id,
			qty: qty,
			type: type,
			block: block,
			lastarrayctr: lastarrayctr,
			flightno: flightno,
			time: time
		},function(data){
			var result = data.split('~@~'); // split on colon
			
			$('#'+ref_id+'_subtotal').text(result[0]);
			$('#'+ref_id+'_max_capacity').text(result[1]);
			$('#'+ref_id+'subtotal').val(result[2]);

			if(block==1){
				$('#transportation_paypal_input_container_for_arrival').html(result[3]);
			}else{
				$('#transportation_paypal_input_container_for_departure').html(result[3]);
			}
			
			//$('#btn-transpo').attr('onChange', 'retrieveValues()');
			
			transportation_grandtotal();
		});
	}
}

function addon_calculation(qty,rate,id){
	var lastarrayctr  = parseInt($('#lastarrayctr').val());
	var subtotal = qty*rate;
	var grandtotal = $('#hidden_gtotal').val();
	$.post('plug/addon.setter.php', {
		qty : qty,
		rate : rate,
		lastarrayctr : lastarrayctr,
		id : id
	},function(html){
		var result = html.split('~@~');
		//$('#subtotal'+id).text("PHP "+ subtotal);
		$('#hidden_gtotal').val(result[0]);
		$('#grand_total_addon').text(result[0]);
		
		$('#addon_paypal_input_container').html(result[1]);
		$('#lastarrayctr').val(result[2]);
		
		$('#subtotal'+id).text(result[3]);
		//$('#addonlabel').html("Addon Service: ");
		//$('#addonamount').html(result[0]);
	})
	
}

function transportation_grandtotal(){
	var type = 2;
	var arrivalsubtotal 	= $('#arrivalsubtotal').val();
	var departuresubtotal 	= $('#departuresubtotal').val();
	
	// PROCESSI INPUTS
	if(parseInt(arrivalsubtotal) > 0 || parseInt(departuresubtotal) > 0){
	
		// CALCULATION PROCESS
		$.get('plug/transport.setter.php', {
			arrivalsubtotal: arrivalsubtotal,
			departuresubtotal: departuresubtotal,
			type: type
		},function(data){
			$('#grand_total_transpo').text(data);
			
			$('#transpolabel').html("Transpo. Service: ");
			$('#transpoamount').html(data);
			
			$('#infopreloader').fadeOut();
		});
	}
}

function transportation_calculation(block){
	var type = 1;
	var lastarrayctr = $('#lastarrayctr').val();
	if(block==1){ // IF ARRIVAL
		var ref_id = 'arrival';
	}else{  // IF DEPARTURE
		var ref_id = 'departure';
	}
	 
	var transportservice_id = $('#'+ref_id+'_transportservice_id option:selected').val();
	
	
	var transportvehicle_id = '';
	
	for(ctr=1;ctr<=3;ctr++){
		var st = $('#'+ref_id+'_transportvehicle_id'+ctr+' option:selected').val();

		if(st.length == 0 ){ st=0; }
		transportvehicle_id = transportvehicle_id+parseInt(st)+',';
		
	}
	
	
	//alert (transportvehicle_id);
	
	var qty 				= '';
	
	
	
	for(ctr=1;ctr<=3;ctr++){
		var st1 = $('#'+ref_id+'_vehicle_qty'+ctr).val();
		
		if(st1.length == 0 ){ st1=0; }
		
		qty = qty+parseInt(st1)+',';
		
		
	}
	
	//alert (qty);
	
	var flightno			= $('#'+ref_id+'_flightno').val();
	var time				= $('#'+ref_id+'_time').val();
	
	// PROCESSI INPUTS
	if(transportservice_id > 0){
		//$('#infopreloader').fadeIn();
		//$('#infopreloader').fadeTo(1000, 0.50);
	
		// CALCULATION PROCESS
		$.get('plug/transport.setter.php', {
			transportvehicle_id: transportvehicle_id,
			transportservice_id: transportservice_id,
			qty: qty,
			type: type,
			block: block,
			lastarrayctr: lastarrayctr,
			flightno: flightno,
			time: time
		},function(data){
			var result = data.split('~@~'); // split on colon
			
			$('#'+ref_id+'_subtotal1').text(result[0]);
			$('#'+ref_id+'_max_capacity1').text(result[1]);
			$('#'+ref_id+'subtotal1').val(result[2]);
			$('#'+ref_id+'_luggage_capacity1').text(result[3]);
			
			$('#'+ref_id+'_subtotal2').text(result[4]);
			$('#'+ref_id+'_max_capacity2').text(result[5]);
			$('#'+ref_id+'subtotal2').val(result[6]);
			$('#'+ref_id+'_luggage_capacity2').text(result[7]);
			
			$('#'+ref_id+'_subtotal3').text(result[8]);
			$('#'+ref_id+'_max_capacity3').text(result[9]);
			$('#'+ref_id+'subtotal3').val(result[10]);
			$('#'+ref_id+'_luggage_capacity3').text(result[11]);
			
			
			if(block==1){
				$('#transportation_paypal_input_container_for_arrival').html(result[12]);
			}else{
				$('#transportation_paypal_input_container_for_departure').html(result[12]);
			}
			
			//$('#btn-transpo').attr('onChange', 'retrieveValues()');
			
			transportation_grandtotal();
		});
	}
}


function addon_calculation(qty,rate,id){
	var lastarrayctr  = parseInt($('#lastarrayctr').val());
	//var subtotal = qty*rate;
	var grandtotal = $('#hidden_gtotal').val();
	var total_charges		= $('#total_charges').val();
	$.post('plug/addon.setter.php', {
		qty : qty,
		rate : rate,
		lastarrayctr : lastarrayctr,
		id : id,
		total_charges : total_charges
	},function(html){
		var result = html.split('~@~');
		
		
		
		$('#hidden_gtotal').val(result[0]);
		$('#grand_total_addon').text(result[0]);
		
		
		
		$('#addon_paypal_input_container').html(result[1]);
		$('#lastarrayctr').val(result[2]);
		$('#subtotal'+id).text(result[3]);
		
		//$('#tc').text(result[3]);
		
		//$('#addonlabel').html("Addon Service: ");
		//$('#addonamount').html(result[0]);
	})
	
}



function transportation_grandtotal(){
	
	var type = 2;
	var totalpickup=0;
	var totaldropoff=0;
	var total_charges		= $('#total_charges').val();
	//var arrivalsubtotal 	= $('#arrivalsubtotal').val();
	
	for(j=1;j<=3;j++){
		var ar = $('#arrivalsubtotal'+j).val();
	
		totalpickup 	= totalpickup + parseInt(ar);
	}
	
	for(j=1;j<=3;j++){
		var ar = $('#departuresubtotal'+j).val();
		totaldropoff 	= totaldropoff + parseInt(ar);
	}
	
	
	
	//var departuresubtotal 	= $('#departuresubtotal').val();
	
	// PROCESSI INPUTS
	if(parseInt(totalpickup) > 0 || parseInt(totaldropoff) > 0){
	
		// CALCULATION PROCESS
		$.get('plug/transport.setter.php', {
			arrivalsubtotal: totalpickup,
			departuresubtotal: totaldropoff,
			type: type,
			total_charges:total_charges
		},function(data){
			var result = data.split('~@~');
			$('#grand_total_transpo').text(result[0]);
			
			//$('#transpolabel').html("Transpo. Service: ");
			//$('#transpoamount').html(data);
			
			//$('#infopreloader').fadeOut();
			$('#tc').text(result[1]);
		});
	}
}


function getSummary(alltotal){
	
	// GET QTY
	var qtys = ''; 
	for (ctr=1; ctr<=alltotal; ctr++){
		//alert($('#qty'+ctr).val());
		var str = $('#qty'+ctr).val();
		qtys += str+'~:~'; 
	} 
	
	var roomtypes = ''; 
	for (ctr=1; ctr<=alltotal; ctr++){
		//alert($('#qty'+ctr).val());
		var str = $('#room_id_'+ctr).text();
		roomtypes += str+'~:~'; 
	}
	
	var roomsel = ''; 
	for (ctr=1; ctr<=alltotal; ctr++){
		$('#option-selection'+ctr+' a').each(function() {
			if($(this).attr('class')=='active'){
				var str = $(this).text();
				roomsel += str+'~:~';
			}
		});
	}
	
	$.post('booking-summary.php', {
		qty_all: qtys,
		gblockctr: alltotal,
		roomtypes_all: roomtypes,
		roomsel_all: roomsel
	},function(data){
	   location.href='booking-summary.php';
	});


}


function totalCharges() {
	if (parseFloat($('#grand_total_addon').text())>0){
	
		$('#addonlabel').html("Addon Service: ");
		
		var total_charges		= $('#total_charges').val();
		$('#addonamount').html($('#grand_total_addon').text());
		
		$.post('plug/totalcharge.php', {
		total_charges: total_charges
		},function(data){
			
			$('#tc').html(data);
			
		})
		
		
		$(document).trigger('close.facebox');
		
	}
}
