
  // check blank spcace
  function chkblank(field,opt,msg1,msg2) // field, opt (0,1) 1 mean require to enter, msg1=entire line is blank (if msg1 is blank,it will not display message and just return false), msg2=field is not enter any character
  {

	if (field.value != "")
	{
		var textlength=field.value.length; // the total length of the text
		var numofspace=0; // inital of the number of space
		for (i=0;i<field.value.length;i++)
		{
			if (field.value.charAt(i) == " ") //See if the string position is space
			{
				numofspace=numofspace+1; // add 1 to the number of space
			}
			else
				{
					break; // no need to carry on to search since it found a non-space charaters
				}
		}
		
		if (numofspace==textlength) // if length of the text equal the total number of space in the text (entire text is space)
		{
			field.focus();
			if (msg1 != "")
			{
				alert(msg1);
			}
			return false;
		} 	
		
	}
	else
	    if (opt==1) 
			{
				field.focus();
				if (msg2 != "")
				{
   					alert(msg2);
				}
   				return false;
			}
		
 
  }


  // check if the address is contain . , & #
  function chkaddress(address,msg1)
  {
  	comma_pos=address.value.indexOf(",")
  	dot_pos=address.value.indexOf(".")
	pound_pos=address.value.indexOf("#")
	plus_pos=address.value.indexOf("+")
  	if(comma_pos >0 || dot_pos >0 || pound_pos >0 || plus_pos>0) 
	{
   		address.focus();
		alert(msg1);
		return false;
	}
  }    
  
  // check if the city is contain . , 
  function chkcity(city,msg1)
  {
  	comma_pos=city.value.indexOf(",")
  	dot_pos=city.value.indexOf(".")
  	if(comma_pos >0 || dot_pos >0) 
	{
   		city.focus();
		alert(msg1);
		return false;
	}
  }     
  
  // check if the email address is contain @ & .
  function chkemailaddress(email,msg1)
  {
  	at_pos=email.value.indexOf("@")
  	dot_pos=email.value.indexOf(".")
  	if(at_pos<1 || dot_pos<1) // @ & . cannot in the first postion
	{
   		email.focus();
		alert(msg1);
		return false;
	}
  }  


//--------------------------------- Validate the form -------------------------------------
	 
  // Validate the form
  function chk(testform)
  {

    // when select LA health check if the currently receiving LA health radio button is both uncheck
	if (testform.reportchoice1.checked)
	{
		if (! testform.oldyesno[0].checked && ! testform.oldyesno[1].checked)
		{
			testform.oldyesno[1].focus();
			alert("Please select Yes or No for currently receiving 'LA Health'!");
			return false;
		}
	}
  
  	// if user did not select the report, stop and go back
	if (! testform.reportchoice1.checked && ! testform.reportchoice2.checked && ! testform.reportchoice3.checked && ! testform.reportchoice4.checked)
	{
		//testform.fname.focus();
		testform.reportchoice1.focus();
	   	alert("Please select one of the reports.");
	   	return false;	
	}

  	// if first name or last name is blank, check if the org is blank
    if (testform.fname.value == "" || testform.lname.value == "")
	{ 
		if (testform.org.value == "")
		{
			testform.fname.focus();
	   		alert("Please enter First name and Last name or just Organization.");
	   		return false;
		}
		else
		    {
				// check organization if it is blank
				result= chkblank(testform.org,1,"Organization cannot be blank.","Please enter organization.")
				if (result == false) return false;					
			}
   	}
	else
		{
			// check first name if it is blank
			result= chkblank(testform.fname,1,"First name cannot be blank.","Please enter first name.")
			if (result == false) return false;
		
			// check first name if it is blank
			result= chkblank(testform.lname,1,"Last name cannot be blank.","Please enter last name.")
			if (result == false) return false;
		
			// if type in name and organization, still need to check the organization type
			if (testform.org.value != "")
			{
				// check if organization type is blank
				result= chkblank(testform.org,0,"Organization cannot be blank.","Please enter organization.")
				if (result == false) return false;
			}	
		}

	 
	 // check if organization type is blank
	 resulttypeorg= chkblank(testform.typeorg,1,"","")
	 resulttypeorgother= chkblank(testform.typeorgother,1,"","")					
	 if (resulttypeorg == false && resulttypeorgother== false)
	 {
		testform.typeorg.focus()
		alert("Please select the organization type or specify another organization type.");
		return false;
	 }	 
	 else
	 	if (resulttypeorg != false && resulttypeorgother != false)
	  	{
			testform.typeorg.focus()
			alert("Please either select the organization type or specify another organization type.");
			return false;
		}
	 // check the address1 if it is blank
	 result= chkblank(testform.address1,1,"Address1 cannot be blank","Please enter address1.")
	 if (result == false) return false;

	 // check the address1 contain ,.#
	 result= chkaddress(testform.address1,"Address1 cannot contain ',' '.' '+' or '#'.")
	 if (result == false) return false;	 
	  
	 // check the city if it is blank
	 result = chkblank(testform.city,1,"City cannot be blank.","Please enter city.")
	 if (result == false) return false;
	 
	 // check the city contain ,.
	 result= chkcity(testform.city,"City cannot contain ',' or '.'.")
	 if (result == false) return false;	 	 
	 
	 // check the state if it is selected
	 //result = chklist(testform.state,1,"Please select the state.")
	 result = chkblank(testform.state,1,"State cannot be blank.","Please select the state.")
	 if (result == false) return false;	 
	 
	 // check the zip code if it is blank
	 result = chkblank(testform.zip,1,"Zip code cannot be blank.","Please enter zip code.")
	 if (result == false) return false;

	 // check if the specify department is selected
	 if (testform.messenger.checked)
	 {	
		
		if (testform.other.value == "")
		{
			result = chkblank(testform.department,1,"Department cannot be blank.","Please select the department.");
			if (result == false) return false;
		}
		
		if (testform.department.value =="")
		{
			result = chkblank(testform.other,1,"You must select a department or enter another.","Please enter other.");
			if (result == false) return false;
		}
		
		// if both the department and the other have value, stop and return
  		if (testform.other.value != "" && testform.department.value !="")
		{
			result = chkblank(testform.other,0,"","");
			if (result != false)
			{ 
				alert("Please only select the department or just enter the other.");
				return false;
			}
		}
		
	 }
	 else // check if they did not check the box but type in the text box
	 	{
			// if they type other but did not click the check box
			if (testform.other.value !="")
			{
				result = chkblank(testform.other,0,"","");
				if (result != false)
				{ 
					alert("Please select the check box of LA county messenger mail.");
					return false;
				}
			}
			
			// if they select the department but did not click the check box
			if (testform.department.value !="")
			{
			alert("Please select the check box of LA county messenger mail.");
			return false;		
			}
		}

	
	 // if email is entered, make sure it contain @ & .
	 if (testform.email.value != "")
	 {
	   	//check the email if it is blank
	 	result = chkblank(testform.email,0,"Email address cannot be blank.","")
	 	if (result == false) return false;
	   
	 	result = chkemailaddress(testform.email,"Missing '@' and '.' in email address.")
		if (result == false) return false;
	 }
	 
	 // check the code if it is blank
	 result = chkblank(testform.code,1,"Code cannot be blank.","Please enter the code")
	 if (result == false) return false;
	 
	 
  }
