//msgs
var wrongMail = 'Oops ! wrong email  .';
var wrongName ='Oops ! wrong name  .';
var radioBoxWarn ='Plz Select a radio box';
var checkBoxWarn ='Plz Select a check box , at least : ';
var wrongSelect='Select any thing';
/*
valdation object
written by : Ahmad Shukr [2tech.ps]
@test on : FF 3.5 , IE 7 
*/
var validation = 
{
  
	  id:null,
	  getId : function()
	  {
		 return this.id;
	  },
	  
	  getObject : function()
	  {
		  return document.getElementById(this.getId());
	  } ,
	  
	  /*
		   @method checkFormInputs : loop through form elements , and check its field .
		   @para : no para
		   @return false on fail[form filed not sastifies the rule  , true on success 
								 
	  */	  
	  checkFormInputs:function()
	  {
		  //get form elements store it tags array
		  var tags = this.getObject().getElementsByTagName('*');
	      //init new counter
		  var i=0;
		  
		  var $return = true;
		  //indicates the first element has error to be focus
		  var firstErrorTag = '';
		  //loop through elements 
		  while( i < tags.length )
		  {
			//the current tag
	        var currentTag = tags[i] ;
			
			//the parent of the current tag
			var parent =  currentTag.parentNode ;
			
			//valid date the element tag with text
			if( ( currentTag.type === 'text'  || currentTag.type === 'file'  ) && currentTag.nodeName.toLowerCase()  === 'input'   )
			{
				 //get the tag value
				 var value = currentTag.value ;
				 
				 
				 //if check type email
				 if( currentTag.name.match(/email/i) )
				 { 
				      //invalid msg
					  var str=wrongMail;
				      //the email regExp patern
				      var emailPattern = /^\s*([\w\.]+)@([\w\.]+)\.([\w\.]+)\s*$/;
					  //if not ture append warn
					  var condtion = value.match( emailPattern ) ;
					  //show or hide the warn .
                      if( this.addRemoveWARN( condtion,parent,currentTag ,str) == false )
					  {
								 //set first error tag 
								 if( firstErrorTag   === '' )
								 {
									  firstErrorTag = currentTag;
								 }								    
								 $return=false;
					 }
						
 
				 }
				 else if( currentTag.name.match(/name/i) )
				 {
					  var str = wrongName;
				      //the email regExp patern
				      var namePattern = /^\s*[\sa-zA-zا-ي]{3,}[^\W0-9]*\s*$/;
					  //if not ture append warn
					  var condtion = value.match( namePattern ) ;
					  //show or hide the warn .
                      if( this.addRemoveWARN( condtion,parent,currentTag ,str) == false )
					  {
								 //set first error tag 
								 if( firstErrorTag   === '' )
								 {
									  firstErrorTag = currentTag;
								 }								    
								 $return=false;
					 }
 

				 }
				 
				 else if( currentTag.name.match(/number/i) )
				 {
					  var str = wrongName;
				      //the email regExp patern
				      var namePattern = /^\s*[0-9\+\-]{5,}\s*$/;
					  //if not ture append warn
					  var condtion = value.match( namePattern ) ;
					  //show or hide the warn .
                      if( this.addRemoveWARN( condtion,parent,currentTag ,str) == false )
					  {
								 //set first error tag 
								 if( firstErrorTag   === '' )
								 {
									  firstErrorTag = currentTag;
								 }								    
								 $return=false;
					 }
 

				 }
 				 else if( currentTag.name.match(/company/i) )
				 {
					  var str = wrongName;
				      //the email regExp patern
				      var namePattern = /^[\w\s]{3,}$/;
					  //if not ture append warn
					  var condtion = value.match( namePattern ) ;
					  //show or hide the warn .
                      if( this.addRemoveWARN( condtion,parent,currentTag ,str) == false )
					  {
								 //set first error tag 
								 if( firstErrorTag   === '' )
								 {
									  firstErrorTag = currentTag;
								 }								    
								 $return=false;
					 }
 

				 }
 				 else if( currentTag.name.match(/url/i) )
				 {
					  var str = wrongName;
				      //the email regExp patern
				      var namePattern = /^\s*(https?:\/\/)?www\.(\w){3,}\.(\w){2,}\s*$/;
					  //if not ture append warn
					  var condtion = value.match( namePattern ) ;
					  //show or hide the warn .
                      if( this.addRemoveWARN( condtion,parent,currentTag ,str) == false )
					  {
								 //set first error tag 
								 if( firstErrorTag   === '' )
								 {
									  firstErrorTag = currentTag;
								 }								    
								 $return=false;
					 }
 

				 }				 
			}  
			if( currentTag.name  )
			{
					if( currentTag.name.match(/select/i)  &&  currentTag.nodeName.toLowerCase()  ===  'select' )
						 {
							  var str = wrongSelect;
							  
							  var selectedIndex = currentTag.selectedIndex;
					 
							  var selectedIndexValue=currentTag.options[selectedIndex];
							  
							  var condtion = true ; 
					 
							   if( selectedIndex  === 0 )
							   {
								   condtion = false;						   	
							   }
							  
							  
							 if( this.addRemoveWARN( condtion,parent,currentTag ,wrongSelect) == false )
							 {
								 //set first error tag 
								 if( firstErrorTag   === '' )
								 {
									  firstErrorTag = currentTag;
								 }								    
								 $return=false;
							 }
							 
	 
					 } 

			 }
	 
			i++;
		  }
			     //focus first error tag if it has 
				 if( firstErrorTag   !== '' )
				 {
				   	  firstErrorTag.focus();
					  //if not select box
					  if( firstErrorTag.nodeName.toLowerCase() !== 'select' )
					  {
					     firstErrorTag.select();
					  }
				 }		    
		  return $return;
		  
	  },
	  
	  
	  
	  /*
		   @method radioBoxCheck : check radio box if one check
		   @para : no para
		   @return false on fail[form filed not sastifies the rule  , true on success 
								 
	  */		  
	  radioBoxCheck : function()
	  {
		  //get form elements store it tags array
		  var tags = this.getObject().getElementsByTagName('*');		  
	      //init new counter
		  var i=0;
		  
		  var lastRadioBox;
		  //detrmine if there is a radio box is check or n
		  var isThereRadioChecked=false;
		
		  var currentTag;
		  //loop through elements 
		  while( i < tags.length )
		  {
			//the current tag
	         currentTag = tags[i] ;
			
			 //if the current element is radio box
			  if(  currentTag.type === 'radio'  && currentTag.nodeName.toLowerCase()  === 'input'   )
			  {
				  //if is checked
				  if( currentTag.checked == true )
				  {
					   isThereRadioChecked=true;					  
				  }
 
			  }
			 i++; 
		  }
		  
	     // get the object , 
         var parent =this.getObject();
		 //the last tag set by the previous loop
		 var lastTag=currentTag;
		 
         var condtion =isThereRadioChecked ;
         this.addRemoveWarnRS(parent,condtion,lastTag,radioBoxWarn); 
 
	   return true;
	  } ,	
	  
	  
	  
	  /*
		   @method radioBoxCheck : check radio box if one check
		   @para numToBeChecked: how many check box should be checked
		   @return false on fail[form filed not sastifies the rule  , true on success 
								 
	  */	  
     checkBoxCheck : function(numToBeChecked)
	  {
		  //get form elements store it tags array
		  var tags = this.getObject().getElementsByTagName('*');		  
	      //init new counter
		  var i=0;
		  
		  var lastRadioBox;
		  //detrmine how many checkbox was checked 
		  var isThereCheckBoxChecked=0;
		
		  var currentTag;
		  
		  
		  //loop through elements 
		  while( i < tags.length )
		  {
			//the current tag
	         currentTag = tags[i] ;
			
			 //if the current element is radio box
			  if( currentTag.type === 'checkbox' && currentTag.nodeName.toLowerCase()  === 'input'   )
			  {
				  //if is checked
				  if( currentTag.checked == true )
				  {
					  isThereCheckBoxChecked++;					  
				  }
 
			  }
			  
			 i++; 
		  }
		 //the last tag 
         var lastTag=currentTag;
 	     // get the object 
         var parent   =   this.getObject() ;
		 //the condtion
		 var condtion =   ( isThereCheckBoxChecked >= numToBeChecked );
		 
          str = checkBoxWarn + ' ' + numToBeChecked;
		 
	     if( this.addRemoveWarnRS(parent,condtion,lastTag,str) == false )
		    return false;
	   
	   return true;
	  } ,	
	  
	  
	  
	  /*
		   @method removeWarn : append new child contain the warn
		   @para str : the warnning string
		   @para parent : the element wich contain the field  
		   @para sibling : the sibling of the wranning
		   @return void
	  */	  
	  appendWarn:function(str , parent , sibling )
	  {
		  
		  
			var warnDiv = document.createElement('div');
			warnDiv.className ='warn';
			warnDiv.innerHTML = str;
			
			//set color to yellow
			sibling.style.backgroundColor = '#F7E580';
			sibling.style.border = '1px solid red';
			
			
			//if it has a sibling
			if( sibling.nextSibling !== null )
			{
				if(  sibling.nextSibling.nodeName.toLowerCase()  !== 'div' )
				{  
					  //syntax:insertBefore(newElement, targetElement)
					  parent.insertBefore( warnDiv , sibling.nextSibling );			  
				}
			}
			//else
			else
			{
				 parent.appendChild(warnDiv);
			}
			
			
	  } ,
	  
	  
	  
	  
	  
	  /*
		   @method removeWarn : remove an previous warning 
		   @para sibling : the sibling of the wranning
		   @para parent : the element wich contain the field and the warn  
		   @return void	   
	  */
	  removeWarn:function( sibling , parent )
	  {   
	        //get the next sibling of the sibling 
            var nextSibling = sibling.nextSibling ;
			
			//the warn is set by method  appendWarn direct after the sibling 
			//with class name 'warn , so if the next sibling has className warn 
			//wee remove it from node tree page
			if(nextSibling  !== null )
			{
				if(  nextSibling.className === 'warn' )
				{  
					  //remove it
					  parent.removeChild( nextSibling );
					  
					  //reset the color i.e. remove the warn
					  sibling.style.backgroundColor = '';
					  sibling.style.border = '';
				}
			}
			
	  },
	  
	  addRemoveWARN:function(condtion,parent,currentTag,str)
	  {
		  
		  if( condtion ===  false  || condtion ===  null  )
		  {
			 if( currentTag.title  && currentTag.nodeName.toLowerCase() ==='input' && currentTag.type === 'text' )
			 {
				 str=' الرجاء ادخال ' +'<strong>'+currentTag.title +'</strong>'+ ' بشكل الصحيح ';
				 this.appendWarn(str,parent,currentTag);  
				 return false;
			 }			 
			 else if( currentTag.title  && currentTag.type === 'file' && currentTag.nodeName.toLowerCase() ==='input' )
			 {
				 str=' الرجاء اختيار :' +'<strong>'+currentTag.title +'</strong>.' ;
				 this.appendWarn(str,parent,currentTag);  
				 return false;
			 }
			 else if( currentTag.title  && currentTag.nodeName.toLowerCase() === 'select' )
			 {
				 str=' الرجاء اختيار :' +'<strong>'+currentTag.title +'</strong>'+ ', من القائمة . ';
				 this.appendWarn(str,parent,currentTag);  
				 return false;
			 }

			 else
			 {
				 this.appendWarn(str,parent,currentTag);  
				 return false;				 
			 }
		  }
		  //remove possible warn was added , if the email true from the first 
		  //removeWarn will not do any thing
		  else
		  {
			 this.removeWarn(currentTag,parent);  
			 return true;
		  }
					    
		  
	  },
	  addRemoveWarnRS:function(parent,condtion,lastTag,str)
	  {
 
		  
		 //if there is no radio checked
         if( condtion === false )
		 {
	       //if the warn not added before
		   if( lastTag.className !== 'warn' )
		   {
			 parent.style.border = '1px solid red';
		     var warnDiv = document.createElement('div');
		     warnDiv.className ='warn';
		     warnDiv.innerHTML = str;
		     parent.appendChild( warnDiv  );
			 
		   }
		   
		   
		   return false;
		 }
		 //else if remove the warn if exsists
		 else  if(lastTag.className === 'warn' )
		 {
			 parent.removeChild(lastTag);
			 parent.style.border = '';
			 return true;
		 }
   
	  }
 
 
}
 