function AddToCart(itemInfo)
{
	var _price = $(itemInfo).attr('price');
	var _itemName = $(itemInfo).attr('itemName');
	var _itemNumber = $(itemInfo).attr('itemNumber');
	
	var _purchaseForm = $('#purchaseForm');
	_purchaseForm.children('input[name=amount]').val(_price);
	_purchaseForm.children('input[name=item_name]').val(_itemName);
	_purchaseForm.children('input[name=item_number]').val(_itemNumber);
	
	_purchaseForm[0].submit();
}

function ViewCart()
{
	$('#cartForm')[0].submit();
}

var __activeOverlayCallbackOnClose;
var __activeOverlayCallbackReturnCode;
function CloseActiveOverlay(returnCode, reloadWindow)
{
	$.unblockUI();
	
	if (__activeOverlayCallbackOnClose != null && __activeOverlayCallbackReturnCode == returnCode)
		__activeOverlayCallbackOnClose();
	
	if (reloadWindow)
		location.reload();
}

function OverlayURL(url, width, height, callbackReturnCode, callbackOnClose)
{
	__activeOverlayCallbackReturnCode = callbackReturnCode;
	__activeOverlayCallbackOnClose = callbackOnClose;
	
	$.extend(
		$.blockUI.defaults.overlayCSS, 
		{ 
			backgroundColor: '#000', 
			opacity: '0.6', 
			cursor: 'default'
			//, height: '100%' 
		}
	);

	var _borderWidth = 3;
	$.extend
	(
		$.blockUI.defaults.css,
		{ 
			left: '50%',
			width: (width + _borderWidth) + 'px', 
			height: (height + _borderWidth) + 'px', 
			margin: '-' + (height + _borderWidth) / 2 + 'px 0 0 -' + (width + _borderWidth) / 2 + 'px'
		}
	);
		
	
	$.blockUI.defaults.fadeIn = 1;
	$.blockUI.defaults.fadeInTime = 300;

	if ($.browser.msie) 
	{
		setTimeout
		(
			"$.blockUI" +
			"(" +
			"	{" +
			"		message: '<iframe id=\"overlayURL\" style=\"width:" + width + "px;height:" + height + "px\" src=\"" + url + "\" scrolling=\"no\" border=\"0\"></iframe>'," +
			"		css:" +
			"		{" +
			"			border: '" + _borderWidth + "px solid #CCCCCC'," +
			"			cursor: 'default'" +
			"		}" +
			"	}" +
			");", 1
		);
	}
	else 
	{
		$.blockUI
		(
			{
				message: '<iframe id="overlayURL" style="width:' + width + 'px;height:' + height + 'px" src="' + url + '" scrolling="no" border="0"></iframe>',
				css:
				{
					border: _borderWidth + 'px solid #CCCCCC',
					cursor: 'default'
				}
			}
		);
	}	
}


//OPEN DIALOG WINDOW
var name='';
function openDialog(url,name,w,h){
	var left = (screen.width/2) - w/2;
	var top = (screen.height/2) - h/2;
	var s = 'no';
	if(name == 'vs')
		s = 'yes';

	if ((name = window.open(url,name,'toolbar=no,location=no,directories=no,status='+s+',menubar=no,scrollbars='+s+',resizable=yes,copyhistory=no,width='+w+',height='+h+',left='+left+',top='+top+',screenX='+left+',screenY='+top)) != null) {
		if (window.focus) 
			name.focus();
	}
}

function ReturnAction(evt, targetEvent) {
	evt = (evt) ? evt : ((window.event) ? window.event : "")

	if(evt.which || evt.keyCode) {
		if ((evt.which == 13) || (evt.keyCode == 13)) {
		
			BrowserDetect.init();
			if (BrowserDetect.browser == "Explorer") {
				event.returnValue = false;
				event.cancel = true;
			}
		
			targetEvent.click();
			return false;
		}
	}
	else
		return true
}

function toggleVisibility(obj) {
	if (obj != null) {
		if (obj.style.display != 'block')
			obj.style.display = 'block';
		else
			obj.style.display = 'none';
	}
}

function ConfirmDelete() {
	return confirm("Are you sure you want to delete?");
}

function SetWindowStatus(statusText) {
	window.status = statusText;
}

function RedirectURL(url) {
	window.location.href = url;
}

//SHOW MODAL
function openModal(url,modalname,w,h){
	var result = window.showModalDialog(url,modalname,'help:off; status:no; resizable:yes; scroll:no; dialogWidth:'+w+'px; dialogHeight:'+h+'px;');
	return result;
}

//Refreshes the opening window
function refreshOpener(closePopup) {
	if(window.opener && !window.opener.closed)
		//window.opener.location.href = window.opener.location.href.replace('#','');
		window.opener.location.reload(); // seems to work in both browsers
	
	if (closePopup)
		window.close();	
}

//Returns a list's element count
function listLen(list, delimiter) {
	var _start = 0;
	var _elementCount = (list != null || list != "") ? 1 : 0; 
	
	while ((_end = list.indexOf(delimiter, _start)) != -1) {
		//Increment the element count
		_elementCount++;
		_start = _end + 1;
	}
	//Return the listCount
	return _elementCount;
}

//Returns the list element at the requested position
function listGetAt(list, position, delimiter) {
	var _start = 0;
	var _retValue = ""; 
	var _position = position;
	
	while (_position-- > 0 && (_end = list.indexOf(delimiter, _start)) != -1) {
		//Requested Position element
		if (_position == 0)
			_retValue = list.substring(_start, _end);
		_start = _end + 1;
	}
		
	//User may have requested the last element of a list
	if (_position == 0)
		_retValue = list.substring(_start);
	return _retValue;
}

//Replaces an element in a list and returns the list
function listSetAt(list, position, value, delimiter) {
	var _retValue = "";
	var _listLen = listLen(list, delimiter);
	
	if (_listLen >= position) {
		for (var i = 1; i <= _listLen; i++) {
			
			//Append the delimiter
			if (i != 1)
				_retValue += delimiter;
			
			if (i == position)
				_retValue += value;
			else
				_retValue += listGetAt(list, i, delimiter);
		}
	}
	return _retValue;
}

//Searched the DOM for a particular attribute
function getElementsByAttribute(attr, val, tagType, topElement) { 
	var _retArray = [];
	
	//See if we want to start from a top element rather than from the document root (for performance)
	var _topElement = topElement != null ? topElement : document;
	var _tags = tagType != null ? _topElement.getElementsByTagName(tagType) : _topElement.all || _topElement.getElementsByTagName('*');
	for(var k=0; k < _tags.length; k++) {
		if(_tags[k].getAttribute(attr) == val)
			_retArray[_retArray.length] = _tags[k];
	}
	return _retArray;
} 

var BrowserDetect = {
	init: function () {
		if (this.browser == null) {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		}
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};