var VIEWERS_CHOICE_OFFER = "/bzJApp/ViewersChoiceOffer.action";
var viewersChoiceProcessURL = "";

/***
 * check if VCO is available for this item and show it to the user
 */
function viewersChoiceOffer(listingID){
	
	var viewersChoiceURL = VIEWERS_CHOICE_OFFER + "?" + "inListing=" + listingID + "&inUserName=" + currentUser +"&cb=" + Math.random();
		
	logToWindow("viewers Choice URL: " + viewersChoiceURL);
	
	$.ajax(
	{	url: viewersChoiceURL,
		context: this,
		success: processVCOResponse,
		error : processVCOResponseError
	});
 
}

 function processVCOResponse (data, textStatus, XMLHttpRequest) {
 	logToWindow("processVCOResponse: " + data);
	var vcoStatusStr = "0";
	
	var vcoResultDoc = parseXMLString(data);
	
	var errorStatus = vcoResultDoc.getElementsByTagName("errorStatus")[0];
	var errorStatusStr = errorStatus.firstChild.nodeValue;
	
	if (errorStatusStr =="NO_ERROR"){
	//get the root of the XML result
		var vcoStatus = vcoResultDoc.getElementsByTagName("status")[0];
	
		vcoStatusStr = vcoStatus.firstChild.nodeValue;
	
		logToWindow("VCO status: " + vcoStatusStr);
		
		var titleStr = vcoResultDoc.getElementsByTagName("title")[0];
	
		var title = titleStr.firstChild.nodeValue;
	
		logToWindow("title : " + title);
		
		var thumbURLString = vcoResultDoc.getElementsByTagName("thumbURL")[0];
	
		var thumbURL = thumbURLString.firstChild.nodeValue;
	
		logToWindow("thumb url: " + thumbURL);
		
		var currentAmountString = vcoResultDoc.getElementsByTagName("currentAmount")[0];
	
		var currentAmount = currentAmountString.firstChild.nodeValue;
	
		logToWindow("currentAmount: " + currentAmount);
		
		var listingNode = vcoResultDoc.getElementsByTagName("listing")[0];
		
		var listing = listingNode.firstChild.nodeValue;
	
		logToWindow("currentAmount: " + currentAmount);
		if (vcoStatusStr=="1") {
			// show VCO msg
			showVCO(title,thumbURL,currentAmount,listing);
		}
	}
		
	return;
	
} //END: processVCOResponse
 
 function processVCOResponseError (XMLHttpRequest, textStatus, errorThrown) {
 	 logToWindow("processVCOResponseError: " + textStatus);
 	 if (errorThrown != null)
 	 {
 		processVCOException(XMLHttpRequest, errorThrown);
 	 }
 } //END: processVCOResponseError
  
 function processVCOException (Req, Ex1) {
	logToWindow("processVCOException: " + Ex1);
 } //END: processVCOException
 
  
function  viewersChoiceOfferProcess(listingID){	
	viewersChoiceProcessURL = vcoProcessLink + "&listing=" + listingID;
	toggleBox('vcoDiv', 0);
	logToWindow("viewers Choice process URL: " + viewersChoiceProcessURL);
	location.href=viewersChoiceProcessURL;
}
 /***
  * Pop up an Div to show the VCO message
  * @param {String} msg
  */
 function showVCO(title,thumbURL,offerAmount,listing) {
 	nf.setNumber(offerAmount);
 	$('#vcoHeader').html("We have an Offer for You!");
	$('#vcoItem').html("<div><img src='" + thumbURL + "' alt='' /></div>" + title);
 	$('#vcoText').html("You have a chance to purchase this item right now for the unbelievable price of<br/><span class='vcoAmount'>" + nf.toFormatted() + "</span><br/>Hurry! This offer will expire in <strong>60 seconds...</strong><a href='javascript:viewersChoiceOfferProcess("+listing+")'><span>Act Now!</span></a>");
	toggleBox('vcoDiv', 1);
 }
 
 /***
  * Pop up an Div to show the VCO process success message
  * @param {String} msg
  */
 function showVCOProcessSuccess() {
 	logToWindow("checkout link : " + checkoutLink);
 	toggleBox('infoDiv', 0);
 	$('#infoHeader').html("Viewers Choice Offer");
 	$('#infoText').html("Item successfully added to your cart. Close this window to continue shopping."
 		 + "<br><br><a href='" + checkoutLink + "'>Checkout Now</a><br>");
	toggleBox('infoDiv', 1);
 }
 /***
  * Pop up an Div to show the VCO Process failure message
  * @param {String} msg
  */
 function showVCOProcessFailure() {
 	toggleBox('infoDiv', 0);
 	$('#infoHeader').html("Viewers Choice Offer");
 	$('#infoText').html("Sorry! This offer is no longer available. Close this window to continue shopping."); 		 
	toggleBox('infoDiv', 1);
 } 

