/*******************************************************************************
    FILE: quoterequest.js

    Various Javascript routines required for the Quote Request pages

    Note: Some of the javascript for these pages will be on the page itself
          as it needs to be built by the server (PHP scripts)
*******************************************************************************/

// ===================================================================
//  FUNCTION: ShowForm(id)
//
//  Finds element id and sets the style to "display: block"
// -------------------------------------------------------------------
function showForm(id) {
	var iSection = document.getElementById(id);
	if (iSection == null) return;

        // Note - Firefox seems to have issues with setting
        // table rows as "display: block", but everybody seems
        // fine with "display:" as a "do show it" setting.
	if (iSection.style.display != "")
    {
    	iSection.style.display = "";
    }
}

// ===================================================================
//  FUNCTION: HideForm(id)
//
//  Finds element id and sets the style to "display: none"
// -------------------------------------------------------------------
function hideForm(id) {
	var iSection = document.getElementById(id);
	iSection.style.display = "none";
}

// ---------------------------------------------------------------------------
//  JAVASCRIPT FUNCTION: addToList()
//      ARGS: fgt, sz, val
// ---------------------------------------------------------------------------
function addToList(fgt, sz, val)
{
    fgt.options[fgt.length] = new Option(sz, val);
}

// ---------------------------------------------------------------------------
//  JAVASCRIPT FUNCTION: checkBase()
//      ARGS: x
// ---------------------------------------------------------------------------
function checkBase(x)
{
   if (x == "yes")
    {
        document.f.fWdwTixBase.checked = true;
    }
    else
    {
        document.f.fWdwTixBase.checked = false;
    }
}

// ===================================================================
//  FUNCTION: togglePrev(id)
//
//  Used to turn on and off the previous cruiselines field for people
//  who have, or haven't cruised before
// -------------------------------------------------------------------
function togglePrev(id)
{
    if (id != "no") {
        document.f.fPreviousCruiselines.disabled = false;
    } else {
        document.f.fPreviousCruiselines.disabled = true;
    }
}

// ===================================================================
//  FUNCTION: toggleDiv(id, divName, numDivs)
//
//
// -------------------------------------------------------------------
function toggleDiv(id, divName, numDivs)
{
    for (i = 1; i <= numDivs; i++)
    {
        if ((id == "yes") || (id == "1") || (id == "true") || (id == "package") ||
            (id.toLowerCase().substr(0, 5) == "other"))
        {
            showForm(divName + i);
        }
        else
        {
            hideForm(divName + i);
        }
    } // for
}

// ===================================================================
//  FUNCTION: updateNames()
//  If there isn't data in the name section already, move the first
//  and last name down
// -------------------------------------------------------------------
function updateNames()
{
    // fFirstName >> fGuestFirstName_1
    // fLastName >> fGuestLastName_1

    with (document.f)
    {
        // Does the first name already have data
        if (fGuestFirstName_1.value == '')
        {
            fGuestFirstName_1.value = fFirstName.value;
        }
        // Does the last name already have data
        if (fGuestLastName_1.value == '')
        {
            fGuestLastName_1.value = fLastName.value;
        }
    }
}

// ===================================================================
// FUNCTION: updateTickets(value)
//
// If the tickets value is not already set, update it when the number
// of nights is changed
// -------------------------------------------------------------------
function updateTickets(nights)
{
    var fid = document.getElementById('f');

    if (fid.fWdwTixDays.value.length == 0)
    {
        // "nights" can end up being a string on some javascript
        // implimentations and subtracting zero forces it to a number
        fid.fWdwTixDays.value = (nights - 0) + 1;
    }
}

// ===================================================================
// FUNCTION: showGuests()
//
// Show the correct number of guests. If guests are being hidden we
// leave the data there, but ignore it during submission - we know how
// many submissions there should be based on the fNumGuests form
// variable
// -------------------------------------------------------------------
function showGuests() {
    var iSel;
    var eid;
    var max = 8;
    var iShow;
    var iNum;
    var fid;


        // if more than 5 then we just get the two primes
        // Subtracting zero from each guarantees we use math and not strings
    with (document.f)
    {
        iShow  = fNumAdults.options[fNumAdults.selectedIndex].value - 0;
        iShow += fNumChildren.options[fNumChildren.selectedIndex].value - 0;
    }
    iNum = iShow;
    if (iNum > max)
    {
        iShow = 2;
    }

        // loop thru all of the items and show/hid as needed
    for (i = 1; i <= max; i++)
    {
        eid = "guest_" + i;

        iSel = document.getElementById(eid);
        if (i > iShow)
        {
            iSel.style.display = "none";
        }
        else
        {
            iSel.style.display = "";
        }
    }

    var x = max + 1;
    eid = "guest_" + x;
    iSel = document.getElementById(eid);
    if (iNum > max)
    {
        iSel.style.display = "";
    }
    else
    {
        iSel.style.display = "none";
    }
}

// ===================================================================
// FUNCTION: showPackageInfo()
//
// Show the link to the current package
// -------------------------------------------------------------------
function showPackageInfo(eid, pkg)
{
    var a = new Array();
    var s;

    if (eid.value != '')
    {
        a = eid.value.split("||");
        s  = '<a href="package.php?id=' + a[1] + '" title="Click for package details"';
        s += ' target="_blank"><img src="img/info_button_14x56.gif" border="0"></a>';
        pkg.innerHTML = s;
    }
    else
    {
        pkg.innerHTML = '';
    }
}

// ===================================================================
// FUNCTION: adjustHotels()
//
// When the user selects "California" as their destination we need to
// show them a list of "generic" hotels and "California" hotels, but
// not Hawaii hotels, etc.
// -------------------------------------------------------------------
function adjustHotels(did)
{
    // TODO: Add Code to alter the Hotels Dropdown List based
    //       on the Destination ID (did).
}

// ===================================================================
// FUNCTION: validateForm()
//
// -------------------------------------------------------------------
function validateForm()
{
    var b = true;
    var gf = elementCheck('vType');

    var bCRUISE = elementCheck('qCRUISE').style.display != 'none';
    var bLAND   = elementCheck('qLAND').style.display != 'none';

        // Check to see that we have a section open
    if (gf.value == 0)
    {
        alert( 'please select a vacation type to begin the form.');
        return false;
    }


	   // Top Section
	b = b & validateText('fFirstName', '', true);              // First Name
	b = b & validateText('fLastName', '', true);               // Last Name
	b = b & validateEmail('fEmail', '', true);                 // Email
	b = b & validateSame('fVerifyEmail', 'fEmail', '', true);  // verify email
	b = b & validateText('zpcal_1', '', true);                 // Start Date
	b = b & validateText('fNumNights', '', true);              // Number of nights

	   // Bottom Section
	b = b & validateSelect('fAirTransport', '', true);         // Air Transoportation
	b = b & validateSelect('fGroundTransport', '', true);      // Ground Transportation

        // Air Departure City
    b = b & validateVisible('fDepartureCity', '', true, 'departureRow1', 'text');

       // First Last & Age for the first set of guest info
	b = b & validateVisible('fGuestFirstName_1', '', true, 'guest_1', 'text');
	b = b & validateVisible('fGuestLastName_1', '', true, 'guest_1', 'text');
	b = b & validateVisible('fGuestAge_1', '', true, 'guest_1', 'text');

	   // Just age for 2 thru 8
	b = b & validateVisible('fGuestAge_2', '', true, 'guest_2', 'text');
	b = b & validateVisible('fGuestAge_3', '', true, 'guest_3', 'text');
	b = b & validateVisible('fGuestAge_4', '', true, 'guest_4', 'text');
	b = b & validateVisible('fGuestAge_5', '', true, 'guest_5', 'text');
	b = b & validateVisible('fGuestAge_6', '', true, 'guest_6', 'text');
	b = b & validateVisible('fGuestAge_7', '', true, 'guest_7', 'text');
	b = b & validateVisible('fGuestAge_8', '', true, 'guest_8', 'text');


	   //  Email Offers?
	b = b & validateSelect('fEmailOffers', '', true);


    // CRUISE
    if (bCRUISE)
    {
        b = b & validateSelect('fCruiseDest', '', true);
        b = b & validateVisible('fCruiseDestOther', '', true, 'cruiseDestOther1', 'text');
        b = b & validateSelect('fCruiseLine', '', true);
        b = b & validateVisible('fCruiseLineOther', '', true, 'cruiseLineOther1', 'text');
        b = b & validateVisible('fCruiseShip', '', true, 'cruiseShip1', 'select');
        b = b & validateVisible('fCruiseShipOther', '', true, 'cruiseShipOther1', 'text');
        b = b & validateSelect('fCruiseStateroomType', '', true);
        b = b & validateText('fCruiseNumStaterooms', '', true);
        b = b & validateSelect('fCruiseDiningTime', '', true);
        b = b & validateSelect('fCruiseBefore', '', true);
    }

    // LAND
    if (bLAND)
    {
        b = b & validateSelect('fLndDestination', '', true);
        b = b & validateVisible('fLndDestOther', '', true, 'lndDestOther1', 'text');
        b = b & validateSelect('fLndHotels', '', true);
        b = b & validateVisible('fLndHotelOther', '', true, 'lndHotelOther1', 'text');
        b = b & validateText('fLndNumRooms', '', true);
        b = b & validateSelect('fLndRoomType', '', true);
    }

    // Return our results
	if (b)
	{
        return true;
    }
    else
    {
        alert ('There are errors on the form.\n\nPlease correct any yellow fields.' );
        return false;
    }
}
