/**
* Checks whether a given value is present in an array (emulates PHP function)
*
* @param	string		needle		The searched value
* @paran	array		haystack	Array to search through
* @return	boolean					Returns TRUE if needle  is found in the array, FALSE otherwise.
*/
function in_array(needle, haystack) {
	for(var key in haystack) {
		if(haystack[key]==needle) {
			return true;	
		}
	}
	return false;
}
/**
* Retrieves all values for a given form, ignoring any submit buttons and returns them as an array with the input name as the key
*
* @param	string		formName	The 'name' attribute of the form
* @return	array					All form values with the input name as the key and the input value as the array value
*/
function formToArray(formName) {
	// Declare our array which will hold our form values
	var dataArray=[];
	// Grab all the elements in our form
	var formElements=eval('document.forms.'+formName+'.elements;');
	// Loop through each element
	for(var i=0; i<formElements.length;i++) {
		var e=formElements[i];
		// Normal text elements
		if(e.type=='text' || e.type=='textarea' || e.type=='password' || e.type=='hidden' || e.type=='select-one') {
			dataArray[e.name]=formElements[i].value;
		}
		// Checkbox and radio elements
		if(e.type=='checkbox' || e.type=='radio') {
			dataArray[e.name]=dataArray[e.name] ? dataArray[e.name] : '';
			dataArray[e.name]=e.checked==true ? e.value : dataArray[e.name];
		}
	}
	return dataArray;
}
/**
* Retrieves all values for a given form, ignoring any submit buttons and returns them as a string suitable to be sent using AJAX
*
* @param	string		formName	The 'name' attribute of the form
* @return	string					All form values escaped and separated by ampersands 
*/
function formToString(formName) {
	var formData=formToArray(formName);
	var pairs=[];
	for(var field in formData) {
		pairs[pairs.length]=escape(field)+'='+escape(formData[field]);	
	}
	return pairs.join('&');
}
/**
* Utility function that loops through an array and alerts the contents to screen
*
* @param	array		array	The array to be alerted
*/
function alertArray(array) {
	var str='';
	for(var i in array) {
		str+=i+' => '+array[i]+'\n';	
	}
	alert(str);
}
/**
* Loops through the passed HTML string and evaluates any code between <script> tags. Typically used when retrieving content using
* AJAX.
*
* @param	string		content		The HTML string to search
* @param	int			startPos	Position to start searching from [OPTIONAL]. Defaults to 0.
*/
function evaluateScriptTags(content, startPos) {
	// Start at the beginning if we haven't specified otherwise
	startPos=!startPos ? startPos : 0;
	// Find the position of the next set of script tags
	var scriptStart=content.indexOf('<script type="text/javascript">',startPos);
	// Find the position of the javascript after the open tag has finished
	var startTag=scriptStart+31;
	// Check whther we found a start tag
	if(scriptStart>-1) {
		// Find the position of the end tag
		var endTag=content.indexOf('</script>',startTag);
		// Retrieve the code from betweent the tags
		var jSCode=content.substring(startTag,endTag);
		// Evaluate any code found
		eval(jSCode);
		// Call our function again
		if(content.indexOf('<script type="text/javascript">',endTag)>-1) {
			evaluateScriptTags(content, endTag);
		}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
