/******************************************************************************
* utils.js
*
* Funcions varies genèriques utilitzables a qualsevol lloc i independents de
*	la base de dades o d'una aplicació en concret.
*
*	Albert Sunyer @ 2008
******************************************************************************/

function isMail(txt){ 
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	return filter.test(txt);
}

function isURL(txt){ 
	var filter  = /^http|https/;
	
	return filter.test(txt);
}

function isDate(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);
}

function right(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);
	}
}

function mid(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);
}

function focusit(c){
	document.getElementById(c).focus();
}


var myWin; 

function popup(url, id, w, h, scroll){
	if(!myWin || myWin.closed){
		myWin = 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{
		myWin.focus();
	};
	
	void(0); 
}

function popupNoToolsFull(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);
}