function isEmpty(s) {
	return ((s == null) || ($.trim(s).length == 0) || ($.trim(s) == ""))
}

function showCal() {
	if ($('#reservationBox').css('opacity') == 1) {
		$('#txtDt').focus();
	}
}

function isEmailValid(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail ID")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Invalid E-mail ID")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail ID")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail ID")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail ID")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail ID")
		return false
	 }

	 return true					
}

function addTour(id) {
	if ($('#reservationBox').css('opacity') != 1) {
		$('#reservationBox').css('opacity', 1);
		$('#reservationBox :input').attr('disabled', false);
	}
	$("#cmbTour option").attr('disabled', false);
	$('#cmbTour').val(id, true);
	window.scrollTo(0,0);
	$('#txtDt').focus();
}

function addTourHome(id) {
	location.href = 'index2.php?id='+id;
}

function addTourRes(id) {
	location.href = 'book.php?id='+id;
}

function editTour(id, date, adult, child, seniors) {
	if($('#err').length > 0) $('#err').get(0).innerHTML='';
	if ($('#reservationBox').css('opacity') != 1) {
		$('#reservationBox').css('opacity', 1);
		$('#reservationBox :input').attr('disabled', false);
	}
	$("#cmbTour option").attr('disabled', true);
	if ($("#cmbTour option[value='" + id + "']").attr('disabled') == true) {
		$("#cmbTour option[value='" + id + "']").attr('disabled', false);
	}
	$('#cmbTour').val(id, true);
	//$('#cmbTour').attr('readonly', true);
	$('#txtDt').val(date);
	$('#txtAdults').val(adult);
	$('#txtChild').val(child);
	$('#txtSeniors').val(seniors);
}

function editTourInline(thisObj, id) {
	if ($(thisObj).get(0).innerHTML == 'Edit') {
		//$(thisObj).get(0).innerHTML = 'Save';
		$(thisObj).hide();
		$('#dt_'+id).hide();
		$('#inputDt_'+id).show();
		$('#txtDt'+id).datepicker({
			numberOfMonths: 2,
			minDate: 0
		});
		$('#adult_'+id).hide();
		$('#inputAdult_'+id).show();
		$('#child_'+id).hide();
		$('#inputChild_'+id).show();
		$('#senior_'+id).hide();
		$('#inputSenior_'+id).show();
		$('#saveResInline').show();
	}
}

function saveTourInline() {
	$('.inputDt').each(function(){
		if($(this).css('display') == 'block') {
			id = $(this).attr('id').replace('inputDt_', '');
			if (isEmpty($('#txtDt'+id).val())) {
				alert("Please enter the tour date");
				$('#txtDt'+id).focus();
				return false;
			}
			if (($('#txtAdults'+id).length > 0 && $('#txtAdults'+id).val() > 0) || ($('#txtChild'+id).length > 0 && $('#txtChild'+id).val() > 0) || ($('#txtSenior'+id).length > 0 && $('#txtSenior'+id).val() > 0)) {
				
			}
			else {
				alert("Please enter the seat");
				return false;
			}
			var params = '';
			if ($('#txtAdults'+id).length > 0) params += '&adult=' + $('#txtAdults'+id).val();
			if ($('#txtChild'+id).length > 0) params += '&child=' + $('#txtChild'+id).val();
			if ($('#txtSenior'+id).length > 0) params += '&senior=' + $('#txtSenior'+id).val();
			$.ajax({
				type: "POST",
				url: "updateRes.php",
				cache: false,
				data: "id="+id+"&dt="+$('#txtDt'+id).val()+params,
				success: function(msg) {
					//var dt = $('#txtDt'+id).val().split('/');
					//$(thisObj).get(0).innerHTML = 'Edit';
					//$('#dt_'+id).get(0).innerHTML = $.datepicker.formatDate('DD, MM dd, yy', new Date(dt[2], dt[0] - 1, dt[1]));
					$('#dt_'+id).get(0).innerHTML = $('#txtDt'+id).val();
					$('#inputDt_'+id).hide();
					$('#dt_'+id).show();
					if ($('#txtAdults'+id).length > 0) {
						$('#adult_'+id).get(0).innerHTML = $('#txtAdults'+id).val();
						$('#adult_'+id).show();
						$('#inputAdult_'+id).hide();
					}
					if ($('#txtChild'+id).length > 0) {
						$('#child_'+id).get(0).innerHTML = $('#txtChild'+id).val();
						$('#child_'+id).show();
						$('#inputChild_'+id).hide();
					}
					if ($('#txtSenior'+id).length > 0) {
						$('#senior_'+id).get(0).innerHTML = $('#txtSenior'+id).val();
						$('#senior_'+id).show();
						$('#inputSenior_'+id).hide();
					}
					
					setTimeout(redirectto(),1000);
					
				}
		   });
		}
	});
}

function redirectto(){
	location.href = 'Book';	
}

function onBook() {
	if ($("#cmbTour option[value='" + $('#cmbTour').val() + "']").attr('disabled') == true) {
		alert("The selected tour already added to your plan. \nPlease click the edit button to make changes.");
		$('#cmbTour').focus();
		return false;
	}
	if ($('#cmbTour').val() == -1) {
		alert("Please select a tour");
		$('#cmbTour').focus();
		return false;
	}
	if (isEmpty($('#txtDt').val())) {
		alert("Please enter the tour date");
		$('#txtDt').focus();
		return false;
	}
	if ($('#txtAdults').val() < 1 && $('#txtChild').val() < 1 && $('#txtSeniors').val() < 1) {
		alert("Please select a seat");
		$('#txtAdults').focus();
		return false;
	}
	/*if (isNaN($('#txtAdults').val()) || isNaN($('#txtChild').val()) || isNaN($('#txtSeniors').val()) || $('#txtAdults').val() < 0 || $('#txtChild').val() < 0 || $('#txtSeniors').val() < 0) {
		alert("Please enter proper value in the seats");
		$('#txtAdults').focus();
		return false;
	}*/
	if ($('#hdnPayType').length > 0 && $('#hdnDepositYN').length > 0) {
		if ($('#hdnPayType').val() < 1 || $('#hdnPayType').val() == "" || $('#hdnPayType').val() == null) {
			alert("Please select the payment method");
			return false;
		}
		if ($('#hdnDepositYN').val() < 1 || $('#hdnDepositYN').val() == "" || $('#hdnDepositYN').val() == null) {
			alert("Please select the deposit");
			return false;
		}
		if (isEmpty($('#txtConcgCode').val())) {
			alert("Please enter your conceirge code");
			$('#txtConcgCode').focus();
			return false;
		}
		if ($('#hdnPayType').val() == 'cash' && (parseInt($('#txtAdults').val())+parseInt($('#txtChild').val())+parseInt($('#txtSeniors').val())) > 8) {
			alert("Total reservation seats must not be greater than 8");
			return false;
		}
	}
	return true;
}
function onCheckout() {
	$('#bookHeader').hide();
	$('#bookContent').hide();
	$('.vTourThums2').hide();
	$('#checkoutContent').show();
}
function onCheckoutBack() {
	$('#frmCheckout').attr("action",'?type=back');
	$('#frmCheckout').submit();
}
function onReviewBack() {
	$('#frmConfirm').attr("action",'book.php?type=reviewBack');
	$('#frmConfirm').submit();
}
function onCheckoutContinue() {
	if ($('#cmbHotel').val() == -1) {
		alert("Please select a hotel");
		$('#cmbHotel').focus();
		return false;
	}
	if ($('#cmbHotel').val() == -2) {
		if (isEmpty($('#txtHotel').val())) {
			alert("Please enter the phone number to arrange the pick up");
			$('#txtHotel').focus();
			return false;
		}
	}
	/*if (isEmpty($('#txtFName').val())) {
		alert("Please enter the first name");
		$('#txtFName').focus();
		return false;
	}*/
	if (isEmpty($('#txtLName').val())) {
		alert("Please enter the last name");
		$('#txtLName').focus();
		return false;
	}
	if ($('#hdn_payType').val() != 'cash') {
		if (isEmpty($('#txtEmail').val()) && isEmpty($('#txtMob').val())) {
			//alert("Please enter the email");
			alert("Please atleast enter either email or cell phone number");
			$('#txtEmail').focus();
			return false;
		}
		else if (!isEmpty($('#txtEmail').val()) && isEmailValid($('#txtEmail').val())==false) {
			$('#txtEmail').parent().removeClass('readonly');
			$('#txtEmail').attr('readonly', false);
			$('#txtEmail').focus();
			return false;
		}
		/*if (isEmpty($('#txtSt').val())) {
			alert("Please enter the street");
			$('#txtSt').focus();
			return false;
		}
		if (isEmpty($('#txtCountry').val())) {
			alert("Please select the country");
			$('#txtCountry').focus();
			return false;
		}
		if (isEmpty($('#txtCity').val())) {
			alert("Please enter the city");
			$('#txtCity').focus();
			return false;
		}
		if (isEmpty($('#txtState').val()) || $('#cmbState').val() == '-Select-') {
			if ($('#cmbState').val() == 'Other') {
				alert("Please enter the state");
				$('#txtState').focus();
			}
			else if ($('#cmbState').val() == '-Select-') {
				alert("Please select the state");
				$('#cmbState').focus();
			}
			return false;
		}
		if (isEmpty($('#txtZip').val())) {
			alert("Please enter the zip / postal code");
			$('#txtZip').focus();
			return false;
		}*/
	}
	$('#frmCheckout').attr("action",'?type=continue');
	$('#frmCheckout').submit();
	
}
function onEditField(id) {
	$('#'+id).parent().removeClass('readonly');
	$('#'+id).attr('readonly', false);
	$('#'+id).focus();
}
function onEditFieldSave(obj) {
	if (obj.attr('readonly') != true) {
		$.ajax({
			type: "POST",
			url: "updateField.php",
			cache: false,
			data: "id="+obj.attr('id')+"&val="+obj.val()
	   });
		obj.parent().addClass('readonly');
		obj.attr('readonly', true);
	}
}
function onApproveContinue() {
	if (isEmpty($('#txtHotel').val())) {
		alert("Please enter the hotel");
		$('#txtHotel').parent().removeClass('readonly');
		$('#txtHotel').attr('readonly', false);
		$('#txtHotel').focus();
		return false;
	}
	/*if (isEmpty($('#txtFName').val())) {
		alert("Please enter the first name");
		$('#txtFName').parent().removeClass('readonly');
		$('#txtFName').attr('readonly', false);
		$('#txtFName').focus();
		return false;
	}*/
	if (isEmpty($('#txtLName').val())) {
		alert("Please enter the last name");
		$('#txtLName').parent().removeClass('readonly');
		$('#txtLName').attr('readonly', false);
		$('#txtLName').focus();
		return false;
	}
	if (isEmpty($('#txtEmail').val()) && isEmpty($('#txtMob').val())) {
		//alert("Please enter the email");
		alert("Please atleast enter either email or cell phone number");
		$('#txtEmail').parent().removeClass('readonly');
		$('#txtEmail').attr('readonly', false);
		$('#txtEmail').focus();
		return false;
	}
	else if (!isEmpty($('#txtEmail').val()) && isEmailValid($('#txtEmail').val())==false) {
		$('#txtEmail').parent().removeClass('readonly');
		$('#txtEmail').attr('readonly', false);
		$('#txtEmail').focus();
		return false;
	}
	/*if (isEmpty($('#txtSt').val())) {
		alert("Please enter the street");
		$('#txtSt').parent().removeClass('readonly');
		$('#txtSt').attr('readonly', false);
		$('#txtSt').focus();
		return false;
	}
	if (isEmpty($('#txtCountry').val())) {
		alert("Please enter the country");
		$('#txtCountry').parent().removeClass('readonly');
		$('#txtCountry').attr('readonly', false);
		$('#txtCountry').focus();
		return false;
	}
	if (isEmpty($('#txtCity').val())) {
		alert("Please enter the city");
		$('#txtCity').parent().removeClass('readonly');
		$('#txtCity').attr('readonly', false);
		$('#txtCity').focus();
		return false;
	}
	if (isEmpty($('#txtState').val())) {
		alert("Please enter the state");
		$('#txtState').parent().removeClass('readonly');
		$('#txtState').attr('readonly', false);
		$('#txtState').focus();
		return false;
	}
	if (isEmpty($('#txtZip').val())) {
		alert("Please enter the zip / postal code");
		$('#txtZip').parent().removeClass('readonly');
		$('#txtZip').attr('readonly', false);
		$('#txtZip').focus();
		return false;
	}*/
	$('#frmConfirm').attr("action",'Payment');
	$('#frmConfirm').submit();
}
function onPaymentBack() {
	$('#frmPayment').attr("action",'Review');
	$('#frmPayment').submit();
}
function onPurchase() {
	if (isEmpty($('#hdnCard').val())) {
		alert("Please select the card");
		return false;
	}
	if (isEmpty($('#txtCC').val())) {
		alert("Please enter the credit card number");
		$('#txtCC').focus();
		return false;
	}
	if (isNaN($('#txtCC').val())) {
		alert("Please enter credit card number without spaces or dashes");
		$('#txtCC').focus();
		return false;
	}
	if (isEmpty($('#txtExpMM').val())) {
		alert("Please enter the expiration month");
		$('#txtExpMM').focus();
		return false;
	}
	if ($('#txtExpMM').val().length != 2 || $('#txtExpMM').val() > 12 || isNaN($('#txtExpMM').val())) {
		alert("Please enter valid expiration date");
		$('#txtExpMM').focus();
		return false;
	}
	if (isEmpty($('#txtExpYY').val())) {
		alert("Please enter the expiration year");
		$('#txtExpYY').focus();
		return false;
	}
	if ($('#txtExpYY').val().length != 2 || isNaN($('#txtExpMM').val())) {
		alert("Please enter valid expiration date");
		$('#txtExpYY').focus();
		return false;
	}
	if (isEmpty($('#txtCode').val())) {
		alert("Please enter the security code");
		$('#txtCode').focus();
		return false;
	}
	if ($('#chkToc').attr('checked') == false) {
		alert("Please accept the terms & conditions");
		return false;
	}
	$('#frmPayment').submit();
}
function onCheck(parent, obj) {
	if (parent == 'cash_credit') {
		if (obj.attr('class') == 'cash') {
			obj.removeClass().addClass('cashOn');
			$('#cash_credit #credit').removeClass().addClass('credit');
			$('#hdnPayType').val('cash');
		}
		else if (obj.attr('class') == 'credit') {
			obj.removeClass().addClass('creditOn');
			$('#cash_credit #cash').removeClass().addClass('cash');
			$('#hdnPayType').val('credit');
			$('#chkDeposit #no').attr('class', 'noOn');
			$('#chkDeposit #yes').attr('class', 'yes');
			$('#hdnDepositYN').val('no');
		}
	}
	else if (parent == 'chkDeposit') {
		if (obj.attr('class') == 'yes') {
			if ($('#cash_credit #credit').attr('class') == 'creditOn') {
				alert('You are not allowed to get deposits in credit payment');
			}
			else {
				obj.removeClass().addClass('yesOn');
				$('#chkDeposit #no').attr('class', 'no');
				$('#hdnDepositYN').val('yes');
			}
		}
		else if (obj.attr('class') == 'no') {
			obj.removeClass().addClass('noOn');
			$('#chkDeposit #yes').attr('class', 'yes');
			$('#hdnDepositYN').val('no');
		}
	}
}
function onCashPurchase() {
	$('#frmConfirm').submit();
}
function onManCCPurchase() {
	$('#frmConfirm').submit();
}
function onCheckRadio(parent, obj) {
	if (parent == 'cash_credit') {
		if (obj.val() == 'credit') {
			$('#rd_deposit_no').attr('checked', 'true');
			$('#hdnDepositYN').val('no');
		}
		$('#hdnPayType').val(obj.val());
	}
	else if (parent == 'chkDeposit') {
		if (obj.val() == 'yes' && $('#hdnPayType').val() == 'credit') {
			alert('You are not allowed to get deposits in credit payment');
			$('#rd_deposit_no').attr('checked', 'true');
			$('#hdnDepositYN').attr('checked', 'true');
		}
		else {
			$('#hdnDepositYN').val(obj.val());
		}
	}
}
function printConfirmation(){
	str=$('#confirmDet').get(0).innerHTML
	str2 = $('#confirmDet2').get(0).innerHTML
	newwin=window.open('','printwin','left=100,top=100,width=800,height=400')
	newwin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml">\n<head>\n')
	newwin.document.write('<title>Print Page</title>\n')
	newwin.document.write('<script>\n')
	newwin.document.write('function chkstate(){\n')
	newwin.document.write('if(document.readyState=="complete"){\n')
	newwin.document.write('window.close()\n')
	newwin.document.write('}\n')
	newwin.document.write('else{\n')
	newwin.document.write('setTimeout("chkstate()",2000)\n')
	newwin.document.write('}\n')
	newwin.document.write('}\n')
	newwin.document.write('function print_win(){\n')
	newwin.document.write('window.print();\n')
	newwin.document.write('chkstate();\n')
	newwin.document.write('}\n')
	newwin.document.write('<\/script>\n')
	newwin.document.write('</head>\n')
	newwin.document.write('<body onload="print_win()">\n<center><b>Customer Receipt</b></center>\n')
	newwin.document.write(str)
	newwin.document.write('<div style="page-break-after:always;"></div>\n<center><b>Boarding Pass</b></center>\n')
	newwin.document.write(str2)
	newwin.document.write('</body>\n')
	newwin.document.write('</html>\n')
	newwin.document.close()
}
function commissionReport() {
	//window.open('admin/commision_rport_pdf.php', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width='+screen.availWidth+',height='+screen.availHeight+',left = 520,top = 150');
	window.open('commission_report.php', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=620,height=290,left = 520,top = 150');
}
function onPostRequest() {
	if (isEmpty($('#txtOrg').val())) {
		alert('Please enter the oraganisation');
		$('#txtOrg').focus();
		return false;
	}
	if (isEmpty($('#txtName').val())) {
		alert('Please enter the contact name');
		$('#txtName').focus();
		return false;
	}
	if (isEmpty($('#txtEmail').val())) {
		alert('Please enter the contact email');
		$('#txtEmail').focus();
		return false;
	}
	if (isEmpty($('#txtComments').val())) {
		alert('Please enter the comments');
		$('#txtComments').focus();
		return false;
	}
	return true;
}
function onReserve(id) {
	window.open('http://dctrails.com/reserve_senior.php?id=' + id, '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=500,left = 520,top = 150');
}