﻿function AjaxFailed(result) {
    alert(result.status + ' ' + result.statusText);
}

function AddComma(MyString) {
    var objRegex = new RegExp('(-?[0-9]+)([0-9]{3})');
    while (objRegex.test(MyString)) {
        MyString = MyString.replace(objRegex, '$1,$2');
    }
    return MyString;
}

isDate = function (y, m, d) {
    if (typeof y == "string" && m instanceof RegExp && d) {
        if (!m.test(y)) return 1;
        y = RegExp["$" + d.y], m = RegExp["$" + d.m], d = RegExp["$" + d.d];
    }
    d = Math.abs(d) || 0, m = Math.abs(m) || 0, y = Math.abs(y) || 0;
    return arguments.length != 3 ? 1 : d < 1 || d > 31 ? 2 : m < 1 || m > 12 ? 3 : /4|6|9|11/.test(m) && d == 31 ? 4
        : m == 2 && (d > ((y = !(y % 4) && (y % 1e2) || !(y % 4e2)) ? 29 : 28)) ? 5 + !!y : 0;
};

function getMessage(r) {
    return r == 0 ? "Valid date"
    : r == 1 ? "Invalid date format"
    : r == 2 ? "Invalid day"
    : r == 3 ? "Invalid month"
    : r == 4 ? "In April, June, September and November the month has only 30 days"
    : r == 5 ? "February has only 28 days"
    : r == 6 && "It's leap year, February has only 29 days";
};

function verifyEmail(ea1, ea2) {
    var status = false;
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if (ea1.search(emailRegEx) == -1) {
        alert("Please enter a valid email address.");
    }
    else if (ea1 != ea2) {
        alert("Email addresses do not match.  Please retype them to make sure they are the same.");
    }
    else { status = true; }

    return status;
}

function ShowCalendar(x) {
    if (x * 1 == 1) {
        if ($("#txtDepart").val() == "") {
            var today = new Date();
            var todayPlus2 = new Date();
            todayPlus2.setFullYear(today.getFullYear(), today.getMonth(), today.getDate());
            todayPlus2.setDate(todayPlus2.getDate() + 2);

            var dd = todayPlus2.getDate();
            var mm = todayPlus2.getMonth() * 1 + 1;

            if (dd < 10) { dd = "0" + dd }
            if (mm < 10) { mm = "0" + mm }

            document.getElementById("txtDepart").value = todayPlus2.getFullYear() + "/" + mm + "/" + dd;
        }

        displayCalendar(document.getElementById("txtDepart"), document.getElementById("txtReturn"), 1, 'yyyy/mm/dd', document.getElementById("imgDepartCal"));
    }
    else if (x * 1 == 2) {
        displayCalendar(document.getElementById("txtReturn"), document.getElementById("txtDepart"), 2, 'yyyy/mm/dd', document.getElementById("imgReturnCal"));
    }
}

function ForceNumbersOnly(myfield, e, dec) {
    var key;
    var keychar;

    if (window.event) {
        key = window.event.keyCode;
    }
    else if (e) {
        key = e.which;
    }
    else {
        return true;
    }
    if (key != 46 && key != 45 && key > 31 && (key < 48 || key > 57)) {
        return false;
    }
    else {
        return true;
    }
}

function RemoveNonNumeric(myfield) {
    var re = /[^0-9\.\-]/g;
    if (re.test(myfield.value)) {
        myfield.value = myfield.value.replace(re, '');
        myfield.value = myfield.value.replace(/\./, '');
    }
}

function tabOnEnter(a, b, c, evt) {
    if (evt.keyCode === 13) {
        if (evt.preventDefault) {
            evt.preventDefault();
        } else if (evt.stopPropagation) {
            evt.stopPropagation();
        } else {
            evt.returnValue = false;
        }

        if (a * 1 == 1)
        { document.getElementById("txtLast" + b + "_" + c).focus(); }
        else if (a * 1 == 2)
        { document.getElementById("img" + b + "_" + c).click(); }

        return false;
    } else {
        return true;
    }
}

