// Detect if the browser is IE or not.
var IE = document.all ? true : false; // force to boolean
// If NS -- (!IE) -- then set up for mouse capture
if(!IE)
	document.captureEvents(Event.MOUSEMOVE);

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;
var coordinates = new Array();

// Main function to retrieve mouse x-y pos.s
function getMouseXY(e)
{
	var mouse_x = 0;
	var mouse_y = 0;
	if(IE)
	{
		// grab the x-y pos.s if browser is IE
		mouse_x = event.clientX + document.body.scrollLeft;
		mouse_y = event.clientY + document.body.scrollTop;
	}
	else
	{
		// grab the x-y pos.s if browser is NS
		mouse_x = e.pageX;
		mouse_y = e.pageY;
	}
	// catch possible negative values in NS4
	if(mouse_x < 0)
		mouse_x = 0
	if(mouse_y < 0)
		mouse_y = 0

	coordinates[0] = mouse_x;
	coordinates[1] = mouse_y;
	return coordinates;
}

function pageWidth() 
{
	return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? 	document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 

function pageHeight() 
{
	return  window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

function posLeft() 
{
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
} 
function posTop() 
{
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function posRight() 
{
	return posLeft() + pageWidth();
}

function posBottom() {return posTop()+pageHeight();}

function show(element_name, html, hide_others)
{ 
	if(hide_others)
		$('.drag_div').hide();

	$('#'+element_name).html(html);
	$('#'+element_name).css('left', coordinates[0] + 100);
	$('#'+element_name).css('top', coordinates[1]);
	$('#'+element_name).show();
}

function stop_bubble(event)
{
	if(!event)
		var event = window.event;
	event.cancelBubble = true;
	if(event.stopPropagation)
		event.stopPropagation();
}
function get_file_upload_id()
{
	return Math.floor(Math.random() * (1 << 30)).toString(16);
}


// For arrays
function unique(a)
{
	tmp = new Array(0);
	for(i=0;i<a.length;i++)
	{
		if(!contains(tmp, a[i]))
		{
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}

function contains(a, e)
{
	for(j=0;j<a.length;j++)
		if(a[j]==e)
			return true;
	return false;
}
function htmlspecialchars(string)
{
	return string.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
}

function bookmark(url,title)
{
	if((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4))
		window.external.AddFavorite(url, title);
	else if(navigator.appName == "Netscape")
		window.sidebar.addPanel(title, url ,"");
	else
		alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
}
function valid_email(email)
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);
}

function add_item(post)
{
	$.ajax
	({
		type:	"POST",
		url:	"/ajax/eval.ajax.php",
		data:	"function=inventory::add_item" + "&" + post,
		success: function(response)
		{
			$('.quantity_box,.add_to_basket_anchor').attr('disabled', false);

			window.location.href = '/basket.php';
		}
	});
}


Array.prototype.in_array = function(value)
{
	for(var i in this)
		if(this[i] === value)
			return i;
	return false;
}

function numbers_only(evt, decimal_allowed)
{
	if(decimal_allowed == 'undefined')
		decimal_allowed = false;

	var allowed_codes = new Array
		(
			  8,											// Backspace
			  9,											// Tab
//			 13,											// Enter
			 16,											// Shift
			 35, 36,										// Home / End
			 37, 38, 39, 40,								// L / U / R / D
			 46,											// del
			 48, 49, 50, 51,  52,  53,  54,  55,  56,  57,	// 0-9 Keyboard
			 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,	// 0-9 Number pad
			116												// F5 (So you can still refresh the browser)
		);

	if(decimal_allowed)
		allowed_codes = allowed_codes.concat(new Array(110, 190));					// decimal point (Number pad / Keyboard)

	var charCode = (evt.which) ? evt.which : event.keyCode;

	/*/
	alert(charCode);
	//*/

	return (allowed_codes.in_array(charCode) !== false); // remember the function could return 0 so you need to check for boolean false	
}

function filter_special_chars(input)
{
	var converted_string = '';
	for(i=0; i < input.length; i++)
	{
		if(input.charCodeAt(i)>127)
			converted_string += '&#' + input.charCodeAt(i) + ';';
		else
			converted_string += input.charAt(i);
	}
	return converted_string;
}