//----------------- QUITAR ESPACIOS EN BLANCO ---------------------------
function trim(s) 
{
	var temp="";
  	for (var i=0; i<s.length; i++)
    {
	 	if (s.substring(i,i+1) == ' ')
			var o=0;	     
		else
		  temp = temp+s.substring(i,i+1);	 
	}
  	return temp;
}


// ----- VALIDAR CARACTERES ESPECIALES -----
function caracteres_especiales(text)
{
	var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
	var cont=0;
  	for (var i = 0; i < text.value.length; i++) {		
		if (iChars.indexOf(text.value.charAt(i)) == -1) {
			var a=1;
  		}
		else
			cont++;
  	}

	if(cont>0)
		return false;
	else
		return true;
	
}

// ----- VALIDAR BUSQUEDAD -----
function validar_busqueda(f)
{
	if (f.buscar.value.length==0 || !trim(f.buscar.value))
	{	
		alert("Introduzca una palabra para realizar la busqueda");
		f.buscar.value="";
		f.buscar.focus();
		return false;
	}
	else
		return true;
}


//------- ALERT PARA CONFIRMAR ELIMINAR -----
function confirmar_eliminar(texto){
	return(confirm(texto));
}

//---- VALIDA QUE SOLO SE INTRODUZCAN NUMEROS EN UNA CAJA DE TEXTO ----
function EvaluateText(cadena, obj, e,coma){
opc = false;
//alert("e.keyCode"+e.keyCode+"e.which".e.which);
tecla = (document.all) ? e.keyCode : e.which;
//alert(tecla+" cadena "+cadena);
if (cadena == "%d")
	if (tecla > 47 && tecla < 58)
		opc = true;
if (cadena == "%f")
{
	if(coma)
	{
		if (tecla > 47 && tecla < 58 || tecla==8 || tecla==0 || tecla==44)
			opc = true;
		if (obj.value.search("[.*]") == -1 && obj.value.length != 0)
			if (tecla == 46)
				opc = true;
	}
	else
	{		
		if (tecla > 47 && tecla < 58 || tecla==8 || tecla==0)
			opc = true;
		if (obj.value.search("[.*]") == -1 && obj.value.length != 0)
			if (tecla == 46)
				opc = true;
	}
}
return opc;
}

//----- Funcion para validar que haya algun item seleccionado para ser borrado --------
function anyCheck(f,mens)
{
	if(f.elements["ids[]"].length){
		for (i = 0; i < f.elements["ids[]"].length; i++){			
			if(f.elements["ids[]"][i].checked==true)
				return true;
		}		
	}
	else{
		if (f.elements["ids[]"].checked) 
			return true;		
	}
	alert("Debes seleccionar "+mens+" que desea eliminar");
	return false;
}


//----- VALIDAR FILE -----
function validar_file(form,file,img2val,mens)
{
	extArray = new Array(".gif", ".jpg", ".png");
	allowSubmit = false;
	if (!file && img2val=="")
	{
		alert(mens);
		//file.focus();
		return false;
	}
	else
	{
		if(img2val=="")
		{
			while (file.indexOf("\\") != -1)
		file = file.slice(file.indexOf("\\") + 1);
		ext = file.slice(file.indexOf(".")).toLowerCase();
		for (var i = 0; i < extArray.length; i++) {
			if (extArray[i] == ext) { allowSubmit = true; break; }
		}
		if (!allowSubmit)
		{
			alert("Solo puedes seleccionar archivos:  " 
		+ (extArray.join("  ")) + "\nPor favor selecciona otro archivo.");
		return false;
		}		
		}
	}	
}

//----- ABRIR POPUP -----
function popup(url,nb_vent,atributos,w,h)
{
	
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	
	atributos=atributos+',top='+Math.round(TopPosition)+',left='+Math.round(LeftPosition);
	window.open(url,nb_vent,atributos);
	
}

//----- VALIDAR FECHA DESDE > FECHA HASTA -----
function comparaFecha(dFormat,dFecMenor, dFecMayor){
	dFecMenor = invFecha(dFormat,dFecMenor);
	dFecMayor = invFecha(dFormat,dFecMayor);

	if(dFecMenor > dFecMayor)
		return false;
	else
		return true;
}
function invFecha(nTipFormat,dFecIni){
	var dFecIni = dFecIni.replace(/-/g,"/");					// reemplaza el - por /	
	
	// primera division fecha
	var nPosUno  = ponCero(dFecIni.substr(0,dFecIni.indexOf("/")));
	// 2º divicion fecha
	var nPosDos  = ponCero(dFecIni.substr(parseInt(dFecIni.indexOf("/")) + 1,parseInt(dFecIni.lastIndexOf("/")) - parseInt(dFecIni.indexOf("/")) - 1));
	// 3º divicion fecha
	var nPosTres = ponCero(dFecIni.substr(parseInt(dFecIni.lastIndexOf("/")) + 1));

	switch(nTipFormat){
		case 1 :	//	DD/MM/YYYY
			dReturnFecha = nPosTres + "" + nPosDos + "" + nPosUno;
			break;

		case 2 :	//	MM/DD/YYYY
			dReturnFecha = nPosTres + "" + nPosUno + "" +nPosDos;
			break;

		case 3 :	//	YYYY/MM/DD
			dReturnFecha = nPosUno + "" + nPosDos + "" +nPosTres;
			break;
	
		case 4 :	//	YYYY/DD/MM
			dReturnFecha = nPosUno + "" + nPosTres + "" +nPosDos;
			break;
	}
	
	return dReturnFecha;	// retorna la fecha 	
}

// Agrega un cero delante del strPon cuando tenga solo un caracter
function ponCero(strPon){
	if(parseInt(strPon.length) < 2)
		strPon = "0" + strPon;
	return strPon;
}

