
var file_type="";
var selected_count = 0;
var edit_value="";
var edit_count=0;
if(!option){
	var option="";
}

if(!run_select_file_groups){
	var run_select_file_groups=0;
}

var current_format="";

$(document).ready(function() {
	//See if we need to run select_file_groups
	
	if(run_select_file_groups && run_select_file_groups == 1){
		//Need to run it
		select_file_groups();
	}
	if(option == "edit"){
		document.getElementById('formBoxGroups').style.display="none";
		select_file_groups();
	}
	
	//Function for when a user clicks edit
	$('a.optLink').click(function() {
		$('div#formBox_edit').empty();
		$('div#formBox_edit').show();
		//Set the current file type
		current_format=document.getElementById('current_format').value;
		if ($(this).attr('id') == "opt_comments"){
			//Editing Comments
			$('div#formBox_edit').html("Please enter new comments here:<br><textarea name='comments' rows='7' cols='30'></textarea>");
			edit_value="comments";

		}
		else if ($(this).attr('id') == "opt_fname"){
			//Editing File(s)
			
			$('div#formBox_file_edit').show();
			if(current_format == "pdf_and_ppt"){
				$('div#formBox_file_edit').html("Choose files to upload:<br> <input type='file' name='uploadedfile_pdf_and_ppt_1'><br> <input type='file' name='uploadedfile_pdf_and_ppt_2'>");
			}else if(current_format == "pdf"){
				$('div#formBox_file_edit').html("Choose files to upload:<br> <input type='file' name='uploadedfile_pdf'>");
			}else if(current_format == "ppt"){
				$('div#formBox_file_edit').html("Choose files to upload:<br> <input type='file' name='uploadedfile_ppt'>");
			}else if(current_format == "xls"){
				$('div#formBox_file_edit').html("Choose files to upload:<br> <input type='file' name='uploadedfile_xls'>");
			}
			else if(current_format == "pdf_and_xls"){
				$('div#formBox_file_edit').html("Choose files to upload:<br> <input type='file' name='uploadedfile_pdf_and_xls_1'><br> <input type='file' name='uploadedfile_pdf_and_xls_2'></b>");
			}
			else if(current_format == "ppt_and_xls"){
				$('div#formBox_file_edit').html("Choose files to upload:<br> <input type='file' name='uploadedfile_ppt_and_xls_1'><br> <input type='file' name='uploadedfile_ppt_and_xls_2'>");
			}
			else if(current_format == "pdf_and_ppt_and_xls"){
				$('div#formBox_file_edit').html("Choose files to upload:<br> <input type='file' name='uploadedfile_pdf_and_ppt_and_xls_1'><br> <input type='file' name='uploadedfile_pdf_and_ppt_and_xls_2'><br> <input type='file' name='uploadedfile_pdf_and_ppt_and_xls_3'>");
			}
			edit_value="fname";

		}
		else if ($(this).attr('id') == "opt_format"){
			//Editing Format
			//$('div#formBox_edit').html("Please choose a file type: <input type='radio' name='format' value='pdf'> PDF <input type='radio' name='format' value='ppt'> PPT <input type='radio' name='format' value='pdf_and_ppt'> PDF &amp; PPT");
			$('div#formBox_edit').html("Please choose a file type: ");
			$('div#formBox_edit').append(" <input type='radio' name='file_type' value='pdf'> PDF");
			$('div#formBox_edit').append(" <input type='radio' name='file_type' value='ppt'> PPT");
			$('div#formBox_edit').append(" <input type='radio' name='file_type' value='xls'> XLS");
			$('div#formBox_edit').append(" <input type='radio' name='file_type' value='pdf_and_ppt'> PDF &amp; PPT");
			$('div#formBox_edit').append(" <input type='radio' name='file_type' value='pdf_and_xls'> PDF &amp; XLS");
			$('div#formBox_edit').append(" <input type='radio' name='file_type' value='pdf_and_ppt_and_xls'> PDF &amp; PPT &amp; XLS");
			$('div#formBox_edit').append(" <input type='radio' name='file_type' value='ppt_and_xls'> PPT &amp; XLS");
			
			edit_value="format";
		}
		else if ($(this).attr('id') == "opt_global_file"){
			//Editing Global File
			$('div#formBox_edit').html("Should this file be accessible to all users? <input type='radio' name='global_file' value='1'> Yes <input type='radio' name='global_file' value='0'> No ");
			edit_value="global_file";
		}
		else if ($(this).attr('id') == "opt_groups"){
			//Editing Global File
			$('div#formBox_edit').html("Please choose the groups that can view this report:");
			edit_value="groups";
			$('div#formBoxGroups').show();
		}
		else if ($(this).attr('id') == "opt_order_date"){
			//Editing Order Date
			//$('div#formBox_edit').html("Please choose a title for this report: <input type='text' size='50' name='title' >");
			$('div#formBox_edit').html("Please specify the date (YYYYMMDD) to which the report applies (used to order the reports) If it is a <br>\
        monthly report use the first day of the month (eg July 2009 Long Range = 20090701):<br><input type='text' size='8' name='order_date' value='"+$('span#order_date_span').html()+"'>");
			edit_value="order_date";
		}
		else if ($(this).attr('id') == "opt_title"){
			//Editing Title
			//$('div#formBox_edit').html("Please choose a title for this report: <input type='text' size='50' name='title' >");
			$('div#formBox_edit').html("Please choose a title for this report: <input type='text' size='50' name='title' value='"+$('span#title_span').html()+"'>");
			edit_value="title";
		}
		else if ($(this).attr('id') == "opt_type"){
			//Editing Type
			$('div#formBox_edit').html("Please choose a report type: <select name='type'><option value='analysis'>Analysis</option><option value='historical'>Historical</option><option value='long_range'>Long Range</option><option value='misc'>Miscellaneous</option><option value='weekly'>Weekly</option></select>");
			edit_value="type";
		}
		else if ($(this).attr('id') == "opt_visible"){
			//Editing Visibility
			$('div#formBox_edit').html("Should this file be visible to all users? <input type='radio' name='visible' value='1'> Yes <input type='radio' name='visible' value='0'> No ");
			edit_value="visible";
		}
		
		$('div#formBox_edit_buttons').show();
		//$('div#formCreatebuttons').show();

	});
	
	$('input#accept_edit').click(function() {
		
		//Store the changed value
		if(edit_value == "comments"){
			
			$('span#comments_span').toggleClass('changed');
			$('span#comments_span').html($('div#formBox_edit > textarea').val());
			$('div#formBox_edit_values').append("<input type='hidden' name='edit_comments' value='"+$('div#formBox_edit > textarea').val()+"'>");
			//$('span#comments_span').toggleClass('changed_reset',1500);
			
		}else if(edit_value == "fname"){
			
			$('span#fname_span').toggleClass('changed');
			$('div#formBox_file_edit').hide();
			if(current_format == "pdf_and_ppt"){	
				$('span#fname_span').html($("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_ppt_1']").val()+"<br>"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_ppt_2']").val());
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_pdf_and_ppt_1' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_ppt_1']").val()+"'>");
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_pdf_and_ppt_2' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_ppt_2']").val()+"'>");
			}else if(current_format == "pdf"){
				$('span#fname_span').html($("div#formBox_file_edit > input:file[@name='uploadedfile_pdf']").val());
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_pdf' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf']").val()+"'>");
			}else if(current_format == "ppt"){
				$('span#fname_span').html($("div#formBox_file_edit > input:file[@name='uploadedfile_ppt']").val());
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_ppt' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_ppt']").val()+"'>");
			}else if(current_format == "xls"){
				$('span#fname_span').html($("div#formBox_file_edit > input:file[@name='uploadedfile_xls']").val());
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_xls' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_xls']").val()+"'>");
			}else if(current_format == "pdf_and_xls"){
				$('span#fname_span').html($("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_xls_1']").val()+"<br>"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_xls_2']").val());
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_pdf_and_xls_1' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_xls_1']").val()+"'>");
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_pdf_and_xls_2' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_xls_2']").val()+"'>");
			}else if(current_format == "pdf_and_ppt_and_xls"){
				$('span#fname_span').html($("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_ppt_and_xls_1']").val()+"<br>"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_ppt_and_xls_2']").val());
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_pdf_and_ppt_and_xls_1' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_ppt_and_xls_1']").val()+"'>");
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_pdf_and_ppt_and_xls_2' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_ppt_and_xls_2']").val()+"'>");
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_pdf_and_ppt_and_xls_3' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_pdf_and_ppt_and_xls_3']").val()+"'>");
			}else if(current_format == "ppt_and_xls"){
				$('span#fname_span').html($("div#formBox_file_edit > input:file[@name='uploadedfile_ppt_and_xls_1']").val()+"<br>"+$("div#formBox_file_edit > input:file[@name='uploadedfile_ppt_and_xls_2']").val());
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_ppt_and_xls_1' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_ppt_and_xls_1']").val()+"'>");
				$('div#formBox_edit_values').append("<input type='hidden' name='edit_uploadedfile_ppt_and_xls_2' value='"+$("div#formBox_file_edit > input:file[@name='uploadedfile_ppt_and_xls_2']").val()+"'>");
			}
			//$('span#fname_span').toggleClass('changed_reset',1500);
			
		}else if(edit_value == "format"){
			
			$('span#format_span').toggleClass('changed',1000);
			if($("div#formBox_edit > input:radio[@name='format']:checked").val() == "pdf_and_ppt"){
				$('span#format_span').html("PDF &amp; PPT");
				document.getElementById('current_format').value=current_format=$("div#formBox_edit > input:radio[@name='format']:checked").val();
			}else if($("div#formBox_edit > input:radio[@name='format']:checked").val() == "pdf"){
				$('span#format_span').html("PDF");
				document.getElementById('current_format').value=current_format=$("div#formBox_edit > input:radio[@name='format']:checked").val();
			}else if($("div#formBox_edit > input:radio[@name='format']:checked").val() == "ppt"){
				$('span#format_span').html("PPT");
				document.getElementById('current_format').value=current_format=$("div#formBox_edit > input:radio[@name='format']:checked").val();
			}else if($("div#formBox_edit > input:radio[@name='format']:checked").val() == "xls"){
				$('span#format_span').html("XLS");
				document.getElementById('current_format').value=current_format=$("div#formBox_edit > input:radio[@name='format']:checked").val();
			}else if($("div#formBox_edit > input:radio[@name='format']:checked").val() == "pdf_and_xls"){
				$('span#format_span').html("PDF &amp; XLS");
				document.getElementById('current_format').value=current_format=$("div#formBox_edit > input:radio[@name='format']:checked").val();
			}else if($("div#formBox_edit > input:radio[@name='format']:checked").val() == "pdf_and_ppt_and_xls"){
				$('span#format_span').html("PDF &amp; PPT &amp; XLS");
				document.getElementById('current_format').value=current_format=$("div#formBox_edit > input:radio[@name='format']:checked").val();
			}else if($("div#formBox_edit > input:radio[@name='format']:checked").val() == "ppt_and_xls"){
				$('span#format_span').html("PPT &amp; XLS");
				document.getElementById('current_format').value=current_format=$("div#formBox_edit > input:radio[@name='format']:checked").val();
			}
			$('div#formBox_edit_values').append("<input type='hidden' name='edit_format' value='"+$("div#formBox_edit > input:radio[@name='format']:checked").val()+"'>");
			//$('span#format_span').toggleClass('changed_reset',1500);
			
		}else if(edit_value == "groups"){
			
			//Loop over all the groups that are highlighted/selected and add their values to the list of groups
			$("select#group_select option:selected").each(function() {
				
			        
				//$('div#selected_groups').append('<div class="country" id="selCount_'+selected_count+'"><a href="javascript://"><img src="/images/remove.gif"></a> [Group ] <span id="sel_name">'+ $(this).text() +'</span><input type="hidden" name="groups[]" value="'+$(this).val()+'"></div>');
				$('div#selected_groups').append("<div class='group_div' id='group_span_"+$(this).val()+"'><a href='javascript://' onclick=\"deleteGroup(this);\"><img src='/images/remove.gif'></a> "+ $(this).text() +"<input type='hidden' name='groups[]' value='"+$(this).val()+"'></div>");
				selected_count++;
			});
			$('div#formBoxGroups').hide();
			
		}else if(edit_value == "global_file"){
			
			$('span#global_file_span').toggleClass('changed');
			if($("div#formBox_edit > input:radio[@name='global_file']:checked").val() == 1){
				$('span#global_file_span').html("Yes");
			}else{
				$('span#global_file_span').html("No");
			}
			
			$('div#formBox_edit_values').append("<input type='hidden' name='edit_global_file' value='"+$("div#formBox_edit > input:radio[@name='global_file']:checked").val()+"'>");
			//$('span#global_file_span').toggleClass('changed_reset',1500);
			
		}else if(edit_value == "order_date"){
			
			$('span#order_date_span').toggleClass('changed');
			$('span#order_date_span').html($("div#formBox_edit > input:text[@name='order_date']").val());
			$('div#formBox_edit_values').append("<input type='hidden' name='edit_order_date' value='"+$("div#formBox_edit > input:text[@name='order_date']").val()+"'>");
			//$('span#title_span').toggleClass('changed_reset',1500);
			
		}else if(edit_value == "title"){
			
			$('span#title_span').toggleClass('changed');
			$('span#title_span').html($("div#formBox_edit > input:text[@name='title']").val());
			$('div#formBox_edit_values').append("<input type='hidden' name='edit_title' value='"+$("div#formBox_edit > input:text[@name='title']").val()+"'>");
			//$('span#title_span').toggleClass('changed_reset',1500);

			
		}else if(edit_value == "type"){
			
			$('span#type_span').toggleClass('changed');
			
			if($('div#formBox_edit > select').val() == "analysis"){
				$('span#type_span').html("Analysis");
			}else if($('div#formBox_edit > select').val() == "historical"){
				$('span#type_span').html("Historical");
			}else if($('div#formBox_edit > select').val() == "long_range"){
				$('span#type_span').html("Long Range");
			}else if($('div#formBox_edit > select').val() == "misc"){
				$('span#type_span').html("Miscellaneous");
			}else if($('div#formBox_edit > select').val() == "weekly"){
				$('span#type_span').html("Weekly");				
			}
			
			$('div#formBox_edit_values').append("<input type='hidden' name='edit_type' value='"+$("div#formBox_edit > select").val()+"'>");
			//$('span#type_span').toggleClass('changed_reset',1500);
			
		}else if(edit_value == "visible"){
			
			$('span#visible_span').toggleClass('changed');
			if($("div#formBox_edit > input:radio[@name='visible']:checked").val() == 1){
				$('span#visible_span').html("Yes");
			}else{
				$('span#visible_span').html("No");
			}
			
			$('div#formBox_edit_values').append("<input type='hidden' name='edit_visible' value='"+$("div#formBox_edit > input:radio[@name='visible']:checked").val()+"'>");
			//$('span#global_file_span').toggleClass('changed_reset',1500);
		}
		
		$('div#formBox_edit_values').append("<input type='hidden' name='edit_variables[]' value='"+edit_value+"'>");
		
		$('div#formBox_edit').empty();
		$('div#formBox_edit').hide();
		$('div#formBox_edit_buttons').hide();
		$('div#formCreatebuttons').show();
	});
	
	$('input#cancel_edit').click(function() {
		$('div#formBox_edit').empty();
		$('div#formBox_edit').hide();
		$('div#formBox_edit_buttons').hide();
		$('div#formBox_file_edit').hide();
		if(option == "edit"){
			$('div#formBoxGroups').hide();
		}
	});

});



function set_file_type(type){
	if(type == "pdf"){
		file_type=type;

		$('div#formBox_ppt').hide();
		$('div#formBox_pdf_and_ppt').hide();
		$('div#formBox_xls').hide();
		$('div#formBox_pdf_and_xls').hide();
		$('div#formBox_pdf_and_ppt_and_xls').hide();
		$('div#formBox_ppt_and_xls').hide();
		$('div#formBox_pdf').show();
		$('div#formCreatebuttons').show();
	}else if(type == "ppt"){
		file_type=type;

		$('div#formBox_pdf_and_ppt').hide();
		$('div#formBox_pdf').hide();
		$('div#formBox_xls').hide();
		$('div#formBox_pdf_and_xls').hide();
		$('div#formBox_pdf_and_ppt_and_xls').hide();
		$('div#formBox_ppt_and_xls').hide();
		$('div#formBox_ppt').show();
		$('div#formCreatebuttons').show();
	}else if(type == "pdf_and_ppt"){
		file_type=type;

		$('div#formBox_ppt').hide();
		$('div#formBox_pdf').hide();
		$('div#formBox_xls').hide();
		$('div#formBox_pdf_and_xls').hide();
		$('div#formBox_pdf_and_ppt_and_xls').hide();
		$('div#formBox_ppt_and_xls').hide();
		$('div#formBox_pdf_and_ppt').show();
		$('div#formCreatebuttons').show();
	}else if(type == "xls"){
		$('div#formBox_ppt').hide();
		$('div#formBox_pdf').hide();
		$('div#formBox_xls').show();
		$('div#formBox_pdf_and_xls').hide();
		$('div#formBox_pdf_and_ppt_and_xls').hide();
		$('div#formBox_ppt_and_xls').hide();
		$('div#formBox_pdf_and_ppt').hide();
		$('div#formCreatebuttons').show();
	}else if(type == "pdf_and_xls"){
	$('div#formBox_ppt').hide();
		$('div#formBox_pdf').hide();
		$('div#formBox_xls').hide();
		$('div#formBox_pdf_and_xls').show();
		$('div#formBox_pdf_and_ppt_and_xls').hide();
		$('div#formBox_ppt_and_xls').hide();
		$('div#formBox_pdf_and_ppt').hide();
		$('div#formCreatebuttons').show();
	}else if(type == "pdf_and_ppt_and_xls"){
		$('div#formBox_ppt').hide();
		$('div#formBox_pdf').hide();
		$('div#formBox_xls').hide();
		$('div#formBox_pdf_and_xls').hide();
		$('div#formBox_pdf_and_ppt_and_xls').show();
		$('div#formBox_ppt_and_xls').hide();
		$('div#formBox_pdf_and_ppt').hide();
		$('div#formCreatebuttons').show();
	}else if(type == "ppt_and_xls"){
		$('div#formBox_ppt').hide();
		$('div#formBox_pdf').hide();
		$('div#formBox_xls').hide();
		$('div#formBox_pdf_and_xls').hide();
		$('div#formBox_pdf_and_ppt_and_xls').hide();
		$('div#formBox_ppt_and_xls').show();
		$('div#formBox_pdf_and_ppt').hide();
		$('div#formCreatebuttons').show();
	}else{
		alert("INVALID FILE TYPE ("+type+")");
	}
	//alert("CHOSE "+type);
	return;
}

function close_file_groups(){
	//Make sure the div exists
	if(document.getElementById('formBoxGroups')){
		//document.getElementById('formBoxGroups').style.display="none";
		//$('div#opts_region').empty();
		$('div#formBoxGroups').empty();
	}
	return 1;
}

function select_file_groups(){
	//Show the selection html
	if(document.getElementById('formBoxGroups') && option != "edit"){
		document.getElementById('formBoxGroups').style.display="";
	}




	$.ajax({
		type: "POST",
		url: "/ajax/file_getData.php",
		data: 'option=1',
		dataType : 'xml',
		success: function(xml) {
			$('div#formBoxGroups').html('Groups <br><select id="group_select" name="fileGroupSelect" multiple size="15"></select><br><div id="add_box"></div>');

			//Call the function to populate the regions selection menu
			$("group",xml).each(function(i) {
				$('select#group_select').append('<option value='+$(this).attr('id')+'>'+$(this).text()+'</option>');
			});

			$("select#group_select option:selected").each(function(){ $(this).attr('selected',''); });
			$('div#add_box').append('<input type="button" class="button" value="Add" id="add_groups">');

			var groups="";
			
			if(option != "edit"){
				$('input#add_groups').click(function() {
					$("select#group_select option:selected").each(function() {
						//if ($('div#selected_groups').find('div.region[@id="'+$(this).text()+'"]').length == 0) {
						$('div#selected_groups').append('<div class="country" id="selCount_'+selected_count+'"><a href="javascript://"><img src="/images/remove.gif"></a> [Group ] <span id="sel_name">'+ $(this).text() +'</span><input type="hidden" name="fileGroups[]" value="'+$(this).val()+'"></div>');
						$('div#selCount_'+selected_count).find('a').click(function() {
							deleteObj = $(this).parent();
							deleteName = deleteObj.find('span').text();
							deleteObj.remove();
						});
						selected_count++;
					});


				});
			}
			

		}
	});
}

function deleteGroup(obj) {

	$(obj).parent().remove();
	$('div#formCreatebuttons').show();

}

function addFileInput(currentNumber,containerID){
	
	var newNumber=currentNumber+1;
	//Make sure they are not trying to add more than 3 files
	if(newNumber >= 13){
		//Too many
		alert("We're sorry but only 3 files can be added for a given report");
		return;
	}
	//Create the HTML for another file input
	inputHTML="\
	  <div id='file_"+newNumber+"'>\
	    Format - <select name='fileFormat[]'><option value='pdf'>PDF</option><option value='ppt'>PowerPoint</option><option value='xls'>Excel</option></select> \
	    File - <input type='file' name='uploadedfile[]'> <span id='addFile_"+newNumber+"'><a href='javascript:void(0);' onclick=\"addFileInput("+newNumber+",'fileContainer');\">[Add Another File]</a></span>\
	  </div>\
	";
	
	//Append this HTML to the containerID element
	$('#'+containerID).append(inputHTML);
	
	//Remove the previous "Add Another File" button
	prevButtonID="addFile_"+currentNumber;
	$('#'+prevButtonID).hide(100);
	return;
}

function deleteFile(id){
	if(id && id >= 1){
		//Looks valid
		var fileDiv="file_span_"+id;
		
		//Hide the object
		$('#'+fileDiv).hide(200);
		
		//Create a record for the file that we need to delete
		$('#removeFiles').append("<input type='hidden' name='removeFile[]' value='"+id+"'>");		
		
	}
	else{
		//Invalid
		alert("Invalid deleteFile");
	}
	return;
}

function deleteGroup(id){
	if(id && id >= 1){
		//Looks valid
		var groupDiv="group_span_"+id;
		
		//Hide the object
		$('#'+groupDiv).hide(200);
		
		//Create a record for the file that we need to delete
		$('#removeGroups').append("<input type='hidden' name='removeGroup[]' value='"+id+"'>");		
		
	}
	else{
		//Invalid
		alert("Invalid deleteGroup");
	}
	return;
}

