/**
* 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 doPositionFixed() {
	
	// Checks for IE6
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	
	//if(IE6) {
	if($.browser.msie && $.browser.version < 7.0){
		positionPopupsIE6();
		
		// Add scroll event to make sure popup and mask stay in centre of screen
		$(window).scroll(function() {
			positionPopupsIE6();
		});
		
	} else {
		// Make position fixed for newer browsers
		$('.fixed').css('position','fixed');	
	}		
}
function positionPopupsIE6() {
	// Get some properties we need to position our items
	var popupWidth		= $('#popup').width();
	var popupHeight		= $('#popup').height();
	var windowX			= $(window).scrollLeft();
	var windowY			= $(window).scrollTop();
	var windowWidth		= $(window).width();
	var windowHeight	= $(window).height();
	var viewportWidth	= $(window).innerWidth();
	var viewportHeight	= $(window).innerHeight();
	$("#popup_mask").css({
		'top'			:		windowY + 'px',
		'left'			:		windowX + 'px',
		'height'		:		windowHeight + 'px',
		'width'			:		windowWidth + 'px'
	});
	
	var popupTop = windowY + (viewportHeight/2) - (popupHeight/2); 
	var popupLeft = windowX + (viewportWidth/2) - (popupWidth/2); 

	$("#popup").css({
		'top'			:		popupTop + 'px'
		//'left'			:		popupLeft + 'px'
	});	
}

function customConfirm(yes_callback, no_callback, message) {
	var no_callback=no_callback ? no_callback : null;
	// Get target of link
	var target=$(this).attr('href');
	var message=message ? message : 'Are you sure you want to do this???';
	var popup='<div id="popup_mask" class="fixed"></div><div id="popup" class="fixed"><div id="popup_inner"><h3>'+message+'</h3><div class="custom_buttons"><a  id="custom_yes">Yes</a> <a  id="custom_no">No</a></div></div></div>';
	$('body').append(popup);
	// Call our function to replace headings etc with nicer font		
	
	// Call our function to emulate position fixed for any popups etc
	doPositionFixed();
	// Add href attribute to yes button
	$('#custom_yes').click(function() {
		$('#popup_mask, #popup').remove();
		if(yes_callback) {
			yes_callback();
		}
	});
	// Add href attribute to no button
	$('#custom_no').click(function() {
		$('#popup_mask, #popup').remove();
		if(no_callback) {
			no_callback();
		}
	});
}


$().ready(function() {
	// Custom warning popup box for links with class 'warn_link'
	$('.warn_link').click(function() {
		// Get target of link
		var linkTarget=$(this).attr('href');
		customConfirm(
			function() {
				window.location=linkTarget;
			},
			function() {
				return false;
			},
			'Are you sure you want to do this???'
		);
		return false;
	});
	//jTip hover functions
	$('.jTip').hover(function() {
		$(this).siblings('.jTip_content').css('display','block');
		$('.jTip').not($(this)).parent('.jTip_container').css('position', 'static');
	},
	function() {
		$(this).siblings('.jTip_content').hide();
		$('.jTip_container').css('position', 'relative');
	});
	// AK - Since hiding the tooltips is buggy, hide them whenever w are over input/select boxes to be sure
	$('input, select').hover(function() {
		$('.jTip_content').hide();								  
	});
	
});


