/*
 The script is created by  Mariyan Mladenov  http://slackrover.com  26 Fub 2006.
 
 Short instructions :
 
 To check for empty field use attribute empty.
 The value of the attribute will appear in alert.
 
 Example:
 <input type="name" empty="First Name">.
 if this field is empty will appear alert: Please check your First Name.
 
 Other options:
 
 attribute : empty - check for empty field.
 attribute : radiochoise - check for empty radio.
 attribute : email - check for valid email.
 attribute : numeric - check for numeric value.
 attribute : real - check for real number value.
 attribute : checkit = check for checked checkbox.
 attribute : notnull = check for null or 0 values.
*/




function checkform(form)
 {
 
 var valid = true;
  

 var form_elements = form.elements.length;
 
 var warrning = "Incorect Fields:\n ------------------------------------\n ";

 var RadioCheck = new Array();

 for(m=0; m<form_elements; m++)
  {
  element = form.elements.item(m);
  
  // check for empty fields
  
  attribute = element.getAttribute('empty');

  if(element.clientHeight == 0)  
   continue;
  
  if(attribute)
   if(empty_field(element))
    {
     warrning = warrning +  attribute+'\n ';
     valid = false;
    }

  // check for not null fields
  
  attribute = element.getAttribute('notnull');
  if(attribute)
   if(element.value == 0) 
    {
     warrning = warrning +  attribute+'\n ';
     valid = false;
    }

    
  // check for empty readio
  
  attribute = element.getAttribute('radiochoise');
  if(attribute && !RadioCheck[element.name])
   if(!RadioChoise(element.name)){    
    RadioCheck[element.name] = 1
    warrning = warrning +  attribute+'\n ';
    valid = false;
   }
   
    
  // check for wrong email    
  
  attribute = element.getAttribute('email');
  if(attribute)
  if(wrong_email(element))
   {
    warrning = warrning +  attribute+'\n ';
    valid = false;
   }    
 
  //check for numeric value
  
  attribute = element.getAttribute('numeric');
  if(attribute)
  if(!is_numeric(element,0))
   {
    warrning = warrning +  attribute+'\n ';
    valid = false;
   }    
  
  //check for real value
  
  attribute = element.getAttribute('real');
  if(attribute)
  if(!is_numeric(element,1))
   {
    warrning = warrning +  attribute+'\n ';
    valid = false;
   }    
    
  //check for a checked 
  attribute = element.getAttribute('checkit');
  if(attribute)
   if(!element.checked)
    {
    warrning = warrning +  attribute+'\n ';
    valid = false;
    }
      
  
 }
 
 warrning = warrning.substring(0,warrning.length-2);
 warrning = warrning+'\n ------------------------------------';
 
 if(!valid)  
   alert(warrning);
   
 
 return valid;
 }
 
function empty_field(field)
 {
 
 
 if((field.value == null)||(field.value.length == 0)||(field.value.charAt(0)==" "))
  return true;
  
   
 return false;
 } 
 
function RadioChoise(ElName){

 var choise = false;
    RadioEl = document.getElementsByName(ElName);
    for(cR = 0; cR < RadioEl.length; cR++){
     if(RadioEl[cR].checked == true)
      choise = true
    }  
    
 return choise
} 
 
function wrong_email(field)
 {
 var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

 if (!filter.test(field.value)) 
  return true 
 
 return false;
 } 

function is_numeric(field,type)
 {
 var filter;
 if(type == 0)
  filter  = "0123456789";
 if(type == 1)
  filter  = "0123456789.-";
    
 if(empty_field(field))
  return false; 
  
 var ch;
 var count = 0;
  
 for(s=0; s<field.value.length; s++)
  {
  ch = field.value.charAt(s);
  if(ch == "-")
   count ++;
  if(filter.indexOf(ch)<0)
   return false;
  } 
 
 if(field.value.indexOf("-")>0)
  return false;
  
 if(count>1)
  return false; 
    
 return true;
 }
