// functions that return true or false indicating validity of a specific 
// type of input 
//
// to use, call the function and pass it input.
// 
// document.myForm.onSubmit = function() {
//	if (!isValidPhoneNumber(document.myForm.phone_number.value)) {
//		alert("the phone number you entered sucks!");
//		return false;
//	}
//	return true;
// }


function isValidZipCode(s) {
	var temp = s.replace(/\D/g, "")
	return temp.match(/^\d{5}$|^\d{9}$/) != null
}

function isValidEmailAddress(s) {
	var temp = s.replace(/\s/g, "")
	return (temp.match(/^[\w\.\-]+\x40[\w\.\-]+\.\w{3}$/)) && 
			temp.charAt(0) != "." && !(temp.match(/\.\./))
}

function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
//alert("You did not enter a valid email address from step 3.")
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
//alert("You did not enter a valid email address from step 3.")
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
//alert("You did not enter a valid email address from step 3.")
return false
}

if (str.indexOf(at,(lat+1))!=-1){
//alert("You did not enter a valid email address from step 3.")
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
//alert("You did not enter a valid email address from step 3.")
return false
}

if (str.indexOf(dot,(lat+2))==-1){
//alert("You did not enter a valid email address from step 3.")
return false
}

if (str.indexOf(" ")!=-1){
//alert("You did not enter a valid email address from step 3.")
return false
}

return true
}

function ValidateForm(){
var emailID=document.myForm.txtEmail

if ((emailID.value==null)||(emailID.value=="")){
alert("Please Enter your Email ID")
emailID.focus()
return false
}
if (echeck(emailID.value)==false){
emailID.value=""
emailID.focus()
return false
}
return true
} 
// masking functions - attempt to parse and reformat 
// element's values as a specific datatype
// to use, tack them to the form field's onblur handler.
function textFieldBlurHandler() {
	this.value = this.value.trim()
}

// convenience function.
// attaches textFieldBlurHandler to the onblur event 
// of every text or textarea element in f
// var myForm = document.myForm
// attachAllTextHandlers(myForm) 
//		-> every text element in myForm now will call 
//		   textFieldBlurHandler (above) onblur
function attachAllTextHandlers(f) {
	var el
	for (var i = 0; (el = f.elements[i]); i++) {
		if (el.type == "text" || el.type == "textarea") 
			el.onblur = textFieldBlurHandler
	}
}

// as convenient a place as any to store this...
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"


// like Trim( ) in vbscript. removes trailing and 
// leading whitespace from a string
String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, "")
}

// push is a quite useful method of arrays in newer 
// javascript implementations, but not in ie5-
Array.prototype.push = function(v) {
	this[this.length] = v
	return v
}