// JavaScript by Casper Thun - September-November 2008

var sShortlist_UserID, ShortlistXMLDoc, ShortlistItemID, ShortlistCurrentItem;

function AddToShortlist(extravalues, id) {
	//showLoader();
	ShortlistCurrentItem = id;
	sQueryString = escape(window.location.search);
	var timestamp = new Date();
	sURL = '/includes/shortlist/addtoshortlist.asp?userid='+sShortlistUserID+'&search_type='+sCurrentSearch+'&sessionid='+sBYOSessionID+extravalues+'&ts='+timestamp+'&qs='+sQueryString;
	sURL=sURL.replace("'","");
	if(bDebug) {
		if(confirm('Open '+ sURL)) {
			window.open(sURL);
		}
	}
	setTimeout("loadAddToShortlistXMLDoc('"+sURL+"')", 100);
}

function SwitchShortlistButtons(){
	o1 = document.getElementById('addtoshortlist0');
	o2 = document.getElementById('addedtoshortlist0');
	o3 = document.getElementById('addtoshortlist0-0');
	o4 = document.getElementById('addedtoshortlist0-0');
	if(o1&&o2&&o3&&o4) {
		o1.style.display='none';
		o3.style.display='none';
		o2.style.display='';
		o4.style.display='';
	}
	bAddedToShortlistFromFullDetails = true;
}

function loadAddToShortlistXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		reqSL = new XMLHttpRequest();
		reqSL.onreadystatechange = processAddToShortlist;
		reqSL.open("GET", url, true);
		reqSL.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		reqSL = new ActiveXObject("Microsoft.XMLHTTP");
		if (reqSL) {
			reqSL.onreadystatechange = processAddToShortlist;
			reqSL.open("GET", url, true);
			reqSL.send();
		}
	}
}

function processAddToShortlist() {
	if (reqSL.readyState == 4) {
		if (reqSL.status == 200){
			ShortlistXMLDoc = reqSL.responseXML.documentElement;
			if(ShortlistXMLDoc.childNodes.item(0).nodeValue.toLowerCase()=='true') {
				if(ShortlistCurrentItem) {
					sInserted = GetAttribute(ShortlistXMLDoc.attributes.getNamedItem("Inserted"));
					sItemID = GetAttribute(ShortlistXMLDoc.attributes.getNamedItem("ItemID"));
					if(sInserted=='1'){
						sShortlistPositions+=ShortlistCurrentItem+'-'+sItemID+'|';
					}
					o1 = document.getElementById('addtoshortlist'+ShortlistCurrentItem);
					o2 = document.getElementById('addedtoshortlist'+ShortlistCurrentItem);
					if(o1&&o2){
						o1.style.display='none';
						o2.style.display='';
					}
					div = document.getElementById('tabShortlist');
					if(div.className.indexOf('inShortlist') < 0) {
						div.className = div.className + ' inShortlist';
					}
				}else{
					if(confirm('Item added to shortlist.\n\nClick OK to see your shortlist now or Cancel to continue browsing.')) {
						SwitchResult('result_tab5');
					}else{
						//setTimeout('RunResults(true,true)',200);	
					}
				}
			}else{
				alert('There was a problem adding the chosen item to your shortlist');
			}			
		}
	}
}

function ClearShortlist() {
	if(confirm('Are you sure you want to delete all the items in your shortlist?')) {
		showLoader('OtherFullContent');
		RemoveShortlistItem(0);
	}
}

function RemoveShortlistItem(id) {
	var timestamp = new Date();
	sURL = '/includes/shortlist/removefromshortlist.asp?user='+sShortlistUserID+'&key='+sShortlistUserKey+'&item='+id+'&timestamp='+timestamp;
	if(bDebug) {
		if(confirm('OPEN URL: '+sURL)) {
			window.open(sURL);
		}
	}
	showLoader('OtherFullContent');
	readFile(sURL);
	if(id==0) {
		sShortlistPositions='|';	
		bAlreadyAddedToShortlist=false;
		bAddedToShortlistFromFullDetails=false;
	}else{
		sShortlistPositions=sShortlistPositions.replace('-'+id+'|','|');	
	}

	if(sShortlistPositions.indexOf('-') < 0){
		div = document.getElementById('tabShortlist');
		div.className = div.className.replace(' inShortlist', '');
	}
	showShortlist();
}

function showShortlist() {
	oShortlistContent = document.getElementById('OtherFullContent');
	var timestamp = new Date();
	sURL = '/includes/shortlist/getshortlist.asp?user='+sShortlistUserID+'&key='+sShortlistUserKey+'&saved='+bShortlistSaved+'&fullaccess='+bShortlistFullAccess+'&timestamp='+timestamp;
	if(bDebug) {
		if(confirm('OPEN URL: '+sURL)) {
			window.open(sURL);
		}
	}
	oShortlistContent.innerHTML = readFileInAsync(sURL);
	div_switch('ResultContent',false, true);
	div_switch('OtherFullContent',true, true);
	bLoaderShowing = false;
}

function EnquireShortlistItem(id) {
	document.forms['frmResults'].EnquireTextID.value = id;
	document.forms['frmResults'].action = "/contact.asp";
	frmBYOSubmitBS();
}

//function BookShortlistItem(id) {
	//frmBYOSubmitBS(true);
	//setTimeout('BookShortlistItemActual('+id+');',500);
//}

function BookShortlistItem(id) {
	ShortlistItemID = id;
	frmBYOSubmitBS(true);
	var timestamp = new Date();
	sURL = '/includes/shortlist/bookshortlist.asp?user='+sShortlistUserID+'&key='+sShortlistUserKey+'&item='+id+'&timestamp='+timestamp;
	if(bDebug) {
		if(confirm('OPEN URL: '+sURL)) {
			window.open(sURL);
		}
	}
	setTimeout("loadBookShortlistXMLDoc('"+sURL+"')", 100);
}

function loadBookShortlistXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		reqBS = new XMLHttpRequest();
		reqBS.onreadystatechange = processBookShortlist;
		reqBS.open("GET", url, true);
		reqBS.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		reqBS = new ActiveXObject("Microsoft.XMLHTTP");
		if (reqBS) {
			reqBS.onreadystatechange = processBookShortlist;
			reqBS.open("GET", url, true);
			reqBS.send();
		}
	}
}

function processBookShortlist() {
	var bAvailable = true;	
	if (reqBS.readyState == 4) {
		if (reqBS.status == 200){
			oXMLDoc = reqBS.responseXML.documentElement;
			if(oXMLDoc) {
				iResultCount = parseFloat(GetNode(oXMLDoc.getElementsByTagName('ResultCount')[0].childNodes.item(0)));
				sItem = GetNode(oXMLDoc.getElementsByTagName('Item')[0].childNodes.item(0));
				if(iResultCount>0) {
					sBYOQuery = GetNode(oXMLDoc.getElementsByTagName('BYOQuery')[0].childNodes.item(0));		
					if(sBYOQuery) {
						sBYOQuery = sBYOQuery.replace('?&','?');
						sBYOQuery = sBYOQuery.replace(/&xmlout=true/gi,'');
					}
					sSearchType = GetNode(oXMLDoc.getElementsByTagName('SearchType')[0].childNodes.item(0));
					sPackage = GetNode(oXMLDoc.getElementsByTagName('Package')[0].childNodes.item(0));
					sSession = GetNode(oXMLDoc.getElementsByTagName('Session')[0].childNodes.item(0));
					sParentID = GetNode(oXMLDoc.getElementsByTagName('ParentID')[0].childNodes.item(0));
					sViewState = GetNode(oXMLDoc.getElementsByTagName('ViewState')[0].childNodes.item(0));
					setLevel('PRICING');
					setViewState(sViewState);
					setSessionID(sSession);
					setSearchType(sSearchType);
					if(sSearchType.toLowerCase()=='packageholiday') {
						oPackage = oXMLDoc.getElementsByTagName('PackageHoliday');
						sItineraryId = GetNode(oPackage[0].getElementsByTagName('ItineraryId')[0].childNodes.item(0));
						sProvider = GetNode(oPackage[0].getElementsByTagName('Provider')[0].childNodes.item(0));
						setPackageHoliday(sProvider,sItineraryId);
					}else if(sSearchType.toLowerCase()=='hotel') {
						oHotel = oXMLDoc.getElementsByTagName('Hotel');
						sProvider = GetNode(oHotel[0].getElementsByTagName('Provider')[0].childNodes.item(0));
						sAccommCode = GetNode(oHotel[0].getElementsByTagName('AccommCode')[0].childNodes.item(0));
						sBoardBasis = GetNode(oHotel[0].getElementsByTagName('BoardBasis')[0].childNodes.item(0));
						setAccomm(sAccommCode,sProvider,'',sParentID,sSession,'Hotel','',sBoardBasis);
					}else if(sSearchType.toLowerCase()=='flight_hotel_ph') {
						oFlightHotel = oXMLDoc.getElementsByTagName('FlightHotel');
						document.getElementById('frmResults').action = '/buildyourown/Results/Results_xsl.aspx' + sBYOQuery;
						
						if(sPackage=='true'){
						    setSearchType("PackageHoliday");
						    sItineraryId = GetNode(oFlightHotel[0].getElementsByTagName('AccommCode')[0].childNodes.item(0));
						    sProvider = GetNode(oFlightHotel[0].getElementsByTagName('HotelProvider')[0].childNodes.item(0));
						    
						    setPackageHoliday(sProvider,sItineraryId);
						}else{
						    setSearchType("Flight_Hotel");
						    sHotelProvider = GetNode(oFlightHotel[0].getElementsByTagName('HotelProvider')[0].childNodes.item(0));
						    sAccommCode = GetNode(oFlightHotel[0].getElementsByTagName('AccommCode')[0].childNodes.item(0));
						    sBoardBasis = GetNode(oFlightHotel[0].getElementsByTagName('BoardBasis')[0].childNodes.item(0));
						    sFlight_Provider = GetNode(oFlightHotel[0].getElementsByTagName('FlightProvider')[0].childNodes.item(0));
						    sFlight_FareID = GetNode(oFlightHotel[0].getElementsByTagName('FareID')[0].childNodes.item(0));
						    sFlight_CSIFareID = GetNode(oFlightHotel[0].getElementsByTagName('FareCSIID')[0].childNodes.item(0));
    						
						    //alert(sBYOQuery);
    						
						    //alert(sFlight_FareID);
						    //alert(sFlight_CSIFareID);
						    //alert(sFlight_Provider);
    						
						    
						    //alert("setAccomm(" + sAccommCode + "," + sHotelProvider + ",''," + sParentID + "," + sSession + ",'Flight_Hotel',''," + sBoardBasis + ")");
						    setAccomm(sAccommCode,sHotelProvider,'',sParentID,sSession,'Flight_Hotel','',sBoardBasis);
						    //alert("setFlight(" + sFlight_FareID + "," + sFlight_CSIFareID + "," + sFlight_Provider +",'')");
						    setFlight(sFlight_FareID,sFlight_CSIFareID,sFlight_Provider,'');
						}
					}
					setTimeout("frmBYOSubmitBS(false)",300);
				}else{
					bAvailable = false;	
					//alert('1');
				}
			}else{
				bAvailable = false;	
				//alert('2');
			}
		}else{
			bAvailable = false;	
			//alert('3');
		}
	}
	if(!bAvailable) {
		SetToEnquiryShortlistItem(ShortlistItemID);
		div_switch('websiteDiv',true,true);
		div_switch('pleasewaitDiv',false,true);
		alert('The selected item is no longer available. Please click the enquire button for more information.');	
	}
}

function SetToEnquiryShortlistItem(id) {
	var timestamp = new Date();
	sURL = '/includes/shortlist/settoenquiryshortlistitem.asp?user='+sShortlistUserID+'&key='+sShortlistUserKey+'&item='+id+'&timestamp='+timestamp;
	if(bDebug) {
		if(confirm('OPEN URL: '+sURL)) {
			window.open(sURL);
		}
	}
	showLoader('OtherFullContent');
	readFile(sURL);
	showShortlist();
}


function MoreFriends() {
	oCopyFrom = document.getElementById('stfCopyFrom');	
	oCopyTo = document.getElementById('stfCopyTo');	
	oCounter = document.getElementById('stfFriendCount');	
	if(oCopyFrom && oCopyTo && oCounter) {
		oEmails = document.getElementsByName('SendShortlist_Email');
		var oEmailArray = new Array();
		for(x=0;x<oEmails.length;x++) {
			if(oEmails[x].value){
				oEmailArray[x] = oEmails[x].value;
			}
		}
		sFromHTML = oCopyFrom.innerHTML;
		oCopyTo.innerHTML += sFromHTML;
		iNumber = parseFloat(oCounter.innerHTML.split(' ')[0]);
		iNewNumber = iNumber+1;
		sNumberText = 'friend';
		if(iNewNumber!=1){sNumberText += 's';}
		oCounter.innerHTML = iNewNumber+' '+sNumberText;
		for(x=0;x<oEmailArray.length;x++) {
			if(oEmailArray[x]) {
				oEmails[x].value = oEmailArray[x]; 
			}
		}
		oEmails[oEmails.length-1].value = '';
	}	
}

function SendShortlist() {
	var e = '';
	var sPostVal='';
	var sShortlistSaveAs = '';
	oEmails = document.getElementsByName('SendShortlist_Email');
	sName = document.getElementsByName('SendShortlist_Name')[0].value.replace(/ /g,'%20');
	sMessage = document.getElementsByName('SendShortlist_Message')[0].value.replace(/ /g,'%20');
	sShortlistName = document.getElementsByName('SendShortlist_ShortlistName')[0].value.replace(/ /g,'%20');
	if(document.getElementsByName('SendShortlist_ShortlistSaveAs')[0]){
		sShortlistSaveAs = document.getElementsByName('SendShortlist_ShortlistSaveAs')[0].value.replace(/ /g,'%20');
	}
	if(oEmails.length>0){
		if(!sName){e+='- Please enter your name\n';}
		if(!sShortlistName && !sShortlistSaveAs){e+='- Please enter a name for your shortlist\n';}
		sPostVal+='name='+escape(sName);
		if(sMessage) {
			sPostVal+='&message='+sMessage.replace(/\n/g, '<br/>');
		}
		for(x=0;x<oEmails.length;x++) {
			sEmail = oEmails[x].value;
			if(!/.+@[^.]+(\.[^.]+)+/.test(sEmail) || isValidField(sEmail,2) == false){
				e+='- Please enter a valid email address for friend '+parseFloat(x+1)+'\n';
			}else{
				sPostVal+='&email='+sEmail;
			}
		}
		if(sShortlistSaveAs) {
			sPostVal+='&ShortlistSaveAs='+sShortlistSaveAs;
		}
		sPostVal+='&shortlist_userid='+sShortlistUserID;
		sPostVal+='&shortlist_key='+sShortlistUserKey;
		sPostVal+='&ShortlistName='+sShortlistName;
	}
	if(e) {
		alert(e);
		return false;
	}else{
		var sFriendText='friend';
		if(oEmails.length>1){sFriendText+='s';}
		showLoader('OtherFullContent');
		readFileInAsync('/includes/shortlist/sendshortlist.asp',true,sPostVal);	
		alert('Shortlist has been sent to your '+sFriendText+'.');
		showShortlist();
	}
}

function SaveShortlist() {
	var e = '';
	sEmail = document.getElementsByName('SaveShortlist_Email')[0].value;
	sName = document.getElementsByName('SaveShortlist_Name')[0].value.replace(/ /g,'%20');
	
	if(!sName){e+='- Please give your shortlist a name\n';}
	if(!/.+@[^.]+(\.[^.]+)+/.test(sEmail) || isValidField(sEmail,2) == false){
		e+='- Please enter a valid email address\n';
	}
	
	if(e) {
		alert(e);
		return false;
	}else{
		showLoader('OtherFullContent');
		sPostVal='shortlist_userid='+sShortlistUserID;
		sPostVal+='&shortlist_key='+sShortlistUserKey;
		sPostVal+='&name='+escape(sName);
		sPostVal+='&email='+sEmail;
		readFileInAsync('/includes/shortlist/saveshortlist.asp',true,sPostVal);	
		alert('Shortlist successfully saved and sent to your email address');
		showShortlist();
	}
	
}