
//The functions below verify format for specific input entry types.
function isblank(s) {
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c !='') && (c !='\n') && (c !='\t')) return false;
    }
    return true;
}

function isEmail(e) {
re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
	if (re.test(e.value)) {
		return true;
	}
return false;
}

function isPhone(e) {
re = /^\(?(\d{3})\)?[\.\-\/ ]?(\d{3})[\.\-\/ ]?(\d{4})$/;		
validPhone = re.exec(e.value);
	if (validPhone) {
		e.value = "(" + validPhone[1] + ") " + validPhone[2] + "-" + validPhone[3];
		return true;
	}
return false;		
}

function isZip(e) {
re = /^\d{5}([-]?\d{4})?$/;
	if (re.test(e.value)) {
		return true;
	}
return false;
}

// This function cycles through the radio element array and evaluates the checked property
function getRadioValue(radioEl) {			
	for (var i = 0; i<radioEl.length; i++) {				
		if (radioEl[i].checked) {						
			return radioEl[i].value;
		}
	}
	return "";
}
// This function enables and requires the other field if selected
function otherOn(slct,other){	
	var elm = document.getElementById(other);
	//alert(elm.name);
	if (slct.value == "Other") {	
	elm.style.backgroundColor = "#FFFFFF";
	elm.disabled = false ;
	elm.optional = false ;
	elm.focus();
	}
	else {
	elm.style.backgroundColor = "#E8E8E8";
	elm.disabled = true;	
	}
}

// This is the function that performs form verification. It is invoked
// from the onsubmit event handler. The handler should return whatever
// value this function returns.
function verify(f) {
    var msg;
    var empty_fields = "";
    var errors = "";
	var focusItem = null;

    // Loop through the elements of the form, looking for all elements that don't have an "optional"
    // property defined. Then, check for fields that are empty and/or not checked and make a list of them.    
    // If the element has a "format" property defined, verify that it is proper format.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
		//var elm_star = e.name +'_star';
		//var mark = document.getElementById(elm_star);
        //alert(e.getAttribute('descr'));
		if (((e.type == "text") || (e.type == "textarea") || (e.type == "select-one") || (e.type == "select-multiple") || (e.type == "password")) && !e.getAttribute('optional')) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.getAttribute('descr');
				//e.style.background ='#FEC2CB';
				//e.style.borderWidth = "2px 2px 2px 2px";
				//e.style.borderColor ='#CC0000';								
				//mark.innerHTML = "&laquo;";
				if (!focusItem){
				focusItem = e;
				}
                continue;
            }
			//e.style.borderWidth = "1px 1px 1px 1px";	
			//e.style.borderColor ='#999999'
			//mark.innerHTML = "";
        }
		//If radio type loop through radio array and verify checked proprerty
		if (((e.type == "radio") || (e.type == "checkbox")) && !e.getAttribute('optional')) {			
			radioEl = eval('document.' + f.name +'.' + e.name);
			if (getRadioValue(radioEl) == "") {				
                empty_fields += "\n          " + e.getAttribute('descr');
				i += radioEl.length-1
				//e.style.borderStyle ='solid'
				//e.style.borderWidth = "2px 2px 2px 2px";			
				//e.style.borderColor ='#CC0000'								
				//mark.innerHTML = "&laquo;";
				if (!focusItem){
				focusItem = e;
				}
                continue;
            }
			i += radioEl.length-1
			//e.style.borderStyle ='none'
			//mark.innerHTML = "";			
		}		
		//Verify email format
		if (e.getAttribute('format') == "email") {
			if (!e.getAttribute('optional') || (e.getAttribute('optional') && e.value != "")){
				if (!isEmail(e)){
					//errors += "\n          " + e.getAttribute('descr') + " - Example: yourname@yourdomain.com";
					errors += "\n          Please enter a valid email address.";
					if (!focusItem){
					focusItem = e;
					}				
				}
			}
		}
		//Verify phone format
		if (e.getAttribute('format') == "phone") {
			if (!isPhone(e)){
				errors += "\n          " + e.getAttribute('descr') + " - Example: (xxx) xxx-xxxx";
				if (!focusItem){
				focusItem = e;
				}				
			}
		}
		//Verify zip format
		if (e.getAttribute('format') == "zip") {
			if (!isZip(e)){
				errors += "\n          " + e.getAttribute('descr') + " - Example: xxxxx-xxxx";
				if (!focusItem){
				focusItem = e;
				}				
			}
		}		
    }

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) {
		//alert('no errors');
		return true;	
	}

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";        		
    }
	if (errors) { 
		msg += "\n- The following field(s) have formatting errors:" 
				+ errors + "\n";
		}    
    alert(msg);
	//warning = document.getElementById('required');
	//warning.innerHTML = "<span class=\"star\">We have encountered an error with the form. All items colored red and/or marked with \"&laquo;\" must be completed.</span>";
	focusItem.focus();	
    return false;
}


