//
// Custom Attributes For Form Validation
var validates_of = ['required', 'message', 'format', 'message_format', 'message_confirm', 'id', 'confirmed'];
//
// Status Tracking Object
var status = new Object();
//
// Go To A URL
function go(url)
{
	window.location = url;
}
//
// Process Button Actions
function buttonAction(action,form,ajax)
{
	//
	// Generic Params
	saveContent = false;
	rtEditor    = false;
	reqURL      = parent.document.URL;
	reqQS       = reqURL.substring(reqURL.indexOf('?')+1, reqURL.length);
	//
	// Set Return URL Params
	queryString = reqQS.split("&");
	returnQS    = "?csdone=true";
	for(q=0; q<queryString.length; q++)
	{
		thisParam = queryString[q].split("=");
		if(thisParam[0]!='form'&&thisParam[0]!='edit')
		{
			returnQS+="&"+queryString[q];
		}
	}	
	//
	// A Form Exists So Process It
	if(form)  
	{
		//
		// Check To See If Form Contains Any TinyMCE Fields
		allFields   = Form.getElements(form);
		for(i=0; i<allFields.length; i++)
		{
			fieldName = allFields[i].name;	
			if(fieldName.indexOf('content') > -1 || fieldName.indexOf('_text') > -1)
			{
				saveContent = true;
				if(fieldName.indexOf('content') > -1)
				{
					rtEditor = true;
				}
			}		
		}		
		if(ajax)
		{
			saveContent = true;
		}
		//
		// If There Is A Content Area Send It With AJAX
		if(saveContent)
		{
			//
			// Trigger Save, Disable Form, Make AJAX URL
			if(rtEditor){ tinyMCE.triggerSave(); }
			$(form).disable;
			formObj = $(form).serialize(true);
			go      = "?"+action+"&form_id="+form;
			params  = Hash.toQueryString(formObj);
			//
			// Make AJAX Request
			new Ajax.Request(go,
			{
				method: 'post',
				parameters: params,
				onSuccess: function(transport) 
			  	{			    	
			    	window.location = returnQS;
			  	},
			  	onFailure: function(transport) 
			  	{			    	
			    	alert("Data was not saved, please fill out all required fields!");
			  	}
			});	
		}
		//
		// No Content, Save Normally
		else
		{
			go = "?"+action+"&form_id="+form+"&"+$(form).serialize();
		}
		//
		// Collect Form Validation Attributes
		Form.getElements(form).each(function(element){
			validates_of.each(function(attr) 
			{	
				a = element.readAttribute(attr);		
				if(a) 
				{
					go += '&' + attr + '_' + element.name + '=' + escape(a);		
				}	
			})
		})
	}
	//
	// Normal Page - Just Go There
	else
	{
		go = "?"+action;
	}	
	//
	// Go There If Not An AJAX Request
	if(saveContent==false)
	{
		window.location = go;
	}
} 
//
// Function To Update Data Rows
function getRows(set)
{
	$('wrapper').style.cursor = 'wait';
	setDetails   = set.split(',');
	setLocation  = setDetails[0]+"RecordSet";	
	setUpdateUrl = "?mode=ajax&controller="+setDetails[0]+"&type=getDataRows&setStart="+setDetails[1]+"&setSize="+setDetails[2];
	new Ajax.Updater(setLocation,setUpdateUrl,{evalScripts:'true',onComplete:function(transport){ $('wrapper').style.cursor = 'auto'; }});
}
//  
// Function To Filter Data Rows
function filterRows(set)
{
	$('wrapper').style.cursor = 'wait';
	setDetails   = set.split(',');
	setLocation  = setDetails[1]+"RecordSet";	
	filterValues = "&filterSet=true";
	//
	// Get Filter Form Fields
	Form.getElements(setDetails[0]).each(function(element){
		if(element.name.indexOf('_filter') > -1 && $(element.name).value != '')
		{
			filterValues +=	"&" + element.name + "=" + $(element.name).value;			
		}
	});
	//
	// Complete URL And Make Request	
	setUpdateUrl = "?mode=ajax&controller="+setDetails[1]+"&type=getDataRows&setStart=&setSize=100"+filterValues;
	new Ajax.Updater(setLocation,setUpdateUrl,{evalScripts:'true',onComplete:function(transport){ $('wrapper').style.cursor = 'auto'; }});
}
//
// Clear The Current Filter
function clearFilter(controller)
{
	window.location = '?mode=form&controller='+controller;
}
//
// Function To Open And Close Form Filters
function filter(filterName)
{
	if(status[filterName] == 'open')
	{
		$('filter_'+filterName+'_field').style.display = 'none';
		status[filterName] = "closed";
	}
	else
	{
		$('filter_'+filterName+'_field').style.display = '';
		status[filterName] = "open";	
	}
}
//
// Function To Fake Check Boxes
function check(field)
{
	valueField = field.substring(0,field.length-3);
	if($(field).checked==true)
	{
		$(valueField).value = 1;	
	}
	else
	{
		$(valueField).value = 0;
	}
}
//
// Open Content For Editing
function edit(sectionId)
{
	window.location = "?edit=true&id="+sectionId;
}
//
// Cross Select Form Element Functions
//
// Move Items
function csMove(from,to,all)
{
	for(i=$(from).options.length-1; i>=0; i--)
	{
		if($(from).options[i].selected||all)
	 	{
	 		$(to).options[$(to).options.length] = new Option($(from).options[i].text,$(from).options[i].value);
			$(from).options[i]                  = null;
		}
	}	 
	csSort(to);
	csSave(from,to);
}
//
// Sort Items
function csSort(list) 
{
	optionsList = new Array();
	for(i=0; i<$(list).options.length; i++)
	{
		optionsList[i] = new Option($(list).options[i].text,$(list).options[i].value); 
	}	
	optionsList.sort(
		function(a,b) 
		{ 
			if((a.text+"")<(b.text+"")){ return -1; }
			if((a.text+"")>(b.text+"")){ return 1;  }
			return 0;
		} 
	);
	for(i=0; i<optionsList.length; i++)
	{	
		$(list).options[i] = new Option(optionsList[i].text,optionsList[i].value); 
	}
}
//
// Save Item IDs
function csSave(from,to)
{
	if(from.indexOf("_selections") > -1)
	{
		valueField = from;
	}
	else
	{
		valueField = to;
	}
	$(valueField+"_ids").value = "";
	valueArray = new Array();
	for(i=$(valueField).options.length-1; i>=0; i--)
	{
		valueArray[i] = $(valueField).options[i].value;
	}
	$(valueField+"_ids").value = valueArray.join();
}






