///////////////////////////////////////////////////////////////////////////
// @Sistema: Espacio Turistico	 										 //
// @Version: 1.0														 //
// @Autor: Juan J. Lopez Lira											 //
// @Fecha: 08 Mayo 2010													 //
// @Descripción: Archivo de Funciones JS	 						 	 //
/////////////////////////////////////////////////////////////////////////*/
	
// Abrir Nueva Ventana
function OpenWindow(Url,NombreVentana,Utilidades) {
  window.open(Url,'Ventana','status=yes,scrollbars=yes,resizable=yes,width=640,height=480');
}


// Ajusta Ventana al Centro del Monitor
function FixWindow(){
	x =(screen.width - 640) / 2;
	y =(150) / 2;
	moveTo(x,y);
}

//Imprime Paginas 
function Print(){
	window.print();
}
	
//Cierra la Pagina Pop Up y Refresca la Pagina Opener
function RefreshWindow(){
	window.opener.location.href = window.opener.location.href;
		
		if (window.opener.progressWindow);
		{
		window.opener.progressWindow.close();
		}
		window.close();
	 
}

//Funcion para Envio de formularios
function Send(form){
	document.getElementById(form).submit();	
}

//Funcion para mostrar mas informacion
function Show(id){
	if(document.getElementById(id).style.display =='none'){
		document.getElementById(id).style.display ='block'
		} else {
			document.getElementById(id).style.display ='none'
			}
	}
	
//Funcion para Borrar contenido de campos
function ClearField(id){
   var field = document.getElementById(id);
      var stringNew = field.value;
      field.value = "";
}

//Funcion para asignar valor default a campos
function SetField(id,ogval){
   var field = document.getElementById(id);
   if(field.value == ""){
      field.value = ogval;
   }
}

///////////////////////////////////
// Funciones AJAX				 //
///////////////////////////////////

// Genera el Objeto de Conexion para Ajax
function newajax(){
	var xmlhttp=false;
	try{
		// Creacion del objeto AJAX para navegadores no IE
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e){
		try{
			// Creacion del objet AJAX para IE
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(E){
				if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
				}
				}
				return xmlhttp; 
}

//Funcion de Precarga Ajax
function precarga(){
	loader = '<div class="loading"></div>';
	return loader;
}

//Guia Turistica
function gtprecarga(){
	loader = '<div class="gtloading"></div>';
	return loader;
}
//Eventos
function evprecarga(){
	loader = '<div class="evloading"></div>';
	return loader;
}
//Buscador
function seletopt(){
	sel = '<select name="tmp"><option>-></option></select>';
	return sel;
}


//Busqueda Preliminar por giro
function BusquedaEstados(){
	giro = document.getElementById('idgiro').value;
	
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('estados').innerHTML = precarga();
			document.getElementById('ciudades').innerHTML = selectopt();
        }
        if (ajax.readyState==4) {
            document.getElementById('estados').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=buscaestados"+"&idgiro="+giro)
}

//Busqueda Preliminar por giro
function BusquedaCiudades(){
	giro = document.getElementById('idgiro').value;
	estado = document.getElementById('idestado').value;
	
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('ciudades').innerHTML = precarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('ciudades').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=buscaeciudades"+"&idgiro="+giro+"&idestado="+estado)
}


//
function getestados(){
	pais = document.getElementById('idpais').value;
	
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('estados').innerHTML = precarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('estados').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=getestados"+"&idpais="+pais)
}

function getciudades(){
	estado = document.getElementById('idestado').value;
	
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('ciudades').innerHTML = precarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('ciudades').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=getciudades"+"&idestado="+estado)
}

function getcolonias(){
	ciudad = document.getElementById('idciudad').value;
	
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('colonias').innerHTML = precarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('colonias').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=getcolonias"+"&idciudad="+ciudad)
}

function getsegmentos(){
	giro = document.getElementById('idgiro').value;
	idempresa = document.getElementById('idempresa').value;
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('segmentos').innerHTML = precarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('segmentos').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=getsegmentos"+"&idgiro="+giro+"&idempresa="+idempresa)
}

function locate(ide){
	var georef = document.getElementById('georef'+ide).value;
	var logo = document.getElementById('logo'+ide).value;
	
	var map = new GMap2(document.getElementById('mapa'+ide));
	var ubicacion = new GLatLng(georef);
	map.setCenter(ubicacion, 12);
	map.setUIToDefault();
	
	//Punto de Referencia (Marker)
	var marker = new GMarker(new GLatLng(georef));
	map.addOverlay(marker);
		
	//Informacion de la Empresa
	var descripcion="<img src='"+ logo +"' align='middle' />";
		marker.openInfoWindowHtml(descripcion);

	GEvent.addListener(marker, "click", function(){
			map.panTo(marker.getLatLng());
			marker.openInfoWindowHtml(descripcion);
	    });
}

function DoDelImg(img,sct,tbl,ids,idv){	
	//alert(noticia);
	ajax = newajax();
	
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('bkimgkontainer').innerHTML = precarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('bkimgkontainer').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=dodelimg"+"&img="+img+"&sct="+sct+"&tbl="+tbl+"&ids="+ids+"&idv="+idv)
}


function ModSegmento(idempresa,idsegmento){
		ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('cont'+idsegmento).innerHTML = precarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('cont'+idsegmento).innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=modsegmentos"+"&idempresa="+idempresa+"&idsegmento="+idsegmento);
	}


function modificarsegmentos(){
	giro = document.getElementById('idgiro').value;
	oldsegmentos = document.getElementById('oldsegmentos').value;
	
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('segmentos').innerHTML = precarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('segmentos').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=getsegmentos"+"&idgiro="+giro+"&oldsegmentos="+oldsegmentos)
}


function GtCiudad(idciudad){
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('gt-ciudades').innerHTML = gtprecarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('gt-ciudades').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=gtciudades"+"&idciudad="+idciudad)
}

function GtDestino(idciudad){
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('gt-destino').innerHTML = gtprecarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('gt-destino').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=gtdestinos"+"&idciudad="+idciudad)
}

function GtEventos(fecha){
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('ev-container').innerHTML = evprecarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('ev-container').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=vieweventos"+"&fecha="+fecha)
}

function GtNoticias(fecha){
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('nt-container').innerHTML = evprecarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('nt-container').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=viewnoticias"+"&fecha="+fecha)
}

function rating(idvisitor,rate,idempresa){
	ajax = newajax();
	ajax.open("POST", "/include/ajaxlib.php", true);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajax.onreadystatechange=function() {
        if (ajax.readyState==1) {
            document.getElementById('dorate').innerHTML = precarga();
        }
        if (ajax.readyState==4) {
            document.getElementById('dorate').innerHTML = ajax.responseText;
        }
    }
	ajax.send("cmd=rate"+"&idvisitor="+idvisitor+"&rate="+rate+"&idempresa="+idempresa)
}


function locate(ide){
		var georef = document.getElementById('gref'+ide).value;
		var logo = document.getElementById('logo'+ide).value;
		var idmap = 'mapa'+ide;
		
		//Inicia Mapa
		var map = new GMap2(document.getElementById(idmap)); 
		var idmap= new GLatLng(georef);
		map.setCenter(idmap, 10);
		map.setUIToDefault();	
				
		//Punto de Referencia (Marker)
		var marker = new GMarker(new GLatLng(georef));
		map.addOverlay(marker);
		
		//Informacion de la Empresa
		var descripcion="<img src='"+ logo +"' align='middle' />";
		//marker.openInfoWindowHtml(descripcion);
		
		GEvent.addListener(marker, "click", function(){
			map.panTo(marker.getLatLng());
			marker.openInfoWindowHtml(descripcion);
	    });	
}

//Textos para Validador
// supply the language
$.tools.validator.localize("es", {
	'*'			: 'Este valor es incorrecto',
	':email'  	: 'Por favor indique una direccion de correo v&aacute;lida',
	':number' 	: 'Este valor debe de ser numerico',
	':url' 		: 'Por favor indique una direccion v&aacute;lida',
	'[max]'	 	: 'indique un valor menor a $1',
	'[min]'		: 'indique un valor mayor a $1',
	'[required]'	: 'Este es un campo obligatorio'
});

//Convertidor de Unidades
function convertform(form){
    var firstvalue = 0;
    for (var i = 1; i <= form.count; i++) {
       
       if (form.elements[i].value != null && form.elements[i].value.length != 0) {
          if (i == 1 && form.elements[2].value != "") return false;
          firstvalue = form.elements[i].value / form.elements[i].factor;
          break;
       }
    }
    if (firstvalue == 0) {
       clearform(form);
       return false;
    }
    for (var i = 1; i <= form.count; i++)
       form.elements[i].value = formatvalue((firstvalue * form.elements[i].factor), form.rsize);
    return true;
}
function formatvalue(input, rsize) {
   var invalid = "Err";
   var nines = "999999999999999999999999";
   var strin = "" + input;
   var fltin = parseFloat(strin);
   if (strin.length <= rsize) return strin;
   if (strin.indexOf("e") != -1 ||
       fltin > parseFloat(nines.substring(0,rsize)+".4"))
      return invalid.substring(0, rsize);
   var rounded = "" + (fltin + (fltin - parseFloat(strin.substring(0, rsize))));
   return rounded.substring(0, rsize);
}
function resetform(form) {
    clearform(form);
    form[1].value = 1;
    convertform(form);
    return true;
}
function clearform(form) {
    for (var i = 1; i <= form.count; i++) form[i].value = "";
    return true;
}


//Fancybox
$(document).ready(function() {
	$(".fb a, a.fb").fancybox({
				'width'				: '75%',
				'height'			: '75%',
				'autoScale'			: false,
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'type'				: 'iframe'
			});
		});
