  
var pageNo = 1
var curURL = location.href
var sIndex = curURL.indexOf("page=")
//var maxPage = parseInt(TOTAL_IMG/(COL_NUM*ROW_NUM)+1)
var maxPage = checkMaxPage()
var popErr = false

function checkMaxPage() {
	tempPage = TOTAL_IMG/(COL_NUM*ROW_NUM);
	if (tempPage - parseInt(tempPage) > 0)
		tempPage = parseInt(tempPage) + 1;

	return tempPage;
}

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/gallery/prePage.gif' width='13' height='10' border='0'>&nbsp;")
		}
		else {
			document.write("<a href='"+SUBFOLDER+".htm?page="+prePage+"'><img src='../images/gallery/prePage.gif' width='13' height='10' border='0'></a>&nbsp;")
		}
	}
	else
		document.write("<img src='../images/gallery/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/gallery/nextPage.gif' width='13' height='10' border='0'>")
		}
		else {
			document.write("&nbsp;<a href='"+SUBFOLDER+".htm?page="+nextPage+"'><img src='../images/gallery/nextPage.gif' width='13' height='10' border='0'></a>")
		}
	}
	else
		document.write("&nbsp;<img src='../images/gallery/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='top'>")
		  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/"+SUBFOLDER+"/w800/img_"+curImgFile+".jpg\")'><IMG src='../images/gallery/"+SUBFOLDER+"/w137/img_"+curImgFile+".jpg' name='img_"+curImgFile+"' id='img_"+curImgFile+"' title='Click to Enlarge' border='0'></a><BR>")
				  if (curImg <= CAT1_MAX)
					  document.write(CAT1_CAP+"<BR>")
				  else if (curImg <= CAT2_MAX)
					  document.write(CAT2_CAP+"<BR>")
				  else if (curImg <= CAT3_MAX)
					  document.write(CAT3_CAP+"<BR>")
				  else if (curImg <= CAT4_MAX)
					  document.write(CAT4_CAP+"<BR>")
				  else if (curImg <= CAT5_MAX)
					  document.write(CAT5_CAP+"<BR>")
				  else if (curImg <= CAT6_MAX)
					  document.write(CAT6_CAP+"<BR>")
				  else if (curImg <= CAT7_MAX)
					  document.write(CAT7_CAP+"<BR>")
				  document.write("<a href='Javascript:;' onClick='Javascript:openSaveImage(\"../images/gallery/"+SUBFOLDER+"/w800/img_"+curImgFile+".jpg\"); return false' title='Download Image'>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='137' height='1'></td>")
			  }
		  }
		  document.write("</tr><tr><td colspan='8'><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("/w137")
		imgOrURL = temp[0]+temp[1]
	}
	window.win = open (imgOrURL, 'win', 'width=820,height=560,scrollbars=1,resizable=1');
	setTimeout('win.document.execCommand("SaveAs")', 5000);
	//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,scrollbars=1,resizable=1')
}

