﻿var months = new Array(13);
   months[0]  = "January";
   months[1]  = "February";
   months[2]  = "March";
   months[3]  = "April";
   months[4]  = "May";
   months[5]  = "June";
   months[6]  = "July";
   months[7]  = "August";
   months[8]  = "September";
   months[9]  = "October";
   months[10] = "November";
   months[11] = "December";

function AddToListBoat(name,date,duration,route,price,base,book,type,boatID)
{
    var NewValue;
    if (isNull(readCookie("lbListBoat")) != true)
	    {
		    NewValue = readCookie("lbListBoat") + "," + name + "|" + date + "|" + duration + "|" +  route + "|" + price + "|" + base + "|" + book + "|" + type + "|" + boatID;
		}
	else
		{
		    NewValue = name + "|" + date + "|" + duration + "|"  +  route + "|" + price + "|" + base + "|" + book + "|" + type + "|" + boatID;
		}
    createCookie("lbListBoat",NewValue,30);
	if(book != '1'){
    alert(name+' added to Shortlist')};
}

function RemoveFromListBoat(name,date,duration,route,price,base,book,type,boatID)
{
    var arrList=readCookie("lbListBoat").split(",");
    var i=0;
    var NewValue="";
    
    while(i < arrList.length)
    {
        arrItem=arrList[i].split("|");
        //if (((arrItem[0]!=name)&&(arrItem[1]!=date)&&(arrItem[2]!=duration)&&(arrItem[3]!=route)&&(arrItem[4]!=price))==false)
        if (arrItem[0]+arrItem[1]+arrItem[2]+arrItem[3]+arrItem[4]+arrItem[5]+arrItem[6]+arrItem[7]+arrItem[8]!=name+date+duration+route+price+base+book+type+boatID)
        {
            if (NewValue=="")
            {
                NewValue = arrItem[0] + "|" + arrItem[1] + "|" + arrItem[2] + "|"  +  arrItem[3] + "|" + arrItem[4] + "|" + arrItem[5] + "|"  +  arrItem[6] + "|" + arrItem[7] + "|" + arrItem[8];
            }
            else
            {
                NewValue = NewValue + "," + arrItem[0] + "|" + arrItem[1] + "|" + arrItem[2] + "|"  +  arrItem[3] + "|" + arrItem[4] + "|" + arrItem[5] + "|"  +  arrItem[6] + "|" + arrItem[7] + "|" + arrItem[8];
            }
        }
        i++
    }
    createCookie("lbListBoat",NewValue,30);
}

function GetListBoat()
{
    if (isNull(readCookie("lbListBoat")))
    {
        return "<List></List>";
    }
    else
    {
    var arrList=readCookie("lbListBoat").split(",");
    var rValue = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><List>";
    var i=0;
    while(i < arrList.length)
    {
		arrItem=arrList[i].split("|");
		
		arrDate=arrItem[1].split("/")
		var showDate = new Date(arrDate[0] + "/" + arrDate[1] + "/" + arrDate[2])
		var ShowMonth = showDate.getMonth()
		var ShowDay = showDate.getDay()
		var month_to_show 	
		var day_to_show
		switch(ShowMonth){
		case 0:
		month_to_show = " Jan "
		break;
		case 1:
		month_to_show = " Feb "
		break;
		case 2:
		month_to_show = " Mar "
		break;
		case 3:
		month_to_show = " Apr "
		break;
		case 4:
		month_to_show = " May "
		break;
		case 5:
		month_to_show = " Jun "
		break;
		case 6:
		month_to_show = " Jul "
		break;
		case 7:
		month_to_show = " Aug "
		break;
		case 8:
		month_to_show = " Sep "
		break;
		case 9:
		month_to_show = " Oct "
		break;
		case 10:
		month_to_show = " Nov "
		break;
		case 11:
		month_to_show = " Dec "
		break;
		default:
		no_of_days = " Jan "
		break;
		}
		
		switch(ShowDay){
		case 0:
		day_to_show = "Sun "
		break;
		case 1:
		day_to_show = "Mon "
		break;
		case 2:
		day_to_show = "Tue "
		break;
		case 3:
		day_to_show = "Wed "
		break;
		case 4:
		day_to_show = "Thu "
		break;
		case 5:
		day_to_show = "Fri "
		break;
		case 6:
		day_to_show = "Sat "
		break;
		default:
		day_to_show = "Sun "
		break;
		}

rValue = rValue + "<Boat>";
        rValue = rValue + " <Name>" + arrItem[0] + "</Name>";
        rValue = rValue + " <DepartureDateToShow>" + day_to_show + showDate.getDate() + month_to_show + showDate.getYear() + "</DepartureDateToShow>";
		rValue = rValue + " <DepartureDate>" + arrItem[1] + "</DepartureDate>";
        rValue = rValue + " <Duration>" + arrItem[2] + "</Duration>";
        rValue = rValue + " <Route>" + arrItem[3] + "</Route>";
        rValue = rValue + " <Price>" + arrItem[4] + "</Price>";
        rValue = rValue + " <Base>" + arrItem[5] + "</Base>";
		rValue = rValue + " <Type>" + arrItem[7] + "</Type>";
		rValue = rValue + " <BoatID>" + arrItem[8] + "</BoatID>";
        rValue = rValue + "</Boat>";
        i++
    }    
     rValue = rValue + "</List>";
     return rValue;
    }
}

function ShowListBoat()
{
    //document.write("<div id=\"shortlist\"></div>")
    xml=LoadXMLBoat();
    xsl=LoadXSLDocBoat("/_styles/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").replaceChild(resultDocument,document.getElementById("shortlist").firstChild);
      //document.getElementById("shortlist").appendChild(resultDocument);
      }
}

function PopulateFormBoat(Position,Boat,Route,Length,DepartureDate)
{
	var DDate = DepartureDate.split("/");
	if (DDate[1].length == 1){DDate[1] = '0' + DDate[1];}
	var f = document.boatingform;
	if (Position=='1st')
	{
		f.elements['1holboat'].value  = Boat;
		f.elements['1holroute'].value = Route;
		SelectDropDownValueBoat(f.elements['1hollength'],Length);
		SelectDropDownValueBoat(f.elements['sday'],DDate[1]);
		SelectDropDownValueBoat(f.elements['smonth'],months[parseInt(DDate[0])-1] + " " + DDate[2]);
	}
	else
	{
		f.elements['2holboat'].value  = Boat;
		f.elements['2holroute'].value = Route;
		SelectDropDownValueBoat(f.elements['2hollength'],Length);
		SelectDropDownValueBoat(f.elements['2sday'],DDate[1]);
		SelectDropDownValueBoat(f.elements['2smonth'],months[parseInt(DDate[0])-1] + " " + DDate[2]);
	}
}

//-----------------
//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 LoadXSLDocBoat(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 LoadXMLBoat()
{
    var xmlDoc;
    // code for IE
    if (window.ActiveXObject)
      {
      
      xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async=false;
      xmlDoc.loadXML(GetListBoat());
      }
    // 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(GetListBoat(),"text/xml");
      }
    else
      {
      alert('Your browser cannot handle this script');
      }
    return(xmlDoc);
}

function SelectDropDownValueBoat(Ddl,SelectValue)
{
for (var i = 0; i < Ddl.options.length; i++) {
  //alert('"' + trim(Ddl.options[i].text) + '" == "' + SelectValue + '"');
  if (trim(Ddl.options[i].text) == SelectValue) {
    Ddl.options[i].selected = true;
    break;
  }
}	
}

//-----------------
//Generic Functions
//-----------------
function isNull(val){return(val==null);}
function isEven(x) { return (x%2)?false:true; }
function isOdd(x) { return !isEven(x); }
function trim(str, chars) { return ltrim(rtrim(str, chars), chars); }
function ltrim(str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("^[" + chars + "]+", "g"), "");}
function rtrim(str, chars) { chars = chars || "\\s"; return str.replace(new RegExp("[" + chars + "]+$", "g"), "");}