/******************************************************************************
* 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 @ SMTec 2009 | http://www.smtec.es
******************************************************************************/

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);
}

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);
}

/**********************************************
*	Función para mostrar mensajes tipo Growl
*	msg = mensaje a mostrar
*	tip = tipo de mensaje ok, ko, null
**********************************************/
function setGrowl(msg,tip) {
	if (tip=='ok') { 
			$.bGrowl({message: msg,icon:'ui-icon-circle-check',timeout:5,className: 'bGrowlSuccess'});
	}else if (tip=='ko') {
		$.bGrowl({message: msg,icon:'ui-icon-alert',timeout:5,className: 'bGrowlFail'});
	}else{
		$.bGrowl({message: msg,timeout:5});
	}
}
// Valida un campo si es obligatorio
function valReq(id) {
	var filter  =  /\S/;
	if (!filter.test($(id).val())) {
		$(id).fadeTo("fast", 0.1, function() { 
			$(this).addClass("tberror");
			$(this).fadeTo("slow", 1);
		});
		return false;
	}else{
		$(id).removeClass("tberror");
		return true;
	}
}
// Valida un campo si es obligatorio y además una dirección de correo valida
function valMail(id) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test($(id).val())) {
		$(id).fadeTo("fast", 0.1, function() { 
			$(this).addClass("tberror");
			$(this).fadeTo("slow", 1);
		});
		return false;
	}else{
		$(id).removeClass("tberror");
		return true;				
	}
}
// Resetea un formulario --> $('form').clearForm()
$.fn.clearForm = function() {
	return this.each(function() {
	  var type = this.type, tag = this.tagName.toLowerCase();
	  if (tag == 'form')
		return $(':input',this).clearForm();
	  if (type == 'text' || type == 'password' || tag == 'textarea')
		this.value = '';
	  else if (type == 'checkbox' || type == 'radio')
		this.checked = false;
	  else if (tag == 'select')
		this.selectedIndex = -1;
	});
};
