 
// this file will keep the methods implemented in order to display the instant sailings counter
var g_currentZone	= "1";
var parentZone		= "1";
var g_defaultVnd	= "-1";
var MonthsArray		= null;
var g_currentVendor;
var g_currentShip;
var g_currentPort;
var g_currentMonth;
var g_currentLength;

/**
*
* 
*/
function OnStartupZonesLoad(ZoneDropDownID)
{	
	var cbZonesDropDown		= getElement(ZoneDropDownID);
	var zonesArray			= eval("ArrayDestObjects");
	FillCombo2(cbZonesDropDown, zonesArray);
	SetDefaultSelection(cbZonesDropDown, g_currentZone);
}

/*
*
*
*/
function FillCombo2(cb, arrayObjects)
{
	for(var i=0; i<arrayObjects.length; i++)
	{
		var oOption				= new Option(arrayObjects[i].Name, arrayObjects[i].ID)
		cb.options[cb.length]	= oOption;
	}
}

/**
*
* 
*/
function LoadVendorsStartup(vVndID, vShipID, vMonthID, vLengthID, vLabelID, parent)
{
	cbVnd			= getElement(vVndID);
	cbShip			= getElement(vShipID);
	cbMonth			= getElement(vMonthID);
	cbLength		= getElement(vLengthID);
	label			= getElement(vLabelID);
	
	var VndArray	= eval('ArrVndObj');
	
	if(VndArray  == null || (VndArray != null && VndArray.length == 0))
	{
		var oOption							= new Option(allCruiselines, "-1")
		cbVnd.options[cbVnd.length]			= oOption;
		oOption								= new Option(showAll, "-1")
		cbMonth.options[cbMonth.length]		= oOption;
		oOption								= new Option(showAll, "0|0")
		cbLength.options[cbLength.length]	= oOption;
		if(label != null)
		{
			label.innerHTML					= notFound;
		}
		return;
	}
	
	FillCombo2(cbVnd, VndArray);
	if(g_currentVendor != null)
	{
		SetDefaultSelection(cbVnd, g_currentVendor);
	}
	// fill Ships Drop Down
	VndDropDownChanged(vVndID, vShipID, vMonthID, vLengthID, vLabelID);
}

/**
*
* 
*/
function VndDropDownChanged(vVndID, vShipID, vMonthID, vLengthID, vLabelID)
{
	var cbVnd		= getElement(vVndID);
	var cbShip		= getElement(vShipID);
	var cbMonth		= getElement(vMonthID);
	var cbLength	= getElement(vLengthID);
	var label		= getElement(vLabelID);

	var VndSel		= cbVnd.value;
	VndArray		= eval('ArrVndObj');
	
	if(VndArray==null)
	{
		alert('Vendors array is NULL');
		return;
	}
	
	// fill Ships drop Down
	ClearCombo(cbShip);
	AddAllOption(cbShip, allShips);
	
	if(cbVnd.value == -1)
	{
		//load all vendors
		var arrShips = new Array();
		for(var j=0;j<VndArray.length;j++)
		{
			arrShips = arrShips.concat(VndArray[j].ShipArray);
		}
		arrShips.sort(SortAlphabetically);
		FillCombo2(cbShip, arrShips);
		if(g_currentShip != null)
		{
			setDefaultSelection(cbShip, g_currentShip);
		}
	}else
	{
		for(var j=0; j<VndArray.length; j++)
		{
			if(VndArray[j].ID == cbVnd.value)
			{
				var arrShips = VndArray[j].ShipArray;
				FillCombo2(cbShip, arrShips);
				if(g_currentShip != null)
				{
					setDefaultSelection(cbShip, g_currentShip);
				}
				break;
			}
		}
	}

	LoadMonthsStartup(vMonthID, vLengthID, vLabelID, parentZone)
}

/**
*
* 
*/
function LoadMonthsStartup(vMonthID, vLengthID, vLabelID, parent)
{
	cbMonth		= getElement(vMonthID);
	cbLength	= getElement(vLengthID);
	label		= getElement(vLabelID);
	
	for(var idx = 0; idx < VndArray.length; idx++)
	{
		if(VndArray[idx].ID == cbVnd.value)
		{
			MonthsArray = VndArray[idx].Months;
			break;
		}
	}
	
	if(MonthsArray  == null || (MonthsArray != null && MonthsArray.length==0))
	{
		ClearCombo(cbMonth);
		ClearCombo(cbLength);
		
		var oOption							= new Option(showAll, "")
		cbMonth.options[cbMonth.length]		= oOption;
		oOption								= new Option(showAll, "0|0")
		cbLength.options[cbLength.length]	= oOption;
		if(label != null)
		{
			label.innerHTML					= notFound;
		}
		return;
	}
	
	var date	= new Date();
	var month	= date.getMonth() + 1;
	date.setMonth(month);
	month		=  month + 2;

	if (month == '13')
	{
	    month = 1;
        date.setFullYear(eval(parseInt(date.getFullYear()) + 1));
	}
	    
	if (month == '14')
	{
	    month = 2;
        
	}
	
	var strSelDate= month + "/1/" + date.getFullYear();	
	if(g_currentMonth != null && g_currentMonth != "")
	{
		strSelDate = g_currentMonth;	
	}
	
	ClearCombo(cbMonth);
	MonthsArray.sort(SortMonth);
	FillCombo2(cbMonth, MonthsArray);
	SetDefaultSelection(cbMonth, strSelDate);
	
	// fill day drop down 
	var arrDays = MonthsArray[0].days;
	for(var idx=0; idx < MonthsArray.length; idx++)
	{
		if(cbMonth.value == MonthsArray[idx].ID)
		{
			arrDays = MonthsArray[idx].days;
		}
	}

	ClearCombo(cbLength);
	FillLengthCombo(cbLength, arrDays);	
	if(parent == "1" && (g_currentLength == null || g_currentLength == "")) // set default 6|9 for Caribbean
	{
		SetDefaultSelection(cbLength, "0|0");
	}

	if(g_currentLength != null && g_currentLength  != "")
	{
		SetDefaultSelection(cbLength, g_currentLength);
	}
				
	LengthDropDownChanged(vMonthID, vLengthID, vLabelID)
}

/**
*
* 
*/
function MonthDropDownChanged(vMonthID, vLengthID, vLabelID, vParent)
{
	cbMonth		= getElement(vMonthID);
	cbLength	= getElement(vLengthID);
	label		= getElement(vLabelID);
	if(MonthsArray  == null || (MonthsArray != null && MonthsArray.length==0))
	{
		var oOption							= new Option(showAll, "")
		cbMonth.options[cbMonth.length]		= oOption;
		oOption								= new Option(showAll, "0|0")
		cbLength.options[cbLength.length]	= oOption;
		if(label != null)
		{
			label.innerHTML					= notFound;
		}
		return;
	}
	
	var month		= cbMonth.options[cbMonth.selectedIndex];	
	var monthSel	= month.value;
	
	for(var idx=0; idx < MonthsArray.length; idx++)
	{
		if(monthSel == MonthsArray[idx].ID)
		{
			ClearCombo(cbLength);
			FillLengthCombo(cbLength, MonthsArray[idx].days);
			if(vParent == 1 && (g_currentLength == null || g_currentLength == "")) // set default 6|9 for Caribbean
			{
				
			}
			
			if(g_currentLength != null && g_currentLength  != "")
			{
				SetDefaultSelection(cbLength, g_currentLength);
			}
			
			LengthDropDownChanged(vMonthID, vLengthID, vLabelID)
			break;
		}
	}
}

/**
*
* 
*/
function LengthDropDownChanged(vMonthID, vLengthID, vLabelID)
{
	if(MonthsArray == null)
	{
		alert("months array null");
		return;
	}
	
	cbMonth		= getElement(vMonthID);
	cbLength	= getElement(vLengthID);
	label		= getElement(vLabelID);
	
	var month		= cbMonth.options[cbMonth.selectedIndex];	
	var monthSel	= month.value;
	
	var dayLength	= cbLength.options[cbLength.selectedIndex];	
	var dayLengthSel= dayLength.value;
	for(var idx=0; idx < MonthsArray.length; idx++)
	{
		if(monthSel == MonthsArray[idx].ID)
		{
			var DaysArray	= MonthsArray[idx].days;
			for(var dayIdx=0; dayIdx < DaysArray.length; dayIdx++)
			{
				if(dayLengthSel == DaysArray[dayIdx].ID)
				{
					if(DaysArray[dayIdx].total == 0)
					{
						if(label != null){
							label.innerHTML	= "No " + sailings + " " + found;
						}
					}else if(DaysArray[dayIdx].total == 1)
					{
						if(label != null){
							label.innerHTML	= "<b><font color='#CC0000'>" + DaysArray[dayIdx].total + "</font></b>" +  " " + sailing + " " + found;
						}
					}else
					{
						if(label != null){
							label.innerHTML	= "<b><font color='#CC0000'>" + DaysArray[dayIdx].total + "</font></b>" + " " + sailings + " " + found;
						}
					}
					break;
				}
			}
			break;
		}
	}
}

function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

	qs = qs.replace(/\+/g, ' ') //// Turn <plus> back to <space>
	var args = qs.split('&') // parse out name/value pairs separated via &
	
	// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}


function SendStatePortSearch(strFile, target)
{
	var strUrl		= strFile;
	var cbSkin		= getElement("Skin");
	var cbPIN		= getElement("PIN");
	var cbPhone		= getElement("Phone");
	var cbHome		= getElement("Home");
	var cbLID		= getElement("LID");
	
	if (cbSkin == null)
	{
		cbSkin = getElement("AG");
		if (cbSkin == null)
	    {
		    cbSkin = getElement("ClientID");
	    }
	}
	
	
	if(cbSkin != null && cbSkin.value!='' && cbSkin.value!=null)
    {
        var arrURL=strURL.split('?');
        if(arrURL.length==1)
        strURL += "?skin="+escape(cbSkin.value);
        else
        strURL += "&skin="+escape(cbSkin.value);
    }
	
	if(cbPIN != null && cbPIN.value!='' && cbPIN.value!=null)
    {
        var arrURL=strURL.split('?');
        if(arrURL.length==1)
        strURL += "?PIN="+escape(cbPIN.value);
        else
        strURL += "&PIN="+escape(cbPIN.value);
    }
    if(cbPhone != null && cbPhone.value!='' && cbPhone.value!=null)
    {
        var arrURL=strURL.split('?');
        if(arrURL.length==1)
        strURL += "?Phone="+escape(cbPhone.value);
        else
        strURL += "&Phone="+escape(cbPhone.value);
    }
    if(cbHome != null && cbHome.value!='' && cbHome.value!=null)
    {
        var arrURL=strURL.split('?');
        if(arrURL.length==1)
        strURL += "?Home="+escape(cbHome.value);
        else
        strURL += "&Home="+escape(cbHome.value);
    }
    if(cbLID != null && cbLID.value!='' && cbLID.value!=null)
    {
        var arrURL=strURL.split('?');
        if(arrURL.length==1)
        strURL += "?LID="+escape(cbLID.value);
        else
        strURL += "&LID="+escape(cbLID.value);
    }
	strUrl   += "&HP=true&CT=4";
		
	Querystring(null);
	
	if(Querystring_get('vendor', null) != null)
	{
	    var arrURL=strUrl.split('?');
        if(arrURL.length==1)
        strUrl += "?vendor="+ Querystring_get('vendor', null);
        else
        strUrl += "&vendor="+ Querystring_get('vendor', null);
	}
	
	if(cbSkin.value=="132" || Querystring_get('nw', null) == 'true' || target == "_blank")
		window.open(strUrl);
	else
	{
		if (target && target == "_top")
		{
			window.top.location.href = strUrl;
		}
		else
		{
			window.parent.location.href = strUrl;
		}
	}
}

/**
*
* 
*/
function SendState(strFile, target)
{
	var strUrl		= strFile + "?";
	var cbDest		= getElement("ZoneDropDown");
	var cbMonth		= getElement("DateDropDown");
	var cbLen		= getElement("LenDropDown");
	var cbVnd		= getElement("VndDropDown");
	var cbShp		= getElement("ShipsDropDown");
	var cbPort		= getElement("PortDropDown");
	var cbSort		= getElement("SortDropDown");
	var cbSenior	= getElement("Seniors");
	var cbSkin		= getElement("Skin");
	var cbRef		= getElement("Ref");
	var cbPIN		= getElement("PIN");
	var cbSN		= getElement("SN");
	var cbMT		= getElement("MT");
	var cbPhone		= getElement("Phone");
	var cbHome		= getElement("Home");
	var cbLID		= getElement("LID");
	var cbEO		= getElement("EO");
	var cbVendor	= getElement("vendor");
		
	if (cbSkin == null)
	{
		cbSkin = getElement("AG");
		if (cbSkin == null)
	    {
		    cbSkin = getElement("ClientID");
	    }
	}
		
	if(cbSkin != null)
	{
	    if(cbSkin.value!='')
	        strUrl   += "skin=" + escape(cbSkin.value);
	}
	
	var strDest		= "-1";
	var DestName	= "";
	
	if (cbDest != null && cbDest.value != '' && cbDest.value != null)
    {
        strUrl   += "&CD="	+ cbDest.value;
    }
    
	// add destination name 
	if (DestName != null && DestName != "")
		strUrl   += "&CDN="	+ escape(DestName);
		
	if(cbMonth != null)
	{
	    if(cbMonth.value!='')
	    strUrl   += "&DF="	+ escape(cbMonth.value);
	}
	
	if(cbLen   != null)
	{
	    if(cbLen.value!='')
	    strUrl   += "&CL="	+ escape(cbLen.value);
	}
	
	if(cbVnd   != null)
	{
	    if(cbVnd.value!='')
	    strUrl   += "&CV="	+ escape(cbVnd.value);
	}
	
	if(cbPort  != null)
	{
	    if(cbPort.value!='')
	    strUrl   += "&CP="	+ escape(cbPort.value);
	}
	
	if(cbShp   != null)
	{
	    if(cbShp.value!='')
	    strUrl   += "&CSP=" + escape(cbShp.value)
	}
	
	if(cbSort != null && cbSort.value != null && cbSort.value != '-1' && cbSort.value != '')
	{
		strUrl   += "&CST=" + escape(cbSort.value); //escape(cbSort.value);
	}
		
	if(cbSenior!=null && cbSenior.type != "select-one")
	{
	    strUrl   += "&SN="	+ escape(cbSenior.checked);
	}
	
    if(cbMT != null && cbMT.value != '')
    {
        if(cbMT.value!='')
        strUrl   += "&MT=" +escape(cbMT.value);
    }
    
    if(cbPhone != null)
    {
        if(cbPhone.value!='')
        strUrl   += "&Phone=" + escape(cbPhone.value);
    }
    
    if(cbHome != null)
    {
        if(cbHome.value!='')
        {
            strUrl   += "&Home=" + escape(cbHome.value);
        }
    }
    
	if(cbEO != null && cbEO.type != "select-one") 
	{
	    strUrl   += "&EO="	+ escape(cbEO.checked);
	}	 
	
	if(cbRef  != null)
	{
	    if(cbRef.value!='')
	    strUrl   += "&Ref="  + escape(cbRef.value);
	}
	
	if(cbPIN != null)
	{
	    if(cbPIN.value!='')
	    strUrl   += "&PIN=" + escape(cbPIN.value);
	}
	
	if(cbLID  != null)
    {
        if(cbLID.value!='')
        strUrl   += "&LID="  + escape(cbLID.value);
    }
    
    if(cbVendor != null)
    {
        if(cbVendor.value!='')
        strUrl   += "&vendor="  + escape(cbVendor.value);
    }
	
	Querystring(null);
	if(cbSkin.value == "132" || Querystring_get('nw', null) == 'true' || target == "_blank")
    {
        window.open(strUrl);
    }
	else
	{
		if (target && target == "_top")
		{
			window.top.location.href = strUrl;
		}
		else
		{
			window.parent.location.href = strUrl;
		}
	}
}


function SortMonth(month1, month2)
{
	var arrMonth1	= month1.ID.split("/");
	var arrMonth2	= month2.ID.split("/");
	
	var varMonth1	= new Date(parseInt(arrMonth1[2]) - 1,parseInt(arrMonth1[0]) - 1,parseInt(arrMonth1[1]) - 1);
	var varMonth2	= new Date(parseInt(arrMonth2[2]) - 1,parseInt(arrMonth2[0]) - 1,parseInt(arrMonth2[1]) - 1);

	if(varMonth1 < varMonth2)
		return -1;
	
	if(varMonth1 == varMonth2)
		return 0;
	
	if(varMonth1 > varMonth2)
		return 1;
}