﻿
$(document).ready(function () {


    function megaHoverOver() {
        $(this).find(".sub").stop().fadeTo('fast', 1).show();

        //Calculate width of all ul's
        (function ($) {
            jQuery.fn.calcSubWidth = function () {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function () {
                    rowWidth += $(this).width();
                });
            };
        })(jQuery);

        if ($(this).find(".row").length > 0) { //If row exists...
            var biggestRow = 0;
            //Calculate each row
            $(this).find(".row").each(function () {
                $(this).calcSubWidth();
                //Find biggest row
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            //Set width
            $(this).find(".sub").css({ 'width': biggestRow });
            $(this).find(".row:last").css({ 'margin': '0' });

        } else { //If row does not exist...

            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub").css({ 'width': rowWidth });

        }
    }

    function megaHoverOut() {
        $(this).find(".sub").stop().fadeTo('fast', 0, function () {
            $(this).hide();
        });
    }


    var config = {
        sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
        interval: 100, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 10, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("ul#TopNav li .sub").css({ 'opacity': '0' });
    $("ul#TopNav li").hoverIntent(config);

});

/*

$(document).ready(function (e) {
    $("span.ReqField")
        .bind("DOMAttrModified propertychange", function (e) {
            // Exit early if IE because it throws this event lots more
            if (e.originalEvent.propertyName && e.originalEvent.propertyName != "isvalid") return;

            var controlToValidate = $("#" + this.controltovalidate);
            var validators = controlToValidate.attr("Validators");
            if (validators == null) return;

            var isValid = true;
            $(validators).each(function () {
                if (this.isvalid !== true) {
                    isValid = false;
                }
            });

            if (isValid) {
                controlToValidate.removeClass("ReqFieldError");
            } else {
                controlToValidate.addClass("ReqFieldError");
            }
        });
    });
*/

function emptyTextReplace(ctlID) {
    if (document.getElementById(ctlID).value == "") {
        document.getElementById(ctlID).value = " Search Title/Actor/Director";
    }
}

function emptyText(ctlID) {
    document.getElementById(ctlID).value = "";
}

function noPostDefault(ctlID) {
    if (document.getElementById(ctlID).value == " Search Title/Actor/Director") {
        return false;
    } else {
        return true;
    }
}

function acePopulated(sender, e) {
    var target = sender.get_completionList();
    var children = target.childNodes;
    var searchText = sender.get_element().value;

    for (var i = 0; i < children.length; i++) {
        var child = children[i];

        //firefox replace
        child.innerHTML = eval('child.innerHTML.replace(/(<b class="AjaxAutoHighlightItem">)/i, "")');
        //IE replace
        child.innerHTML = eval('child.innerHTML.replace(/(<B class=AjaxAutoHighlightItem>)/i, "")');
        child.innerHTML = child.innerHTML.replace("</B>", "");
        child.innerHTML = eval("child.innerHTML.replace(/(" + searchText + ")/i, '<b class=AjaxAutoHighlightItem>$1</b>')");
    }
}

function aceSelected() {
    var behavior = $find('AutoCompleteEx');
    var target = behavior.get_completionList();
    var acindex = behavior._selectIndex;

    if (acindex == -1) {
        acindex = 0;
    }

    var children = target.childNodes;
    var child = children[acindex];
    //firefox replace
    child.innerHTML = eval('child.innerHTML.replace(/(<b class="AjaxAutoHighlightItem">)/i, "")');
    //IE replace
    child.innerHTML = eval('child.innerHTML.replace(/(<B class=AjaxAutoHighlightItem>)/i, "")');
    child.innerHTML = child.innerHTML.replace("</B>", "");
    document.getElementById('ctl00_Header1_txtSearch').value = child.innerHTML;

    forceSearchClick(event, 'ctl00_Header1_imgSearch');
}

function forceSearchClick(e, elemId) {
    var elem = document.getElementById(elemId);
    var evt = (e) ? e : window.event;
    var intKey = (evt.which) ? evt.which : evt.keyCode;

    var Behavior = $find('AutoCompleteEx');

    elem.click();
    return false;
}

function forceClick(e, elemId) {
    var elem = document.getElementById(elemId);
    var evt = (e) ? e : window.event;
    var intKey = (evt.which) ? evt.which : evt.keyCode;

    if (intKey == 13) {
        elem.click();
        return false;
    }
    return true;
}

function ShowTerms() {
    window.open('http://www.Screenclick.com/Terms', 'terms', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,left=20,top=20,width=900,height=800');
}

function validatePage() {
    var modalPopupBehavior = $find('programmaticModalPopupBehavior');

    if (Page_ClientValidate() == false) {
        modalPopupBehavior.show();
        return false;
    } else {
        modalPopupBehavior.hide();
        return true;
    }
}

function validatePageSignUp() {

    document.getElementById('lblInvalidFields').style.display = "";
    document.getElementById('lblRequiredFields').style.display = "none";

    var validPage;
    validPage = 1;

    if (Page_ClientValidate() == false) { validPage = 0; }
    else { document.getElementById('lblInvalidFields').style.display = "none"; }

    document.getElementById('li1').style.display = "none";
    document.getElementById('li2').style.display = "none";
    document.getElementById('li3').style.display = "none";

    if (document.getElementById("ctl00_MainContent_txtEmailAddress").value == "") {
        //document.getElementById("ctl00_MainContent_txtEmailAddress").setAttribute("class", "ReqFieldError");
        document.getElementById('lblRequiredFields').style.display = "";
        document.getElementById("li1").style.display = "";
        validPage = 0;
    }

    if (document.getElementById("ctl00_MainContent_txtPassword").value == "") {
        //document.getElementById("ctl00_MainContent_txtPassword").setAttribute("class", "ReqFieldError");
        document.getElementById('lblRequiredFields').style.display = "";
        document.getElementById("li2").style.display = "";
        validPage = 0;
    }

    if (document.getElementById("ctl00_MainContent_txtPasswordConfirm").value == "") {
        //document.getElementById("ctl00_MainContent_txtPasswordConfirm").setAttribute("class", "ReqFieldError");
        document.getElementById('lblRequiredFields').style.display = "";
        document.getElementById("li3").style.display = "";
        validPage = 0;
    }

    if (document.getElementById("ctl00_MainContent_valSummary").innerHTML == "<UL></UL>") {
        document.getElementById('lblInvalidFields').style.display = "none";
    }

    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    if (validPage == 1) {
        modalPopupBehavior.hide();
        return true;
    } else {
        modalPopupBehavior.show();
        return false;
    }

}

function validateCounty(ctlCountyID, ctlPostcodeID) {

    var county;
    county = document.getElementById(ctlCountyID).value;

    if (county == 'Dublin') {
        document.getElementById(ctlPostcodeID).style.visibility = 'visible';
    }
    else {
        document.getElementById(ctlPostcodeID).style.visibility = 'hidden';
    }
}

function validateUKCounty(ctlCountyID, ctlPostcodeID) {
    var county;
    county = document.getElementById(ctlCountyID).value;
    if (county == 'Dublin') {
        document.getElementById(ctlPostcodeID).style.visibility = 'visible';
        document.getElementById(ctlPostcodeID).maxLength = 2;
        document.getElementById(ctlPostcodeID).title = '2 digit postcode';
    }
    else if ((county == 'Antrim') || (county == 'Armagh') || (county == 'Derry') || (county == 'Down') || (county == 'Fermanagh') || (county == 'Tyrone')) {
        document.getElementById(ctlPostcodeID).style.visibility = 'visible';
        document.getElementById(ctlPostcodeID).maxLength = 8;
        document.getElementById(ctlPostcodeID).title = '7 digit postcode';
    }
    else {
        document.getElementById(ctlPostcodeID).style.visibility = 'hidden';
        document.getElementById(ctlPostcodeID).value = '';
    }
}

function selectPackage(pkgID, txtControlID, cmdControlID, txtPAYGID, PAYG) {
    document.getElementById(txtControlID).value = pkgID;
    document.getElementById(txtPAYGID).value = PAYG;
    document.getElementById(cmdControlID).click();
}

function showPAYGInfo() {

    var modalPopupBehavior = $find('popPAYGDetails');
    modalPopupBehavior.show();
    return false;

}

function validatePageActCard() {
    var validPage = 1;
    var d = new Date();

    if (Page_ClientValidate() == false) { validPage = 0; }
    else { document.getElementById('lblRequiredFields').style.display = "none"; }

    document.getElementById("lblIAgree").style.display = "none";
    document.getElementById("lblInvalidFields").style.display = "none";
    document.getElementById("expDate").style.display = "none";
    document.getElementById("cardValid").style.display = "none";

    if ((document.getElementById("ctl00_MainContent_cboYear").value == d.getFullYear())
        && (document.getElementById("ctl00_MainContent_cboMonth").selectedIndex < d.getMonth())) {
        document.getElementById("lblInvalidFields").style.display = "";
        document.getElementById("expDate").style.display = "";

        document.getElementById("ctl00_MainContent_cboYear").setAttribute("className", "ReqFieldError");
        document.getElementById("ctl00_MainContent_cboMonth").setAttribute("className", "ReqFieldError");
        validPage = 0;
    }
    else {
        document.getElementById("ctl00_MainContent_cboYear").setAttribute("className", "");
        document.getElementById("ctl00_MainContent_cboMonth").setAttribute("className", "");
    }

    var number = document.getElementById("ctl00_MainContent_txtCardNo").value;
    var i, sum, weight;
    sum = 0;
    for (i = 0; i < number.length - 1; i++) {
        weight = number.substr(number.length - (i + 2), 1) * (2 - (i % 2));
        sum += ((weight < 10) ? weight : (weight - 9));
    }
    if (parseInt(number.substr(number.length - 1)) == ((10 - sum % 10) % 10)) {
        //number OK
        //validPage = 1;
    } else {
        document.getElementById("lblInvalidFields").style.display = "";
        document.getElementById("cardValid").style.display = "";
        validPage = 0;
    }

    if (document.getElementById("IAgree").checked != true) {
        document.getElementById("lblIAgree").style.display = "";
        validPage = 0;
    }

    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    if (validPage == 1) {
        modalPopupBehavior.hide();
        return true;
    } else {
        modalPopupBehavior.show();
        return false;
    }

}

function validatePage_CP() {
    var validPage;
    validPage = 1;

    if (Page_ClientValidate() == false) { validPage = 0; }
    else { document.getElementById('lblRequiredFields').style.display = "none"; }

    document.getElementById("lblInvalidFields").style.display = "none";
    document.getElementById("expDate").style.display = "none";
    document.getElementById("cardValid").style.display = "none";

    var d = new Date();
    if ((document.getElementById("ctl00_MainContent_cboCardYear").value == d.getFullYear())
        && (document.getElementById("ctl00_MainContent_cboCardMonth").selectedIndex < d.getMonth())) {
        document.getElementById("lblInvalidFields").style.display = "";
        document.getElementById("expDate").style.display = "";
        document.getElementById("ctl00_MainContent_cboCardYear").setAttribute("className", "ReqFieldError");
        document.getElementById("ctl00_MainContent_cboCardMonth").setAttribute("className", "ReqFieldError");
        validPage = 0;
    }
    else {
        document.getElementById("ctl00_MainContent_cboCardYear").setAttribute("className", "");
        document.getElementById("ctl00_MainContent_cboCardMonth").setAttribute("className", "");
    }

    var number = document.getElementById("ctl00_MainContent_txtCardNo").value;
    var i, sum, weight;
    sum = 0;
    for (i = 0; i < number.length - 1; i++) {
        weight = number.substr(number.length - (i + 2), 1) * (2 - (i % 2));
        sum += ((weight < 10) ? weight : (weight - 9));
    }
    if (parseInt(number.substr(number.length - 1)) == ((10 - sum % 10) % 10)) {
        //number OK
        //validPage = 1;
    } else {
        document.getElementById("lblInvalidFields").style.display = "";
        document.getElementById("cardValid").style.display = "";
        validPage = 0;
    }

    var modalPopupBehavior = $find('programmaticModalPopupBehavior');
    if (validPage == 1) {
        modalPopupBehavior.hide();
        return true;
    } else {
        modalPopupBehavior.show();
        return false;
    }

}

function validateExpiry1(ctlMonth, ctlYear) {
    var d = new Date();
    var modalPopupBehavior = $find('programmaticModalPopupBehavior2');
    if ((document.getElementById(ctlYear).value == d.getFullYear())
        && (document.getElementById(ctlMonth).selectedIndex < d.getMonth())) {
        document.getElementById(ctlMonth).setAttribute("className", "ReqFieldError");
        document.getElementById(ctlYear).setAttribute("className", "ReqFieldError");
        modalPopupBehavior.show();
        return false;
    } else {
        document.getElementById(ctlMonth).setAttribute("className", "");
        document.getElementById(ctlYear).setAttribute("className", "");
        modalPopupBehavior.hide();
        return true;
    }
}

function ValidateSilverlight() {
    var isSilverlightInstalled = false;
    try {
        //check on IE
        try {
            var slControl = new ActiveXObject('AgControl.AgControl');
            isSilverlightInstalled = true;
        }
        catch (e) {
            //either not installed or not IE. Check Firefox
            if (navigator.plugins["Silverlight Plug-In"]) {
                isSilverlightInstalled = true;
            }
        }
    }
    catch (e) {
    }

    if (isSilverlightInstalled) {
       showhide("ctl00_MainContent_pnlNoSL", "none");
       showhide("ctl00_MainContent_Upnl1", "");
    }
    else {
        showhide("ctl00_MainContent_pnlNoSL", "");
        showhide("ctl00_MainContent_Upnl1", "none");
    }
}

function showhide(id, displayVal) {
    if (document.getElementById(id)) {
        obj = document.getElementById(id);
        obj.style.display = displayVal;
    }
}

function showModal(msg, elem) {
    document.getElementById('ctl00_lblPageMessage').innerHTML = msg;
    var modalPopupBehavior = $find(elem);
    modalPopupBehavior.show();
    return false;
}

function enableBluray(catalogID, dvd) {

    if (document.getElementById('ctl00_MainContent_txtBlurayID')) {
        document.getElementById('ctl00_MainContent_txtBlurayID').value = catalogID;
    }
    else {
        document.getElementById('ctl00_MainContent_txtCatalogID').value = catalogID;
    }
    document.getElementById('ctl00_MainContent_txtTitle').value = dvd;

    var modalPopupBehavior = $find('programmaticModalPopupBehavior2');
    modalPopupBehavior.show();
    return false;
}

//CannotRentWarning
function cannotRent(catalogID, dvdTitle, switchType) {

    //    document.getElementById('ctl00_ContentPlaceHolder1_txtCatalogID').value = catalogID;
    //    document.getElementById('ctl00_ContentPlaceHolder1_txtTitle').value = dvdTitle;
    document.getElementById('ctl00_MainContent_txtCannotRentType').value = switchType;

    if (switchType == 1) {
        document.getElementById('CannotRentWarning').innerHTML = "Your account is currently inactive.<br /><br />Click 'Continue' to select your rental package and start renting movies today!";
    } else if (switchType == 2) {
        document.getElementById('CannotRentWarning').innerHTML = "Your account has been put on hold due to a problem with your payment method.<br /><br />Click 'Continue' for more information.";
    } else if (switchType == 3) {
        document.getElementById('CannotRentWarning').innerHTML = "You are currently signed up to a movie only package.<br /><br />Click 'Continue' to upgrade your package to include games.";
    }

    var modalPopupBehavior = $find('programmaticModalPopupBehaviorCannotRent');
    modalPopupBehavior.show();
    return false;
}


function reRentTitle(catalogID, dvdTitle) {
    if (document.getElementById('ctl00_MainContent_txtBlurayID')) {
        document.getElementById('ctl00_MainContent_txtBlurayID').value = catalogID;
    }
    else {
        document.getElementById('ctl00_MainContent_txtCatalogID').value = catalogID;
    }
    document.getElementById('ctl00_MainContent_txtTitle').value = dvdTitle;

    var modalPopupBehavior = $find('programmaticModalPopupBehavior1');
    modalPopupBehavior.show();
    return false;
}

function ValidateSurvey() {
    var elem1 = document.getElementsByName("ctl00$MainContent$radAnswers");
    var i = elem1.length
    var found_it = 0

    for (var x = 0; x <= (i - 1); x++) {
        if (elem1[x].checked) {
            found_it = 1
        }
    }

    if (found_it == 0) {
        var modalPopupBehavior = $find('programmaticModalPopupBehavior4');
        modalPopupBehavior.show();
        found_it = 0
        return false;
    }
}

