﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
/// <reference path="jquery.validate.js" />
/// <reference path="forms.js" /> 
//http://malsup.com/jquery/form/#api
//http://docs.jquery.com/Plugins/Validation/validate#options
var js_ModalEditContactOption;
var js_SendEmail = false;
$(document).ready(function () {
    ResizeSideBar();
    SetMinHeight();

    $(".modalDiv a.btnCancel").click(function () {
        $.unblockUI();
    });

    $(".modalDiv .closeModal").click(function () {
        $.unblockUI();
    });

    $("#input_checkAnotherSerial").keypress(function (e) {
        OnEnterF(e, CheckAnotherSerial);
    });

    $(".featuresPlus a, .featuresPremium a").click(function () {

        $.blockUI({ message: $('.purchaseOptions') })

    });
    $(".emailPOInfo").click(function () {

        $(this).closest("div").find(".emailEntry").fadeIn('fast');
    });
    $(".SFXPurchase").change(function () {
        $(".purchaseOptions div.active").removeClass("active");
        $(".purchaseOptions div.option" + parseInt($(this).val())).addClass("active");
    });
    $("#a_AssociateProduct").click(function () {
        $.blockUI({ message: $('.newProdForm') });
    });

    if ($(".rememberMe div input").is(":checked")) {
        $(".rememberMe div").addClass("on");
    }
    $(".rememberMe div").click(function () {        
        if ($(".rememberMe div input").is(":checked")) {            
            ForgetMe();
        }
        else {
            RememberMe();          
        }
    });
	$(".goBack").click(function(){goBack();});

});
function goBack()
  {
  window.history.back();
  }

function IsIphoneOrIpad() {
    return navigator.userAgent.match(/iPhone/i) != null;
}

function AssociateSerial(text) {

    $(".img_associateSerial").show();
    $.wsAjax({
        url: (urlPrefix + "services/ProductService.asmx/AssociatePrioritySupportSerial"),
        data: function () {
            return {
                Number: $.trim(text).toUpperCase(),
                SendEmail: js_SendEmail
            }
        },
        success: function (result) {
            $(".img_associateSerial").hide();
            if (result.Status == 2) {
                //ShowWrning:
                $(".AssociateWarningObjs").show();
                $("#strong_oldEmail").text(result.Email);
                $("#strong_OldSubsLast6").text(result.HtmlContent);
                
                js_SendEmail = true;
            }
            else {
                js_SendEmail = false;
                if (result.Status == 1) {
                    window.location.reload();
                }
                else
                    CustomAlert(result);
            }
        }
    });
}

function CustomAlert(result) {    

    if (result.Error != null && result.Error != "")
        alert(result.Error);
    else {
        if (result.HtmlContent != null && result.HtmlContent != "")
            alert(result.HtmlContent);
        else {
            if (result != null)
                alert(result);
        }
    }
}

function DisableLink(link) {
    var jqLink = $(link);
    jqLink.attr("disabled", "disabled");
    //jqLink.text("test");

    var clickEvent;
    if (jqLink.data("clickFunction") == null)
        clickEvent = jqLink.attr("onclick");
    else
        clickEvent = jqLink.data("clickFunction");
    jqLink.data("clickFunction", clickEvent);
    jqLink.unbind("click", clickEvent);
    jqLink.removeAttr("onclick");
    jqLink.css("cursor", "default");

    var href;
    if (jqLink.data("href") == null)
        href = jqLink.attr("href");
    else
        href = jqLink.data("href");
    jqLink.data("href", href);
    jqLink.removeAttr("href");

    return clickEvent;
}

function EnableLink(link) {
    var jqLink = $(link);
    jqLink.removeAttr("disabled");
    jqLink.css("cursor", "pointer");
    if (jqLink.data("clickFunction") != null)
        jqLink.bind("click", jqLink.data("clickFunction"));
    if (jqLink.data("href") != null)
        jqLink.attr("href", jqLink.data("href"));
}

function ShowStudioFXUpgrade(ProductName, ImgPath, ProductKey) {

    if (ProductName == "") {
    }
    else {
        $(".prodNADescription .prodName").text(ProductName);
        $("#img_prodNAInfo").attr("src", ImgPath);
        CreateOportunity(ProductName);
    }
    var url = $(".a_DownloadTrial").attr("url");
    url = url.replace("?", "/" +ProductKey + "?");
    $(".a_DownloadTrial").attr("href", url);

    $.blockUI({ message: $('.prodNAInfo'), css: { width: '800px', top: '10%', left: '11%'} })
}

function ResizeMain() {
    $(".mainCol").attr("style", "height:" + parseInt($(".secondaryCol").height()) + "px; height:auto !important; min-height:" + parseInt($(".secondaryCol").height()) + "px;");
}

function CreateOportunity(productName) {
    $.wsAjax({
        url: ("/services/UserService.asmx/CreateOportunity"),
        data: function () {
            return {
                ProductName: productName
            }
        },
        success: function (result) {
            if (result.Status == 1) {
            }
        }
    });
}

function ForgetMe() {
    $.wsAjax({
        url: ("/services/UserService.asmx/ForgetMe"),
        data: function () {
            return {}
        },
        success: function (result) {
            $(".rememberMe div").removeClass("on");
            $(".rememberMe div input").removeAttr('checked');      
        }
    });
}

function RememberMe() {
    $.wsAjax({
        url: ("/services/UserService.asmx/RememberMe"),
        data: function () {
            return {}
        },
        success: function (result) {
            $(".rememberMe div").addClass("on");
            $(".rememberMe div input").attr('checked', 'checked');   
        }
    });
}

function SetEditContactValues(option) {
    js_ModalEditContactOption = option;



    switch (option) {
        case 1:
            $(".userEditAdministrator").show();
            $(".userEditContact").hide();
            $("#input_userEditEmail").val($("#a_AdmintEmail").text());
            $("#input_userEditFirstName").val($("#p_AdminFirstName").text());
            $("#input_userEditLastName").val($("#p_AdminLastName").text());
            break;
        case 2:
            $(".userEditContact").show();
            $(".userEditAdministrator").hide();
            $("#input_userEditEmail").val($("#a_TechContactEmail").text());
            $("#input_userEditFirstName").val($("#p_TechContactFirstName").text());
            $("#input_userEditLastName").val($("#p_TechContactLastName").text());
            break;
        case 3:
            $(".userEditContact").show();
            $(".userEditAdministrator").hide();
            $("#input_userEditEmail").val($("#a_TechSerialContactEmail").text());
            $("#input_userEditFirstName").val($("#p_TechSerialContactFirstName").text());
            $("#input_userEditLastName").val($("#p_TechSerialContactLastName").text());
            break;
    }
    $.blockUI({ message: $('#div_ModalUserEdit') })
}

function userEditChangeContact() {
    var firstName = $("#input_userEditFirstName").val();
    var lastName = $("#input_userEditLastName").val();
    var email = $("#input_userEditEmail").val();

    $("#img_userEditModal").show();
    $.wsAjax({
        url: ("/services/UserService.asmx/ChangeContact"),
        data: function () {
            return {
                Option: js_ModalEditContactOption,
                Email: email,
                FirstName: firstName,
                LastName: lastName
            }
        },
        success: function (result) {
            $("#img_userEditModal").hide();
            if (result.Status == 1) {
                //                switch (js_ModalEditContactOption) {
                //                    case 1:
                //                        $("#p_AdminFullName").text(result.FullName);
                //                        $("#a_AdmintEmail").text(result.Email);
                //                        $("#a_AdmintEmail").attr("href", "mailto:" + result.Email);
                //                        break;
                //                    case 2:
                //                        $("#p_ContactFullName").text(result.FullName);
                //                        $("#a_TechContactEmail").text(result.Email);
                //                        $("#a_TechContactEmail").attr("href", "mailto:" + result.Email);
                //                        break;
                //                    case 3:
                //                        $("#p_SerialContactFullName").text(result.FullName);
                //                        $("#a_TechSerialContactEmail").text(result.Email);
                //                        $("#a_TechSerialContactEmail").attr("href", "mailto:" + result.Email);
                //                        break;
                //                }
                //                $.unblockUI();
                window.location.reload();
            }
            else
                CustomAlert(result);
        }
    });
}

function ResizeSideBar() {
    if ($("div.secondaryCol").height() > $("div.mainCol").height()) {
        $(".mainCol").attr('style', 'min-height:' + parseInt($(".secondaryCol").height()) + 'px; height:auto!important; height:' + parseInt($(".secondaryCol").height()) + 'px');
    }
}

function SetMinHeight() {
    var height = $("#SubscriptionSideBar").height();
    if (height < 400)
        height = 400;
    $("#content").css("min-height", height + "px");
}

function OnEnter(e, buttonJQueryID) {
    var keycode = null;
    if (!e) e = window.event
    if (e.keyCode) keycode = e.keyCode;
    else if (e.which) keycode = e.which;
    if (keycode == 13) {
        if (e.cancelBubble != null)
            e.cancelBubble = true;
        if (e.stopPropagation)
            e.stopPropagation();
        if (e.preventDefault)
            e.preventDefault();
        if (window.event)
            e.returnValue = false;
        if (e.cancel != null)
            e.cancel = true;
        e.returnValue = false;
        e.cancel = true;
        $(buttonJQueryID).click();
    }
}
function OnEnterF(e, f) {
    var keycode = null;
    if (!e) e = window.event
    if (e.keyCode) keycode = e.keyCode;
    else if (e.which) keycode = e.which;
    if (keycode == 13) {
        if (e.cancelBubble != null)
            e.cancelBubble = true;
        if (e.stopPropagation)
            e.stopPropagation();
        if (e.preventDefault)
            e.preventDefault();
        if (window.event)
            e.returnValue = false;
        if (e.cancel != null)
            e.cancel = true;
        e.returnValue = false;
        e.cancel = true;
        f();
    }
}

function Master_SignOut(GoToLogin) {
    $("#imgLoading_AccountLogOut").show();
    $.wsAjax({
        url: ("/services/UserService.asmx/Logout"),
        data: function () { return {} },
        success: function () {
            $("#imgLoading_AccountLogOut").hide();
            if (GoToLogin)
                $.goTo("/login.aspx");
            else
                window.location.reload(true);
        } 
    });
}
function FixBodyWidth() {
    $("body").width($("body").width());
    $("body").width("100%");
}

function CreateThemedRecaptcha(id) {
    Recaptcha.create("6LfhoggAAAAAAOstDudMt0SawJtnvb6Hg5wL6qdU",
        id, {
            theme: "clean"
        });
}
