// JavaScript Document

// string prototypes for trim funciton
String.prototype.lTrim = function () {
    var whitespace = new RegExp("^\\s+","gm");
    return this.replace(whitespace, "");
}

String.prototype.rTrim = function () {
    var whitespace = new RegExp("\\s+$","gm");
    return this.replace(whitespace, "");
}

String.prototype.trim = function () {
    return this.rTrim().lTrim();
}

function isInteger(inputVal)
{

	 inputStr= inputVal.toString()
	 for (var i = 0; i < inputStr.length; i++)
	 {
    	var oneChar = inputStr.charAt(i)
	    if (oneChar == "-" || oneChar == "." || oneChar == "(" || oneChar == ")")
    	{
      		continue
	    }
    
	    if (oneChar < "0" || oneChar > "9")
    	{
	      return false
	    } 

	 } //end for loop
     return true
	 
}//end function isInteger

function check_fields()
{
	
	var strYourName = document.frm_contact_westat.your_name.value
	var strEmailAddress = document.frm_contact_westat.email_address.value
	var strPhoneNumber = document.frm_contact_westat.phone_nbr.value
	var strSubject = document.frm_contact_westat.subject.value
	var strMsgText = document.frm_contact_westat.msg_text.value
	
	strYourName = strYourName.trim()
	strEmailAddress = strEmailAddress.trim()
	strPhoneNumber = strPhoneNumber.trim()
	strSubject = strSubject.trim()
	strMsgText = strMsgText.trim()
	
	if (strYourName.length < 3)
	{
	   alert ('Please enter your name before submitting this form!')
	   document.frm_contact_westat.your_name.focus()
	   document.frm_contact_westat.your_name.select()
	   return false
	}
	
	/*var strOrganization = trim(document.frm_contact_westat.organization.value)
	if (strOrganization.length < 2)
	{
	   alert ('Please enter the name of organization or affiliation before submitting this form!')
	   document.frm_contact_westat.organization.focus()
	   document.frm_contact_westat.organization.select()
	   return false
	} */
	

	if (strEmailAddress.length < 6)
	{
	   alert ('Please enter an email for contact before submitting this form!')
	   document.frm_contact_westat.email_address.focus()
	   document.frm_contact_westat.email_address.select()
	   return false
	}
	
	if(!/^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i.test(strEmailAddress))
	{
		alert("Please enter a valid email address for contact before submitting this form!");
		document.frm_contact_westat.email_address.focus()
		document.frm_contact_westat.email_address.select()
		return false
	}
	
	/*if (strPhoneNumber.length > 1)
	{
	   if (!isInteger(strPhoneNumber))
	 	{
			 alert('Please make sure your enter only numbers or dashes for phone number.')
			 document.frm_contact_westat.phone_nbr.focus()
			 document.frm_contact_westat.phone_nbr.select()
			 return false
	 	}	
	}*/
	
	if (strSubject.length < 3)
	{
	   alert ('Please enter a subject before submitting this form!')
	   document.frm_contact_westat.subject.focus()
	   document.frm_contact_westat.subject.select()
	   return false
	}
	
	if (strMsgText.length < 3)
	{
	   alert ('Please enter a message before submitting this form!')
	   document.frm_contact_westat.msg_text.focus()
	   document.frm_contact_westat.msg_text.select()
	   return false
	}
	
	//check if the program areas radio button is selected, if so, verify if an option was selected
	if (document.frm_contact_westat.send_msg_to[1].checked)
	{
		if (document.frm_contact_westat.prog_areas.selectedIndex== 0)
		{
			alert ("Please select one of the program areas options before submitting the form.")
			document.frm_contact_westat.prog_areas.focus()
			return false
		}
	}
	
	//check if the statistical Software radio button is selected, if so, verify if an option was selected
	if (document.frm_contact_westat.send_msg_to[2].checked)
	{
		if (document.frm_contact_westat.stat_software.selectedIndex== 0)
		{
			alert ("Please select one of the Statistical Software options before submitting the form.")
			document.frm_contact_westat.stat_software.focus()
			return false
		}
	}
	
	return true
	
} //end function check_fields

function ValidatePhone(elem) {
	var pn = elem.value;
	if (pn == "") return;
	pn = pn.replace(/\D/g,"");

	//if (pn.length == 7)
		//elem.value = pn.replace(/(\d{3})(\d{4})/,"$1-$2");
	//else 
	if (pn.length == 10)
		elem.value = pn.replace(/(\d{3})(\d{3})(\d{4})/,"($1) $2-$3");
	else {
		alert('The telephone number must be a 10 digit phone number');
		elem.focus();
		return false;
		}
	}