// JavaScript Document
var currentElementId=null;
var currentResponseHandler=null;
var currentUploadAssetDisplayName=null;
var uploadInProgress=false;
function generateFileUploadFunction(elementID,responseHandler) {
	//
	//
	currentResponseHandler=responseHandler;
	currentElementId = elementID;
	
	var theContent = "<form id=\"ContentFormNewAsset\" action=\"/servlets/MultipartParserServlet\" method=\"post\" enctype=\"multipart/form-data\" target=\"_new\" onsubmit=\"return formSubmitEvent();\" onreset=\"formResetEvent();\">";
	theContent += "<input name=\"MPP_CONTENT_TYPE\" value=\"application/octet-stream\" type=\"hidden\" ID=\"MPP_CONTENT_TYPE\"/>";
	theContent += "<input name=\"MPP_OBJECT_PATH\" value=\"\" type=\"hidden\" ID=\"MPP_OBJECT_PATH\"/>";
	theContent += "<input name=\"MPP_OBJECT_NAME\" value=\"\" type=\"hidden\" ID=\"MPP_OBJECT_NAME\"/>";
	theContent += "<input name=\"MPP_HANDLER_DEF\" value=\"\" type=\"hidden\" ID=\"MPP_HANDLER_DEF\"/>";
	theContent += "<input name=\"FILE_NEW_ASSET\" type=\"file\" />";
	theContent += "&nbsp;<input type=\"submit\" class=\"fileFormSubmit\" id=\"Submit\" name=\"Submit\" value=\"OK\"/>";
	theContent += "<input type=\"reset\" class=\"fileFormSubmit\" id=\"cancel\" name=\"cancel\" value=\"Cancel\"/>"
	theContent += "</form>";
	//
	//
	var divscontent = document.getElementById(currentElementId);
	divscontent.innerHTML = theContent;
}
/**
 *
 */
function formSubmitEvent() {
	//
	//
	if(uploadInProgress == true) {
		currentResponseHandler(false,"Sorry, your previous upload has not finished!");
		return false;
	}
	//
	// set up emulator client and response handler
	var theForm=document.getElementById("ContentFormNewAsset");
	var fth=document.getElementById("FormTargetHandler");
	theForm.target=fth.name;
	//
	//
	var loginState = isLoggedIn();
	if(loginState == false) {
		currentResponseHandler(false, "Sorry, you need to be logged in to upload a photo!");
		uploadInProgress = false;
		return false;
	}	
	//
	// get the selected files full path on client machine
	var strVal = theForm.FILE_NEW_ASSET.value;
	if(strVal==null || strVal==undefined || strVal.length<4) {
		currentResponseHandler(false, "Sorry, the selected file is not supported by " + SERVICE_BRAND_DISPLAY_NAME);
		uploadInProgress = false;
		return false;
	}
	//
	// create path ID for asset
	currentAssetPath = strVal.replace(/[^A-Za-z0-9\+\/\=\\\/.]/g, "_");
	
	//
	// parse display name
	var lastDelimIndex = currentAssetPath.lastIndexOf(filePathDelimChar)+1;
	if(lastDelimIndex>0 && lastDelimIndex<currentAssetPath.length) {
		currentUploadAssetDisplayName=currentAssetPath.substring(lastDelimIndex);
		currentAssetDisplayName=currentUploadAssetDisplayName;
	}
	else {
		currentUploadAssetDisplayName=strVal;
		currentAssetDisplayName=currentUploadAssetDisplayName;
	}
	//
	// make path and name ID data sufficiently unique in account context
	var timeNow = new Date();
	var timeNowTech = timeNow.getTime();
	var lastDotIndex = currentAssetPath.lastIndexOf(".");
	if(lastDotIndex>0) {
		var firstPart = currentAssetPath.substring(0, currentAssetPath.lastIndexOf("."));
		firstPart += "" + timeNowTech;
		var lastPart = currentAssetPath.substring(currentAssetPath.lastIndexOf("."));
		currentAssetPath = firstPart + lastPart;
	}
	else {
		currentAssetPath += "" + timeNowTech;
	}
	//
	// create technical name for asset
	currentAssetName = currentAssetPath.substring(currentAssetPath.lastIndexOf(filePathDelimChar)+1);
	//
	// generate ID for asset (for this service we rely on accountID beeing unique, 
	// new upload automatically replaces previous)
	var accountID = getAccountID();
	currentAssetID = "";
	if(accountID!=null && accountID!=undefined)
		currentAssetID = accountID;
	else{
		currentResponseHandler(false); 
		uploadInProgress = false;
		return false;
	}
	currentAssetID += "." + timeNowTech;
	//
	// Required: create base 64 encoded set of commands that define the handler tool chain to execute for uploaded asset
	var aMimeType = resolveMimeType(currentAssetName);
	var decodedCmdSet = "";
	if(aMimeType=="image/jpeg") {
		//alert("image");
		decodedCmdSet = "<COMMANDS>" +
						"<COMMAND CLASS_NAME=\"com.tenduke.services.multimediatranscoder.ScaleImage\"" +
								" OBJECT_PATH=\"" + encode64(currentAssetPath) + "\"" +
								" OBJECT_NAME=\"" + encode64(currentAssetName) + "\"" +
								" OUTPUT_PATH=\"" + encode64(currentAssetPath + THUMB_FILE_POSTFIX) + "\"" +
								" OUTPUT_NAME=\"" + encode64(currentAssetName + THUMB_FILE_POSTFIX) + "\"" +
								" WIDTH=\"100\"" +
								" PRESERVE_ASPECT=\"true\"" +
								" IS_ENCODED=\"true\"" +
								"/>" +
						"</COMMANDS>";
	}
	else if(resolveObjectType(currentAssetName)=="OBJECT_TYPE_VIDEO_FILE") {
		decodedCmdSet = "<COMMANDS>" +
						"<COMMAND CLASS_NAME=\"com.tenduke.services.multimediatranscoder.TranscodeVideo\"" +
								" OBJECT_PATH=\"" + encode64(currentAssetPath) + "\"" +
								" OBJECT_NAME=\"" + encode64(currentAssetName) + "\"" +
								" TARGET_FORMAT=\".flv\"" +
								" TARGET_TYPE=\"video\"" +
								"/>" +
						"<COMMAND CLASS_NAME=\"com.tenduke.services.multimediatranscoder.TranscodeVideo\"" +
								" OBJECT_PATH=\"" + encode64(currentAssetPath) + "\"" +
								" OBJECT_NAME=\"" + encode64(currentAssetName) + "\"" +
								" TARGET_FORMAT=\"" + THUMB_FILE_POSTFIX + "\"" +
								" TARGET_TYPE=\"thumbnail\"" +
								"/>" +
						"</COMMANDS>";
	}
	else {
		currentResponseHandler(false, "Sorry, the selected file was not recognized!");
		uploadInProgress = false;
		return false;
	}
	
	//alert("decodedCmdSet: "+decodedCmdSet);
	//
	//
	document.body.style.cursor = "wait";
	uploadInProgress = true;
	//
	// Required: set MPP_OBJECT_PATH and MPP_OBJECT_NAME values
	theForm.MPP_OBJECT_PATH.value = encode64(currentAssetPath);
	theForm.MPP_OBJECT_NAME.value = encode64(currentAssetName);
	//
	// Required: set MPP_HANDLER_DEF value (base64 encode above command set)
	theForm.MPP_HANDLER_DEF.value = encode64(decodedCmdSet);
	//
	//
	return true;
}
/**
 *
 */
function formResetEvent() {
	document.body.style.cursor = "default";
	uploadInProgress = false;
		
	currentResponseHandler(false,"RESET");
		
	currentUploadAssetDisplayName=null;
}
/**
 *
 */
function formSubmitResponse() {
	if(currentUploadAssetDisplayName!=null) {
		
		document.body.style.cursor = "default";
		uploadInProgress = false;
		
		
		
		currentUploadAssetDisplayName=null;
		//
		//
		//
		if(addAssetToContainer(currentPath, currentName, currentAssetPath, currentAssetName, currentAssetID, currentAssetDisplayName, resolveObjectType(currentAssetName), resolveMimeType(currentAssetName))==0) {
			currentResponseHandler(true);
		}
		else{
			currentResponseHandler(false,"1 Sorry, the selected file was not recognized!");
		}
		
	}
	else{
		uploadInProgress = false;
		//currentResponseHandler(false, "Sorry, the file upload failed!");
	}
}
