﻿cities=new Array("london", "birmingham", "bristol", "glasgow","manchester");
citieLFIDs=new Array(4189, 4201, 4202, 4203, 4204);
function checkCity(city)
{
    var dropdown = document.getElementById("propertyCode");
    if(dropdown!=null)
    {
        for(i=0; i<dropdown.options.length; i++)
        {
            if(dropdown.options[i].text.toLowerCase() == city.toLowerCase()) dropdown.options[i].selected = true;
        }
    }
    	
}

function daysBetweenDates() {
d1 = new Date(document.getElementById("arriveYear").value, document.getElementById("arriveMonth").value-1, document.getElementById("arriveDate").value);
d2 = new Date(document.getElementById("departYear").value, document.getElementById("departMonth").value-1, document.getElementById("departDate").value);

// The number of milliseconds in one day
var oneDay = 1000 * 60 * 60 * 24;

// Convert both dates to milliseconds
var date1_ms = d1.getTime();
var date2_ms = d2.getTime();

// Calculate the difference in milliseconds
var difference = Math.abs(date1_ms - date2_ms);

// Convert back to days and return
document.getElementById("numberOfNights").value = Math.round(difference/oneDay);
}

//Javascript for Booking Component//

function submitFunction(){
	var arrival = document.getElementById('startDate').value;
	var depart = document.getElementById('endDate').value;
	var rooms = 1;
	
	// variable for children have tobe entered into string in a certain order hence loop
	var children = document.getElementById('children')[document.getElementById('children').selectedIndex].value;
	var childNumbers = children;	
	//var childrenString = "";
	for(i=1; i<=childNumbers; i++){
			//childrenString += "&prsNumChildren=1&prsRoomAge=5";
			children += "&prsRoomAge=5";
	}
	
	var adults = document.getElementById('adults')[document.getElementById('adults').selectedIndex].value;
	
	//booking engine does not deal with 'adults' and 'children' 
	//- instead has occupancy value which is a total and needs to know how many of this total are children
	var occupancy = parseInt(adults, 10) + parseInt(children, 10);
	
	//for promo value - if statment stops an empty '=' added to end of string if no promo code is used.
	var accountId="";  
	var promo = document.getElementById('rateCode').value;	
	if(promo){
		code = new Array();
	code["PROMIF"]="M2L";code["PROGUA"]="M3L";code["BPE09"]="C4C";code["SPRC09"]="P3C";code["LOYAL"]="C1R";code["PARTNR"]="P1B";code["CBI09"]="R1D";code["SPLEDM"]="E4E";code["PKGLBG"]="C3R";code["PROCOR"]="C4R";code["SFTW01"]="H0O";code["SFTW02"]="H5O";code["SFTW03"]="H6O";code["SFTW04"]="H9O";code["SFTW05"]="H7O";code["SFTW06"]="H8O";code["SFTW07"]="H2O";code["SFTW08"]="H1O";code["SFTW09"]="H4O";code["SFTW10"]="H3O";code["BPE09"]="C4C";code["DLC09"]="I1A";code["IAHSA"]="R5C";code["RHSU09"]="S8E";code["STEP09"]="T2E";code["TWC10"]="O2W";code["ESSENT"]="B2N";code["NEGBDN"]="D2A";code["NEGBKT"]="B1T";code["NEGDNA"]="U3M";code["NEGMPZ"]="M9P";code["NEGUNI"]="M2E";code["NEGBKI"]="B3N";code["NEGMTW"]="U4M";code["NEGUNE"]="D9X";code["NEGAV4"]="A1V";code["NEGCA4"]="P2I";code["NEHAK2"]="A4N";code["NEGAOI"]="A5N";code["NEGAPP"]="A6N";code["NEGAXA"]="AXA";code["NEGBAR"]="B4N";code["NEGDTZ"]="D1Z";code["NEGMOD"]="U1E";code["NEGO2T"]="O1W";code["NEGUVR"]="A7N";code["NEGXPX"]="XCH";code["NEGAC2"]="Q1Q";code["NEGADI"]="ADI";code["NEGGEC"]="G5E";code["NEGQAI"]="R6C";code["NEGBBC"]="B5N";code["NEGPO2"]="R7C";code["PROWHP"]="W2H";code["BRITAC"]="T2A";code["STWC10"]="W3H";code["TEXAS"]="C9Y";code["NEGPKF"]="P1K";code["CLA01"]="C2I";code["WWTLU"]="L3U";code["SCOTOP"]="O1P";code["PFI09"]="H9U";code["HFIS10"]="H8H";code["PROBAF"]="H9G";code["PROGBB"]="H9K";code["PROANG"]="H9L";code["NEGEGT"]="E9F";code["BLUF10"]="U1W";code[" PROFET"]="U1H";code["PROLFF"]="L1V";code["REXAM"]="REX";code["PROZOO"]="T9Z";code["PROQVC"]="Q1V";code["JAZZ09"]="L1Z";code["NEGARC"]="O4A";code["NEGHOS"]="I4O";code["NEGTHS"]="I3O";code["PROMET"]="O3O";code["PROASD"]="A6E";code["CHOICE"]="H6O";code["KHOOTM"]="O7O";code["NEGHAR"]="W1A";code["TWYFRD"]="W1Y";code["NEG21A"]="D9U";code["PROZOO"]="H5O";code["TIMES+"]="H1O";code["BPE10"]="C4C";code["NEGIOP"]="I0I";code["ERBM10"]="O2Y";code["NEGTHK"]="TKR";code["SFTW06"]="H8O";code["NEGBCW"]="BE1";code["NEGGAR"]="GA1";code["NEGBCW"]="BE1";code["NEGSEC"]="SC2";code["SPRK10"]="SA1";code["LUUP10"]="UU1";code["NEGMPR"]="ME1";code["NEGCAM"]="CA1";code["NEGPKF"]="P1K";code["NEGPAC"]="PA4";code["EMPIRI"]="EM2";code["NEGVES"]="VE2";code["NEGART"]="AF2";code["NEGIPF"]="*NO72$";code["JUMBUC"]="JU1";code["NEGCHC"]="CH1";code["PKGPGT"]="PE1";code["TRAZOO"]="*HJN$";code["ASDA14"]="AS3";code["NEGBRB"]="BR3";code["NEGZOO"]="BR4";
		if (promo.length>3) {
			accountId=promo;
			promo=code[promo];
			if (promo==undefined) promo="";
		}
	}

	var Language = document.getElementById('whatLanguage').value;
	
	// 'city' and 'hotel' values are both taken from the hotel dropdown on the booking form
	var hotel = document.getElementById('propertyCode')[document.getElementById('propertyCode').selectedIndex].value;
	var City = document.getElementById('propertyCode')[document.getElementById('propertyCode').selectedIndex].text;
	if(City == "London"){
		City = "LON000";
	}
	else if(City=="Birmingham"){
		City = "BHX001";
	}
	else if(City=="Bristol"){
		City = "BRS000";
	}
	else if(City=="Glasgow"){
		City = "GLA000";
	}
	else if(City=="Manchester"){
		City = "MAN000";
	}
	else if(City=="Leeds"){
		City = "LBA001";
	}
	//document.form1.action="https://www.yourreservation.net/ibe/cityinn/main.html?calArrivalDateField="+arrival+"&calDepartureDateField="+depart+"&ccpSelectCity="+City+"&ccpSelectCountry=GB-XX&ccpSelectHotel="+hotel+"&language="+Language+"&occupancy="+occupancy+"&prsRoomCount="+rooms+"&prsNumChildren="+children+"&quick=quick&rateAccessCode"+promo; 
	//document.form1.action="https://www.yourreservation.net/ibe/cityinn/main.html?calArrivalDateField="+arrival+"&calDepartureDateField="+depart+"&ccpSelectCity="+City+"&ccpSelectCountry=GB-XX&ccpSelectHotel="+hotel+"&language="+Language+"&occupancy="+occupancy+"&prsRoomCount="+rooms+"&prsNumChildren="+children+"&quick=quick&rateAccessCode="+promo+"&accountType=travelagency-company-A&accountId="+accountId;
	document.form1.action="https://www.yourreservation.net/ibe/cityinn/main.html?calArrivalDateField="+arrival+"&calDepartureDateField="+depart+"&ccpSelectCity="+City+"&ccpSelectCountry=GB-XX&ccpSelectHotel="+hotel+"&language="+Language+"&occupancy="+occupancy+"&prsRoomCount="+rooms+"&prsNumChildren="+children+"&quick=quick";
	
	if(promo) document.form1.action +="&rateAccessCode="+promo+"&accountType=travelagency-company-A&accountId="+accountId;
}

function updateStart(cal)
{
    var date = document.getElementById("startDate2");
    var arr = date.value.split("-");
    var aday = document.getElementById("arriveDate");
    var amonth = document.getElementById("arriveMonth");
    var ayear = document.getElementById("arriveYear");
    aday.value = arr[0];
    amonth.value = arr[1];
    ayear.value = arr[2];
    //cal.hide();
    
   
    var start = document.getElementById("endDate").value;
    
    if(!start) 
    {
    var aday2 = parseInt(aday.value, 10) + 1;
    var theString = aday2 + "-" + amonth.value + "-" + ayear.value;
    document.getElementById("endDate2").value = theString;
    }
    document.getElementById("startDate").value = document.getElementById("startDate2").value;
}

function updateEnd(cal)
{
    var date = document.getElementById("endDate2");
    var arr = date.value.split("-");
    var aday = document.getElementById("departDate");
    var amonth = document.getElementById("departMonth");
    var ayear = document.getElementById("departYear");
    aday.value = arr[0];
    amonth.value = arr[1];
    ayear.value = arr[2];
    //cal.hide();
    
     
     var end = document.getElementById("startDate").value;
     
     if(!end) 
     {
      var aday2 = parseInt(aday.value, 10) - 1;
     var theString = aday2  + "-" + amonth.value + "-" + ayear.value;
     document.getElementById("startDate2").value = theString;
    }
     document.getElementById("endDate").value = document.getElementById("endDate2").value;
}

//Google Analytics pagtracker variable passed as parameter - its value determines whether form onclick attribute is fired later in this function
//This prevents booking engine from failing if Google Analytics code is compromised(by antivirus software for instance)
function checkSubmit_2009(pageTracker)
{
    var ok = true;
    var okPerRoom = true;
	
    var arrive = document.getElementById("startDate");
    var end = document.getElementById("endDate");
    var hotel = document.getElementById("propertyCode");
	var children = document.getElementById('children')[document.getElementById('children').selectedIndex];
	var adults = document.getElementById('adults')[document.getElementById('adults').selectedIndex];
	var rooms = "1";
	
	if((parseInt(children.value, 10) + parseInt(adults.value, 10))/(parseInt(rooms, 10)) >3) {okPerRoom = false;}
	
    if(arrive.value=="") ok = false;
    if(end.value=="") ok = false;
    if(hotel.value=="") ok = false;

    if(ok && okPerRoom)
    {
		daysBetweenDates();
		submitFunction()
		
		//if pagetracker variable is set correctly then form onsubmit is fired
		//if Google Analytics code causes javascript error the booking engine will work but not track the booking
		if(pageTracker){
			document.form1.onsubmit();
		}
		document.form1.submit();
    }
    else if(okPerRoom == false && ok == true)
	{
		alert("Maximum of 3 people per room");
	}
	else
    {
        alert("Please fill in all the details in the form");
    }
}

function checkSubmit()
{
    var ok = true;
    var arrive = document.getElementById("startDate");
    var end = document.getElementById("endDate");
    var hotel = document.getElementById("propertyCode");
    if(arrive.value=="") ok = false;
    if(end.value=="") ok = false;
    if(hotel.value=="") ok = false;

    if(ok)
    {
        daysBetweenDates();
        assignFeel();
        document.form1.onsubmit();
        document.form1.submit();
    }
    else
    {
        alert("Please fill in all the details in the form");
    }
}

function assignFeel()
{
    cities=new Array("LONCI", "BHXCI", "BRSCI", "GLACI","MANCI");
    looks =new Array(4189, 4201, 4202, 4203, 4204);
    var hotel = document.getElementById("propertyCode");
    var lookandfeel = document.getElementById("lookAndFeelId");
    
    for(var i=0; i< cities.length; i++)
    {
		if(cities[i]==hotel.value)
		{
		    lookandfeel.value = looks[i];
		}
	}		
}