﻿$.ajaxSetup({
    error: function (request, status, error) {
        showErrorAlert(request.responseText);
    }
});

$(function () {
    $("#btnSearch").click(function () {
        var search = $("#Search").val();
        location.href = "/Search?cx=002749151403721809707%3Ajdseziqcwki&cof=FORID%3A9&q=" + escape(search);
    });

    $("#Search").keypress(function (event) {
        if (event.which == '13') {
            var search = $("#Search").val();
            location.href = "/Search?cx=002749151403721809707%3Ajdseziqcwki&cof=FORID%3A9&q=" + escape(search);
        }
    });
});

// Ajax Call
function Ajax(obj) {
    obj.type = "POST";
    obj.dataType = "json";
    obj.contentType = "application/json; charset=utf-8";
    obj.cache = false;
    obj.timeout = 60000;
    $.ajax(obj);
}

// Error Alert
var isErrorLoaded = false;
function showErrorAlert(msg) {
    if (!isErrorLoaded) {
        $('<div id="erroralert"></div>').html(msg).appendTo("body").dblclick(function () {
            hideModal($("#erroralert"));
        });
    }
    isErrorLoaded = true;
    showModal($("#erroralert"));
    return false;
}

// Modal Overlay
function showModal(jqObj) {
    $("<div id='modalbg' class='modalbg'></div>").appendTo("body").css("z-index", "100");
    jqObj.css({ "position": "absolute", "z-index": "101" }).show();
    changeModal(jqObj);
    $(window).resize(function () {
        changeModal(jqObj);
    });
    $(window).scroll(function () {
        changeModal(jqObj);
    });
}
function hideModal(jqObj) {
    jqObj.hide();
    $("#modalbg").remove();
}
function changeModal(jqObj) {
    resizeModalBg();
    resizeModal(jqObj);
}
function resizeModalBg() {
    var bg = $("#modalbg");
    var documentwidth = $(document).width();
    var documentheight = $(document).height();
    bg.width(documentwidth);
    bg.height(documentheight);
}
function resizeModal(jqObj) {
    var screenwidth = $(window).width();
    var screenheight = $(window).height();
    var scrolltop = $(window).scrollTop();
    var jqObjwidth = $(jqObj).width();
    var jqObjheight = $(jqObj).height();
    var lefter = ((screenwidth - jqObjwidth) / 2);
    var upper = ((screenheight - jqObjheight) / 2) + scrolltop;
    jqObj.css({ "top": upper + "px", "left": lefter + "px" });
}

// Show Validation Errors
function ShowValidationErrors(formOutput, ids) {

    $("span.error").remove();
    $(".errorinput").removeClass("errorinput");

    for (var i in formOutput.InvalidFields) {
        var invalidfield = formOutput.InvalidFields[i];
        DisplayValidationError(formOutput, invalidfield.Name, invalidfield.Code, ids);
    }

}

function DisplayValidationError(formOutput, name, code, ids) {

    var msg = "";
    if (code == 1) {
        msg = "invalid input";
    } else if (code == 4) {
        msg = "already taken";
    } else if (code == 5) {
        msg = "invalid password";
    } else if (code == 6) {
        msg = "does not match";
    } else if (code == 7) {
        msg = "master already chosen";
    } else if (code == 7) {
        msg = "master cannot inherit";
    } else if (code == 19) {
        msg = "email not confirmed";
    } else if (code == 20) {
        msg = "email not registered";
    } else if (code == 21) {
        msg = "email already registered";
    }else if (code == 22) {
        msg = "locked out for " + formOutput.LockWaitMinutes + " minutes";
    } else if (code == 23) {
        msg = "invalid password. " + formOutput.AttemptsLeft + " attempts left.";
    } else if (code == 24) {
        msg = "email not active";
    } else if (code == 25) {
        msg = "date must expire in the future";
    } else if (code == 26) {
        msg = "invalid captcha";
    } else if (code == 27) {
        msg = "maximum about is $2,500. Please donate via check at the center for larger donations.";
    } else if (code == 28) {
        msg = "email already confirmed";
    } else {
        alert("error code " + code);
    }

    if (ids) {
        if (ids[name]) {
            name = ids[name];
        }
    }

    var espan = $('<span class="error">' + msg + '</span>');

    $("#" + name).focus();
    $("#" + name).after(espan);

    espan.show().css({
        opacity: 0,
        "margin-left": 50 + 'px'
    }).animate({
        "margin-left": 2 + 'px',
        opacity: 1
    }, 'slow');
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
