var curWindow=0;

function doAuthTwitter(theform){
    setOpacity(document.getElementById('authbutton'),6);
    document.getElementById('authbutton').disabled=true;
    theform.submit();
    return true;
 }

function doPost(theform){
    twscreen=document.getElementById('twscreen');
    promotion_text=theform.promotion_text.value;
    url='/forms?promotion_text='+promotion_text;
    twscreen.innerHTML='<div id="wait"><br><br><br><b>Please wait a moment..<br><br><br><br><br></div>';
    randomOpacity('twscreen',3,8,120);
    newContent=doAJAXRequest(url);
    twscreen.innerHTML=newContent;
    return false;
} 

fxState = new Array();
fxCount = 0;
roID=0;


function fxOr(){
    //url='/forms?id';
    //newContent=doAJAXRequest(url);
    url='/ajaxGetDownloadtime?id';
    response=doAJAXRequest(url).split(",");
    timemax=response[0];
    timeremain=response[1];
    //alert('timeremain'+timeremain);
    if (timeremain >=0){
        maxa=10/timemax*timeremain;
        fxState[fxCount]=0;
        fxCount+=1;
        fxState[fxCount]=1;
        if (roID){
            clearInterval(roID);
        }
        roID=setInterval("randomOpacity('twscreen',"+maxa/2+","+maxa+",1000,"+timemax+","+timeremain+");",120);
        
        //bla=window.setTimeout('fxOr',10000);
        twcount=document.getElementById('trest');
        newContent="Download expires in "+timeremain+" min.";
        twcount.innerHTML=newContent;
    }
    else{
        if (fxCount>0){
            clearInterval(fxID);
            if (roID){
                clearInterval(roID);
            }
            url='/forms?id=1';
            newContent=doAJAXRequest(url);
            twscreen=document.getElementById('twscreen');
            newContent=newContent=doAJAXRequest(url);
            twscreen.innerHTML=newContent;
            setOpacity(twscreen,10);
        }
    }
}

tw_opacity=0;
tw_operator=1;
function rotateOpacity(divid,omin,omax,delay){
    div=document.getElementById(divid);
    setOpacity(div,tw_opacity);
    tw_opacity+=tw_operator;
    if (tw_opacity>omax){
        tw_operator*=-1;
        tw_opacity=omax;
    }
    else if(tw_opacity<omin){
        tw_operator*=-1;
        tw_opacity=omin;
    }
    cmd="rotateOpacity('"+divid+"',"+omin+","+omax+","+delay+")";
    //alert(tw_opacity);
    bla2=window.setTimeout(cmd,delay);
}

function randomOpacity(divid,omin,omax,delay,maxw,curw,sFxCount){
    if (sFxCount>0){
        lFxCount=sFxCount;
    }
    else{
        lFxCount=fxCount;
    }
    div=document.getElementById(divid);
    quality=GetRandom( curw, maxw);
    if (quality >= (maxw-1)){
        setOpacity(div,10);
    }
    else{
        opa=GetRandom(omin,omax);
        setOpacity(div,opa);
    }
}

function GetRandom( min, max ) {
    if( min > max ) {
        return( -1 );
    }
    if( min == max ) {
        return( min );
    }
 
    return( min + parseInt( Math.random() * ( max-min+1 ) ) );
}

function divhide(divid){
    document.getElementById(divid).style.visibility='hidden';
    document.getElementById(divid).style.display='none';
}

function divshow(divid){
    document.getElementById(divid).style.visibility='visible';
    document.getElementById(divid).style.display='block';
}

function setOpacity(obj,value) {
    obj.style.opacity = value/10;
    obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function doAJAXRequest(url){
    if (window.XMLHttpRequest)
      {
      xmlHttp=new XMLHttpRequest();
      }
    else // for older IE 5/6
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    url=url+"&sid="+Math.random();
    xmlHttp.open("GET",url,false);
    xmlHttp.send(null);
    return xmlHttp.responseText;
}


