/*
 * This method will traverse the response XML document and build a
 * simple HTML module comprised of data from the following tags:
 *
 * Data in the first <title> tag in the document.
 * Data in the first <lastBuildDate> tag in the document.
 * HTML from the second <description> tag in the document.
 *
 */
 
var ProductPopUpMain=null;
var ProductPopUpCurrentPrizeNum = null;
var ProductPopUpCurrentPrizeID = null;
var CartPopTimer = null;

function ProdPopsuccessHandler(o){
	//ProductPopUpMain.innerHTML = o.responseText
	//alert(o.responseXML)
	var root = o.responseXML.documentElement;
	var el;

	var lID = parseInt(root.getElementsByTagName('PrizeID')[0].firstChild.nodeValue);
	ProductPopUpCurrentPrizeID = lID
	
	var lNum = parseInt(root.getElementsByTagName('PrizeNum')[0].firstChild.nodeValue);
	ProductPopUpCurrentPrizeNum = lNum
	
	var sName = root.getElementsByTagName('PrizeName')[0].firstChild.nodeValue;
	
	el = root.getElementsByTagName('Subtitle')//[0].firstChild.nodeValue;
	var sSubtitle = ''
	if (el && el[0] && el[0].firstChild && el[0].firstChild.nodeValue) sSubtitle = el[0].firstChild.nodeValue;
	
	var sDescription = root.getElementsByTagName('Description')[0].firstChild.nodeValue;
	
	var cPrice = parseInt(root.getElementsByTagName('Price')[0].firstChild.nodeValue);
	
	el = root.getElementsByTagName('SponsoredBy')//[0].firstChild.nodeValue;
	var sSponsoredBy = ''
	if (el && el[0] && el[0].firstChild && el[0].firstChild.nodeValue) sSponsoredBy = el[0].firstChild.nodeValue
	
	el = root.getElementsByTagName('Icon')//[0].firstChild.nodeValue;
	var sIcon = ''
	if (el && el[0] && el[0].firstChild && el[0].firstChild.nodeValue) sIcon = el[0].firstChild.nodeValue
	
	el = root.getElementsByTagName('Winner')//[0].firstChild.nodeValue;
	var sWinner = ''
	if (el && el[0] && el[0].firstChild && el[0].firstChild.nodeValue) sWinner = el[0].firstChild.nodeValue
	
	var x = UpdateProdPop(lID,lNum, sName, sSubtitle, sDescription, cPrice, sSponsoredBy, sIcon, sWinner);
}

function ClearProdPop() {
	var x = UpdateProdPop()
}

function UpdateProdPop(lID,lNum, sName, sSubtitle, sDescription, cPrice, sSponsoredBy, sIcon, sWinner) {
	var o
//	alert(lNum)
	o = document.images['itemPopup-Image']
	if (o) {
		if (lNum && lNum!='') {o.src = 'PrizeImages/290/' + lNum + '.jpg'; o.style.display='block';  } else { o.style.display='none'; o.src = 'images/clear.gif'; };
	}
	
	o = document.getElementById('itemPopup-Num')
	if (o) {
		if (lNum && lNum!='') { o.innerHTML = lNum + '.' ; o.style.display='block'} else { o.style.display='none' }
	}
	
	o = document.getElementById('itemPopup-Name')
	if (o) {
		if (sName && sName!='') { o.innerHTML = sName ; o.style.display='block'} else { o.style.display='none' }
	}
	
	o = document.getElementById('itemPopup-Subtitle')
	if (o) {
		if (sSubtitle && sSubtitle!='') { o.innerHTML = sSubtitle ; o.style.display='block'} else { o.style.display='none' }
	}

	o = document.getElementById('itemPopup-Description')
	if (o) {
		if (sDescription && sDescription!='') { o.innerHTML = sDescription ; o.style.display='block'} else { o.style.display='none' }
	}

	o = document.getElementById('itemPopup-SponsoredBy')
	if (o) {
		if (sSponsoredBy && sSponsoredBy!='') { o.innerHTML = sSponsoredBy ; o.style.display='block'} else { o.style.display='none' }
	}

	o = document.getElementById('itemPopup-Price')
	if (o) {
		if (cPrice && cPrice!='') { o.innerHTML = '$' + cPrice ; o.style.display='block'} else { o.style.display='none' }
	}

	o = document.images['itemPopup-Icon']
	if (o) {
		if (sIcon && sIcon!='') { o.src = 'images/icon_' + sIcon + '.gif'; o.alt = sIcon; o.style.display='block';  } else { o.style.display='none'; o.src = 'images/clear.gif'; };
	}

	o = document.getElementById('itemPopup-Winner')
	if (o) {
		if (sWinner && sWinner!='') { o.innerHTML = 'Winner: ' + sWinner ; o.style.display='block'} else { o.style.display='none' }
	}

	o = document.forms['AddToCart']
	if (o) {
			o.elements['Prize'].value = lID
	}
}
/*
 *
 * This is a simple failure handler that will display
 * the HTTP status code and status message if the resource
 * returns a non-2xx code.
 *
 */
function ProdPopfailureHandler(o){
	//ProductPopUpMain.innerHTML = o.status + " " + o.statusText;
}

var oProdPopCallback = {
	success: ProdPopsuccessHandler,
	failure: ProdPopfailureHandler,
	timeout: 5000
}

function GetProdPop(lItem) {
	ProductPopUpMain = document.getElementById('itemPopupMain');
//	return
	var x = ClearProdPop()
	hideCart()
	if (CartPopTimer) clearTimeout(CartPopTimer)

	var sUrl = 'PrizeDetailXML.asp?Prize=' + lItem
	var transaction = YAHOO.util.Connect.asyncRequest('GET', sUrl, oProdPopCallback, null);
}
function AddToCartsuccessHandler(o){
	//ProductPopUpMain.innerHTML = o.responseText
	var root = o.responseXML.documentElement;
	var el;
	var lStatus = parseInt(root.getElementsByTagName('Status')[0].firstChild.nodeValue);
	var sMsg = root.getElementsByTagName('Message')[0].firstChild.nodeValue;
	var lItems = parseInt(root.getElementsByTagName('Items')[0].firstChild.nodeValue);
	var o = document.getElementById('CartQuantity');
	if (o) o.innerHTML = lItems;

	UpdateCartPop()
	
	var sMsg = root.getElementsByTagName('Message')[0].firstChild.nodeValue;
	if (lStatus = 1) {	// success
		var lNum = parseInt(root.getElementsByTagName('PrizeNum')[0].firstChild.nodeValue);
		var sName = root.getElementsByTagName('PrizeName')[0].firstChild.nodeValue;
		UpdateCartPop(lStatus, sMsg, lNum, sName);
	} else {
		UpdateCartPop(lStatus, sMsg);
	}
	showCart();
	CartPopTimer = setTimeout("hideCart()",5000);
}

function UpdateCartPop(lStatus, sMsg, lNum, sName) {
	var o

	o = document.getElementById('cartPopup-Msg')
	if (o) {
		if (sMsg && sMsg!='') { o.innerHTML = sMsg ; o.style.display='inline'} else { o.style.display='none' }
	}

	o = document.images['cartPopup-Image']
	if (o) {
		if (lNum && lNum!='') {o.style.display='inline'; o.src = 'PrizeImages/75/' + lNum + '.jpg' } else { o.style.display='none' };
	}
	
	o = document.getElementById('cartPopup-Num')
	if (o) {
		if (lNum && lNum!='') { o.innerHTML = lNum ; o.style.display='inline'} else { o.style.display='none' }
	}
	
	o = document.getElementById('cartPopup-Name')
	if (o) {
		if (sName && sName!='') { o.innerHTML = sName ; o.style.display='inline'} else { o.style.display='none' }
	}

}

function AddToCartfailureHandler(o){
	//ProductPopUpMain.innerHTML = o.status + " " + o.statusText;
	alert(o.status + ' ' + o.statusText)
//	alert(o.responseText)
}

var oAddToCartCallback = {
	success: AddToCartsuccessHandler,
	failure: AddToCartfailureHandler,
	timeout: 5000
}

function AddToCartpop() {
	var frm = document.forms['AddToCart']
	if (frm.elements['Quantity'].value == '' || isNaN(parseInt(frm.elements['Quantity'].value))) {
		alert('Please enter a quantity')
		return
	}
	
	hidePopup()	//hide product popup
	YAHOO.util.Connect.setForm(document.forms['AddToCart'])
	var sUrl = 'AddToCartXML.asp'
	var transaction = YAHOO.util.Connect.asyncRequest('POST', sUrl, oAddToCartCallback, null);
}

