//-------------------------------------------------------------------------------------
//-----		Name:			ajax.js 											  -----
//-----		Purpose:		Used to store Ajax Handlers for the CMS	    		  -----
//-----		Additional															  -----
//-----		information:													      -----
//-----				- Ajax_Get ::												  -----
//-----						Purpose:	Retrieves data from the server			  -----
//-----									and populates a given element			  -----
//-----									with the requested data					  -----
//-----																			  -----
//-----						Parameters: responseContainerID						  -----
//-----										container to update					  -----
//-----									filepath								  -----
//-----										path to the PHP file which			  -----
//-----										contains the function 				  -----
//-----									params									  -----
//-----										Object Literal Syntax of              -----
//-----										parameters for the server			  -----
//-----										side function. First				  -----
//-----										parameters is the function			  -----
//-----										name which is required				  -----
//-----										(ex: {function: 'add', num1: 3 ... }  -----
//-----									methodType								  -----
//-----										Specifies the method				  -----
//-----										('get' or 'post') for the			  -----
//-----										data being passed to server			  -----
//-----										(OPTIONAL -- DEFAULTS TO 'post')	  -----
//-----																			  -----
//-----																			  -----
//-----				- Ajax_Get_Aggregate ::										  -----
//-----						Purpose:	Gets the results of an aggregate		  -----
//-----									function query.							  -----
//-----																			  -----
//-----						Parameters: filepath								  -----
//-----										path to the PHP file which			  -----
//-----										contains the function 				  -----
//-----									params									  -----
//-----										Object Literal Syntax of              -----
//-----										parameters for the server			  -----
//-----										side function. First				  -----
//-----										parameters is the function			  -----
//-----										name which is required				  -----
//-----										(ex: {function: 'add', num1: 3 ... }  -----
//-----									methodType								  -----
//-----										Specifies the method				  -----
//-----										('get' or 'post') for the			  -----
//-----										data being passed to server			  -----
//-----										(OPTIONAL -- DEFAULTS TO 'post')	  -----
//-----																			  -----
//-----																			  -----
//-----				- Ajax_Set ::												  -----
//-----						Purpose:	Sends data to the server to be stored	  -----
//-----									in a file, database, etc...				  -----
//-----																			  -----
//-----						Parameters: filepath								  -----
//-----										path to the PHP file which			  -----
//-----										contains the function 				  -----
//-----									params									  -----
//-----										Object Literal Syntax of              -----
//-----										parameters for the server			  -----
//-----										side function. First				  -----
//-----										parameters is the function			  -----
//-----										name which is required				  -----
//-----										(ex: {function: 'add', num1: 3 ... }  -----
//-----									methodType								  -----
//-----										Specifies the method				  -----
//-----										('get' or 'post') for the			  -----
//-----										data being passed to server			  -----
//-----										(OPTIONAL -- DEFAULTS TO 'post')	  -----	
//-------------------------------------------------------------------------------------

function Ajax_Get(responseContainerID, filepath, params, methodType)
{
	// Used to get data to fill a container element
	
	// Default methodType to 'post' if not specified
	if (methodType == null || methodType == '') {
		methodType = 'post';	
	}
	
	// Create a new Updater object
	new Ajax.Updater(responseContainerID,
		filepath,
		{
			method: methodType,
			parameters: params,
			// Show the error message if failure
			onFailure: Show_Error
		});
}

function Ajax_Get_Aggregate(filepath, params, methodType)
{
	// Used to retrieve a value to use in code
	
	// Default methodType to 'post' if not specified
	if (methodType == null) {
		methodType = 'post';	
	}
	
	// Create a new Request object
	new Ajax.Request(filepath,
		{
			method: methodType,
			parameters: params,
			// Show the error message if failure
			onFailure: Show_Error,
			onSuccess: function (transport) {
				 return transport.responseText;
			}
		});
}

function Ajax_Fill_Image(inputId, filepath, params, methodType)
{
	// Default methodType to 'post' if not specified
	if (methodType == null) {
		methodType = 'post';	
	}
	
	// Create a new Request object
	new Ajax.Request(filepath,
		{
			method: methodType,
			parameters: params,
			// Show the error message if failure
			onFailure: Show_Error,
			onSuccess: function (transport) {
				 document.getElementById(inputId).src = './images/main/' + transport.responseText;
			}
		});
}


function Ajax_Fill_TinyMCE(MCEid, filepath, params, methodType)
{
	// Default methodType to 'post' if not specified
	if (methodType == null) {
		methodType = 'post';	
	}
	
	// Create a new Request object
	new Ajax.Request(filepath,
		{
			method: methodType,
			parameters: params,
			// Show the error message if failure
			onFailure: Show_Error,
			onSuccess: function (transport) {
				document.getElementById(MCEid).value = transport.responseText;
				tinyMCE.updateContent(MCEid);
			}
		});
}

function Ajax_Fill_CheckBox(inputId, filepath, params, methodType)
{
	// Default methodType to 'post' if not specified
	if (methodType == null) {
		methodType = 'post';	
	}
	
	// Create a new Request object
	new Ajax.Request(filepath,
		{
			method: methodType,
			parameters: params,
			// Show the error message if failure
			onFailure: Show_Error,
			onSuccess: function (transport) {
				if(transport.responseText == 4){
				 document.getElementById(inputId).checked = true;
				}
			}
		});
}

function Ajax_Fill_Value(inputId, filepath, params, methodType)
{
	// Default methodType to 'post' if not specified
	if (methodType == null) {
		methodType = 'post';	
	}
	
	// Create a new Request object
	new Ajax.Request(filepath,
		{
			method: methodType,
			parameters: params,
			// Show the error message if failure
			onFailure: Show_Error,
			onSuccess: function (transport) {
				 document.getElementById(inputId).value = transport.responseText;
			}
		});
}


function Ajax_Image_Change(box){
	document.getElementById('BioImage').src = '/images/main/' + box.options[box.selectedIndex].value;
}
function Ajax_Select(selectId, filepath, params, methodType)
{
	// Default methodType to 'post' if not specified
	if (methodType == null) {
		methodType = 'post';	
	}
	
	// Create a new Request object
	new Ajax.Request(filepath,
		{
			method: methodType,
			parameters: params,
			// Show the error message if failure
			onFailure: Show_Error,
			onSuccess: function (transport) {
				var make = true;
				var box = document.getElementById(selectId);
				for(i = 0; i < box.length; i++){
					if(box.options[i].value == transport.responseText){
						box.options[i].selected = 'selected';
						box.selectedIndex = i;
						make = false;
					}
				}
				if(make){
					box.options[box.length] = new Option(transport.responseText, transport.responseText);
				}
				Ajax_Image_Change(box);
			}
		});
}

function Ajax_Set(filepath, params, methodType)
{
	// Used to send data to server-side storage
	
	// Default methodType to 'post' if not specified
	if (methodType == null) {
		methodType = 'post';	
	}
	
	// Create a new Request object
	new Ajax.Request(filepath,
		{
			method: methodType,
			parameters: params,
			// Show the error message if failure
			onFailure: Show_Error,
			onSuccess: function (transport) {
				var response = transport.responseText;
				if(response != ""){
					alert(response);
				}
			}
		});
}

function Ajax_Run(filepath, params, methodType)
{
	// Used to run server-side php code durring page operation
	
	// Default methodType to 'post' if not specified
	if (methodType == null) {
		methodType = 'post';	
	}
	
	// Create a new Request object
	new Ajax.Request(filepath,
		{
			method: methodType,
			parameters: params,
			// Show the error message if failure
			onFailure: Show_Error,
			onSuccess: function (transport) {
				alert(transport.responseText);
				return transport.responseText;
			}
		});
}

function Show_Error()
{
	// Used to show the error message
	alert('The request could not be completed.');	
}