function ValidDate(source, args, theDay, theMonth, theYear) {
    args.IsValid = true;
    
    var day     = GetObj(theDay);
    var month   = GetObj(theMonth);
    var year    = GetObj(theYear);
    
    // ensure all values (dd,mm,yyyy) have been supplied
    if (day.selectedIndex == 0 || month.selectedIndex == 0 || year.selectedIndex == 0) {
        args.IsValid = false;
        return;
    }
    
    // ensure the date is valid
    var theDate = day.options[day.selectedIndex].text + "/" + month.options[month.selectedIndex].text + "/" + year.options[year.selectedIndex].text;
    
    if (!isDate(theDate)) {
       args.IsValid = false;
       return; 
    }
}

// Ensures the start and end dates are in the correct order
function ValidDateOrder(source, args, startDay, startMonth, startYear, endDay, endMonth, endYear) {
    args.IsValid = true;
    
    var theStartDay     = GetObj(startDay);
    var theStartMonth   = GetObj(startMonth);
    var theStartYear    = GetObj(startYear);
    
    var theEndDay     = GetObj(endDay);
    var theEndMonth   = GetObj(endMonth);
    var theEndYear    = GetObj(endYear);
    
    if ((theStartDay.selectedIndex != 0 && theStartMonth.selectedIndex != 0 && theStartYear.selectedIndex != 0)
    && (theEndDay.selectedIndex != 0 && theEndMonth.selectedIndex != 0 && theEndYear.selectedIndex != 0)) {
        
        var startDate = theStartMonth.options[theStartMonth.selectedIndex].text + "/" + theStartDay.value + "/" + theStartYear.value;
        var endDate   = theEndMonth.options[theEndMonth.selectedIndex].text + "/" + theEndDay.value + "/" + theEndYear.value;

        if (Date.parse(startDate) > Date.parse(endDate)) {
            args.IsValid = false;
        }
    }
}

// Validates a listbox
function ValidListBoxSelection(source, args, listBoxControlId, ddlGroupsControlId) {
    args.IsValid = false;
    
    var theListBox = GetObj(listBoxControlId);
    var i;
    
    for (i = 0; i < theListBox.options.length; i++) {
        if (theListBox.options[i].selected) {
            args.IsValid = true;
            break;
        }
    }
    
    // check for customer account group selection
    if (ddlGroupsControlId != "") {
        var theDDList = GetObj(ddlGroupsControlId);
        
        if (theDDList.selectedIndex != 0) {
            args.IsValid = true;
        }
    }
}


// Validates a radiobuttonlist
function ValidRadioSelection(source, args, controlId) {
    args.IsValid = false;
    
    var radioButtons = GetObj(controlId);
    var i;
    
    var arrRadioButtons = radioButtons.getElementsByTagName("input");
    for (i = 0; i < arrRadioButtons.length; i++) {
        if (arrRadioButtons[i].checked) {
            args.IsValid = true;
            break;
        }
    }
}

// Ensures the selected mpans have not exceeded the specified maximum
// Checks to determine if the report type is a graph
function ValidTotalMpans(source, args, theMpanList, theReportType, maxMpans) {
    args.IsValid = true;
    
    var blnGraphSelected = false;
    var mpanCount = 0;
    var theListBox = GetObj(theMpanList);
    var radioButtons = GetObj(theReportType);
    var i;

    // check if the graph option has been selected, if it has check mpan count
    var arrRadioButtons = radioButtons.getElementsByTagName("input");
    for (i = 0; i < arrRadioButtons.length; i++) {
        if (arrRadioButtons[i].checked) {
            if (arrRadioButtons[i].value == "G") {
               blnGraphSelected = true;
               break;
            }
        }
    }

    if (blnGraphSelected) {
        // count the number of selected mpans
        for (i = 0; i < theListBox.options.length; i++) {
            if (theListBox.options[i].selected) {
                mpanCount++;
            }
        }
    }

    if (mpanCount > maxMpans) {
        args.IsValid = false;
    }
}

function ValidTotalCaNumbers(source, args, theMpanList, maxCaNumbers) {
    args.IsValid = true;
    
    var caNumberCount = 0;
    var theListBox = GetObj(theMpanList);
    var i;
    
    for (i = 0; i < theListBox.options.length; i++) {
            if (theListBox.options[i].selected) {
                caNumberCount++;
            }
    }
    

    if (caNumberCount > maxCaNumbers) {
        args.IsValid = false;
    }
    
}

// Ensures only 1 mpan is selected for the User Defined report regardless of the report type
function ValidUDMpanCount(source, args, theMpanList, maxMpans) {
    args.IsValid = true;
    
    var mpanCount = 0;
    var theListBox = GetObj(theMpanList);
    var i;
    
    // count the number of selected mpans
    for (i = 0; i < theListBox.options.length; i++) {
        if (theListBox.options[i].selected) {
            mpanCount++;
        }
    }
    
    if (mpanCount > maxMpans) {
        args.IsValid = false;
    }
}


// Checks all or unchecks all items in a listbox
function ChangeListSelection(blnChecked, controlId) {
    
    var theListBox = GetObj(controlId);
    var i;
        
    for (i = 0; i < theListBox.options.length; i++) {
        if (blnChecked)
            theListBox.options[i].selected = true;
        else
            theListBox.options[i].selected = false;
    }
}

// Validates a checkboxlist
function ValidCheckboxSelection(source, args, controlId) {
    args.IsValid = false;
    
    var checkboxes = GetObj(controlId);
    var i;
    
    var arrCheckboxes = checkboxes.getElementsByTagName("input");
    for (i = 0; i < arrCheckboxes.length; i++) {
        if (arrCheckboxes[i].checked) {
            args.IsValid = true;
            break;
        }
    }
}

// Ensures the scheduled time is in the correct format
function ValidTime(source, args, theHour, theMinute) {
    args.IsValid = true;
    
    var startHour = GetObj(theHour);
    var startMinute = GetObj(theMinute);

    // ensure length of 2
    if (startHour.value.length != 2 || startMinute.value.length != 2) {
        args.IsValid = false;
    }
    
    // ensure the supplied values are numeric
    if (isNaN(startHour.value) || isNaN(startMinute.value)) {
        args.IsValid = false;
    }
    
    // ensure values fall within correct range
    if (startHour.value < 0 || startHour.value > 23) {
        args.IsValid = false;
    }
    
    if (startMinute.value < 0 || startMinute.value > 59) {
        args.IsValid = false;
    }
}

function ValidScheduleEndDate(source, args, theDay, theMonth, theYear, chkRun) {
    args.IsValid = true;
    
    var day     = GetObj(theDay);
    var month   = GetObj(theMonth);
    var year    = GetObj(theYear);
    var run     = GetObj(chkRun);
    
    // ensure all values (dd,mm,yyyy) have been supplied if the run continuously checkbox is not checked
    if (!run.checked) {
        if (day.selectedIndex == 0 || month.selectedIndex == 0 || year.selectedIndex == 0) {
            args.IsValid = false;
            return;
        }
    
        // ensure the date is valid
        var theDate = day.options[day.selectedIndex].text + "/" + month.options[month.selectedIndex].text + "/" + year.options[year.selectedIndex].text;
        
        if (!isDate(theDate)) {
           args.IsValid = false;
           return; 
        }
    }
}

// Ensures the scheduled time is in the correct format
function ValidScheduleEndTime(source, args, theHour, theMinute, chkRun) {
    args.IsValid = true;
    
    var startHour = GetObj(theHour);
    var startMinute = GetObj(theMinute);
    var run = GetObj(chkRun);

    if (!run.checked) {
        // ensure length of 2
        if (startHour.value.length != 2 || startMinute.value.length != 2) {
            args.IsValid = false;
        }
        
        // ensure the supplied values are numeric
        if (isNaN(startHour.value) || isNaN(startMinute.value)) {
            args.IsValid = false;
        }
        
        // ensure values fall within correct range
        if (startHour.value < 0 || startHour.value > 23) {
            args.IsValid = false;
        }
        
        if (startMinute.value < 0 || startMinute.value > 59) {
            args.IsValid = false;
        }
    }
}

// Ensures the supplied email address is valid if the email option is selected
function ValidEmail(source, args, theEmail, deliveryOptions) {
    args.IsValid = true;
    
    var email = GetObj(theEmail);
    var radioButtons = GetObj(deliveryOptions);
    var i;
    
    var arrRadioButtons = radioButtons.getElementsByTagName("input");
    for (i = 0; i < arrRadioButtons.length; i++) {
        if (arrRadioButtons[i].checked) {
            if(arrRadioButtons[i].value == "email") {
                if (email.value.length == 0) {
                    args.IsValid = false;   
                }
                else {
                    // check format
                    if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email.value)) {
                       args.IsValid = false; 
                    }
                }
            }
        }
    }
}

// Ensures the start and end dates are in the correct order if the schedule is not being run once or
// continuously
function ValidScheduleDateOrder(source, args, startDay, startMonth, startYear, endDay, endMonth, endYear, radFrequency, chkRun) {
    args.IsValid = true;
    
    var theStartDay     = GetObj(startDay);
    var theStartMonth   = GetObj(startMonth);
    var theStartYear    = GetObj(startYear);
    
    var theEndDay     = GetObj(endDay);
    var theEndMonth   = GetObj(endMonth);
    var theEndYear    = GetObj(endYear);
    
    var theFrequency = GetObj(radFrequency);
    var theRunContinuous = GetObj(chkRun);
    
    var runOnce = IsRunOnce(theFrequency);
    
    if ((theStartDay.selectedIndex != 0 && theStartMonth.selectedIndex != 0 && theStartYear.selectedIndex != 0)
    && (theEndDay.selectedIndex != 0 && theEndMonth.selectedIndex != 0 && theEndYear.selectedIndex != 0)) {
        
        if (!runOnce && !theRunContinuous.checked) {
            var startDate = theStartMonth.options[theStartMonth.selectedIndex].text + "/" + theStartDay.value + "/" + theStartYear.value;
            var endDate   = theEndMonth.options[theEndMonth.selectedIndex].text + "/" + theEndDay.value + "/" + theEndYear.value;

            if (Date.parse(startDate) > Date.parse(endDate)) {
                args.IsValid = false;
            }
        }
    }
}

function IsRunOnce(radioButtonList) {
    var runOnce = false;
    
    if (radioButtonList.length == undefined) {
        var arrRadioButtons = radioButtonList.getElementsByTagName("input");
        if (arrRadioButtons[0].checked) {
            runOnce = true;
        }
    }
    else {
        if (radioButtonList[i].checked) {
            runOnce = true;
        }
    }
    
    return runOnce;
}

function Test1(mpansListBoxId, mpansSearchId) {
    var list1 = GetObj(mpansListBoxId);
    var search = GetObj(mpansSearchId);
    var i;
    
    for (i = 0; i < 20; i++) {
        if (list1.options[i].value == search.value) {
           alert(list1.options[i].value);
           list1.options[i].selected = true;
           break;
        }
    }    
}

// Ensures that no ca numbers are selected if the user selects a tender group
function ClearAccountItems(caNumbersListboxId) {

    var theListbox = GetObj(caNumbersListboxId);
    var i;
    
    for (i = 0; i < theListbox.options.length; i++) {
        theListbox.options[i].selected = false;
    }
}

// Ensures that no ca group is selected if the user selects a ca number
function ClearCaGroup(ddlCaGroupsId) {

    var groups = GetObj(ddlCaGroupsId);
    
    groups.selectedIndex = 0;
}


