// =============================================================
// JavaScript Document
// Autors : Marano Marco
//==============================================================
// Dependance :
// mywindows.js
// myupload.js
// myprogressbar.js
// prototype.js
// scriptaculous.js
// =============================================================
// EXEMPLE
// =============================================================
// <form id="uploadFrm" name="uploadFrm" enctype="multipart/form-data" target="myupload_iframe_controller" class="cssform" method="post" action="/admin/php/uploadProgress.php">
// <input type="hidden" name="UPLOAD_IDENTIFIER" id="UPLOAD_IDENTIFIER" value="{$progress_key|escape}"/>
// <label for="upload_file_name">File <span class="requireField">*</span> :</label>
// <input type="file" name="upload_file_name" id="upload_file_name" maxlength="10000000" accept="application/msword,image/*,application/msexcel,application/pdf" style="width:325px; display:{if $file->file_original_name == ""}inline{else}none{/if};" onchange="fileController.startUpload();" />
// <input type="hidden" name="MAX_FILE_SIZE" value="10000000">
// </form> 
// =============================================================


var fileController = {
	
	mywindow   				: null,
	uploadType 				: null,
	myForm	   				: null,
	uploadControllerUrl		: null,
	upload_newFileName  	: null,
	upload_originalFileName : null,
	upload_newFileId	    : null,
	deleteTarget		    : null,
	_onCompletefunction		: null,
	_onDeletedfunction		: null,
	
	startUpload : function (type,MyForm,uploadControllerUrl,language){
		
		
		this.uploadType 		 = type;
		this.myForm				 = MyForm;
		this.uploadControllerUrl = uploadControllerUrl;
		this.language            = language;
		
		validExt = false;
		
		switch(this.uploadType){
			case 'photo' 	 :
				cCount    = 0;  
				extension = new Array('.jpg');
				break;
			case 'cv' :
				cCount    = 0; 
				extension = new Array('.doc','.docx','.pdf','.txt');	
				break;
			case 'documents' :
				cCount    = parseInt($('cfilescount').value); 
				extension = new Array('.doc','.docx','.pdf','.xls','.txt');
				break;
			default :
				extension = new Array('.doc','.docx','.pdf','.xls','.ppt','.jpg','.gif','.png','.tif','.wmv','.avi','.zip','.rar','.txt');
				break;
		}
		
		file = $(this.myForm).upload_file_name.value.toLowerCase();
		
		i=0
		while(i<extension.length && validExt == false){
			if(file.indexOf(extension[i]) != -1){
				validExt = true;			
			}
			i++;
		}
		
				
		if(!validExt || file == "" || cCount >= 5 ){
			
			if(file == "")
				var message = "<em><strong>Please select a new file</strong></em>";
			else
				var message = "<em><strong>Invalid file extension<br>please select a new file</strong></em>";
				
			if(cCount >= 5 ){
				var message = "<em><strong>Maximum number of files allowed</strong></em>";	
			}
				
			var option  = {
							windowContent: ""+message+"",
							overlayColor: '#575759',
							windowType:'alert',
							windowHeight:'100',
							windowWidth:'250',
							btnOk:'ok'						
						  };
			this.mywindow = new myWindow('alert_box',option);
		}else{
			// identifier arleady pass by ASP
			//$('UPLOAD_IDENTIFIER').value = this.getUniqueID();
			this.mywindow = new myWindow('upload_window',{overlayColor: '#575759',windowType:'upload',windowWidth:'262',appearEffect:true, uploadControllerUrl:this.uploadControllerUrl, language: this.language});
			//this.mywindow.uploadController.options.onComplete  = this.onUploadComplete.bind(this);
			this.mywindow.uploadController.options.onError  = this.onUploadError.bind(this);
			this.mywindow.uploadController.filename = file
			this.mywindow.uploadController.options.language = this.language;
			

			$(MyForm).submit();
			setTimeout(this.launchUploadController.bind(this),2000);
			
			//setTimeout(function (){ $(MyForm).submit()},2500);
		}
	},
	
	launchUploadController :function(){
		this.mywindow.uploadController.start();
	},
	
	
	onUploadError : function (){
		this.mywindow.setAlertButton();	
	},
	
	onUploadComplete : function (){
		this.mywindow.options.btnCustomCallback = this.onUploadCompleteAction.bind(this);
		
		switch(this.language){
			case 'fr':
				this.mywindow.options.btnCustom  = "continuer"
				break;
			case 'nl':
				this.mywindow.options.btnCustom  = "continue"
				break;
			case 'en':
				this.mywindow.options.btnCustom  = "continue" 
				break;
		}	
		
		this.mywindow.setCustomsButton();
		$(this.myForm).upload_file_name.value = '';
	},
	
	onUploadCompleteAction : function (){
		this.mywindow.hideWindow();				
		if(this.onCompletefunction != null)
			this._onCompletefunction();
	},
	
	
	getUniqueID : function(){ 
		var uniqueID = new Date();
		var myRandom=Math.floor(Math.random()*1000)
		return uniqueID.getTime()+''+myRandom;
	},
	
	
	deleteFile : function(type,callback){

		this.uploadType 	= type;
		
		switch(this.uploadType){
			case 'photo':
				message = "<strong>Are you sure to delete this file ?";
				break;
			case 'cv':
				message = "<strong>Are you sure to delete this file ?";
				break;
			case 'documents' :
				this.deleteTarget	= $(cible);
				message = "<strong>Are you sure to delete this file ?";
				break;		
		}
		
		
		switch(this.language){
			case 'fr':
				message = "<strong>Etes vous sure de vouloir supprimer ce fichier</strong>";
				break;
			case 'nl':
				message = "<strong>Are you sure to delete this file ?</strong>";
				break;
			case 'en':
				message = "<strong>Are you sure to delete this file ?</strong>";
				break;
		}	
		
		
		if(callback != null){
			this._onDeletedfunction = callback; 
		}
		
		if(this._onDeletedfunction != null)
			_onAgree = this._onDeletedfunction.bind(this);
		else
			_onAgree = this.deleteFile_default_callback.bind(this);			
		
		var option  = {
						windowContent: ""+message+"",
						overlayColor: '#575759',
						appearEffect: true,
						windowType:'dialog',
						windowHeight:'100',
						windowWidth:'250',
						btnOk:'Yes',
						btnCancel:'No',
						btnOkCallback: _onAgree
					  };
					  
		this.mywindow = new myWindow('dialog_box',option);	
	},
	
	deleteFile_Complete : function(deleteInfo){

		if(deleteInfo == 1){
			
			
			switch(this.language){
				case 'fr':
					message = "<strong>Fichier supprimer avec succes</strong>";
					break;
				case 'nl':
					message = "<strong>File successfully deleted</strong>";
					break;
				case 'en':
					message = "<strong>File successfully deleted</strong>";
					break;
			}	
			
		}else{
			message = "<strong>An error is occured</strong><br>Please contact the system administrator";	
		}
		
		this.mywindow.options.btnOk = 'ok';
		this.mywindow.setWindowContent(message);
		this.mywindow.setAlertButton();
	},
	
	onLoading_message_callback : function(){
		message = "<strong>Please wait...";
		this.mywindow.destroyButton();	
		this.mywindow.setWindowContent(message);
	},
	
	
	
	deleteFile_default_callback : function(){
		alert('no call back defined')
	}
	
}




