// MCL Scripts Copyright Mark Fesco, Media Creative Ltd.

// Main Ajax Call -----------------------------------------------------------

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("baskethere").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function returnBookings() { 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="/working/returnbooking.asp"; 
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
//-------------------------------------------------------------------------

var openDiv=1;

function checkAvailability(){
var dFrom = document.forms[0].date1.value;
var dTo =  document.forms[0].date2.value;
var dAd = document.forms[0].adults.value;
var dCh = document.forms[0].children.value;
var dAc = document.forms[0].aType.value;
var dFc = new Date(dFrom);
var dTc = new Date(dTo);
if(dFrom=="" || dTo ==""){
alert("\nDATE(S) NOT SPECIFIED\n\nPlease select both an 'Arrival' and 'Departure' date\n");
}
else if (dFrom == dTo){
alert("\nReservations can only be taken for overnight stays\n\nYour arrival and departure dates are the same\n");
}
else if(validate(dFrom,dTo)==false){
return;
}
else {
document.location.href="/availability.asp?df=" + dFrom + "&dt=" + dTo + "&ad=" + dAd + "&ch=" + dCh + "&dAc=" + dAc;
}
}

function openCloseDiv(){
	if(openDiv==1){
		openDiv=2;
		CollapsiblePanel100.open();
		document.getElementById("wordHere").innerHTML="Close";
	}
	else
	{
		openDiv=1;
		CollapsiblePanel100.close();
		document.getElementById("wordHere").innerHTML="Change";
	}
}

//-------------------------------------------------------------------------

var thisNumber;

function bookMe(obj,sd){
//if (document.getElementById('nights').value < 3 && document.getElementById('roomtype' + obj).value = "Lodge"){
//alert("Self-Catering Lodges can only be booked for a minimum 3 night stay");
//}
//else {

thisNumber = obj;
var i = "rate" + obj;
var w = "addedHere" + obj;
var wa = "addedHereTwo";
var y = "nights" + obj;
var ya = "rate" + obj;
var x = "roomNo" + obj;
var z = "roomtype" + obj;

var rate = document.getElementById(i).value;
var roomtype = document.getElementById(z).value;
var roomnumber = document.getElementById(x).value;
var nights = document.getElementById(y).value;
var rate = document.getElementById(ya).value;
var datefrom = sd;

var thisLot;
thisLot = "roomNo=" + encodeURIComponent(document.getElementById(x).value);
thisLot = thisLot + "&roomtype=" + encodeURIComponent(document.getElementById(z).value);
thisLot = thisLot + "&datefrom="  + encodeURIComponent(datefrom);
thisLot = thisLot + "&nights="  + encodeURIComponent(document.getElementById(y).value);
thisLot = thisLot + "&rate="  + encodeURIComponent(document.getElementById(ya).value);
thisLot = thisLot + "&adults="  + encodeURIComponent(document.getElementById('adults').value);
thisLot = thisLot + "&children="  + encodeURIComponent(document.getElementById('children').value);
document.getElementById(w).innerHTML="<div class=alertTD>Room Added</div>";




xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="/working/addBooking.asp?"  
xmlHttp.onreadystatechange = alertContentsUser;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", thisLot.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(thisLot);
}

function alertContentsUser() {
document.getElementById('baskethere').innerHTML = "<center><img src=/graphics/loader.gif>";
      if (xmlHttp.readyState == 4) {
         if (xmlHttp.status == 200) {
            //alert(xmlHttp.responseText);
            returnBookings();
			document.location.href="#top";
            //document.getElementById('baskethere').innerHTML = result;         
         } else {
            alert('There was a problem with the request.');
         }
      }     
   }

 function removeRoom() { 
 if (confirm('Are you sure you want to DELETE selection(s)?\n\nThis action cannot be undone!\n\n')) {
	for (i=0; i<document.forms[0].elements.length; i++) { 
		el = document.forms[0].elements[i];
		if (el.type=="checkbox" && el.name.indexOf("checkbox")!=-1){
			if (el.checked) {
			var id = el.name.replace(/checkbox/, "");
			deleteme(id);
}
		} 
	}
	returnBookings();
	}
}

function deleteme(id){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="/working/delete.asp";
url=url+"?id="+id;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function confirmRoom(){
var id=1;
var checktrue=false;

for (i=0; i<document.forms[0].elements.length; i++) { 
		el = document.forms[0].elements[i];
		if (el.type=="checkbox" && el.name.indexOf("checkbox")!=-1){
			if (el.checked) {
			checktrue=true;			
		}
	}
} 
		
if(checktrue==false){
alert("You must tick the required room(s)");
}
else{

	for (i=0; i<document.forms[0].elements.length; i++) { 
		el = document.forms[0].elements[i];
		if (el.type=="checkbox" && el.name.indexOf("checkbox")!=-1){
			if (el.checked) {
			id = id + "," + el.name.replace(/checkbox/, "");
			
}
		} 
	}
	confirmme(id);

}
}

function confirmme(id){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="make-booking.asp";
url=url+"?id="+id;
document.location.href=url;
xmlHttp.onreadystatechange=goBookings;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}


// PLACE BOOKING ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function placeBooking(id) { 
//error checking........................................................................
var firstname = document.forms[0].firstname.value;
var surname = document.forms[0].surname.value;
var ad1 = document.forms[0].ad1.value;
var town = document.forms[0].town.value;
var county = document.forms[0].county.value;
var postcode = document.forms[0].postcode.value;
var telephone = document.forms[0].telephone.value;
var emailaddress = document.forms[0].emailaddress.value;
var adults = document.forms[0].adults.value;
var children = document.forms[0].children.value;
var findus = document.forms[0].findus.value;

// reset fields before alert .......................................................
document.getElementById("firstname1").innerHTML = "&nbsp;";
document.getElementById("surname1").innerHTML = "&nbsp;";
document.getElementById("ad11").innerHTML = "&nbsp;";
document.getElementById("town1").innerHTML = "&nbsp;";
document.getElementById("county1").innerHTML = "&nbsp;";
document.getElementById("postcode1").innerHTML = "&nbsp;";
document.getElementById("telephone1").innerHTML = "&nbsp;";
document.getElementById("emailaddress1").innerHTML = "&nbsp;";
document.getElementById("adults1").innerHTML = "&nbsp;";
document.getElementById("children1").innerHTML = "&nbsp;";
document.getElementById("findus1").innerHTML = "&nbsp;";
//..........................................................................................

var thisAlert="<table border='0' cellspacing='0' cellpadding='1'><tr><td class='alertTD2'><b>&nbsp;&lt;&nbsp;</td><td class='alertTD2'>Required Information&nbsp; &nbsp; </td></tr></table>";

if(firstname==""){
document.getElementById("firstname1").innerHTML = thisAlert;
document.location.href="#top";
}
else if (surname==""){
document.getElementById("surname1").innerHTML = thisAlert;
document.location.href="#top";
}
else if (ad1==""){
document.getElementById("ad11").innerHTML = thisAlert;
document.location.href="#top";
}
else if (town==""){
document.getElementById("town1").innerHTML = thisAlert;
document.location.href="#top";
}
else if (county==""){
document.getElementById("county1").innerHTML = thisAlert;
document.location.href="#top";
}
else if (postcode==""){
document.getElementById("postcode1").innerHTML = thisAlert;
document.location.href="#top";
}
else if (telephone==""){
document.getElementById("telephone1").innerHTML = thisAlert;
document.location.href="#top";
}
else if (emailaddress==""){
document.getElementById("emailaddress1").innerHTML = thisAlert;
document.location.href="#top";
}
else if (findus=="none"){
document.getElementById("findus1").innerHTML = thisAlert;
document.location.href="#top";
}
else if (adults==""){
document.getElementById("adults1").innerHTML = thisAlert;
document.location.href="#top";
}
else if (children==""){
document.getElementById("children1").innerHTML = thisAlert;
document.location.href="#top";
}
else {

var thisLot;
thisLot = "ititle=" + encodeURIComponent(document.forms[0].ititle.value);
thisLot = thisLot + "&firstname=" + encodeURIComponent(document.forms[0].firstname.value);
thisLot = thisLot + "&surname=" + encodeURIComponent(document.forms[0].surname.value);
thisLot = thisLot + "&ad1=" + encodeURIComponent(document.forms[0].ad1.value);
thisLot = thisLot + "&ad2=" + encodeURIComponent(document.forms[0].ad2.value);
thisLot = thisLot + "&ad3=" + encodeURIComponent(document.forms[0].ad3.value);
thisLot = thisLot + "&ad4=" + encodeURIComponent(document.forms[0].ad4.value);
thisLot = thisLot + "&ad5=" + encodeURIComponent(document.forms[0].ad5.value);
thisLot = thisLot + "&town=" + encodeURIComponent(document.forms[0].town.value);
thisLot = thisLot + "&county=" + encodeURIComponent(document.forms[0].county.value);
thisLot = thisLot + "&postcode=" + encodeURIComponent(document.forms[0].postcode.value);
thisLot = thisLot + "&icountry=" + encodeURIComponent(document.forms[0].icountry.value);
thisLot = thisLot + "&telephone=" + encodeURIComponent(document.forms[0].telephone.value);
thisLot = thisLot + "&emailaddress=" + encodeURIComponent(document.forms[0].emailaddress.value);
thisLot = thisLot + "&findus=" + encodeURIComponent(document.forms[0].findus.value);
thisLot = thisLot + "&adults=" + encodeURIComponent(document.forms[0].adults.value);
thisLot = thisLot + "&children=" + encodeURIComponent(document.forms[0].children.value);
//if()
thisLot = thisLot + "&cancontact=" + encodeURIComponent(document.forms[0].cancontact.value);
thisLot = thisLot + "&idstring=" + encodeURIComponent(document.forms[0].idstring.value);
thisLot = thisLot + "&pCode=" + encodeURIComponent(document.forms[0].pCode.value);
thisLot = thisLot + "&tCost2=" + encodeURIComponent(document.forms[0].tCost2.value);
thisLot = thisLot + "&pSaving=" + encodeURIComponent(document.forms[0].pSaving.value);
thisLot = thisLot + "&tCost3=" + encodeURIComponent(document.forms[0].tCost3.value);

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
  alert ("Your browser does not support AJAX!");
  return;
  } 
 // alert(thisLot);
var url="/working/makeBookingSSL.asp";
//document.location.href=url+"?"+thisLot;
xmlHttp.onreadystatechange = stateChanged100;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", thisLot.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(thisLot);
}
}

function stateChanged100() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("bookingsHere").innerHTML=xmlHttp.responseText;
setTimeout("initTransfer()",3000);
}
else {
document.getElementById("bookingsHere").innerHTML="<center><img src=/graphics/loader.gif></center>";
}
}

function initTransfer(){
var thisLot;
thisLot = "ititle=" + encodeURIComponent(document.forms[0].ititle.value);
thisLot = thisLot + "&firstname=" + encodeURIComponent(document.forms[0].firstname.value);
thisLot = thisLot + "&surname=" + encodeURIComponent(document.forms[0].surname.value);
thisLot = thisLot + "&telephone=" + encodeURIComponent(document.forms[0].telephone.value);
thisLot = thisLot + "&emailaddress=" + encodeURIComponent(document.forms[0].emailaddress.value);
thisLot = thisLot + "&bookingref=" + encodeURIComponent(document.forms[0].bookingref.value);
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
  alert ("Your browser does not support AJAX!");
  return;
  } 
//alert(thisLot);
var url="https://vault1.secured-url.com/portsonachanhotel/default.asp";
url=url+"?"+thisLot;
document.location.href=url;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", thisLot.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(thisLot);
}


///////////////////////////////////////////////////////////////////////////

function selectSarah(){

var chtrue;
if(document.forms[0].cb1.checked==true){
chtrue=true;
}
else {
chtrue=false;
}

	for (i=0; i<document.forms[0].elements.length; i++) { 
		el = document.forms[0].elements[i];
		if (el.type=="checkbox" && el.name.indexOf("checkbox")!=-1){
			if (chtrue==true) {
			el.checked=false;
			
}
else {
el.checked=true;
}
		} 
}
}

/////////////////////////

function calcJulian(isDate){

gregDate = new Date(isDate);
year = gregDate.getFullYear(); 
month = gregDate.getMonth()+1; 
day = gregDate.getDate();
A = Math.floor((7*(year+Math.floor((month+9)/12)))/4);
B = day+Math.floor((275*month)/9)
isJulian = (367*year)-A+B+1721014;
return isJulian; 
}

function validate(date1,date2){

tmp = date1.split("/")
xDate = tmp[1]+"/"+tmp[0]+"/"+tmp[2];
refDate = calcJulian(xDate);
tmp = date2.split("/")
xDate = tmp[1]+"/"+tmp[0]+"/"+tmp[2];
fwdDate = calcJulian(xDate);

if (fwdDate < refDate)
{
alert("Your arrival date is LATER THAN YOUR DEPARTURE date");
return false;
}
}

function goCode(){
thisLot =  "pCode="  + encodeURIComponent(document.getElementById('pCode').value);


xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="/working/checkCode.asp?"  
xmlHttp.onreadystatechange = returnCode;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlHttp.setRequestHeader("Content-length", thisLot.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(thisLot);
}

function returnCode() {
      if (xmlHttp.readyState == 4) {
         if (xmlHttp.status == 200) {
            //alert(xmlHttp.responseText);
            if(xmlHttp.responseText==1){
			alert("The code you have entered is invalid\n\nPlease try again...");
			}else if (xmlHttp.responseText==2){
			alert("Sorry, this promotional code is exclusively for use when booking a self-catering holiday");
			}
			else{
			calculatePcode(xmlHttp.responseText);
			}
			
         } else {
            alert('There was a problem with the request.');
         }
      }     
   }
   
   function calculatePcode(obj){
   var origC = document.forms[0].tCost2.value;
   var disc = obj;
   var discN = parseFloat((origC/100)*disc);
   document.getElementById("pcSave").innerHTML=discN.toFixed(2);
 	document.getElementById("pcCost").innerHTML=parseFloat(origC-discN).toFixed(2); 
   document.forms[0].pSaving.value=discN.toFixed(2);
   document.forms[0].tCost3.value=parseFloat(origC-discN).toFixed(2); 
   }
