/**************************************************************************************************
	Predefined Variables
***************************************************************************************************/
	var http_requests = new Array();
	if (window.XMLHttpRequest) var http_request = new XMLHttpRequest();  else var http_request=false;
	//var loading_graphic = "<img src='"+ img_path + "loading_16x16.gif' width='16' height='16' border='0' align='absmiddle'>&nbsp;";
	var loading_graphic = "<img src='"+ img_path + "loading_16x16.gif' >&nbsp;";
/**************************************************************************************************
	Set ScrollBox Heights
***************************************************************************************************/
//** FUNCTION: Set SrollBox Height Based on Element Input Array
	function checkPage(scrollBox,el_array) {
		var adjustOffset = 0;
		if (eval(el_array)) {
			for (i=0; i< el_array.length; i++) {
				if (eval(document.getElementById(el_array[i])) && !isNaN(document.getElementById(el_array[i]).offsetHeight)) {
					adjustOffset = adjustOffset + document.getElementById(el_array[i]).offsetHeight;
				}
			}
		}
		var height = document.body.clientHeight - adjustOffset;
        document.getElementById(scrollBox).style.height=height+'px';
	}
/**************************************************************************************************
	AJAX Server Requests
***************************************************************************************************/
	function build_HttpRequest() {
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			var request = new XMLHttpRequest();
			if (request.overrideMimeType) { request.overrideMimeType('text/xml');}
		} else if (window.ActiveXObject) { // IE
			try {var request = new ActiveXObject("Msxml2.XMLHTTP");}
			catch (e) {
				try {var request = new ActiveXObject("Microsoft.XMLHTTP");} catch (e) {}
			}
		}
		if (!request) { alert('Cannot create an XMLHTTP instance');return false;}
		return request;
	}
//** FUNCTION: Make Request to Server passing GET variables
	function makeRequest(url,do_function) {
		http_request = build_HttpRequest();
		var prefix = "";
      	if (document.domain.indexOf('secure') !=-1)
        {
	           prefix = "https://secure.fileden.com/";
        } else {
               prefix ="";
        }

        url = prefix + url;

		var time = new Date();
		if (url.indexOf('?')>0){ url = url + '&time='+time.getTime(); } else { url = url + '?time='+time.getTime();}
		http_request.onreadystatechange = do_function;
		http_request.open('GET', url, true);
		http_request.send(null);
	}
/**************************************************************************************************
	Upload Form Functions
***************************************************************************************************/
//** FUNCTION: Create Upload Form
	function createFileInput(path) {
        //if (chkObject(false, document.getElementById('list_body'))){
        if (chkObject(false, 'list_body')){
          var tbody = document.getElementById('list_body').getElementsByTagName('tbody')[0];
      	// Create Table Row
      		var tr = document.createElement("tr");
      		if (bg_color) { tr.className="on"; bg_color= false; } else { bg_color = true;}
      	// Create Table Cell
      		var td = document.createElement("td");
      		td.id='upload_'+count;
      		td.width='100%';
      		//path += document.getElementById('upload_to')[document.getElementById('upload_to').selectedIndex].value;
      		path += upload_to;
      		var prefix = "";
      		if (document.domain.indexOf('secure') !=-1)
            {
	           prefix = "https://secure.fileden.com/";
            } else {
               prefix ="";
            }
      		var output = new Array();
      		output.push("<form id='frmUpload_"+count+"' target='uploadForm' method='post' enctype='multipart/form-data' action='"+prefix+"dynamic/upload.cgi?sid="+sid+"'>");
      		output.push("<input type='hidden' name='path' value='"+path+"'>");
      		output.push("<input style='margin:10px;width:90%;' size='50' type='file' name='filename' ><\/form>");
      		td.innerHTML = output.join('');
      		tr.appendChild(td);
          tbody.appendChild(tr);
      		uploads.push(count);
      		/*if (count != 0 ) {
      			var tiggerForm = document.getElementById('frmUpload_'+count);
      			tiggerForm['filename'].click();
      			tiggerForm = null;
      		} */
      		count++;
      	}
	}
//** FUNCTION: Check form file Extensions that pose a Security Issue
	function checkFileExtentions(form){
		if(check_file_extentions == false){ return false; }
		var re = /(\.php)|(\.sh)$/i;   //Change line 126 in uber_uploader.cgi to match
		if(form['filename'].value != ""){
			if(form['filename'].value.match(re)){
				var string = form['filename'].value;
				var num_of_last_slash = string.lastIndexOf("\\");
				if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }
				var file_name = string.slice(num_of_last_slash + 1, string.length);
				var file_extention = file_name.slice(file_name.indexOf(".")).toLowerCase();
				alert('Sorry, uploading a file with the extention "' + file_extention + '" is not allowed.');
				return true;
			}
		}
		return false;
	}
//** FUNCTION: Process Queued Uploads
	function upload() {

		if (uploads.length>0) {
			form = document.getElementById('frmUpload_'+uploads[0]);
			if (form["filename"].value == ""){
				alert("Please Choose a file to upload.");
			} else {
				filename = form["filename"].value;
				if (filename.lastIndexOf("\\")>0) {
					filename = filename.substring(filename.lastIndexOf("\\")+1,filename.length);
				} else if (filename.lastIndexOf("/")>0) {
					filename = filename.substring(filename.lastIndexOf("/")+1,filename.length);
				}
				//if(checkFileExtentions(form)){ return false; }
				makeRequest("upload_progress.php?sid="+sid+"&filename="+filename,progress);
				form.submit();
				document.getElementById("btn_upload").innerHTML = loading_graphic+" Uploading...";
			}
		}
	}
//** FUNCTION: Process Response From Ajax Request
	function progress() {

		switch (http_request.readyState) {
			case 1 : checkCount++; break;
			case 4 :
				if (http_request.status == 200) {
					if (debug) status.innerHTML = checkCount + " - " +http_request.responseText;
					response = http_request.responseText.split('|');
					upload_cell = document.getElementById('upload_'+uploads[0]);
					switch(response[0]) {
						case "wait":
							var output = new Array();
							var ext = response[1].substring(response[1].lastIndexOf(".")+1,response[1].length);
							output.push("<table cellspacing='0' cellpadding='0' border='0' width='98%' style='margin-top:10px;'>");
							output.push("<tr>");
							output.push("<td rowspan='2' width='30' height='40' class='img_32_"+ext+"'>&nbsp;<\/td>");
							output.push("<td class='upload_filename'>"+response[1]+"<\/td>");
							output.push("<\/tr>");
							output.push("<tr><td class='upload_stats'>Waiting for Download to Start ("+checkCount+")<\/td><\/tr>");
							output.push("<\/table>");
							upload_cell.innerHTML = output.join('');
							setTimeout('makeRequest("upload_progress.php?sid='+sid+'",progress);',500);
							break;
						case "started":
							makeRequest("upload_progress.php?sid="+sid,progress);
							break;
						case "downloading":
							var output = new Array();
							var ext = response[1].substring(response[1].lastIndexOf(".")+1,response[1].length);
							output.push("<table cellspacing='0' cellpadding='0' border='0' width='98%' style='margin-top:10px;'>");
							output.push("<tr>");
							output.push("<td rowspan='3' width='30' height='40' class='img_32_"+ext+"'>&nbsp;<\/td>");
							output.push("<td class='upload_filename'>"+response[1]+"<\/td>");
							output.push("<\/tr>");
							output.push("<tr><td><div id='upload_bar'><div style='width:"+response[3]+"%;'>&nbsp;<\/div><\/div><\/td><\/tr>");
							output.push("<tr><td class='upload_stats'>"+response[4]+" of "+response[5]+" ("+response[6]+"/s) " +response[2]+"<\/td><\/tr>");
							output.push("<\/table>");
							upload_cell.innerHTML = output.join('');
							setTimeout('makeRequest("upload_progress.php?sid='+sid+'",progress);',1333);

							break;
						case "copying":
							var output = new Array();
							var ext = response[1].substring(response[1].lastIndexOf(".")+1,response[1].length);
							output.push("<table cellspacing='0' cellpadding='0' border='0' width='98%' style='margin-top:10px;'>");
							output.push("<tr>");
							output.push("<td rowspan='2' width='30' height='40' class='img_32_"+ext+"'>&nbsp;<\/td>");

							output.push("<td class='upload_filename'>"+response[1]+"<\/td>");
							output.push("<\/tr>");
							output.push("<tr><td class='upload_stats'>Copying file to destination directory<\/td><\/tr>");
							output.push("<\/table>");
							upload_cell.innerHTML = output.join('');
							setTimeout('makeRequest("upload_progress.php?tmp_sid='+tmp_sid+'",progress);',1000);
							break;
						case "Success":
							uploads.splice(0,1);
							var output = new Array();
							var ext = response[1].substring(response[1].lastIndexOf(".")+1,response[1].length);
							output.push("<table cellspacing='0' cellpadding='0' border='0' width='98%' style='margin-top:10px;'>");
							output.push("<tr>");
							output.push("<td rowspan='2' width='30' height='40' class='img_32_"+ext+"'>&nbsp;<\/td>");
							output.push("<td class='upload_filename'>"+response[1]+"<\/td>");
							output.push("<\/tr>");
							output.push("<tr><td class='upload_stats'>"+response[2]+"<\/td><\/tr>");
							output.push("<\/table>");
							upload_cell.innerHTML = output.join('');
                     document.getElementById("btn_upload").innerHTML = '<a href="javascript:upload();" class="two"><\/a>';
							checkCount = 0;
							setTimeout("upload();",1000);
							if (uploads.length == 0) { document.getElementById('upload_form').submit();}
							break;
						default:
							alert(http_request.responseText);

					}
				} else { alert("There was a problem with the request.("+http_request.responseText+")"); }
				break;
		}

	}
//** FUNCTION: Cancel Upload
	function cancelUpload(msg) {
		alert(msg);
	}

	function chkObject(inParent,theVal) {
      if(inParent){
    	  if (window.opener.document.getElementById(theVal) != null) {
    	     return true;
    	  } else {
    	     return false;
    	  }
      }else{
    	  if (document.getElementById(theVal) != null) {
    	     return true;
    	  } else {
    	     return false;
    	  }
      }
    }
