var g_comet;
var g_action;
var g_dropdown;
var g_targetDropdown;
var g_firstSailDateOption = "When do you want to go?";
var g_firstDurationOption = "How many nights?";
var g_firstCruiseLineOption = "Which cruise line?";

function pageLoad()
{
	//initSailDateDropDown();
	//initDurationDropDown();
	//initCruiseLineDropDown();
}

function initSailDateDropDown(ptext)
{
	document.cruiseSearchForm.sailDate.options.length = 0;
	var g_opt_1stSailDate = document.createElement("OPTION");
	document.cruiseSearchForm.sailDate.options.add(g_opt_1stSailDate);
	g_opt_1stSailDate.innerText = ptext;
	g_opt_1stSailDate.value = "";
}

function initDurationDropDown(ptext)
{
	document.cruiseSearchForm.duration.options.length = 0;
	var g_opt_1stDuration = document.createElement("OPTION");
	document.cruiseSearchForm.duration.options.add(g_opt_1stDuration);
	g_opt_1stDuration.innerText = ptext;
	g_opt_1stDuration.value = "";
}

function initCruiseLineDropDown(ptext)
{
	document.cruiseSearchForm.cruiseLine.options.length = 0;
	var g_opt_1stCruiseLine = document.createElement("OPTION");
	document.cruiseSearchForm.cruiseLine.options.add(g_opt_1stCruiseLine);
	g_opt_1stCruiseLine.innerText = ptext;
	g_opt_1stCruiseLine.value = "";
}

function changeDropDown(pobj)
{
	var lurl;
	g_dropdown = pobj;
	g_action = pobj.name;
	clearValues(g_action);
	lurl
	=   "quick_search?action=" + pobj.name
	+   "&destination=" + document.cruiseSearchForm.destination.value
	+   "&sailDate=" + (document.cruiseSearchForm.sailDate ? document.cruiseSearchForm.sailDate.value : "")
	+   "&duration=" + (document.cruiseSearchForm.duration ? document.cruiseSearchForm.duration.value : "")
	+   "&cruiseLine=" + (document.cruiseSearchForm.cruiseLine ? document.cruiseSearchForm.cruiseLine.value : "")
	;
	//alert(lurl);
	if(typeof XMLHttpRequest != "undefined")
	{
		g_comet = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		g_comet = new ActiveXObject("Microsoft.XMLHTTP");
	}

	g_comet.open("GET", lurl, true);
	g_comet.onreadystatechange = processChange;
	g_comet.send();
}

function clearValues(paction)
{
	if(paction == "destination")
	{
		document.cruiseSearchForm.sailDate.value = "";
		document.cruiseSearchForm.duration.value = "";
		document.cruiseSearchForm.cruiseLine.value = "";
	}
	else if(paction == "sailDate")
	{
		document.cruiseSearchForm.duration.value = "";
		document.cruiseSearchForm.cruiseLine.value = "";
	}
	else if(paction == "duration")
	{
		document.cruiseSearchForm.cruiseLine.value = "";
	}
}

function processChange()
{
	var thexml;
	if (g_comet.readyState == 4)
	{
		if (g_comet.status == 200)
		{
			setDropDowns();
			thexml = g_comet.responseXML
			if(thexml.documentElement)
			{
				if(thexml.documentElement.hasChildNodes())
				{
					var descNodes = thexml.getElementsByTagName("Description");
					var valNodes = thexml.getElementsByTagName("Value");
					for(var i = 0; i < descNodes.length; i++)
					{
						var opt = document.createElement("OPTION");
						g_targetDropdown.options.add(opt);
						opt.innerText = descNodes.item(i).text;
						opt.value = valNodes.item(i).text;
					}
				}
			}
		}
	}
}

function setDropDowns()
{
	if(g_action == "destination")
	{
		initSailDateDropDown(g_firstSailDateOption);
		initDurationDropDown(g_blankopt);
		initCruiseLineDropDown(g_blankopt);
		g_targetDropdown = document.cruiseSearchForm.sailDate;
	}
	else if(g_action == "sailDate")
	{
		initDurationDropDown(g_firstDurationOption);
		initCruiseLineDropDown(g_blankopt);
		g_targetDropdown = document.cruiseSearchForm.duration;
	}
	else if(g_action == "duration")
	{
		initCruiseLineDropDown(g_firstCruiseLineOption);
		g_targetDropdown = document.cruiseSearchForm.cruiseLine;
	}
	else if(g_action == "cruiseLine")
	{
	}
}


// bdp - end ajax code


function checkRate(scode, snum, iname, sid, sdate, sabbr) {
    document.itin.ship_code.value = scode;
    document.itin.supplier_num.value = snum;
    document.itin.iti_name.value = iname;
    document.itin.sailing_id.value = sid;
    document.itin.start_date.value = sdate;
    document.itin.supplier_abbr.value = sabbr;
    document.itin.submit();
}

function gotoPage(page) {
	var count = getCompareCount();
	if (count > 0 && confirm(compareConfirmAlert)) {
		document.cruise_compare.submit();
	} else {
	    document.page_navigator.page.value = page;
	    document.page_navigator.submit();
	}
}

function clearField(fieldName) {
    switch (fieldName.name) {
        case 'destination' :
            document.cruiseSearchForm.sailDate.value = '';
            document.cruiseSearchForm.duration.value = '';
            document.cruiseSearchForm.cruiseLine.value = '';
            break;
        case 'sailDate' :
            document.cruiseSearchForm.duration.value = '';
            document.cruiseSearchForm.cruiseLine.value = '';
            break;
        case 'duration' :
            document.cruiseSearchForm.cruiseLine.value = '';
            break;
    }
}

function updateParentSearchBox() {

	parent.document.getElementById("destinationDiv").innerHTML = document.getElementById("destinationDiv").innerHTML;
	parent.document.getElementById("sailDateDiv").innerHTML = document.getElementById("sailDateDiv").innerHTML;
	parent.document.getElementById("durationDiv").innerHTML = document.getElementById("durationDiv").innerHTML;
	parent.document.getElementById("cruiseLineDiv").innerHTML = document.getElementById("cruiseLineDiv").innerHTML;
	//updateCount();
	//parent.focus();
	
	//alert(document.referrer);
	
	//searchFrame.history.back();
	//parent.searchFrame.history.back();
	
	//alert(history.previous);
	//if (history.current == history.previous) {   
//		history.go(0);
//	}


}

// update the cruise count on the parent frame
function updateCount() {
	var count = document.cruiseSearchForm.count.value;

	if (count > 1) {
		message = count + " cruises found";
	} else {
		message = count + " cruise found";
	}

	//parent.document.getElementById("countSpan").innerHTML = message;
	parent.document.getElementById("cruiseSearchForm").count.value = count;
	parent.enableSearch();
/*  if (count <= 125) {
		
	} else {

		// If all 4 values have been selected and the count is more than 125 turn on the select button
		if (parent.document.cruiseSearchForm.cruiseLine.value != "" && parent.document.cruiseSearchForm.cruiseLine.value != null) {
		
				parent.enableSearch();		
		} else {
			parent.disableSearch();
		}
		
	}
*/
}


// light up the results button
function enableSearch() {
	document.submitImage.style.cursor = "hand";
	document.submitImage.src = activeButtonImage;
}

// dim the results button
function disableSearch() {
	document.submitImage.style.cursor = "default";
	document.submitImage.src = inactiveButtonImage;
}

function setValues(destination, sailDate, duration, cruiseLine) {
	
	
	addOption(document.cruiseSearchForm.destination, destination, 'Please Select', true);
	setAllDesitionOptions();
	
	addOption(document.cruiseSearchForm.sailDate, sailDate, '', true);
	addOption(document.cruiseSearchForm.duration, duration, '', true);
	addOption(document.cruiseSearchForm.cruiseLine, cruiseLine, '', true);
	document.cruiseSearchForm.submit();		
}

function doSearch() 
{
	document.cruiseSearchForm.action = searchResultsPage;
	document.cruiseSearchForm.target = '_self';
	if(checkForm()) {
		ShowWait();
		document.cruiseSearchForm.submit();

	}
}

function checkForm()
{    
	var zipCodeFormat = /\d\d\d\d\d/;
	var validateZip = zipCodeFormat.exec(document.cruiseSearchForm.cs_ResidencyZipCode.value);

	if(document.cruiseSearchForm.cs_ResidencyZipCode.value != "") {
		if(validateZip == null) {
			alert("You have entered an incorrect zip code.  Please try again.");
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function doCompare() {
	if (getCompareCount() > 1) {
		//document.cruise_compare.search_redirect.value = document.location.href;
		var protocolString = document.location.protocol + "//";
		var hostString = document.location.host;
		var pathNameString = document.location.pathname;		
		var queryString = document.location.search;
		var arrQueryString = new Array();
		var intBVSessionIDPos = -1;
		var intBVEngineIDPos = -1;
		var intSPos = -1;
		var intEPos = -1;
		if(queryString.length > 0){
			intBVSessionIDPos = queryString.indexOf("BV_SessionID");
			intBVEngineIDPos = queryString.indexOf("BV_EngineID");
			if(intBVSessionIDPos > -1 || intBVEngineIDPos > -1){
				arrQueryString = queryString.split('&');
				for(i = 0; i < arrQueryString.length; i++){
					intSPos = arrQueryString[i].indexOf("BV_SessionID");
					intEPos = arrQueryString[i].indexOf("BV_EngineID");
					if(intSPos == 0 || intEPos == 0){
						arrQueryString[i] = "";
					}					
				}
				queryString = arrQueryString.join('');	
			}			
		}
		
		//document.cruise_compare.search_redirect.value = protocolString + hostString + pathNameString + queryString;
		document.cruise_compare.submit();
	} else {
		alert(minimumCompareAlert);
	}
}

function getCompareCount() {
	var count = 0;
	if (document.cruise_compare.itinerary_id[0] == null) {
		// there is only one checkbox on the page
		if (document.cruise_compare.itinerary_id.checked) {
			return 1;
		} else {
			return 0;
		}
	} else {
		// there is an array of them, get the count that way
		for (i = 0; i < document.cruise_compare.itinerary_id.length; i++) {
			if (document.cruise_compare.itinerary_id[i].checked) {
				count++;
			}
		}
		return count;
	}
}

function validateCompareCount(checkbox) {
	if (getCompareCount() > 3) {
		alert(maximumCompareAlert);
		checkbox.checked = false;
	}
}


//Search Results Header Functionality
function ChangeSortField(sSortField) {
	document.frmRepost.sort.value = sSortField;
	ChangePage(1);
}
function ChangePage(iPage) {
	document.frmRepost.page.value = iPage;
	document.frmRepost.submit();
}

function ChangeSortOrder(sOrder) {
	document.frmRepost.sortorder.value = sOrder;
	document.frmRepost.submit();
}


