/* <<false>> if a visitor exits the booking engine without completing the booking process
   <<true>> if a visitor navigates between pages of booking engine
*/
var isInBookingProcess = false;
/*
	Display pop-up window on all the pages of the booking engine when a visitor exits the booking engine without completing the booking process.
*/
function PopUpClickToCallWindow(){
	/* Display pop-up window ONLY on exit from booking engine without completing the booking process
	*/
	if(!isInBookingProcess){
		setPageVisitedValues();
	}
}

/* Set the flag to not display pop-up window if a visitor navigates between pages of booking engine
*/
function BlockClickToCallPopup(){
	isInBookingProcess = true;
}

var workflowSequence = new WorkflowSequence();
function setPageVisitedValues(){
	var url = document.location;
	document.cookie	= "LastURL=" + escape(url);
	document.cookie	= "LastTitle=" + window.parent.document.title;
	document.cookie	= "LastFrom=" + window.parent.document.location.pathname;
	document.cookie	= "wasbrowsernavigated=true";
}

function isInNavigationSequence(){
	var rtr = false;
	var LastPage = getLastPageVisited();
	var currentURL = GetCallingPage ( workflowSequence, document.location );
	var sequence = new WorkflowSequence();
	var pageSequenceIndex=0;
	for ( var i = 0; i < sequence.pages.length; i ++ ){
		if ( currentURL == sequence.pages[i] ){
			pageSequenceIndex = i;
		}
	}
	var LastURL = GetCallingPage ( workflowSequence, LastPage.PageURL )
	if ( pageSequenceIndex > 0 ){
		rtr = ( LastURL == sequence.pages[pageSequenceIndex - 1 ] )
	}
	
	if ( pageSequenceIndex < sequence.pages.length - 1 ){
		rtr = ( LastURL == sequence.pages[pageSequenceIndex + 1 ] )
	}
	return rtr;
}

function getLastPageVisited(){
	var vp = new VisitedPage();
	var dc = document.cookie;
	var parsedDC = dc.replace ( /;$/, "" );
	var cookies = parsedDC.split(";");
	var name = "";
	var nev = null;
	var del = 0;
	for ( var i = 0; i < cookies.length; i ++ ){
		del = cookies[i].indexOf("=");
		name = cookies[i].substring(0,del).toLowerCase().replace(/^\s/, "");
		if ( name == "lasturl" ){
			vp.PageURL = unescape(cookies[i].substring(del+1,cookies[i].length));
			hasCookie = true;
		}
		if ( name == "lasttitle" ){
			vp.PageTitle = unescape(cookies[i].substring(del+1,cookies[i].length));
		}
		if ( name == "lastfrom" ){
			vp.PageFrom = unescape(cookies[i].substring(del+1,cookies[i].length));
		}
	}
	return vp;
}

function getCookie( cookieName ){
	var dc = document.cookie;
	var parsedDC = dc.replace ( /;$/, "" );
	var parsedDC = parsedDC.replace ( /;\s/gi, ";" );
	var cookies = parsedDC.split(";");
	var name = "";
	var nev = null;
	var del = 0;
	var value = "";
	for ( var i = 0; i < cookies.length; i ++ ){
		del = cookies[i].indexOf("=");
		name = cookies[i].substring(0,del).toLowerCase();
		if ( name == cookieName ){
			value = unescape(cookies[i].substring(del+1,cookies[i].length));
		}
	}
	return value;
}

function VisitedPage(){
	this.PageTitle = window.parent.document.title;
	this.PageURL = GetCallingPage( workflowSequence, document.location );
	this.PageFrom = window.parent.document.location.pathname;
}

function WorkflowSequence(){
	this.pages = ["departureinformation.aspx", "itinerary.aspx", "departureinformation.aspx", "bookcategory.aspx", "bookstateroom.aspx", "bookguestpref.aspx", "bookguestpref.aspx", "enterpaymentinfo.aspx", "htmlpage1","htmlpage2","htmlpage3" ]
}

function GetCallingPage( workflowSequence, url ){
	var urlLower = (url+"").toLowerCase();
	var page = "";
	for ( var i = 0; i < workflowSequence.pages.length; i++ ){
		if ( urlLower.indexOf(workflowSequence.pages[i]) > 0 ){
			page = workflowSequence.pages[i];
		}
	}
	return page;
}

function ShowClickToCallModal(){
	var page = getLastPageVisited();
	var calledPage="ClickToCall.aspx?Page="+page.PageURL+ "&From=" + page.PageFrom;
	window.showModalDialog(calledPage, "ClickToCall" , "dialogHeight:330px;dialogWidth:456px;help:0;resizable:0;status:0;scroll:0;");
}

function wasbrowsernavigated(){
	var isBrowserNavigated = ( ( getCookie( "wasbrowsernavigated" ) == "true" ) && isInNavigationSequence() )
	document.cookie = "wasbrowsernavigated=false";
	return isBrowserNavigated;
}

function ShowModalIfVisited(){
	if ( wasbrowsernavigated() ){
		ShowClickToCallModal();
	}
	else{
		document.body.visited = true;
	}
}
function appendToOnloadIE(){
	// parses function body, appends new call, and assigns new body.onload
	var anonymous = document.body.onload.toString();
	var aBody = anonymous.substring( anonymous.indexOf("{")+1, anonymous.lastIndexOf("}") ) ;
	aBody += "\nShowModalIfVisited()";
	document.body.onload = new Function (aBody);
}

function appendToOnloadMO(){
	// ASSUMPTION: this is designed to work with Carnival Popups, therefore, 
	// it is assumed that the CarnivalPopups.js file has already been loaded
	// and has assigned an Onload event if one didn't exist already.
	document.body.setAttribute("onload", document.body.getAttribute("onload") + "; ShowModalIfVisited();");
}

if ( navigator.appName.toLowerCase().indexOf("explorer") > 0 ){
    appendToOnloadIE();
}
else{
    appendToOnloadMO();
}
