
var IMG_SOURCE = "/cms/global/assets/images/promotion/uk/may_campaign_2009/";
var IMG_SOURCE2 = "/cms/global/assets/images/promotion/uk/novsale2009/thumbnails/";
var IMG_STAR = [["1*","1","<img height='9' width='10' alt='star' src='"+IMG_SOURCE+"star.gif'/>"],
["2*","2","<img height='9' width='20' alt='two star' src='"+IMG_SOURCE+"twostars.gif'/>"],
["2+*","2+","<img height='9' width='29' alt='two plus star' src='"+IMG_SOURCE+"twoplusstars.gif'/>"],
["2.5*","2.5","<img height='9' width='25' alt='two and a half star' src='"+IMG_SOURCE+"twohalfstars.gif'/>"],
["3*","3","<img height='9' width='30' alt='three star' src='"+IMG_SOURCE+"threestars.gif'/>"],
["3+*","3+","<img height='9' width='39' alt='three plus star' src='"+IMG_SOURCE+"threeplusstars.gif'/>"],
["3.5*","3.5","<img height='9' width='35' alt='three and a half star' src='"+IMG_SOURCE+"threehalfstars.gif'/>"],
["4*","4","<img height='9' width='39' alt='four star' src='"+IMG_SOURCE+"fourstars.gif'/>"],
["4+*","4+","<img height='9' width='50' alt='four plus star' src='"+IMG_SOURCE+"fourplusstars.gif'/>"],
["4.5*","4.5","<img height='9' width='45' alt='four and a half star' src='"+IMG_SOURCE+"fourhalfstars.gif'/>"],
["5*","5","<img height='9' width='50' alt='five star' src='"+IMG_SOURCE+"fivestars.gif'/>"]];
var ICON_FLIGHT = "<img height='18' width='17' alt='flight only' src='"+IMG_SOURCE+"flight_icon.gif'/>";
var ICON_FLIGHT_HOTEL = "<img height='18' width='37' alt='Flight + hotel' src='"+IMG_SOURCE+"flight_hotel_icon.gif'/>";
var ICON_FLIGHT_CAR = "<img height='18' width='37' alt='Flight + car' src='"+IMG_SOURCE+"flight_car_icon.gif'/>";
var HOTEL_LINK = [["Le Meridien Abu Dhabi","<a class='thickbox' href='/main/lb-hotel-lemeridienabudhabi/' title='View more information on this hotel.'>Le Meridien Abu Dhabi</a>"],
["Castilla","<a class='thickbox' href='/main/lb-hotel-castilla/' title='View more information on this hotel.'>Castilla</a>"],
["Side Star Park","<a class='thickbox' href='/main/lb-hotel-sidestarpark/' title='View more information on this hotel.'>Side Star Park</a>"]];
var TWIDDLY_CLOSED = "<div class='twiddly'>show more travel dates</div>";
var TWIDDLY_OPEN = "<div class='twiddly'>show less travel dates</div>";
var extrasData = null;
var extrasURL = "/cms/global/json/jansale2010/extras.json";
var inFile = openFile(extrasURL);
try {
extrasData = eval('(' + inFile + ')');
}
catch(e) {
alert(e);
}
function getDealCell(sTo, sFrom, sOrigin)
{
var hotelHTML = getHotelHTML(sTo, sFrom, sOrigin);
var carHTML = getCarHTML(sTo, sFrom, sOrigin);
var notesHTML = getNotesHTML(sTo, sFrom, sOrigin);
var temp = "";

if(hotelHTML != "")	{
temp += "<div class='dealIcon'>" + ICON_FLIGHT_HOTEL + "</div>";
temp += hotelHTML;
}


if(carHTML != "")
{
temp += "<div class='dealIcon'>" + ICON_FLIGHT_CAR + "</div>";
temp += carHTML;
}


if(notesHTML != "")	{
temp += notesHTML;
}


if(temp == "") {
temp = "&nbsp;";

temp = "<td class='flightHotelCellEmpty'>" + temp + "</td>";
}
else {

temp = "<td class='flightHotelCell'>" + temp + "</td>";
}

return temp;

}
function getHotelHTML(sTo, sFrom, sOrigin)
{
var temp = "";
var tempFirstDate = "";
var tempMoreDates = "";
var leadLine = "";
var matched = 0;
var lastHotelName = "";
var lastHotelBoard = "";
var row = null;
var priceFirstHotelFound = false;
for(var index = 0; index < extrasData.extras.length; index++) {
row = extrasData.extras[index];

if(row.deal_type.toLowerCase() != "hotel") {
continue;
}


if ((gTravelClass == 'M' || gTravelClass == 'W') && row.flight_class != 'M') {
continue;
}
if ((gTravelClass == 'J' || gTravelClass == 'F') && row.flight_class != 'J') {
continue;
}


if((row.to == sTo) && (row.from == sFrom)) {

if(row.hotel_name != lastHotelName){

temp += createDetailHTML(matched, leadLine, tempFirstDate, tempMoreDates);

matched = 0;

lastHotelName = row.hotel_name;
lastHotelBoard = row.hotel_board;
}
else if (row.hotel_board != lastHotelBoard){
temp += createDetailHTML(matched, leadLine, tempFirstDate, tempMoreDates);

matched = 0;

lastHotelBoard = row.hotel_board;
}



var price = getDealPriceFrom(row, sFrom, sOrigin);

if(price != null) {
if(matched == 0) {
leadLine = "<div class='leadPrice'>";
leadLine += "<a href=\"" + getDpUrl(sTo, sFrom, sOrigin);
leadLine += "\" title=\"Plan or book.\">";
leadLine += "&pound;" + price +"</a>";
leadLine += "<span class='offerHeading'>Flight + "+row.hotel_nights+ " nights hotel, "+row.hotel_board+"<sup>1</sup></span><br/>";
leadLine += "<span class='offerHotelName'>"+getPicForStarRating(row.hotel_stars);

if(row.hotel_name.indexOf("rex resorts") != -1) {
row.hotel_name.replace( "rex resorts", "<i>rex resorts</i>" )
leadLine += "<span class='hotelName'>" + row.hotel_name + "</span> ";

}

else {

leadLine += "<span class='hotelName'>" + row.hotel_name + "</span> ";
}

leadLine += "</span></div>";
tempFirstDate = row.outbound_travel_from + " - " + row.outbound_travel_to;
tempMoreDates = "";
matched++;

if (row.upgrade_addon != '' && row.upgrade_addon != null) {
priceFirstHotelFound = true;
}

}

else {

tempMoreDates += "<div class='detailPrice'>";
tempMoreDates += "<a href=\"" + getDpUrl(sTo, sFrom, sOrigin);
tempMoreDates += "\" title=\"Plan or book.\">";
tempMoreDates += "&pound;" + price + "</a></div> ";
tempMoreDates += "<div class='detailDates'>";
tempMoreDates += row.outbound_travel_from + " - " + row.outbound_travel_to;
tempMoreDates += "</div>";
tempMoreDates += "<div class='clearBoth peekFix'> </div>";
matched++;
}
}
}
}

temp += createDetailHTML(matched, leadLine, tempFirstDate, tempMoreDates);


if (priceFirstHotelFound == false && (gTravelClass == 'F' || gTravelClass == 'W')) {
gPackageFound = false;
temp = "";
} else {
gPackageFound = true;
}
return temp;
}
function getCarHTML(sTo, sFrom, sOrigin)
{
var temp = "";
var tempFirstDate = "";
var tempMoreDates = "";
var leadLine = "";
var matched = 0;
var lastHotelName = "";
var lastHotelBoard = "";
var row = null;
var priceCarUpgradeFound = false;
for(var index = 0; index < extrasData.extras.length; index++) {
row = extrasData.extras[index];

if(row.deal_type.toLowerCase() != "car") {
continue;
}


if ((gTravelClass == 'M' || gTravelClass == 'W') && row.flight_class != 'M') {
continue;
}
if ((gTravelClass == 'J' || gTravelClass == 'F') && row.flight_class != 'J') {
continue;
}


if((row.to == sTo) && (row.from == sFrom)) {


var price = getDealPriceFrom(row, sFrom, sOrigin);

if(price != null) {
if(matched == 0) {
leadLine = "<div class='leadPrice'>";
leadLine += "<a href=\"" + getCarDpUrl(row, sTo, sFrom, sOrigin);
leadLine += "\" title=\"Plan or book.\">";
leadLine += "&pound;" + price +"</a>";
leadLine += "<span class='offerHeading'>Flight + ";
leadLine += row.car_days;
leadLine += " days car rental</span>";

tempFirstDate += row.outbound_travel_from;
tempFirstDate += " - ";
tempFirstDate += row.outbound_travel_to;
tempFirstDate += "</div>";
tempMoreDates = "";

matched++;

if (row.upgrade_addon != '' && row.upgrade_addon != null) {
priceCarUpgradeFound = true;
}

}

else {

tempMoreDates += "<div class='detailPrice'>";
tempMoreDates += "<a href=\"" + getCarDpUrl(row, sTo, sFrom, sOrigin);
tempMoreDates += "\" title=\"Plan or book.\">";
tempMoreDates += "&pound;" + price + "</a></div> ";
tempMoreDates += "<div class='detailDates'>";
tempMoreDates += row.outbound_travel_from;
tempMoreDates += " - ";
tempMoreDates += row.outbound_travel_to;
tempMoreDates += "</div>";
tempMoreDates += "<div class='clearBoth peekFix'> </div>";

matched++;
}

}

}

}

temp += createDetailHTML(matched, leadLine, tempFirstDate, tempMoreDates);


if (priceCarUpgradeFound == false && (gTravelClass == 'F' || gTravelClass == 'W')) {
gPackageFound = false;
temp = "";
} else {
gPackageFound = true;
}
return temp;

}
function getNotesHTML(sTo, sFrom, sOrigin)
{
var temp = "";

if (gPackageFound == false) {
return temp;
}
for(var index = 0; index < extrasData.extras.length; index++)
{
var row = extrasData.extras[index];

if((row.to == sTo) && (row.from == sFrom) && (row.notes != ""))
{
temp = "<div class='note'>" + row.notes + "</div> ";
}
}

return temp;
}
function createDetailHTML(matched, leadLine, tempFirstDate, tempMoreDates)
{
var html = "";

if(matched == 1) {

html += "<div class='deal'>";
html += "<div class='dealOutline'>";
html += "<div class='topline'>";
html += leadLine;
html += "</div>"
html += "<div class='clearBoth peekFix'> </div>";
html += "<div class='leadDates'>";
html += tempFirstDate;
html += "</div>";
html += "</div>";
html += "</div>";
}

else if(matched > 1)
{

html += "<div class='deal'>";
html += "<div class='dealOutline'>";
html += "<div class='topline'>";
html += leadLine;
html += "</div>"
html += "<div class='clearBoth peekFix'> </div>";
html += "<div class='leadDates'>";
html += tempFirstDate;
html += "</div>";
html += TWIDDLY_CLOSED;
html += "</div>";
html += "<div class='dealDetail'>";
html += "<div class='topline'>";
html += leadLine;
html += "</div>"
html += "<div class='clearBoth peekFix'> </div>";
html += "<div class='leadDates'>";
html += tempFirstDate;

html += "</div>";
html += TWIDDLY_OPEN;
html +=	tempMoreDates;
html += "</div>";
html += "</div>";
}
return html;
}
function getDealPriceFrom(row, sFrom, sOrigin)
{
var stationToMatch;
var price = 0;

if(sOrigin == "LON") {
stationToMatch = sFrom;
}

else {
stationToMatch = sOrigin;
}


if(row.from == stationToMatch) {
price = parseInt(row.price);
}

else if (stationToMatch == "ABZ") price = parseInt(row.abz_price);

else if (stationToMatch == "BFS") price = parseInt(row.bfs_price);

else if (stationToMatch == "EDI") price = parseInt(row.edi_price);

else if (stationToMatch == "GLA") price = parseInt(row.gla_price);

else if (stationToMatch == "JER") price = parseInt(row.jer_price);

else if (stationToMatch == "MAN") price = parseInt(row.man_price);

else if (stationToMatch == "NCL") price = parseInt(row.ncl_price);


priceFirstHotel = row.upgrade_addon;
if ((gTravelClass == 'F' || gTravelClass == 'W') && priceFirstHotel != null && priceFirstHotel != '') {
price += parseInt(priceFirstHotel);
}
if(price == "")	{
return null;
}

else {
return price;
}
}
function getPicForStarRating(sStars)
{
for(var index = 0; index < IMG_STAR.length; ++index) {
if (sStars == IMG_STAR[index][0]||sStars == IMG_STAR[index][1])
{
return IMG_STAR[index][2];
}
}
return "<p>" + sStars + "<p>";
}
function getHTMLforThumbnail(sAirportCode, sDestName)
{
var sTemp = "";
var imageURL = IMG_SOURCE2 + "thumbnail_" + sAirportCode + ".jpg";


sTemp += "<img height='93' width='160' alt='" + sDestName.replace(/'/g, '&acute;') + ".' ";
	sTemp += "' src='" + imageURL + "'/>";



return sTemp;
}

function openFile(sURL)
{
var oRequest = getRequest();

if (oRequest != null)
{
oRequest.open("GET",sURL,false);
oRequest.setRequestHeader("User-Agent",navigator.userAgent);
oRequest.send(null)
if (oRequest.readyState == 4)
{
var response = oRequest.responseText;

response = response.replace(/\r\n/g, '');
return(response);
}
else
{

alert("failed to read data");
}
}
return (null);
}
function getRequest()
{
var oRequest = null;

try
{
oRequest = new XMLHttpRequest();
}
catch (e)
{
oRequest = null;
}
if (oRequest == null)
{
try
{
oRequest = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
oRequest = null;
}
}
if (oRequest == null)
{
try
{
oRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
oRequest = null;
}
}
return oRequest;

}
function getDpUrl(sTo, sFrom, sOrigin)
{
var temp = "";
var temp2 = "";
var urlStart = 0;
var cabinStart = 0;
var tempFirstDate = "";
var tempMoreDates = "";
var leadLine = "";
var matched = 0;
var lastHotelName = "";
var lastHotelBoard = "";
var row = null;

for(var index = 0; index < extrasData.extras.length; index++)
{
row = extrasData.extras[index];

if(row.deal_type.toLowerCase() != "hotel")
{
continue;
}

if((row.to == sTo) && (row.from == sFrom))
{


var price = getDealPriceFrom(row, sFrom, sOrigin);

if(price != null)
{
temp = row.url;

urlStart = row.url.search("/travel/");
if (urlStart != -1) {
temp = row.url.substring(urlStart);
}

temp = temp.replace("fromPkg=LON", "fromPkg=" + sFrom );


cabinStart = temp.search("cabin=");
if (cabinStart == -1) {

temp = temp.concat("&cabin="+gTravelClass);
} else {

temp = temp.replace("cabin=M", "cabin=" + gTravelClass );
temp = temp.replace("cabin=W", "cabin=" + gTravelClass );
temp = temp.replace("cabin=J", "cabin=" + gTravelClass );
temp = temp.replace("cabin=F", "cabin=" + gTravelClass );
}

temp = temp.replace("&", "&amp;");
}
}
}



if(temp == "") {
temp += "/main/FX?to=";
temp += sTo + "&amp;fromPkg=";
temp += sFrom;
}

return temp;

}
function getCarDpUrl(row, sTo, sFrom, sOrigin)
{
var cabinStart = 0;
var tempURL = row.url;

if((tempURL == null) || (tempURL == "")) {
tempURL += "/main/FX?saleOption=PAKFC&amp;to=";
tempURL += sTo + "&amp;fromPkg=";
tempURL += sFrom;
} else {

tempURL = tempURL.replace("fromPkg=LON", "fromPkg=" + sFrom );

cabinStart = tempURL.search("cabin=");
if (cabinStart == -1) {

tempURL = tempURL.concat("&cabin="+gTravelClass);
} else {

tempURL = tempURL.replace("cabin=M", "cabin=" + gTravelClass );
tempURL = tempURL.replace("cabin=W", "cabin=" + gTravelClass );
tempURL = tempURL.replace("cabin=J", "cabin=" + gTravelClass );
tempURL = tempURL.replace("cabin=F", "cabin=" + gTravelClass );
}

tempURL = tempURL.replace("saleOption=PAKFH", "saleOption=PAKFC");

tempURL = tempURL.replace("&", "&amp;");
}

return tempURL;

}

