  /*******************************************************************
  * bidObject :
  * 
  * stores the following properties
  * 
  * listingID :
  * bidAmount :
  * bidButton :
  * bidInput  :
  * 
  ********************************************************************/
 
 function bidObject() {
 	this.listingID = "";
	this.bidAmount = "";
	this.bidButton = "";
	this.bidInput  = "";
 } //END: bidObject constructor
 
 var PLACE_BID_URL			= "/bzJApp/PlaceBidXML.action"; 
 var CUSTOMER_COOKIE_NAME   = "customer";
 var SHOW_USER_PROXY ="N";
 var SHOW_USER_MAX_BID ="N";
 
 function linkVerifyUser() {
 	location.href=verifyLink; //globally set in the site header
 } //END: linkVerifyUser
 
 /***
  * Expected parameters for PlaceBid.action:
  * 
	<input type="hidden" name="tid" value="xxx" />
	<input type="hidden" name="sid" value="yyy" />
	<input type="hidden" name="auctionId" value="zzz" />
	<input type="hidden" name="mode" value="finalize" />
	<input type="text" name="bidAmt" value="qqq" />
  */
 
 function placeBid(elem_input, elem_button, listno, sid, tid) {
 
 	logToWindow("Placing bid: " + $(elem_input).value + " on listing: " + listno);
	
	//let's check if the user is logged in to place a bid
	var customerCookie = getCookie(CUSTOMER_COOKIE_NAME);
	if (customerCookie == null || customerCookie=="" || customerCookie.length<5) {
		logToWindow("User must be logged on before a bid is placed. Redirecting.");
		return redirectLogon();
	} //END: invalid customer
	
	//first let's remove the focus lock so the nextbid gets updated
	lockedInputField = null;
	
	//disable the button while the bid is being placed
	//$(elem_button).disabled = true;
	//$(elem_button).value = "Bidding...";
	
	//store the bid values for reference once the bid comes back
	var bidObj = new bidObject();
	
	bidObj.listingID = listno;
	bidObj.bidAmount = $(elem_input).value;
	bidObj.bidButton = elem_button;
	bidObj.bidInput  = elem_input;
	
	var bidUrlWithCacheBuster = PLACE_BID_URL + "?" + "sid=" + sid + "&tid=" + tid + "&auctionId=" + bidObj.listingID + "&mode=finalize&bidAmt=" + bidObj.bidAmount + "&cb=" + Math.random();
	
	logToWindow("Placing bid with URL: " + bidUrlWithCacheBuster);
	
	var ajaxReq = new Ajax.Request(bidUrlWithCacheBuster, { method: 'get', onSuccess: processBidResponse, onFailure : processBidResponseError, onException : processBidException });
 	
 } //END: placeBid
 
 
var DEFAULT_LOGON_URL = "https://secure.bidz.com/CustomerLogin/Login.aspx?returnURL=http://www.bidz.com/";
 
 function redirectLogon() {
  	if (redirectLogonURL==null || redirectLogonURL=="") {
		location.href = DEFAULT_LOGON_URL;
	}
	else {
		location.href = redirectLogonURL;
	}
 } //END: redirectLogon
 
 /***
  * Ajax Handlers
  * 
  */

/***
 * 
 * processBidResponse is responsible for parsing the placeBidXML reponse
 * and displaying an error message if appropriate.
 * 
 * @param {Object} transport
 */

 function processBidResponse (transport) {
 	logToWindow("processBidResponse: " + transport.responseText);
	
	var bidResultDoc = (new DOMParser()).parseFromString(transport.responseText, "text/xml");
	
	//logToWindow("bidResultDoc: " + bidResultDoc);
	
	//get the root of the XML result
	var errorStatus = bidResultDoc.getElementsByTagName("errorStatus")[0];
	
	var errorStatusStr = errorStatus.firstChild.nodeValue;
	
	logToWindow("Error status: " + errorStatusStr);
	
	if (errorStatusStr=="NO_ERROR") {
		var userProxy = bidResultDoc.getElementsByTagName("userProxy")[0];
			logToWindow("user proxy: " + userProxy);
					
			var userProxyVal = userProxy.firstChild.nodeValue;
			logToWindow("user proxyval: " + userProxyVal);

			//get the auctiopn id
			var auctionIdNode = bidResultDoc.getElementsByTagName("auctionId")[0];					
			var auctionId = auctionIdNode.firstChild.nodeValue;
			logToWindow("auction id: " + auctionId);
				
		if (SHOW_USER_PROXY =="Y"){		
			if (userProxyVal!="" && userProxyVal !="-1.0"){
				displayUserProxy(userProxyVal, auctionId);			
			}
			else if (userProxyVal =="-1.0"){
				disableUserProxy(auctionId);
			}
		}
		
		if (SHOW_USER_MAX_BID == "Y"){
			if (userProxyVal!="" && userProxyVal !="-1.0"){
				displayUserMaxBid(userProxyVal, auctionId);
			}
		}
		//we are OK, nothing to do, no error to consume
		return;
	}
	
	//now let's get the error messages
	var msgElements = bidResultDoc.getElementsByTagName("errorMsg");
	
	var msgToHTML = "";	
	
	for (i=0; i<msgElements.length; i++) {
		logToWindow("Error message found: " + msgElements[i].firstChild.nodeValue);
		msgToHTML = msgToHTML + "<li>" + msgElements[i].firstChild.nodeValue;
	} //END: looping through error messages
	
	showBidError(msgToHTML);
 } //END: processBidResponse
 
 /***
  * Pop up an Div to show the bid error that has been returned
  * @param {String} msg
  */
 function showBidError(msg) {
 	$('msgErrorText').innerHTML = msg;
	toggleBox('errorDiv', 1);
 }
 

 function processBidResponseError (transport) {
 	 logToWindow("processBidResponseError: " + transport.status);
 } //END: processBidResponseError
  
 function processBidException (Req, Ex1) {
	logToWindow("processBidException: " + Ex1);
 } //END: processBidException
 