﻿var RUTA = "";
var logueado = false;
var InitFuncArray = new Array();
var browser = navigator.appName;

// Funciones que se llaman al terminar de cargar la pagina
function onLoadInit()
{
	for (var i = 0; i < InitFuncArray.length; i++)
	{
		eval(InitFuncArray[i]).call();
	}
	checkScrollTo();
}

function addInitFunc(func)
{
	InitFuncArray.push(func);
}

function recargar_div(div,pagina, func_OnComplete){
	var url = RUTA+pagina;
	new Ajax.Updater(div,url, {onComplete: func_OnComplete,evalScripts: true});
}

function borrarInput(miInput,valor){
	if(miInput.value==valor){
		miInput.value = "";
	}
}

function llenarInput(miInput,valor){
	if(miInput.value == ""){
		miInput.value = valor;
	}
}

function buscar_directorio(){
	var id = $F('cat_susc');
	var nombre = $('cat_susc').options[$('cat_susc').selectedIndex].text;
	nombre = nombre.replace(/ /g,'-');
	window.location.href = RUTA+'directorio/'+id+'_'+nombre+'/';
}

function buscar(){
	if($F('busqueda') != '' && $F('busqueda') != 'Búsqueda rápida' ){
		window.location.href=RUTA+'busqueda/'+urlencode($F('busqueda'))+'/';
	}
}

function urlencode(str) {
    var result = "";
    for (i = 0; i < str.length; i++) {
        if (str.charAt(i) == " ") result += "+";
        else result += str.charAt(i);
    }
    return escape(result);
}

function get_captcha(color) 
{
	var rand = Math.random();
	//$('captcha').src = RUTA+"captcha/"+color+"/"+rand+"/";
	var captcha = $('captcha');
	if(captcha)
		captcha.src = RUTA+"include/captcha/imagebuilder.php?color="+color+"&rando="+rand;
}

//SELECTS
function mostrar(ele){
	$(ele).show();
}

function ocultar(div){
	$(div).hide();
}


// POP UPS
function open_div(id,width,height,mod,idRef){
	var div = 'div_'+id;
	if(id == 'nueva_pieza'){
		var url = 'estructura/div_'+id+'.php?id='+mod;
		var onComp = '';
	}
	else if(id == 'nueva_campania'){
		var url = 'estructura/div_'+id+'.php?id='+mod;
		var onComp = '';
	}
	else{
		var url = 'estructura/div_'+id+'.php?modulo='+mod+'&id='+idRef;
		var onComp = '';
	}
	
	recargar_div(div,url,onComp);
	showDiv(div,width,height);
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
	return yWithScroll;
}

function open_login(width,height,on_complete){
	var div = 'div_login';
	var url = 'estructura/div_login.php?function='+on_complete;

	showDiv(div,width,height);
	recargar_div(div,url);
}

function showDiv(div,width,height){
	$('overlay').style.height =  document.body.scrollHeight+"px";
	$('overlay').show();
	center(div,width,height);
}

function hideDiv(div){
	div = parent.$(div);
	div.hide();  
	$('overlay').hide();
}


function center(element,width,height){
 
 	element = $(element);
	
	var my_width  = getWindowWidth();
    var my_height = getWindowHeight();

	element.style.position = 'absolute';
	element.style.zIndex   = 999999999;

	var scrollY = 0;

    if ( document.documentElement && document.documentElement.scrollTop ){
        scrollY = document.documentElement.scrollTop;
    }else if ( document.body && document.body.scrollTop ){
        scrollY = document.body.scrollTop;
    }else if ( window.pageYOffset ){
        scrollY = window.pageYOffset;
    }else if ( window.scrollY ){
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);

    var setX = ( my_width  - width  ) / 2;
    var setY = ( my_height - height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top  = setY + "px";

    element.style.display  = 'block';
}


var IE = navigator.appName.toLowerCase().indexOf("microsoft") > -1;

function only_numbers(evt){	
// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57, '.'=46, ','  = 44
	var key = IE ? evt.keyCode : evt.which;
	return (key <= 13 || (key >= 48 && key <= 57));
}


// Alto de la Pagina
function getWindowHeight() {

	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

// Ancho de la Pagina
function getWindowWidth() {

	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	return windowWidth;
}

// Anchor dinámico
function checkScrollTo()
{
	if (location.hash) {
		var str_anchor = location.hash.split("#")[1];
		if ($("anchor_"+str_anchor)) {
			Effect.ScrollTo("anchor_"+str_anchor);
		}
	}
}


function open_popup(dir,my_width,my_height){
	var my_win = window.open(dir,null,'scrollbars=yes,width='+my_width+',height='+my_height+'');
	my_win.moveTo(window.screen.availWidth/2-(my_width/2), window.screen.availHeight/2-(my_height/2));
}

function urlencode(str) {
    var result = "";
    for (i = 0; i < str.length; i++) {
        if (str.charAt(i) == " ") result += "+";
        else result += str.charAt(i);
    }
    return escape(result);
}


function urldecode(str) {    
    var result = str.replace(/\+/g, " ");
    return unescape(result);
}


function buscar_en_directorio(categoria,letra, selectObj){
	//alert(categoria+'-----'+letra+'----'+selectObj.value);
	window.location.href = RUTA+'directorio/'+categoria+selectObj.value+'/'+letra+'/';
}