// JavaScript Document

function embedUploader()
{
	//init variables
	var requiredFlashVersion = "9.0.28";				   
	//set up and embed the uploader for zip files
	var flashvars_script = {};
	
	flashvars_script.configFile = "/publications/apps/news/common/xml/uploadConfig_script_"+getTier()+".xml";
	var params_script = {};

	params_script.bgcolor = "#FFFFFF";
	params_script.wmode = "transparent";
	var attributes_script = {};
	attributes_script.name = "scriptUpload";
	swfobject.embedSWF("/shared/Flash/CodeGalleryUploader.swf", "scriptUpload", "300", "35", requiredFlashVersion, "/Shared/JSApps/swfobject_2.0/expressInstall.swf", flashvars_script, params_script, attributes_script);		
}



function getTier(){
	var domain = window.location.toString().split(".");
	var subDomain = domain[0];
	var tier = "prd";
	if(subDomain.indexOf("dev") >= 0)
		tier = "dev";
	else if(subDomain.indexOf("stg") >= 0)
		tier = "stg";	
		
	return tier;
}

function newUploadStarted(caller){
	$('#UploadStartimg').css("display","block");
	var theDiv = getMessageDiv(caller);
	//showing Ajax Loader
	theDiv.innerHTML = "";
	thisDiv.style.display = "none";
}


function fileSizeViolation(caller, eventObject, maxFileSize){
	var theDiv = getMessageDiv(caller);
	theDiv.innerHTML = "The file you have chosen is TOO LARGE.<br/>The maximum file size is 10 MB";
}


function uploadError(caller, eventObject, data){
	var theDiv = getMessageDiv(caller);
	theDiv.innerHTML = "We are sorry but there was a problem. Please try again later.";
}

function singleFileUpload(caller, eventObject, data, reference){
	$('#UploadStartimg').css('display','none');
	var theDiv = getMessageDiv(caller);
	$('#filename').val(data.serverFileName);
	theDiv.className = "reqColor";
	theDiv.innerHTML = "The file <strong>" + data.clientFileName + "</strong> has been uploaded and will be placed in the appropriate project folder after the form is submitted.";
}

function IOError(caller, error){
	var theDiv = getMessageDiv(caller);
	theDiv.innerHTML = "We are sorry but there is a problem connecting to the server.<br/>Please try again shortly.";
}

function SecurityError(caller, error){
	var theDiv = getMessageDiv(caller);
	theDiv.innerHTML = "We are sorry but there is a problem connecting to the server.<br/>Please try again later.";
}

function toKilobytes(_size){
	var kb = String(Math.round(_size/1024));
	return kb;
}

function getMessageDiv(caller){
	var msDiv = "";
	if(caller == "scriptUpload"){
		msDiv = document.getElementById('scriptMessage');
		msDiv.style.display = "block";
	}
	return msDiv;
}	

function getCurrentFile(caller){
	try{
		var theForm = document.forms['codeGalleryForm'];
		if(caller == "scriptUpload"){
			return theForm.clientFile.value;	
		}else{
			return theForm.screenshotFileName.value;	
		}
	}catch(e){
		return "";	
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}