// JavaScript Document
function approveForm(){
		if(!checkPresence("fldName")){
			alert("Please enter your name.");
			document.getElementById("fldName").focus();
			return false;
		}
		if(!checkPresence("fldNumber") && !checkPresence("fldEmail")){
			alert("Please enter either a phone number or an email address.");
			document.getElementById("fldNumber").focus();
			return false;
		}
		var phoneNum = document.getElementById("fldNumber").value;
		if(phoneNum != ""){
			if(!isPhoneNumber(phoneNum)){
				document.getElementById("fldNumber").focus();
				return false;	
			}
		}
		var email = document.getElementById("fldEmail").value;
		if(email != ""){
			if(!emailCheck("fldEmail")){
				document.getElementById("fldEmail").focus();
				return false;	
			}
		}
		
		return true;
}
function checkPresence(field){
	var tstFld = document.getElementById(field).value;
	if(tstFld == ""){
		return false;
	}
	else{
		return true;
	}
	
}
function isPhoneNumber(s){
	// Check for correct phone number
    rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
    if (!rePhoneNumber.test(s)) {
		alert("Phone Number Must Be Entered As: (###) ###-####");
		return false;
	}
	return true;
}

function emailCheck(eml) {
	 var str = document.getElementById(eml).value;
	 
	 var at="@"
	 var dot="."
	 var lat=str.indexOf(at)
	 var lstr=str.length
	 var ldot=str.indexOf(dot)
	 if (str.indexOf(at)==-1){
		 alert("Invalid email address.  Please correct.")
		 return false
	 }
	 if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		 alert("Invalid email address.  Please correct.")
		 return false
	 }
	 if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		 alert("Invalid email address.  Please correct.")
		 return false
	 }
	 if (str.indexOf(at,(lat+1))!=-1){
		 alert("Invalid email address.  Please correct.")
		 return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		 alert("Invalid email address.  Please correct.")
		 return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
		 alert("Invalid email address.  Please correct.")
		 return false
	 } 
	 if (str.indexOf(" ")!=-1){
		 alert("Invalid email address.  Please correct.")
		 return false
	 }
	return true					
}

function check_length(fld){
	maxLen = 500; // max number of characters allowed
	var txtFld = document.getElementById(fld);
	var charCt = document.getElementById("charsLeft");
	if (txtFld.length >= maxLen) {
	// Alert message if maximum limit is reached.
	// If required Alert can be removed.
	var msg = "You have reached your maximum limit of characters allowed";
	alert(msg);
	// Reached the Maximum length so trim the textarea
	txtFld.value = txtFld.value.substring(0, maxLen);
	}
	else{ // Maximum length not reached so update the value of my_text counter
	charCt.value = maxLen - txtFld.value.length;}
}


