// JavaScript Document
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
  // check for IE7 or greater.
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver < 7.0 )
	  window.location = "non-compatable.htm";
  }

function errorBox(errorText,what){
  if(what=='show'){
	document.getElementById('errorbox').style.display='block';
	document.getElementById('errorbox').style.visibility='visible';
	document.getElementById('errorboxContent').innerHTML='<p class="right"><a href="javascript:errorBox(\'\',\'hide\');" title="close"><img src="pics/delete.gif" width="10" height="10" alt="close" title="close" /></a></p><div class="clearing"></div>'+errorText;
  }
  if(what=='hide'){
	document.getElementById('errorbox').style.display='none';
	document.getElementById('errorbox').style.visibility='hidden';
  }
}

function tandcBox(tandcText,what){
  if(what=='show'){
	document.getElementById('tandcbox').style.display='block';
	document.getElementById('tandcbox').style.visibility='visible';
	document.getElementById('tandcboxContent').innerHTML='<p class="right"><a href="javascript:tandcBox(\'\',\'hide\');" title="close"><img src="pics/delete.gif" width="10" height="10" alt="close" title="close" /></a></p><div class="clearing"></div>'+tandcText + '<p class="right"><a href="javascript:tandcBox(\'\',\'hide\');" title="close"><img src="pics/delete.gif" width="10" height="10" alt="close" title="close" /></a></p><div class="clearing"></div>';
  }
  if(what=='hide'){
	document.getElementById('tandcbox').style.display='none';
	document.getElementById('tandcbox').style.visibility='hidden';
  }
}

function checkSearch(dateStr,nights) {
  dateStr=dateStr;
  var datePat = /^(\d{1,2})(\/)(\d{1,2})(\/)(\d{4})$/;
  var matchArray = dateStr.match(datePat); // is the format ok?
  var sendForm = true
  var d = new Date();
  
  if (matchArray == null) {
    errorBox("Please enter your arrival date as mm/dd/yyyy","show");
    sendForm = false;
  }
  
  day = matchArray[1]; // p@rse date into variables
  month = matchArray[3];
  year = matchArray[5];
  
  if (month < 1 || month > 12) { // check month range
    errorBox(month+" is an invalid month","show");
    sendForm = false;
  }
  
  if (day < 1 || day > 31) {
    errorBox(day+" is an invalid day","show");
    sendForm = false;
  }
  
  if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	if(month==4){ monthname="April"; }
	if(month==6){ monthname="June"; }
	if(month==9){ monthname="September"; }
	if(month==11){ monthname="November"; }
    errorBox(monthname+" does not have 31 days!","show");
    sendForm = false;
  }
  
  if (month == 2) { // check for february 29th
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap)) {
      errorBox("February " + year + " doesn`t have " + day + " days!","show");
      sendForm = false;
    }
  }
  
  if(year<d.getFullYear()){
    errorBox("Arrival date cannot be in the past!","show");
    sendForm = false;
  } else {
	if(month<(d.getMonth()+1)){
      errorBox("Arrival date cannot be in the past!","show");
      sendForm = false;
    } else {
	  if(month==(d.getMonth()+1) && day<d.getDate()){
        errorBox("Arrival date cannot be in the past!","show");
        sendForm = false;
	  }
	}
  }
  
  if (isNaN(nights) || nights<1) {
    errorBox("You must stay for more than one night!","show");
    sendForm = false;
  }
  
  if(document.getElementById('location').value=='enter town or postcode'){
    errorBox("You must enter a location!","show");
    sendForm = false;
  }
  
  if(sendForm == true){
	document.searchform.submit();
    //return true; // search form is valid
  }
}

function checkWipe(){
  if(document.getElementById('location').value=='enter town or postcode'){
    document.getElementById('location').value='';
  }
}

function showTab(tabName){
  document.getElementById('Booking').style.display='none';
  document.getElementById('Booking').style.visibility='hidden';
  document.getElementById('Map').style.display='none';
  document.getElementById('Map').style.visibility='hidden';
  document.getElementById('Reviews').style.display='none';
  document.getElementById('Reviews').style.visibility='hidden';
  document.getElementById('Facilities').style.display='none';
  document.getElementById('Facilities').style.visibility='hidden';
  
  // Show correct tab
  document.getElementById(tabName).style.display='block';
  document.getElementById(tabName).style.visibility='visible';
  
  if(tabName=='Map'){
	showMapTab();
  }
}

function showLowerTab(tabName){
  document.getElementById('LowerTabs').style.display='block';
  document.getElementById('LowerTabs').style.visibility='visible';
  document.getElementById('CancellationTab').style.borderBottom='0px';
  document.getElementById('InformationTab').style.borderBottom='0px';
  document.getElementById('Cancellation').style.display='none';
  document.getElementById('Cancellation').style.visibility='hidden';
  document.getElementById('Information').style.display='none';
  document.getElementById('Information').style.visibility='hidden';
  
  // Show correct tab
  document.getElementById(tabName).style.display='block';
  document.getElementById(tabName).style.visibility='visible';
}


function GetXmlHttpObject(){
  if (window.XMLHttpRequest){
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject){
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
}

function adminBedsValue(){
  if(document.searchform.beds.length>0){
    for (var i=0; i < document.searchform.beds.length; i++){
      if (document.searchform.beds[i].checked){
        return document.searchform.beds[i].value;
      }
	}
  } else {
	return document.searchform.beds.value;
  }
}

function bedsValue(){
  if(document.apartmentForm.beds.length>0){
    for (var i=0; i < document.apartmentForm.beds.length; i++){
      if (document.apartmentForm.beds[i].checked){
        return document.apartmentForm.beds[i].value;
      }
	}
  } else {
	return document.apartmentForm.beds.value;
  }
}

function adminGetPrices(arr_date,nights,property,beds,qty){
  document.getElementById('calendarPrices').style.display='none';
  document.getElementById('calendarPrices').style.visibility='hidden';
  document.getElementById('calendarPricesLoading').style.display='block';
  document.getElementById('calendarPricesLoading').style.visibility='visible';
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null){
    alert ("Your browser does not support XMLHTTP!");
    return;
  }
  var url="../includes/ajax-prices.php?arr_date="+arr_date+"&nights="+nights+"&property="+property+"&beds="+beds+"&qty="+qty;
  xmlhttp.onreadystatechange=stateChanged;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  // Get the apartment description and then the total proce for the stay
  getDescription(property,beds);
  getTotalPrice(arr_date,nights,property,beds,qty);
  
  if(document.searchform.ppnExVAT.value>0){
	calcPPN(document.searchform.nights.value,document.searchform.qty.value,document.searchform.ppn.value);
  } else if(document.searchform.ppnIncVAT.value>0){
	calcPPN(document.searchform.nights.value,document.searchform.qty.value,document.searchform.ppn.value);
  }
}

function calcPPN(nights,apartments,ppn){
  if(ppn>0){
	var ppnPrice = (nights * ppn) * apartments;
    document.getElementById('ppnPrice').innerHTML = 'Total price for this stay at \'ppn rate\': &pound;'+ppnPrice.toFixed(2);
  } else {
    document.getElementById('ppnPrice').innerHTML = '';
  }
}

function calcTotalStay(totalPrice){
  if(totalPrice>0){
    document.getElementById('ppnPrice').innerHTML = 'Total price for this stay at \'total stay rate\': &pound;'+parseFloat(totalPrice).toFixed(2);
  } else {
    document.getElementById('ppnPrice').innerHTML = '';
  }
}

function adminBook(apartmentID,arrDate,nights,beds,qty,ppn,member_id,copyBookingID,repeatBookingID,repeatOldBookingID,totalStayPrice){
  var newPrice=document.getElementById('TotalPrice').innerHTML;
  
  if(newPrice=='FULL'){
	alert('There is no availability for these dates.');
  } else {
    window.location='bookings-confirm.php?id='+apartmentID+'&arr_date='+arrDate+'&nights='+nights+'&beds='+beds+'&qty='+qty+'&ppn='+ppn+'&member_id='+member_id+'&new='+copyBookingID+'&repeat='+repeatBookingID+'&repeatold='+repeatOldBookingID+'&totalStayPrice='+totalStayPrice;
    return null;
  }
}

function book(apartmentID,arrDate,nights,beds,qty){
  var newPrice=document.getElementById('TotalPrice').innerHTML;
  
  if(newPrice=='FULL'){
	alert('There is no availability for these dates.');
  } else {
    window.location='../book.html?id='+apartmentID+'&arr_date='+arrDate+'&nights='+nights+'&beds='+beds+'&qty='+qty;
    return null;
  }
}

function IsDeliverySame_clicked() {

    if (document.customerform.bIsDeliverySame.checked) {
		document.getElementById('showDelivery').style.visibility = 'hidden';
		document.getElementById('showDelivery').style.display = 'none';

        document.customerform.BillingFirstnames.value = "";
        //document.customerform.BillingFirstnames.className = "inputBoxDisable";
        document.customerform.BillingFirstnames.disabled = true;
        
        document.customerform.BillingSurname.value = "";
        //document.customerform.BillingSurname.className = "inputBoxDisable";
        document.customerform.BillingSurname.disabled = true;
        
        document.customerform.BillingAddress1.value = "";
        //document.customerform.BillingAddress1.className = "inputBoxDisable";
        document.customerform.BillingAddress1.disabled = true;

        document.customerform.BillingAddress2.value = "";
        //document.customerform.BillingAddress2.className = "inputBoxDisable";
        document.customerform.BillingAddress2.disabled = true; 

        document.customerform.BillingCity.value = "";
        //document.customerform.BillingCity.className = "inputBoxDisable";
        document.customerform.BillingCity.disabled = true;

        document.customerform.BillingPostCode.value = "";
        //document.customerform.BillingPostCode.className = "inputBoxDisable";
        document.customerform.BillingPostCode.disabled = true;

        document.customerform.BillingCountry.value = "";
        //document.customerform.BillingCountry.className = "inputBoxDisable";
        document.customerform.BillingCountry.disabled = true;

        document.customerform.BillingState.value = "";
        //document.customerform.BillingState.className = "inputBoxDisable";
        document.customerform.BillingState.disabled = true;

        document.customerform.BillingPhone.value = "";
        //document.customerform.BillingPhone.className = "inputBoxDisable";
        document.customerform.BillingPhone.disabled = true;
    } 
    else 
    {
		document.getElementById('showDelivery').style.visibility = 'visible';
		document.getElementById('showDelivery').style.display = 'block';
		
        document.customerform.BillingFirstnames.disabled = false;
        //document.customerform.DeliveryFirstnames.className = "inputBoxEnable";
        document.customerform.BillingFirstnames.focus();
        document.customerform.BillingSurname.disabled = false;
        //document.customerform.BillingSurname.className = "inputBoxEnable";
        document.customerform.BillingAddress1.disabled = false;
       // document.customerform.BillingAddress1.className = "inputBoxEnable";
        document.customerform.BillingAddress2.disabled = false;
        //document.customerform.BillingAddress2.className = "inputBoxEnable";
        document.customerform.BillingCity.disabled = false;
        //document.customerform.BillingCity.className = "inputBoxEnable";
        document.customerform.BillingPostCode.disabled = false;
        //document.customerform.BillingPostCode.className = "inputBoxEnable";
        document.customerform.BillingCountry.disabled = false;
        //document.customerform.BillingCountry.className = "inputBoxEnable";
        document.customerform.BillingState.disabled = false;
        //document.customerform.BillingState.className = "inputBoxEnable";
        document.customerform.BillingPhone.disabled = false;
       //document.customerform.BillingPhone.className = "inputBoxEnable";
    }
}

// Check if refundable deposit needs to be charged
function checkDeposit(date,nights){
  var dates=date.split("/");
  var checkDate = new Date(dates[2], dates[1]-1, dates[0], 0, 0, 0, 0);
  var day = checkDate.getDay();
  if(day==5 && nights<3){
    // Set deposit
	document.getElementById('TotalDeposit').innerHTML = '&pound;100';
  } else if(day==6 && nights==1){
    // Set deposit
	document.getElementById('TotalDeposit').innerHTML = '&pound;100';
  } else {
	document.getElementById('TotalDeposit').innerHTML = '&pound;0';
  }
}

// Recalculate Price
function reCalcPrice(){
  var bookingPrice = document.getElementById('TotalPrice').innerHTML;
  bookingPrice = bookingPrice.replace("£","");
  bookingPrice = bookingPrice.replace(",","");
  var depositPrice = document.getElementById('TotalDeposit').innerHTML;
  depositPrice = depositPrice.replace("£","");
  depositPrice = depositPrice.replace(",","");
  var ccChargePrice = document.getElementById('TotalCCCharge').innerHTML;
  ccChargePrice = ccChargePrice.replace("£","");
  ccChargePrice = ccChargePrice.replace(",","");
  var totalPaid = document.getElementById('TotalPaid').innerHTML;
  totalPaid = totalPaid.replace("£","");
  totalPaid = totalPaid.replace(",","");
  var newTotalPrice = parseFloat(bookingPrice) + parseFloat(depositPrice) + parseFloat(ccChargePrice);
  var unpaid = parseFloat(newTotalPrice) - parseFloat(totalPaid);
  
  document.getElementById('SumPrice').innerHTML = '&pound;'+newTotalPrice;
  document.getElementById('TotalDue').innerHTML = '&pound;'+unpaid.toFixed(2);
  document.getElementById('TotalHidden').innerHTML = '<input type="hidden" name="price" value="'+bookingPrice+'" /><input type="hidden" name="deposit" value="'+depositPrice+'" /><input type="hidden" name="cc_charge" value="'+ccChargePrice+'" />';
}
function reCalcBookingPrice(){
  var bookingPrice = document.getElementById('price').value;
  bookingPrice = bookingPrice.replace(",","");
  var depositPrice = document.getElementById('TotalDeposit').innerHTML;
  depositPrice = depositPrice.replace("£","");
  depositPrice = depositPrice.replace(",","");
  var ccChargePrice = document.getElementById('TotalCCCharge').innerHTML;
  ccChargePrice = ccChargePrice.replace("£","");
  ccChargePrice = ccChargePrice.replace(",","");
  var totalPaid = document.getElementById('TotalPaid').innerHTML;
  totalPaid = totalPaid.replace("£","");
  totalPaid = totalPaid.replace(",","");
  var newTotalPrice = parseFloat(bookingPrice) + parseFloat(depositPrice) + parseFloat(ccChargePrice);
  var unpaid = parseFloat(newTotalPrice) - parseFloat(totalPaid);
  
  document.getElementById('SumPrice').innerHTML = '&pound;'+newTotalPrice;
  document.getElementById('TotalDue').innerHTML = '&pound;'+unpaid.toFixed(2);
  //document.getElementById('TotalHidden').innerHTML = '<input type="hidden" name="price" value="'+bookingPrice+'" /><input type="hidden" name="deposit" value="'+depositPrice+'" /><input type="hidden" name="cc_charge" value="'+ccChargePrice+'" />';
  document.getElementById('TotalHidden').innerHTML = '<input type="hidden" name="deposit" value="'+depositPrice+'" /><input type="hidden" name="cc_charge" value="'+ccChargePrice+'" />';
}

// Add handling fee
function calcHandling(){
  if(document.getElementById('handling').checked==true){
    var bookingPrice = document.getElementById('TotalPrice').innerHTML;
    bookingPrice = bookingPrice.replace("£","");
    bookingPrice = bookingPrice.replace(",","");
    var depositPrice = document.getElementById('TotalDeposit').innerHTML;
    depositPrice = depositPrice.replace("£","");
    depositPrice = depositPrice.replace(",","");
    var ccCharge = ( ( ( parseFloat(bookingPrice) + parseFloat(depositPrice) )/100 ) * 2.5).toFixed(2);
  } else {
	var ccCharge = "0";
  }
  
  document.getElementById('TotalCCCharge').innerHTML = '&pound;'+ccCharge;
  reCalcPrice();
}

// Show booking fee info
function showInfo(type){
  if(type=='handlingfee'){
	errorBox("<p>Credit card transactions are charged at 2.5%</p><p>There is however no fee for paying via debit card.</p><p>To pay via debit card, use the 'back' button at the top of this confirmation screen, and select 'debit card' from the 'Payment Method' option at the bottom of the screen.</p>","show");
  } else if(type=='deposit'){
	errorBox("<p>A refundable deposit of &pound;100 is taken for all weekend bookings.</p>","show");
  }
}