function isValidPhoneNumber(s) {
    var temp = s.replace(/\D/g, "")
    return temp.length > 9 && temp.length < 26
}

function isValidZipCode(s) {
    var temp = s.replace(/\D/g, "")
    return temp.match(/^\d{5}$|^\d{9}$/) != null
}

function isValidSelectBox(o) {
    return (o.options[o.selectedIndex].value != "_none_" && 
            o.options[o.selectedIndex].value.trim() != "")
}

function isValidEmailAddress(s) {
    var temp = s.replace(/\s/g, "")
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var regex = new RegExp(emailReg);
    return regex.test(s);

	/*
    return (temp.match(/^[\w\.\-]+\x40[\w\.\-]+\.\w{3}$/)) && 
            temp.charAt(0) != "." && !(temp.match(/\.\./))
	*/
}

function textFieldBlurHandler() {
    this.value = this.value.trim()
}

function dateFieldBlurHandler() {
    var d = new Date(this.value)
    if (!isNaN(d)) {
        if (this.value.search(/\d{3}/) == -1) 
            d.setYear(Math.floor((new Date()).getFullYear() / 100) * 100 + d.getFullYear() % 100);

        this.value = d.getHumanDateString()
    }
}

function timeFieldBlurHandler() {
    var t = new Time(this.value)
    if (!isNaN(t)) this.value = t.getHumanTimeString()
}

function phoneFieldBlurHandler() {
    var temp = this.value.replace(/\D/g, "")

    if (temp.length > 9 && temp.length < 26)
        if (temp.length == 10) {
            this.value = "(" + temp.substring(0,3) + ") " 
            this.value += temp.substring(3,6) + "-" + temp.substring(6,10)
        }
    }

function zipcodeFieldBlurHandler() {
    var temp = this.value.replace(/\D/g, "")

    if (temp.length == 5 || temp.length == 9) { 
        if (temp.length == 5) this.value = temp
        else this.value = temp.substring(0,5) + "-" + temp.substring(5,9)
    }
}

function attachAllTextHandlers(f) {
    var el
    for (var i = 0; (el = f.elements[i]); i++) {
        if (el.type == "text" || el.type == "textarea") 
            el.onblur = textFieldBlurHandler
    }
}

var STD_ERROR_PREFIX = "There were one or more problems with the form values "
STD_ERROR_PREFIX += "you entered.\nPlease check the following and try submitting "
STD_ERROR_PREFIX += "the form again:\n\n"

Date.prototype.getMonthName = function() {
    return ["January","February","March","April","May","June","July","August",
            "September","October","November","December"][this.getMonth()]
}

Date.prototype.getHumanDateString = function() {
return this.getMonthName() + " " + this.getDate() + ", " + this.getFullYear()
}

Date.prototype.getHumanTimeString = function() {
    var h = this.getHours()
    var m = this.getMinutes()
    var t = h >= 12 ? "pm" : "am"

    if (h == 0) h = 24
    if (h > 12) h -= 12
    h = String(h)
    m = String(m)
    if (m.length == 1) m = "0" + m

    return h + ":" + m + " " + t
}

function Time(sTimeStr) {
    if (sTimeStr.trim() != "")
    {
        var a = sTimeStr.match(/\d{1,2}/g)
        var t = sTimeStr.match(/(am|pm|a|p)/ig)
        if (a) {
            var d = new Date()
            var h = a[0]
            var m = a[1] ? a[1] : 0;
            if (t) t = String(t[0]);
            if (t && t.charAt(0).toLowerCase() == "p") h = h % 12 + 12
            d.setHours(h)
            d.setMinutes(m)
            return d
        }
        return null
    }
}

String.prototype.trim = function() {
    return this.replace(/^\s*|\s*$/g, "")
}

String.prototype.endsWith = function() {
    var bOk = false
    for (var i = 0; i < arguments.length; i++) {
        if (this.indexOf(arguments[i]) == this.length - arguments[i].length) {
            bOk = true
            break
        }
    }
    return bOk
}

Array.prototype.push = function(v) {
    this[this.length] = v
    return v
}

