var regexEmail = /^[\w\-\.\+]+\@([a-zA-Z0-9\-]{1,}\.)+([a-zA-Z]{2,4})$/;
var regexNumber = /^\d+$/;

function isDate(month, day, year) {
    if (month < 1 || month > 12)
        return false;
    else if(day < 1 || day > 31)
        return false;
    else if((month == 4 || month == 6 || month == 9 || month == 11) && day == 31)
        return false;
    else if (month == 2) {
        var isLeapYear = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if(day > 29 || (day == 29 && !isLeapYear))
            return false;
    }
    
    return true;
}


function getSelectedRadioValue(radioButton) {
    if(!radioButton) return "";
	var radioLength = radioButton.length;
    
    if(radioLength == undefined) return radioButton.checked ? radioButton.value : '';
    for(var i = 0; i < radioLength; i++) if(radioButton[i].checked) return radioButton[i].value;
    
    return "";
}

function trim(sourceString) {
    while(sourceString.substring(0, 1) == ' ') sourceString = sourceString.substring(1, sourceString.length);
    while(sourceString.substring(sourceString.length - 1, sourceString.length) == ' ') sourceString = sourceString.substring(0, sourceString.length - 1);
    return sourceString;
}

function validationAlert(messageString, offendingField) {
    alert(messageString);
    if(!offendingField)
        return(false);

    offendingField.focus();
    if(offendingField.type == "text" || offendingField.type == "password")
        offendingField.select();
    
    return(false);
}


function validate_aaeTopic(sourceForm) {
    if(sourceForm.aaeText.value == "") return(validationAlert("Please enter text for the topic.", sourceForm.aaeText));
    else if(sourceForm.aaeEmailTarget.value == "") return(validationAlert("Please enter a target e-mail address for the topic.", sourceForm.aaeEmailTarget));
    else if(!regexEmail.test(sourceForm.aaeEmailTarget.value)) return validationAlert("Please enter a valid e-mail address.", sourceForm.aaeEmailTarget);
    
    return(true);
}


function validate_surveyResults(sourceForm) {
    if(sourceForm.surveyID.value == "") return(validationAlert("Please select a survey.", sourceForm.surveyID));
    if(getSelectedRadioValue(sourceForm.action) == "")  return validationAlert("Please indicate the action you would like to perform.", sourceForm.action[0]);
    
    if(getSelectedRadioValue(sourceForm.scope) == "")  return validationAlert("Please indicate the scope you would like to include.", sourceForm.scope[0]);
    else if(getSelectedRadioValue(sourceForm.scope) == "last") {
        if(sourceForm.scopeLastNumber.value == "") return(validationAlert("Please indicate the number of responses to list under scope.", sourceForm.scopeLastNumber));
        else if(!regexNumber.test(sourceForm.scopeLastNumber.value)) return(validationAlert("Please include only positive numbers for the responses to list under scope.", sourceForm.scopeLastNumber));
    }
    else if(getSelectedRadioValue(sourceForm.scope) == "date") {
        var fromSpecified = false, toSpecified = false;
        
        if(sourceForm.scopeDateBeginMonth.value != "" || sourceForm.scopeDateBeginDay.value != "" || sourceForm.scopeDateBeginYear.value != "") {
            fromSpecified = true;
            if(sourceForm.scopeDateBeginMonth.value == "") return(validationAlert("Please indicate the start month for the date range.", sourceForm.scopeDateBeginMonth));
            else if(!regexNumber.test(sourceForm.scopeDateBeginMonth.value)) return(validationAlert("Please use only positive numbers for the start month in the date range.", sourceForm.scopeDateBeginMonth));
            else if(sourceForm.scopeDateBeginDay.value == "") return(validationAlert("Please indicate the start day for the date range.", sourceForm.scopeDateBeginDay));
            else if(!regexNumber.test(sourceForm.scopeDateBeginDay.value)) return(validationAlert("Please use only positive numbers for the start day in the date range.", sourceForm.scopeDateBeginDay));
            else if(sourceForm.scopeDateBeginYear.value == "") return(validationAlert("Please indicate the start year for the date range.", sourceForm.scopeDateBeginYear));
            else if(!regexNumber.test(sourceForm.scopeDateBeginYear.value)) return(validationAlert("Please use only positive numbers for the start year in the date range.", sourceForm.scopeDateBeginYear));
            else if(!isDate(sourceForm.scopeDateBeginMonth.value, sourceForm.scopeDateBeginDay.value, sourceForm.scopeDateBeginYear.value)) return(validationAlert("The start date you specified in the date range is invalid.", sourceForm.scopeDateBeginMonth));
        }
        
        if(sourceForm.scopeDateEndMonth.value != "" || sourceForm.scopeDateEndDay.value != "" || sourceForm.scopeDateEndYear.value != "") {
            if(sourceForm.scopeDateEndMonth.value == "") return(validationAlert("Please indicate the end month for the date range.", sourceForm.scopeDateEndMonth));
            else if(!regexNumber.test(sourceForm.scopeDateEndMonth.value)) return(validationAlert("Please use only positive numbers for the end month in the date range.", sourceForm.scopeDateEndMonth));
            else if(sourceForm.scopeDateEndDay.value == "") return(validationAlert("Please indicate the end day for the date range.", sourceForm.scopeDateEndDay));
            else if(!regexNumber.test(sourceForm.scopeDateEndDay.value)) return(validationAlert("Please use only positive numbers for the end day in the date range.", sourceForm.scopeDateEndDay));
            else if(sourceForm.scopeDateEndYear.value == "") return(validationAlert("Please indicate the end year for the date range.", sourceForm.scopeDateEndYear));
            else if(!regexNumber.test(sourceForm.scopeDateEndYear.value)) return(validationAlert("Please use only positive numbers for the end year in the date range.", sourceForm.scopeDateEndYear));
            else if(!isDate(sourceForm.scopeDateEndMonth.value, sourceForm.scopeDateEndDay.value, sourceForm.scopeDateEndYear.value)) return(validationAlert("The end date you specified in the date range is invalid.", sourceForm.scopeDateEndMonth));
            toSpecified = true;
        }
        
        if(!fromSpecified && !toSpecified) return(validationAlert("Please indicate at least the 'from' or 'thru' date for the date range.", sourceForm.scopeDateBeginMonth));
    }
    
    if(getSelectedRadioValue(sourceForm.output) == "")  return validationAlert("Please select the output type.", sourceForm.output[0]);

    return(true);
}

function askAnEducatorForm_validation(sourceForm) {
    if(trim(sourceForm.topic.value) == "") return validationAlert("Please select a topic for your question.", sourceForm.topic);
    if(trim(sourceForm.name.value) == "") return validationAlert("Please enter your name.", sourceForm.name);
    if(trim(sourceForm.email.value) == "") return validationAlert("Please enter your e-mail address.", sourceForm.email);
    else if(!regexEmail.test(sourceForm.email.value)) return validationAlert("Please enter a valid e-mail address.", sourceForm.email);
    if(trim(sourceForm.body.value) == "") return validationAlert("Please enter your question.", sourceForm.body);
    
    return(true);
}


function feedbackForm_validation(sourceForm) {
    if(getSelectedRadioValue(sourceForm.questionFind) == "")  return validationAlert("Please indicate if you were able to find the information you needed.", sourceForm.questionFind[0]);
    if(getSelectedRadioValue(sourceForm.questionFriendly) == "")  return validationAlert("Please indicate if the web site was user-friendly.", sourceForm.questionFriendly[0]);
    if(getSelectedRadioValue(sourceForm.questionLearn) == "")  return validationAlert("Please indicate if you were able to learn something from the website.", sourceForm.questionLearn[0]);
    if(getSelectedRadioValue(sourceForm.questionTrustworthy) == "")  return validationAlert("Please indicate if the information on the website was trustworthy.", sourceForm.questionTrustworthy[0]);
    // if(trim(sourceForm.comments.value) == "")  return validationAlert("Please include any additional comments or suggestions you may have regarding the website.", sourceForm.questionTrustworthy[0]);


    return(true);
}
