var g_LightboxIdx;

var aryImages = new Array(2);

aryImages[0] = "/images/tick.gif";
aryImages[1] = "/images/lb.gif";
aryImages[2] = "/images/c.gif";


function OnChangePageSize(sUrl ) {
	var sUserId

    sUserId = GetCookie("AlamyUserID")

	if( sUserId == null ) {

		var sCookie = document.cookie;
		document.cookie = "accept=true";

		if ( GetCookie("accept") == null) {
			alert("This feature requires cookies. Please set your browser to accept cookies and reload this page.");
			return false;
		}

	}

	window.parent.window.location.href = sUrl

	return true;
}

function ow(strSrc) {
	var left;
	var top;
	left = (screen.width - 600) / 2;
	top = (screen.height - 600) / 2 - 20;
	window.open(strSrc, "","width=680,height=575,top=" + top + ",left=" + left + ",alwaysRaised=yes,toolbar=0,directories=0,menubar=0,status=0,resizable=yes,location=0,scrollbars=1,copyhistory=0");
}

for (i=0; i < aryImages.length; i++)
{
	var preload = new Image();
	preload.src = aryImages[i];
}

function swap(imgId) {
	var fn;

	fn =  "swapOnTimeout('" + imgId + "','" + document[imgId].src + "')";
	document[imgId].src = aryImages[0];
	setTimeout(fn, 250 );
}


function swapOnTimeout(imgId,imageSrc){
	document[imgId].src = imageSrc;
}

function onServerExecute() {
    window.status = "";
    }
function ServerExecute(href, status) {
    var httprequest = new Image();
    httprequest.onabort = onServerExecute;
    httprequest.onerror = onServerExecute;
    httprequest.onload  = onServerExecute;
    httprequest.src = href;
    window.status = status;
}

function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0])
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

function onSetLicense()
{
    //alert("Updated License Details");
    window.status = "";

}

function onAddToLightbox()
{
    //alert("Added Image To Lightbox");
    window.status = "";

}

function onAddToOrder()
{
    //alert("Added Image To Shopping Cart");
    window.status = "";
}

function onUpdateOrder()
{
    //alert("Updated image in Shopping Cart");
    window.status = "";
}

function AddURLParam( sURL, sName, sValue )
{
	if( sURL.toLowerCase().indexOf(sName.toLowerCase()) == -1)
		return sURL + "&" + sName + "=" + sValue

	return sURL
}

function AddToLightbox( imgname, sLightboxId, sImageId, sCollectionId )
{
    var sUserId;
    var sRetURL;
    var undefined;


    sUserId = GetCookie("AlamyUserID")

	if(imgname != "" )
		swap(imgname);

    if( sUserId == null || sUserId == "undefined" || sUserId == "" )
    {
		var topmostLocation = window.parent.window.location;
		sRetURL = topmostLocation.protocol + "//" + topmostLocation.hostname + topmostLocation.pathname + window.location.search
		if(sRetURL.indexOf("?") == -1)
			sRetURL = sRetURL + "?cmd=add2lb"
		else
			sRetURL = sRetURL + "&cmd=add2lb"

		sRetURL = AddURLParam( sRetURL, "imageid",sImageId )
		sRetURL = AddURLParam( sRetURL, "clbid",sLightboxId )
		sRetURL = AddURLParam( sRetURL, "collid",sCollectionId )
		
		//alert (sRetURL);

		sRetURL = escape(sRetURL);
		
		topmostLocation.href = sLogonURL + "?returnurl=" + sRetURL;
    }
    else
    {
        var shref
        var now = new Date()

        shref = "/add-to-lightbox.asp?imageid=" + sImageId + "&clbid=" + sLightboxId + "&t=" + now.getTime() + "&collid=" + sCollectionId

        var httprequest = new Image();
        httprequest.onabort = onAddToLightbox;
        httprequest.onerror = onAddToLightbox;
        httprequest.onload  = onAddToLightbox;
        httprequest.src = shref

		//window.parent.window.location.href = shref

        window.status = "Adding image to Lightbox...";
    }
}


function AddToLightbox_new( imgname, sLightboxId, sImageId, sCollectionId )
{
    var sUserId;
    var sRetURL;
    var undefined;

    sUserId = GetCookie("AlamyUserID")

	if(imgname != "" )
		swap(imgname);

    if( sUserId == null || sUserId == "undefined" || sUserId == "" )
    {
	var topmostLocation = window.parent.window.location;
	//alert (topmostLocation);
	sRetURL = topmostLocation.protocol + "//" + topmostLocation.hostname + topmostLocation.pathname + window.location.search
	if(sRetURL.indexOf("?") == -1)
		sRetURL = sRetURL + "?cmd=add2lb"
	else
		sRetURL = sRetURL + "&cmd=add2lb"


	sRetURL = AddURLParam( sRetURL, "imageid",sImageId )
	sRetURL = AddURLParam( sRetURL, "clbid",sLightboxId )
	sRetURL = AddURLParam( sRetURL, "collid",sCollectionId )

	sRetURL = escape(sRetURL);

	if (opener==undefined) //if not on image-details pop up page
	{	if (topmostLocation.href.indexOf("crm2/alamy.asp")!=-1)
		{	window.location.href = sLogonURL + "?returnurl=" + window.location + "&cmd=add2lb";
		}
		else
		{	window.location.href = sLogonURL + "?returnurl=" + sRetURL;
		}
	}
	else
	{	if (opener.parent.location.href.indexOf("crm2/alamy.asp")!=-1)
		{	opener.parent.frames.mainFrame.location.href = sLogonURL + "?returnurl=" + opener.location + "&cmd=add2lb";
			window.close();

		}
		else
		{	opener.location.href = sLogonURL + "?returnurl=" + opener.location + "&cmd=add2lb";
			window.close();
		}
	}
    }
    else
    {
        var shref
        var now = new Date()

        shref = "/add-to-lightbox.asp?imageid=" + sImageId + "&clbid=" + sLightboxId + "&t=" + now.getTime() + "&collid=" + sCollectionId

        var httprequest = new Image();
        httprequest.onabort = onAddToLightbox;
        httprequest.onerror = onAddToLightbox;
        httprequest.onload  = onAddToLightbox;
        httprequest.src = shref

		//window.parent.window.location.href = shref

        window.status = "Adding image to Lightbox...";
    }
}

function AddCDToLightbox( imgname, sLightboxId, sCDId )
{

    var sUserId;
    var sRetURL;
    var undefined;

    sUserId = GetCookie("AlamyUserID")

	if(imgname != "" )
		swap(imgname);

    if( sUserId == null || sUserId == "undefined" || sUserId == "" )
    {
		var topmostLocation = window.parent.window.location;
		sRetURL = topmostLocation.protocol + "//" + topmostLocation.hostname + topmostLocation.pathname + window.location.search
		if(sRetURL.indexOf("?") == -1)
			sRetURL = sRetURL + "?cmd=addcd2lb"
		else
			sRetURL = sRetURL + "&cmd=addcd2lb"


		sRetURL = AddURLParam( sRetURL, "cdid",sCDId )
		sRetURL = AddURLParam( sRetURL, "clbid",sLightboxId )


		sRetURL = escape(sRetURL);
		topmostLocation.href = sLogonURL + "?returnurl=" + sRetURL;
    }
    else
    {
        var shref
        var now = new Date()

        shref = "add-CD-to-lightbox.asp?cdid=" + sCDId + "&clbid=" + sLightboxId + "&t=" + now.getTime()

        var httprequest = new Image();
        httprequest.onabort = onAddToLightbox;
        httprequest.onerror = onAddToLightbox;
        httprequest.onload  = onAddToLightbox;
        httprequest.src = shref

		//window.parent.window.location.href = shref

        window.status = "Adding CD to Lightbox...";
    }
}

function AddToBasket( imgname, sImageId, sCollectionId )
{
    var sUserId;
    var sRetURL;
    var undefined;

    sUserId = GetCookie("AlamyUserID")

	if(imgname != "" )
		swap(imgname);

    if( sUserId == null || sUserId == "undefined" || sUserId == "" )
    {
		var topmostLocation = window.parent.window.location;
		sRetURL = topmostLocation.protocol + "//" + topmostLocation.hostname + topmostLocation.pathname + window.location.search
		if(sRetURL.indexOf("?") == -1)
			sRetURL = sRetURL + "?cmd=add2basket"
		else
			sRetURL = sRetURL + "&cmd=add2basket"

		sRetURL = AddURLParam( sRetURL, "imageid", sImageId );
		sRetURL = AddURLParam( sRetURL, "collid", sCollectionId );

	    sRetURL = escape(sRetURL);
        topmostLocation.href = sLogonURL + "?returnurl=" + sRetURL;
    }
    else
    {
        var shref
        var now = new Date()
        var httprequest = new Image();
        var statustext;

        shref = "add-to-basket.asp?imageid=" + sImageId + "&t=" + now.getTime() + "&collid=" + sCollectionId;

        //window.parent.window.location = shref

        httprequest.onabort = onAddToOrder;
        httprequest.onerror = onAddToOrder;
        httprequest.onload  = onAddToOrder;
        statustext = "Adding image to Shopping Cart...";

        httprequest.src = shref;
        window.status = statustext;
    }
}

function AddToBasket_new( imgname, sImageId, sCollectionId )
{
    var sUserId;
    var sRetURL;
    var undefined;

    sUserId = GetCookie("AlamyUserID")

	if(imgname != "" )
		swap(imgname);

    if( sUserId == null || sUserId == "undefined" || sUserId == "" )
    {
	var topmostLocation = window.parent.window.location;
	sRetURL = topmostLocation.protocol + "//" + topmostLocation.hostname + topmostLocation.pathname + window.location.search

	if(sRetURL.indexOf("?") == -1)
		sRetURL = sRetURL + "?cmd=add2basket"
	else
		sRetURL = sRetURL + "&cmd=add2basket"

	sRetURL = AddURLParam( sRetURL, "imageid", sImageId );
	sRetURL = AddURLParam( sRetURL, "collid", sCollectionId );

	sRetURL = escape(sRetURL);

	if (opener==undefined) //if not on image-details pop up page
	{	if (topmostLocation.href.indexOf("crm2/alamy.asp")!=-1)
		{	window.location.href = sLogonURL + "?returnurl=" + window.location + "&cmd=add2basket";
		}
		else
		{	window.location.href = sLogonURL + "?returnurl=" + sRetURL;
		}
	}
	else
	{
		if (opener.parent.location.href.indexOf("crm2/alamy.asp")!=-1)
		{	opener.parent.frames.mainFrame.location.href = sLogonURL + "?returnurl=" + opener.location + "&cmd=add2basket";
			window.close();

		}
		else
		{	opener.location.href = sLogonURL + "?returnurl=" + opener.location + "&cmd=add2basket";
			window.close();
		}
	}
    }
    else
    {
        var shref
        var now = new Date()
        var httprequest = new Image();
        var statustext;

        shref = "add-to-basket.asp?imageid=" + sImageId + "&t=" + now.getTime() + "&collid=" + sCollectionId;

        //window.parent.window.location = shref

        httprequest.onabort = onAddToOrder;
        httprequest.onerror = onAddToOrder;
        httprequest.onload  = onAddToOrder;
        statustext = "Adding image to Shopping Cart...";

        httprequest.src = shref;
        window.status = statustext;
    }
}

function AddCDToBasket( imgname, sCDId )
{
    var sUserId;
    var sRetURL;
    var undefined;

    sUserId = GetCookie("AlamyUserID")

	if(imgname != "" )
		swap(imgname);

    if( sUserId == null || sUserId == "undefined" || sUserId == "" )
    {
		var topmostLocation = window.parent.window.location;
		sRetURL = topmostLocation.protocol + "//" + topmostLocation.hostname + topmostLocation.pathname + window.location.search
		if(sRetURL.indexOf("?") == -1)
			sRetURL = sRetURL + "?cmd=addCD2basket"
		else
			sRetURL = sRetURL + "&cmd=addCD2basket"

		sRetURL = AddURLParam( sRetURL, "cdid", sCDId );

	    sRetURL = escape(sRetURL);
        topmostLocation.href = sLogonURL + "?returnurl=" + sRetURL;
    }
    else
    {
        var shref
        var now = new Date()
        var httprequest = new Image();
        var statustext;

        shref = "add-cd-to-basket.asp?cdid=" + sCDId + "&t=" + now.getTime();

        //window.parent.window.location = shref

        httprequest.onabort = onAddToOrder;
        httprequest.onerror = onAddToOrder;
        httprequest.onload  = onAddToOrder;
        statustext = "Adding to Shopping Cart...";

        httprequest.src = shref;
        window.status = statustext;
    }
}

function SetLicense( sImageId, sLicenseDetails, sOrderItemID,sCollId )
{
    var sUserId;
    var sRetURL;
    var undefined;

    sUserId = GetCookie("AlamyUserID")

    if( sUserId == null || sUserId == "undefined" || sUserId == "" )
    {
        alert("error")
    }
    else
    {
        var shref
        var now = new Date()
        var httprequest = new Image();
        var statustext;


        if( sOrderItemID == undefined )
		{
            sOrderItemID = ""
			httprequest.onabort = onSetLicense;
			httprequest.onerror = onSetLicense;
			httprequest.onload  = onSetLicense;
			statustext = "Saving License Details..";
        }
		else
		{
		//	httprequest.onabort = onAddToOrder;
		//	httprequest.onerror = onAddToOrder;
		//	httprequest.onload  = onAddToOrder;
			statustext = "Adding to Order...";
		}

        shref = "Set-Order-Item-License.asp?imageid=" + sImageId + "&t=" + now.getTime() + "&orderitemid=" + sOrderItemID + "&licenseinfo=" + sLicenseDetails + "&collid=" + sCollId;

        //window.parent.window.location = shref


        httprequest.src = shref;
        window.status = statustext;
    }
}

function SetCurrentLightBoxOnClick( idx )
{
	g_LightboxIdx = idx;
	return true;
}

function SetCurrentLightBoxFromSelect( selLightboxes )
{
	var sLightboxId = selLightboxes[selLightboxes.selectedIndex].value

	if ("NOLIGHTBOX"==sLightboxId){
		alert ("Please select a lightbox");
		selLightboxes.selectedIndex = g_LightboxIdx;
		return false;
	}
	else
	{
		SetCurrentLightBox(sLightboxId);
	}

	return true;
}

function SetCurrentLightBox( sLightboxId )
{
    var sUserId;
    var sRetURL;
    var undefined;

    sUserId = GetCookie("AlamyUserID")

    if( sUserId == null || sUserId == "undefined" || sUserId == "" )
    {
        // Should Never Happen ??
    }
    else
    {
        var shref
        var now = new Date()

        shref = "set-current-lightbox.asp?clbid=" + sLightboxId + "&t=" + now.getTime()

        var httprequest = new Image();
        httprequest.src = shref
    }
}

function SaveLightboxNotes( sImageId, sLightboxId, sNotes )
{
    var sUserId;
    var sRetURL;
    var undefined;

    sUserId = GetCookie("AlamyUserID")

    if( sUserId == null || sUserId == "undefined" || sUserId == "" )
    {
        // Should Never Happen. User will have logged in /registered
        // to get to his lightbox
    }
    else
    {

        var shref
        var now = new Date()

        shref = "my-lightbox-notes.asp?imageid=" + sImageId + "&clbid=" + sLightboxId + "&tanotes=" + escape(sNotes) + "&t=" + now.getTime()


        var httprequest = new Image();
        httprequest.src = shref
    }
}

function SaveLightboxCDNotes( sCDId, sLightboxId, sNotes )
{
    var sUserId;
    var sRetURL;
    var undefined;

    sUserId = GetCookie("AlamyUserID")

    if( sUserId == null || sUserId == "undefined" || sUserId == "" )
    {
        // Should Never Happen. User will have logged in /registered
        // to get to his lightbox
    }
    else
    {

        var shref
        var now = new Date()

        shref = "my-lightbox-CD-notes.asp?cdid=" + sCDId + "&clbid=" + sLightboxId + "&tanotes=" + escape(sNotes) + "&t=" + now.getTime()

        var httprequest = new Image();
        httprequest.src = shref
    }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);


//speed check code

//imgNum, imgCount, tmStart, tmHtml needed to be set in the asp
var imgNum = -1;
var imgCount = -1;
var tmStart;
var tmHtml = new Date();


var imgLoaded = 0;
var imgFailed = 0;
var rpt_ihits = 0;
var rpt_chits = 0;
var rpt_stm   = 0;

  function imgLd() {
  	imgLoaded++;
  }

  function imgFl() {
  	imgFailed++;
  }


  function sndRpt(status) {

  	//in case if images are loaded b4 html finish (e.g. from cache)
	tmHtml = tmHtml.getTime() - tmStart.getTime();
	tmHtml = (tmHtml > 0? tmHtml : -1);
	status += (tmHtml > 0? "" : "(NO_tmHtml)")

    	//report
	tmFull = new Date();

	rep = "chkspeed.asp";
	rep += location.search + "&";
	rep += "imgNum=" +  escape(imgNum) + "&";
	rep += "tmHtml=" + tmHtml  + "&";
	rep += "tmFull=" +  (tmFull.getTime() - tmStart.getTime()) + "&";
	rep += "ClientTime=" +  escape(tmFull.getFullYear() +"/"+ (tmFull.getMonth()+1) +"/"+ tmFull.getDate() +
		" "+ tmFull.getHours() +":"+ tmFull.getMinutes() + ":00") + "&";

	rep += "imgLoaded=" +  imgLoaded + "&";
	rep += "imgFailed=" +  imgFailed + "&";
	rep += "ihits=" + rpt_ihits + "&";
	rep += "chits=" + rpt_chits + "&";
	rep += "stm=" + rpt_stm;

	i = new Image();

	i.src = rep;
  }



