/*******************************************************************************
    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 == "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 = '';
    }
}

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

    var bWDW = elementCheck('qWDW').style.display != 'none';
    var bDCL = elementCheck('qDCL').style.display != 'none';
    var bDLR = elementCheck('qDLR').style.display != 'none';
    var bWDWC = elementCheck('qWDWcruise').style.display != 'none';
    var bDLP = elementCheck('qDLP').style.display != 'none';
    var bDHK = elementCheck('qDHK').style.display != 'none';
    var bTDS = elementCheck('qTDS').style.display != 'none';
    var bABD = elementCheck('qABD').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 & validateVisible('fAirTransport', '', true, 'airTransport1', 'select');
	b = b & validateSelect('fGroundTransport', '', true);      // Ground Transportation

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

       // First Last & Age for the 5 sets 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');
	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');


	// b = b & validateSelect('fCitizens', '', true);
	b = b & validateSelect('fEmailOffers', '', true);

	// WDW
	if (bWDW)
	{
    	b = b & validateSelect('fWdwPackage', '', true);
    	b = b & validateVisible('fWdwPackageType', '', true, 'packageRow1', 'select');
    	b = b & validateVisible('fWdwTixDays', '', true, 'packageRow2', 'text');
    	b = b & validateText('fWdwNumRooms', '', true);
    	b = b & validateSelect('fWdwResort1', '', true);
    	b = b & validateSelect('fWdwRoomType', '', true);
	}

	// DCL
	if (bDCL)
	{
        b = b & validateSelect('fDclDestination', '', true);
    	b = b & validateSelect('fDclStateroomType', '', true);
        b = b & validateText('fDclNumStaterooms', '', true);
    	// b = b & validateSelect('fDclDiningTime', '', true);
    	// b = b & validateSelect('fDclAddOnPackages', '', true);
	}

	// DLR
	if (bDLR)
	{
        b = b & validateSelect('fDlrPackage', '', true);
    	b = b & validateVisible('fDlrPackageType', '', true, 'dlrpackageRow1', 'select');
    	b = b & validateVisible('fDlrTixDays', '', true, 'dlrpackageRow2', 'text');
    	b = b & validateText('fDlrNumRooms', '', true);
    	b = b & validateSelect('fDlrResort1', '', true);
    	b = b & validateSelect('fDlrRoomType', '', true);
    	b = b & validateSelect('fDlrCharacterMeal', '', true);
    	b = b & validateSelect('fDlrLaAttractions', '', true);
    }

	// DCL & WDW
	if (bWDWC)
	{
        b = b & validateSelect('fWdwcDestination', '', true);
    	b = b & validateSelect('fWdwcStateroomType', '', true);
        b = b & validateText('fWdwcNumStaterooms', '', true);
    	b = b & validateSelect('fWdwcResort1', '', true);
    	// b = b & validateSelect('fWdwcDiningTime', '', true);
    	// b = b & validateSelect('fWdwcAddOnPackages', '', true);
    }
    
    // Disneyland Paris
	if (bDLP)
	{
        b = b & validateSelect('fDlpPackage', '', true);
    	b = b & validateVisible('fDlpPackageType', '', true, 'dlppackageRow1', 'select');
    	b = b & validateVisible('fDlpTixDays', '', true, 'dlppackageRow2', 'text');
    	b = b & validateText('fDlpNumRooms', '', true);
    	b = b & validateSelect('fDlpResort1', '', true);
    	b = b & validateSelect('fDlpRoomType', '', true);
    	b = b & validateSelect('fDlpCharacterMeal', '', true);
    	b = b & validateSelect('fDlpLaAttractions', '', true);
    }
    
    // Disney Hongkong
    if (bDHK)
    {
        b = b & validateSelect('fDhkPackage', '', true);
    	b = b & validateVisible('fDhkPackageType', '', true, 'dhkpackageRow1', 'select');
    	b = b & validateVisible('fDhkTixDays', '', true, 'dhkpackageRow2', 'text');
    	b = b & validateText('fDhkNumRooms', '', true);
    	b = b & validateSelect('fDhkResort1', '', true);
    	b = b & validateSelect('fDhkRoomType', '', true);
    	b = b & validateSelect('fDhkCharacterMeal', '', true);
    	b = b & validateSelect('fDhkLaAttractions', '', true);
    }
    
    // Tokyo Disney Land/Sea
    if (bTDS)
    {
        b = b & validateSelect('fTdsPackage', '', true);
    	b = b & validateVisible('fTdsPackageType', '', true, 'tdspackageRow1', 'select');
    	b = b & validateVisible('fTdsTixDays', '', true, 'tdspackageRow2', 'text');
    	b = b & validateText('fTdsNumRooms', '', true);
    	b = b & validateSelect('fTdsResort1', '', true);
    	b = b & validateSelect('fTdsRoomType', '', true);
    	b = b & validateSelect('fTdsCharacterMeal', '', true);
    	b = b & validateSelect('fTdsLaAttractions', '', true);
    }
    
    // Adventures by Disney
    if (bABD)
    {
        b = b & validateSelect('fAbdAdventure', '', true);
        b = b & validateText('fAbdNumRooms', '', true);
    }
    

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