var ieBrowser = false; 
var otherBrowser = false;
var postUrl = encodeURI('action=akamai');
/*
 * Stores dynamic script that came from the server. Before execute a method which
 * belongs to this script block an eval call must be done before. 
 */
var mainScript; mainScript='';

var bins;

// Auxiliar variable. This var indicates to other scripts that this code runned successfully.
var processOK = false; 

var g_rating = 0;

function getHTTPRequest()
{
	if(navigator.appName == 'Microsoft Internet Explorer') {
		ieBrowser = true;
	} else {
		otherBrowser = true;
	}

	if(window.XMLHttpRequest && otherBrowser) { // Firefox
	    //otherBrowser = true;
		return new XMLHttpRequest();
	} else  { // IE
		//ieBrowser = true;
	    var parsers = new Array();
		parsers[0] = "MSXML3"; parsers[1] = "Microsoft"; parsers[2] = "MSXML2"; 
		parsers[3] = "MSXML";
		for(var i=0;i < parsers.length;i++) {
			try {
				return new ActiveXObject(parsers[i] + ".XMLHTTP");
			} catch(e) {
			    //ignore the error and try to create a new Request object
			    // with the next parser.
			}
		}
	} 
}

function xmlhttpPost(strURL) {

    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    self.xmlHttpReq = getHTTPRequest();
    
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');


    if (otherBrowser) { 
		self.xmlHttpReq.overrideMimeType('text/xml'); 
		self.xmlHttpReq.setRequestHeader("Content-Length", postUrl);
	} 
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
		    if (self.xmlHttpReq.status == 200) {
	                updatepage(self.xmlHttpReq.responseXML);
		    }
        }
    }
    self.xmlHttpReq.send(postUrl);
}

/**
 * This function generates the URL used by the client script in order to request 
 * the page not cached by Akamai network.
 */
function createurl() {
	var qstring = "";
	var akamai_url = urlToRequest();
	var metatags = document.getElementsByTagName("META");


	for (var i=0;i < metatags.length;i++) {

		if (metatags[i].getAttribute("name") != null 
			&& metatags[i].getAttribute("name").indexOf("Akamai") != -1) {

			qstring += metatags[i].getAttribute("name") + "=" + metatags[i].getAttribute("content");
			if (i+1 != metatags.length) qstring+="&"; 
		}
	}

	return akamai_url + "?" + qstring + '&get=' + Math.random();
}
/**
 * This function defines the URL to request. Depending of the origin
 */
function urlToRequest() {
	var akamai_url_default = "/Akamai/general_functions_xml.asp";
	var akamai_url_prod_details = "/Akamai/general_functions_prod_details.asp";
	
	var currentUrl = document.URL.toLowerCase();
	
	 if (currentUrl.indexOf('_productdetails.asp') != -1) {
		return akamai_url_prod_details;
	} else {
		return akamai_url_default;
	} 
}

function isDebug() {
	if (document.URL.indexOf('debug=true') != -1) {
		return true;
	}
}

/**
 * This routine is called during the request of the page. 
 * It is also in charge to populate any container in the page with the XML sent back 
 * to the user's browser.
 */
function updatepage(str){


	if (ieBrowser) {
		if (!self.xmlHttpReq.documentElement && self.xmlHttpReq.responseStream) {
			self.xmlHttpReq.responseXML.load(self.xmlHttpReq.responseStream);
		} 
	} 


	// Getting the Bin colletcion.
	bins = str.getElementsByTagName('Bin');

	for(var i=0;i < bins.length;i++)
	{
		try
		{
			// The id for each bin correponds to a container in the page
			var bin = document.getElementById(bins[i].getAttribute("id"));
	
			// The XML replied could contain JavaScript. A special treatment to this case
			if (bins[i].getAttribute("id") != null && 
				bins[i].getAttribute("id").indexOf('AkamaiScript') > -1) {
	
				if (ieBrowser) {
					eval(bins[i].firstChild.text);
					if ((bins[i].getAttribute("id").indexOf('AkamaiScriptMain')) > -1)
						mainScript =  bins[i].firstChild.text; 
				} else {
					eval(bins[i].firstChild.nodeValue);
					if ((bins[i].getAttribute("id").indexOf('AkamaiScriptMain')) > -1)
						mainScript =  bins[i].firstChild.nodeValue;
					}
			} else {
				if (ieBrowser) {
					bin.innerHTML = bins[i].firstChild.text;
				} else {
					bin.innerHTML = bins[i].firstChild.nodeValue;
				}
			} // end of dynamic script
		}
		catch(e) {}
	}
	
	processOK = true;
}

// Making the call 
xmlhttpPost(createurl());