var strTMP="";

function Init(){
	strTMP = frm_found.p_search.value.replace(/'/g, "\'");
	frm_found.p_search.focus();
	execJS(frm_found.p_search.value,2);	
}

function Dispara(){
	var strTMP2 = "";
	strTMP2 = document.getElementById("p_search").value.replace(/'/g, "\'");
	strTMP2 = strTMP2.replace(/&/g, "@@");
	if(strTMP!=strTMP2 || strTMP.length==0)
	{
		execJS(strTMP2,0);
		strTMP = document.getElementById("p_search").value.replace(/'/g, "\'");
		strTMP = strTMP.replace(/&/g, "@@");
	}
}

function execJS(str,exclude){
	 var sc = document.createElement("script");
	 //sc.setAttribute("charset", "utf8");
	 sc.src = "./resultados_previos.php?p_search="+str+"&p_exclude="+exclude;
	 document.getElementById("res").appendChild(sc);
}

function FormatTexto(texto)
{
	 return texto.replace(/'/g, "\'");
}

function ShowPlano(Latitud,Longitud,IdDireccion)
{
	try {
		Ajax = new XMLHttpRequest();
	}
	catch (e) {
		try {
			Ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				Ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				Ajax = false;
				alert("Su navegador no soporta Ajax!");
				return false;
			}
		}
	}

	var address = '';
	
	//<![CDATA[
	if (GBrowserIsCompatible()) {
		// Display the map, with some controls and set the initial location
		var map = new GMap2(document.getElementById("mapa"));
		var point = new GLatLng(Latitud,Longitud);
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(point,17);
		
		if(Ajax){
			Ajax.open("GET", "./ajax_getdatosdir.php?p_iddireccion=" + IdDireccion + "&rRand=" + (new Date()) / 1);
			Ajax.onreadystatechange = function(){
				if (Ajax.readyState == 4 && Ajax.status == 200){
					address = Ajax.responseText;
					
					var iconoMarca = new GIcon(G_DEFAULT_ICON);
					iconoMarca.image = "./assets/images/gm_icon.png"; 
					var tamanoIcono = new GSize(25,25);
					iconoMarca.iconSize = tamanoIcono;
					var tamanoSombra = new GSize(0,0);
					iconoMarca.shadowSize = tamanoSombra;
					iconoMarca.iconAnchor = new GPoint(10, 12);
					
					var marker = new GMarker(point, iconoMarca);
					GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(address);
					});
					map.addOverlay(marker);
					marker.openInfoWindowHtml(address);
				}
			}
			Ajax.send(null);
		}
		document.getElementById('DIVPlano').style.visibility = 'visible';
	}	
	//]]>
}
///
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 fc_mnuciu(){
	x = document.getElementById('mnuciu');
	if(x.style.visibility.length==0 || x.style.visibility == 'hidden'){
	  x.style.top  = parseInt(document.getElementById('madre').offsetTop) + 15;
	  x.style.left = parseInt(document.getElementById('madre').offsetLeft) + parseInt(document.getElementById('madre').offsetWidth) - parseInt(x.offsetWidth);
	  x.style.visibility = 'visible';
	}
	else
	  x.style.visibility = 'hidden';
}

function CC(id){
	frm_cc.p_idciudad.value = id;
	frm_cc.submit();
}