var VSSubmit;
var progressId;
var TAOForm = null;
var bar1 = null;
//Valores de la ventana
var viewportwidth;
var viewportheight;
var altura;
var scrollY;

function TAOdoWait()
{
    //Control para que no se inicie más de una vez la barra
    if (bar1 != null)
        return;
    
    SetWindowsValues();
    
	var odiv = document.getElementById(progressId + "_waitBox");
    if (odiv != null )
    {
        // Se crea la barra de progreso en el div.
		bar1= createBar(odiv,70,10,0,"");

		// Se muestra el div (necesario para obtener su altura)
		odiv.style.display = "";

		// Se calcula la altura
		var y = Math.max(Math.ceil((viewportheight - odiv.offsetHeight) / 2),0)+ scrollY;
		
		//Se establece el TOP del div
		odiv.style.top = y + "px";				
	}

	// Se procesa el div que lo cubre todo y lo inhabilita
	var odivContainer = document.getElementById(progressId + "_fullWindow");
	if (odivContainer != null )
	{
		//Se establece el tamaño del div para que ocupe todo el contenido del navegador.
		if (odivContainer.getAttribute("BlockFullWindow") == 'True')
		{
			
			odivContainer.style.display = "";
			odivContainer.style.height = altura + "px";
			//alert('altura div:' + altura);
		}
	}
}

function SetWindowsValues()
{
     // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
     if (typeof window.innerWidth != 'undefined')
     {
          viewportwidth = window.innerWidth,
          viewportheight = window.innerHeight
     }
     
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
     else if (typeof document.documentElement != 'undefined'
         && typeof document.documentElement.clientWidth !=
         'undefined' && document.documentElement.clientWidth != 0)
     {
           viewportwidth = document.documentElement.clientWidth,
           viewportheight = document.documentElement.clientHeight
     }
     
     // older versions of IE
     else
     {
           viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
           viewportheight = document.getElementsByTagName('body')[0].clientHeight
     }     
    altura = Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,viewportheight);
    scrollY = self.pageYOffset || (document.documentElement.scrollTop+document.body.scrollTop);
     
 }

function TAOProgressOnLoad(id)
{
	progressId = id;
	
	if(typeof(theForm) != 'undefined' )
	{	
	    if (theForm.onsubmit)
		    VSSubmit = theForm.onsubmit;

	    theForm.onsubmit = TAOProgressOnSubmit;
	}
}

function TAOProgressOnSubmit()
{
	if (typeof(VSSubmit) != 'undefined' )
	{
	    if (!VSSubmit())
	        return false;
    	TAOdoWait();
    	//return false;
	}
	return true;
}
function TAOProgressLink(link)
{
    TAOProgressLinkWindow(link, window);
}
function TAOProgressLinkWindow(link, ventana)
{
	if(IsN(link)) return;
	if(link.toLowerCase().indexOf("javascript") >= 0) eval(link);
	else
	{		
		TAOdoWait();
		ventana.location.href = link;
	}
}

function TAOIsEmpty(obj)
{
	return (obj == null) || (typeof(obj) == 'undefined');
}


// xp_progressbar
// Copyright 2004 Brian Gosselin of ScriptAsylum.com
//
// v1.0 - Initial release
// v1.1 - Added ability to pause the scrolling action (requires you to assign
//        the bar to a unique arbitrary variable).
//      - Added ability to specify an action to perform after a x amount of
//      - bar scrolls. This requires two added arguments.
// v1.2 - Added ability to hide/show each bar (requires you to assign the bar
//        to a unique arbitrary variable).

// var xyz = createBar(
// element_conatainer,
// scroll_speed,
// block_count,
// scroll_count,
// action_to_perform_after_scrolled_n_times
// )

var w3c=(document.getElementById)?true:false;
var ie=(document.all)?true:false;
var N=-1;

function createBar(container,speed,blocks,count,action)
{
    // Se lee el ancho y alto del control de los CSS, sino se encuentra se usan valores por defecto 300 x 20 px
    var w = 300;
    var h = 20;
    
    waitBoxStyle = ReadCSSStyle('.WaitBox');
    if (waitBoxStyle)
    {
        width = parseInt(waitBoxStyle.width.replace('px',''));
        if (!isNaN(width)) w = width;
    }
    pgHeight = ReadCSSStyle('.xpbar');
    if (pgHeight)
    {
        height = parseInt(pgHeight.height.replace('px',''));
        if (!isNaN(height)) h = height;
    }
    
    if(ie||w3c)
    {
        var t='<div id="_xpbar'+(++N)+'" class="xpbar">';
        t+='<span id="blocks'+N+'" class="xpbar_blocks" style="left:-'+(h*2+1)+'px;">';
        for(i=0;i<blocks;i++){
            t+='<span style=" left:-'+((h*i)+i)+'px; ';
            t+='filter:alpha(opacity='+(100-i*(100/blocks))+'); -Moz-opacity:'+((100-i*(100/blocks))/100) +'; opacity:'+((100-i*(100/blocks))/100) +';">';
            t+='"</span>';
        }
        t+='</span></div>';
        container.innerHTML = t + container.innerHTML;
        var bA=(ie)?document.all['blocks'+N]:document.getElementById('blocks'+N);
        bA.bar=(ie)?document.all['_xpbar'+N]:document.getElementById('_xpbar'+N);
        bA.blocks=blocks;
        bA.N=N;
        bA.w=w;
        bA.h=h;
        bA.speed=speed;
        bA.ctr=0;
        bA.count=count;
        bA.action=action;
        bA.togglePause=togglePause;
        bA.showBar=function(){
            this.bar.style.visibility="visible";
        }
        bA.hideBar=function(){
            this.bar.style.visibility="hidden";
        }
        bA.tid=setInterval('startBar('+N+')',speed);
        return bA;
    }
}

function startBar(bn){
var t=(ie)?document.all['blocks'+bn]:document.getElementById('blocks'+bn);
if(parseInt(t.style.left)+t.h+1-(t.blocks*t.h+t.blocks)>t.w){
t.style.left=-(t.h*2+1)+'px';
t.ctr++;
if(t.ctr>=t.count){
eval(t.action);
t.ctr=0;
}}else t.style.left=(parseInt(t.style.left)+t.h+1)+'px';
}

function togglePause(){
if(this.tid==0){
this.tid=setInterval('startBar('+this.N+')',this.speed);
}else{
clearInterval(this.tid);
this.tid=0;
}}

function togglePause(){
if(this.tid==0){
this.tid=setInterval('startBar('+this.N+')',this.speed);
}else{
clearInterval(this.tid);
this.tid=0;
}}

function ReadCSSStyle(selector)	
{
    for (j=0; j<document.styleSheets.length; j++)
    {
        var mysheet=document.styleSheets[j];
        var myrules=mysheet.cssRules? mysheet.cssRules: mysheet.rules;
        var t = '';
        for (i=0; i<myrules.length; i++)
            if (myrules[i].selectorText.toLowerCase()==selector.toLowerCase())
                return myrules[i].style;
    }	
    return null;	
}

