﻿function allowOnlyNumbers(event) {

    var key = event.which || event.keyCode;

    if (!event.shiftKey && !event.altKey && !event.ctrlKey &&
    // numbers   
                         key >= 48 && key <= 57 ||
    // Numeric keypad
                         key >= 96 && key <= 105 ||

    // Backspace and Tab and Enter
                        key == 8 || key == 9 || key == 13 ||
    // Home and End
                        key == 35 || key == 36 ||
    // left and right arrows
                        key == 37 || key == 39 ||
    // Del and Ins
                        key == 46 || key == 45) {    //do nothing
    }
    else {
        event.preventDefault(); event.stopPropagation(); return false;
    }
};

function allowOnlyDecimalNumbers(event) {

    var key = event.which || event.keyCode;

    if (!event.shiftKey && !event.altKey && !event.ctrlKey &&
    // numbers   
                         key >= 48 && key <= 57 ||
    // Numeric keypad
                         key >= 96 && key <= 105 ||
    // comma, period and minus, . on keypad
                        key == 190 || key == 188 || key == 109 || key == 110 ||
    // Backspace and Tab and Enter
                        key == 8 || key == 9 || key == 13 ||
    // Home and End
                        key == 35 || key == 36 ||
    // left and right arrows
                        key == 37 || key == 39 ||
    // Del and Ins
                        key == 46 || key == 45) {    //do nothing
    }
    else {
        event.preventDefault(); event.stopPropagation(); return false;
    }
};

function ShowModal(message) {
    $('body').append("<div id='modalPage' style='position:absolute;width:100%;height:100%;top:0px;left:0px;'>" +
                         "<div style='filter:Alpha(Opacity=50);-moz-opacity:0.5; opacity: 0.5;width:100%;height:100%;background-color:#999;position:absolute;z-index:500;top:0px;left:0px;'>" +
                         "</div>" +
                         "<div style='position:absolute;width:400px;left:50%;top:50%;z-index:750;'>" +
                             "<div style='background-color:white;border:solid 1px black;position:relative;top:-150px;left:-150px;z-index:1000;width:400px;padding:0px;'>" +
                                 "<div style='width:392px;background-color:#393939;padding:4px;color:#fff;text-align:right;'>" +
                                     "<a onclick='$(\"#modalPage\").remove();' style='cursor:pointer;'>X</a>" +
                                 "</div>" +
                                 "<div style='padding:10px;text-align:center;'>" +
                                     "<p>" + message + "</p>" +
                                 "</div>" +
                                 "<div style='padding:10px;width:100px;margin:auto;'>" +
                                     "<input type='button' value='Ok' onclick='$(\&quot;#modalPage\&quot;).remove();' style='width:100px;background-color:#393939;color:#FFF;border:1px solid #000;cursor:pointer;'>" +
                                 "</div>" +
                             "</div>" +
                         "</div>" +
                     "</div>");
}

function ShowErrorModal(message) {
    $('body').append(
        "<div id='modalPage' style='position:absolute;width:100%;height:100%;top:0px;left:0px;'>" +
            "<div style='filter:Alpha(Opacity=50);-moz-opacity:0.5; opacity: 0.5;width:100%;height:100%;background-color:white;position:absolute;z-index:500;top:0px;left:0px;'>" +
            "</div>" +
            "<div style='position:absolute;width:550px;left:33%;top:50%;z-index:750;'>" +
                "<div class='errorModal'>" +
                    "<h3>Errores de Validación</h3>" +
                    "<ul>" +
                        "<li><p>" + message + "</p></li>" +
                    "</ul>" +
                    "<input type='button' value='Ok' onclick='$(\&quot;#modalPage\&quot;).remove();' class='buttonStyle' />" +
                "</div>" +
            "</div>"
    );
}

function customDateValidation(from, to) {
    var date1str = from;
    if (date1str != "") {
        //se le pasa mes -1 porque es zero based
        var date1 = parseStringToDate(date1str);
    }
    var date2str = to;
    if (date2str != "") {
        var date2 = parseStringToDate(date2str);
    }

    if (date1str != "" && date2str != "") {
        if (date1 < date2 && date1 > new Date() && date2 > new Date()) {
            return true;
        }
        else {
            return false;
        }
    }
    else if (date1str != "" && date1 <= new Date()) {
        return false;
    }
    else if (date2str != "" && date2 <= new Date()) {
        return false;
    }
    else if (date1str == "" && date2str == "") {
        return true;
    }
    else {
        return false;
    }
}

function parseStringToDate(datestr) {
    return new Date(datestr.substring(6, 10), datestr.substring(3, 5) - 1, datestr.substring(0, 2));
}

function minDate(actualDate) {
    var actualDate = new Date(actualDate); // convert to actual date
    var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth(), actualDate.getDate() + 1); // create new increased date
    return newDate;
}

function ValidateControl(control, type, errorMessageInvalid, errorMessageRequired) {
    var isValid = true;
    var objectToValidate = control;

    if (type == 'text') {
        if (objectToValidate.val() == '') {
            objectToValidate.addClass("cellError");
            ShowErrorTip(objectToValidate, errorMessageRequired);
            isValid = false;
        }
        else { //remove class error
            objectToValidate.removeClass("cellError");
        }
    }
    else if (type == 'select') {
        if (objectToValidate.val() == 0) {
            //if default value
            objectToValidate.addClass("cellError");
            ShowErrorTip(objectToValidate, errorMessageRequired);
            isValid = false;
        }
        else { //remove class error
            objectToValidate.removeClass("cellError");
        }
    }
    else if (type = 'email') {
        var validationExpression = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]+$/;
        if (objectToValidate.val() == '') {
            objectToValidate.addClass("cellError");
            ShowErrorTip(objectToValidate, errorMessageRequired);
           isValid = false;
        }
        else if (!validationExpression.test(objectToValidate.val())) {

        objectToValidate.addClass("cellError");
        ShowErrorTip(objectToValidate, errorMessageInvalid);
            isValid = false;
        }
        else {//remove class error
            objectToValidate.removeClass("cellError");
        }
    }
    return isValid;
}

function ValidateDateControls(controlFrom, controlTo, errorMessageInvalid, errorMessageRequired) {
    var isValid = true;
    //check if empty
    if (controlFrom.val() == '') {
        controlFrom.addClass("cellError");
        ShowErrorTip(controlFrom, errorMessageRequired);
        isValid = false;
    }
    else { //remove class error
        controlFrom.removeClass("cellError");
    }
    //check if empty
    if (controlTo.val() == '') {
        controlTo.addClass("cellError");
        ShowErrorTip(controlTo, errorMessageRequired);
        isValid = false;
    }
    else { //remove class error
        controlTo.removeClass("cellError");
    }
    if (isValid) {
        //check dates are valid
        if (!customDateValidation(controlFrom.val(), controlTo.val())) {
            controlFrom.addClass("cellError");
            controlTo.addClass("cellError");
            ShowErrorTip(controlTo, errorMessageInvalid);
            isValid = false;
        }
        else { //remove class error
            controlFrom.removeClass("cellError");
            controlTo.removeClass("cellError");
        }
    }
    return isValid;
}

function ShowErrorTip(ctrl, message) {
    ctrl.qtip({
        persistent: true,
        content: message,
        focus: true,
        hidden: false,
        position: {
            corner: {
                target: 'middleRight',
                tooltip: 'middleLeft'
            }
        },
        style: {
            tip: 'leftMiddle',
            border: {
                width: 1,
                radius: 4,
                color: 'red'
            },
            fixed: true,
            background: 'red',
            color: 'white',
            'font-weight': 'bold',
            'font-size': 9,
            width: 105,
            padding: 2
        },
        show: {
            when: { target: ctrl },
            ready: true // Show the tooltip when ready
        }
});
    
}
