/**
 *  Trims leading and trailing spaces.
 */
function trim(s) {

    while (s.substring(0, 1) == ' ') {
        s = s.substring(1, s.length);
    }
    while (s.substring(s.length - 1, s.length) == ' ') {
        s = s.substring(0, s.length - 1);
    }
    return s;
}

/**
 *  Does a sanity check on supposed email addresses.
 */
function validateEmail(email) {

    var i = 1;
    while ((i < email.length) && (email.charAt(i) != "@")) {
        i++;
    }
    if ((i >= email.length) || (email.charAt(i) != "@")) {
        return false;
    } else {
        i += 2;
    }
    while ((i < email.length) && (email.charAt(i) != ".")) {
        i++;
    }
    if ((i >= email.length - 1) || (email.charAt(i) != ".")) {
        return false;
    } else {
        return true;
    }
}

/**
 *  Returns true if character c is a digit (0 .. 9).
 */
function isDigit(c) {

    return ((c >= "0") && (c <= "9"));
}

/**
 *  Returns true if all characters in string s are numbers.
 *  Accepts non-signed integers only.
 *  Does not accept floating point, exponential notation, etc.
 */
function isInteger(s) {

    var i = 0;
    for (; i < s.length; i++) {
        if (!isDigit(s.charAt(i))) {
            return false;
        }
    }
    return true;
}

/**
 *  Validates the generic enquiry form.
 */
function validateEnquiry() {

    var strAlert = "_______________________________________\n\nPlease Provide Your:\n\n";
    var isAlert = false;

    if (trim(Enquiry.realname.value) == "") {
        isAlert = true;
        strAlert += "- Name\n";
    }
    if (!validateEmail(trim(Enquiry.email.value))) {
        strAlert += "- Email\n";
        isAlert = true;
    }
    if (trim(Enquiry.Enquiry.value) == "") {
        strAlert += "- Enquiry\n";
        isAlert = true;
    }

    // Display Any Error Messages

    if (isAlert == true) {
        strAlert += "\n_______________________________________";
        alert(strAlert);
        return false;
    } else {
        alert("Thank you for your enquiry.");
        return true;
    }
}

/**
 *  Validates the volunteers enquiry form.
 */
function validateVolunteers() {

    var strAlert = "_______________________________________\n\nPlease Provide Your:\n\n";
    var isAlert = false;

    if (trim(Volunteers.Name.value) == "") {
        isAlert = true;
        strAlert += "- Full Name\n";
    }
    if (trim(Volunteers.Phone.value) == "") {
        isAlert = true;
        strAlert += "- Phone Number\n";
    }
    if (!validateEmail(trim(Volunteers.email.value))) {
        strAlert += "- Email Address\n";
        isAlert = true;
    }
    if (trim(Volunteers.ConnectGroup.value) == "") {
        isAlert = true;
        strAlert += "- Connect Group Leader\n";
    }
    var indAvailability = -1;
    for (i = 0; i < Volunteers.Availability.length; i++) {
        if (Volunteers.Availability[i].checked) {
            indAvailability = i;
        }
    }
    if (indAvailability == -1) {
        isAlert = true;
        strAlert += "- Service Availability\n";
    }
    var indArea = -1;
    for (i = 0; i < Volunteers.Area.length; i++) {
        if (Volunteers.Area[i].checked) {
            indArea = i;
        }
    }
    if (indArea == -1) {
        isAlert = true;
        strAlert += "- Area Of Involvement\n";
    }

    // Display Any Error Messages

    if (isAlert == true) {
        strAlert += "\n_______________________________________";
        alert(strAlert);
        return false;
    } else {
        alert("Thank you for volunteering!");
        return true;
    }
}

/**
 *  Validates the diaster relief donations form.
 */
function validateDonations() {

    // Personal Details

    var strAlert = "_______________________________________\n\nPlease Provide Your:\n\n";
    var isAlert = false;

    if (trim(Donations.FirstName.value) == "") {
        isAlert = true;
        strAlert += "- First Name\n";
    }
    if (trim(Donations.LastName.value) == "") {
        isAlert = true;
        strAlert += "- Last Name\n";
    }
    if (trim(Donations.Address.value) == "") {
        isAlert = true;
        strAlert += "- Address\n";
    }
    if (trim(Donations.Suburb.value) == "") {
        isAlert = true;
        strAlert += "- Suburb\n";
    }
    if (trim(Donations.Postcode.value) == "") {
        isAlert = true;
        strAlert += "- Postcode\n";
    }
    if (trim(Donations.State.value) == "") {
        isAlert = true;
        strAlert += "- State\n";
    }
    if (trim(Donations.Country.value) == "") {
        isAlert = true;
        strAlert += "- Country\n";
    }
    if (trim(Donations.Phone.value) == "") {
        isAlert = true;
        strAlert += "- Phone Number\n";
    }
    if (!validateEmail(trim(Donations.email.value))) {
        strAlert += "- Email Address\n";
        isAlert = true;
    }

    // Credit Card Details

    var indCardType = -1;
    for (i = 0; i < Donations.CardType.length; i++) {
        if (Donations.CardType[i].checked) {
            indCardType = i;
        }
    }
    if (indCardType == -1) {
        isAlert = true;
        strAlert += "- Credit Card Type\n";
    }
    if (trim(Donations.CardName.value) == "") {
        isAlert = true;
        strAlert += "- Name On Credit Card\n";
    }
    if (trim(Donations.CardNumber.value) == "") {
        isAlert = true;
        strAlert += "- Credit Card Number\n";
    }
    if (trim(Donations.CardExpiryMonth.value) == "" || trim(Donations.CardExpiryYear.value) == "") {
        isAlert = true;
        strAlert += "- Credit Card Expiry Date\n";
    }
    if (trim(Donations.Amount.value) == "") {
        isAlert = true;
        strAlert += "- Donation Amount\n";
    }

    // Display Any Error Messages

    if (isAlert == true) {
        strAlert += "\n_______________________________________";
        alert(strAlert);
        return false;
    } else {
        alert("Thank you for your donation!");
        return true;
    }
}

/**
 *  Validates the United mailing list form.
 */
function validateUnited() {

    if (!validateEmail(trim(united.email.value))) {
        var strAlert = "Please Enter Your Email";
        alert(strAlert);
        return false;
    } else {
        alert("Thank you, you will hear from Hillsong United soon!\n");
        return true;
    }
}

/**
 *  Validates the United "Glory Story" form.
 */
function validateGloryStory() {

    var strAlert = "_______________________________________\n\nPlease Provide Your:\n\n";
    var isAlert = false;
    var strName = trim(document.glorystory.Name.value);
    var strEmail = trim(document.glorystory.email.value);
    var strStory = trim(document.glorystory.Story.value);

    if (strName == "") {
        isAlert = true;
        strAlert += "- Name\n";
    }
    if (!validateEmail(strEmail)) {
        isAlert = true;
        strAlert += "- Email\n";
    }
    if (strStory == "") {
        isAlert = true;
        strAlert += "- Glory Story\n";
    }
    if (isAlert == true) {
        strAlert += "\n_______________________________________";
        alert(strAlert);
        return false;
    } else {
        alert("Thank you for your Glory Story!\n");
        return true;
    }
}

/**
 *  Validates the magazine registration form.
 */
function validateMagazine() {

    var strAlert = "_______________________________________\n\nPlease Provide Your:\n\n";
    var isAlert = false;
    var strFirstName = trim(document.magazine.strFirstName.value);
    var strLastName = trim(document.magazine.strLastName.value);
    var strAddress = trim(document.magazine.strAddress.value);
    var strSuburb = trim(document.magazine.strSuburb.value);
    var strPostcode = trim(document.magazine.strPostcode.value);
    var strState = trim(document.magazine.strState.value);

    if (strFirstName == "") {
        isAlert = true;
        strAlert += "- First Name\n";
    }
    if (strLastName == "") {
        isAlert = true;
        strAlert += "- Last Name\n";
    }
    if (strAddress == "") {
        isAlert = true;
        strAlert += "- Address\n";
    }
    if (strSuburb == "") {
        isAlert = true;
        strAlert += "- Suburb / City\n";
    }
    if (strPostcode == "") {
        isAlert = true;
        strAlert += "- Postcode / ZIP\n"
   }
    if (strState == "") {
        isAlert = true;
        strAlert += "- State\n";
    }
    if (isAlert == true) {
        strAlert += "\n_______________________________________";
        alert(strAlert);
        return false;
    } else {
        alert("Thank you for your interest in the Hillsong Magazine.\n");
        return true;
    }
}

/**
 *  Validates the Watoto mailing list form.
 */
function validateWatoto() {

    if (!validateEmail(trim(watoto.email.value))) {
        var strAlert = "Please Enter Your Email";
        alert(strAlert);
        return false;
    } else {
        alert("Thank you, you will hear from us soon!\n");
        return true;
    }
}

/**
 *  Validates the orchestra enquiry form.
 */
function validateOrchestra() {

    var strAlert = "_______________________________________\n\nPlease Provide Your:\n\n";
    var isAlert = false;

    if (trim(Orchestra.Name.value) == "") {
        isAlert = true;
        strAlert += "- Name\n";
    }
    if (trim(Orchestra.Instrument.value) == "") {
        isAlert = true;
        strAlert += "- Instrument\n";
    }
    if (trim(Orchestra.Address.value) == "") {
        isAlert = true;
        strAlert += "- Address\n";
    }
    if (trim(Orchestra.Phone.value) == "" || trim(Orchestra.Mobile.value)) {
        isAlert = true;
        strAlert += "- Contact Number\n";
    }
    if (!validateEmail(trim(Orchestra.email.value))) {
        strAlert += "- Email Address\n";
        isAlert = true;
    }

    // Display Any Error Messages

    if (isAlert == true) {
        strAlert += "\n_______________________________________";
        alert(strAlert);
        return false;
    } else {
        alert("Thank you for your enquiry!");
        return true;
    }
}

