// <script type="text/javascript">
//-----------------------------
//Author: Gary Morris
//Date  : 1/23/2005
//-----------------------------
var MIN_DPI = 170;
var NSIZES  = 9;

var paperX   = new Array(NSIZES);
var paperY   = new Array(NSIZES);
var prices   = new Array(NSIZES);
var shipping = new Array(NSIZES);

var goodSizes  = new Array(0);
var nGoodSizes = 0;

var exifVisible = false;
var shopVisible = false;
var adVisible   = true;

//--------------------------------------------------------------
// NOTE: Change NSIZES above if you add/del sizes:
//--------------------------------------------------------------
paperX[0] =  4;   paperY[0] =  6;   prices[0] = 2;   shipping[0] = 1;
paperX[1] =  5;   paperY[1] =  7;   prices[1] = 10;  shipping[1] = 2;
paperX[2] =  8;   paperY[2] = 10;   prices[2] = 20;  shipping[2] = 5;
paperX[3] =  8;   paperY[3] = 12;   prices[3] = 25;  shipping[3] = 5;
paperX[4] = 11;   paperY[4] = 14;   prices[4] = 40;  shipping[4] = 5;
paperX[5] = 12;   paperY[5] = 16;   prices[5] = 45;  shipping[5] = 5;
paperX[6] = 12;   paperY[6] = 18;   prices[6] = 50;  shipping[6] = 5;
paperX[7] = 16;   paperY[7] = 20;   prices[7] = 90;  shipping[7] = 5;
paperX[8] = 16;   paperY[8] = 24;   prices[8] = 95;  shipping[8] = 5;

//-------------------------------------------------------------
// Function to determine if the paper size is valid given
// the size of the original image based on DPI if not cropped
//-------------------------------------------------------------
function isGoodSize(size, shortSide, longSide) {
	var dpi = shortSide / paperX[size];
	if (dpi >= MIN_DPI) return true;
	
	dpi = longSide / paperY[size];
	return (dpi >= MIN_DPI);
}

//-------------------------------------------------------------
// Event handler to change the price of the item in the form
//-------------------------------------------------------------
function changePrice(form) {
    // update the item price/shipping
    var index = goodSizes[ form.os0.options.selectedIndex ];
    
    form.amount.value   = prices[index]; 
    form.shipping.value = shipping[index];
}

//-------------------------------------------------------------
// Function to output the HTML for the form to Add a print
// purchase to the Paypal shopping cart.  User may select the
// size and the price is automatically updated by the above
// event handler (changePrice)
//-------------------------------------------------------------
function writeAddToCartForm(title, fileName, srcWidth, srcHeight, specialInstr) {
  var shortSide, longSide;
  var priceCode = "0";
  var forSale = specialInstr.indexOf("^P0") == -1;
  
  if (srcWidth < srcHeight) {
  	shortSide = srcWidth;
  	longSide  = srcHeight;
  } else {
  	shortSide = srcHeight;
  	longSide  = srcWidth;
  }

  if (forSale) {
    priceCode = specialInstr.substring(specialInstr.indexOf("^P")+2);
     
    if (priceCode == "1") {
		prices[0] = 0.99;   shipping[0] = 0.40;
		prices[1] = 4.95;   shipping[1] = 0.50;
		prices[2] = 9.95;   shipping[2] = 0.50;
		prices[3] = 11.95;  shipping[3] = 0.50; 
		prices[4] = 19.95;  shipping[4] = 2; 
		prices[5] = 24.95;  shipping[5] = 5; 
		prices[6] = 29.95;  shipping[6] = 5; 
		prices[7] = 44.95;  shipping[7] = 5; 
		prices[7] = 49.95;  shipping[7] = 5; 
    }
    
    if (priceCode == "S") {
    	// Art Show - no shipping charges
    	for (i = 0; i< NSIZES; i++) shipping[i] = 0;
    }	

    nGoodSizes = 0;
    for (i = 0; i< NSIZES; i++) {
      // determine which sizes we can print for this image, save index
      if (isGoodSize(i, shortSide, longSide)) {
        goodSizes[nGoodSizes] = i;
        nGoodSizes++;
      }
    }
  }
  
  if (!forSale) {
    document.write('<span class="errmsg">Prints of this image are not available for purchase.</span>');
    if (document.referrer.length > 0) {
      document.write('&nbsp;<a class="navlinks" href="' + document.referrer + '">Back</a>');
    }
 
  } else if (nGoodSizes == 0) {
    document.write('<span class="errmsg">The resolution of this image is insufficient for printing.</span>');
    if (document.referrer.length > 0) {
      document.write('&nbsp;<a class="navlinks" href="' + document.referrer + '">Back</a>');
    }
    
  } else {
    document.write('<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">');
    document.write('<input type="hidden" name="on0" value="size">');
    document.write('<label for="mpa_size"><strong>Print Size:</strong>');
    document.write('<select name="os0" id="mpa_size" onChange="changePrice(this.form)">');

    for (i = 0; i< nGoodSizes; i++) {
      var j = goodSizes[i];
      document.write('<option value="' + paperX[j] + 'x' + paperY[j] + '">');
      document.write(paperX[j] + '&quot; x ' + paperY[j] + '&quot; for $' + prices[j]);
    }
  
    document.write  ('</select></label>');
    document.write  ('<input type="hidden" name="on1" value="finish">');
    document.write  ('<label for="fin">&nbsp;Finish:');
    document.writeln('<select name="os1" id="fin"><option value="glossy">glossy<option value="matte">matte</select></label>');  
    document.write  ('<input type="image" align="absmiddle" border="0" alt="Add to Cart"');
    document.writeln(' hspace=4 name="submit" height="23" width="87" src="../../images/AddToCart87x23.gif">'); 
    document.writeln('<input type="hidden" name="add" value="1">');
    document.writeln('<input type="hidden" name="cmd" value="_cart">');
    document.writeln('<input type="hidden" name="business" value="orders@morrisphotoart.com">');
    document.writeln('<input type="hidden" name="item_name" value="' + title + '">');
    document.writeln('<input type="hidden" name="item_number" value="' + fileName + '">');
    document.writeln('<input type="hidden" name="amount" value="' + prices[goodSizes[0]] + '">');
    document.writeln('<input type="hidden" name="shipping" value="' + shipping[goodSizes[0]] + '">');
    document.writeln('<input type="hidden" name="no_note" value="1">');
    document.writeln('<input type="hidden" name="currency_code" value="USD">');
    document.writeln('<input type="hidden" name="lc" value="US"></form>');
  }
}

function toggleAd() {
  var bannerAd = document.getElementById("bannerad");
  
  if (adVisible) {
    bannerAd.style.visibility = "hidden";
    adVisible = false;
    
  } else {
    bannerAd.style.visibility = "visible";   
    adVisible = true;
  }
  return true;
}

function toggleExif() {
  var block = document.getElementById("exifdata");
  
  if (exifVisible) {
    block.style.left = "-999em";
    exifVisible = false;
    toggleAd();  /* make visible */
    
  } else {
  	/* show exif area, hide others */
  	if (shopVisible) toggleShop();
  	if (adVisible)   toggleAd();
    block.style.left = "auto";  
    exifVisible = true;
  }
  return true;
}

function toggleShop() {
  var block = document.getElementById("shopping");
  
  if (shopVisible) {
    block.style.left = "-999em";
    shopVisible = false;
    toggleAd();  /* make visible */
    
  } else {
  	/* show shopping area, hide others */
  	if (exifVisible) toggleExif();
  	if (adVisible)   toggleAd();
    block.style.left = "auto";   
    shopVisible = true;
  }
  return true;
}

function startShopping() {
  document.write('<table border=0 width=800 cellspacing=0 cellpadding=0 id="shopping" class="shoparea">');
  document.write(' <tr valign=middle>');
  document.write('  <td width=150 height=100 class="notes">Purchase a print of this image by selecting');
  document.write('    a size, a finish, and then click Add to Cart.</td>');
  document.write('  <td width=450 align=center>');
}

function endShopping() {
  document.write('  </td>');
  document.write('  <td width=200 align=left id="buying">');
  document.write('  <a name="shop" href="../../store/prints.html" target="printinfo">Please');
  document.write('     read more about our prints, shipping, and sales policies before ordering</a>');
  document.write('  </td>');
  document.write(' </tr>');
  document.write('</table>');
}

function startBannerAd() {
  writeAd(2);
  document.writeln('<p><div id="bannerad"><table border=0 width=800 cellspacing=0 cellpadding=0><tr><td align=center>');
}

function endBannerAd() {
  document.writeln('</td></tr></table></div>');
}

function startExifArea(srcWidth,srcHeight,shutter,aperture,iso,focalLength,lens,dateTimeStr) {
  document.writeln('<table border=0 width=800 cellspacing=0 cellpadding=0 id="exifdata" class="exifarea">');
  document.writeln('<tr><td>');
  document.writeln('Size: ' + srcWidth + ' x ' + srcHeight + ' pixels');
  if (shutter.length > 0 && aperture.length > 0) {
  	  document.write('<br>Exposure: ' + shutter + ' at f/' + aperture);
	  if (iso.length > 0) {
	  	document.write(', ISO ' + iso);
	  }
	  if (focalLength.length > 0) {
	  	document.write(', ' + focalLength + 'mm');
	  }
	  if (lens.length > focalLength.length) {
	  	document.write(' (range ' + lens + ')');
	  }
  }
  document.writeln('<br>Date: ' + dateTimeStr);
  document.writeln('<a name="exif"></a>');
}

function endExifArea() {
  document.writeln('</td><td align=right valign=top><a class="navlinks" href="#" onClick="toggleExif()">Close</a></td></tr></table>');
}

function writeImage(title,caption,copyright,year,byline,srcWidth,srcHeight,imageURL) {
  var copyrightNotice = copyright;
  
  if (copyrightNotice.length == 0) {
  	if (byline.length == 0) {
      copyrightNotice = 'Copyright ' + year + ' Gary A. Morris, All Rights Reserved.';
    } else {
      copyrightNotice = 'Copyright ' + year + ' ' + byline + ', All Rights Reserved.';
    }
  }
  
  var captionText = caption;
  captionText = captionText.replace(/&lt;/g,   "<");
  captionText = captionText.replace(/&gt;/g,   ">");
  captionText = captionText.replace(/&quot;/g, '"');
  captionText = captionText.replace(/&amp;/g,  '&');

  if (srcWidth < 800) {
    leftFill  = (800 - srcWidth) / 2;
    rightFill = 800 - srcWidth - leftFill;
  } else {
  	leftFill  = 1;
  	rightFill = 1;
  }
  
  document.writeln('<table border=0 width=800 cellspacing=0 cellpadding=0>');
  document.writeln('  <tr><td class="title" colspan=3><a name="image"></a>' + title + '</td></tr>');
  document.writeln('  <tr><td class="caption" colspan=3>' + captionText + '</td></tr>');
  document.writeln('  <tr><td><img src="../../images/trans1x1.gif" width="' + leftFill + '"></td>');
  document.writeln('  <td align=center background="' + imageURL + '" width="' + srcWidth + '" height="' + srcHeight + '">');
  document.writeln('  <img width="' + srcWidth + '" height="' + srcHeight + '" vspace=0 hspace=0 border=0 src="../../images/trans1x1.gif" alt=""></td>');
  document.writeln('  <td><img src="../../images/trans1x1.gif" width="' + rightFill + '"></td></tr>');
  document.writeln('</table>');
  
  document.writeln('<table border=0 width=800 cellspacing=0 cellpadding=0>');
  document.writeln('  <tr valign=top align="center">');
  document.writeln('  <td width=30>&nbsp;</td>');
  document.writeln('  <td width=100 class="navlinks">');
  document.writeln('      <a href="#shop" onClick="toggleShop()"><img src="../../images/cart32x27.gif" ');
  document.writeln('      width=32 height=27 hspace=4 border=0 alt="Purchase" align=absmiddle>Shop</a></td>');
  document.writeln('  <td class="copyright">&copy;' + copyrightNotice + ', All Rights Reserved.</td>');
  document.writeln('  <td width=100 class="navlinks" valign="middle">');
  document.writeln('      <a href="#exif" onClick="toggleExif()">Details</a></td>');
  document.writeln('  <td width=30>&nbsp;</td>');
  document.writeln('  </tr>');
  
  document.writeln('  <tr><td colspan=5 height=4></td></tr>');
  document.writeln('</table>');
}

// </script>