  /********************/
  var TOTAL_IMG = 61
  var COL_NUM = 4
  var ROW_NUM = 6
  var CAT1_MAX = 12
  var CAT2_MAX = 49
  var CAT3_MAX = 61
  /********************/
  
  var pageNo = 1
  var curURL = location.href
  var sIndex = curURL.indexOf("page=")
  var maxPage = parseInt(TOTAL_IMG/(COL_NUM*ROW_NUM)+1)
  var popErr = false

function checkField() {
  if (galleryform.page.value == "") {
  	alert("Please enter a page number from 1 to "+maxPage+".");
  	pageNo = 1
	return false;
  }
}

  if (sIndex>0) {
  	if (!isNaN(curURL.substr(sIndex+5, 1)))
		pageNo = curURL.substr(sIndex+5, 1)
	else
		popErr = true
  	if (!isNaN(curURL.substr(sIndex+5, 2)))
		pageNo = curURL.substr(sIndex+5, 2)
	else
		popErr=true
  }
  if (pageNo>maxPage) {
  	pageNo = 1
  	popErr=true
  }
  if (popErr)
  	alert("Please enter a page number from 1 to "+maxPage+".")
	
	// check if it is print version
	var isPrint = false;
	if (curURL.indexOf("print") > 0) {
		isPrint = true;
	}

function showPageNo() {
	var prePage = pageNo-1
	var nextPage = parseInt(pageNo)+1
	if (pageNo>1) {
		// not clickable for print version
		if (isPrint) {
			document.write("<img src='../images/global/prePage.gif' width='13' height='10' border='0'>&nbsp;")
		}
		else {
			document.write("<a href='gallery.htm?page="+prePage+"'><img src='../images/global/prePage.gif' width='13' height='10' border='0'></a>&nbsp;")
		}
	}
	else
		document.write("<img src='../images/global/prePage_off.gif' width='13' height='10'>&nbsp;")
	document.write(" Page "+pageNo+" of "+maxPage+" ")
	if (pageNo<parseInt(maxPage)) {
		// not clickable for print version
		if (isPrint) {
			document.write("&nbsp;<img src='../images/global/nextPage.gif' width='13' height='10' border='0'>")
		}
		else {
			document.write("&nbsp;<a href='gallery.htm?page="+nextPage+"'><img src='../images/global/nextPage.gif' width='13' height='10' border='0'></a>")
		}
	}
	else
		document.write("&nbsp;<img src='../images/global/nextPage_off.gif' width='13' height='10'>")
}
function showGallery() {
  for (var i=1; i<=ROW_NUM; i++) {
	  var curRow = (pageNo-1)*COL_NUM*ROW_NUM + (i-1)*COL_NUM
	  if (curRow < TOTAL_IMG) {
		  document.write("<tr valign='bottom'><td><IMG src='../images/global/spacer.gif' width='10' height='1'></td>")
		  for (var j=1; j<=COL_NUM; j++) {
			  var curImg = (pageNo-1)*COL_NUM*ROW_NUM + (i-1)*COL_NUM + j
			  if (curImg<=TOTAL_IMG) {
				  var curImgFile = curImg;
				  if (curImg < 10) {
					curImgFile = "0" + curImgFile;
				  }
				  if (curImg < 100) {
					curImgFile = "0" + curImgFile;
				  }
				  document.write("<td><IMG src='../images/global/spacer.gif' width='8' height='1'></td>")
				  document.write("<td><a href='JavaScript:;' onClick='Javascript:previewImage(\"../images/gallery/w800/img_"+curImgFile+".jpg\")'><IMG src='../images/gallery/w122/img_"+curImgFile+".jpg' name='img_"+curImgFile+"' id='img_"+curImgFile+"' alt='Medium Resolution' width='122' border='0'></a><BR>")
				  if (curImg <= CAT1_MAX)
					  document.write("&nbsp;Drug R&amp;D<BR>")
				  else if (curImg <= CAT2_MAX)
					  document.write("&nbsp;China healthcare<BR>")
				  else if (curImg <= CAT3_MAX)
					  document.write("&nbsp;Consumer products<BR>")
				  document.write("&nbsp;<a href='Javascript:;' onClick='Javascript:openSaveImage(document.img_"+curImgFile+"); return false' title='High Resolution'>Download Image</A></td>")
			  }
			  else {
				  document.write("<td><IMG src='../images/global/spacer.gif' width='8' height='1'></td>")
				  document.write("<td><IMG src='../images/global/spacer.gif' width='122' height='1'></td>")
			  }
		  }
		  document.write("</tr><tr><td colspan='9'><IMG src='../images/global/spacer.gif' width='1' height='12'></td></tr>")
	  }
  }
}

function openSaveImage(imgOrURL) {
	if (typeof imgOrURL == 'object') {
		imgOrURL = imgOrURL.src;
		temp = imgOrURL.split("/w122")
		imgOrURL = temp[0]+temp[1]
	}
	setTimeout('win.document.execCommand("SaveAs")', 5000);
	window.win = open (imgOrURL, 'win', 'width=820,height=560,scrollbars=1,resizable=1');
	//window.open(imgOrURL, 'win', 'width=820,height=560')
}
function saveImageAs(img) {
	document.frameimg.location.href = img;
	setTimeout('waitOrSaveImageAs(100);', 100);
}
function waitOrSaveImageAs(timesRemaining) {
	if (document.frameimg.document.readyState == "complete") {
		document.frameimg.document.execCommand("SaveAs", false);
	} 
	else {
		timesRemaining = timesRemaining - 1;
		if (timesRemaining <= 0) {
			alert("Error opening 'Save As' dialog for this image.");
		} 
		else {
			setTimeout('waitOrSaveImageAs(' + timesRemaining + ');', 100);
		}
	}
}
function previewImage(img) {
	window.open(img, 'Image', 'width=820,height=560')
}

