
// check field not empty (e.g.password)
function validate_notempty(thefield,fieldname)
{
    if (thefield.value == "")
    {
	    alert("Please enter your " + fieldname + "!");
	    thefield.select();
	    return false;
	}
	return true;
}
    
   
// check email field is valid
 function validate_email(theemail)
 {
    if (theemail.value == "")
    {
        alert("Please enter your email address!");
        theemail.select();
        return false;
    }
    if (theemail.value != "")
    {
        var emailStr = theemail.value;
        var emailPat = /^(.+)@(.+)$/
        var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
        var validChars = "\[^\\s" + specialChars + "\]"
        var quotedUser = "(\"[^\"]*\")"
        var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
        var atom = validChars + '+'
        var word = "(" + atom + "|" + quotedUser + ")"
        var word2 = "(" + validChars + "*|" + quotedUser + ")";
        var userPat = new RegExp("^" + word + "(\\." + word2 + ")*$")

        //var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
        var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$")
        var matchArray = emailStr.match(emailPat)
        if (matchArray == null) {
            alert("Invalid email! - Your email must include the '@' character")
            theemail.select();
            return false;
        }
        var user = matchArray[1]
        var domain = matchArray[2]
        if (user.match(userPat) == null) {
            alert("Invalid email! - The username part of your email has invalid characters")
            form_name.email.select();
            return false;
        }
        var IPArray = domain.match(ipDomainPat)
        if (IPArray != null) {
            for (var i = 1; i <= 4; i++) {
                if (IPArray[i] > 255) {
                    alert("Invalid email! - The IP address used in your email is invalid")
                    theemail.select();
                    return false;
                }
            }
            return true;
        }
        var domainArray = domain.match(domainPat)
        if (domainArray == null) {
            alert("Invalid email! - The domain or host for your email address is invalid. It should be similar to 'mail.mycompany.ie' or 'yahoo.com'")
            theemail.select();
            return false;
        }
        var atomPat = new RegExp(atom, "g")
        var domArr = domain.match(atomPat)
        var len = domArr.length
        if (domArr[domArr.length - 1].length < 2 ) {
            alert("Invalid email! - The last part of your email domain, must be either 2 or more characters long. (for example .ie, or .com)")
            theemail.select();
            return false;
        }
        if (len < 2) {
            alert("Invalid email! Address Too Short!")
            theemail.select();
            return false;
        }
    }
    return true;
}

// check email field is valid
function validate_emailstring(theemail) {
    if (theemail.value == "") {
        msg = "Please enter your email address!"
        theemail.select();
        return msg;
    }
    if (theemail.value != "") {
        var emailStr = theemail.value;
        var emailPat = /^(.+)@(.+)$/
        var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
        var validChars = "\[^\\s" + specialChars + "\]"
        var quotedUser = "(\"[^\"]*\")"
        var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
        var atom = validChars + '+'
        var word = "(" + atom + "|" + quotedUser + ")"
        var word2 = "(" + validChars + "*|" + quotedUser + ")";
        var userPat = new RegExp("^" + word + "(\\." + word2 + ")*$")

        //var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
        var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$")
        var matchArray = emailStr.match(emailPat)
        if (matchArray == null) {
            msg ="Invalid email address! - Your email must include the '@' character"
            theemail.select();
            return msg;
        }
        var user = matchArray[1]
        var domain = matchArray[2]
        if (user.match(userPat) == null) {
            msg = "Invalid email address! - contains invalid characters"
            form_name.email.select();
            return msg;
        }
        var IPArray = domain.match(ipDomainPat)
        if (IPArray != null) {
            for (var i = 1; i <= 4; i++) {
                if (IPArray[i] > 255) {
                    msg = "Invalid email address! - Please check the host IP address."
                    theemail.select();
                    return msg;
                }
            }
            return "";
        }
        var domainArray = domain.match(domainPat)
        if (domainArray == null) {
            msg = "Invalid email address! - Please check the domain or host name."
            theemail.select();
            return msg;
        }
        var atomPat = new RegExp(atom, "g")
        var domArr = domain.match(atomPat)
        var len = domArr.length
        if (domArr[domArr.length - 1].length < 2) {
            msg = "Invalid email! - The last part of your email domain must be 2 or more characters. (e.g. .ie, or .com )"
            theemail.select();
            return msg;
        }
        if (len < 2) {
            msg = "Invalid email address! Address Too Short!"
            theemail.select();
            return msg;
        }
    }
    return "";
}

function isValidDate(day,month,year) {
    // Checks for the following valid date formats:
    // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
    // Also separates date into month, day, and year variables
    //day = parseInt(day)
    //month = parseInt(month)
    //year =parseInt(year)
    if (month < 1 || month > 12) { // check month range
        alert("Please select a valid Month -  between 1 and 12.");
        return false;
    }
    if (day < 1 || day > 31) {
        alert("Please select a valid Day - between 1 and 31. - your day was("+ day +")");
        return false;
    }
    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
        alert("Sorry the Month " + month + " doesn't have 31 days! - please correct your selection")
        return false
    }
    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !isleap)) {
            alert("Sorry February " + year + " doesn't have " + day + " days! - please correct your selection");
            return false;
        }
    }
    return true;  // date is valid
}
