//Constants
var TYPE_LIGHTBOX = 1;
var TYPE_SHOPPINGCART = 2;

//global
var previewwindow;

//open cpimages details page
function CPImgOpenDetailWindow(queryString)
{

	var compingImageUrl = "cpimages_details.pop.fwx?" + queryString;
	var windowFeatures = "dependent=0,width=800,height=680,menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0,resizable=1";
	
	//if window already exists close window for clean up
	if (previewwindow != null && !previewwindow.closed) 
	   previewwindow.close();
	   
	previewwindow = window.open(compingImageUrl, 'name', windowFeatures);
	
	//give focus
	if (window.focus) 
		previewwindow.focus()
}
//open new window with width and height
 function CPOpenInNewWindow(url, windowName, width, height )
 {
 	var windowFeatures = "dependent=0,menubar=0,scrollbars=1,location=1,status=0,titlebar=0,toolbar=0,resizable=1,width=" + width + ",height=" + height;
	
	var wnd;
	wnd = window.open(url, windowName, windowFeatures);
	wnd.focus();
 }
 function WindowPopup(url, windowName, width, height )
 {		
	var winl = (screen.width - width) / 2;
    var wint = (screen.height - height) / 2;
  	
    //var windowFeatures = "dependent=0,menubar=0,scrollbars=1,location=1,status=0,titlebar=0,toolbar=0,resizable=1,width=" + width + ",height=" + height;

  	var windowFeatures = 'dependent=yes,height=' + height + ',width='+ width +',top=' + wint +',left=' + winl + ',menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0,resizable=1';
	var wnd;
	wnd = window.open(url, windowName, windowFeatures);
	
 }
 //open new window with width and height
  function CPOpenInScrollNewWindow(url, windowName, width, height )
 {
    var left   = (screen.width  - width)/2;
    var top    = (screen.height - height)/2;
 	var windowFeatures = "dependent=0,menubar=0,scrollbars=1,location=1,status=0,titlebar=1,toolbar=1,resizable=1,width=" + width + ",height=" + height + ",top=" + top + ",left=" + left;
	
	var wnd;
	wnd = window.open(url, windowName, windowFeatures);
	wnd.focus();
 }
 //open in compinf window
 function CPOpenCompingImageWindow(queryString, width, height)
 {
 	var compingImageUrl = "CPImages_CompingImage.fwx?" + queryString;
	var windowFeatures = "dependent=0,width=" + width + ",height=" + height + ",menubar=0,scrollbars=0,status=0,titlebar=0,toolbar=0,resizable=1";
	
	
	var wnd;
	var wndName = "";
	
	// To open all comps in the same window, uncomment the line below
	//wndName = "FWCompingImage";
	
	wnd = window.open(compingImageUrl, wndName, windowFeatures);
	wnd.focus();
 }
 //opens url in opener window
 function LinkToParent(url)
 {
 	window.opener.location.href = url;
	window.close();
 }
//submit passed form object
function Search(objform)
{
	document.getElementById(objform).submit();
}

//create XMLHttpRequest object to comunicate with server Asynchronously (AJAX)
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;
}
/*
	Using XMLHttpRequest update types passed into method.
		
*/
function addTo(url, query, id, type) 
{
  // Obtain an XMLHttpRequest instance
  var req = newXMLHttpRequest();

  // Set the handler function to receive callback notifications
  // from the request object
  var handlerFunction = getReadyStateHandler(req, id, type);
  req.onreadystatechange = handlerFunction;
  
  // Open an HTTP POST connection to the shopping cart servlet.
  // Third parameter specifies request is asynchronous.
  req.open("POST", url, 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(query);
}

/*
 * 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, id, type) 
{
   var debug = true;
  // 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) 
	  {
        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
	  else
	  {
	    //create shopping cart link object
	  	var obj = document.getElementById(id);
		//choose object to update
		switch(type)
		{
			case TYPE_LIGHTBOX:
				//update shopping cart total in link
	 			 if(obj != null)
	  				obj.innerHTML = "Lightbox (" + req.responseText.replace('\r\n', '') + ")";
					//update status message
				window.status = "Image added to lightbox!";
			break;
			case TYPE_SHOPPINGCART:
				//update shopping cart total in link
	 			 if(obj != null)
	  				obj.innerHTML = "Shopping Cart (" + req.responseText.replace('\r\n', '') + ")";
					//update status message
				window.status = "Image added to shopping cart!";
			break;
		}
		//clear message from status
		setTimeout("Clear()", 6000);
	  }
    }
  }
}

//clears windows status display
function Clear()
{
	window.status = "";
}
