
function file_click(click_type){
    var location
	var file_type = $F("ctl_file_type");
	var file_location = $F("ctl_schema");
	var file_name = $F("ctl_file_name");
	var bol_pass = true;


	if (file_location == '' || file_name == ''){
		alert("A folder and file name must be selected")
		bol_pass = false;
	}
	if (file_location == 'Temp Store'){
		alert("Please select a folder within the Temp Store")
		bol_pass = false;
	}

	if (bol_pass){
		switch (file_type){
			case 'db':
			case 'dbt':
			case 'dbr':
			case 'dbras':
				location = 'metaeditor.asp?dbo='+file_location+'&dbn='+file_name+'&db='+file_type
				break;
			
			
			case 'file':
				location = 'metaeditor.asp?file='+file_location+'/'+file_name
				break;
		}


		switch (click_type){
			case 'Open':
				open_file(location)
				break;

			case 'Save':
				var ctl_attach = document.getElementById("ctl_attachment")

				if (ctl_attach && ctl_attach.value != '' && document.getElementById("chk_email").checked)
					{




					// submit the attachment form via iframe to upload the file
					g_location = location
					$("but_upload_submit").click() // oncomplete then calls the metadata_save function
					}
				else
					{
					metadata_save('saveas',location)
					}

				break;
			case 'Delete':
				if (file_type == 'dbt'){
					if (confirm("Are you sure you want to delete this metadata?")){
						location = location.substr(15)
						AjaxRequest.get(
									{
									 'url': 'deletefile.asp?'+location
									,'onSuccess':function(req){expandfolder('Temp Store/'+document.getElementById("ctl_schema").value,'dbt',1)
															  }
									,'onError':function(req){alert("There was a problem excuting the delete command:\n" + req.statusText);}
									}
						);

					}
				} else {
					alert ('You can only delete metadata from the Temp Store')
				}
				break;

		}
	}
}

function resize_open_dialog(){
	$("file_sideselect").style.height = 200;
	$("file_filewin").style.height = 200;
}




function open_file(str_location){
	update_div('div_main_content',str_location,'','file_on_load()')
	popup_close()
}

function select_file(filename, filetype){
	//alert(filename +' '+filetype)
	$("ctl_file_name").value = filename;
	$("ctl_file_type").value = filetype;
}


function filewindow(wintype) {
	popup_div('div_file_win',654,460,'','meta_list.asp?type='+wintype,'','filewindow_resize()')
}
function filewindow_resize(){
	if ($('email_details')) {
		$('file_sideselect').style.height = '230px';
		$('file_filewin').style.height = '230px';

		$('div_popup_outer_1').style.height = '500px'; //should be the only popup win
		$('div_file_win').style.height = '460px';
	}
}


function expandfolder(foldername, source, trace, displayname){

		switch(trace){
			// if called from uponelevel then remove current level and returning to from the array
			case -1:
				prev_folders.pop()
				prev_folders.pop()
				break;

			case 0:
				// if called from the main bar then clear the array
				prev_folders = new Array;
				break;
		}

		// get the length of the array
		var flength = prev_folders.length

		if (flength>0){
			$("ctl_UpOneLevel").href = "javascript:" + prev_folders[(flength-1)]
		} else {
			$("ctl_UpOneLevel").href = "#"
		}

		// set the file location label
		if (source.substring(0,2)=='db'&&trace==1){
			$("lbl_file_location").innerHTML = 'Owner : '
		} else {
			$("lbl_file_location").innerHTML = 'Location : '
			$("ctl_file_location").disabled=true;
		}


		// add the level now showing to the array
		prev_folders.push("expandfolder('"+foldername+"','"+source+"',-1)")


		if (source.substring(0,2)=='db'){
			var first_slash = foldername.indexOf('/')
			var next_slash = foldername.indexOf('/',first_slash+1)
			if (next_slash == -1) next_slash = foldername.length;
			var schema = foldername.substring(first_slash+1,next_slash)

			var last_slash = foldername.lastIndexOf('/')
			var last_foldername = foldername.substring(last_slash+1)
			$("ctl_schema").value = schema
			$("ctl_file_location").value = last_foldername
		} else {
			$("ctl_schema").value = foldername
			$("ctl_file_location").value = foldername
		}

		$("ctl_file_name").value = ""
		$("ctl_file_type").value = source

		update_div('file_filewin','folder_contents.asp?f='+foldername+'&s='+source)

}





function file_on_load(str_type){
	showstep(1);
	set_step_heights();
	showcontact(1);
	$('ctl_app_path').value = '' // relative location of metaeditor app
	$('sp_file_name').innerHTML = $F('ctl_metadata_filename');
	$('but_save_as').disabled = '';
	if (str_type != 'new' && $F('ctl_user_sal') == 'true') {
		
		$('but_save').disabled = '';
	}

}


