function ImageControl_OnImageLoadFinish()
{
	BuildZoomObject(startZoom);
}
function Navigate(url)
{
	if(document.fNavigate)
	{
		document.fNavigate.action = url;
		document.fNavigate.submit();
	}
	else
	{
		window.location.href=url;
	}
}
function GotoImage(idx)
{
	if (currentImageNo != idx)
	{
		if(isBook)
		{
				Navigate("/Browse/BookView.aspx?dbid=" + dbid + "&pageno=" + idx);
		}
		else
		{
			var bNaN = isNaN(parseInt(idx));
			idx = parseInt(idx);

			if (bNaN || idx < 1 || idx > cImages)
			{
				alert(numRange + cImages);	// numRange is from resource file via ViewerActiveX.ascx
            }
			else if(1 != idx || 1 != cImages)
			{
				Navigate("/Browse/View.aspx?dbid=" + dbid + "&path=" + imagePath + "."+idx+"&sid=" + sid);
			}
		}
	}
	return false;	
}
function PrevPage()
{
	if(prevUrl.length>0)
	{
		Navigate(prevUrl);
	}
}
function NextPage()
{
	if(nextUrl.length>0)
	{
		Navigate(nextUrl);
	}
}
function HiliteRect(next)
{
  ImageControl.NextHighlight();
}
function SetMode(mode)
{
	if(1==mode)//drag
	{
		document.all("modePan").src = cacheDomain + "/i/content/tools/pan_s.gif";
		document.all("modeMag").src = cacheDomain + "/i/content/tools/magnify.gif";
	}
	else if(2==mode)//select
	{
		document.all("modePan").src = cacheDomain + "/i/content/tools/pan.gif";
		document.all("modeMag").src = cacheDomain + "/i/content/tools/magnify.gif";
	}
	else if(3==mode)//mag
	{
		document.all("modePan").src = cacheDomain + "/i/content/tools/pan.gif";
		document.all("modeMag").src = cacheDomain + "/i/content/tools/magnify_s.gif";
	}
	ImageControl.Mode = mode;
}
function AddZoomItem(oZoom, idx, cText, cValue)
{
	oZoom.options[idx] = new Option( cText, cValue );
}
function RemoveZoomItem(oZoom, iIndex)
{
	oZoom.remove(iIndex);
}
function RemoveZoomItems(oZoom)
{
	while (oZoom.options.length > 0)
	{
	    RemoveZoomItem(oZoom, 0);
	}
}
function BuildZoomObject(cValue)
{
	var oZoom = document.getElementById("zoom");
	RemoveZoomItems(oZoom);
	var z = [cValue,-1,-2,-3,25,50,75,100,125,150,175,200];
	z.sort( function(a,b) { if (a < 0 && b < 0) { return b-a; } else { return a-b; } } );
	var cnt = 0;
	for (i=0; i < z.length; i++)
	{
		if (i > 0)
		{
			if (z[i] == z[i-1])
			{
				continue;
			}
		}
		if (z[i] == -1)
		{
			AddZoomItem(oZoom, cnt, fitImage,  z[i]);	// fitIMage is from resource file via ViewerActiveX.ascx
			cnt++;
		}
		else if (z[i] == -2)
		{
			AddZoomItem(oZoom, cnt, fitWidth, z[i]);	// fitWidth is from resource file via ViewerActiveX.ascx
			cnt++;
		}
		else if (z[i] == -3)
		{
			AddZoomItem(oZoom, cnt, fitHeight,  z[i]);	// fitHeight is from resource file via ViewerActiveX.ascx
			cnt++;
		}
		else
		{
			var szSpacer = "";

			if (z[i] < 10)
			{
				szSpacer = "     ";
			}
			else if (z[i] < 100)
			{
				szSpacer = "    ";
			}
			else if (z[i] < 1000)
			{
				szSpacer = "   ";
			}
			else if (z[i] < 10000)
			{
				szSpacer = "  ";
			}
			else
			{
				szSpacer = " ";
			}
			AddZoomItem(oZoom, cnt, szSpacer + z[i] + "%", z[i]);
			cnt++;
		}
		if (z[i] == cValue)
		{
			oZoom.selectedIndex = i;
			SetZoomDivs(cValue);
		}
	}
}
function SetZoom(value)
{
	SetZoomDivs(value);
	ImageControl.Zoom = value;
	SetCookieZoom();
}
function SetZoomDivs(zoom)
{
	if (zoom >= 200)
	{
		document.getElementById("zoomin").style.display = "none";
		document.getElementById("zoomin_no").style.display = "";
	}
	else
	{
		document.getElementById("zoomin").style.display = "";
		document.getElementById("zoomin_no").style.display = "none";
	}
	if (zoom <= 25)
	{
		document.getElementById("zoomout").style.display = "none";
		document.getElementById("zoomout_no").style.display = "";
	}
	else
	{
		document.getElementById("zoomout").style.display = "";
		document.getElementById("zoomout_no").style.display = "none";
	}
}
function ZoomIn()
{
	var oZoom = document.getElementById("zoom");
	var cItems = oZoom.length;
	var iIndex = oZoom.selectedIndex;
	var zp = ImageControl.Zoom;
	if (iIndex >= 0)
	{
		for (i=iIndex; i < cItems; i++)
		{
			if (oZoom.options[i].value > zp)
			{
				SetZoom(oZoom.options[i].value);
				oZoom.selectedIndex = i;
				break;
  } } }
}
function ZoomOut()
{
	var oZoom = document.getElementById("zoom");
	var cItems = oZoom.length;
	var iIndex = oZoom.selectedIndex;
	var zp = ImageControl.Zoom;

	if (iIndex >= 0)
	{
		for (i=iIndex; i >= 0; i--)
		{
			if (oZoom.options[i].value > 0)
			{
				if (oZoom.options[i].value < zp)
				{
					SetZoom(oZoom.options[i].value);
					oZoom.selectedIndex = i;
					break;
	} } } }
}
function ToggleChrome()
{
	ImageControl.FullScreen = !ImageControl.FullScreen;
}
function SwitchToBasic()
{
	setDictionaryCookie("BROWSE", "MODE", "BASIC", null, "Tue, 02 Jan 2035 00:00:00 UTC");
	var newUrl = window.location.href;
	newUrl = newUrl.replace('&a=1','');
	Navigate(newUrl+'&a=0');
}
function GoToInstall()
{
	Navigate(installUrl);
}
function SetCookieIQ()
{
	setDictionaryCookie("BROWSE", "XIQ", iq, null, "Tue, 02 Jan 2035 00:00:00 UTC");
}
function SetCookieZoom()
{
	window.focus();
	var zp = ImageControl.Zoom;
	setDictionaryCookie("BROWSES", "Z"+dbid ,escape(zp), null, null);
}
function SetCookieIC()
{
	setDictionaryCookie("BROWSE", "XIC", ic, null, "Tue, 02 Jan 2035 00:00:00 UTC");
}
function SetCookieTN()
{
	setDictionaryCookie("BROWSE", "XTN", tn, null, "Tue, 02 Jan 2035 00:00:00 UTC");
}
function Share()
{
	EmailFriend();
}
function SavePopup(show)
{
	var pop;
	var popIframe;
	if(show)
	{
		if('undefined' == typeof SaveThis)
		{
			Save();
		}
		else
		{
			if('undefined' == typeof SaveOwt)
			{
				_findObj("owtLink").style.display = "none";
			}
			pop = _findObj("saveHTML");
			popIframe = _findObj("popIFRAME");
			popIframe.style.width = 517;
			popIframe.style.height = 272;
			popIframe.style.left = pop.style.left = document.body.offsetWidth/2-250;
			popIframe.style.top = pop.style.top  = 185;//document.body.offsetHeight/2-77;
			popIframe.style.display = "block";
			pop.style.display = "block";
			popIframe.style.zIndex = 99;
			pop.style.zIndex = 100;
			_findObj("saveBtn").focus(); // this IE not to loose the iframe
		}
	}
	else
	{
			pop = _findObj("saveHTML");
			popIframe = _findObj("popIFRAME");
			popIframe.style.display = "none";
			popIframe.style.zIndex=0;
			pop.style.display = "none";
	}
}
function OkSave(radio)
{
	SavePopup(false);
	var st = "";
	for(i=0;i<radio.length;i++)
	{
		if(radio[i].checked)
		{
			st = radio[i].value;
			break;
		}
	}
	if(st == "saveOwt")
		SaveOwt();
	else if(st == "savePt")
		SavePt();
	else if(st == "savePtOther")
		SavePtOther();
	else if(st == "savePtStart")
		SavePtStart();
	else if(st == "saveShoebox")
		SaveThis("Image");
	else
		Save();
}
function Save()
{
    var myRes = new Image();
    var successURL = "/handler/ajax/SuccessHandler.ashx?op=Write&dbid=" + dbid + "&pid=" + pid + "&iid=" + iid + "&unq=" + Math.floor(Math.random() * 1999999999) + "&pagetype=Image&successtype=" + SuccessType + "&eventtype=savDisk";
    myRes.src = successURL;
	ImageControl.Save();
}
function PrintPopup(bOrder)
{
	OptionsPopup(false);
	SavePopup(false);
	if('undefined' == typeof OrderPrint)
	{
		Print();
	}
	else
	{
		var pop;
		if(bOrder)
			pop = document.getElementById( "orderPrintDialogHtml" );
		else
			pop = document.getElementById( "printDialogHtml" );
		
		if(null != pop)
		{
			var popIframe = _findObj("popIFRAME");
			var dialogWidth = pop.getAttribute("width");
			var dialogHeight = pop.getAttribute("height");
			var dialogPosX = Math.max(0, ( document.body.clientWidth / 2 ) - ( dialogWidth / 2 ));
			var dialogPosY = Math.max(0, ( document.body.clientHeight / 3 ) - ( dialogHeight / 2 )); 
			
			popIframe.style.left = pop.style.left = Math.round(dialogPosX);
			popIframe.style.top = pop.style.top  = Math.round(dialogPosY);
			pop.style.width = dialogWidth;
			pop.style.height = dialogHeight;
			popIframe.style.width = Math.round(dialogWidth)+2;
			popIframe.style.height = Math.round(dialogHeight)+2;
			pop.style.position = "absolute";
			pop.style.zIndex = 3;
			pop.style.backgroundColor = "#EEEEEE";
			pop.style.border = "1px solid #999999";
			popIframe.style.display = "block";
			pop.style.display = "block";
			popIframe.style.zIndex = 99;
			pop.style.zIndex = 100;
		}
		else
		{
			Print();
		}
	}
}
function printDialogPrint() {   
    var myRes = new Image();
    var successURL = "/handler/ajax/SuccessHandler.ashx?op=Write&dbid=" + dbid + "&pid=" + pid + "&iid=" + iid + "&unq=" + Math.floor(Math.random() * 1999999999) + "&pagetype=Image&successtype=" + SuccessType + "&eventtype=print";
    myRes.src = successURL;
   // var ajaxObj = new AJAXObject();		
	//	ajaxObj.InitiateAsyncRequest("/handler/ajax/SuccessHandler.ashx?op=Write&dbid=<%=dbid%>&unq=<%=DateTime.Now.Ticks.ToString()%>&type=img&pagetype=Image&successtype=Search&eventtype=print");
	printDialogCancel();
	Print();
}
function printDialogCancel()
{
	var popIframe = _findObj("popIFRAME");
	if(null != popIframe)
	{
	popIframe.style.display = "none";
	popIframe.style.zIndex=0;
	}
	var pop = _findObj("orderPrintDialogHtml");
	if(null != pop)
		pop.style.display = "none";
	pop = _findObj("printDialogHtml");
	if(null != pop)
		pop.style.display = "none";
}
function Print()
{
	ImageControl.Print();
}
function PrintBasic()
{
	window.open("/Browse/print_u.aspx?" + printUrlParams,"Print");
}
function printDialogGotoApress(host, action, query) 
{
    var eventtype = "printCanvas";
    if (query == "&ptid=15")
        eventtype = "print";
    
    var myRes = new Image();
    var successURL = "/handler/ajax/SuccessHandler.ashx?op=Write&dbid=" + dbid + "&pid=" + pid + "&iid=" + iid + "&unq=" + Math.floor(Math.random() * 1999999999) + "&pagetype=Image&successtype=" + SuccessType + "&eventtype=" + eventtype + " ";
    myRes.src = successURL;    
    printDialogCancel();	
	var url = "http://" + host + "/index.aspx?action=" + action + "&ppvhash=" + ppv + "&dbid=" + dbid + "&iid=" + iid + "&pid=" + pid + query;
	var newwindow = window.open(url,'_blank','status=0,location=0,toolbar=0,menubar=0,resizable=1,scrollbars=1,titlebar=0');
	if (newwindow != null) { newwindow.focus(); }
}
function Help()
{
	window.open("/Browse/help_x.aspx","Help","toolbar=no,scrollbars=1,resizable=1,menubar=no,location=no,height=375,width=456");
}
function OptionsPopup(show)
{
var popIframe;
var pop;
	if(show)
	{
		printDialogCancel();
		SavePopup(false);
		pop = _findObj("optionsHTML");
		popIframe = _findObj("popIFRAME");
		popIframe.style.width = 522;
		popIframe.style.height = 287;
		popIframe.style.left = pop.style.left = document.body.offsetWidth/2-250;
		popIframe.style.top = pop.style.top  = 150;//document.body.offsetHeight/2-72;
		popIframe.style.display = "block";
		pop.style.display = "block";
		popIframe.style.zIndex = 99;
		pop.style.zIndex = 100;
		_findObj('thumbCheck').checked = tn==1;
		_findObj('contrast0').checked = 0==ic;
		_findObj('contrast1').checked = 1==ic;
	    var quality = _findObj('quality0');
	    if (quality != undefined && quality != null) {
		    _findObj('quality0').checked = 1==iq;
		    _findObj('quality1').checked = 0==iq;
		}
		_findObj("saveBtn").focus(); // this IE not to loose the iframe
	}
	else
	{
			pop = _findObj("optionsHTML");
			popIframe = _findObj("popIFRAME");
			popIframe.style.display = "none";
			popIframe.style.zIndex=0;
			pop.style.display = "none";
	}
}
function ToggleThumb()
{
	tn = _findObj('thumbCheck').checked?1:0;
	SetCookieTN(tn);
	ImageControl.Thumb=tn;
}
function ToggleContrast()
{
	ic = _findObj('contrast0').checked?0:1;
	SetCookieIC(ic);
	if (ic==1)
	{
		ImageControl.ImageContrast = "normal";
	}
	else
	{
		ImageControl.ImageContrast = "autolevel";
	}
}
function ToggleViewer()
{
	var viewer = _findObj('viewer0').checked?0:1;
	if (viewer==1)
	{ //switch to basic
		SwitchToBasic();
	}
}
var origIQ = iq; 
function OkOptions()
{
	OptionsPopup(false);
	ToggleThumb();
	ToggleContrast();

	var quality = _findObj('quality0');
	if (quality != undefined && quality != null) {
	    iq = _findObj('quality0').checked?1:0;
	    SetCookieIQ(iq);
    }
	ToggleViewer();
	if (0 == origIQ && 1 == iq)
	{
		if(isBook)
			Navigate("/Browse/BookView.aspx?dbid=" + dbid + "&iid=" + iid + '&iq=1');
		else
			Navigate("/Browse/View.aspx?dbid=" + dbid + "&iid=" + iid + '&iq=1');
	}
}

