function movepic(img_name,img_src) {
	document[img_name].src=img_src;
}

function formHandler(form){
	var URL = document.form.site.options[document.form.site.selectedIndex].value;
	window.location.href = URL;
}

function setBGColor (layerName, layerBGColor)
{
	if (document.getElementById) // Netscape 6 and IE 5+
	{
		var targetElement = document.getElementById(layerName);
		targetElement.style.background = layerBGColor;			
	}
}

function showLayer(layerName, shadowLayerName)
{
	if (document.getElementById) // Netscape 6 and IE 5+
	{
		var targetElement = document.getElementById(layerName);
		var shadowElement = document.getElementById(shadowLayerName);
		targetElement.style.visibility = 'visible';
	}
}					

function hideLayer(layerName)
{
	if (document.getElementById) 
	{
		var targetElement = document.getElementById(layerName);
		targetElement.style.visibility = 'hidden';
	}
}
/*
 * Returns a new XMLHttpRequest object, or false if this browser
 * doesn't support it
 */
function newXMLHttpRequest() {

  var xmlreq = false;

  if (window.XMLHttpRequest) {

    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer

      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e1) {

      // Failed to create required ActiveXObject

      try {
        // Try version supported by older versions
        // of Internet Explorer

        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (e2) {

        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }

  return xmlreq;
}

function hypePage(page) {

	// Obtain an XMLHttpRequest instance
	var req = newXMLHttpRequest();

	// Set the handler function to receive callback notifications
	// from the request object
	var handlerFunction = getReadyStateHandler(req, updateHyped);
	req.onreadystatechange = handlerFunction;

	// Open an HTTP POST connection to the hype servlet.
	// Third parameter specifies request is asynchronous.
	req.open("POST", "hype.php", true);

	// Specify that the body of the request contains form data
	req.setRequestHeader("Content-Type", 
					   "application/x-www-form-urlencoded");

	// Send form encoded data stating that I want to add the 
	// specified item to the cart.
	req.send("action=hype&page="+page);
	
	// Set Cookie
	if (getCookie('hyped') == null) {
		setCookie('hyped', page, 1);
	} else {
		setCookie('hyped', getCookie('hyped') + ',' + page, 1);			
	}
}  

/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its XML response
 * to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
function getReadyStateHandler(req, responseXmlHandler) {

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {
      
      // Check that a successful server response was received
      if (req.status == 200) {

        // Pass the XML payload of the response to the 
        // handler function
        responseXmlHandler(req.responseXML);

      } else {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }
  }
}

function updateHyped(pageXML) {
	// Get the root "cart" element from the document
	var page = pageXML.getElementsByTagName("page")[0];
	// Update the cart's total using the value from the cart document
	document.getElementById("hypetotal").innerHTML = page.getAttribute("total");
	document.getElementById("hypelink").innerHTML = "<img src=\"images/hype/hyped.png\" width=\"109\" height=\"34\" border=\"0\" alt=\"\" />";
}

function setCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)
				 + ";expires="+expire.toGMTString();
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
