var srvr = location.protocol + "//" + location.host;

var getDetailsForItem_req;

var selectedPriceType = 0;
var selectedItem = 0;

var optionlist = [];
var optionselection = [];

var purchaseItem_req;

function PurchaseInfo(msg)
{
	var dialogTitle = 'Merchandise added to cart';
	
	if (msg.indexOf("currently unavailable") > -1 )
		dialogTitle = "Merchandise Unavailable";
		
	var hndlnginfo = showModalWindow('purchase-modal-panel', dialogTitle, 258, 255, 350, 200);

	hndlnginfo.find('.modal-content').html(msg);
	
	hndlnginfo.find('.bottombar .button').html('<a href="#" onclick="hideModalWindow(\'purchase-modal-panel\'); return false;"><img src="/assets/images/button_OK.png" width="108" height="32" alt="OK" title="OK" /></a>');
	
}



function purchaseItem_complete()
{
	if (purchaseItem_req.readyState < 4 ) return;
	var result = purchaseItem_req.responseText;
	$('#img_waiting').hide();
	var dest = document.getElementById("option_content");
	var src;
	if ( result.substr(0,2) == "OK" )
	{
		src = document.getElementById("purchase_success_result");
	}
	else
	{
		src = document.getElementById("purchase_failure_result");
		dest.innerHTML = src.innerHTML;
	}
	PurchaseInfo(src.innerHTML);
		
}

function purchaseItem()
{
	var xy = getxy("test_button");
	$('#img_waiting').css("left", "" + xy.x + "px");
	$('#img_waiting').css("top", "" + xy.y + "px");
	$('#img_waiting').show();
	
	$('#test_button').hide();
	
	try
	{
		purchaseItem_req = getreq();
		purchaseItem_req.open("POST", (srvr + "/assets/lib/ajax/reserve_seats.aspx"), true);
		purchaseItem_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		purchaseItem_req.onreadystatechange = purchaseItem_complete;
		var qs = "perf_no=" + selectedItem;
		qs += "&price_type=" + selectedPriceType;
		qs += "&qty=" + document.getElementById("tbx_qty").value;
		qs += "&zone_no=0";
		if(optionlist.length > 0)
		{
			var optionstring = "";
			for(x=0; x<optionlist.length; x++)
			{
				if(optionstring.length>0)
					option_string += "|";
				optionstring += optionlist[x] + "_" + escape(optionselection[optionlist[x]]);
			}
			qs += "&options=" + optionstring;
		}
		purchaseItem_req.send(qs);
	}
	catch(err)
	{
		alert(err.description ? err.description : err );
	}
}

function setOption(i,j)
{
	var allset = ( j == '' ? false : true );
	optionselection[i] = j;
	for (x=0;x<optionlist.length;x++)
	{
		if (! optionselection[optionlist[x]])
			allset = false;
	}
	if (allset)
		$('#test_button').show();
	else
		$('#test_button').hide();
}

function showPricing (o)
{	
	var els = o.getElementsByTagName("pricing");
	if (els.length > 0 )
	{
		selectedPriceType = els[0].getElementsByTagName("price_type").item(0).firstChild.nodeValue;
		var td_selected_price = document.getElementById("td_selected_price");
		td_selected_price.innerHTML = els[0].getElementsByTagName("price").item(0).firstChild.nodeValue;
	}

	els = o.getElementsByTagName("option_type");
	for(x = 0; x < els.length; x++ )
		optionlist[ x ] = els[x].getElementsByTagName("option_description").item(0).firstChild.nodeValue;
	if (els == null || els.length == 0 )
		$('#test_button').show();

	
	els = o.getElementsByTagName("option_content");
	if (els.length > 0 )
	{
		// rewritten to solve stupid DOM problem with Safari
		try
		{
			for (var x = 0; x < els[0].childNodes.length; x++ )
			{
				if ( els[0].childNodes[x].nodeType == 1 )
				{
					$('#option_content').html(els[0].childNodes[x].childNodes[0].nodeValue );
				}
			}
		}
		catch (err)
		{
			// alert("dw-store L107: " + ( err.description ? err.description : err ) );		
		}
	}
}

function getDetailsForItem_complete()
{
	if (getDetailsForItem_req.readyState < 4 ) return;
	var result = getDetailsForItem_req.responseXML;
	showPricing(result.documentElement);
}

function getDetailsForItem(i)
{

	try
	{
		$('#test_button').hide();
		getDetailsForItem_req = getreq();
		getDetailsForItem_req.open("POST", (srvr + "/store/ajax/storesupport.asmx/GetPricing"), true);
		getDetailsForItem_req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		getDetailsForItem_req.onreadystatechange = getDetailsForItem_complete;
		getDetailsForItem_req.send("perf_no="+i);
	}
	catch(err)
	{
		alert(err.description ? err.description : err );
	}

}


function getItem(s)
{
	try
	{
		var i = parseInt(s);
		if (i>0)
		{
			getDetailsForItem(i);
			selectedItem = i;
		}	
	}
	catch(err)
	{
		alert(err.description ? err.description : err );
	}
	
}

