// JavaScript Document
//
/**
 * Copyright (C) 2002-2003, CodeHouse.com. All rights reserved.
 * CodeHouse(TM) is a registered trademark.
 *
 * THIS SOURCE CODE MAY BE USED FREELY PROVIDED THAT
 * IT IS NOT MODIFIED OR DISTRIBUTED, AND IT IS USED
 * ON A PUBLICLY ACCESSIBLE INTERNET WEB SITE.
 * 
 * CodeHouse.com JavaScript Library Module: Cookie Utility Class
 *
 * You can obtain this script at http://www.codehouse.com
 */
function CJL_CookieUtil(name, duration, path, domain, secure)
{
   this.affix = "";
   
   if( duration )
   {   	  
      var date = new Date();
	  var curTime = new Date().getTime();

	  date.setTime(curTime + (1000 * 60 * duration));
	  this.affix = "; expires=" + date.toGMTString();
   }
   
   if( path )
   {
      this.affix += "; path=" + path;
   }
   
   if( domain )
   {
      this.affix += "; domain=" + domain;
   }

   if( secure )
   {
      this.affix += "; secure=" + secure;
   }
   
      
   function getValue()
   {
      var m = document.cookie.match(new RegExp("(" + name + "=[^;]*)(;|$)"));

      return m ? m[1] : null;   
   }
   
   this.cookieExists = function()
   {
      return getValue() ? true : false;
   }
      
   this.expire = function()
   {
      var date = new Date();
	  date.setFullYear(date.getYear() - 1);
	  document.cookie=name + "=noop; expires=" + date.toGMTString(); 
   }
        
   this.setSubValue = function(key, value)
   {
      var ck = getValue();

      if( /[;, ]/.test(value) )
      {
         //Mac IE doesn't support encodeURI
		 value = window.encodeURI ? encodeURI(value) : escape(value);
      }

      
      if( value )
      {
         var attrPair = "@" + key + value;

         if( ck )
         {
             if( new RegExp("@" + key).test(ck) )
	         {
		        document.cookie =
				   ck.replace(new RegExp("@" + key + "[^@;]*"), attrPair) + this.affix;
	         }
	         else
	         {
		        document.cookie =
				   ck.replace(new RegExp("(" + name + "=[^;]*)(;|$)"), "$1" + attrPair) + this.affix;
	         }
         }
         else
         {
	        document.cookie = name + "=" + attrPair + this.affix;
         }
      }
      else
      {      
	     if( new RegExp("@" + key).test(ck) )
	     {
	        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), "") + this.affix;
	     }
      }
   }

      
   this.getSubValue = function(key)
   {
      var ck = getValue();

      if( ck )
      {
         var m = ck.match(new RegExp("@" + key + "([^@;]*)"));

	     if( m )
	     {
	        var value = m[1];

	        if( value )
	        { 
	           //Mac IE doesn't support decodeURI
			   return window.decodeURI ? decodeURI(value) : unescape(value);
	        }
	     }
      }
   }
}
var session = new CJL_CookieUtil("nmasters");

//
// XML startup
//
function importXML()
{
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.async=false;
		xmlDoc.onload = createTable();		
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) createTable()
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}	
	xmlDoc.load("paintings.xml");		
	fullNodes = xmlDoc.documentElement.getElementsByTagName("full");
	thumbNodes = xmlDoc.documentElement.getElementsByTagName("thumb");
	galleryNodes = xmlDoc.documentElement.getElementsByTagName("gallery");
	titleNodes = xmlDoc.documentElement.getElementsByTagName("title");
	mediumNodes = xmlDoc.documentElement.getElementsByTagName("medium");
	paintNodes = xmlDoc.documentElement.getElementsByTagName("paint");
	titleNodes = xmlDoc.documentElement.getElementsByTagName("title");
	descNodes = xmlDoc.documentElement.getElementsByTagName("desc");
	sizeNodes = xmlDoc.documentElement.getElementsByTagName("size");
	priceNodes =    xmlDoc.documentElement.getElementsByTagName("price");
	paintingNodes = xmlDoc.documentElement.getElementsByTagName("painting");
	pNums = new Array(); 

	for (i = 0; i < paintingNodes.length; i++)
	{
  		pNums[i] = paintingNodes[i].getAttribute("pid");

	}
	

}

function createTable()
{
	cTable = 'yes';
}
importXML();
//
// initialize cart
//
function initializeCart()
{
	ncards = new Array();
	prints = new Array();	
	if( !(session.cookieExists()) )
	{
		for (i=0;i<fullNodes.length;i++)
		{
			session.setSubValue("ncards" + i + "=",'0');

			
			session.setSubValue("prints" + i + "=",'0');
		}
		session.setSubValue("ncardsTot=",'0');	
	}
	for (i=0;i<fullNodes.length;i++)
	{
		ncards[i] = session.getSubValue("ncards" + i + "=");
		prints[i] = session.getSubValue("prints" + i + "=");
	}
	ncardsTot = session.getSubValue("ncardsTot=");
	ncardPrice = 20;
	printPrice = 20; 
}
initializeCart();
//
// index.htm slide show
//
	function chgPic(delay)
	{
		cycle = 0;
		var delayMs = delay * 1000;
		imgLine = '<img class="grayborder" src="' + fullNodes[cycle].firstChild.nodeValue + '" />';
		document.getElementById("picDisp").innerHTML = imgLine;
		var intval =self.setInterval("nextPic()",delayMs);		
	}
	
	function nextPic()
	{		
		if (cycle < fullNodes.length - 1) {
			cycle++;
		}
		else {
			cycle = 0
		}
 	imgLine = '<img class="grayborder" src="' + fullNodes[cycle].firstChild.nodeValue + '" />'; 
	document.getElementById("picDisp").innerHTML = imgLine; 
	}
 //
 // wearable art picture & text change
 //
 	function wear(view)
	{
		imgFull='<img src="assets/wearablefull/' + view + '.jpg" width="400" height="500" class="posfull" />';		
		imgThumb ='<img src="assets/wearabledetail/' + view + '.jpg" width="200" height="200" class="posdetail" />';
		wearTxt = ' ';
		if (view == "blueback" || view == "bluefront")
			{
				wearTxt = 'Medusa I - Blue jacket <br><br>This jacket is featured in the 2008 fall issue of <b>"Altered Couture"</b> magazine on page 50. This has become my signature jacket.  This was my first major work of wearable art.  The inspiration came from a picture in an advertisement in the back of a craft magazine.  The jacket was my mothers and I never wore it because it was boring and out of style.  I had the most fun putting together the color scheme from fibers, rovings and novelty yarns in my collection, borrowing some and shopping for the accents.  As long as you have a garment that is at least 75% wool, fleece or felt you can machine felt almost anything on to it.  The possibilities are endless.  I do hand beading at any spot that looks like it may be pulled out.  The beading adds the extra icing to the garment.<br><br>After purchasing a felting machine, this was one of my first projects.  I painted the face on muslin and glued it on to the jacket back and then started felting in all kinds of yarns and novelty yarns.';
			}
		if (view == "burgback" || view == "burgfront")
			{
				wearTxt = 'Burgundy jacket<br><br>This jacket was in my mother\'s closet and I didn\'t know what to do with it.  The overall design was an experiment.  This jacket did not work too well since it was 75% acrylic and 25% wool.  In order to felt with a machine, you need at least 50% wool.  I normally like a theme to my felting, but this design was just random and I let the machine go where it wanted to go.';
			}
		if (view == "creamback" || view == "creamfront")
			{
				wearTxt = 'Cream sweater<br><br>This jacket is featured in the 2008 fall issue of <b>"Altered Couture"</b> magazine on page 49. The design on this sweater was done in stages.  I pinned on the design and would check it out in a mirror to see if I liked the effect.  I used Berroco Hip Hop for the entire sweater.  It was felted on the right side, back side and the right side.<br><br>Leaves and flowers were formed by gently teasing the fat part of the yarn into a leaf shape.  If more yarn was needed strands could be layered on top and felted in.  To make the veins in the leaves, a darker strand was layered on top and felted.  The yarn is naturally variegated and lends itself to flowers and leaves.  This hand knit sweater was too large and was cut apart and resized with a serger sewing machine.  The sleeves were machine felted before stitching them back together.';
			}
		if (view == "medusa2")
			{
				wearTxt = 'Medusa II<br><br>This jacket belongs to my sister-in-law Shirley Masters in Buffalo, NY.  I took the portrait from an advertisement for Samsonite Luggage.  I paint the portrait on muslin and glue it on to the jacket and then I start felting the yarns.  I finish off with hand beading.';
			}
		if (view == "duponi")
			{
				wearTxt = 'Duponi silk jacket<br><br>This is a duponi silk jacket I made of hundreds of little squares of silk ranging in size from ½” to 1 1/2” sewn together in a bargello pattern design of graduated sizes.  After putting the jacket together I then appliquéd silk yoyos to the surface of the jacket.  I would not even venture to think how many hours have gone into this garment.  I completed the outfit with a silk shell and pants.  There is also a purse to compliment the jacket.   Yoyos which were in fashion in the 1940’s have come back into fashion although updated.  They are round circles that have gathered edges that are then pulled tightly to form a puffy circle.  I stuffed them, layered them, beaded and embroidered them.';
			}
		document.getElementById("wearFull").innerHTML = imgFull; 
		document.getElementById("wearThumb").innerHTML = imgThumb; 
		document.getElementById("weartext").innerHTML = wearTxt;
	}
//
// display painting
//
	function display(pNum)
	{	

		if(pNum == null)
		{
			thumbGallery('Automotive','1');
		}
		else
		{
			var gallery = "";
			var k = 1;
			cellNum = 0;
			for (i=0;i<fullNodes.length;i++)
			{
				if(pNums[i] == pNum)
				{

					gallery = galleryNodes[i].firstChild.nodeValue;
					i= 1000;
				}
			}

			for(i=0;i<fullNodes.length;i++)	
			{
				if(galleryNodes[i].firstChild.nodeValue == gallery)
				{			
					if(pNums[i] == pNum)
					{
						cellNum = k;
						i = 1000;
					}
					else
					{
						k++;
					}
				}
			}
			thumbGallery(gallery,cellNum);
		}
	}
//
// populate thumb gallery
//

	function thumbGallery(subject,galleryNum)
	{
		
		thumbCells = new Array();
		var k = 1;
		document.getElementById("galleryTitle").innerHTML = subject + ' Gallery';
		for(i=0;i<fullNodes.length;i++)	
			{
			if(galleryNodes[i].firstChild.nodeValue == subject)
				{			
				imgLine = '<img src="' + thumbNodes[i].firstChild.nodeValue + '" />';
				thumbCells[k]=i
				cellNum = 'cell' + k;
				document.getElementById(cellNum).innerHTML = imgLine;
				k++;
			}
		}
		for(i=k;i<29;i++)
		{
			imgLine = '';
			cellNum = 'cell' + i;
			document.getElementById(cellNum).innerHTML = imgLine;
		}
		
		displayPainting(galleryNum)
	}
//
// display painting and data
//
	function displayPainting(selectNum)
	{
		currentThumb = selectNum;
		m = thumbCells[selectNum];
		if (thumbCells[selectNum] != null)
		{
			imgLine = '<img src="' + fullNodes[m].firstChild.nodeValue + '" class="grayborder" />' ;
			document.getElementById('fullImg').innerHTML = imgLine;
			document.getElementById('title').innerHTML = titleNodes[m].firstChild.nodeValue;
			document.getElementById('size').innerHTML = sizeNodes[m].firstChild.nodeValue;
			document.getElementById('media').innerHTML = paintNodes[m].firstChild.nodeValue + " on " + mediumNodes[m].firstChild.nodeValue;
			document.getElementById('desc').innerHTML = descNodes[m].firstChild.nodeValue;
			document.getElementById('price').innerHTML = priceNodes[m].firstChild.nodeValue;
			if(prints[m] == undefined){prints[m]=0;}
			document.qtyForm.printQty.value = prints[m];
			if(ncards[m] == undefined){ncards[m]=0;}
			document.qtyForm.ncardQty.value = ncards[m];
			if(ncardsTot == undefined){ncardsTot=0;}
			document.qtyForm.ncardTotal.value = ncardsTot;
			if (currentThumb == 1)
			{
				document.getElementById('prevButton').style.display = "none";
			}
			else
			{
				document.getElementById('prevButton').style.display = "inline";
			}
			if (currentThumb == thumbCells.length - 1)
			{
				document.getElementById('nextButton').style.display = "none";
			}
			else
			{
				document.getElementById('nextButton').style.display = "inline";
			}
		}
		if (priceNodes[m].firstChild.nodeValue == 'Inquire for price')
			{
			document.getElementById('priceButton').style.display = "inline";
				mailLink = '<A HREF="mailto:rn1757' + '@' +'aol.com?subject=Inquiry for price&body=Nancy,%0A%0AI am looking for information about buying the original of the painting - ' + titleNodes[m].firstChild.nodeValue + '%0A%0A"> - click here</A>';
				document.getElementById('priceButton').innerHTML = mailLink;
			}
			else
			{
				document.getElementById('priceButton').style.display = "none";
			}
	}
//
// previous/next buttons
//
	function displayNext(direction)
	{

		if(direction == 0)
		{
			if (currentThumb > 1)
			{
				selectNum = parseInt(currentThumb,10) - 1;
				displayPainting(selectNum);
			}
		}
		if(direction == 1)
		{
			if (currentThumb < thumbCells.length - 1)
			{
				selectNum = parseInt(currentThumb,10) + 1;
				displayPainting(selectNum);
			}
		}
	}
//
// mailto
//
	function emailUs()
	{
		mailLink = '<A HREF="mailto:rn1757' + '@' +'aol.com?subject=Inquiry from website">Click here</A>';
		document.getElementById('emailLink').innerHTML = mailLink;
	}
//
// gallery update
//
	function updatePrint()
	{
	printQ = document.qtyForm.printQty.value;
	for (var i = 0; i < printQ.length; i++) {
		var oneChar = printQ.charAt(i)
		if (oneChar < "0" || oneChar > "9") {
			alert("Quantity must be between 1 and 99");
			document.qtyForm.printQty.value = '0';
			return;
		}
	}
	prints[m]=printQ;
	session.setSubValue("prints" + m + "=",prints[m]);	
	}
	
	function updateNcard()
	{
		

		ncardQ = document.qtyForm.ncardQty.value;
		for (var i = 0; i < ncardQ.length; i++) {
			var oneChar = ncardQ.charAt(i)
			if (oneChar < "0" || oneChar > "9") {
				alert("Quantity must be between 1 and 99");
				document.qtyForm.ncardQty.value = '0';
				ncardQ='0'
				i= ncardQ.length;
			}
		}
	ncardsTot = ncardsTot - ncards[m];
	ncards[m]=parseInt(ncardQ,10);

	ncardsTot = ncardsTot + ncards[m];

	document.qtyForm.ncardTotal.value = ncardsTot;
	session.setSubValue("ncardsTot=", ncardsTot);

	session.setSubValue("ncards" + m + "=",ncards[m]);	
	}
//
// cart display
//
	function cart()
{
	var printTot = 0;
	printOrderIndex = new Array();
	ncardOrderIndex = new Array();
	printTxtLine = "";
	ncardTxtLine = "";
	var itemCount = 0;
	var printTotalCost = 0;
	var ncardTotalCost = 0;
//prints
	var txtLine =  '<table class="black" width="800" border="0" cellspacing="0"  align="center" cellpadding="0"><tr class="headerBlack" align="left"><td> Prints</td></tr><tr bgcolor="#cccccc" class="largeblackUnder" align="center"><td width="350">Title</td><td align="right" width="80">Quantity</td><td align="right" width="110">Price Each</td><td align="right" width="110">Total</td></tr><tr><td>&nbsp</td></tr>';
	var m = 0;
	for (i=0;i<fullNodes.length;i++)
		{
			if(parseInt(prints[i],10) > 0)
			{
				printOrderIndex[m] = i;
				
				printTot += parseInt(prints[i],10);
				txtLine += '<tr><td  width="350 align="left"><A href="gallery.htm?' + pNums[i]+ '"> Print - ' + titleNodes[i].firstChild.nodeValue + '</A></td>';
				
				
				txtLine += '<td width="80"  align="right">' + prints[i] +'</td>';
				
				
				m++;
				txtLine += '<td width="110"  align="right">$' + printPrice + '.00</td>';
				var totItem = printPrice * prints[i];
				printTotalCost += totItem;
				txtLine += '<td width="110"  align="right">$' + totItem + '.00</td></tr>';
				itemCount++;
				printTxtLine += '<input type="hidden" name="item_name_'+ itemCount + '" value="Print - ' + titleNodes[i].firstChild.nodeValue + '">';
				printTxtLine += '<input type="hidden" name="quantity_'+ itemCount + '" value="' + prints[i] + '">';
				printTxtLine += '<input type="hidden" name="amount_'+ itemCount + '" value="' + printPrice + '.00">';
			}
		}
		
		if(printTot < 1)
		{
			txtLine += '<tr><td>No prints ordered</td></tr><tr><td>&nbsp</td></tr><tr><td>&nbsp</td></tr>';
			
		}
		else
		{
			txtLine += '<tr><td>&nbsp</td></tr><tr>';
			txtLine += '<tr bgcolor="#ffdd99"><td  width="350 align="left"> Prints    TOTAL</td>';
			txtLine += '<td width="80"  class="over"   align="right">' + printTot + '</td>';
			txtLine += '<td width="110"  align="right">&nbsp</td>';
			txtLine += '<td width="110"  class="over"    align="right">$' + printTotalCost + '.00</td></tr></tr><tr><td>&nbsp</td></tr><tr><td colspan="3" class="normal">To change a quantity, click on the title of the painting and you will return to its gallery page.</td></tr>';
			txtLine += '</table>';
		}
	document.getElementById('printTable').innerHTML = txtLine;


// notecards
	var txtLine =  '<table class="black" width="800" border="0" cellspacing="0"  align="center" cellpadding="0"><tr class="headerBlack" align="left"><td> Notecards</td></tr><tr bgcolor="#cccccc" class="largeblackUnder" align="center"><td width="350">Title</td><td align="right" width="80">Quantity</td><td align="right" width="110"></td><td align="right" width="110">Total</td></tr><tr><td>&nbsp</td></tr>';
	var m = 0;
	for (i=0;i<fullNodes.length;i++)
		{
			if(parseInt(ncards[i],10) > 0)
			{
				ncardOrderIndex[m] = i;
				
				txtLine += '<tr><td  width="350 align="left"><A href="gallery.htm?' + pNums[i]+ '"> Notecard - ' + titleNodes[i].firstChild.nodeValue + '</A></td>';
				
				txtLine += '<td width="80"  align="right">' + ncards[i] +' cards</td>'	
				m++;
				txtLine += '<td width="110"  align="right">&nbsp</td>';
				var totItem = ncardPrice * ncards[i];
				txtLine += '<td width="110"  align="right">&nbsp</td></tr>';
				itemCount++
				ncardTxtLine += '<input type="hidden" name="item_name_'+ itemCount + '" value="Notecard - ' + titleNodes[i].firstChild.nodeValue + '">';
				ncardTxtLine += '<input type="hidden" name="quantity_'+ itemCount + '" value="' + ncards[i] + '">';
				ncardTxtLine += '<input type="hidden" name="amount_'+ itemCount + '" value="0.00">';
			}
		}
		if(ncardsTot < 1 || ncardsTot == undefined)
		{
			txtLine += '<tr><td>No notecards ordered</td></tr><tr><td>&nbsp</td></tr><tr><td>&nbsp</td></tr>';
			
		}
		else
		{
			ncardTotalCost = ncardPrice * Math.ceil(ncardsTot / 8);
			var remainder = 8 - (ncardsTot % 8);
			if(remainder == 8) {remainder = 0};
			txtLine += '<tr><td>&nbsp</td></tr><tr>';
			txtLine += '<tr bgcolor="#ffdd99"><td  width="350 align="left"> Notecards    TOTAL (price is $'+ ncardPrice + '.00 for every 8 cards)</td>';
			txtLine += '<td width="80"  class="over"   align="right">' + ncardsTot + ' cards</td>';
			txtLine += '<td width="110"  align="right">&nbsp</td>';
			txtLine += '<td width="110"  class="over" align="right">$' + ncardTotalCost + '.00</td></tr>';
			itemCount++;
			ncardTxtLine += '<input type="hidden" name="item_name_'+ itemCount + '" value="Notecards Packs of 8">';
			ncardTxtLine += '<input type="hidden" name="quantity_'+ itemCount + '" value="' + Math.ceil(ncardsTot / 8) + '">';
			ncardTxtLine += '<input type="hidden" name="amount_' + itemCount + '" value="' + ncardPrice + '.00">';
			if (remainder != 0)
				{txtLine += '<tr><td>&nbsp</td></tr><tr><td  bgcolor="#ffaaaa" colspan="4">You can choose <span class="largeblack">' + remainder + ' more </span>notecards with no increase in price. Notecards are sold in mix-n-match lots of 8 for $' + ncardPrice + '.00 per lot.  ';
				}
			txtLine += '</td></tr><tr><td>&nbsp</td></tr><tr><td colspan="3" class="normal">To change a quantity, click on the title of the painting and you will return to its gallery page.</td></tr>';
			txtLine += '<tr><td>&nbsp</td></tr></table>';
		}
		document.getElementById('ncardTable').innerHTML = txtLine;
// total
			var txtLine =  '<table class="black" width="800" border="0" cellspacing="0"  align="center" cellpadding="0"><tr class="headerBlack" align="left"><td> TOTAL</td></tr><tr><td>&nbsp</td></tr>';
			txtLine += '<tr bgcolor="#ffdd99"><td  width="350 align="left"> ORDER  TOTAL</td>';
			txtLine += '<td width="80"  align="right">&nbsp</td>';
			txtLine += '<td width="110" align="right">&nbsp</td>';
			OrderTotalCost = ncardTotalCost + printTotalCost;
			txtLine += '<td width="110"  class="over"  align="right">$' + OrderTotalCost + '.00</td></tr>';
			txtLine += '<tr><td>&nbsp</td></tr><tr><td colspan="4"align="right"><input name="printOrder" type="button" class="black" align="right" onClick="submitOrder()" value="Go To Checkout" /></td></tr></table>';
		document.getElementById('totalTable').innerHTML = txtLine;
		
}
//
// submit order
//
	function submitOrder()
	{
		if(OrderTotalCost == 0)
		{
			alert("Your cart is empty.");
			return;
		}
		txtLine = '<form name="paypalForm" action="https://www.paypal.com/cgi-bin/webscr" method="post">';
		txtLine += '<input type="hidden" name="cmd" value="_cart">';
		txtLine += '<input type="hidden" name="upload" value="1">';
		txtLine += '<input type="hidden" name="business" value="rn1757@aol.com">';
		txtLine += printTxtLine;
		txtLine += ncardTxtLine;
		
//		txtLine += '<input type="hidden" name="item_name_1" value="Item Name 1">';
//		txtLine += '<input type="hidden" name="amount_1" value="1.00">';
		
		
		
//		txtLine += '<input type="submit" value="PayPal">';
		txtLine += '</form>';
		
//		txtLine += '<input type="hidden" name="display" value="1">';
		
		document.getElementById('PayPalForm').innerHTML = txtLine;
		document.paypalForm.submit();
		
	}
