
/**
 * Project:     Phoenix Services Online
 * File:        basescripts.js
 *
 * Please do not copy or distribute this code without prior consent
 * from Webstrong Internet Solutions.
 *
 * This software is protected under Irish CopyRight Law.
 *
 * @link http://www.webstrong.net/
 * @copyright 2007-2008 Webstrong Internet Solutions
 * @author Iarfhlaith Kelly <ik at webstrong dot net>
 * @package PhoenixServicesOnline
 * @version 1.0
 *
 * For questions, help, comments, discussion, etc., please visit the
 * Webstrong website. http://www.webstrong.net
 *
 */

function runFormVisuals()
{
	// Remove Any Notices
	$(".error").empty().hide();
	
	// Grey Out All Form Inputs
	$("input, select, textarea").addClass('disabled');
	
	// Display Loading Graphic
	$('#loader').show().css('display','inline');
	
	// Update Submit Button Text and Disable
	$("#submit").val('Please Wait...');	
}

function resetFormVisuals(tag)
{
	// Enable the Form
	$("input, select, textarea").attr('disabled', '');
	
	// Hide Loading Graphic
	$("#loader").hide();
	
	// Update Submit Button Text and Disable
	$("#submit").val(tag);
}

/**** Used for the autocomplete plugin ****/
function findValueCallback(event, data, formatted)
{
	$("<li>").html( !data ? "No match!" : "Selected: " + formatted).appendTo("#result");
}

/**** Used for the autocomplete plugin ****/
function formatItem(row)
{
	return row[0] + " (<strong>id: " + row[1] + "</strong>)";
}

/**** Used for the autocomplete plugin ****/
function formatResult(row)
{
	return row[0].replace(/(<.+?>)/gi, '');
}

/**
 * Asynchronously retrieve data as JSON 
 *
 * Success: 	Returns Data as a JSON encoded array
 * Incomplete:	Form errors will be highlighted
 * Not Allowed: Display error message
 * Comm Error:	Display comm error message
 *
 * @param   string  action	 - The name of the script to submit the form to.
 * @param	array	formVars - A serialised string of the submitted form fields
 * @param   string  type     - The type of load required ('select' or 'table')
 * @param   string  dest     - The selector of the destination object (example: 'select#taskSelect')
 * @return	void
 *
 */
function loadLocations(companyID)
{
	// Show Location Loader
	$('#locationLoader').show();
	
	// Do the AJAX to submit the form.
	$.ajax
	({
		type:	  'GET',
		url:	  '/locations.php',
		data:	  'id='+companyID+'&api=1',
		dataType: 'json',
		
		success: function(data)
		{
			var options = "";
			
			if(data['response'].length > 1)
			{
				options = "<option value='' id='locationDefault'>Please Select...</option>";
			}
			
			// Populate Select Box
			for (var i = 0; i < data['response'].length; i++)
			{	
        		options += "<option value='" + data['response'][i].id + "'>" + data['response'][i].country + "</option>";
      		}
			
			$('#locationLoader').hide();
			$('#location').html(options).removeAttr('disabled').focus();
			$('#locationDefault').attr('selected', 'selected');
		},
	
		error:function(data)
		{
			resetFormVisuals();
			alert('Ooops! Looks like there was a problem communicating with the server. Please try again in a little while.');
			
			// Hide Location Loader
			$('#locationLoader').hide();
		}
	});
}
		