
function updateBuyUrl(pageType)
{
	updateBuyUrlReal(pageType, document.getElementById('pidVal').value);	
}

function updateBuyUrlWithPid(pageType, pid)
{
	updateBuyUrlReal(pageType, pid);
}

function updateBuyUrlReal(pageType, pid)
{
	var quantity, reguid, tag, buyUrl, el;
	
	if ((typeof(el = document.getElementById('quantity')) != 'undefined') && (isNumeric(el.value)))
		quantity = el.value;
	else if ((typeof(el = document.getElementById('quantity' + pid)) != 'undefined') && (isNumeric(el.value)))
		quantity = el.value;
	else
		quantity = 1;
	
	tag = 'buyStatusTag' + ((document.getElementById('buyStatusTag' + pid)) ? pid : '');
	reguid = (document.getElementById('reguid')) ? document.getElementById('reguid').value : -1;
	giftWrapping = ((el = document.getElementById('giftWrappingEnabled')) && (el.checked)) ? 1 : 0;

	buyUrl = "cart.php?du=1&m=add&pid=" + pid;
	buyUrl += "&q=" + quantity;
	buyUrl += "&o0=" + getOptionGetStr(0, pid);
	buyUrl += "&o1=" + getOptionGetStr(1, pid);
	buyUrl += "&o2=" + getOptionGetStr(2, pid);
	buyUrl += "&reguid=" + reguid;
	buyUrl += "&giftWrapping=" + giftWrapping;
	buyUrl += "&pagetype=" + pageType;

	replacePanel(tag, buyUrl);
}

function getOptionGetStr(optionNum, pid)
{
	
	if ((optBox = document.getElementById("optName" + optionNum)) != null) { }
	else if ((optBox = document.getElementById("optName" + optionNum + pid)) != null) { }
	else return '';
	
	optId = optBox.lastChild.id;
	selectedOpt = optBox.lastChild.childNodes[optBox.lastChild.selectedIndex].value;

	return optId + "," + selectedOpt;
}


function isNumeric(str)
{
	var validChars = "0123456789";
	for (i = 0; i < str.length; i++)
	{
		if (validChars.indexOf(str.charAt(i)) == -1)
			return false;
	}
	return true;
}
