// JScript File
// Included by Kumuthini. L on 02-Feb-2010



// Function to enable / disable the controls when viewing & closing the Scheduling Reports Settings
function ShowHideScheduleReports(isShow, canClearFields) {
    var isIE = CheckBrowser();
//    if (document.getElementById('ddlReportOptions') != null) {
//        if (document.getElementById('ddlReportOptions').value == "26") {
//            CheckWeeklySchedule(true);
//        }
//        else {
//            CheckWeeklySchedule(false);
//        }
//    }
    if (document.getElementById('trSchedulingReports') != null) {
        document.getElementById('trSchedulingReports').style.display = (isShow) ? 'block' : 'none'; //(isIE ? "block" : "initial") : 'none';
    }
    
    if (document.getElementById('trMarker') != null) {
        document.getElementById('trMarker').style.display = (isShow) ? (isIE ? "block" : "initial") : 'none';
    }

    if (document.getElementById('trSaveOperation') != null) {
        document.getElementById('trSaveOperation').style.display = (isShow) ? (isIE ? "block" : "initial") : 'none';
    }

    ShowHideButtons(isShow);
   
    if (document.getElementById('trDurationSelection') != null) {
        document.getElementById('trDurationSelection').style.display = (isShow) ? 'block' : 'none'; //(isIE ? "block" : "initial");
        document.getElementById('trDurationSelection').value = "";
    }
    

    document.getElementById('hdnIsScheduleReports').value = (isShow) ? 'Y' : 'N';

//    if (!isShow) {
//        if (document.getElementById('txtEffectiveFrom') != null) {
//            document.getElementById('txtEffectiveFrom').disabled = false;
//        }
//    }

    FrequencyChange();

    ShowHideEffTillText("False");
   
    if (canClearFields) {
        // Function Call to clear the values in the controls in Scheduling Reports Settings
        ClearScheduleReportsControls();
    }
}

// Function to show / hide duration
function ShowHideReportDuration(isShow) {
    var isIE = CheckBrowser();
    if (document.getElementById('trDurationSelection') != null) {
        document.getElementById('trDurationSelection').style.display = (isShow) ? 'block' : 'none'; //(isIE ? "block" : "initial")
    }
}

function DisableControlsForEditing(isEdit){
    document.getElementById('tblReportFilters').disabled = isEdit;
    if (document.getElementById('tdFrequencyInfor') != null) {
        document.getElementById('tdFrequencyInfor').disabled = isEdit;
    }
}

// Function to clear the values in the controls in Scheduling Reports Settings
function ClearScheduleReportsControls() {
    if (document.getElementById('txtEffectiveFrom') != null) {
        document.getElementById('txtEffectiveFrom').value = "";
    }
    if (document.getElementById('txtEffectiveTo') != null) {
        document.getElementById('txtEffectiveTo').value = "";
    }
    if (document.getElementById('txtEffectiveFrom') != null) {
        document.getElementById('txtEffectiveFrom').disabled = false;
    }
    if (document.getElementById("chkNoEndDate") != null) {
        document.getElementById("chkNoEndDate").checked == false;
    }
    if (document.getElementById('ddlFrequency') != null) {
        document.getElementById('ddlFrequency').value == '0';
        FrequencyChange();
    }

    ShowHideEffTillText("False");
}

// Function to Show / Hide the Effective Till Text Box based on the selected radio button 
function ShowHideEffTillText(isView) {
    if (document.getElementById("txtEffectiveTo") != null) {
        if (isView == "False") {
            if (document.getElementById("chkNoEndDate") != null && document.getElementById("chkNoEndDate").checked == true) {
                document.getElementById("txtEffectiveTo").disabled = true;
            }
            else {
                document.getElementById("txtEffectiveTo").disabled = false;
            }
        }
        else {
            document.getElementById("txtEffectiveTo").disabled = true;
        }
    }
}
/****************************************Check Frequency weekly days***********************************/


var weekDayNo = new Array(1, 2, 3, 4, 5, 6, 7);
var weekDayText = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
function LoadDayWeekRanges(ddlToBePopulated, ddlDayExclude, ddlWeeklyFrom, ddlWeeklyTo, ddlReportOn) {
    var dayToBeExcluded = 0;
    var dropDownControl = document.getElementById(ddlToBePopulated);
    var dropDowntoBeExcluded = document.getElementById(ddlDayExclude);
    if (dropDowntoBeExcluded != null) {
        dayToBeExcluded = eval(dropDowntoBeExcluded.value);
    }
    var selectedOption = document.getElementById(ddlToBePopulated).value;
    var selectedWeekDayTo = document.getElementById(ddlWeeklyFrom).value;
    var selectedGenerateOn = document.getElementById(ddlReportOn).value;
    if ((selectedWeekDayTo != '') && (selectedOption != '') && (selectedGenerateOn != '')) {
        document.getElementById('hdnWeekDays').value = selectedWeekDayTo + ',' + selectedOption + ',' + selectedGenerateOn;
    }
    dropDownControl.options.length = 0;
    var option;
    var weekDay = new Array();
    weekDay = String(document.getElementById('hdnWeekDay').value).split(',');
    if (weekDay.length >= 3) {
        if (dropDowntoBeExcluded == null) {
            selectedOption = weekDay[0];
        }
        else {
            selectedOption = weekDay[1];
        }
    }
    for (var i = 0; i < weekDayNo.length; i++) {
        if (dayToBeExcluded != weekDayNo[i]) {
            option = document.createElement("OPTION");
            option.innerHTML = weekDayText[i];
            option.value = weekDayNo[i];
            if (selectedOption != null) {
                if (eval(selectedOption) == weekDayNo[i]) {
                    option.selected = true;
                }
            }
            dropDownControl.appendChild(option);
        }
    }

    if (dropDowntoBeExcluded == null) {
        LoadDayWeekRanges(ddlWeeklyTo, ddlToBePopulated, ddlWeeklyFrom, ddlWeeklyTo, ddlReportOn);
    }
    LoadGenerateOn(ddlWeeklyFrom, ddlWeeklyTo, ddlReportOn, selectedGenerateOn);
}
function ChangeReportOn() {
    var weekDay = new Array();
    weekDay = String(document.getElementById('hdnWeekDay').value).split(',');
    if (weekDay.length >= 3) {
        weekDay[2] = document.getElementById('ddlWeekDayOn').value;
        document.getElementById('hdnWeekDay').value = weekDay.join(",");
    }
}

function LoadGenerateOn(ddlWeekStart, ddlWeekEnd, ddlReportOn) {
    var startDay = eval(document.getElementById(ddlWeekStart).value);
    var endDay = eval(document.getElementById(ddlWeekEnd).value);
    var dropDownControl = document.getElementById(ddlReportOn);
    weekDay = String(document.getElementById('hdnWeekDays').value).split(',');
    if ((startDay != '') && (endDay != '') && (dropDownControl.value != '')) {
        document.getElementById('hdnWeekDays').value = startDay + ',' + endDay + ',' + dropDownControl.value;
            document.getElementById('hdnWeekDay').value = document.getElementById('hdnWeekDays').value;
       
    }
    var weekDay = new Array();
    weekDay = String(document.getElementById('hdnWeekDay').value).split(',');

    var selectedOption = dropDownControl.value;

    if (selectedOption == null) {
    }
    
    var temp = startDay;
    dropDownControl.options.length = 0;
    var option;
    for (var i = 0; i < weekDayNo.length; i++) {
        var canAdd = true;
        if (startDay < endDay) {
            var canAdd = false;
            if (weekDayNo[i] < startDay) { canAdd = true; }
            else if (weekDayNo[i] > startDay) {
                if (weekDayNo[i] >= endDay) { canAdd = true; }
            }
        }
        else {
            if (weekDayNo[i] < endDay) { canAdd = false; }
            else if (weekDayNo[i] >= startDay) {
                canAdd = false;
            }
        }
        if (canAdd) {
            option = document.createElement("OPTION");
            option.innerHTML = weekDayText[i];
            option.value = weekDayNo[i];
            if (selectedOption != null) {
                if (eval(selectedOption) == weekDayNo[i]) {
                    option.selected = true;
                }
            }
            dropDownControl.appendChild(option);
        }
    }
    if (document.getElementById('hdnWeekDay').value != '') {
        weekDay = new Array();
        weekDay = String(document.getElementById('hdnWeekDay').value).split(',');
        document.getElementById('ddlReportWeeklyFrom').value = weekDay[0];
        document.getElementById('ddlReportWeeklyTo').value = weekDay[1];
        document.getElementById('ddlWeekDayOn').value = weekDay[2];
        if (document.getElementById('ddlWeekDayOn').value=="") {
            document.getElementById('ddlWeekDayOn').selectedIndex = 0;
          weekDay[2] = document.getElementById('ddlWeekDayOn').value;
           document.getElementById('hdnWeekDay').value = weekDay.join( ",");
        }
        if (document.getElementById('ddlReportWeeklyTo').value == "") {
            document.getElementById('ddlReportWeeklyTo').selectedIndex = 0;
            weekDay[1] = document.getElementById('ddlReportWeeklyTo').value;
           document.getElementById('hdnWeekDay').value = weekDay.join(",");
        }
    }
}

function CheckWeek() {
    ddlReportWeeklyFrom = document.getElementById('ddlReportWeeklyFrom');
    ddlReportWeeklyTo = document.getElementById('ddlReportWeeklyTo');
    ddlWeekDayOn = document.getElementById('ddlWeekDayOn');
    if ((ddlReportWeeklyFrom.value != '') && (ddlReportWeeklyTo.value != '') && (ddlWeekDayOn.value != '')) {
        document.getElementById('hdnWeekDays').value = ddlReportWeeklyFrom.value + ',' + ddlReportWeeklyTo.value + ',' + ddlWeekDayOn.value;
    }
    ChangeReportOn();
}

/***********************************End of the check frequency weekly*********************************************/
function FrequencyChange() {
    var isIE = CheckBrowser();
    if (document.getElementById('ddlFrequency') != null) {
        if (document.getElementById('ddlFrequency').value == '0' || document.getElementById('ddlFrequency').value == '3') {
            document.getElementById('pnlFrequencyDaily').style.display =  "block";
            document.getElementById('pnlFrequencyWeekly').style.display = 'none';
            document.getElementById('pnlFrequencyMonthly').style.display = 'none';

            CheckTimePicker(document.getElementById('ddlReportFromHrs'), 'ddlReportFromAMPM');
            CheckTimePicker(document.getElementById('ddlReportToHrs'), 'ddlReportToAMPM');
            CheckTimePicker(document.getElementById('ddlReportGenerationTime'), 'ddlReportGenerationTimeAMPM');
        }
        else if (document.getElementById('ddlFrequency').value == '1') {
        
            ddlReportWeeklyFrom=document.getElementById('ddlReportWeeklyFrom');
            ddlReportWeeklyTo=document.getElementById('ddlReportWeeklyTo');
            ddlWeekDayOn = document.getElementById('ddlWeekDayOn');
            var str = ddlReportWeeklyFrom.value;            
            LoadDayWeekRanges(ddlReportWeeklyFrom.id, 'nothing', ddlReportWeeklyFrom.id, ddlReportWeeklyTo.id, ddlWeekDayOn.id);
            document.getElementById('pnlFrequencyDaily').style.display = 'none';
            document.getElementById('pnlFrequencyWeekly').style.display = "block";
            document.getElementById('pnlFrequencyMonthly').style.display = 'none';

            CheckTimePicker(document.getElementById('ddlReportWeeklyGenerateTime'), 'ddlReportWeeklyGenerationFormat');
        }
        else if (document.getElementById('ddlFrequency').value == '2') {

            document.getElementById('pnlFrequencyDaily').style.display = 'none';
            document.getElementById('pnlFrequencyWeekly').style.display = 'none';
            document.getElementById('pnlFrequencyMonthly').style.display ="block" ;

            CheckTimePicker(document.getElementById('ddlReportMonthlyGenerationTime'), 'ddlReportMonthlyGenerationFormat');
        }
        else {
            document.getElementById('pnlFrequencyDaily').style.display = 'none';
            document.getElementById('pnlFrequencyWeekly').style.display = 'none';
            document.getElementById('pnlFrequencyMonthly').style.display = 'none';

            CheckTimePicker(document.getElementById('ddlReportGenerationTime'), 'ddlReportGenerationTimeAMPM');
        }
        if (document.getElementById('txtEffectiveFrom') != null) {
            if (!document.getElementById('txtEffectiveFrom').disabled) {
                checkDateLessThanCurrentDate($find("ceEffectiveFrom"), null);
            }
        }
    }
}

function ShowHideButtons(isShow) {
    var isIE = CheckBrowser();
    if (document.getElementById('lbtShowReport') != null) {
        document.getElementById('lbtShowReport').style.display = (isShow) ? 'none' : (isIE ? "block" : "initial");
    }
    if (document.getElementById('ibtnSendEmail') != null) {
        document.getElementById('ibtnSendEmail').style.display = (isShow) ? 'none' : (isIE ? "block" : "initial");
    }
    if (document.getElementById('ibtnScheduleReports') != null) {
        document.getElementById('ibtnScheduleReports').style.display = (isShow) ? 'none' : (isIE ? "block" : "initial");
    }
    if (document.getElementById('divReportbutton') != null) {
        document.getElementById('divReportbutton').style.display = (isShow) ? 'none' : (isIE ? "block" : "initial");
    }
    
}

function ValidCurrRate()
{
    var lCurr = document.getElementById('lblCurrencyRate');
    var dCurr = document.getElementById('ddlCurrency');
    var uCurr = document.getElementById('hdnUserCurrency');
    if(dCurr != null && uCurr !=null)
    {
        if(dCurr.options[dCurr.selectedIndex].text == uCurr.value)
        {
            document.getElementById('txtCurrRate').disabled = true;
            lCurr.innerHTML = 'Currency Rate ('+ uCurr.value + ')';
            document.getElementById('txtCurrRate').value = '';
        }
        else
        {
            lCurr.innerHTML = 'Currency Rate Per '+ dCurr.options[dCurr.selectedIndex].text +' ('+ uCurr.value + ')';
            document.getElementById('txtCurrRate').disabled = false;
            document.getElementById('txtCurrRate').value = '';
        }
    }

}