var scrollTop = 0;
var clientHeight = 0;

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function getDocWidth() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
        Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
        Math.max(D.body.clientWidth, D.documentElement.clientWidth)
    );
}

window.size = function()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}

window.scroll = function()
{
	var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};

	var offsetX = 0;
	var offsetY = 0;

	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	return{left:offsetX,top:offsetY};
}


function findPos(obj) {
	var curleft = curtop = 0;
	var w = obj.offsetWidth;
	var h = obj.offsetHeight;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [ [curleft,curtop] , [curleft+w,curtop+h] ];
}
		
		var lastUpdateStatus;
		var fileBoxName;
		var scriptCaller;

		function validateUpload(fileBoxNameL,scriptCallerL){

			fileBoxName = fileBoxNameL;
			scriptCaller = scriptCallerL;
			var error;
			
			error = "";
			
			if(document.getElementById(fileBoxName).value == "")
			{
				error = "Please Select a File to Upload";
			}	
			
			if(error != "")
			{
				alert(error);
			}
			else
			{
				//alert("uploadInit.pl?fileString=c:\\something\\something\\"+escape(document.getElementById(fileBoxName).value)+"&userId="+sessionUserId+"&rand="+Math.random()+"&caller="+scriptCaller)
								var preDownloadInitRequest = new Ajax.Request("/"+subfolderPath+"/getToken.asp",{method: 'get',	onComplete: step2});
			}
		}

		function step2(iResponse)
		{
				code = iResponse.responseText;
				//alert(code)
				//alert("/"+subfolderPath+"/uploadInit.pl?fileString=c:\\something\\something\\"+escape(document.getElementById(fileBoxName).value)+"&userId="+sessionUserId+"&rand="+Math.random()+"&caller="+scriptCaller+"&downloadKey="+code+"&xtraId="+xtranetId)
				var downloadInitRequest = new Ajax.Request("/"+subfolderPath+"/uploadInit.pl?fileString=c:\\something\\something\\"+escape(document.getElementById(fileBoxName).value)+"&userId="+sessionUserId+"&rand="+Math.random()+"&caller="+scriptCaller+"&downloadKey="+code+"&xtraId="+xtranetId,{method: 'get',	onComplete: initResponse});
		}

				
		function uploadStatusRequest(fileName,downloadKey){
		var uploadStatusRequest = new Ajax.Request("/"+subfolderPath+"/uploadStatus.asp?fileName=" +fileName + "&downloadKey=" +downloadKey+"&rand="+Math.random(),{method: 'get',	onComplete: proccessUploadStatus});
		}
		
		
		
		function proccessUploadStatus(uploadStatusResponse){
		var uploadData = uploadStatusResponse.responseText.split(";");
		var iHTML;
		var uploadStatus;
		
		var fileName = 0;
		var totalDownloaded = 1;
		var totalSize = 2;
		var timeElapsed = 3;
		var transferSpeed = 4;
		var percentComplete = 5;
		var downloadKey = 6;

		
		if(uploadData[totalSize] == uploadData[totalDownloaded] && uploadData[totalDownloaded] == 0)
		{
		uploadStatus = "initializing";
		}
		
		if(uploadData[totalDownloaded] > 0){
		uploadStatus = "uploading";
		}
		
		if(uploadData[totalSize] == uploadData[totalDownloaded] && uploadData[totalDownloaded] != 0)
		{
		clearInterval(updaterInterval);
		uploadStatus = "finalizing";
		}
		if (uploadStatus == undefined)
		{
			uploadStatus = lastUploadStatus;
		}
		else
			{
				lastUploadStatus = uploadStatus;
			}
		iHTML ="";
		iHTML +="<div class='uploadHeader'>Upload Progress</div>";
		iHTML +="<div align='center'>";
		iHTML +="<table style='border:none;'>";
		iHTML +="<tr>";
		iHTML +="<td style='border:none;'>";
		iHTML +="Status:";
		iHTML +="</td>";
		iHTML +="<td style='border:none;' id='statusText'>";
		iHTML +=uploadStatus;
		iHTML +="</td>";
		iHTML +="</tr>";
		iHTML +="<tr>";
		iHTML +="<td style='border:none;'>";
		iHTML +="Filename:";
		iHTML +="</td>";
		iHTML +="<td style='border:none;'>";
		iHTML +=uploadData[fileName];
		iHTML +="</td>";
		iHTML +="</tr>";
		iHTML +="<tr>";
		iHTML +="<td style='border:none;'>";
		iHTML +="Uploaded:";
		iHTML +="</td>";
		iHTML +="<td style='border:none;'>";
		iHTML +=Math.floor(uploadData[totalDownloaded]/1000) + "KB of " + Math.floor(uploadData[totalSize]/1000) + "KB";
		iHTML +="</td>";
		iHTML +="</tr>";
		iHTML +="<tr>";
		iHTML +="<td style='border:none;'>";
		iHTML +="Time Elapsed:";
		iHTML +="</td>";
		iHTML +="<td style='border:none;'>";
		iHTML +=uploadData[timeElapsed];
		iHTML +="</td>";
		iHTML +="</tr>";
		iHTML +="<tr>";
		iHTML +="<td style='border:none;'>";
		iHTML +="Transfer Speed:";
		iHTML +="</td>";
		iHTML +="<td style='border:none;'>";
		iHTML +=uploadData[transferSpeed]+"KBps";
		iHTML +="</td>";
		iHTML +="</tr>";
		iHTML += "</table>";
		iHTML +="</div>";
		iHTML +="<div class='spacerDiv'></div>";
		iHTML +="<div align='center'><div align='left' style='border:1px solid black;width:200px;height:20px;'>";
		iHTML +="<div class='uploadProgress' style='width:"+uploadData[percentComplete]*2+"px;'></div>"
		iHTML +="</div>";
		iHTML +="<div align='center' style='width:200px;padding:0 5px 0 5px;'><div style='display:inline;left:90px;'>"+Math.floor(uploadData[percentComplete])+"%</div></div>";
		iHTML +="<div class='spacerDiv'></div>";
		iHTML+="</div>";
		document.getElementById("progressDiv").style.width = "235px";
		document.getElementById("progressDiv").style.border = "1px solid black";
		document.getElementById("progressDiv").innerHTML = iHTML;
		//document.getElementById("progressDiv").style.opacity=1;
		//document.getElementById("progressDiv").filters.alpha.opacity=100;

		clientHeight = window.scroll()['top'] + window.size()['height'];
		document.getElementById('progressDiv').style.top = ((clientHeight/2-90))+"px";
		
		if (uploadStatus == "finalizing"){
		//var uploadFinalRequest = new Ajax.Request("https://"+location.hostname+"/uploadFinalize.asp?fileName="+uploadData[fileName]+"&downloadKey="+uploadData[downloadKey]+"&platform="+platformVal+"&application="+applicationVal+"&caller="+gCaller,{method: 'get',onComplete: finalizeResponse});			
		finalizeResponse("trash");
		}
		
		}
		
	function finalizeResponse (resp){
				document.getElementById("statusText").innerHTML = "complete";
				setTimeout("document.getElementById('progressDiv').style.display = 'none';",1000)
				setTimeout("document.getElementById('progressDivContainer').style.display = 'none';",1000)
				//alert(document.getElementById("addForm"))
				document.getElementById("addForm").submit();
		}
		
		/*function refreshFilesResponse (resp){
		document.getElementById("filesDiv").innerHTML = resp.responseText;
		
		fileBoxObj = document.getElementById(fileBoxName);
		parentObj = fileBoxObj.parentNode;
		parentObj.removeChild(fileBoxObj);
		fileBoxHolderObj = document.getElementById("fileBoxHolder");
		newFileBoxObj = document.createElement("input");
		newFileBoxObj.setAttribute("type","file");
		newFileBoxObj.setAttribute("name","art");
		newFileBoxObj.setAttribute("id","fileBox");		
		parentObj.insertBefore(newFileBoxObj,fileBoxHolderObj);
		//document.getElementById("fileBox").value = "";
		document.getElementById("platform").selectedIndex = 0
		document.getElementById("creatingApplication").selectedIndex = 0
		}*/
		
		function initResponse(resp){
		//alert(resp.responseText)
		//document.close()
		if (resp.responseText != "")
		{
		fileData = resp.responseText.split(";");


		document.getElementById("addItemDiv").style.display = "none";
		document.getElementById("progressDivContainer").style.left = "0px";
		document.getElementById("progressDivContainer").style.top = "0px";
		document.getElementById("progressDivContainer").style.width = getDocWidth()+"px";
		document.getElementById("progressDivContainer").style.height = getDocHeight()+"px";

		uploadFormObj = document.getElementById("uploadForm");
		//alert(fileData[0])
		uploadFormObj.action = "uploader.pl?fileName=" + fileData[0] + "&downloadKey=" +fileData[1];
		//alert("uploader.pl?fileName=" + fileData[0] + "&downloadKey=" +fileData[1])
		//alert("uploader.pl?fileName=" + fileData[0] + "&downloadKey=" +fileData[1])
		fbox = document.getElementById(fileBoxName)
		fbox.name = fbox.name + "data";
		fbox.id = fbox.name + "data";
		newFileBox = fbox.cloneNode(true);
		newFileBox.id = "fileBox"
		newFileBox.name = "fileBox"

		uploadFormObj.appendChild(fbox);
		aTextBox=document.createElement('input');
		aTextBox.type = 'text';
		aTextBox.name= 'trash';
		uploadFormObj.appendChild(aTextBox);

		aTextBox=document.createElement('input');
		aTextBox.type = 'hidden';
		aTextBox.id= fileBoxName;
		aTextBox.name = fileBoxName;
		aTextBox.value = fileData[0];
		document.getElementById("addForm").appendChild(aTextBox);

		uploadFormObj.submit();
		cDiv = document.getElementById("progressDivContainer");
		p = findPos(cDiv);

		scrollTop = p[0][1];
		scrollLeft = p[0][0];
		clientHeight = window.scroll()['top'] + window.size()['height'];
		clientWidth = p[1][0] - p[0][0];			
		updaterInterval = setInterval("uploadStatusRequest('"+fileData[0]+"','"+fileData[1]+"');",500);
		//document.getElementById("progressDivContainer").style.width = clientWidth+scrollLeft+"px";
		//document.getElementById("progressDivContainer").style.height = clientHeight+scrollTop+"px";
		//document.getElementById("progressDivContainer").style.left = "0px";
		//document.getElementById("progressDivContainer").style.top = "0px";
		document.getElementById('progressDiv').style.top = (scrollTop+(clientHeight/2-90))+"px";
		document.getElementById('progressDiv').style.left = (scrollLeft+(clientWidth/2-120))+"px";
		//document.getElementById('progressDiv').style.opacity = 1.0;
		document.getElementById('progressDiv').style.display = 'block';
		document.getElementById('progressDivContainer').style.display = 'block';
		
		}
		else{alert("You cannot upload that type of file");}
		}
