﻿// 'global' vars
var eHupControl = null;
var HupControl = null;
var sHupFwdUrl = null;
var sEHupFwdUrl = null;
var sFwdUrl = null;
var sEmailAddress = null;
var phUnknownDomain = null;
var phRegionRequest = null;
var phWales = null;
var sCheckEmailUrl = null;
var sOffersPage = null;

// Wentworth namespace
if (typeof Wentworth == 'undefined')
    Wentworth = {}

Wentworth.eHup = Class.create();

Object.extend(Wentworth.eHup.prototype, {
    initialize: function(id) {
        this._id = id;
        this._div = document.getElementById(this._id);

        var descendants = $(this._id).childElements();
        
        this._topshadow = descendants[0].childElements()[0];
        this._hupinner = descendants[1];

        var topshadowDescendants = Element.childElements(this._topshadow);
        this._fieldset = topshadowDescendants[0];

        var fieldsetDescendants = Element.childElements(this._fieldset);
        this._email = fieldsetDescendants[0];
        this._checkEmail = fieldsetDescendants[1];
        this._checkEmail.style.display = 'none';               //disabled by default, enabled onPageLoad

        this._Validators = descendants[1].childElements()[0];
        this._UnknownDomain = this._Validators.childElements()[2];
        this._RegionRequest = this._Validators.childElements()[3];
        this._PhWales = this._Validators.childElements()[4];

        this._div.widget = this;
    },


    setEmail: function(email) {
        $(this._email).value = email;
        return this;
    },

    getEmail: function() {
        return this._email.value;
    },

    getFwdUrl: function() {
        return this._fwdUrl;
    },
    setFwdUrl: function(fwdUrl) {
        this._fwdUrl = fwdUrl;
        return this;
    },
    getId: function() {
        return this._id;
    },
    getPhUnknownDomain: function() {
        return this._UnknownDomain;
    },
    getPhRegionRequest: function() {
        return this._RegionRequest;
    },
    getPhWales: function() {
        return this._PhWales;
    },
    enableGoButton: function() {
        this._checkEmail.style.display = 'block';
        return this;
    },
    hideValidators: function() {

        if (this._RegionRequest.style.display == 'block') {
            this._RegionRequest.style.display = 'none';
        }
        if (this._UnknownDomain.style.display == 'block') {
            this._UnknownDomain.style.display = 'none';
        }
        return this;
    }

});

function CheckDomain(Page, EmailAddress, UnknownDomain, RegionRequest, Wales) {

    sEmailAddress = EmailAddress;
    phUnknownDomain = UnknownDomain;
    phRegionRequest = RegionRequest;
    phWales = Wales;

    // could be called from either of the two controls on the page (eHUP and HUP)
    // call the validators for the relevant control
    var bPageIsValid;
    if (Page == "eHupControl") {
        bPageIsValid = Page_ClientValidate("EHupRequest");
    } else {
        bPageIsValid = Page_ClientValidate("HupRequest");
    }

    if (bPageIsValid == true) {
        new Ajax.Request
                      (sCheckEmailUrl,
                          { method: 'POST', contentType: 'application/x-www-form-urlencoded',
                              parameters: { Email: EmailAddress },
                              onSuccess: processResponse

                              ,
                              onFailure: function(transport) {
                                  alert("Sorry, there was an unexpected error, please try again");
                              }
                          }
                      );
    }
}

function processResponse(originalRequest) {
    var _response = originalRequest.responseText;

    var _isvaliddomain = _response.indexOf("isvaliddomain=1");
    var _isexcluded = _response.indexOf("isexcluded=1");
    var _iswales = _response.indexOf("wales=1");
    var _programcode = _response.substring(_response.indexOf("programcode=") + 12);

    if (_iswales > -1) {    //wales agreement expired on 31/12/08
        phWales.style.display = 'block';
    } else {

        phWales.style.display = 'none';
        
        if (_isvaliddomain > -1) {

            //change the placeholders with the actual data
            var _HUPUrl = sFwdUrl.replace("xxxxxx", sEmailAddress).replace("yyyyyy", _programcode);

//          ------------------
//          Window popup code disabled, replacement is to open HUP site in the same window (see below)
//          ------------------
//            var _window = window.open(_HUPUrl, '', 'scrollbars=yes,resizable=yes,toolbar=yes,status=no,width=800,height=400')
//            if (_window == null || typeof (_window) == 'undefined') {
//            
//                $('phPopupBlocked').style.display = 'block';
//                $('uiEhupClickHere').href = _HUPUrl;
//                
//            }
//            else    //popup opened OK, set omniture tags and redirect to the offers page
//            {
//                var s = s_gi(s_account);
//                s.linkTrackVars = 'events,eVar27';
//                s.linkTrackEvents = 'event7';
//                s.events = 'event7';
//                if (_HUPUrl.indexOf("orders.microsoft.com") > -1) {  //HUP
//                    s.eVar27 = 'HUP';
//                    s.tl(this, 'o', 'HUP');
//                } else {
//                    s.eVar27 = 'eHup Referral';
//                    s.tl(this, 'o', 'eHup Referral');
//                }

//                window.location.href = sOffersPage;
//            }


            // Fire omniture tracking code
            var s = s_gi(s_account);
            s.linkTrackVars = 'events,eVar27';
            s.linkTrackEvents = 'event7';
            s.events = 'event7';
            if (_HUPUrl.indexOf("orders.microsoft.com") > -1)
            {  //HUP
                s.eVar27 = 'HUP';
                s.tl(this, 'o', 'HUP');
            }  else {
                s.eVar27 = 'eHup Referral';
                s.tl(this, 'o', 'eHup Referral');
            }

            // Open the HUP orders page in the same window
            window.location.href = _HUPUrl;

        } else {


            phUnknownDomain.style.display = 'block';
            

            if ($('phPopupBlocked').style.display != null){ $('phPopupBlocked').style.display = 'none'; } //eHUP           


            if (_isexcluded > -1) {
                phRegionRequest.style.display = 'none';
            } else {
                phRegionRequest.style.display = 'block';
            }

        }
    }
}