function ge(el) {
	return document.getElementById(el);
}

function getiframe(iFrameName)  
{  
	var myIFrame = document.getElementById(iFrameName);  
	var content = myIFrame.contentWindow.document.body.innerHTML;       
	return content;
}

function read_iframe2() {
	try {
    	var data=getiframe('ifr2');
		if ((data != 0) && (data != '')) {
			set_size(100);
			ge('upload-form').style.display='none';
        	ge('link-form').style.display='block';
			ge('re-try2').style.display='block';
			ge('url').value=data;
			if (ge('period').selectedIndex == 0) ge('expires').innerHTML='an hour';
			if (ge('period').selectedIndex == 1) ge('expires').innerHTML='a day';
			if (ge('period').selectedIndex == 2) ge('expires').innerHTML='3 days';
			if (ge('period').selectedIndex == 3) ge('expires').innerHTML='a week';
			if (ge('period').selectedIndex == 4) ge('expires').innerHTML='a month';
			select_all('url');
		} else
		window.setTimeout("read_iframe2()",50);
	}
	catch(e) {
		window.setTimeout("read_iframe2()",50);
		return '0';
	}
}

var UP = function() {
    
    /* private variables */
    
    var ifr = null;
    
    var startTime = null;
    var upload_max_filesize = 100857600;
    
    var infoUpdated = 0;
    
    var writeStatus = function(text,color) {
        var statDiv = ge("status");
        if (color == 1 ) {
            statDiv.style.backgroundColor = "green";
        } else if (color == 2 ) {
            statDiv.style.backgroundColor = "orange";
        } else if (color == 3 ) {
            statDiv.style.backgroundColor = "red";
        } else {
            statDiv.style.backgroundColor = "white";
        }
        statDiv.innerHTML = text;
    }
    
    
    return {
        start: function() {
		   document.getElementById('main-form').style.display='none';
		   document.getElementById('upload-form').style.display='block';
           ifr = document.getElementById("ifr");
           startTime = new Date();
           infoUpdated = 0;
		   window.setTimeout("read_iframe2()",50);
           this.requestInfo();
        },
        stop: function(files) {
           if (typeof files == 'undefined' || files) {
                if (infoUpdated > 0) {
					
                }  
           } else {
			   ge('upload-error').style.display='block';
			   ge('upload-error').innerHTML='<p>Link Bam Pow did not report any uploaded file.</p><p>Maybe it was too large, try a smaller one.</p>';
			   ge('re-try1').style.display='block';
           }
           startTime = null;
        },
        requestInfo: function() {
                ifr.src="info.php?ID="+id+"&"+new Date();
        },
        
        updateInfo: function(uploaded, total, estimatedSeconds) {
            if (startTime) {
                if (uploaded) {
                    infoUpdated++;
                    if (total > upload_max_filesize) {
						ge('upload-error').style.display='block';
				 	    ge('upload-error').innerHTML='<p>The file is too large and won\'t be available after the upload</p><p>Try a smaller one.</p>';
					    ge('re-try1').style.display='block';	
						ge('seconds').innerHTML='never';
                    } else {
						var sec = estimatedSeconds;
						if (estimatedSeconds < 60) {
							if (sec == 1) ge('seconds').innerHTML=sec+' second'; else ge('seconds').innerHTML=sec+' seconds';
						}
						if (estimatedSeconds > 60) {
							sec=Math.round(estimatedSeconds/60);
							if (sec == 1) ge('seconds').innerHTML=sec+' minute'; else ge('seconds').innerHTML=sec+' minutes';
						}
						if (estimatedSeconds > 3600) {
							sec=Math.round(estimatedSeconds/3600);
							if (sec == 1) ge('seconds').innerHTML=sec+' hour'; else ge('seconds').innerHTML=sec+' hours';
						}
						set_size(Math.floor(uploaded / total * 100));
                    }
                }
                window.setTimeout("UP.requestInfo()",1000);
            }
        }
        
        
    }

}()

function select_all(el) {
	ge(el).focus();
	ge(el).select();
}

function set_size(perc) {
	ge('progress-bar').style.width=perc_to_size(perc)+'px';
}

function perc_to_size(perc) {
	return 399*perc/100;
}

function submit_form() {
	if (ge('file-sel').value != '') {
		UP.start();
		document.file_submit.submit();
	} else
	{
		ge('error').style.display='block';
	}
}