/*
+----------------------------------------------------------------------
|   JavaScript / Librerias varias
|   ========================================
|   @developer  Jose Zanni
|   @copyleft (L) 2006 Crear Imagen
|
|   ========================================
|   @Web http://www.crearimagen.com
|   @Email info@crearimagen.com
+-----------------------------------------------------------------------
*/

/***
*
* Abre una ventana e imprime el contenido del ID(nombre) parametro
*
***/
function imprSelec(nombre)
{
	var ficha = document.getElementById(nombre);
	var ventimp = window.open(' ', 'popimpr');
	ventimp.document.write( ficha.innerHTML );
	ventimp.document.close();
	ventimp.print( );
	ventimp.close();
}


/*
 *
 * Env?a formularios por email e informa por pantalla el resultado
 *
 */
function enviarFormulario(url, formid, enviando)
{

	// validar conexion AJAX
	var peticion = false;
	try
	{
		peticion = new XMLHttpRequest();
	}
	catch (trymicrosoft)
	{
		try
		{
			peticion = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (othermicrosoft)
		{
			try
			{
				peticion = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (failed)
			{
	        	peticion = false;
			}
		}
	}

	if (!peticion)
	{
	       alert("ERROR AL INICIALIZAR AJAX!");
	}
	else
	{
		// conexion AJAX ok
		document.getElementById('infoResultado').innerHTML = '<div class="infoResult"><img src="images/ajax-loader.gif" align="left" />&ensp;&ensp;'+enviando+'</div>';

		// leer parametros POST
		var Formulario = formid; // var Formulario = document.getElementById(formid);
	    var longitudFormulario = Formulario.elements.length;
	    var cadenaFormulario = ""
		var sepCampos
		sepCampos = ""
	    for (var i=0; i <= Formulario.elements.length-1;i++)
	    {
	    	cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
	        sepCampos="&";
		}

		// hacer llamada...
		// var peticion = new XMLHttpRequest();
		peticion.open("POST", url, true);
	    peticion.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=ISO-8859-1');
	    peticion.onreadystatechange = function ()
	    {
	    	if (peticion.readyState == 4)
	    	{
				// document.getElementById('infoResultado').innerHTML = "Proceso terminado...<br />"+peticion.responseText;
				info = '<div class="infoResult">';
				info += peticion.responseText+'<br />';
				info += '</div>';

//				info = '';
//				alert( peticion.responseText );

				document.getElementById('infoResultado').innerHTML = info;
				// window.location = urlok + peticion.responseText;
			}

		}
		peticion.send(cadenaFormulario);
	}
}

/*
 *
 * Al limpiar/reset un formulario vuelve a quedar activo para
 *
 */
function form_reset( form )
{
	form.enviar.disabled = "";
}




/*
*
*
*
*/

function togleVisible(id)
{
	vista = (document.getElementById(id).style.visibility == 'visible') ? 'hidden' : 'visible';
	document.getElementById(id).style.visibility = vista;

/*	estado = document.getElementById(id).visible;
//	alert(id);
//	alert(estado);

	if(estado=='visible')
	{
		document.getElementById(id).style.visible='visible';
	}
	else
	{
		document.getElementById(id).style.visible='hidden';
	}
	*/
	// return false;
}




/*
*
*
*
*/
function validarEmail(objName, msgRequerido, msgValido)
{
    var strName = Trim(objName.value);
    if (strName.length == 0)
    {
        alert(msgRequerido); // 'Debe ingresar una direccion de email.'
        objName.focus();
        return false;
    }
    else if( !esEmail(strName) )
    {
        alert(msgValido); // 'Debe ingresar una direccion de email valida.'
        objName.focus();
        return false;
	}
    return true;
}
/*
*
*
*
*/
function validarNick(objName, msgRequerido, msgValido )
{
	if (objName.value.length < 1)
	{
	    alert(msgRequerido);
	    objName.focus();
	    return false;
	}

	var checkOK = "ABCDEFGHIJKLMN?OPQRSTUVWXYZ?????abcdefghijklmn?opqrstuvwxyz????? ";
	var checkStr = objName.value;
	var allValid = true;

	for (i = 0;  i < checkStr.length;  i++)
	{
	    ch = checkStr.charAt(i);
	    for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
	}

	if (!allValid)
	{
    	alert(msgValido); // "Escriba s?lo letra caracteres en el campo \"nombre_usuario\"."
    	objName.focus();
    	return false;
	}
}
/*
*
*
*/
function validarString(objName,vmsg)
{
    var strName = Trim(objName.value);
    if (strName.length == 0)
    {
        alert(vmsg);
        objName.focus();
        return false;
    }
    return true;
}

/**/

function empty(objName)
{
    var strName = Trim(objName.value);
	if (strName.length == 0)
    {
        return true;
    }
    return false;
}


function esEmail( direccion )
{
	apos=direccion.indexOf("@")
	dotpos=direccion.lastIndexOf(".")
	if (apos<1 || dotpos-apos<2)
	{
		// alert(alerttxt);
		return false
	} else {
		return true
	}
}



function XXXesEmail( direccion )
{
	var strName = direccion.indexOf("@");
	alert( "paca"+strName );
	if ( str !== -1)
	{
//	if( direccion.indexOf ('@', 0) == -1 || direccion.length < 5 ) {
		alert("alla");
		return false;
	}
	else
	{
		alert("aqui");
		return true;
	}
}

/* USADOS AL VALIDAR FORMULARIOS */
/* Trims the leading and trailing blanks from a given string. */
function Trim(strToTrim) {
    while(strToTrim.charAt(0)==' '){strToTrim = strToTrim.substring(1,strToTrim.length);}
    while(strToTrim.charAt(strToTrim.length-1)==' '){strToTrim = strToTrim.substring(0,strToTrim.length-1);}
    return strToTrim;
}



/********/

