// JavaScript Document
var destid;    // Id of the selected Destination
var priceid;   // Id of the selected Price
var cabintypecount;  // Number of the selected cabin types
var cruiselinecount; // Number of the selected cruise lines
var departfromcount; // Number of the selected depart from ports
var departfromstring;// The list of the selected depart from ports 
var crcount;
var cruise_date_str; // The list of months returned by AJAX
var resQ;    // Is equal to 0 when an AJAX request is in process and 1 otherwise
var monthsResQ;
var querystr;
var cruise_count = Array();
var destempty;

Global_Images_Directory = "http://d127u7nzglpmon.cloudfront.net";

function init()     //Giving the initial values to variables and initilizing functions
{


    destid = "";
    priceid = "priceAny";
    cabintypecount = 0;
    cruiselinecount = 0;
    departfromcount = 0;
	departfromstring = '';
    crcount = 0;
    resQ = 1;
    monthsResQ = 1;
    destempty = 1;

    document.getElementById( "pleaseselectdest" ).style.display = "block";  // Show the "Your destination please" arrow
    document.getElementById( "priceAny" ).className = "listitem_3";   // Select "Any Price"
    document.getElementById( "cabintypeAny" ).className = "listitem_3";  // Select "Any Cabin Type"
    document.getElementById( "cruiselineAny" ).className = "listitem_3"; // Select "Any Cruise Line"
    document.getElementById( "departfromAny" ).className = "listitem_3"; // Select "Any Price"
    document.getElementById( "departfromAny" ).style.display = "none";  // Select "Any Port"
    // document.getElementById('botarrow').style.display="none";
    // document.getElementById('toparrow').style.display="none";

    // render(0,1);
    months();
};

function readParametersFromCookie()
{
    All_Cookies_Array = document.cookie.split( ';' );
    Params_In_Cookie = new Array();
 
    // Build Params_In_Cookie array which would contain the parameters in the cookie "Last_Search_Params". 
    // For example, it would be array( "p0=destination_6", "p1=depart_from_000GAL", "p2=cruise_line_CCL" )

    for( Index in All_Cookies_Array )
        if( All_Cookies_Array[ Index ].indexOf( 'Last_Search_Params=' ) > -1 )
        {
            Cookie_Content = unescape( decodeURI( All_Cookies_Array[ Index ] ) );

            // If the "Last_Search_Params" cookie has been put in place by one of the long tail pages 
            // of page_type Region_Port or Subregion_Port, then do nothing.
            if( Cookie_Content.indexOf( 'region' ) > -1 ) { render( 0, destempty ); return; }

            Cookie_Content = Cookie_Content.replace( 'Last_Search_Params=', '' ); 
            Params_In_Cookie = Cookie_Content.split( '&' );
        }

    if ( Params_In_Cookie !== null )
        for ( iterOfCookies in Params_In_Cookie )
            {
                parValueCookie = Params_In_Cookie[ iterOfCookies ].split( '=' );

                if ( document.getElementById( parValueCookie[ 1 ] ) !== null )
                    {
                        if ( parValueCookie[ 1 ].indexOf( "Any" ) == -1 )
                            {
                                if ( parValueCookie[ 1 ].indexOf( "month_" ) == 0 )
                                    {
                                        document.getElementById( parValueCookie[ 1 ] ).className = "m_listitem_1";
                                        m_itemclick( document.getElementById( parValueCookie[ 1 ] ), true );
                                    }

                                else
                                    {
                                        document.getElementById( parValueCookie[ 1 ] ).className = "listitem_1";
                                        itemclick( document.getElementById( parValueCookie[ 1 ] ), true );
                                    }

                            }
                    }
				else
					{
						if (  parValueCookie[ 1 ].search( "depart_from_" ) == 0 )
							{
								departfromcount++;
								departfromstring += "@" +  parValueCookie[ 1 ];
								document.getElementById( "departfromAny" ).className = "listitem_0";
								document.getElementById( "pleaseselectdest" ).style.display = "none";
                           		document.getElementById( "departfromAny" ).style.display = "block";
								document.getElementById( "contain_depart_from" ).style.display = "block";

							}
					}

            }

    render( 0, destempty );

};

function itemclick( obj, norender )
{
    if ( resQ )
        {
            var destempty = 0;
            crcount0 = 0;

            if ( obj.className == "listitem_3" ) {obj.className = "listitem_1";}

            else if ( obj.className == "listitem_1" || obj.className == "listitem_0" ) {obj.className = "listitem_3";}

            destQ = 0;

            if ( obj.id.search( "destination" ) == 0 )
                {
                    crcount = 0;

                    document.getElementById( "departfromAny" ).className = "listitem_3";
                    departfromcount = 0;
					departfromstring = '';

                    if ( destid && document.getElementById( destid ).className == "listitem_3" && destid != obj.id )
                        document.getElementById( destid ).className = "listitem_0";


                   	document.getElementById( "contain_depart_from" ).style.display = "none";
					
					if ( obj.className == "listitem_1" )
                        {
                            document.getElementById( "pleaseselectdest" ).style.display = "block";
                            document.getElementById( "departfromAny" ).style.display = "none";
							document.getElementById( "contain_depart_from" ).style.display = "none";
                            destempty = 1;
                        }

                    else
                        {
							toggleDepartFromVisability();
                        }

                    destid = obj.id;
                    destQ = 1;


                }

            if ( obj.id.search( "price" ) == 0 )
                {
                    if ( priceid && document.getElementById( priceid ).className == "listitem_3" && priceid != obj.id ) document.getElementById( priceid ).className = "listitem_0";

                if ( priceid == obj.id ) { document.getElementById( "priceAny" ).className = "listitem_3"; priceid = "priceAny";}

                    else
                        priceid = obj.id;
                }

            if ( obj.id.search( "cabintype_" ) == 0 )
                {
                    if ( obj.className == "listitem_1" )
                        {
                            cabintypecount--;

                            if ( cabintypecount === 0 ) document.getElementById( "cabintypeAny" ).className = "listitem_3";
                        }

                    else
                        {
                            cabintypecount++;
                            document.getElementById( "cabintypeAny" ).className = "listitem_0";
                        }
                }


            if ( obj.id.search( "cabintypeAny" ) == 0 )
                cabintypecount = 0;


            if ( obj.id.search( "cruise_line_" ) == 0 )
                {
                    if ( obj.className == "listitem_1" )
                        {
                            cruiselinecount--;

                            if ( cruiselinecount == 0 ) document.getElementById( "cruiselineAny" ).className = "listitem_3";
                        }

                    else
                        {
                            cruiselinecount++;
                            document.getElementById( "cruiselineAny" ).className = "listitem_0";
                        }
                }

            if ( obj.id.search( "cruiselineAny" ) == 0 )
                cruiselinecount = 0;


            if ( obj.id.search( "depart_from_" ) == 0 )
                {
                    if ( obj.className == "listitem_1" )
                        {
                            departfromcount--;
							departfromstring = departfromstring.replace( "@" + obj.id , "" );
                            if ( departfromcount == 0 ) document.getElementById( "departfromAny" ).className = "listitem_3";
                        }

                    else
                        {
                            departfromcount++;
							departfromstring += "@" + obj.id;
                           document.getElementById( "departfromAny" ).className = "listitem_0";
                        }
                }

            if ( obj.id.search( "departfromAny" ) == 0 )
				{
                	departfromcount = 0;
					departfromstring = '';
				}

            if ( !norender ) render( destQ, destempty );
        }
};

function toggleDepartFromVisability()
{
	if(document.getElementById( "countdepartfromAny" ).innerHTML == document.getElementById( "count" ).innerHTML)
		{
		    document.getElementById( "pleaseselectdest" ).style.display = "none";
		    document.getElementById( "departfromAny" ).style.display = "block";
		    document.getElementById( "contain_depart_from" ).style.display = "block";	
		}
	else
		{
			setTimeout("toggleDepartFromVisability()",200);
		}
};

First_Time_Render = 1;

function render( destQ, destempty )
{

    querystr = "";
    var querynum = 0;


    //var depart_count=0;

    for ( i = 0;i < document.getElementsByTagName( '*' ).length;i++ )
        {

  /*           if(document.getElementsByTagName('*')[i].id.search("depart_from")==0 && destQ)
             {

              
               if(destempty)
                document.getElementsByTagName('*')[i].style.display="none";


               else
               {
                if(cruise_count[destid][document.getElementsByTagName('*')[i].id][0])
               crcount+=cruise_count[destid][document.getElementsByTagName('*')[i].id][0];
            //    document.getElementById("count_"+document.getElementsByTagName('*')[i].id).innerHTML=cruise_count[destid][document.getElementsByTagName('*')[i].id];
               //depart_count++;
               }
            //    else
            //     document.getElementsByTagName('*')[i].style.display="none";
             }
         */

            if ( document.getElementById( "cabintypeAny" ).className == "listitem_3" && document.getElementsByTagName( '*' ) [ i ].id.search( "cabintype_" ) == 0 && document.getElementsByTagName( '*' ) [ i ].className == "listitem_3" )
                {
                    document.getElementsByTagName( '*' ) [ i ].className = "listitem_0";
                }


            if ( document.getElementById( "cruiselineAny" ).className == "listitem_3" && document.getElementsByTagName( '*' ) [ i ].id.search( "cruise_line_" ) == 0 && document.getElementsByTagName( '*' ) [ i ].className == "listitem_3" )
                {
                    document.getElementsByTagName( '*' ) [ i ].className = "listitem_0";
                }

            if ( document.getElementById( "departfromAny" ).className == "listitem_3" && document.getElementsByTagName( '*' ) [ i ].id.search( "depart_from_" ) == 0 && document.getElementsByTagName( '*' ) [ i ].className == "listitem_3" )
                {
                    document.getElementsByTagName( '*' ) [ i ].className = "listitem_0";
                }

            if ( document.getElementsByTagName( '*' ) [ i ].className == "listitem_3" )
                {
                    querystr += "&p" + querynum + "=" + document.getElementsByTagName( '*' ) [ i ].id;
                    querynum++;

  /*                  if ( document.getElementsByTagName( '*' ) [ i ].id.search( "depart_from" ) == 0 )
                        crcount0 += cruise_count[ destid ][ document.getElementsByTagName( '*' ) [ i ].id ][ 0 ];
*/
                }

            if ( document.getElementsByTagName( '*' ) [ i ].className == "m_listitem_3" )
                {
                    querystr += "&p" + querynum + "=" + document.getElementsByTagName( '*' ) [ i ].id;
                    querynum++;
                }





        }

    /* if(document.getElementById('dep_td').scrollHeight>200) {
     if(document.getElementById('dep_td').scrollTop==0)  document.getElementById('botarrow').style.display="block";
     }
     else {  document.getElementById('botarrow').style.display="none";
       document.getElementById('toparrow').style.display="none";
       }
     */
	sendDepartFromListRequest( 'http://www.cayole.com/lib/AJAX/Homepage_Departure_Ports.php?' + querystr.substr( 1 ) );


    // If First_Time_Render is 1 and there are no Last_Search_Params cookies set, then show the initial message
    // in the Best Cruise Deals window. Otherwise, get the best deals via AJAX and show those deals.

    if( First_Time_Render == 1 && document.cookie.indexOf( 'Last_Search_Params=' ) == -1 ) 
    { 
        First_Time_Render = 0;
        Display_Initial_Message_In_Best_Cruise_Deals_Window();
    }
    else Get_Best_Cruise_Deals( 'http://www.cayole.com/lib/AJAX/Homepage_Get_Best_Deals.php?' + querystr.substr( 1 ) );



    monthsResQ = 0;
	

    sendRequest0( 'http://www.cayole.com/lib/AJAX/Homepage_Departure_Months.php?' + querystr.substr( 1 ) + "&dps=" + departfromstring.substr( 1 ) , 'cruise_date_str', getRequest0 );

    months();

};


function sendCruiseCountRequest( str )
{
    if ( resQ )
        {
            resQ = 0;

            if ( querystr.search( "cruise_line" ) < 0 && querystr.search( "price_" ) < 0 && querystr.search( "month" ) < 0 && querystr.search( "duration" ) < 0 && querystr.search( "cabintype_" ) < 0 && querystr.search( "depart_from" ) < 0 && querystr.search( "destination" ) < 0 )
                {

                                document.getElementById( "count" ).innerHTML = document.getElementById( "counthid" ).innerHTML;

                    resQ = 1;
                }

            else
                sendRequest( str, 'count', getRequest );
        }

    else setTimeout( "sendCruiseCountRequest('" + str + "')", 200 );
};


function sendDepartFromListRequest( str )
{
    if ( resQ )
        {
            resQ = 0;
            sendRequest( str, 'depart_from_str', getRequest );
            sendDepartFromListRequestInner();
        }

    else setTimeout( "sendDepartFromListRequest('" + str + "')", 200 );
};

function sendDepartFromListRequestInner()
{
    if ( resQ )
        {
            //       alert(document.getElementById("depart_from_str").innerHTML+" - "+document.getElementById("depart_from_str").innerHTML.length);

            contain_depart_from_str = "";

            if ( document.getElementById( "depart_from_str" ).innerHTML.length > 0 )
                {
                    ajax_depart_from_array = document.getElementById( "depart_from_str" ).innerHTML.split( "@@" );

                    cruise_count[ destid ] = Array();
					
					sum_depart_from=0;
					crcount_depart_from=0;

                    for ( index_of_depart_from_array in ajax_depart_from_array )
                        {

                            temp_arr = new Array();
                            temp_arr = ajax_depart_from_array[ index_of_depart_from_array ].split( "@" );
                            cruise_count[ destid ][ 'depart_from_' + temp_arr[ 0 ] ] = new Array( temp_arr[ 1 ], temp_arr[ 2 ] );
							
							sum_depart_from += parseInt( cruise_count[ destid ][ 'depart_from_' + temp_arr[ 0 ] ][ 0 ]);
							
							if(departfromstring.indexOf("depart_from_" + temp_arr[ 0 ])==-1)
							    {
							        depart_from_className="listitem_0";
								}
							else
							    {
							        depart_from_className="listitem_3";
									crcount_depart_from += parseInt( cruise_count[ destid ][ 'depart_from_' + temp_arr[ 0 ] ][ 0 ]);
								}
								
                            contain_depart_from_str += '<div id="depart_from_' + temp_arr[ 0 ] + '" class="'+depart_from_className+'" onclick="itemclick(this);" onmouseover="itemmouseover(this);" onmouseout="itemmouseout(this);" style="width:180px; padding-right:15px;">' + cruise_count[ destid ][ 'depart_from_' + temp_arr[ 0 ] ][ 1 ] + ' (' + cruise_count[ destid ][ 'depart_from_' + temp_arr[ 0 ] ][ 0 ] + ')</div>';

                        }
		            document.getElementById( "countdepartfromAny" ).innerHTML = sum_depart_from;
					document.getElementById( "contain_depart_from" ).innerHTML = contain_depart_from_str;
					
					if( crcount_depart_from > 0 )
						document.getElementById( "count" ).innerHTML = crcount_depart_from;
					else
						document.getElementById( "count" ).innerHTML = sum_depart_from;
                }
				
			else
				{
		            document.getElementById( "countdepartfromAny" ).innerHTML = 0;
					document.getElementById( "contain_depart_from" ).innerHTML = '';
					sendCruiseCountRequest( 'http://www.cayole.com/lib/AJAX/Homepage_Cruise_Count.php?' + querystr.substr( 1 ) + "&dps=" + departfromstring.substr( 1 ) );

				}
        }

    else setTimeout( "sendDepartFromListRequestInner()", 200 );

};


function months()
{

    if ( monthsResQ )
        {
            cruise_date_str = document.getElementById( "cruise_date_str" ).innerHTML;


            for ( i = 0;i < document.getElementsByTagName( '*' ).length;i++ )
                {
                    if ( document.getElementsByTagName( '*' ) [ i ].id.search( "month" ) == 0 )
                        {
                            if ( cruise_date_str.indexOf( document.getElementsByTagName( '*' ) [ i ].id ) == -1 )
                                document.getElementsByTagName( '*' ) [ i ].className = "m_listitem_4";
                            else if ( document.getElementsByTagName( '*' ) [ i ].className == "m_listitem_4" )
                                document.getElementsByTagName( '*' ) [ i ].className = "m_listitem_0";
                        }

                }
        }

    else
        {
            setTimeout( "months()", 200 );
        }

};


function itemmouseover( obj )
{
    if ( obj.className != "listitem_3" )
        {
            obj.className = "listitem_1";
        }
};

function itemmouseout( obj )
{
    if ( obj.className != "listitem_3" )
        {
            obj.className = "listitem_2";
            setTimeout( 'if(document.getElementById("' + obj.id + '").className=="listitem_2") document.getElementById("' + obj.id + '").className="listitem_0"', 300 );
        }
};

function m_itemclick( obj, norender )
{
    if ( resQ )
        {

            if ( obj.className == "m_listitem_3" ) obj.className = "m_listitem_1";
            else if ( obj.className == "m_listitem_1" ) obj.className = "m_listitem_3";

            var destempty = 0;

            crcount0 = 0;

            destQ = 0;


            if ( !norender ) render( destQ, destempty );

        }
};

function m_itemmouseover( obj )
{
    if ( obj.className != "m_listitem_3" && obj.className != "m_listitem_4" )
        {
            obj.className = "m_listitem_1";
        }
};

function m_itemmouseout( obj )
{
    if ( obj.className == "m_listitem_1" )
        {
            obj.className = "m_listitem_2";
            setTimeout( 'if(document.getElementById("' + obj.id + '").className=="m_listitem_2") document.getElementById("' + obj.id + '").className="m_listitem_0"', 300 );
        }
};

function viewresults()
{
    var querystr = "";
    var querynum = 0;

    for ( i = 0;i < document.getElementsByTagName( '*' ).length;i++ )
        {
            if ( document.getElementsByTagName( '*' ) [ i ].className == "listitem_3" )
                {
                    querystr += "&p" + querynum + "=" + document.getElementsByTagName( '*' ) [ i ].id;
                    querynum++;
                }

            if ( document.getElementsByTagName( '*' ) [ i ].className == "m_listitem_3" )
                {
                    querystr += "&p" + querynum + "=" + document.getElementsByTagName( '*' ) [ i ].id;
                    querynum++;
                }


        }


    if ( document.getElementById( "count" ).innerHTML == "0" )
        {
            alert( 'No cruises were found based on your search options.\n Please broaden your search criteria.' );
        }

    else
        top.location.href = "http://www.cayole.com/Sailings.php?" + querystr.substr( 1 );
};


function startover()
{
    for ( i = 0;i < document.getElementsByTagName( '*' ).length;i++ )
        {

            if ( document.getElementsByTagName( '*' ) [ i ].className == "listitem_3" )
                document.getElementsByTagName( '*' ) [ i ].className = "listitem_0";

            if ( document.getElementsByTagName( '*' ) [ i ].className == "m_listitem_3" )
                document.getElementsByTagName( '*' ) [ i ].className = "m_listitem_0";

            if ( document.getElementsByTagName( '*' ) [ i ].id.search( "depart_from" ) == 0 )
                document.getElementsByTagName( '*' ) [ i ].style.display = "none";
        }

    document.getElementById( "count" ).innerHTML = document.getElementById( "counthid" ).innerHTML;


    init();
    Display_Initial_Message_In_Best_Cruise_Deals_Window();

};


var httpRequest = createHttpRequest();
var resultId = '';
function createHttpRequest()
{
    var httpRequest;
    var browser = navigator.appName;

    if ( browser == "Microsoft Internet Explorer" )
        {
            httpRequest = new ActiveXObject( "Microsoft.XMLHTTP" );
        }

    else
        {
            httpRequest = new XMLHttpRequest();
        }

    return httpRequest;
};

function sendRequest( file, _resultId, getRequestProc )
{
    resultId = _resultId;

    var txt = '' /*s_id.value*/;

    httpRequest.open( 'post', file );
    httpRequest.onreadystatechange = getRequestProc;

    httpRequest.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded;charset=utf8" );

    httpRequest.send( txt );
};

function getRequest()
{
    if ( httpRequest.readyState == 4 )
        {
            document.getElementById( resultId ).innerHTML = httpRequest.responseText;
            resQ = 1;
        }
};


var httpRequest0 = createHttpRequest0();
var resultId0 = '';
function createHttpRequest0()
{
    var httpRequest0;
    var browser = navigator.appName;

    if ( browser == "Microsoft Internet Explorer" )
        {
            httpRequest0 = new ActiveXObject( "Microsoft.XMLHTTP" );
        }

    else
        {
            httpRequest0 = new XMLHttpRequest();
        }

    return httpRequest0;
};

function sendRequest0( file, _resultId, getRequestProc )
{
    resultId0 = _resultId;


    var txt = '' /*s_id.value*/;

    httpRequest0.open( 'post', file );
    httpRequest0.onreadystatechange = getRequestProc;

    httpRequest0.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded;charset=utf8" );

    httpRequest0.send( txt );
};

function getRequest0()
{
    if ( httpRequest0.readyState == 4 )
        {
            document.getElementById( resultId0 ).innerHTML = httpRequest0.responseText;
            monthsResQ = 1;
        }
};


// This function sets/overrides a cookie.

function Set_Cookie( Cookie_Name, Cookie_Value, Days_Till_Expiry )
{
    var Expiry_Date = new Date();
    Expiry_Date.setDate( Expiry_Date.getDate() + Days_Till_Expiry );
    document.cookie = Cookie_Name+ "=" + escape( Cookie_Value ) + ";expires=" + Expiry_Date.toUTCString() + "; path=/";
};


// This function is called when the user clicks the "Submit" button for the email.
// The function checks if everything is ok and sends the email address with appropriate search parameters to DB.

function Submit_Email()
{
    // If the user has clicked the "Submit" button on the email without entering anything
    // in the email address, direct him/her to the sailings page i.e. act as if "View Cruise Results" has been pressed.

    if( document.getElementById( "email1" ).value == "" ) {
        viewresults();
        return;
    }

    // Check to see if the email is valid of not i.e. if it contains "@" and then a "." after the "@".

    if ( document.getElementById( "email1" ).value.indexOf( "@" ) > 0 && 
         document.getElementById( "email1" ).value.indexOf( ".", document.getElementById( "email1" ).value.indexOf( "@" ) ) > 0 )
    {
        Set_Cookie( "Asked_For_Email", "Gave_Email", 3650 ); // Set the cookie for 10 years

        var Email_Params = "";
        var Param_Count = 0;

        for( i = 0; i < document.getElementsByTagName( '*' ).length; i++ )
        {
            if ( document.getElementsByTagName( '*' )[ i ].className == "listitem_3" || ( document.getElementsByTagName( '*' )[ i ].className == "m_listitem_3" ) )
            {
                Email_Params += "p" + Param_Count + "=" + document.getElementsByTagName( '*' ) [ i ].id + "&";
                Param_Count++;
            }
        }

        // If the last character of Email_Params is "&" then remove it.

        if( ( Email_Params != "" ) && ( Email_Params.charAt( Email_Params.length - 1 ) == '&' ) )
            Email_Params = Email_Params.substring( 0, Email_Params.length - 1 );


        sendRequest( 'http://www.cayole.com/lib/AJAX/General_Add_Email.php?' + Email_Params + "&Email=" + document.getElementById( "email1" ).value + "&Webpage=Homepage", 'botrow', getRequest );
    }

    else alert( "Please enter a valid email address." );
};

// This function sends an AJAX request to get the best cruise deals.
// The reply of the AJAX request is processed by the Process_Best_Cruise_Deals_AJAX_Request() function.

function Get_Best_Cruise_Deals( AJAX_URL )
{
    if( window.XMLHttpRequest ) xmlhttp=new XMLHttpRequest();
    else xmlhttp=new ActiveXObject( "Microsoft.XMLHTTP" );
 
    xmlhttp.onreadystatechange=function()
    {
        if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) Process_Best_Cruise_Deals_AJAX_Request( xmlhttp.responseText );
    }
 
    xmlhttp.open( "POST", AJAX_URL, true );
    xmlhttp.send();
};


// This function processes the reply received from AJAX request to get the best cruise deals.
// The function updates the "Best Cruise Deals" window as appropriate.

function Process_Best_Cruise_Deals_AJAX_Request( AJAX_Reply )
{
    if( AJAX_Reply.length < 10 ) { Display_Best_Deals_Not_Found_Message(); return; } // This will be called if AJAX_Reply="None"

    // Build Parameters to be passed to Categories.php when the user clicks one of the Best Cruise Deals.
    // The parameters are needed so that cookie with selection criteria can be set so that we can ask user's email.
    // This section should be removed once we move the cookie setting from Sailings.php & Categories.php to index.php.

    var URL_Params = "";
    var Param_Count = 0;

    for ( i = 0;i < document.getElementsByTagName( '*' ).length;i++ )
    {
        if ( document.getElementsByTagName( '*' ) [ i ].className == "listitem_3" )
        {
            URL_Params += "&p" + Param_Count + "=" + document.getElementsByTagName( '*' ) [ i ].id;
            Param_Count++;
        }

        if ( document.getElementsByTagName( '*' ) [ i ].className == "m_listitem_3" )
        {
            URL_Params += "&p" + Param_Count + "=" + document.getElementsByTagName( '*' ) [ i ].id;
            Param_Count++;
        }
    }
    URL_Params = URL_Params.substr( 1 ); // Delete the last "&"

    // End of the part to build URL_Params.
    
    Best_Deals = AJAX_Reply.split( "|" );

    T  = '<table cellspacing="0" cellpadding="0" width="236" style="border:#FFFFFF solid 2px; background-color:#F6D9BB; margin-right: 10px;         ';
    T += '    margin-top: 10px; text-align:center; vertical-align: top; color:#000000; font-size:14px; font-family: Verdana; height: 537px;">       ';         
    T += '  <tr>                                                                                                                                    ';
    T += '    <td height="20" style="font-size:18px; color:#7B3501; font-weight:bold; padding-top: 15px; padding-bottom: 6px; height: 20px; ">      ';
    T += '      Best Cruise Deals                                                                                                                   ';
    T += '    </td>                                                                                                                                 ';
    T += '  </tr>                                                                                                                                   ';
    T += '  <tr><td height="10" style="text-align: center; padding-bottom: 14px; height: 10px; "> That Match Your Criteria </td></tr>               ';
    
    for( Index in Best_Deals )
    {
        Params = Best_Deals[ Index ].split( "#" );
        Nights = Params[ 0 ];
        Region = Params[ 1 ];
        Departure_Date = Params[ 2 ];
        Departure_Port = Params[ 3 ];
        Arrival_Port = Params[ 4 ];
        Ship = Params[ 5 ];
        Cabin = Params[ 6 ];
        Min_Price = Params[ 7 ];    
        Sailing_ID = Params[ 8 ];

        if( Best_Deals.length == 1 ) Height_Text = "height: 410px;";
                                else Height_Text = "";
   
    T += '  <tr height="20">                                                                                                                        ';
    T += '    <td style="font-size: 18px; text-align: center; padding-top: 5px; padding-bottom: 5px; border-top:#FFFFFF solid 3px; height: 20px; "> ';
    T += '      <u>' + Nights + ' Nights Cruise</u>                                                                                                 ';
    T += '    </td>                                                                                                                                 ';
    T += '  </tr>                                                                                                                                   ';
    T += '  <tr>                                                                                                                                    ';
    T += '    <td style="width: 100%; padding-top: 5px; padding-bottom: 10px; padding-left: 5px; padding-right: 10px; vertical-align: top;          ';
    T +=                                                                                                                    Height_Text + '">       ';
    T += '      <table border="0" cellpadding="0" cellspacing="0" align="center">                                                                   ';
    T += '        <tr><td style="text-align: right;">Region:&nbsp;</td><td style="text-align: left;"> ' + Region + ' </td></tr>                     ';
    T += '        <tr><td style="text-align: right;">Date:&nbsp;</td><td style="text-align: left;"> ' + Departure_Date + ' </td></tr>               ';
    T += '        <tr><td style="text-align: right;">Depart:&nbsp;</td><td style="text-align: left;"> ' + Departure_Port + ' </td></tr>             ';
    T += '        <tr><td style="text-align: right;">Arrive:&nbsp;</td><td style="text-align: left;"> ' + Arrival_Port + ' </td></tr>               ';
    T += '        <tr><td style="text-align: right; vertical-align: top;">Ship:&nbsp;</td><td style="text-align: left;"> ' + Ship + ' </td></tr>    ';
    T += '        <tr><td style="text-align: right;">Cabin:&nbsp;</td><td style="text-align: left;"> ' + Cabin + ' </td></tr>                       ';
    T += '        <tr>                                                                                                                              ';
    T += '          <td colspan="2" style="font-size: 16px; font-weight: bold; height: 30px; color: #0033CC;">                                      ';
    T += '            From $' + Min_Price + '                                                                                                       ';
    T += '          </td>                                                                                                                           ';
    T += '        </tr>                                                                                                                             ';
    T += '        <tr>                                                                                                                              ';
    T += '          <td colspan = "2" style="text-align: center; height: 40px;">                                                                    ';
    T += '            <input type="image" src="' + Global_Images_Directory + '/btn2_normal.gif"                                                     ';
    T += '              onmouseover="this.src=\'' + Global_Images_Directory + '/btn2_mouseOver.gif\';"                                              ';
    T += '              onmouseout="this.src=\'' + Global_Images_Directory + '/btn2_normal.gif\';"                                                  ';
    T += '              onClick="top.location.href=\'http://www.cayole.com/Categories.php?Sailing_ID=' + Sailing_ID + '&Deals=BestDeals&' + URL_Params +  '\'" />       ';
    T += '          </td>                                                                                                                           ';
    T += '        </tr>                                                                                                                             ';
    T += '      </table>                                                                                                                            ';
    T += '    </td>                                                                                                                                 ';
    T += '  </tr>                                                                                                                                   ';

    }

    T += '</table>                                                                                                                                  ';

    document.getElementById( 'Best_Cruise_Deals_Content' ).innerHTML = T;    
};


// This function displays a message in the Best Cruise Deals window. The
// input variable - Message - can be an HTML text.

function Display_Message_In_Best_Cruise_Deals_Window( Message )
{
    T  = '<table cellspacing="0" cellpadding="0" width="236" style="border:#FFFFFF solid 2px; background-color:#F6D9BB; margin-right: 10px;         ';
    T += '    margin-top: 10px; text-align:center; vertical-align: top; color:#000000; font-size:14px; font-family: Verdana; height: 537px;">       ';         
    T += '  <tr>                                                                                                                                    ';
    T += '    <td height="20" style="font-size:18px; color:#7B3501; font-weight:bold; padding-top: 15px; padding-bottom: 6px; height: 20px; ">      ';
    T += '      Best Cruise Deals                                                                                                                   ';
    T += '    </td>                                                                                                                                 ';
    T += '  </tr>                                                                                                                                   ';
    T += '  <tr><td height="10" style="text-align: center; padding-bottom: 14px; height: 10px; "> That Match Your Criteria </td></tr>               ';
    T += '  <tr>                                                                                                                                    ';
    T += '    <td style="width: 100%; padding: 10px; border-top:#FFFFFF solid 3px; vertical-align: top; text-align: left; height: 437px;">          ';
    T +=        Message; 
    T += '    </td>                                                                                                                                 ';
    T += '  </tr>                                                                                                                                   ';
    T += '</table>                                                                                                                                  ';

    document.getElementById( 'Best_Cruise_Deals_Content' ).innerHTML = T;    
};


// This function displays the initial message in the Best_Cruise_Deals window. 

function Display_Initial_Message_In_Best_Cruise_Deals_Window()
{
    Message = 'Select (click on) your cruise options on the left. Here you will <u>instantaneously</u> see the best cruise deals that match your cruise criteria.';
    Display_Message_In_Best_Cruise_Deals_Window( Message );
};


// This function displays the "There are no cruises matching your search criteria" 
// message in the Best_Cruise_Deals window. 

function Display_Best_Deals_Not_Found_Message()
{
    Message = 'We could not find best cruise deals matching your search criteria. Please broaden your search criteria.';
    Display_Message_In_Best_Cruise_Deals_Window( Message );
};


function leftarrow()
{
    document.getElementById( 'tablemonth1' ).style.display = 'block';
    document.getElementById( 'tablemonth3' ).style.display = 'none';
    document.getElementById( 'ArrowLeft' ).style.display = 'none';
    document.getElementById( 'ArrowRight' ).style.display = 'block';
};

function rightarrow()
{
    document.getElementById( 'tablemonth1' ).style.display = 'none';
    document.getElementById( 'tablemonth3' ).style.display = 'block';
    document.getElementById( 'ArrowLeft' ).style.display = 'block';
    document.getElementById( 'ArrowRight' ).style.display = 'none';
};



