/*
version: 0.8
autor: Marc Palau
url: http://www.javascript.es | http://www.nbsp.es
contacto: guru (arroba) javascript (punto) es

HELP ABOUT USE: http://livedocs.macromedia.com/flash/mx2004/main_7_2/00001408.html#wp3994770
AYUDA DE USO: http://www.cristalab.com/tutoriales/6/tutorial-de-LoadVars-en-flash-mx-2004

Wrapper implementado completamente

Obj
*# LoadVars class

Methods
*# LoadVars.addRequestHeader()
*# LoadVars.decode()
*# LoadVars.getBytesLoaded() //not applied
*# LoadVars.getBytesTotal() //not applied
*# LoadVars.load()
*# LoadVars.send()
*# LoadVars.sendAndLoad()
*# LoadVars.toString() 

Attributes
*# LoadVars.contentType *
*# LoadVars.loaded *

Events
*# LoadVars.onData
*# LoadVars.onLoad

*/
LoadVars=function(){
	this.loaded=false;
	this.sandlobj=false;
	this.onData=function(){};
	this.onLoad=function(){};
	this.headers=new Object();
	this.addRequestHeader("content-type","application/x-www-form-urlencoded");
	cConex=this.cconex();
}
//Private methods
LoadVars.prototype.cconex=function(){
	var conex = "";
	if(window.XMLHttpRequest){conex=new XMLHttpRequest();}
	else if(window.ActiveXObject){
		try{conex = new ActiveXObject("Msxml2.XMLHTTP");}
		catch(e){try{conex = new ActiveXObject("Microsoft.XMLHTTP");}
		catch(e){conex=false;}
		}
	}
	return conex;	
}
LoadVars.prototype.parseVars=function(){
	//this.decode(this.valor);
	this.loaded=true;
	if(oConex.sandlobj){
		try{
			oConex.sandlobj.onLoad(true);
			oConex.sandlobj.onData(oConex.valor);
		}catch(e){
			alert("ERROR: "+e);
		}
	}else{
		oConex.onLoad(true);
		oConex.onData(oConex.valor);	
	}
}
LoadVars.prototype.processOnLoad=function(){
	if(cConex.readyState == 4){
		if(cConex.status==200){
			oConex.valor=cConex.responseText
			oConex.parseVars();
		}else if(cConex.status==404){
			//alert("[processOnLoad.1] No se ha encontrado el archivo requerido: "+oConex.ruta);
			if(oConex.sandlobj){
				try{
					oConex.sandlobj.onLoad(false);
					oConex.sandlobj.onData(false);
				}catch(e){
					alert("[processOnLoad.2] El objeto: "+oConex.sandlobj.toString()+" no es un objeto loadVars valido");
				}
			}else{
				oConex.onLoad(false);
				oConex.onData(false);
			}
		}
	}
}
LoadVars.prototype.setHeaders=function(){
	for(var x in this.headers){
		var vari = x;
		var valu = this.headers[x];
		cConex.setRequestHeader(vari,valu);
	}
}
LoadVars.prototype.addVars=function(obj){
	for(var i in obj){
		this[i]=obj[i];
	}
}
LoadVars.prototype.getVariables=function(bool){
	var excepcions = new Array("","contentType","loaded","onData","onLoad","headers","cconex","parseVars","processOnLoad","setHeaders","getVariables","addRequestHeader","decode","load","send","sendAndLoad","toString","ruta","variables","valor","rutaArg","creaForm","addVars","loadPost","sandlobj","getBytesLoaded","getBytesTotal");
	var retorn = "";
	if(bool){
		var o = new Object();
	}
	for(var i in this){
		var isin=false;
		for(var j in excepcions){
			if(i == excepcions[j]){isin=true;}
		}
		if(!isin){
			if(bool){o[i]=this[i];
			}else{retorn += (i + "=" + this[i] + "&");}
		}
	}
	if(bool){
		return o;
	}else{
		return retorn.substring(0,retorn.length-1);
	}
}
LoadVars.prototype.creaForm=function(action,objVars,method){
	var form=document.createElement("form");
	form.action=action;
	form.method=method;
	form.style.display="none";
	for(var i in objVars){
		var inp=document.createElement("input");
		inp.type="hidden";
		inp.name=i;
		inp.value=objVars[i];
		form.appendChild(inp);
	}
	return form;
}
LoadVars.prototype.loadPost=function(ruta){
	oConex=this;
	this.ruta=ruta;
	if(cConex){
		cConex.onreadystatechange=this.processOnLoad;
		cConex.open("POST",this.ruta);
		this.setHeaders();
		var varPost=this.getVariables();
		cConex.send(varPost);
	}
}
//Public methods
LoadVars.prototype.addRequestHeader=function(variable,value){
	if(arguments.length==2){
		this.headers[variable]=value;
	}else if(arguments.length==1){
		for(var x=0;x<variable.length;x+=2){
			var vari = variable[x];
			var valu = variable[x+1];
			this.headers[vari]=valu;
		}
	}
}


// 
LoadVars.prototype.load=function(ruta){
	oConex=this;
	this.ruta=ruta;
	this.rutaArg=ruta+"?"+this.getVariables();
	if(cConex){
		cConex.onreadystatechange=this.processOnLoad;
		cConex.open("GET",this.rutaArg);
		this.setHeaders();
		cConex.send(null);
	}
}


LoadVars.prototype.send=function(ruta,target,method){
	method=method.toUpperCase();
	if(method!="GET")method="POST";
	var vars = this.getVariables(true)
	var formulari=this.creaForm(ruta,vars,method);
	formulari.target=target;
	document.body.appendChild(formulari);
	formulari.submit();
	formulari.parentNode.removeChild(formulari);
}
LoadVars.prototype.sendAndLoad=function(ruta,obj,method){
	method=method.toUpperCase();
	if(method!="GET")method="POST";
	if(method=="GET"){
		this.addVars(obj);
		this.load(ruta);
	}else{
		this.addVars(obj);
		this.loadPost(ruta);
	}
	this.sandlobj=obj;
}
LoadVars.prototype.toString=function(){
	return this.getVariables();
}
LoadVars.prototype.getBytesLoaded=function(){
	if(cConex.readyState > 2){
		return cConex.responseText.length;
	}else{
		return 0;
	}
}
LoadVars.prototype.getBytesTotal=function(){
	var v=cConex.getResponseHeader("Content-Length");
	if(!v){v=0;}
	return v;
}

/* Traza y teléfono*/
function traza (nodo,id1,id2){
	var vars = new LoadVars();
	vars.nombre = id2;
	vars.telefono = id1;
	vars.load("/seguimiento.php",vars,"POST");
	nodo.childNodes[0].style.visibility='hidden';
	nodo.childNodes[0].style.position='absolute';
	nodo.nextSibling.style.visibility='visible';
	//nodo.nextSibling.childNodes[0].style.visibility='visible';
	//nodo.nextSibling.childNodes[0].style.position="relative";
	//nodo.nextSibling.childNodes[0].style.display="block";
	//nodo.nextSibling.childNodes[0].style.clear="both";
	return false;			
		}
		

/* Tabs */

function ficha(tabNum) {
        switchAllOff();
        document.getElementById('tab'+tabNum).className = 'selected';
        document.getElementById('tabcontents'+tabNum).className = 'tabselectedcontents';
}

function homeOn(tabNum) {
        switchAllOff();
        document.getElementById('tab'+tabNum).className = 'selected';
        document.getElementById('tabcontents'+tabNum).className = 'tabselectedcontents';
        setCookie('hometabCookie', tabNum, 2);
}

function provinciaOn(tabNum,url) {
        switchAllOff();
        document.getElementById('tab'+tabNum).className = 'selected';
		document.getElementById('tabcontents'+tabNum).className = 'tabselectedcontents';
        setCookie('provinciatabCookie', tabNum, 3);
}

function provincias(tabNum) {
        //switchAllOff();
		var url = 'http://degarantia.com/mudanzas/guardamuebles/transportes/'+tabNum;
		window.location.href = url;
        setCookie('provinciasmudanzasCookie', tabNum);
}


function comprobar_provincias() {
        tabNum=getCookie('provinciasmudanzasCookie');
        if (tabNum==null) {
                tabNum='madrid';
        }

        provincias(tabNum);
}

function checkTabCookieHome() {
        tabNum=getCookie('hometabCookie');
        if (tabNum==null) {
                tabNum=1;
        }

        homeOn(tabNum);
}

function checkTabCookieProvincia() {
        tabNum=getCookie('provinciatabCookie');
        if (tabNum==null) {
                tabNum=1;
        }

        provinciaOn(tabNum);
}

function clearTop() {
        topRow = document.getElementById('general');
        tabArray = topRow.childNodes;
        for (var i=0; i<tabArray.length; i++) {
                tabArray[i].className = 'tab';
        }
}

function clearContents() {
        tabContents = document.getElementById('datos_tabs');
        contentsArray = tabContents.childNodes;
        for (var j=0; j<contentsArray.length; j++) {
                contentsArray[j].className = 'tabcontent';
        }
}

function switchAllOff() {
        clearTop();
        clearContents();
}

function getCookie(NameOfCookie) {
        if (document.cookie.length > 0) {
                begin = document.cookie.indexOf(NameOfCookie+"=");
                if (begin != -1) {
                        begin += NameOfCookie.length+1;
                        end = document.cookie.indexOf(";", begin);
                        if (end == -1) end = document.cookie.length;
                        return unescape(document.cookie.substring(begin, end));
                }
        }
        return null;
}

function setCookie(NameOfCookie, value, expiredays) {
        var ExpireDate = new Date ();
        ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
        document.cookie = NameOfCookie + "=" + escape(value) + "; expires=" + ((expiredays == null) ? "0" : ExpireDate.toGMTString()) + "; path=/; domain=degarantia.com;" ;
}

//

function muestramapa() {
document.getElementById('mapagoogle').style.visibility='visible';
}

function ocultamapa() {
document.getElementById('mapagoogle').style.visibility='hidden';	
}

function muestra(capa) {
document.getElementById(capa).style.display='block';
}

function oculta(capa) {
document.getElementById(capa).style.display='none';
}

function ocultar() {
document.getElementById('mensaje').style.display='none';
}

function activar_cat1(id1){
	if (id1=='1'){
	document.getElementById('nav-proemp').className='pest_activo';
	} else if (id1=='2'){
	document.getElementById('nav-fabricantes').className='pest_activo';
	} else {
	document.getElementById('nav-tiendas').className='pest_activo';	
	}
	}

	
function activar_cat2(id2){
	document.getElementById('prov_'+id2).className = 'current-cat';
	}
	
function activar_combo(id){
	document.getElementById('prov_combo'+id).selected = true;
	document.getElementById('prov_combo'+id).style.fontWeight = "bold";
	}
		function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}