/*
 * Functions to be called when an allclassifieds public pages DOM has been loaded.
 */
function loadClassPublic() {
   newBARequest();
}

/*
 * Send an asynchronous request for a new bannerad for display.
 * The response will be an XML document containing the banner ad id and URL.
 */
function newBARequest() {
   var httpRequest = createXmlRequest();

   if (!httpRequest) {
      // Failed to create an XMLHttpRequest instance.
      return false;
   }

   httpRequest.onreadystatechange = function() { updateBADisplay(httpRequest); };
   httpRequest.open('GET', 'badv', true);
   httpRequest.send(null);

}

/*
 * Callback function used to process the response of the new bannerad request.
 * It parses the XML document and inserts the bannerad id and image source into the bannerad anchor.
 *
 * <banner>
 *    <id>2</id>
 *    <imgsrc>ahda/bannerad/ndp4a.gif</imgsrc>
 * </banner>
 */
function updateBADisplay(httpRequest) {
   if (httpRequest.readyState == 4) {
      if (httpRequest.status == 200) {
         // Parse response
         var bannerXmlDoc = httpRequest.responseXML.documentElement;
         var bannerAdId = bannerXmlDoc.getElementsByTagName('id')[0].childNodes[0].nodeValue;
         var bannerAdImgSrc = bannerXmlDoc.getElementsByTagName('imgsrc')[0].childNodes[0].nodeValue;

         // Update href and image source
         var bannerLink = document.getElementById('badlink');
         bannerLink.href = 'bad?baid=' + bannerAdId;

         var bannerImage = document.getElementById('badimg');
         bannerImage.src = bannerAdImgSrc;
         bannerImage.style.display = "inline";
         
         // Update the img src that requests a banner ad view hit count increment
         var bannerViewHitCountImage = document.getElementById('badhc');
         bannerViewHitCountImage.src = 'badhc?baid=' + bannerAdId;
      }
   }
}

/*
 * Add a listener to the onload event. 
 */
function addOnLoadListener(fn)
{
   if (typeof window.addEventListener != 'undefined') {
      window.addEventListener('load', fn, false);
   }
   else if (typeof document.addEventListener != 'undefined') {
      document.addEventListener('load', fn, false);
   }
   else if (typeof window.attachEvent != 'undefined') {
      window.attachEvent('onload', fn);
   }
   else {
      var oldfn = window.onload;
      if (typeof window.onload != 'function') {
         window.onload = fn;
      }
      else {
         window.onload = function() {
            oldfn();
            fn();
         };
      }
   }
}

/*
 * Returns a new HTTP Request for the purpose of requesting an XML document.
 */
function createXmlRequest() {
   var httpRequest;

   if (window.XMLHttpRequest) {
      // Mozilla and Safari
      httpRequest = new XMLHttpRequest();
      if (httpRequest.overrideMimeType) {
         //
         httpRequest.overrideMimeType('text/xml');
      }
   }
   else if (window.ActiveXObject) {
      // IE
      try {
         httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e) {
         try {
            httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch (e) {}
      }
   }
   return httpRequest;
}


/*
 * Disables/Enables Prrice fields in ac0020
 */
function checkCategory(selectbox)
{
	if (selectbox.options[selectbox.selectedIndex].text == 'FREEBIES') {
	 	document.pd_form.price1.disabled=true
		document.pd_form.price2.disabled=true
	} else {
	 	document.pd_form.price1.disabled=false
	 	document.pd_form.price2.disabled=false
	}
}
