/*
 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 : 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 : long = check for min length of the string
 attribute : month = check for value between 1 -12

*/



function checkform(form)
 {
 
 var valid = true;
 
 var RadioCheck = new Array();

 
 var form_elements = form.elements.length;
 
 var warrning = "";
 
 for(m=0; m<form_elements; m++)
  {
  element = form.elements.item(m);

  // check for empty fields

 
   attribute = element.getAttribute('empty');
  if(element.clientHeight > 0)  
   if(attribute)
   {
   if(empty_field(element))
    {
     warrning = warrning +  attribute+'\n';
     valid = false;
    }
    }

    
  // check for wrong email    
  
  attribute = element.getAttribute('email');
  if(element.clientHeight > 0)  
  if(attribute)
  if(wrong_email(element))
   {
    warrning = warrning +  attribute+'\n';
    valid = false;
   }    

  //check for numeric value
  
  attribute = element.getAttribute('numeric');
   if(element.clientHeight > 0)
    if(attribute)
  if(!is_numeric(element,0))
   {
    warrning = warrning +  attribute+'\n';
    valid = false;
   }    
 
  //check for real value
  
  attribute = element.getAttribute('real');
   if(element.clientHeight > 0)
  if(attribute)
  if(!is_numeric(element,1))
   {
    warrning = warrning +  attribute+'\n';
    valid = false;
   }    

  //check for a checked 
  attribute = element.getAttribute('checkit');
   if(element.clientHeight > 0)
  if(attribute)
   if(!element.checked)
    {
    warrning = warrning +  attribute+'\n';
    valid = false;
    }
 
 //check for month value
  
  attribute = element.getAttribute('month');
   if(element.clientHeight > 0)
  if(attribute)
   if(!month(element,1,12))
    {
    warrning = warrning +  attribute+'\n';
    valid = false;
    }

 //check for year value
  
  attribute = element.getAttribute('year');
   if(element.clientHeight > 0)
  if(attribute)
   if(!month(element,2005,4000))
    {
    warrning = warrning +  attribute+'\n';
    valid = false;
    }

  
  //check for minimun length
   attribute = element.getAttribute('long');        
   if(element.clientHeight > 0)
   if(attribute)
    if(!length(element,5))
    {
     warrning = warrning +  attribute+'\n';
     valid = false;
    } 
 
 
   // check for empty readio
   
     attribute = element.getAttribute('radiochoise');
       if(element.clientHeight > 0)
       if(attribute && !RadioCheck[element.name])
          if(!RadioChoise(element.name)){
	      RadioCheck[element.name] = 1
	          warrning = warrning +  attribute+'\n';
  	          valid = false;
      }

			 
  //check for not null value
    attribute = element.getAttribute('notnull');
       if(element.clientHeight > 0)
       if(attribute)
           if(element.value ==0 || element.value == null){
	        warrning = warrning +  attribute+'\n';
		     valid = false;
      }
			 
  
  
 }


 warrning = warrning+'';

 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 length(field,len)
 {
 
 if(field.value.length<len)
  return false;
  
 return true; 
 }
 
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 month(field,start,end)
 {
 
 if(!is_numeric(field,1))
  return false;
 
 if((field.value<start)||(field.value>end))
  return false;
   
 return true;
 }

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;
 }
 
 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
 }
		  
 
 
//calculate number of nights

 function NumNights(StringByLang)
  {
    var date1 = document.getElementById('check_in');
    var date2 = document.getElementById('check_out');
    
    if(date1.value != '' && date2.value != '')
     {
     
     Date1Array = date1.value.split("/");
     Date2Array = date2.value.split("/");
     
     
     newDate1 = new Date(Date1Array[2],Date1Array[1]-1,Date1Array[0]);
     newDate2 = new Date(Date2Array[2],Date2Array[1]-1,Date2Array[0]);     
     


     var difference = newDate2 - newDate1;


     var diffDays = Math.round(difference/86400/1000);
     
//     var diffDays = diffDate.getDate()-1;
     
     var nights = document.getElementById('lenStay');

    
        nights.innerHTML = '';     
	
     if(diffDays > 0)
      {
      
       nights.innerHTML = StringByLang + " " + diffDays;
      }
      

     }
    
  
  }
 