// ********************
// Begin Popup Single File upload
// ********************
var popFileDstFld;
var popFileUploadWin;

// ******************************
// Expected params:
// [0] Window Name
// [1] Destination Field
// [2] Destination Field Substitute

// [3] Universe id
// [4] Item type
// [5] Property key
// [6] Unique upload id
// [7] Portal id
// [8] Url base
// [9] File Validation pattern
// [10] impl id

// [..] Window Title
// [..] Uplaod button text
// [..] Default text
// [..] Wrong type text
// [..] Close button text

// ******************************
function popupUploadSingle()
{
	var dstWindowName = "";

	// Initialize the window to an empty object.
	popFileUploadWin = new Object();
	
	// Check for the right number of arguments
	if (arguments.length < 16)
	{
		alert("popupUploadSingle(): Wrong number of arguments.");
		return void(0);
	}
	// get the command line arguments 
	dstWindowName = popupUploadSingle.arguments[0];
	popFileDstFld = popupUploadSingle.arguments[1];
	popFileDstFldSubst = popupUploadSingle.arguments[2];
	
	// lookup dst field 
	if (popFileDstFld != "") {
	  popFileDstFld = document.getElementById(popFileDstFld);
	}
	if (popFileDstFldSubst != "") {
	  popFileDstFldSubst = document.getElementById(popFileDstFldSubst);
	}

	//Call the routine to draw the file upload
	loadUploadPopup(dstWindowName, popupUploadSingle.arguments[3], popupUploadSingle.arguments[4], popupUploadSingle.arguments[5], popupUploadSingle.arguments[6], popupUploadSingle.arguments[7], popupUploadSingle.arguments[8], popupUploadSingle.arguments[9], popupUploadSingle.arguments[10], popupUploadSingle.arguments[11], popupUploadSingle.arguments[12], popupUploadSingle.arguments[13], popupUploadSingle.arguments[14], popupUploadSingle.arguments[15]);
	
	return void(0);
}
 
function closeUploadPopup()
{
	//Can't tell the child window to close itself, the parent window has to 
	//tell it to close.
	popFileUploadWin.close();
	return void(0);
}
 
function loadUploadPopup(dstWindowName, uniId, itemType, propertyKey, uuid, portalId, urlBase, validationPattern, implId, wndTitle, uploadStartText, defaultText, wrongTypeText, closeLabel) 
{
	//Set the window's features here
	var windowFeatures = "toolbar=no, location=no, status=no, menubar=no, scrollbars=no, resizable=no, height=270, width=270, top=" + ((screen.height - 270)/2).toString()+",left="+((screen.width - 270)/2).toString();
	
	//Get the calendar data
	var popUploadHtml = uploadPopupSetupHtml(dstWindowName, uniId, itemType, propertyKey, uuid, portalId, urlBase, validationPattern, implId, wndTitle, uploadStartText, defaultText, wrongTypeText, closeLabel);
 
	//Check to see if the window has been initialized, create it if it hasn't been
	if( popFileUploadWin.toString() == "[object Object]" )
	{
		popFileUploadWin = window.open("", dstWindowName, windowFeatures);
		popFileUploadWin.opener = self;
		// Window im Vordergrund
		popFileUploadWin.focus();
	}
	else 
	{
    popFileUploadWin.document.close();
		popFileUploadWin.document.clear();
  }
	
	// this is the line with the big problem
  popFileUploadWin.document.write(popUploadHtml);
	return void(1);
}
 
function uploadPopupSetupHtml(dstWindowName, uniId, itemType, propertyKey, uuid, portalId, urlBase, validationPattern, implId, wndTitle, uploadStartText, defaultText, wrongTypeText, closeLabel)
{
	var res = "";
	res += ("<HTML><HEAD><TITLE>"+wndTitle+"</TITLE></HEAD><BODY BGCOLOR=\"#F1F1F1\" TEXT=\"#000000\" LINK=\"#364180\" ALINK=\"#FF8100\" VLINK=\"#424282\"><FONT SIZE=\"3\">");
	res += ("<DIV align=\"center\">");
  res += ("<script src=\""+urlBase+"DesktopModules/SitewalkContortionist/controls/edit/shPopupUploadSingle.js\" type=\"text/javascript\"></script>");
	res += ("<form action=\""+urlBase+"DesktopModules/SitewalkContortionist/controls/edit/upload.ashx?muid="+uuid+"&uniid="+uniId+"&itt="+itemType+"&ptk="+propertyKey+"&pid="+portalId+"&iid="+implId+"\" enctype=\"multipart/form-data\" method=\"post\" target=\"UIF\" name=\"UPFO\" id=\"UF\" onsubmit=\"if(window.opener.popFileDstFldSubst)window.opener.popFileDstFldSubst.value = document.UPFO.alt_file_name.value;window.opener.popFileDstFld.value = document.UPFO.alt_file_name.value; sw_sh_upload_single_startUpload('"+uuid+"', '"+urlBase+"');\">");
	res += ("<input type=\"file\" id=\"file1\" name=\"file1\" class=\"fu\" size=\"18\" onchange=\"sw_sh_upload_single_checkFileExists('"+urlBase+"', '"+uniId+"', '"+itemType+"', '"+propertyKey+"', '"+portalId+"', '"+implId+"', '"+validationPattern+"', '"+wrongTypeText+"');\" /><br/>");
	res += ("<input type=\"hidden\" id=\"alt_file_name\" value=\"\" />");
	res += ("<input type=\"submit\" name=\"start_upload_button\" value=\""+uploadStartText+"\" disabled=\"true\" />");
	res += ("</form>");
	res += ("<div id=\"UPP\"><div id=\"UPO\"><div id=\"UPB\"></div><div id=\"UPBT\">"+defaultText+"</div></div></div>");
	res += ("<iframe id=\"UIF\" name=\"UIF\" style=\"display:none\" ></iframe>");
	res += ("</DIV><BR>"); 
	res += ("<CENTER><input type=button value=\""+closeLabel+"\" onClick=\"javascript:window.opener.closeUploadPopup()\"></CENTER>");
	res += ("</FONT></BODY></HTML>");
	return( res );
}

function sw_sh_upload_single_checkFileExists(urlBase, uniId, itemType, propertyKey, portalId, implId, validationPattern, wrongTypeText){
  var orig = sw_sh_upload_single_stripFilePath(document.UPFO.file1.value);
  document.UPFO.start_upload_button.disabled = true;
  var pattern = new RegExp(validationPattern)
  if (pattern.test(orig)){
    request = sw_sh_upload_single_createXMLHttpRequest();
    window.setTimeout("sw_sh_upload_single_checkFile(\"" + orig + "\", \"" + urlBase + "\", \"" + uniId + "\", \"" + itemType + "\", \"" + propertyKey + "\", \"" + portalId + "\", \"" + implId + "\")", 200);
  } else {
    document.getElementById("UPBT").innerHTML = wrongTypeText;
  }
}

function sw_sh_upload_single_checkFile(origName, urlBase, uniId, itemType, propertyKey, portalId, implId){
    request.open("GET", urlBase + "DesktopModules/SitewalkContortionist/controls/edit/CheckFile.ashx?uniid="+uniId+"&itt="+itemType+"&ptk="+propertyKey+"&pid="+portalId+"&iid="+implId+"&orig=" + encodeURIComponent(origName), true);
    request.onreadystatechange = function() {
        if (request.readyState == 4) { 
            if(request.status == 200) {
              document.UPFO.alt_file_name.value = request.responseText;
              document.UPFO.start_upload_button.disabled = false;
              document.getElementById("UPBT").innerHTML = "";
            }
        }
    }
    request.send(null);
}


function sw_sh_upload_single_stripFilePath(fileWithPotentialPath){
  var res = fileWithPotentialPath;
  var pos = fileWithPotentialPath.lastIndexOf('\\');
  if (pos >= 0) {
    res = fileWithPotentialPath.substr(pos + 1);
  }
  return res
}


function sw_sh_upload_single_createXMLHttpRequest(){
    if (typeof XMLHttpRequest != "undefined") { return new XMLHttpRequest();} 
    else if (typeof ActiveXObject != "undefined") { return new ActiveXObject("Microsoft.XMLHTTP");} 
    else { throw new Error("XMLHttpRequest not supported");}
}

function sw_sh_upload_single_update(uuid, urlBase){
    request.open("GET", urlBase + "DesktopModules/SitewalkContortionist/controls/edit/UploadProgress.ashx?muid=" + uuid, true);
    request.onreadystatechange = function() {
        if (request.readyState == 4) { 
            if(request.status == 200) {
              var upo = document.getElementById("UPO")
              if (upo != null) {
                //var ups = eval('(' + request.responseText + ')');
                //upo.innerHTML = ups.pc
                upo.innerHTML = request.responseText;
              }
            }
        }
    }
    request.send(null);
}

function sw_sh_upload_single_startUpload(uuid, urlBase){
    request = sw_sh_upload_single_createXMLHttpRequest();
    window.setInterval("sw_sh_upload_single_update(\"" + uuid + "\", \"" + urlBase + "\")", 1000);
    return true;
}

 
 
 


// ******************
// End Popup Upload Single
// ******************


