var strTMP="";

var Ajax = false;
if (window.XMLHttpRequest) 
{
	Ajax = new XMLHttpRequest();
} 
else if (window.ActiveXObject) 
{
	Ajax = new ActiveXObject("Microsoft.XMLHTTP");
}

function CargarAjax(method, url, div){
	if(Ajax){
		Ajax.open(method, url +"&rRand=" + (new Date()) / 1);
		Ajax.onreadystatechange = function(){
			if (Ajax.readyState == 4 && Ajax.status == 200) 
			{
				document.getElementById(div).innerHTML = Ajax.responseText;
			}
		}
		Ajax.send(null);
	} 
}

function validar_busqueda(){
	var txt = Trim(document.frm_buscar.p_search.value);
	if(txt==' ingrese aquí el texto a buscar...'){
		alert("Debe ingresar un texto a buscar.");
		return false;
	}
	
	if(txt.length<3){
		alert("Debe ingresar al menos 3 caracteres para la búsqueda.");
		return false;
	}
	return true;	
}

function bus_vaciar(obj){
	if(Trim(obj.value)==' ingrese aquí el texto a buscar...') {
		obj.value='';
		obj.className = 'bus_editable';
	}
}

function bus_original(obj){
	if(Trim(obj.value)==''){ 
		obj.value=' ingrese aquí el texto a buscar...';
		obj.className = 'bus_inicial';
	}
}

function sen_vaciar(obj){
	if(Trim(obj.value)=='Opcionalmente puede escribir aquí un mensaje para el destinatario...') {
		obj.value='';
		obj.className = 'bus_editable';
	}
}

function sen_original(obj){
	if(Trim(obj.value)==''){ 
		obj.value='Opcionalmente puede escribir aquí un mensaje para el destinatario...';
		obj.className = 'bus_inicial';
	}
}
///
function isMail(_email) {
     var emailReg = /^[a-z][a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,4}$/i
     return emailReg.test(_email);
  }
///
function tieneDatos(Valor) { 
 for (var i=0; i<Valor.length; i++) { 
   if ((" \t\n\r").indexOf(Valor.charAt(i))==-1) return true; 
   } 
 return false; 
}

function EsTextoFecha(objTexto, bolPermitirNulo)
{
	objTexto.value = Trim(objTexto.value);
	if (bolPermitirNulo && objTexto.value.length == 0) {
		return true;
	}
	if (EsFecha(objTexto.value)) {
		return true;
	}
	else {
		return false;
	}
}

function Trim(strValue) 
{
	var objRegExp = /^(\s*)$/;
    //check for all spaces    
	if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)          
	   		return strValue;    
	}    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;   
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
	}  
	return strValue;
}

function EsFecha(strValor)
{
 	var objRegExp = /^\d{2}\/\d{2}\/\d{4}$/ 
  	//check to see if in correct format  
	if(!objRegExp.test(strValor)) {
    	return false; //doesn't match pattern, bad date  
	}
	else{
    	//var strSeparator = strValor.substring(2,3) //find date separator
    	var arrayDate = strValor.split('/'); //split date into month, day, year
    	//create a lookup for months not equal to Feb.
    	var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        	'08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
		var intDay = parseInt(arrayDate[0], 10); 
    	//check if month value and day value agree
    	if(arrayLookup[arrayDate[1]] != null) {
      		if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)
        		return true; //found in lookup table, good date    
		}    
    	//check for February    
		var intYear = parseInt(arrayDate[2],10);
    	var intMonth = parseInt(arrayDate[1],10);
    	if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      		return true; //Feb. had valid number of days  
    }
  	return false; //any other values, bad date
}

function EsTextoEntero(objTexto, bolPermitirNulo, intMinimo, intMaximo)
{
	objTexto.value = Trim(objTexto.value);
	if (bolPermitirNulo && objTexto.value.length == 0) {
		return true;
	}
	if (EsEntero(objTexto.value, intMinimo, intMaximo)) {
		objTexto.value = parseInt(objTexto.value,10);
		return true;
	}
	else {
		return false;
	}
}

function EsTextoNumeric(objTexto, bolPermitirNulo, intMinimo, intMaximo)
{
	objTexto.value = Trim(objTexto.value);
	objTexto.value = objTexto.value.replace(/,/g,".");
	if (bolPermitirNulo && objTexto.value.length == 0) {
		return true;
	}
	if (EsNumeric(objTexto.value, intMinimo, intMaximo)) {
		objTexto.value = parseFloat(objTexto.value);
		return true;
	}
	else {
		return false;
	}
}


function EsEntero(strValor, intMinimo, intMaximo)
{
	var objRegExp = /(^(\+|-)?\d\d*$)/;
	var intValor;
	
	if (objRegExp.test(strValor)) {
		intValor = parseInt(strValor,10);
		if (! isNaN(intMinimo)) {
			if (intValor < intMinimo)
				return false
		}
		if (! isNaN(intMaximo)) {
			if (intValor > intMaximo)
				return false;
		}
		return true;
	}
	else {
		return false;
	}
}

function EsNumeric(strValor, numMinimo, numMaximo)
{
	var objRegExp = /(^(\+|-)?\d\d*\.\d*$)|(^(\+|-)?\d\d*$)/; 
	var numValor;
	
	if (objRegExp.test(strValor)) {
		numValor = parseFloat(strValor);
		if (! isNaN(numMinimo)) {
			if (numValor < numMinimo)
				return false
		}
		if (! isNaN(numMaximo)) {
			if (numValor > numMaximo)
				return false;
		}
		return true;
	}
	else {
		return false;
	}
}

function ChkContacto()
{
	f = frm_contacto;
	f.goes.disabled=true;
	if(!tieneDatos(f.p_nombre.value))
	{
		alert("Debe ingresar su nombre.");
		f.p_nombre.focus();
		f.goes.disabled = false;
		return false;
	}
	
	if(!isMail(f.p_email.value))
	{				
		alert("Ingrese un e-mail válido.");
		f.p_email.focus();
		f.goes.disabled = false;
		return false;
	}
	
	if(!tieneDatos(f.p_consulta.value))
	{
		alert("Debe ingresar la consulta o comentario.");
		f.p_consulta.focus();
		f.goes.disabled = false;
		return false;
	}
	f.submit();
}

function zoomText(Accion,Elemento){
var max = 22;
var min = 12;
incremento=2;

var obj=document.getElementById(Elemento);
if (obj.style.fontSize=="" || isNaN(obj.style.fontSize.replace("px",""))){
	actual = 15;
}
else{
	actual = parseInt(obj.style.fontSize.replace("px",""));
}
if( Accion=="increase"){
	if((actual+incremento) <= max ){
		valor=actual+incremento;
		obj.style.fontSize=valor+"px";
	}
	else{
		alert("No se puede aumentar más el tamaño de la letra.");
		return false;
	}
}

if( Accion=="decrease"){
	if((actual+incremento) > min ){
		valor=actual-incremento;
		obj.style.fontSize=valor+"px";
	}
	else{
		alert("No se puede disminuir más el tamaño de la letra.");
		return false;
	}
}
} 

function printNews(id){
	var a=window.open('./noticias_imprimir.php?p_id='+id,'Impresión','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,height=350,width=650');	
}

function votar(){
	var opt; 
	opt = -1; 
	for ( i=0; i<document.getElementById('frm_encuesta').p_opcion.length; i++) { 
		if (document.getElementById('frm_encuesta').p_opcion[i].checked) { 
			opt = document.getElementById('frm_encuesta').p_opcion[i].value;
			tmp_url = './ajax_encuesta_votar.php?p_opcion=' + opt;
			CargarAjax('GET', tmp_url, 'encuesta');
			return true;
		}
	}
	alert('Debe seleccionar una de las opciones para votar.');
	return false;
}
	
function enviarNoticia(idnoticia){
	frm = document.getElementById('frm_envio_noticia');
	
	if(!tieneDatos(frm.p_remitente.value)){
		alert("Debe ingresar su nombre.");
		frm.p_remitente.focus();
		return false;
	}
	
	if(!tieneDatos(frm.p_emailremitente.value)){
		alert("Debe ingresar su e-mail.");
		frm.p_emailremitente.focus();
		return false;
	}
	
	if(!isMail(frm.p_emailremitente.value)){				
		alert("Ingrese un e-mail válido.");
		frm.p_emailremitente.focus();
		return false;
	}
	
	if(!tieneDatos(frm.p_emaildestinatario.value))	{
		alert("Debe ingresar al menos una dirección de destino.");
		frm.p_emaildestinatario.focus();
		return false;
	}
	arrDestinos = frm.p_emaildestinatario.value.split(",");
	for(i=0; i<arrDestinos.length;i++){
		if(!tieneDatos(Trim(arrDestinos[i])) || !isMail(Trim(arrDestinos[i]))){				
			alert("E-mail(s) de destino no válido(s).");
			frm.p_emaildestinatario.focus();
			return false;
		}		
	}
	
	tmp_url = './ajax_noticia_enviar.php?p_idnoticia='+ idnoticia +'&p_remitente=' + frm.p_remitente.value  +'&p_emailremitente=' + frm.p_emailremitente.value + '&p_emaildestinatario=' + frm.p_emaildestinatario.value + '&p_mensaje='+ frm.p_mensaje.value;
	document.getElementById('buttons').style.display = 'none';
	document.getElementById('loading').style.display = 'block';
	CargarAjax('GET', tmp_url, 'tofriend-content');
	return true;
			
}