

/**
 * Open a popup window containing the terms and conditions
 */
function popupWarrantyTerms(oEvent)
{
    oEvent = oEvent || window.event;
    oEvent.cancelBubble = true;
    oEvent.stopPropagation && oEvent.stopPropagation();
    oEvent.preventDefault && oEvent.preventDefault();
    var iPopupWidth = 720;
    var iPopupHeight = 500;
    var iLeft = Math.round((screen.availWidth - iPopupWidth) / 2);
    var iTop = Math.round((screen.availHeight - iPopupHeight) / 2);
    window.open('terms.php', 'warranty_terms', 'status=no,toolbar=no,location=no,menubar=no,directories=no,resizable=yes,scrollbars=yes,height='+iPopupHeight+',width='+iPopupWidth+',left='+iLeft+',top='+iTop);
    return false;
}


/**
 * When the terms and condition window is opened by JavaScript, add a button
 * to close the popup with JavaScript
 */
var bIsWarrantyTermsButtonAdded = false
function addWarrantyTermsButton(oEvent)
{
    oEvent = oEvent || window.event;
    if (bIsWarrantyTermsButtonAdded)
    {
        return false;
    }
    bIsWarrantyTermsButtonAdded = true;

    // window was opened without javascript
    if (!window.opener)
    {
        return false;
    }

    // popup was opened using javascript
    var aTranslations = {
        'en' : 'Close',
        'fr' : 'Fermer',
        'nl' : 'Sluiten'
    };
    var sLang = document.documentElement.getAttribute('lang');
    if (!sLang)
    {
        return false;
    }
    sLang = sLang.substr(0, 2);
    if (!aTranslations[sLang])
    {
        return false;
    }
    var oContent = document.getElementById('content');
    var oButton = oContent.appendChild(document.createElement('button'));
    oButton.appendChild(document.createTextNode(aTranslations[sLang]));
    window.addEvent(oButton, 'click', closeWarrantyTerms, false);
}


/**
 * Close the terms and conditions popup when the button is clicked
 */
function closeWarrantyTerms(oEvent)
{
    oEvent = oEvent || window.event;
    window.close();
}


/**
 * Prepare the landing page to open the Kieskeurig review page in a new window
 * (not a popup)
 */
var bIsKieskeurigReviewLoaded = false
function loadKieskeurigReview(oEvent)
{
    oEvent = oEvent || window.event;
    if (bIsKieskeurigReviewLoaded)
    {
        return false;
    }
    bIsKieskeurigReviewLoaded = true;

    var oForm = document.getElementById('kieskeurig');
    if (!oForm)
    {
        return false;
    }
    addEvent(oForm, 'submit', openKieskeurigReview, false);
}


/**
 * Open the Kieskeurig review page in a new window
 */
function openKieskeurigReview(oEvent)
{
    oEvent = oEvent || window.event;
    oEvent.cancelBubble = true;
    oEvent.stopPropagation && oEvent.stopPropagation();
    oEvent.preventDefault && oEvent.preventDefault();
    var oModel = document.getElementById('kieskeurig-model');
    if (!oModel)
    {
        return false;
    }
    window.open('kieskeurig.php?model='+oModel.value, 'kieskeurig');
    return false;
}


/**
 * Open a popup window containing the serial photograph
 */
function popupWarrantySerial(oEvent)
{
    oEvent = oEvent || window.event;
    oEvent.cancelBubble = true;
    oEvent.stopPropagation && oEvent.stopPropagation();
    oEvent.preventDefault && oEvent.preventDefault();
    var iPopupWidth = 720;
    var iPopupHeight = 500;
    var iLeft = Math.round((screen.availWidth - iPopupWidth) / 2);
    var iTop = Math.round((screen.availHeight - iPopupHeight) / 2);
    window.open('serial.php', 'warranty_serial', 'status=no,toolbar=no,location=no,menubar=no,directories=no,resizable=yes,scrollbars=yes,height='+iPopupHeight+',width='+iPopupWidth+',left='+iLeft+',top='+iTop);
    return false;
}


/**
 * When the serial and condition window is opened by JavaScript, add a button
 * to close the popup with JavaScript
 */
var bIsWarrantySerialButtonAdded = false
function addWarrantySerialButton(oEvent)
{
    oEvent = oEvent || window.event;
    if (bIsWarrantySerialButtonAdded)
    {
        return false;
    }
    bIsWarrantySerialButtonAdded = true;

    // window was opened without javascript
    if (!window.opener)
    {
        return false;
    }

    // popup was opened using javascript
    var aTranslations = {
        'en' : 'Close',
        'fr' : 'Fermer',
        'nl' : 'Sluiten'
    };
    var sLang = document.documentElement.getAttribute('lang');
    if (!sLang)
    {
        return false;
    }
    sLang = sLang.substr(0, 2);
    if (!aTranslations[sLang])
    {
        return false;
    }
    var oContent = document.getElementById('content');
    var oButton = oContent.appendChild(document.createElement('button'));
    oButton.appendChild(document.createTextNode(aTranslations[sLang]));
    window.addEvent(oButton, 'click', closeWarrantySerial, false);
}


/**
 * Close the serial and conditions popup when the button is clicked
 */
function closeWarrantySerial(oEvent)
{
    oEvent = oEvent || window.event;
    window.close();
}


/**
 * Load everything
 */
var bIsWarrantyLoaded = false;
function loadWarranty(oEvent)
{
    oEvent = oEvent || window.event;
    if (bIsWarrantyLoaded)
    {
        return false;
    }
    bIsWarrantyLoaded = true;

    // open the terms & conditions links in a popup
    var aLinks = document.getElementsByTagName('a');
    var iLinks = aLinks.length;
    for (var i = 0; i < iLinks; ++i)
    {
        var oLink = aLinks[i];
        var sHref = oLink.getAttribute('href', 2);
        if ('terms.php' === sHref)
        {
            addEvent(oLink, 'click', popupWarrantyTerms, true);
        }
        else if ('serial.php' === sHref)
        {
            addEvent(oLink, 'click', popupWarrantySerial, true);
        }
    }

    // do stuff depending on the current page
    var sType = document.body.className;

    // add a 'close' button in the terms and conditions popup
    if ('terms' === sType)
    {
        addWarrantyTermsButton(oEvent);
    }
    // add a 'close' button in the serial popup
    else if ('serial' === sType)
    {
        addWarrantySerialButton(oEvent);
    }
    // load the preview page in a new window
    else if ('landing' === sType)
    {
        loadKieskeurigReview(oEvent);
    }
}


// load everything on DOM or window load
addEvent(window, 'DOMContentLoaded', loadWarranty, true);
addEvent(window, 'load', loadWarranty, true);


