/*
** Global utilities, callbacks
*/

/*******************************
* Utility functions            *
*******************************/

/*
** Return question id from
** string (eg fireFormRow8 = 8).
**
** @param string
*/
function getNum(str)
{
	if(typeof(str) != 'undefined' && str != '')
	{
		var r = str.match(/[\d\.]+/g);	
		if(typeof(r) != 'undefined' && r && typeof(r[0]) != 'undefined') return r[0];
	}
	
	return 0;
};

/*
** Return browser specific linebreak
** control character.
*/
function getLineBreakForTextarea()
{
	var lineBreak = "\n";
	
	if($.browser.msie) lineBreak = "\r";
	
	return lineBreak;
}

/*
** Trim spaces from left 
** and right.
**
** @param string String to trim
** @param
*/
function trim(str, chars)
{
	return ltrim(rtrim(str, chars), chars);
}

/*
** Trim spaces from left.
**
** @param string String to trim
** @param
*/
function ltrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

/*
** Trim spaces from right.
**
** @param string String to trim
** @param
*/
function rtrim(str, chars)
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/*
** Determine if array 
** contains a value.
**
** @param string Needle
** @param array Haystack
*/
Array.prototype.inArray = function(value, caseInsensitive)
{
	var i;
	for (i=0; i < this.length; i++) 
	{
		// use === to check for Matches. ie., identical (===),
		if(caseInsensitive)
		{ //performs match even the string is case sensitive
			if (this[i].toLowerCase() == value.toLowerCase()) { return true; }
		}
		else { if (this[i] == value) { return true; } }
	}
	
	return false;
};

/*
** Retrun true if value
** exists in json.
**
** @param string Needle
** @param array Haystack
*/
function inJson(value, json)
{
	for(var i in json)
	{
		if(json[i] == value) return true;
	}
	
	return false;
}

/*
** Determine if argument 
** is empty.
**
** @param mixed Entity to check
*/
function empty(mixed_var)
{
	// http://kevin.vanzonneveld.net
	// +   original by: Philippe Baumann
	// +      input by: Onno Marsman
	// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +      input by: LH
	// +   improved by: Onno Marsman
	// +   improved by: Francesco
	// *     example 1: empty(null);
	// *     returns 1: true
	// *     example 2: empty(undefined);
	// *     returns 2: true
	// *     example 3: empty([]);
	// *     returns 3: true
	// *     example 4: empty({});
	// *     returns 4: true
	
	var key;
	
	if(mixed_var === "" || mixed_var === 0 || mixed_var === "0" || mixed_var === null || mixed_var === false || mixed_var === undefined) return true;
	
	if(typeof mixed_var == 'object')
	{
		for(key in mixed_var)
		{
			if(typeof mixed_var[key] !== 'function') return false;
		}
		return true;
	}
	
	return false;
}


/*******************************
* Ajax event callbacks         *
*******************************/

/*
** UNUSED
**
** Ajax start event callback.
*/
function fireFormAjaxStart()
{
	$("#fireFormAjaxProgress").show();
	$("#fireForm").jqmShow();
}

/*
** NEED TO RENAME fireFormAjaxError!
**
** Backward compatibility.
*/
//function fireFormAjaxError() { fireFormAjaxError(); }

/*
** NEED TO RENAME fireFormAjaxError!
**
** Ajax error event callback.
**
** @param mixed Error messages
** @param
*/
function fireFormAjaxError(error, jqmShow)
{
	if(typeof(error) == 'array' || typeof(error) == 'object') var errors = error;
	else var errors = error.split('\n');
 
	var html = '';
	html += '<ul class="fireFormAjaxErrorItem">';
 
	for(var i in errors)
	{
		if(typeof(errors[i]) == 'string' && errors[i].replace(/^\s+|\s+$/, '') != '')
		{
			html += '<li>' + errors[i] + '</li>';
		}
	}
 
	html += '</ul>';
 
	$("#fireForm .fireFormAjaxErrorBody").empty().append(html);
	$("#fireFormAjaxProgress").hide();   // Hide processing window (if visable)
	/*$("#fireFormAjaxError").show();*/
	$("#fireForm").jqmShow();
};

/*
** Ajax complete event callback.
*/
function fireFormAjaxComplete() { $("#fireForm").jqmHide(); }


/*******************************
* Misc.                        *
*******************************/

/*
** Confirm delete; used in 
** backend.
*/
function confirmDelete(url, msg)
{
	if(window.confirm(msg)) document.location.href = url;
	return false;
}


/*******************************
* Not sure?                    *
*******************************/

/*
**	Checks if any radios
** in a set are checked.
*/
function isRadioSelect(selector)
{
	$(selector).each(function() { if(this.checked) return true; });
	
	return false;
}

/*
**	Checks if any checkboxes 
** in a set are checked.
*/
function isCheckboxSelect(selector)
{
	$(selector).each(function() { if(this.checked) return true; });
	
	return false;	
}


/*******************************
* UNUSED????                   *
*******************************/

/*
** Unused???
**
** Used in admin area.
*/
function tableRuler(selector, className)
{
	var className = typeof(className) == 'undefined' ? 'over' : className;
   var rows = $(selector);
    
	$(rows).each(function(){
		$(this).mouseover(function(){
			$(this).addClass(className);
		});
		
		$(this).mouseout(function(){
			$(this).removeClass(className);
		});
	});
}

/*
** UNUSED - Specify feature removed.
**
** Select change callback. Used 
** to support "Specify" feature.
*/
function changeFireFormSelect(qId, elem)
{
	if(elem.value == 'fireFormOthers') $('#fireFormSelectOthers' + qId).show();
	else
	{
		$('#fireFormSelectOthers' + qId).hide();
		$('#fireFormOthers' + qId).val('');
	}
}

/*
** UNUSED - Specify feature removed.
**
** Radio change callback. Used 
** to support "Specify" feature.
*/
function changeFireFormRadio(qId, elem)
{
	if(elem.value == 'fireFormOthers') $('#fireFormSelectOthers' + qId).show();
	else
	{
		$('#fireFormSelectOthers' + qId).hide();
		$('#fireFormOthers' + qId).val('');
	}
}

/*
** UNUSED - Specify feature removed.
**
** Checkbox change callback. Used 
** to support "Specify" feature.
*/
function changeFireFormCheckbox(qId, elem)
{
	if($("#fireFormRow" + qId + " input:checked[@value=fireFormOthers][@name=\'" + qId + "[]\']").length) $('#fireFormSelectOthers' + qId).show();
	else
	{
		$('#fireFormSelectOthers' + qId).hide();
		$('#fireFormOthers' + qId).val('');
	}
}

/*
** UNUSED???
**
** Checks if the advcheckbox has been checked.
*/
function isAdvCheckboxTrue(selector)
{	
	$(selector).each(function() { if(typeof(this.checked) != "undefined" && this.checked && this.value == "1") return true; });
	
	return false;	
}
