//**********************************************
////////////////////////////////////////////////
//WARNING
////////////////////////////////////////////////
//**********************************************

    //A utility function that returns true if a string contains only a whitespace
	//characters
	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 verify the value be a integer 
	function isinteger(s)
	{
		
		 var checkOK = "0123456789";
		  var checkStr = s; 
		  var allValid = true;
		  var decPoints = 0;
		  var allNum = "";
		  for (i = 0;  i < checkStr.length;  i++)
		  {
		    ch = checkStr.charAt(i);
		    for (j = 0;  j < checkOK.length;  j++)
			{
		      if (ch == checkOK.charAt(j))
		        break;
			}
		    if (j == checkOK.length)
		    {
		      allValid = false;
		      break;
		    }
		    allNum += ch;
		  }
		  if (!allValid)
		  {
		   return false;
		  }
		  return true;
	}
	
	
	//function verify the value be a decimal 
	function isdecimal(s)
	{
		
		 var checkOK = "0123456789.";
		  var checkStr = s; 
		  var allValid = true;
		  var decPoints = 0;
		  var Points=0;
		  var allNum = "";
		  for (i = 0;  i < checkStr.length;  i++)
		  {
		    ch = checkStr.charAt(i);
		    for (j = 0;  j < checkOK.length;  j++)
			{
		      if (ch == "." && checkOK.charAt(j)==".")
			  	{   Points++;		
				}
			  if (ch == checkOK.charAt(j))
		        break;
			}
		    if (j == checkOK.length)
		    {
		      allValid = false;
		      break;
		    }
			if (Points>1)
			{
			  allValid = false;
		      break;
			}
		    allNum += ch;
		  }
		  if (!allValid)
		  {
		   return false;
		  }
		  return true;
	}
	
	
	//function validate that the value is  date with mask, probably is missing another mask 
	function isdate(s)
	{
		if (!(check_mask("##/##/####",s)||check_mask("##/##/##",s)||check_mask("#/##/##",s)||check_mask("##/#/##",s)||check_mask("####/##/##",s)||check_mask("#/#/##",s)||check_mask("#/#/####",s)))
		{
			return false;
		}
		return true;
	}

	//function validate that the value is  date with mask, probably is missing another mask 
	function  check_mask(mask,value)
	{
		var i;
		if (value.length != mask.length) 
		{
			return false;
		}
		for (i=0; i<mask.length; i++)
		{
			if ( mask.charAt(i) == '#' )
			{
				if ( !parseInt(value.charAt(i)) && value.charAt(i) != '0' )
				{
					return false;
				}
			}
			else
			{
				if (mask.charAt(i) != value.charAt(i))
				{
					return false;
				}
			}
		}
		return true;
	}
	
	function isemail(email)
	{
		invalidChars = " /:,;"
	
		// Check if it's empty			
		if (email=="")
			return false;
		
		if (email==null)
			return false;
		
		if (isblank(email))
			return false;
		
		// Check if it has invalid characters
		for (i=0; i<invalidChars.length; i++)
		{
			badChar = invalidChars.charAt(i)
			if (email.indexOf(badChar,0)>-1)
			{	
				return false; 
			}
		}
		
		// Check if it contains an @
		atPos = email.indexOf("@",1)
		if (atPos==-1)
			return false;
			
		// only one @
		atPos = email.indexOf("@",atPos+1)
		if (atPos>-1)
			return false;
		
		// Check if it contains a period after @
		periodPos = email.indexOf(".", atPos)
		if (periodPos==-1)
			return false;
			
		// At least 2 characters after the period
		if (periodPos+3 > email.length)
			return false;
			
		// everything ok
		return true;
	}

	//review each field of the form sent
	function verify(f)
	{
		var msg;
		var empty_fields = "";
		var errors=""; 	
		
		
	  
		
		for(var i=0;i<f.length;i++)
		{
			//alert (e.name);
			var e=f.elements[i];
				
			if(((e.type=="text")||(e.type=="textarea") ||(e.type=="password") ) && e.optional!=1)
			{
				//first check if the field is empty
				if((e.value==null) || (e.value=="") || isblank(e.value))
				{
                   if(e.description!=null && e.description!="")
                    {                                                        
                        empty_fields+="\n        " + e.description;;
                    }
                    else
                    {                                              
                        empty_fields+="\n        " + e.name;
                    }
					continue;
				}
			}	
			if (e.date && !((e.value==null) || (e.value=="") || isblank(e.value)))
			{
				
				if(!isdate(e.value))
				{
				 errors+="The field " + e.name + " must be a valid date.\n";
				}
			}
			
			
			if (e.email && (!isemail(e.value)))
			{
			    errors+="Please enter a valid Email for the \"email\" field.\n";
			}
			
			//check for select
            if((e.type=="select-one" || e.type=="select-multiple") && !e.optional)
            {
                
                if(e.selectedIndex == -1)
                {
                    if(e.description!=null && e.description!="")
                    {
                        empty_fields+="\n        " + e.description;
                    }
                    else
                    {
                        empty_fields+="\n        " + e.name;
                    }
					
					continue;
                }
                else
                {
                    if(e.options[e.selectedIndex].value=="")
                    {
                        if(e.description!=null && e.description!="")
                        {
                            empty_fields+="\n        " + e.description;
                        }
                        else
                        {
                            empty_fields+="\n        " + e.name;
                        }
						
    					continue;
    				}
                }
            }
			
			//now check for fields that are supposed to be numeric
			if(e.numeric || (e.min!=null) || (e.max!=null) || e.integer || e.decimal)
			{
				//validate of integer
				if (e.integer)
				{
					if (!isinteger(e.value))
					{
						 errors+="The field " + e.name + " must be integer.\n"
					}
				}
				//validate of decimal
				if (e.decimal)
				{
					
					if (!isdecimal(e.value))
					{
						 errors+="The field " + e.name + " must be decimal numeric.\n"
					}
				}
				
				var v = parseFloat(e.value);
				if ((e.min) || (e.max))
				{
					if(isNaN(v) || ((e.min!=null)&&(v<e.min)) || ((e.max!=null)&&(v>e.max)))
					{
						errors+="- The field " + e.name + " must be numeric";
						if(e.min!=null)
							errors+=" mayor than" + e.min;
						if(e.min!=null && e.max!=null)
							errors+=" minor than " + e.max;
						else if(e.max!=null)
							errors+=" minor than " + e.max;
						errors+=".\n";
						
					}
				}
			}
		}	


		//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)
			return true;
		
		msg =  "-------------------------------------------------------------\n\n";
		msg += "The information did not send because of these error(s).\n";
		msg += "Please correct these error(s) and try again.\n";
		msg += "-------------------------------------------------------------\n\n";

		if (empty_fields)
		{
			msg += " - These fields are empty:" + empty_fields + "\n";
			if(errors) msg += "\n";
		}

		msg += errors;
		alert(msg);	
		return false;
	}