﻿function AddToList(name, Region, HolidayDate, HolidayLength, Sleeps, Pets, Smoking, AccType, Longitude, Latitude, county, code, startdate, enddate, price, unitcode, bookable)
{
    var NewValue;
    if (isNull(readCookie("cottList")) != true)
	    {
	        NewValue = readCookie("cottList") + "," + name + "|" + Region + "|" + HolidayDate + "|" + HolidayLength + "|" + Sleeps + "|" + Pets + "|" + Smoking + "|" + AccType + "|" + Longitude + "|" + Latitude + "|" + county + "|" + code + "|" + startdate + "|" + enddate + "|" + price + "|" + unitcode + "|" + bookable;
		}
	else
		{
		    NewValue = name + "|" + Region + "|" + HolidayDate + "|" + HolidayLength + "|" + Sleeps + "|" + Pets + "|" + Smoking + "|" + AccType + "|" + Longitude + "|" + Latitude + "|" + county + "|" + code + "|" + startdate + "|" + enddate + "|" + price + "|" + unitcode + "|" + bookable;
		}
    createCookie("cottList",NewValue,30);
}

function GetList()
{
    if (isNull(readCookie("cottList")))
    {
        return "Your ShortList is empty";
    }
    else
    {
    var arrList=readCookie("cottList").split(",");
	var encArrList = readCookie("encCottList").split(",");
    var rValue = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><List>";
    var i=0;
    
    while(i < arrList.length)
    {
        arrItem=arrList[i].split("|");
        rValue = rValue + "<Cottage>";
        rValue = rValue + " <Name>" + arrItem[0] + "</Name>";
        rValue = rValue + " <Region>" + arrItem[1] + "</Region>";
        rValue = rValue + " <HolidayDate>" + arrItem[2] + "</HolidayDate>";
        rValue = rValue + " <HolidayLength>" + arrItem[3] + "</HolidayLength>";
        rValue = rValue + " <Sleeps>" + arrItem[4] + "</Sleeps>";
        rValue = rValue + " <Pets>" + arrItem[5] + "</Pets>";
        rValue = rValue + " <Smoking>" + arrItem[6] + "</Smoking>";
        rValue = rValue + " <AccType>" + arrItem[7] + "</AccType>";
        rValue = rValue + " <Longitude>" + arrItem[8] + "</Longitude>";
        rValue = rValue + " <Latitude>" + arrItem[9] + "</Latitude>";
        rValue = rValue + " <County>" + arrItem[10] + "</County>";
        rValue = rValue + " <Code>" + arrItem[11] + "</Code>";
        rValue = rValue + " <StartDate>" + arrItem[12] + "</StartDate>";
        rValue = rValue + " <EndDate>" + arrItem[13] + "</EndDate>";
        rValue = rValue + " <Price>" + arrItem[14] + "</Price>";
        rValue = rValue + " <unitCode>" + arrItem[15] + "</unitCode>";
        rValue = rValue + " <bookable>" + arrItem[16] + "</bookable>";
		rValue = rValue + " <encString>" + encArrList[i] + "</encString>";
        rValue = rValue + "</Cottage>";
        i++
    }    
     rValue = rValue + "</List>";
     return rValue;
    }
}

function ShowList()
{
    //document.write("<div id=\"shortlist\"></div>")
    xml=LoadXML();
    xsl=LoadXSLDoc("shortlist.xslt");
    // code for IE
    if (window.ActiveXObject)
      {
      ex=xml.transformNode(xsl);
      document.getElementById("shortlist").innerHTML=ex;
      }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation 
    && document.implementation.createDocument)
      {
      xsltProcessor=new XSLTProcessor();
      xsltProcessor.importStylesheet(xsl);
      resultDocument = xsltProcessor.transformToFragment(xml,document);
      document.getElementById("shortlist").appendChild(resultDocument);
      }
}
function AddToLodges(name, Region, HolidayDate, HolidayLength, Sleeps, Pets, Smoking, AccType, Longitude, Latitude, county, code, startdate, enddate, isPark, ProductType, price, unitcode, siteCode, siteName, bookable) {
    var NewValue;
    if (isNull(readCookie("lodgeList")) != true) {
        NewValue = readCookie("lodgeList") + "," + name + "|" + Region + "|" + HolidayDate + "|" + HolidayLength + "|" + Sleeps + "|" + Pets + "|" + Smoking + "|" + AccType + "|" + Longitude + "|" + Latitude + "|" + county + "|" + code + "|" + startdate + "|" + enddate + "|" + isPark + "|" + ProductType + "|" + price + "|" + unitcode + "|" + siteCode + "|" + siteName + "|" + bookable;
    }
    else {
        NewValue = name + "|" + Region + "|" + HolidayDate + "|" + HolidayLength + "|" + Sleeps + "|" + Pets + "|" + Smoking + "|" + AccType + "|" + Longitude + "|" + Latitude + "|" + county + "|" + code + "|" + startdate + "|" + enddate + "|" + isPark + "|" + ProductType + "|" + price + "|" + unitcode + "|" + siteCode + "|" + siteName + "|" + bookable;
    }
    createCookie("lodgeList", NewValue, 30);
}

function GetLodges() {
    if (isNull(readCookie("lodgeList"))) {
        return "Your ShortList is empty";
    }
    else {
        var arrList = readCookie("lodgeList").split(",");
		var encArrList = readCookie("encLodgeList").split(",");
        var rValue = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><List>";
        var i = 0;

        while (i < arrList.length) {
            arrItem = arrList[i].split("|");
            rValue = rValue + "<Cottage>";
            rValue = rValue + " <Name>" + arrItem[0] + "</Name>";
            rValue = rValue + " <Region>" + arrItem[1] + "</Region>";
            rValue = rValue + " <HolidayDate>" + arrItem[2] + "</HolidayDate>";
            rValue = rValue + " <HolidayLength>" + arrItem[3] + "</HolidayLength>";
            rValue = rValue + " <Sleeps>" + arrItem[4] + "</Sleeps>";
            rValue = rValue + " <Pets>" + arrItem[5] + "</Pets>";
            rValue = rValue + " <Smoking>" + arrItem[6] + "</Smoking>";
            rValue = rValue + " <AccType>" + arrItem[7] + "</AccType>";
            rValue = rValue + " <Longitude>" + arrItem[8] + "</Longitude>";
            rValue = rValue + " <Latitude>" + arrItem[9] + "</Latitude>";
            rValue = rValue + " <County>" + arrItem[10] + "</County>";
            rValue = rValue + " <Code>" + arrItem[11] + "</Code>";
            rValue = rValue + " <StartDate>" + arrItem[12] + "</StartDate>";
            rValue = rValue + " <EndDate>" + arrItem[13] + "</EndDate>";
            rValue = rValue + " <isPark>" + arrItem[14] + "</isPark>";
            rValue = rValue + " <ProductType>" + arrItem[15] + "</ProductType>";
            rValue = rValue + " <Price>" + arrItem[16] + "</Price>";
            rValue = rValue + " <unitCode>" + arrItem[17] + "</unitCode>";
            rValue = rValue + " <siteCode>" + arrItem[18] + "</siteCode>";
            rValue = rValue + " <siteName>" + arrItem[19] + "</siteName>";
            rValue = rValue + " <bookable>" + arrItem[20] + "</bookable>";
			rValue = rValue + " <encString>" + encArrList[i] + "</encString>";
            rValue = rValue + "</Cottage>";
            i++
        }
        rValue = rValue + "</List>";
        return rValue;
    }
}

function ShowLodges() {
    //document.write("<div id=\"shortlist\"></div>")
    xml = LoadLodgeXML();
    xsl = LoadXSLDoc("lodge-shortlist.xslt");
    // code for IE
    if (window.ActiveXObject) {
        ex = xml.transformNode(xsl);
        document.getElementById("shortlist").innerHTML = ex;
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation
    && document.implementation.createDocument) {
        xsltProcessor = new XSLTProcessor();
        xsltProcessor.importStylesheet(xsl);
        resultDocument = xsltProcessor.transformToFragment(xml, document);
        document.getElementById("shortlist").appendChild(resultDocument);
    }
}

//-----------------
//Private Functions
//-----------------
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function LoadXSLDoc(fname)
{
    var xmlDoc;
    // code for IE
    if (window.ActiveXObject)
      {
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation 
    && document.implementation.createDocument)
      {
      xmlDoc=document.implementation.createDocument("","",null);
      }
    else
      {
      alert('Your browser cannot handle this script');
      }
    xmlDoc.async=false;
    xmlDoc.load(fname);
    return(xmlDoc);
}

function LoadXML()
{
    var xmlDoc;
    // code for IE
    if (window.ActiveXObject)
      {
      
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async=false;
      xmlDoc.loadXML(GetList());
      }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation 
    && document.implementation.createDocument)
      {
      xmlDoc=document.implementation.createDocument("","",null);
      xmlDoc.async=false;
      parser=new DOMParser();
      xmlDoc=parser.parseFromString(GetList(),"text/xml");
      }
    else
      {
      alert('Your browser cannot handle this script');
      }
    return(xmlDoc);
}
function LoadLodgeXML() {
    var xmlDoc;
    // code for IE
    if (window.ActiveXObject) {

        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.loadXML(GetLodges());
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation
    && document.implementation.createDocument) {
        xmlDoc = document.implementation.createDocument("", "", null);
        xmlDoc.async = false;
        parser = new DOMParser();
        xmlDoc = parser.parseFromString(GetList(), "text/xml");
    }
    else {
        alert('Your browser cannot handle this script');
    }
    return (xmlDoc);
}

//-----------------
//Generic Functions
//-----------------
function isNull(val){return(val==null);}
function isEven(x) { return (x%2)?false:true; }
function isOdd(x) { return !isEven(x); }
