<!--//--><![CDATA[//><!--

$.metadata.setType("attr", "validate");

//

$(document).ready(function() {
	
	$('#editScreen').jqm({
					
		ajax: '@href', 
		trigger: 'a.showEditScreen',
		modal:true,
		onLoad: function(hash) {
			initEditForm(hash);
		}
		
	});	
	
						   
});
	
function initEditor() {
	
  tinyMCE.init({
		mode : "textareas",
		editor_selector : "mceEditor",
		theme : "advanced",
		plugins : "table, paste,filemanager,visualchars, media, template",
		extended_valid_elements : "iframe[name|src|framespacing|border|frameborder|scrolling|title|height|width], script[language|type|src]",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_blockformats : "h1,h2,h3,p",
		theme_advanced_buttons1: "formatselect,bold,italic,bullist,link,unlink,image,charmap,removeformat,code,tablecontrols",
		theme_advanced_buttons2: "",
		theme_advanced_buttons3: "",
		content_css : "/css/screen-rte.css?q=a",
		apply_source_formatting : true,
		paste_auto_cleanup_on_paste : true,
		paste_strip_class_attributes : "all",
		paste_remove_spans : true,
		force_p_newlines:false,
		relative_urls : false,
		remove_script_host : true,
		verify_html : false
	});
}

function initEditForm(hash) {
	
	initEditor();
	  
	$('#container').tabs();
	
	
	$("#programEditForm").validate({
		
		rules: {
			contentType: {
				required: true
			},
			title: {
				required: true,
				remote: {
					url: "/admin/category/includes/checktitle.php",
					type: "post",
					data: {
					itemID: function() {
						
						if ($("#itemID").length>0) {
							
							return $("#itemID").val();
							
						} else {
							
							return "add";
							
						}
						
					  }
					}
				}
			},
			startdate: {
			  dateISO: true
			},
			enddate: {
			  dateISO: true
			}
		},
		messages: {
			title: {
				remote: jQuery.format("This title is already in use")
			}
		},
		invalidHandler: function(form, validator) {

			var errors = validator.numberOfInvalids();
			if (errors) {				
				alert( "This form still contains errors, they have been highlighted in red");
			}
			
		},
		submitHandler: function(form) {
			
			tinyMCE.triggerSave();
			
			$('#editFormSubmitButton').attr("disabled", "disabled");
			$('#cancelFormButton').attr("disabled", "disabled");
			$("#processingForm").attr("style", "display:block"); 

			jQuery(form).ajaxSubmit({
				success: processFormSubmit,
				type:'POST',
				dataType: "json",
				iframe:false
			});
			
			return false;
		}, 
		debug: false,
		onkeyup: false

	});
	
	jQuery(".showDatePicker").datepicker({
						 
												dateFormat:"yy-mm-dd", 
												showOn: 'button', 
												buttonImageOnly: true, 
												buttonImage: '/images/calendar.gif'
													
											});
	
	var displayType =  $('#displayType').val();
	
	switch(displayType) {
		
		case "public":
			$("#viewGroupRow").attr({style:"display:none;"});
		break;
		
		case "private":
			$("#viewGroupRow").attr({style:"display:table-row;"});
		break;
		
	}
		

	
}

function fixTextAreas() {
	
	//var descContent = tinyMCE.get('#desc').getContent();
	$('#desc').val('descContent'); // put it in the textarea
	
	//var timesContent = tinyMCE.get('#field_session_times').getContent();
	$('#field_session_times').val('timesContent'); // put it in the textarea
	
	//var costsContent = tinyMCE.get('#field_session_costs').getContent();
	$('#field_session_costs').val('costsContent'); // put it in the textarea
	
	alert(  $('#field_session_costs').val() );
}

function showViewGroups() {

	var selectedView = $("#displayType").val();
	
	switch(selectedView) {
	
		case "public":
			
			$("#viewGroupRow").attr({style:"display:none;"});
			
		break;
		
		case "private":
			
			$("#viewGroupRow").attr({style:"display:table-row;"});
			
		break;
	
	}

}

function processFormSubmit( data ) {
	
	/*
	{ 	
		"processStatus" : "success", 
		"processContentID" : "2717", 
		"processRevisionID" : "3456", 
		"processContentType" : "program", 
		"processAction" : "add", 
		"error" : "", 
		"errorMessage" : "successful content entry" 
	}
	*/
	
	var results = eval(data.error);
	
	 switch( results ) {
		
		//if we have an error we won't redirect but display 
		//the error message on the screen with the form
		//to try again
		case true:

			var errorMessage = data.errorMessage;
			$("#formErrorMessage").html( errorMessage );					
			$("#formErrorMessage").attr({  style: "display:block; color:#FF0000; font-size:11px;" });
			
			$('#editFormSubmitButton').attr("disabled", "");
			$('#cancelFormButton').attr("disabled", "");

			$("#processingForm").attr("style", "display:none"); 
			
		break;
		
		//if there is no error we can
		//ust successfully reload the page after the admin
		//process is completed
		case false:
			
			//jQuery("#searchingModal").attr("style", "display:block");
			
			document.location.reload();
			//reloadData( data );
	
		break;
		
	}

}
			
function reloadData( data ) {
	
	var str = $('#programSearch').serialize();

	$.ajax({
				   
		type: "POST",
		url: "/includes/getPrograms.php",
		data: str,
		dataType: "html",
		cache: false,
		success: function(result){
		 	
			jQuery("#colThree-inner").html( result );
	
			//jQuery("#submitProgramSearch").attr("disabled", ""); 
			
			//jQuery("#searchingModal").attr("style", "display:none");
			
			$('#editScreen').jqmAddTrigger('.showEditScreen'); 
		
			var errorMessage = data.errorMessage;
		
			$("#processingPostMessage").html(errorMessage); 
			
			$('#processingPreResults').fadeOut('slow', function() 
			{
				
				$('#processingPostResults').fadeIn('slow');
							
			});	

		  
		}
	  
	});

	
}
 
function cancelForm() {
	 
	$('#editScreen').jqmHide();
	 
}

//--><!]]>