 /******  Global extensions on strings *************/
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

String.prototype.erase=function(blocks)
{ 
      var victim=this;
	  
	  blocks=blocks.split(" ");
	   for(i=0;i<=blocks.length-1;i++)
			 victim=victim.replace(blocks[i],""); 
		   return victim; 
	}





//********* A CLASS HIVE *************************///

function Class(){  
  return function(args){ 
    if ( this instanceof arguments.callee ) {
      if ( typeof this.construct == "function" )
	     if(args.callee)
              this.construct.apply( this, args);
	     else 	
		      this.construct.apply( this, arguments);
    }
	else
	{ 	
        return  new arguments.callee( arguments );           
	}
  }
}; 


var GuruFactor=new Class();


/* constructor */
GuruFactor.prototype.construct=function()
{ 
	}

//gets the node type
GuruFactor.prototype.getType=function(node)
   {
	      return $("#"+node).attr("nodeName");
	   }


/* creates an object out of a string like: prop1=val1,prop2=val2 */
GuruFactor.prototype.objectize=function(wannabe)
	{  
	    if(wannabe==null) return new Object();
	
		wannabe=wannabe.split(",");
		
		var the_object=new Object();
		
		for(var i=0;i<=wannabe.length-1;i++)
		 {    
		     var chunks=wannabe[i].split("=");
			 
			   the_object[chunks[0]]=chunks[1];
			 }
		
		return the_object;
		}
		
/* searches for input type=text|select|textarea nodes in the children of a given id .
  returns a string separated by &, url escaped       */
GuruFactor.prototype.findNodesUri=function(node,target_type)
{  
	 var found=new Array();
	 var params="";
	
	 if(typeof node=="string") node_data=document.getElementById(node).getElementsByTagName("input"); 
	 
	       if(node_data.length>=1)
		    {
				for(var i=0;i<=node_data.length-1;i++)
				  if(node_data[i].type=="text" || node_data[i].type=="hidden"  || node_data[i].type=="password" || node_data[i].type=="checkbox" || node_data[i].type=="radio"  || node_data[i].type=="button" || node_data[i].type=="submit" || node_data[i].type=="image" )
				   { 
				     if(node_data[i].type!="checkbox" && node_data[i].type!="radio") 
					  {
						 if((target_type!=null && target_type==node_data[i].type) || target_type==null) 
				           params+="&"+node_data[i].name+"="+node_data[i].value;
					  }
					  else
					  { 
						 if((target_type!=null && target_type==node_data[i].type) || target_type==null) 
						    if(node_data[i].checked) 
							   params+="&"+node_data[i].name+"="+node_data[i].value; 
						  }
				   }
  
				   
			} 
      
	 node_data=new Array();			
	 if(typeof node=="string") node_data=document.getElementById(node).getElementsByTagName("select"); 
	       if(node_data.length>=1)
		    { 
				for(var i=0;i<=node_data.length-1;i++)
					 if((target_type!=null && target_type==node_data[i].type) || target_type==null) 
				       params+="&"+node_data[i].name+"="+node_data[i].value;
					  
				   
			} 	
			
	 node_data=new Array();			
	 if(typeof node=="string") node_data=document.getElementById(node).getElementsByTagName("button"); 
	       if(node_data.length>=1)
		    { 
				for(var i=0;i<=node_data.length-1;i++)
					 if((target_type!=null && target_type==node_data[i].type) || target_type==null) 								
				      params+="&"+node_data[i].name+"="+node_data[i].value;
					
				   
			} 	
	
	 node_data=new Array();			
	 if(typeof node=="string") node_data=document.getElementById(node).getElementsByTagName("textarea"); 
	       if(node_data.length>=1)
		    { 
				for(var i=0;i<=node_data.length-1;i++){
						 if((target_type!=null && target_type==node_data[i].type) || target_type==null) 								
						    params+="&"+node_data[i].name+"="+node_data[i].value;
				}
					
				   
			} 		
			
	
			
		return params;
	}

/* limits the text based on a number of words or characters */
GuruFactor.prototype.limitText=function($obj,$type,limit,print_id)
{    
  
  var target="";
      
	  if(document.getElementById($obj)!=null) target=document.getElementById($obj);
                                      else  
									  {
										      alert("invalid target for text limiting!"); 
									          return false;
                                       }
  if($type=="chars")
	var nrc=target.value.length;
 else  if($type=="words")
   {
	  var tmp=target.value.split(" ");
	  var nrc=tmp.length;
	   } 
	var ramase=limit-nrc;
	if(ramase<=0)
	{
	var text_redus=target.value.substr(0,limit);
	this.obj.value=text_redus;
	ramase=limit;
     }
    if(engine.getIt(print_id).readOnly ==false)	 document.getElementById(print_id).readOnly =true;  
	var afis=engine.getIt(print_id);
	afis.value=ramase+'/'+limit;
	 
	}	

/* cheks and un-checks allt checkboxes in a container */
GuruFactor.prototype.checkAll=function($container,exclude)
  {
	    $("#"+$container).find("input").each(function(index){
											
											if($(this).attr("id")!=exclude.id)
											if($(this).attr("checked")==false) $(this).attr("checked",true);	
											                             else $(this).attr("checked",false);	
														 });
		
	  }

/*  checks wheter a given value is integer */
GuruFactor.prototype.isInteger=function(s)
   {
      var i;

      if (this.isEmpty(s))
      if (this.isInteger.arguments.length == 1) return 0;
      else return (this.isInteger.arguments[1] == true);
      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!this.isDigit(c)) return false;
      }

      return true;
   }
   
/*  checks wheter a given value is empty */   
GuruFactor.prototype.isEmpty=function(s)
   {
      return ((s == null) || (s == "") || (s.length == 0))
   }
   
 /*  checks wheter a given value is a digit */    
GuruFactor.prototype.isDigit=function(c)
   {
      return ((c >= "0") && (c <= "9"))
   }

/* return a 8 characters random string  */
GuruFactor.prototype.randomString=function() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 8;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
		var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}	


var gf=new GuruFactor("0");	
