﻿function showAjaxMessage(ajaxLoaderDiv, ajaxMessage) {
    var ajaxLoader = "<img src='" + loadingImageSrc + "' alt=''>";
    $(ajaxLoaderDiv).html("<p class='ajaxworkingmessage'>" + ajaxLoader + ajaxMessage + "</p>");
}

function getPartialView(actionUrl, targetDiv, ajaxLoaderDiv, ajaxMessage, callback) {
    $(targetDiv).slideDown("slow");
    showAjaxMessage(ajaxLoaderDiv, ajaxMessage);
    $.get(actionUrl, null, function (result) {
        $(targetDiv).html(result);
        $(ajaxLoaderDiv).empty();
        callback();
    });
}

function submitForm(actionUrl, targetDiv, ajaxLoaderDiv, ajaxMessage, form, callback) {
    var data = $(form).serialize();
    showAjaxMessage(ajaxLoaderDiv, ajaxMessage);
    $.post(
        actionUrl,
        data,
        function (data) {
            $(targetDiv).html(data);
            $(ajaxLoaderDiv).empty();
            callback();
        }
    );
}

function simpleSubmitForm(actionUrl, targetDiv, workingMessage, formId, doAfter) {
    var options = {
        url: actionUrl,
        target: targetDiv,
        success: doAfter,
        beforeSubmit: function () {
            showAjaxMessage(targetDiv, workingMessage);
        }
    };
    $(formId).ajaxSubmit(options);
    return false;
}

function searchEvents(targetId, initString, searchButtonId) {
    $(searchButtonId).click(function () {
        var keyword = $(targetId).val();
        if ($.trim(keyword).length < 2) {
            alert("Please enter at least 2 characters.");
            $(targetId).focus();
            return false;
        }
        // filter the excluded words
        keyword = keyword.toLowerCase().replace(/the/g, "").replace(/and/g, "").replace(/an/g, "");
        // submit and get the data.
        $(this).parent().submit();
    });
    $(targetId).focus(function () {
        if ($(this).val() == initString) {
            $(this).val("");
        }
    });
    $(targetId).blur(function () {
        if ($(this).val() == "") {
            $(this).val(initString);
        }
    });
}

function openNewWindow(URL, winWidth, winHeight, popUpWin, windowName, leftPos, topPos) {
    // Default window width and height, if not passed in.
    var w = 800, h = 600;

    //Check browser is IE.
    if (document.all) {
        w = document.body.clientWidth;
        h = document.body.clientHeight;
    }
    else {
        w = window.innerWidth;
        h = window.innerHeight;
    }

    //Check if window width was passed. If so, use that width.
    if (winWidth != null && winWidth != "") {
        var popW = winWidth;
    }
    else {
        var popW = Math.abs(w - 100);
    }

    //Check if window height was passed. If so, use that height.
    if (winHeight != null && winHeight != "") {
        var popH = winHeight;
    }
    else {
        var popH = Math.abs(h - 100);
    }

    //Check if window should be opened as popup.
    if (popUpWin != null) {
        var noPopUp = 'yes';
        if (popUpWin.toLowerCase() == 'y' || popUpWin.toLowerCase() == 'yes') {
            noPopUp = 'no';
        }
    }
    else {
        var noPopUp = 'yes';
    }

    // Create the window name, if not passed in.
    if (windowName != null) {
        // Use the window name passed in.
        var winName = windowName;
    } else {
        //Generate window name with random number (ie 'win274').
        var winName = 'win' + Math.floor(Math.random() * 1000);
    }

    // Set the window positions.
    if ((leftPos == null) || (leftPos == "undefined")) {
        leftPos = Math.abs(w - popW) / 2;
    }
    if ((topPos == null) || (topPos == "undefined")) {
        topPos = Math.abs(h - popH) / 2;
    }

    // Open the new window.
    newWindow = window.open(URL, winName, 'width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ',menubar=' + noPopUp + ',location=' + noPopUp + ',directories=' + noPopUp + ',fullscreen=no,resizable=' + noPopUp + ',scrollbars=yes,status=no,titlebar=yes,toolbar=' + noPopUp);

    return newWindow;
}

function writeLVReview(writeReviewUrl) {
    openNewWindow(writeReviewUrl, 625, 650, 'Y', 'WriteAReview');
}

function removeAutoComplete(controlName) {
    $("#" + controlName + "").attr("autoComplete", "off");
}


function isValidCreditCard(type, ccnum)
{
    var re = /\\/; //new RegExp();
        switch (type) 
        {
            case 'AX': re =/^[34|37][0-9]{14}$/;
                break;
            case 'MC': re =/^[5][1-5][0-9]{14}$/;
                break;
            case 'VI': re =/^[4]([0-9]{15}$|[0-9]{12}$)/;
                break;
            case 'DI': re =  /^6011-?d{4}-?d{4}-?d{4}$/;
                break;
        }
   if (re.length <= 0 || ccnum.length<=0)
       return false;
   if (!re.test(ccnum))
       return false;
    ccnum = ccnum.split("-").join("");
    var checksum = 0;
    for (j = 0; j < ccnum.length; j++) {
        positionNum = parseInt(ccnum.charAt(ccnum.length - 1 - j));
        checksum = checksum + ((positionNum * (j % 2 + 1) - 1) % 9 + 1);
    }
    if ((checksum % 10) == 0)
    {
        return true;
    }
    else
        return false;
}

function ValidCreditCardType(ccnum) {
    var Typeresult = "N";
    if (ccnum.length <= 0)
        return Typeresult;
    ccnum = ccnum.split("-").join("");
    var checksum = 0;
    for (j = 0; j < ccnum.length; j++) {
        positionNum = parseInt(ccnum.charAt(ccnum.length - 1 - j));
        checksum = checksum + ((positionNum * (j % 2 + 1) - 1) % 9 + 1);
    }
    if ((checksum % 10) == 0) {
        Typeresult = "Y";
    }
    else
        return Typeresult;
    if (Typeresult == "Y") {
        var AX = /^[34|37][0-9]{14}$/;

        var MC = /^[5][1-5][0-9]{14}$/;

        var VI = /^[4]([0-9]{15}$|[0-9]{12}$)/;

        var DI = /^6011-?d{4}-?d{4}-?d{4}$/;
        if (AX.test(ccnum))
            Typeresult = "AX";
        else if (MC.test(ccnum))
            Typeresult = "MC";
        else if (VI.test(ccnum))
            Typeresult = "VI";
        else if (ccnum.startsWith('6011') && ccnum.length == 16)
            Typeresult = "DI";
        else
            Typeresult = "N";
    }
    return Typeresult;
}

function Hash() {
    this.length = 0;
    this.items = new Array();
    for (var i = 0; i < arguments.length; i += 2) {
        if (typeof (arguments[i + 1]) != 'undefined') {
            this.items[arguments[i]] = arguments[i + 1];
            this.length++;
        }
    }

    this.removeItem = function (in_key) {
        var tmp_previous;
        if (typeof (this.items[in_key]) != 'undefined') {
            this.length--;
            var tmp_previous = this.items[in_key];
            delete this.items[in_key];
        }

        return tmp_previous;
    }

    this.getItem = function (in_key) {
        return this.items[in_key];
    }

    this.setItem = function (in_key, in_value) {
        var tmp_previous;
        if (typeof (in_value) != 'undefined') {
            if (typeof (this.items[in_key]) == 'undefined') {
                this.length++;
            }
            else {
                tmp_previous = this.items[in_key];
            }

            this.items[in_key] = in_value;
        }

        return tmp_previous;
    }

    this.hasItem = function (in_key) {
        return typeof (this.items[in_key]) != 'undefined';
    }

    this.clear = function () {
        for (var i in this.items) {
            delete this.items[i];
        }

        this.length = 0;
    }
}

var pixamiDialogOpts = {
    modal: true,
    bgiframe: true,
    autoOpen: false,
    height: 550,
    width: 950,
    zIndex:1100,
    draggable: true,
    resizeable: true
};

var initiatePzTransactionUrl = '';
var pzServerUrl = '';

function pzProductClickFromProduct() {
    var productId = $(this).attr("productid");
//    var pzTransactionId = $("#PzDialog" + productId).attr("pzTransaction");
    var qty = $("#" + productId + "quantityRountBox").val();

    $("#PzDialog" + productId).dialog({
        open: function()  {
            $.ajax({
                type: "Get",
                url: initiatePzTransactionUrl,
                dataType: "json",
                data: "id=" + productId,
                success: function (data) {
                    if(data==null)
                        alert("Call Personalization Failed.");
                    else if(data.length>0)
                    {
                        var url = pzServerUrl + data + "&qty=" + qty + "&frompage=product";
                        
                        $("#PixamiFrame" + productId).attr("src", url);

                        //$("#PzDialog" + productId).attr("pzTransaction", data);
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.statusText);
                }
            });
        }
    });
    $("#PzDialog" + productId).dialog(pixamiDialogOpts);
    $("#PzDialog" + productId).dialog("open");
    $("#PzDialog" + productId).dialog('option', 'position', 'center');

    return false;
}

var familyProductDetailsDialogOpts = {
    modal: false,
    bgiframe: true,
    autoOpen: false,
    height: 200,
    width: 500,
    draggable: true,
    resizeable: true,
    zIndex:1100
};


