﻿
function dateSelectionChanged(sender, args) {
    var selectedDate = new Date(sender.get_selectedDate());
    var strDate = selectedDate.format("yyyy/MM/dd");
    var arrDate = strDate.split('/');

    var strUserControlClientID = getUserControlClientID(sender._id);

    ChangeSelectByValue(strUserControlClientID + "_ddlDays", arrDate[2]);
    ChangeSelectByValue(strUserControlClientID + "_ddlMonths", arrDate[1]);
    ChangeSelectByValue(strUserControlClientID + "_ddlYears", arrDate[0]);
    document.getElementById(strUserControlClientID + "_txtDate").value = strDate;
    document.getElementById(strUserControlClientID + "_txtHiddenDate").value = strDate;
    var strControlToCopyID = document.getElementById(strUserControlClientID + "_txtControlToCopyValue").value
    if (strControlToCopyID != "") {
        var xx = strControlToCopyID;
        if (strUserControlClientID.lastIndexOf('_') > -1)
            xx = strUserControlClientID.substring(0, strUserControlClientID.lastIndexOf('_')) + "_" + strControlToCopyID;

        document.getElementById(xx).value = getSelectedDate(2, strUserControlClientID);
    }
    ForceSingleValidation(strUserControlClientID);
    callValidator(strUserControlClientID);
}

function copyDate(obj) {
    var strUserControlClientID = getUserControlClientID(obj.id)
    var strDate = getSelectedDate(1, strUserControlClientID);

    document.getElementById(strUserControlClientID + "_txtDate").value = strDate;
    document.getElementById(strUserControlClientID + "_txtHiddenDate").value = strDate;
    var strControlToCopyID = document.getElementById(strUserControlClientID + "_txtControlToCopyValue").value
    if (strControlToCopyID != "") {
        var xx = strControlToCopyID;
        if (strUserControlClientID.lastIndexOf('_') > -1)
            xx = strUserControlClientID.substring(0, strUserControlClientID.lastIndexOf('_')) + "_" + strControlToCopyID;

        document.getElementById(xx).value = getSelectedDate(2, strUserControlClientID);
    } ForceSingleValidation(strUserControlClientID);
    callValidator(strUserControlClientID);
}

function callValidator(pUserControlClientID) {

    var tmpRequiredValidator = document.getElementById(pUserControlClientID + "_cvDate");
    ValidatorValidate(tmpRequiredValidator);
}

function ForceSingleValidation(pUserControlClientID) {

    var strValidatorID = document.getElementById(pUserControlClientID + "_txtValidatorID").value;
    if (strValidatorID != "") {
        var xx = strValidatorID;
        if (pUserControlClientID.lastIndexOf('_') > -1) {
            xx = pUserControlClientID.substring(0, pUserControlClientID.lastIndexOf('_')) + "_" + strValidatorID;
        }
        var tmpRequiredValidator = document.getElementById(xx);

        ValidatorValidate(tmpRequiredValidator);
    }
}

function ChangeSelectByValue(ddlID, value) {
    var ddl = document.getElementById(ddlID);
    for (var i = 0; i < ddl.options.length; i++) {
        if (ddl.options[i].value == value) {
            ddl.selectedIndex = i;
            break;
        }
    }
}

function isValidDate(s) {
    // format D(D)/M(M)/(YY)YY
    var dateFormat = /^\d{1,4}[\.|\/|-]\d{1,2}[\.|\/|-]\d{1,4}$/;
    if (dateFormat.test(s)) {
        // remove any leading zeros from date values
        s = s.replace(/0*(\d*)/gi, "$1");
        var dateArray = s.split(/[\.|\/|-]/);
        // correct month value
        dateArray[1] = dateArray[1] - 1;
        // correct year value
        if (dateArray[2].length < 4) {
            // correct year value
            dateArray[2] = (parseInt(dateArray[2]) < 50) ? 2000 + parseInt(dateArray[2]) : 1900 + parseInt(dateArray[2]);
        }
        var testDate = new Date(dateArray[2], dateArray[1], dateArray[0]);
        if (testDate.getDate() != dateArray[0] || testDate.getMonth() != dateArray[1] || testDate.getFullYear() != dateArray[2]) {
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

function validateDate(oSrc, args) {
    args.IsValid = isValidDate(getSelectedDate(1, getUserControlClientID(oSrc.id)));
}

function validateDate2(oSrc, args) {
    args.IsValid = isValidDate(getSelectedDate(1, getUserControlClientID(oSrc.id)));


}

function getUserControlClientID(pUserControlID) {
    var lastIndexOfUnderscore = pUserControlID.lastIndexOf('_');
    var strUserControlClientID = pUserControlID.substring(0, lastIndexOfUnderscore);
    return strUserControlClientID;
}

function getSelectedDate(pFormat, pUserControlClientID) {

    var ddlDay = document.getElementById(pUserControlClientID + "_ddlDays");
    var strDay = ddlDay.options[ddlDay.selectedIndex].value;

    var ddlYear = document.getElementById(pUserControlClientID + "_ddlYears");
    var strYear = ddlYear.options[ddlYear.selectedIndex].value;

    var ddlMonth = document.getElementById(pUserControlClientID + "_ddlMonths");
    var strMonth = ddlMonth.options[ddlMonth.selectedIndex].value;


    var strSelectedDate = "";
    if (pFormat == 1) // dd/MM/yyyy
        strSelectedDate = strDay + "/" + strMonth + "/" + strYear;
    else // yyyy/MM/dd
        strSelectedDate = strYear + "/" + strMonth + "/" + strDay;

    if (strSelectedDate.length == 10)
        return strSelectedDate;
    else
        return "";
}

