// JavaScript Document
//
//For prod
siteURL = "https://www.royalgorgeroute.com/";
adminURL = "https://admin.royalgorgeroute.com/";
insecureAdminURL = "http://admin.royalgorgeroute.com/";
cartURL = "https://www.royalgorgeroute.com/content/cart/Itinerary.aspx";
purchaseCompleteUrl = "https://www.royalgorgeroute.com/content/cart/PurchaseComplete.aspx";

//For dev
/*siteURL = "http://localhost/royalgorgereservationsdev/";
adminURL = "http://admin.royalgorgereservations.com/";
cartURL = "http://localhost/royalgorgereservationsdev/content/cart/Itinerary.aspx";

//for local
/*siteURL = "http://localhost/cumbrestoltec/";
adminURL = "http://cumbrestoltecdev.fulgen.com";
cartURL = "http://localhost/cumbrestoltec/templateCart.html";*/

// Create the cart methods object
cartMethods = {};

// An array of cart item swf movies
registeredCartItems = [];

/*********************************	
// SWF MOVIE FINDER
*********************************/	
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	}else{
		if(document[movieName].length != undefined){
			return document[movieName][1];
		}
		return document[movieName];
	}
}
// Writes the cart items into the supplied cart div. 
// Cart Items are supplied from the stored items flash sharedobject.
function writeCartInformation( srcDivName, srcCartURL, srcStoredObject ){
	// If the administrator has sized the div that the cart will be written into
	// use the sizing provided. Otherwise, use a given default size
	var width = (document.getElementById(srcDivName).clientWidth > 0) ? document.getElementById(srcDivName).clientWidth : 360;
	var height = (document.getElementById(srcDivName).clientHeight > 0) ? document.getElementById(srcDivName).clientHeight : 40;
	//alert(srcStoredObject);
	// The retrieved stored Flash Object
	var storedObj = {};
	storedObj = srcStoredObject;
	
	// The extracted cart from the stored object.
	var returnArr  = [];
	returnArr = storedObj.cart;
	
	// The created an array that will hold the div and swfobject objects created
	// by each pricing type in the cart.
	var divsArr = [];
	
	// Create the Divs for each of the stored items and write a swfobject to 
	// each object. When each object is created, put it into the divsArr array.
	
	// Each div in the divsArr consists of a number of spans that will hold the pricing types. The 
	// div itself will hold the saleunit for a tripinstance and all of the spans for each 
	// of the pricingtypes for that tripinstance. Trip instance is the 
	if(returnArr){
		//
		var i = returnArr.length;
		while(i--){
			var currentTrip = returnArr[i];
			
			//Create an object that will hold both the new div html and the swfobject.
			var tripObj = {};
			
			// The divHTML prop is the HTML code for each block of saleunits It consists of a div 
			// that holds the title and a span that holds the swf. 
			tripObj.divHTML = "<div id='"+currentTrip.saleUnitName+"'><span class='cartItem'>"+currentTrip.departureDateFriendly+" "+currentTrip.saleUnitName+"</span></div>";
			
			// Add the new html to the target div supplied in the arguments.
			document.getElementById(srcDivName).innerHTML += "<div id='"+currentTrip.saleUnitName+"'><span class='cartItem'>"+currentTrip.departureDateFriendly+" "+currentTrip.saleUnitName+"</span></div>";
			
			// Create an array that will hold each swf object that will need to be written to the page.
			tripObj.swfObjects = [];
			
			// For each pricing type in the current trip's (unique tripinstance and saleunit) pricingtypes, 
			// create a swf object that will later be written to the corresponding span tag
			var j = 0;
			while( j < currentTrip.sortedPricingTypes.length ){
	
				// Create the div html to put the cartitem swf into the pricingTypeObject
				var pricingTypeObj = {};
				pricingTypeObj.divName = "cartItem_"+i+"_"+j; // id of the container div.
				pricingTypeObj.divHTML = "<div id='"+pricingTypeObj.divName+"' style='height:40px;'>loading...</div>"; // div html
				
				// Add the div html to the target div supplied in the arguments.
				document.getElementById(srcDivName).innerHTML += "<div id='"+pricingTypeObj.divName+"' style='height:40px;'>loading...</div>";
				
				// create the swf object and add the pricingtype properties from the saved
				// stored object
				var so = new SWFObject(siteURL+"flash/CartItem.swf", "cartItemSwf"+j, width, "40", "8", "#FFFFFF");
				so.addParam("wmode", "transparent");
				so.addVariable("serverAddress", adminURL);
				
				// Add the cartURL to the properties of the swfobject
				if(srcCartURL != undefined){
					so.addVariable("cartUrl",srcCartURL);
				}else{
					so.addVariable("cartUrl",cartURL);
				}
				
				// Add the swf to the global registered items array
				registeredCartItems.push("cartItemSwf"+j);
				
				// Insert all of the properties from the sharedobject
				for(var props in currentTrip.sortedPricingTypes[j]){
					so.addVariable(props, currentTrip.sortedPricingTypes[j][props]);
				}
				
				// add the swfobject to the current pricingTypeObject and push it into the swfObjects array.
				pricingTypeObj.swfObj = so;
				tripObj.swfObjects.push(pricingTypeObj);
				
				j++;
				
			}	
			
			divsArr.push(tripObj);
			
		}
	}
	// Loop through the divs aray and for each PricingTYpeObject, issue the "write" method
	// to show the swf object on the page in the appropriate div.
	for( i = 0; i < divsArr.length; i++){
		//
		for( j = 0; j < divsArr[i].swfObjects.length; j++){
			//
			divsArr[i].swfObjects[j].swfObj.write(divsArr[i].swfObjects[j].divName);
			//
		}
		//
	}
	
	// If there are not items in the cart, hide the divs and indicate that 
	// there are no items in the cart.
	if( returnArr == undefined || returnArr.length < 1){
		document.getElementById("orderInstructions").style.visibility = "hidden";	
		document.getElementById("cartInstructions").style.visibility = "hidden";
		document.getElementById("cartInstructions").style.position = "absolute";
		document.getElementById("cartContent").innerHTML = (storedObj.confirmationText != "") ?"": "There are no items in your cart. If you had items in your cart, they may have expired. All tickets are held for 15 minutes before they are removed from your reservation.";
		/*document.getElementById("cartContent").innerHTML = "There are no items in your cart. If you had items in your cart, they may have expired. All tickets are held for 10 minutes before they are removed from your reservation.";*/
		document.getElementById("cartUserInformation").innerHTML = "";	
		document.getElementById("cartConfirmation").innerHTML = (storedObj.confirmationText) ? storedObj.confirmationText : "";	
	}/*else if(returnArr.length < 1 ) {
		document.getElementById("orderInstructions").style.visibility = "hidden";	
		document.getElementById("cartInstructions").style.visibility = "hidden";
		document.getElementById("cartInstructions").style.position = "absolute";
		document.getElementById("cartContent").innerHTML = "There are no items in your cart. If you had items in your cart, they may have expired. All tickets are held for 10 minutes before they are removed from your reservation.";
		document.getElementById("cartUserInformation").innerHTML = "";	
		document.getElementById("cartConfirmation").innerHTML = (storedObj.confirmationText) ? storedObj.confirmationText : "";	
	}*/else{
		document.getElementById("cartConfirmation").innerHTML = "";	
		document.getElementById("orderInstructions").style.visibility = "visible";	
		document.getElementById("cartInstructions").style.visibility = "visible";
		document.getElementById("cartInstructions").style.position = "relative";
		document.getElementById("cartTotal").innerHTML = "<span class='cartTotal' align='right'>Your Total: "+storedObj.totalAmountFriendly;
		
		writeUserInformation("cartUserInformation",cartURL); 
	}
}
// Writes the cart items into the supplied cart div. 
// Cart Items are supplied from the stored items flash sharedobject.
function writeCart( srcDivName, contentHolder, srcCartURL, srcStoredObject ){
	// If the administrator has sized the div that the cart will be written into
	// use the sizing provided. Otherwise, use a given default size
	var width = (document.getElementById(srcDivName).clientWidth > 0) ? document.getElementById(srcDivName).clientWidth : 360;
	var height = (document.getElementById(srcDivName).clientHeight > 0) ? document.getElementById(srcDivName).clientHeight : 40;
	//alert(srcStoredObject);
	// The retrieved stored Flash Object
	var storedObj = {};
	storedObj = document.getElementById("cartData").getStoredItems();
	
	// The extracted cart from the stored object.
	var returnArr  = [];
	returnArr = storedObj.cart;
	
	// The created an array that will hold the div and swfobject objects created
	// by each pricing type in the cart.
	var divsArr = [];
	
	// Create the Divs for each of the stored items and write a swfobject to 
	// each object. When each object is created, put it into the divsArr array.
	
	// Each div in the divsArr consists of a number of spans that will hold the pricing types. The 
	// div itself will hold the saleunit for a tripinstance and all of the spans for each 
	// of the pricingtypes for that tripinstance. Trip instance is the 
	if(returnArr){
		//
		var i = returnArr.length;
		while(i--){
			var currentTrip = returnArr[i];
			
			//Create an object that will hold both the new div html and the swfobject.
			var tripObj = {};
			
			// The divHTML prop is the HTML code for each block of saleunits It consists of a div 
			// that holds the title and a span that holds the swf. 
			tripObj.divHTML = "<div id='"+currentTrip.saleUnitName+"'><span class='cartItem'>"+currentTrip.departureDateFriendly+" "+currentTrip.saleUnitName+"</span></div>";
			
			// Add the new html to the target div supplied in the arguments.
			document.getElementById(srcDivName).innerHTML += "<div id='"+currentTrip.saleUnitName+"'><span class='cartItem'>"+currentTrip.departureDateFriendly+" "+currentTrip.saleUnitName+"</span></div>";
			
			// Create an array that will hold each swf object that will need to be written to the page.
			tripObj.swfObjects = [];
			
			// For each pricing type in the current trip's (unique tripinstance and saleunit) pricingtypes, 
			// create a swf object that will later be written to the corresponding span tag
			var j = 0;
			while( j < currentTrip.sortedPricingTypes.length ){
	
				// Create the div html to put the cartitem swf into the pricingTypeObject
				var pricingTypeObj = {};
				pricingTypeObj.divName = "cartItem_"+i+"_"+j; // id of the container div.
				pricingTypeObj.divHTML = "<div id='"+pricingTypeObj.divName+"' style='height:40px;'>loading...</div>"; // div html
				
				// Add the div html to the target div supplied in the arguments.
				document.getElementById(srcDivName).innerHTML += "<div id='"+pricingTypeObj.divName+"' style='height:40px;'>loading...</div>";
				
				// create the swf object and add the pricingtype properties from the saved
				// stored object
				var so = new SWFObject(siteURL+"flash/CartItem.swf", "cartItemSwf"+j, width, "40", "8", "#FFFFFF");
				so.addParam("wmode", "transparent");
				so.addVariable("serverAddress", adminURL);
				
				// Add the cartURL to the properties of the swfobject
				if(srcCartURL != undefined){
					so.addVariable("cartUrl",srcCartURL);
				}else{
					so.addVariable("cartUrl",cartURL);
				}
				
				// Add the swf to the global registered items array
				registeredCartItems.push("cartItemSwf"+j);
				
				// Insert all of the properties from the sharedobject
				for(var props in currentTrip.sortedPricingTypes[j]){
					so.addVariable(props, currentTrip.sortedPricingTypes[j][props]);
				}
				
				// add the swfobject to the current pricingTypeObject and push it into the swfObjects array.
				pricingTypeObj.swfObj = so;
				tripObj.swfObjects.push(pricingTypeObj);
				
				j++;
				
			}	
			
			divsArr.push(tripObj);
			
		}
	}
	// Loop through the divs aray and for each PricingTYpeObject, issue the "write" method
	// to show the swf object on the page in the appropriate div.
	for( i = 0; i < divsArr.length; i++){
		//
		for( j = 0; j < divsArr[i].swfObjects.length; j++){
			//
			divsArr[i].swfObjects[j].swfObj.write(divsArr[i].swfObjects[j].divName);
			//
		}
		//
	}
	
	// If there are not items in the cart, hide the divs and indicate that 
	// there are no items in the cart.
	if(returnArr.length < 1){
		document.getElementById("orderInstructions").style.visibility = "hidden";	
		document.getElementById("cartInstructions").style.visibility = "hidden";
		document.getElementById("cartInstructions").style.position = "absolute";
		document.getElementById("cartContent").innerHTML = "There are no items in your cart. If you had items in your cart, they may have expired. All tickets are held for 10 minutes before they are removed from your reservation.";
		document.getElementById("cartUserInformation").innerHTML = "";	
		document.getElementById("cartConfirmation").innerHTML = (storedObj.confirmationText) ? storedObj.confirmationText : "";	
	}else{
		document.getElementById("cartConfirmation").innerHTML = "";	
		document.getElementById("orderInstructions").style.visibility = "visible";	
		document.getElementById("cartInstructions").style.visibility = "visible";
		document.getElementById("cartInstructions").style.position = "relative";
		document.getElementById("cartTotal").innerHTML = "<span class='cartTotal' align='right'>Your Total: "+storedObj.totalAmountFriendly;
		
		writeUserInformation("cartUserInformation",cartURL); 
	}
}

// Enables or disables the cartitems swfs. This is used when 
// the reservation is being sent to the server to stop further user input.
function enableItems(srcEnable){
	var i = 0;
	while( i < registeredCartItems.length ){
		
		// Each cartitem.swf and cart.swf page
		document.getElementById(registeredCartItems[i]).enabled(srcEnable);
		i++;
	}
}

// Writes the swf that holds access to the user's sharedObjects  to the page.
// These are the main items that will appear in the cart.
function writeStoredItems(divName, movieName, sharedObjectName ) {
	var so12 = new SWFObject(siteURL+"flash/StoredItems.swf",movieName,"1","1","8","#FFFFFF");
	so12.addParam("wmode","transparent");
	so12.addVariable("sharedObject",sharedObjectName);
	so12.addVariable("siteRoot",siteURL);
	so12.addVariable("serverAddress",adminURL);
	so12.write(divName);
}


// Writes the swf that holds access to the user's sharedObjects  to the page.
// These are the main items that will appear in the cart.
function writeStoredCartItems(divName, movieName, sharedObjectName, srcCartURL, srcTargetDiv ) {
	var so12 = new SWFObject(siteURL+"flash/StoredItems.swf",movieName,"1","1","8","#FFFFFF");
	so12.addParam("wmode","transparent");
	so12.addVariable("sharedObject",sharedObjectName);
	so12.addVariable("cartURL",srcCartURL);
	so12.addVariable("purchaseCompleteUrl",purchaseCompleteUrl);
	so12.addVariable("targetCartDiv",srcTargetDiv);
	so12.addVariable("siteRoot",siteURL);
	so12.addVariable("serverAddress",adminURL);
	so12.write(divName);
}

// Writes the swf that the user will use to configure their purchase to the supplied div.
// This is the "make reservations" swf.
function writeReservableItem(divName, saleUnitId, selectedDate, srcTitle, srcCartURL, srcRedirectUrl, srcHasProducts, srcColors, srcDateRanges, srcBlackoutDates ) {
	
	//saleunitid to be used
	var sendSaleUnitId = (saleUnitId != undefined) ? saleUnitId : 0; 
	
	//The administrator can show a selected date to have the movie start on.
	var sendSelectedDate = selectedDate; 
	
	//Title of the movie
	var sendTitle = srcTitle; 
	
	//URL that the cart swfs are displayed on.
	var sendCartUrl = (srcCartURL != undefined && srcCartURL != "") ? srcCartURL : cartURL; 
	
	//URL of the page that the user is directed to when they add something to their cart
	var sendRedirectUrl = ( srcRedirectUrl != undefined && srcRedirectUrl != "" ) ? srcRedirectUrl : cartURL; 
	
	// Determine the width of the div where the movie will be placed into and add that height to the 
	var width = document.getElementById(divName).clientWidth;
	var height = document.getElementById(divName).clientHeight;
	//
	// Write the swf to the div supplied in the args.
	var so1 = new SWFObject(siteURL+"Flash/ReservationsInterface.swf","Reservations",width,height,"8","#FFFFFF");
	//
	//
	//if (so1.installedVer.versionIsValid(8)) {   
		//
		so1.addParam("wmode", "transparent");
		so1.addVariable("saleUnitId",sendSaleUnitId); 
		so1.addVariable("selectedDate",sendSelectedDate);
		so1.addVariable("redirectUrl",sendRedirectUrl);
		so1.addVariable("title",sendTitle);
		so1.addVariable("cartUrl",sendCartUrl);
		so1.addVariable("siteRoot",siteURL);
		so1.addVariable("serverAddress",adminURL);
		so1.addVariable("eventDateRanges",srcDateRanges);
		so1.addVariable("blackoutDates",srcBlackoutDates);
		so1.addVariable("entryPage",document.location);
		so1.addVariable("hasProducts",srcHasProducts);
		//
		// Add the colors to the movie
		for( var props in srcColors){
			so1.addVariable(props,srcColors[props]);
		}
		//
		so1.write(divName);
	/*}else{
		alert("not valid");
		document.getElementById(divName).innerHTML = "The most current version of Flash Player was not detected on your machine. Please download Flash Player in order to make reservations.<br><a href='http://www.adobe.com/go/getflashplayer' target='blank'><img src='https://www.adobe.com/images/shared/download_buttons/get_flash_player.gif' alt='Get Adobe Flash player' /></a>";
	}*/
	
}
function writeReservableTripItem(divName, tripId, selectedDate, srcTitle, srcCartURL, srcRedirectUrl, srcColors ) {
	//
	var sendTripId = (tripId != undefined) ? tripId : 0;
	var sendSelectedDate = selectedDate;
	var sendTitle = srcTitle;
	var sendCartUrl = (srcCartURL != undefined && srcCartURL != "") ? srcCartURL : cartURL;
	var sendRedirectUrl = ( srcRedirectUrl != undefined && srcRedirectUrl != "" ) ? srcRedirectUrl : cartURL;
	//
	var width = document.getElementById(divName).clientWidth;
	var height = document.getElementById(divName).clientHeight;
	//
	var so1 = new SWFObject(siteURL+"Flash/ReservationsInterfaceTrip_v1_0.swf","Reservations",width,height,"8","#FFFFFF");
	so1.addParam("wmode", "transparent");
	so1.addVariable("tripId",tripId); 
	so1.addVariable("selectedDate",sendSelectedDate);
	so1.addVariable("redirectUrl",sendRedirectUrl);
	so1.addVariable("title",sendTitle);
	so1.addVariable("cartUrl",sendCartUrl);
	so1.addVariable("siteRoot",siteURL);
	so1.addVariable("serverAddress",adminURL);
	so1.addVariable("entryPage",document.location);
	//
	//add the colors to the movie
	for( var props in srcColors){
		so1.addVariable(props,srcColors[props]);
	}
	//
	so1.write(divName);
}

// Writes the billing information swf movie to the supplied div.
function writeUserInformation(divName, srcCartURL) {
	
	// Set the height and width
	var width = (document.getElementById(divName).clientWidth > 0) ? document.getElementById(divName).clientWidth : 460;
	var height = (document.getElementById(divName).clientHeight > 0) ? document.getElementById(divName).clientHeight : 600;
	
	// Write the object
	var so2 = new SWFObject(siteURL+"flash/Cart.swf","Cart",width,height,"8","#000000");
	so2.addParam("wmode","opaque");
	so2.addVariable("siteRoot",siteURL);
	so2.addVariable("serverAddress",adminURL);
	so2.addVariable("purchaseCompleteUrl",purchaseCompleteUrl);
	if(srcCartURL != undefined){
		so2.addVariable("cartUrl",srcCartURL);
	}else{
		so2.addVariable("cartUrl",cartURL);
	}
	so2.write(divName);
}

// FIXME: Obselete function. 
function checkForSave() {
	//	
	var confirmed = confirm("Are you sure you would like to save this reservation?");
	if(confirmed){
		document.getElementById("Cart").createReservation();
	}
	//
}
//