var dragapproved=false
var minrestore=0
var initialwidth,initialheight
var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all
var div_es_mou=''

function crear_div_opacogeneral(opaco,opacitat){
	var newone = document.createElement("div"); 
	newone.id='opacogeneral'+opaco;
	newone.style.display='block';
	newone.style.position="absolute";
	newone.style.top=0;
	newone.style.left=0;
	newone.style.width=+document.body.offsetWidth-24;
	newone.style.height=document.body.scrollHeight;
	newone.innerHTML = '<table border="0" Style="height:100%; width:100%; background-color:#000000;-moz-opacity:.'+opacitat+'; filter:alpha(opacity='+opacitat+');" cellspacing="0" cellpadding="0"><tr><td align="center" valign="middle"></td></tr></table>'
	document.body.appendChild(newone);
}

function iecompattest(){
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function drag_drop(e){
	if (ie5&&dragapproved&&event.button==1){
		document.getElementById(div_es_mou).style.left=tempx+event.clientX-offsetx+"px"
		document.getElementById(div_es_mou).style.top=tempy+event.clientY-offsety+"px"
	} else if (ns6&&dragapproved){
		document.getElementById(div_es_mou).style.left=tempx+e.clientX-offsetx+"px"
		document.getElementById(div_es_mou).style.top=tempy+e.clientY-offsety+"px"
	}
}

function initializedrag(e,div_nom){
	offsetx=ie5? event.clientX : e.clientX
	offsety=ie5? event.clientY : e.clientY
	tempx=parseInt(document.getElementById(div_nom).style.left)
	tempy=parseInt(document.getElementById(div_nom).style.top)
	
	dragapproved=true
	div_es_mou=div_nom
	document.getElementById(div_nom).onmousemove=drag_drop;
}


function ampliar_flotant(div_nom,height){
		initialheight=height
	document.getElementById(div_nom).style.height=height+"px"		
}

function loadwindow(div_nom,left,top,width,height,mostrar){
	if (mostrar=='S') {
		if (document.getElementById("opacogeneral")!=null) {		
			crear_div_opacogeneral('2','75');
			bringToFront('opacogeneral2')  
		} else {
			crear_div_opacogeneral('','50');
			bringToFront('opacogeneral')  
		}	
	}
	
// Sustituides les variables 
//			initialwidth per .tag			
//			initialheight per .tag2			
	document.getElementById(div_nom).tag=width
		initialwidth=width
	document.getElementById(div_nom).tag2=height	
		initialheight=height

	document.getElementById(div_nom).style.display=''
	document.getElementById(div_nom).style.width=width+"px"
	document.getElementById(div_nom).style.height=height+"px"
	document.getElementById(div_nom+"content").style.height=initialheight-20;
	
	if (left<0) {
		if (left==-1) {
			left=(document.body.offsetWidth-initialwidth)/2
		} else {
			left=((document.body.offsetWidth-initialwidth)/2)+Math.abs(left);		
		}
	}
	document.getElementById(div_nom).style.left=left;
	if (top==-1) {
		if (ns6) {
			top=(document.body.ScrollHeight-initialheight)/2
		} else {
			top=(document.body.offsetHeight-initialheight)/2
		}
	}
	document.getElementById(div_nom).style.top=top;
	bringToFront(div_nom)  
//	guardar_apariencia(div_nom);		
}

function guardar_apariencia(div_nom){
	if (document.getElementById('frameacciones')!=null){
		var valor
			valor =         div_nom +'|'
			valor = valor + document.getElementById(div_nom).style.left +'|'
			valor = valor + document.getElementById(div_nom).style.top +'|'			
			valor = valor + document.getElementById(div_nom).tag +'|'			
			valor = valor + document.getElementById(div_nom).tag2 +'|'			
// Sustituides les variables 
//			initialwidth per .tag			
//			initialheight per .tag2			
//			valor = valor + initialwidth +'|'			
//			valor = valor + initialheight +'|'
			if (document.getElementById(div_nom).style.height!='10px'){
				valor = valor + 'N|'
			} else {
				valor = valor + 'S|'
			}
			if ((document.getElementById(div_nom).style.display!='none') || (document.getElementById(div_nom).style.visibility=='visible')){
				valor = valor + 'S|'
			} else {
				valor = valor + 'N|'
			}							
		document.getElementById('frameacciones').src='/includes/guardar/home.asp?v='+valor
	}	
}


function plegar(div_nom,el_width){
	if (document.getElementById(div_nom).style.height!='10px'){
		minrestore=1 
		document.getElementById(div_nom+"buto").setAttribute("src","/imagenes/finestres/obrir.png");
		document.getElementById(div_nom+"content").style.height=0;
		document.getElementById(div_nom+"content").style.display='none';
		document.getElementById(div_nom).style.height=10;
		if (el_width!=null){
			document.getElementById(div_nom).style.width=el_width;
		} else {
			document.getElementById(div_nom).style.width=250;			
		}		
	} else {
		minrestore=0 
		document.getElementById(div_nom+"buto").setAttribute("src","/imagenes/finestres/tancar.png");

		document.getElementById(div_nom).style.width=document.getElementById(div_nom).tag;
		document.getElementById(div_nom).style.height=document.getElementById(div_nom).tag2;
//		document.getElementById(div_nom).style.width=initialwidth;
//		document.getElementById(div_nom).style.height=initialheight;
		document.getElementById(div_nom+"content").style.display='';
		document.getElementById(div_nom+"content").style.height=initialheight-20;
	}
//	guardar_apariencia(div_nom);
}


function plegar_fix(div_nom){
	if (document.getElementById(div_nom).style.height!='10px'){
		document.getElementById(div_nom+"buto").setAttribute("src","/imagenes/finestres/obrir.png");
		document.getElementById(div_nom+"content").style.height=0;
		document.getElementById(div_nom+"content").style.display='none';
		document.getElementById(div_nom).style.height=10;
	} else {
		document.getElementById(div_nom+"buto").setAttribute("src","/imagenes/finestres/tancar.png");
		document.getElementById(div_nom).style.height="auto";
		document.getElementById(div_nom+"content").style.display='';
		document.getElementById(div_nom+"content").style.height="auto";
	}
//	guardar_apariencia(div_nom);	
	$('#cuerpo_2').jScrollPane({showArrows:true});		
}


function plegar_tot(div_nom){
	if (document.getElementById(div_nom).style.height!='1px'){
		minrestore=1 
		document.getElementById(div_nom+"buto").setAttribute("src","/imagenes/finestres/obrir.png");
		document.getElementById(div_nom).style.height=1;
		document.getElementById(div_nom).style.visibility='hidden';
	} else {
		minrestore=0 
		document.getElementById(div_nom+"buto").setAttribute("src","/imagenes/finestres/tancar.png");
		document.getElementById(div_nom).style.height="auto";
		document.getElementById(div_nom).style.visibility='visible';
	}	
	$('#cuerpo_2').jScrollPane({showArrows:true});	
}


function tancar(div_nom){
	if (document.getElementById(div_nom)!=null) {		
		document.getElementById(div_nom).style.display='none'
	}
	if (document.getElementById(div_nom+"contentframe")!=null) {		
		document.getElementById(div_nom+"contentframe").src='';
	}
	if (document.getElementById("opacogeneral2")!=null) {		
		document.body.removeChild(document.getElementById("opacogeneral2"));
	} else {
		if (document.getElementById("opacogeneral")!=null) {		
			document.body.removeChild(document.getElementById("opacogeneral"));
		}
	}
//	guardar_apariencia(div_nom);	
}

function stopdrag(div_nom){
	dragapproved=false;
	div_es_mou='';
	document.getElementById(div_nom).onmousemove=null;
//	guardar_apariencia(div_nom);	
}


function getAbsoluteDivs()   
{   
    var arr = new Array();   
    var all_divs = document.body.getElementsByTagName("div");   
    var j = 0;   
  
    for (i = 0; i < all_divs.length; i++)   
        if (all_divs.item(i).style.position=='absolute')   
        {   
            arr[j] = all_divs.item(i);   
            j++;   
        }   
  
    return arr;   
}   
  
function bringToFront(id)   
{   
    if (!document.getElementById ||   
        !document.getElementsByTagName)   
        return;   
  
    var obj = document.getElementById(id);   
    var divs = getAbsoluteDivs();   
    var max_index = 0;   
    var cur_index;   
  
    // Compute the maximal z-index of   
    // other absolute-positioned divs   
    for (i = 0; i < divs.length; i++)   
    {   
        var item = divs[i];   
        if (item == obj ||   
            item.style.zIndex == '')   
            continue;   
  
        cur_index = parseInt(item.style.zIndex);   
		
        if (max_index < cur_index)   
        {   
            max_index = cur_index;   
        }   
    }   
  
    obj.style.zIndex = max_index + 1;   
}   
  
function sendToBack(id)   
{   
    if (!document.getElementById ||   
        !document.getElementsByTagName)   
        return;   
  
    var obj = document.getElementById(id);   
    var divs = getAbsoluteDivs();   
    var min_index = 999999;   
    var cur_index;   
  
    if (divs.length < 2)   
        return;   
  
    // Compute the minimal z-index of   
    // other absolute-positioned divs   
    for (i = 0; i < divs.length; i++)   
    {   
        var item = divs[i];   
        if (item == obj)   
            continue;   
  
        if (item.style.zIndex == '')   
        {   
            min_index = 0;   
            break;   
        }   
  
        cur_index = parseInt(item.style.zIndex);   
        if (min_index > cur_index)   
        {   
            min_index = cur_index;   
        }   
  
    }   
  
    if (min_index > parseInt(obj.style.zIndex))   
    {   
        return;   
    }   
  
    obj.style.zIndex = 1;   
  
    if (min_index > 1)   
        return;   
  
    var add = min_index == 0 ? 2 : 1;   
  
    for (i = 0; i < divs.length; i++)   
    {   
        var item = divs[i];   
        if (item == obj)   
            continue;   
  
        item.style.zIndex += add;   
    }   
}   



