function setFocus(id) {
    if (id != "") {
        document.getElementById(id).focus();
    }
}

function Redirect(url) {
    if (url != "") {
        window.location = url
    }
}

function Back() {
    window.history.back()
}

function PopUp(url) {
    if (url != "") {
        window.open(url, "_blank")
    }
}

function PopUpWindow(url, specs) {
    if (url != "") {
        window.open(url, "_blank", specs)
    }
}

function noChar(Element) {
    Element.value = Element.value.replace(/\D/g, '')
}

function addSeparator(value) // temp
{
    var i
    for (i = Math.floor(value.length / 3); i > 0; i--) {
        value = value.substring(0, value.length - i * 3) + "," + value.substring(value.length - i * 3, value.length)
    }
    if (value.indexOf(",") == 0) {
        value = value.substring(1)
    }
    return value
}

function ParseDate(id) {
    var year, month, day, date;

    year = parseInt(document.getElementById(id + "_dpYear").value, 10);
    month = parseInt(document.getElementById(id + "_dpMonth").value, 10);
    day = parseInt(document.getElementById(id + "_dpDay").value, 10);

    if (!isNaN(year) && !isNaN(month) && !isNaN(day)) {
        date = new Date(year, month - 1, day);
        if (date.getMonth() + 1 != month) {
            for (var i = 1; date.getMonth() + 1 != month; i++) {
                date = new Date(year, month - 1, day - i);
            }
            document.getElementById(id + "_dpYear").value = (date.getYear() < 100) ? "19" + date.getYear() : date.getYear();
            document.getElementById(id + "_dpMonth").value = (date.getMonth() + 1 < 10) ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);
            document.getElementById(id + "_dpDay").value = (date.getDate() < 10) ? "0" + date.getDate() : date.getDate();
        }
    }
}

function ParseDateAndRange(id1, id2, value) {
    ParseDate(id1)
    ParseDate(id2)

    var year1, month1, day1, year2, month2, day2, date1, date2;
    year1 = parseInt(document.getElementById(id1 + "_dpYear").value, 10);
    month1 = parseInt(document.getElementById(id1 + "_dpMonth").value, 10);
    day1 = parseInt(document.getElementById(id1 + "_dpDay").value, 10);
    year2 = parseInt(document.getElementById(id2 + "_dpYear").value, 10);
    month2 = parseInt(document.getElementById(id2 + "_dpMonth").value, 10);
    day2 = parseInt(document.getElementById(id2 + "_dpDay").value, 10);

    if (!isNaN(year1) && !isNaN(month1) && !isNaN(day1) && !isNaN(year2) && !isNaN(month2) && !isNaN(day2)) {
        date1 = new Date(year1, month1 - 1, day1 + value);
        date2 = new Date(year2, month2 - 1, day2);
        if (date2 < date1) {
            document.getElementById(id2 + "_dpYear").value = (date1.getYear() < 100) ? "19" + date1.getYear() : date1.getYear();
            document.getElementById(id2 + "_dpMonth").value = (date1.getMonth() + 1 < 10) ? "0" + (date1.getMonth() + 1) : (date1.getMonth() + 1);
            document.getElementById(id2 + "_dpDay").value = (date1.getDate() < 10) ? "0" + date1.getDate() : date1.getDate();
        }
    }
}

function CopyDate(id1, id2) {
    ParseDate(id1)

    var year1, month1, day1, year2, month2, day2, date1, date2;
    year1 = parseInt(document.getElementById(id1 + "_dpYear").value, 10);
    month1 = parseInt(document.getElementById(id1 + "_dpMonth").value, 10);
    day1 = parseInt(document.getElementById(id1 + "_dpDay").value, 10);

    if (!isNaN(year1) && !isNaN(month1) && !isNaN(day1)) {
        document.getElementById(id2 + "_dpYear").value = document.getElementById(id1 + "_dpYear").value;
        document.getElementById(id2 + "_dpMonth").value = document.getElementById(id1 + "_dpMonth").value;
        document.getElementById(id2 + "_dpDay").value = document.getElementById(id1 + "_dpDay").value;
    }
}

function ParseDateString(id) {
    if (document.getElementById(id).value.indexOf("/") != -1)
        var separator = "/"
    else if (document.getElementById(id).value.indexOf("-") != -1)
        var separator = "-"
    else if (document.getElementById(id).value.indexOf(".") != -1)
        var separator = "."

    var day, month, year, date;
    day = parseInt(document.getElementById(id).value.split(separator)[0], 10);
    month = parseInt(document.getElementById(id).value.split(separator)[1], 10);
    year = parseInt(document.getElementById(id).value.split(separator)[2], 10);

    if (year >= 0 && year < 100)
        year += 2000;
    if (month > 12)
        month = 12
    if (!isNaN(year) && !isNaN(month) && !isNaN(day) && year >= 1900 && year <= 2099 && month > 0 && day > 0) {
        date = new Date(year, month - 1, day);
        if (date.getMonth() + 1 != month) {
            for (var i = 1; date.getMonth() + 1 != month; i++)
                date = new Date(year, month - 1, day - i);
        }
        document.getElementById(id).value = ((date.getDate() < 10) ? "0" + date.getDate() : date.getDate()) + "/" + ((date.getMonth() + 1 < 10) ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1)) + "/" + ((date.getYear() < 100) ? "19" + date.getYear() : date.getYear());
    }
    else
        document.getElementById(id).value = ""
}

function setDeleteDate(id1, id2) {
    ParseDateString(id1)
    ParseDateString(id2)
    if (document.getElementById(id1).value != "" && (document.getElementById(id2).value == "" || id1 == 'txtLastTicketDate')) {
        var day, month, year, date;
        day = parseInt(document.getElementById(id1).value.split("/")[0], 10);
        month = parseInt(document.getElementById(id1).value.split("/")[1], 10);
        year = parseInt(document.getElementById(id1).value.split("/")[2], 10);
        date = new Date(year, month - 1, day + 1);
        document.getElementById(id2).value = ((date.getDate() < 10) ? "0" + date.getDate() : date.getDate()) + "/" + ((date.getMonth() + 1 < 10) ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1)) + "/" + ((date.getYear() < 100) ? "19" + date.getYear() : date.getYear());
    }
}

function AutoTick(id1, id2) {
    if (document.getElementById(id1).value != "") {
        document.getElementById(id2).checked = true
    }
}

function AutoClear(id1, id2) {
    if (document.getElementById(id1).checked == false) {
        document.getElementById(id2).value = ""
    }
}

