var formChecker = null;
function swfUploadLoaded() {																										// call back form swfuplod initialise,  directs submit button to doSubmit
	//alert('handlers swfUploadLoaded');
	var btnSubmit = document.getElementById("btnSubmit");
	
//	btnSubmit.onclick = doSubmit;
	btnSubmit.onclick = doSubmitJQ;


	
}





// Called by the submit button to start the upload
//function doSubmitPreview(e) {
function doSubmitPreview() {
		//alert('handler.doSubmitPreview');
/*
		uploading=false;
		found=true;
		index=0;
	  $$('[id^=fsUploadProgress]').each(function(el){
																		//	alert('fred');
				var target = el.readAttribute('id');															 
			 index = target.substring(16,target.length);
																			//alert('fred'+index);
																		 
				var txtFileName = document.getElementById(Manage.swfuList[index].customSettings.txt_filename);
																			//alert(txtFileName.value);														
				if((txtFileName.value == "")||(Manage.swfuList[index].customSettings.upload_successful == true)){
//				if(txtFileName.value == ""){
					//return;															// carryon if no filename
				}else{
					uploading = true;
					try {
						Manage.upCount++;
						Manage.swfuList[index].customSettings.upload_successful = false;													
						Manage.swfuList[index].startUpload();
					} catch (ex) {

					}
				}
	 });

	
	if(!uploading){
		document.forms[0].submit();

/////		var btnSubmit = document.getElementById("btnSubmit");
/////		btnSubmit.onclick = doSubmit;




		return false;																		// normal form submission
	}
	
	e = e || window.event;											/// pause submission until uploads finished
	if (e.stopPropagation) {
		e.stopPropagation();
	}
	e.cancelBubble = true;
	
	
	
//////	var btnSubmit = document.getElementById("btnSubmit");
//////	btnSubmit.onclick = doSubmit;
*/
		document.forms[0].submit(); //extra	
	return false;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Called by the submit button to start the upload
//
//
function doSubmitJQ(e) {
		//alert('handler.doSubmitJQ');
/*	
	if (Manage.checkForm(document.forms[0])){
	}else{
		return false;																	// validation failed
	}
*/
	//alert('doSubmitJQ');


		//alert(Manage2.swfuList.length);


		uploading=false;
	
		found=true;
		index=0;
	  $('[id^=fsUploadProgress]').each(function(el){
			//	alert('fred');

//				var target = el.readAttribute('id');	
			 var target = this.id;			
														 
			 index = target.substring(16,target.length);

			 //alert('ShowIndex:'+index);
						
				if((index != '_xcountx_')&&(index != 'All')){		
					//alert('Inside IF');										 
					var txtFileName = document.getElementById(Manage2.swfuList[index].customSettings.txt_filename);
					//alert('txtFileName.value:'+txtFileName.value);			




					// this should happen on upload complete
	//				var assetFileName = document.getElementById(this.customSettings.asset_filename);
					var assetFileName = document.getElementById(Manage2.swfuList[index].customSettings.asset_filename);
					assetFileName.value = txtFileName.value;
					//alert('assetFileName.value'+assetFileName.value);
											
					if((txtFileName.value == "")||(Manage2.swfuList[index].customSettings.upload_successful == true)){
							//alert('empty filename');
						//return;															// carryon if no filename
					}else{
						//alert('just about to start upload');
						uploading = true;
						try {
							//alert('about to start upload');

							Manage2.upCount++;
							Manage2.swfuList[index].customSettings.upload_successful = false;													
							Manage2.swfuList[index].startUpload();
							//alert('after to start upload');
						} catch (ex) {
							alert("Exception: " + ex.name + " Message: " + ex.message);
						}
					}
				}
				//alert('End if');
	 });
	
	
	//alert('handlers:Set action:End of doSubmitJQ');
/*	
	//tod set up form action
	var form = $$('form.manageForm')[0];															// reset edit form from preview to original submit action
	form.setAttribute("action", Manage2.currentAction);
	form.setAttribute("target", "");
*/	
	if(!uploading){
		//alert('doSubmitJQ returning');
		return;																		// normal form submission
	}

	//alert('Pausing submission');
	e = e || window.event;											/// pause submission until uploads finished
	if (e.stopPropagation) {
		e.stopPropagation();
	}
	e.cancelBubble = true;
	
	//alert('doSubmitJQ return false');
	return false;


//	return true; //todo this is fakery see above
}


//
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// Called by the submit button to start the upload
function doSubmit(e) {
		//alert('handler.doSubmit');
	
	if (Manage.checkForm(document.forms[0])){
	}else{
		return false;																	// validation failed
	}


		uploading=false;
	
		found=true;
		index=0;
	  $$('[id^=fsUploadProgress]').each(function(el){
																		//	alert('fred');
				var target = el.readAttribute('id');															 
			 index = target.substring(16,target.length);
																			//alert('fred'+index);
																		 
				var txtFileName = document.getElementById(Manage.swfuList[index].customSettings.txt_filename);
																			//alert(txtFileName.value);														
				if((txtFileName.value == "")||(Manage.swfuList[index].customSettings.upload_successful == true)){
					//return;															// carryon if no filename
				}else{
					uploading = true;
					try {
						Manage.upCount++;
						Manage.swfuList[index].customSettings.upload_successful = false;													
						Manage.swfuList[index].startUpload();
					} catch (ex) {
						alert("Exception: " + ex.name + " Message: " + ex.message);
					}
				}
	 });
	
	
	
	var form = $$('form.manageForm')[0];															// reset edit form from preview to original submit action
	form.setAttribute("action", Manage.currentAction);
	form.setAttribute("target", "");
	
	if(!uploading){
		//alert('returning');
		return;																		// normal form submission
	}

	e = e || window.event;											/// pause submission until uploads finished
	if (e.stopPropagation) {
		e.stopPropagation();
	}
	e.cancelBubble = true;
	
	return false;
}

 // Called by the queue complete handler to submit the form
function uploadDone() {
	//alert('uploadDone');

	Manage2.upCount--;
	if(Manage2.upCount==0){
	try {
		//alert('submitting');
		document.forms[0].submit();
		//alert('submited');
//////		var btnSubmit = document.getElementById("btnSubmit");
//////		btnSubmit.onclick = doSubmit;
			
	} catch (ex) {
		alert("Error submitting form");
	}
	}
}

function fileDialogStart() {
	//alert('fileDialogStart');
	
	var txtFileName = document.getElementById(this.customSettings.txt_filename);
	txtFileName.value = "";
	
	

	this.cancelUpload();
}



function fileQueueError(file, errorCode, message)  {
	try {
		// Handle this error separately because we don't want to create a FileProgress element for it.
		switch (errorCode) {
		case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
			alert("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
			return;
		case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
			alert("The file you selected is too big.");
			this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
			alert("The file you selected is empty.  Please select another file.");
			this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
			alert("The file you choose is not an allowed file type.");
			this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		default:
			alert("An error occurred in the upload. Try again later.");
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		}
	} catch (e) {
	}
}

function fileQueued(file) {
	//alert('fileQueued');
	try {
		var txtFileName = document.getElementById(this.customSettings.txt_filename);
		txtFileName.value = file.name;
		
	} catch (e) {
	}

}
function fileDialogComplete(numFilesSelected, numFilesQueued) {
	//alert('fileDialogComplete');
	if(numFilesQueued>0){
		this.customSettings.upload_successful = false;
	}
	
//	validateForm();
}

function uploadProgress(file, bytesLoaded, bytesTotal) {
	// causes a lot of pop up if we alert here //alert('Handlers:uploadProgress');
	try {
		var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);

		file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
		var progress = new FileProgress(file, this.customSettings.progress_target);
		progress.setProgress(percent);
		progress.setStatus("Uploading...");
	} catch (e) {
	}
}

function uploadSuccess(file, serverData) {
	//alert('Handlers:uploadSuccess');
	try {
		file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
		var progress = new FileProgress(file, this.customSettings.progress_target);
		progress.setComplete();
		progress.setStatus("Complete.");
//filerejects		progress.toggleCancel(false);
		
		if (serverData === " ") {
			this.customSettings.upload_successful = false;
		} else {
			this.customSettings.upload_successful = true;
			document.getElementById(this.customSettings.hidfileid).value = serverData;
		}
		
	} catch (e) {
	}
}

function uploadComplete(file) {
	//alert('Handlers:uploadComplete');
	try {
		if (this.customSettings.upload_successful) {
//disable upload			this.setButtonDisabled(true);
			
			var txtFileName = document.getElementById(this.customSettings.txt_filename);
			
			var assetFileName = document.getElementById(this.customSettings.asset_filename);				//cpo for swf to asset
			assetFileName.value = txtFileName.value;														//cpo for swf to asset
			
			//alert(txtFileName.value);
			//alert(assetFileName.value);			
			
			uploadDone();
		} else {
			file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
			var progress = new FileProgress(file, this.customSettings.progress_target);
			progress.setError();
			progress.setStatus("File rejected");
//filerejects			progress.toggleCancel(false);
			
			var txtFileName = document.getElementById(this.customSettings.txt_filename);
			
			var assetFileName = document.getElementById(this.customSettings.asset_filename);				//cpo for swf to asset
			assetFileName.value = txtFileName.value;														//cpo for swf to asset
			
			
//filerejects			txtFileName.value = "";
//filerejects			alert("There was a problem with the upload.\nThe server did not accept it.");
			uploadDone();			//filerejects
		}
	} catch (e) {
	}
}

function uploadError(file, errorCode, message) {
	//alert('Handlers:uploadError');
	try {
		
		if (errorCode === SWFUpload.UPLOAD_ERROR.FILE_CANCELLED) {
			// Don't show cancelled error boxes
			return;
		}
		
		var txtFileName = document.getElementById(swfu.customSettings.txt_filename);
		txtFileName.value = "";
//		validateForm();
		
		// Handle this error separately because we don't want to create a FileProgress element for it.
		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
			alert("There was a configuration error.  You will not be able to upload a resume at this time.");
			this.debug("Error Code: No backend file, File name: " + file.name + ", Message: " + message);
			return;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
			alert("You may only upload 1 file.");
			this.debug("Error Code: Upload Limit Exceeded, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			break;
		default:
			alert("An error occurred in the upload. Try again later.");
			this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			return;
		}

		file.id = "singlefile";	// This makes it so FileProgress only makes a single UI element, instead of one for each file
		var progress = new FileProgress(file, this.customSettings.progress_target);
		progress.setError();
		progress.toggleCancel(false);

		switch (errorCode) {
		case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
			progress.setStatus("Upload Error");
			this.debug("Error Code: HTTP Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
			progress.setStatus("Upload Failed.");
			this.debug("Error Code: Upload Failed, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.IO_ERROR:
			progress.setStatus("Server (IO) Error");
			this.debug("Error Code: IO Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
			progress.setStatus("Security Error");
			this.debug("Error Code: Security Error, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
			progress.setStatus("Upload Cancelled");
			this.debug("Error Code: Upload Cancelled, File name: " + file.name + ", Message: " + message);
			break;
		case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
			progress.setStatus("Upload Stopped");
			this.debug("Error Code: Upload Stopped, File name: " + file.name + ", Message: " + message);
			break;
		}
	} catch (ex) {
	}
}

