// used to open a pop-up window
function pop(path, width, height, scroll) {
    var attributes = "left=20,top=20,width=" + width + ",height=" + height + ",scrollbars=" + scroll;
    var myPop = window.open(path, 'pop', attributes);
    myPop.focus();

} // End of pop.

//loads a new page in the parent window (as opposed to the parent frame)
function loadParentWindow(url) {
    window.opener.location.href = url;
} // end of loadParentWindow()

// For certain types of fields, BlueMartini inserts a hidden field with the same name
// e.g: if we have a field called "ADDRESS<>state_cd" which is a dropdown,
// another hidden field with the same name is inserted by BM.  When accessing
// values from the dropdown, we need this method to get to the dropdown rather
// than the hidden field
function findNotHidden(fieldName) {
    var form = document.forms[0];
    var formlength = form.length;
    for (var i = 0; i < formlength; i++) {
        if ( form[i].name == fieldName ) {
            if ( form[i].type != 'hidden' ) {
                return i;
            }
        }
    }

    return -1;

} // End of findNotHidden(fieldName).

//The below function set the URLcode in bannerAccess page for savings program.
function getURLCode (url, partnerIdVar, partnerId, imagePath, partnerName, savingsProgram, type) {

    var bannerForm = document.forms[0];
    var bannerCode;
    if ( type == 'image') {

        bannerCode = "<a href=\"" + url + "?" + partnerIdVar + "=" + partnerId + "&" + partnerName + "=" + savingsProgram + "&offer=default\"><img border=\"0\" src=\"" + imagePath + "\"></a>";
    } else {
        bannerCode = "<a href=\"" + url + "?" + partnerIdVar + "=" + partnerId + "&" + partnerName + "=" + savingsProgram + "&offer=default\">" + imagePath + "</a>";
    }

    bannerForm[findNotHidden("URLCode")].value = bannerCode;

} //End of getURLCode function.

// populate the billing field on the LOA portion of the order form
function populateLOABilling() {
    // if the loa portion is displayed on the screen, we need to only check if
    // one of those fields is not undefined.  We dont need to check for all of
    // the LOA fields because the screen displays all of them or none at all
    var loaExists = findNotHidden("ORDER_FORM<>loa_billing");
    var vadLoaExists = findNotHidden("ORDER_FORM<>vad_loa_billing");

    if ( loaExists != -1 || vadLoaExists != -1 )
    {
        orderForm = document.forms[0];
        var firstName = orderForm[findNotHidden("CURRENT_USER<>firstName")].value;
        var lastName = orderForm[findNotHidden("CURRENT_USER<>lastName")].value;
        var address1 = orderForm[findNotHidden("ADDRESS<>address1")].value;
        var address2 = orderForm[findNotHidden("ADDRESS<>address2")].value;
        var city = orderForm[findNotHidden("ADDRESS<>city")].value;
        var stateSelect = orderForm[findNotHidden("ADDRESS<>state_cd")];
        var state = stateSelect.options[stateSelect.options.selectedIndex].value;
        var postal = orderForm[findNotHidden("ADDRESS<>postal")].value;

        var billing = getFormIndex( loaExists, "loa_billing" );

        if ( ( firstName != "") && ( lastName != "") && ( address1 != "") &&
            ( city != "") && ( state != "") && ( postal != "") )
        {
            orderForm[ billing ].value =
                firstName + ' ' + lastName + '\n' +
                address1 + ' ' + address2 + '\n' +
                city + ', ' + state + ' ' + postal;
        }
        else
        {
            orderForm[ billing ].value = "";
        }

    }

} // End of populateLOABilling().

function getFormIndex( loaExists, formField )
{
    if ( loaExists > -1 )
    {
        return findNotHidden( "ORDER_FORM<>" + formField );
    }
    else
    {
        return findNotHidden( "ORDER_FORM<>" + "vad_" + formField );
    }
}

// populate the first phone number in the LOA portion of the form
function populateLOAFirstPhone() {
    // if the loa portion is displayed on the screen, we need to only check if
    // one of those fields is not undefined.  We dont need to check for all of
    // the LOA fields because the screen displays all of them or none at all
    var loaExists = findNotHidden("ORDER_FORM<>loa_billing");
    var vadLoaExists = findNotHidden("ORDER_FORM<>vad_loa_billing");

    if ( ( loaExists != -1 ) || ( vadLoaExists != -1 ) )
    {
        var ldChecked = true;
        var ltChecked = false;

        var ldCheckBox1 = getFormIndex( loaExists, "loa_ldCheckBox1" );
        var ltCheckBox1 = getFormIndex( loaExists, "loa_ltCheckBox1" );

        orderForm = document.forms[0];

        var selectLocalToll = findNotHidden("ORDER_FORM<>selectLocalToll");
        if ( selectLocalToll != -1 )
        {
            ltChecked = orderForm[selectLocalToll].checked;
        }

        // in the case of a LD product with the local toll reduction option, there
        // will not be a local toll checkbox at the top of the order form.  Instead
        // there will only be a local toll reduction option checkbox
        var localTollReductionOption = findNotHidden("localTollReductionOption");
        if ( localTollReductionOption != -1 )
        {
            ltChecked = orderForm[localTollReductionOption].checked;
        }

        var loaPhoneNumber1 = getFormIndex( loaExists, "loa_phoneNumber1" );
        var areaCode = orderForm[findNotHidden("ORDER_FORM<>home_phone_area_code")].value;
        var prefix = orderForm[findNotHidden("ORDER_FORM<>home_phone_prefix")].value;
        var suffix = orderForm[findNotHidden("ORDER_FORM<>home_phone_suffix")].value;

        if ( ( areaCode != "") && ( prefix != "") && ( suffix != "") ) {
            orderForm[loaPhoneNumber1].value = areaCode + '-' + prefix + '-' + suffix;

            if ( ldCheckBox1 != -1 ) {
                orderForm[ldCheckBox1].checked = ldChecked;
            }

            if ( ltCheckBox1 != -1 ) {
                orderForm[ltCheckBox1].checked = ltChecked;
            }

        } else {
            orderForm[loaPhoneNumber1].value = "";

            if ( ldCheckBox1 != -1 ) {
                orderForm[ldCheckBox1].checked = false;
            }

            if ( ltCheckBox1 != -1 ) {
                orderForm[ltCheckBox1].checked = false;
            }
        }

    }

} // End of populateLOAFirstPhone().

// populate the second phone number in the LOA portion of the form
function populateLOASecondPhone() {
    // if the loa portion is displayed on the screen, we need to only check if
    // one of those fields is not undefined.  We dont need to check for all of
    // the LOA fields because the screen displays all of them or none at all
    var loaExists = findNotHidden("ORDER_FORM<>loa_billing");

    if ( loaExists != -1 ) {
        var ldChecked = true;
        var ltChecked = false;

        var ldCheckBox2 = findNotHidden("ORDER_FORM<>loa_ldCheckBox2");
        var ltCheckBox2 = findNotHidden("ORDER_FORM<>loa_ltCheckBox2");

        orderForm = document.forms[0];

        var selectLocalToll = findNotHidden("ORDER_FORM<>selectLocalToll");
        if ( selectLocalToll != -1 ) {
            ltChecked = orderForm[selectLocalToll].checked;
        }

        // in the case of a LD product with the local toll reduction option, there
        // will not be a local toll checkbox at the top of the order form.  Instead
        // there will only be a local toll reduction option checkbox
        var localTollReductionOption = findNotHidden("localTollReductionOption");
        if ( localTollReductionOption != -1 )
        {
            ltChecked = orderForm[localTollReductionOption].checked;
        }

        var loaPhoneNumber2 = findNotHidden("ORDER_FORM<>loa_phoneNumber2");
        var areaCode2 = orderForm[findNotHidden("ORDER_FORM<>second_phone_area_code")].value;
        var prefix2 = orderForm[findNotHidden("ORDER_FORM<>second_phone_prefix")].value;
        var suffix2 = orderForm[findNotHidden("ORDER_FORM<>second_phone_suffix")].value;

        if ( ( areaCode2 != "") && ( prefix2 != "") && ( suffix2 != "") ) {
            orderForm[loaPhoneNumber2].value = areaCode2 + '-' + prefix2 + '-' + suffix2;

            if ( ldCheckBox2 != -1 ) {
                orderForm[ldCheckBox2].checked = ldChecked;
            }

            if ( ltCheckBox2 != -1 ) {
                orderForm[ltCheckBox2].checked = ltChecked;
            }

        } else {
            orderForm[loaPhoneNumber2].value = "";

            if ( ldCheckBox2 != -1 ) {
                orderForm[ldCheckBox2].checked = false;
            }

            if ( ltCheckBox2 != -1 ) {
                orderForm[ltCheckBox2].checked = false;
            }

        }

    }

} // End of populateLOASecondPhone().

// populate the SSN in the LOA portion of the form
function populateSSN() {
    // if the loa portion is displayed on the screen, we need to only check if
    // one of those fields is not undefined.  We dont need to check for all of
    // the LOA fields because the screen displays all of them or none at all
    var loaExists = findNotHidden("ORDER_FORM<>loa_billing");
    var vadLoaExists = findNotHidden("ORDER_FORM<>vad_loa_billing");

    if ( loaExists != -1 || vadLoaExists != -1 )
    {
        orderForm = document.forms[0];

        var loaSSN = getFormIndex( loaExists, "loa_ssn" );
        var ssn = orderForm[findNotHidden("ORDER_FORM<>ssn1")].value +
                  orderForm[findNotHidden("ORDER_FORM<>ssn2")].value +
                  orderForm[findNotHidden("ORDER_FORM<>ssn3")].value;

        if ( ssn.length == 9 )
        {
            orderForm[loaSSN].value = ssn;
        }
        else
        {
            orderForm[loaSSN].value = "";
        }
    }
} // End of populateSSN().

// Populates the earthlink userid field with what is selected in the dropdown
function populateEarthlinkUsername() {
    orderForm = document.forms[0];
    var selectedUsername = orderForm[findNotHidden("suggestion")].value;
    if (selectedUsername != "") {
        orderForm[findNotHidden("ISP_INFO<>userid")].value = selectedUsername;
    }

} // end populateEarthlinkUsername

// populate the birthday field in the LOA portion of the form
function populateBirthday() {
    // if the loa portion is displayed on the screen, we need to only check if
    // one of those fields is not undefined.  We dont need to check for all of
    // the LOA fields because the screen displays all of them or none at all
    var loaExists = findNotHidden("ORDER_FORM<>loa_billing");
    var vadLoaExists = findNotHidden("ORDER_FORM<>vad_loa_billing");

    if ( ( loaExists != -1 ) || ( vadLoaExists != -1 ) )
    {
        orderForm = document.forms[0];

        var daySelect = orderForm[findNotHidden("ORDER_FORM<>dayOfBirth")];
        var day = daySelect.options[daySelect.options.selectedIndex].value;

        var monthSelect = orderForm[findNotHidden("ORDER_FORM<>monthOfBirth")];
        var month = monthSelect.options[monthSelect.options.selectedIndex].value;

        var yearSelect = orderForm[findNotHidden("ORDER_FORM<>yearOfBirth")];
        var year = yearSelect.options[yearSelect.options.selectedIndex].value;

        if ( ( day != "") && ( month != "") && ( year != "") )
        {
            var birthDay = getMonthString(month) + ' ' + day + ', ' + year;
            orderForm[ getFormIndex( loaExists, "loa_birthday" ) ].value = birthDay;
        }
        else
        {
            orderForm[ getFormIndex( loaExists, "loa_birthday" ) ].value = "";
        }

    }

} // end populateBirthday

// convert month number to string
function getMonthString(month) {
    var monthString;

    if ( month == 0 ) {
        monthString = "Jan";
    } else if ( month == 1 ) {
       monthString = "Feb";
    } else if ( month == 2 ) {
       monthString = "Mar";
    } else if ( month == 3 ) {
       monthString = "Apr";
    } else if ( month == 4 ) {
       monthString = "May";
    } else if ( month == 5 ) {
       monthString = "Jun";
    } else if ( month == 6 ) {
       monthString = "Jul";
    } else if ( month == 7 ) {
       monthString = "Aug";
    } else if ( month == 8 ) {
       monthString = "Sep";
    } else if ( month == 9 ) {
       monthString = "Oct";
    } else if ( month == 10 ) {
       monthString = "Nov";
    } else if ( month == 11 ) {
       monthString = "Dec";
    }

    return monthString;

} // end getMonthString

// if foncard checkbox is checked, change the num foncards quantity to 1 and uncheck the
// welcome email checkbox.  if unchecked, change the quantity to 0 and also check
// the welcome email checkbox
function setOtherOptions() {
    // ASSUMPTION: number of foncards in the dropdown begins at 0.  Therefore
    // number of foncards at index 0 is 0, index 1 is 1, so on...

    orderForm = document.forms[0];

    // For United airlines, we don't display the welcome_package_by_email checkbox
    var welcomeCheckboxIndex = findNotHidden("ORDER_FORM<>welcome_package_email");

    var ldFoncardsChecked = orderForm[findNotHidden("ORDER_FORM<>ld_foncard_cbox")].checked;
    var numCardsSelect = orderForm[findNotHidden("ORDER_FORM<>num_ld_foncards")];

    if ( ldFoncardsChecked ) {
        numCardsSelect.selectedIndex = 1;
        if ( welcomeCheckboxIndex != -1 ) {
            orderForm[welcomeCheckboxIndex].checked = false;
        }
    } else {
        numCardsSelect.selectedIndex = 0;
        if ( welcomeCheckboxIndex != -1 ) {
            orderForm[welcomeCheckboxIndex].checked = true;
        }
    }

} // end setOtherOptions

// if number of foncards is zero, uncheck the foncard checkbox and check the
// "welcome package by email" checkbox
function setFoncardCheckBox() {
    // ASSUMPTION: number of foncards in the dropdown begins at 0.  Therefore
    // number of foncards at index 0 is 0, index 1 is 1, so on...

    orderForm = document.forms[0];

    var ldFoncardsCheckbox = orderForm[findNotHidden("ORDER_FORM<>ld_foncard_cbox")];
    var numCardsSelect = orderForm[findNotHidden("ORDER_FORM<>num_ld_foncards")];

    // For United airlines, we don't display the welcome_package_by_email checkbox
    var welcomeCheckboxIndex = findNotHidden("ORDER_FORM<>welcome_package_email");

    if ( numCardsSelect.selectedIndex == 0 ) {
        ldFoncardsCheckbox.checked = false;
        if ( welcomeCheckboxIndex != -1 ) {
            orderForm[welcomeCheckboxIndex].checked = true;
        }
    } else {
        ldFoncardsCheckbox.checked = true;
        if ( welcomeCheckboxIndex != -1 ) {
            orderForm[welcomeCheckboxIndex].checked = false;
        }
    }

} // end setFoncardCheckBox


// populate the local toll checkboxes on the LOA
function populateLocalToll() {
    var loaExists = findNotHidden("ORDER_FORM<>loa_billing");
    var vadLoaExists = findNotHidden("ORDER_FORM<>vad_loa_billing");

    if ( ( loaExists != -1 ) || ( vadLoaExists != -1 ) )
    {
        orderForm = document.forms[0];

        var ltChecked = orderForm[findNotHidden("localTollReductionOption")].checked;

        var ltCheckBox1 = getFormIndex( loaExists, "loa_ltCheckBox1" );
        var loaPhoneNumber1 = getFormIndex( loaExists, "loa_phoneNumber1" );
        var areaCode = orderForm[findNotHidden("ORDER_FORM<>home_phone_area_code")].value;
        var prefix = orderForm[findNotHidden("ORDER_FORM<>home_phone_prefix")].value;
        var suffix = orderForm[findNotHidden("ORDER_FORM<>home_phone_suffix")].value;

        if ( ( areaCode != "") && ( prefix != "") && ( suffix != "") ) {
            orderForm[loaPhoneNumber1].value = areaCode + '-' + prefix + '-' + suffix;

            if ( ltCheckBox1 != -1 ) {
                orderForm[ltCheckBox1].checked = ltChecked;
            }

        } else {
            orderForm[loaPhoneNumber1].value = "";

            if ( ltCheckBox1 != -1 ) {
                orderForm[ltCheckBox1].checked = false;
            }
        }

        var ltCheckBox2 = findNotHidden("ORDER_FORM<>loa_ltCheckBox2");
        var loaPhoneNumber2 = findNotHidden("ORDER_FORM<>loa_phoneNumber2");
        var areaCode2 = orderForm[findNotHidden("ORDER_FORM<>second_phone_area_code")].value;
        var prefix2 = orderForm[findNotHidden("ORDER_FORM<>second_phone_prefix")].value;
        var suffix2 = orderForm[findNotHidden("ORDER_FORM<>second_phone_suffix")].value;

        if ( ( areaCode2 != "") && ( prefix2 != "") && ( suffix2 != "") ) {
            orderForm[loaPhoneNumber2].value = areaCode2 + '-' + prefix2 + '-' + suffix2;

            if ( ltCheckBox2 != -1 ) {
                orderForm[ltCheckBox2].checked = ltChecked;
            }

        } else {
            orderForm[loaPhoneNumber2].value = "";

            if ( ltCheckBox2 != -1 ) {
                orderForm[ltCheckBox2].checked = false;
            }

        }

    }

} // end populateLocalToll

// set prod code into hidden field, assign sub-request name to "the bmSubmit hidden field",
// modify the form action, and finally submit the form
function addToOrder(prodCode) {
    var paramIdentifier = '';
    var strAction = document.planMatrix.action;
    if ( strAction.indexOf('?') == -1 ) {
        paramIdentifier = '?';
    } else {
        paramIdentifier = '&';
    }
    document.planMatrix.bmSubmit.value = 'addToOrder';
    document.planMatrix.prodCode.value = prodCode;
    document.planMatrix.action = document.planMatrix.action + paramIdentifier + 'addToOrder=addToOrder';
    document.planMatrix.submit();
}

// set prod code into hidden field, assign sub-request name to "the bmSubmit hidden field",
// modify the form action, and finally submit the form from airline Matrix page.
function airlineAddToOrder(prodCode) {
    var paramIdentifier = '';
    var strAction = document.airlineMatrix.action;
    if ( strAction.indexOf('?') == -1 ) {
        paramIdentifier = '?';
    } else {
        paramIdentifier = '&';
    }
    document.airlineMatrix.bmSubmit.value = 'addToOrder';
    document.airlineMatrix.prodCode.value = prodCode;
    document.airlineMatrix.action = document.airlineMatrix.action + paramIdentifier + 'addToOrder=addToOrder';
    document.airlineMatrix.submit();
}

// set prod and pid codes into hidden fields, assign sub-request name to "the bmSubmit hidden field",
// modify the form action, and finally submit the form
function orderFromPartnerMatrix(prodCode, pidCode) {
    var paramIdentifier = '';
    var strAction = document.voicepartnermatrixproduct.action;
    if ( strAction.indexOf('?') == -1 ) {
        paramIdentifier = '?';
    } else {
        paramIdentifier = '&';
    }
    document.voicepartnermatrixproduct.bmSubmit.value = 'orderNow';
    document.voicepartnermatrixproduct.prodCode.value = prodCode;
    document.voicepartnermatrixproduct.PID.value = pidCode;
    document.voicepartnermatrixproduct.action = document.voicepartnermatrixproduct.action + paramIdentifier + 'orderNow=orderNow';
    document.voicepartnermatrixproduct.submit();
}

// function used to get the internatinal rates for each country.
function getIntlRate() {
    var correctForm;
    var dropdownfield = -1;
    var textboxfield = -1;

    var currentForm;
    var numForms = document.forms.length;
    var formlength;
    var currentFieldName;

    for (var formIterator = 0; formIterator < numForms; formIterator++) {
        currentForm = document.forms[formIterator];
        formlength = currentForm.length;

        for (var fieldIterator = 0; fieldIterator < formlength; fieldIterator++) {
            currentFieldName = currentForm[fieldIterator].name;
            if ( currentFieldName == "rates" ) {
                if ( currentForm[fieldIterator].type != 'hidden' ) {
                    dropdownfield = fieldIterator;
                    correctForm = currentForm;
                }
            } else if ( currentFieldName == "ratefield" ) {
                if ( currentForm[fieldIterator].type != 'hidden' ) {
                    textboxfield = fieldIterator;
                    correctForm = currentForm;
                }
            }

        } // loop thru fields in current form

        // if both the fields are found, there is no need to loop thru the remaining forms
        if ( ( dropdownfield != -1 ) && ( textboxfield != -1 ) ) {
            break;
        }

    } // loop thru all forms

    if ( ( dropdownfield != -1 ) && ( textboxfield != -1 ) ) {
        var rateSelect = correctForm[dropdownfield];
        var rate = rateSelect.options[rateSelect.options.selectedIndex].value;
        correctForm[textboxfield].value = rate;
    }

} // End of getIntlRate.
