/******************************************************************************
* utils.js
*
* Funcions varies genèriques utilitzables a qualsevol lloc i independents de
*	la base de dades o d'una aplicació en concret.
*
* UTILS.isMail('xxxx');
*
*	Albert Sunyer @ Soluciones Multimedia Tec (SMTec) 2009 | http://www.smtec.es
******************************************************************************/

var UTILS={
	
	isMail:function(txt){
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		
		return filter.test(txt);
	},
	
	isURL:function(txt){
		var filter  = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
		
		return filter.test(txt);
	},
	
	isDate:function(txt){
		var filter = /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/;
		
		return filter.test(txt);
	},
	
	right:function(str, n){
		/***
		IN: str - the string we are RIGHTing
				n - the number of characters we want to return
		
		RETVAL: n characters from the right side of the string
		***/
		
		if(n<=0) return ""; // Invalid bound, return blank string
		else if(n>String(str).length) return str; // Invalid bound, return entire string
		else { // Valid bound, return appropriate substring
			 var iLen=String(str).length;
			 return String(str).substring(iLen, iLen-n);
		}
	},
	
	mid:function(str, start, len){
		/***
		IN: str - the string we are LEFTing
				start - our string's starting position (0 based!!)
				len - how many characters from start we want to get
		
		RETVAL: The substring from start to start+len
		***/
		// Make sure start and len are within proper bounds
		if(start<0 || len<0) return "";
	
		var iEnd, iLen=String(str).length;
		
		if(start+len>iLen) iEnd=iLen;
		else iEnd=start+len;
	
		return String(str).substring(start,iEnd);
	},
	
	WIN_POPUP:null,
	popup:function(url, id, w, h, scroll){
		if(!WIN_POPUP || WIN_POPUP.closed){
			WIN_POPUP = window.open(''+url+'',''+id+'','width='+w+',height='+h+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scroll+',resizable=yes,top='+((screen.height/2)-(200))+',left='+((screen.width/2) - (150)) + '');
		}else{
			WIN_POPUP.focus();
		};
		
		void(0); 
	},
	
	popupNoToolsFull:function(url, nom, scroll){
		settings='height='+screen.availHeight+',width='+screen.availWidth+',top=0,left=0,scrollbars='+scroll+',resizable,toolbar=no,status=no,resizable=yes';
		win=window.open(url, nom, settings);
	}
}
