var form;

function Validate() {

    form.subjectAreaString.value = GatherSelections(form.subjectArea);
    form.trainingProgrammeString.value = GatherSelections(form.trainingProgramme)

    if (form.basedOn[0].checked) {
        form.locationString.value = GatherSelections(form.localAuthorityArea);
    } else if (form.basedOn[1].checked) {
        form.locationString.value = GatherSelections(form.fundRegion);
    } else {
        form.locationString.value = GatherSelections(form.region);
    }

    form.vacancyOnly.value = VacancyOnly();

    if (!CheckDate(form.tempStartDate.value, "startdate")) {
        alert("Invalid start date.  Please try again.");
        return false;
    }
    if (!CheckDate(form.tempEndDate.value, "enddate")) {
        alert("Invalid end date.  Please try again.");
        return false;
    }

    if (form.startDate.value == "")
    {
        form.startDate.value = " ";
    }
    if (form.endDate.value == "")
    {
        form.endDate.value = " ";
    }
    if (form.startDate.value > form.endDate.value) {
        if (form.tempStartDate.value > "") {
            if (form.tempEndDate.value > "") {
                alert('End date must be after the start date.  Please try again.')
                return false;
            }
        }
    }
    return true;

}

// This function returns a string containing
// all the options selected from a list
function GatherSelections(theList) {
    // put vertical bars around each selection
    var selections = '|'
    for (var i = 0; i < theList.length; i++) {
        if (theList.options[i].selected) {
            selections = selections + theList.options[i].value + '|';
        }
        ;
    }
    ;
    return selections;
}

// This function returns 'Y' if vacancies_flag is
// checked else returns 'N'
function VacancyOnly() {
    if (form.vacanciesFlag.checked)
    {
        return 'Y'
    }
    else
    {
        return 'N'
    }
}

// This functions checks if the date entered is a valid date
function CheckDate(ad_date, whichdate) {
    if (ad_date > '          ') {
        // search for the position of the "/"
        d = new Date(ad_date);
        if (isNaN(d)) {
            return false;
        }
        ;

        lpos1 = Pos(ad_date, '/', 0)
        lpos2 = Pos(ad_date, '/', lpos1 + 1)
        lyear = ad_date.substring(lpos2 + 1, ad_date.length)
        lmonth = ad_date.substring(lpos1 + 1, lpos2)
        if (Number(lmonth) > 12 || Number(lmonth) < 1) {
            //alert('invalid month')
            return false;
        }

        lday = ad_date.substring(0, lpos1)

        if (Number(lyear) < 100) {
            if (Number(lyear) > 50) {
                lyear = '19' + lyear
            }
            else
            {
                lyear = '20' + lyear
            }
        }
        if (Number(lyear) < 999 || Number(lyear) > 9999) {
            //alert('Invalid year')
            return false
        }

        // pad single digit month and day with zeros
        if (Number(lmonth) < 10) {
            lmonth = '0' + Number(lmonth)
        }
        if (Number(lday) < 10) {
            lday = '0' + Number(lday)
        }

        dYMD = lyear + '/' + lmonth + '/' + lday

        if (whichdate == 'startdate') {
            form.startDate.value = dYMD
        }
        if (whichdate == 'enddate') {
            form.endDate.value = dYMD
        }

        d = new Date(Number(lyear), Number(lmonth) - 1, Number(lday))

        if (isNaN(d)) {
            //alert('invalid')
            return false;
        }
        ;

        if (d.getMonth() + 1 != Number(lmonth)) {
            return false;
        }
        return true;
    }

    if (whichdate == 'startdate') {
        form.startDate.value = '1999/01/01'
    }
    if (whichdate == 'enddate') {
        form.endDate.value = '2050/01/01'
    }

    return true;
}

// This function searches for the position of a substring within a string
// returns the position of the string or -1 if not found
function Pos(orig_str, sub_str, start_pos) {
    for (var i = start_pos; i < orig_str.length; i++) {
        if (orig_str.substring(i, i + sub_str.length) == sub_str) {
            return i
        }
    }
    return -1
}

// This function is used to move the radio button to
// selected regional selection and then clears
// selection of unselected regional selection
function SetRegion(whichselection) {
    if (whichselection == 'govtregion') {
        form.basedOn[0].checked = true
        ClearSelections(form.fundRegion)
        ClearSelections(form.region)
        return true
    }
    if (whichselection == 'fundregion') {
        form.basedOn[1].checked = true
        ClearSelections(form.localAuthorityArea)
        ClearSelections(form.region)
        return true
    }
    if (whichselection == 'skillnzregion') {
        form.basedOn[2].checked = true
        ClearSelections(form.localAuthorityArea)
        ClearSelections(form.fundRegion)
        return true
    }

    return true
}

// this function will clear all selections in a list
function ClearSelections(theList) {
    // set all selections to false
    for (var i = 0; i < theList.length; i++) {
        theList.options[i].selected = false
    }
    ;
    return true
}


// SelectRadio will clear all other unselected basedOn

function SelectRadio() {
    if (form.basedOn[0].checked) {
        ClearSelections(form.fundRegion)
        ClearSelections(form.region)
        return true
    }
    if (form.basedOn[1].checked) {
        ClearSelections(form.localAuthorityArea)
        ClearSelections(form.region)
        return true
    }
    if (form.basedOn[2].checked) {
        ClearSelections(form.localAuthorityArea)
        ClearSelections(form.fundRegion)
        return true
    }
    return true
}


