/*
function viewSwatch( w, swatchName, menuName )
{
   // construct a menu name
   var field = w.document.getElementById( menuName );

   var v = field.value;

   // extract the material part of the id (ie. everything before the dash)
   var material = v.substring( 0, v.indexOf( '-' ) );
   var directory = swatchName.substring( 0, swatchName.indexOf( '__' ) );
   var graphic = "/DB_IMAGES/" + directory + "/" + material + ".gif";

   // evalit should be a line of js in the following format:
   // document.TIBETAN__CIR_ENVPreview.src = '/DB_IMAGES/TIBETAN/WP_RSBRY.gif';
   var evalit = "w.document." + swatchName + "Preview.src = '" + graphic + "'";

   // evaluate evalit to change the image on the page
   eval( evalit );
}*/
function switchSwatch( w, swatchName, menuName )
{
   // Get the current selection for the specified menu name
   var v = w.document.getElementById( menuName ).value;
   
   // extract the image url from the value
   var url = v.substring( v.indexOf( '/' ) );
   url = url.substring( 0, url.lastIndexOf( '-' ) );
   
   // change the image preview
   // alert( swatchName + "=" + w.document.getElementById( swatchName ).src );
   
   w.document.getElementById( swatchName ).src = url + '.jpg';
}
function selectSwatch( menuName, materialName, swatchName )
{
	materialName += '/';
	if ( window.opener && ! window.opener.closed )
	{
		var menu = window.opener.document.getElementById( menuName );
		if ( menu )
		{
			var i;
			for ( i=0; i < menu.options.length; i++ )
			{
				if ( materialName == menu.options[i].value.substring( 0, materialName.length ) )
				{
					menu.selectedIndex = i;
					switchSwatch( window.opener, swatchName, menuName );
					self.close();
				}
			}
		}
	}
}
function popSwatches( url, name )
{
	window.open( url, name );
}

function fmtPrice( price )
{
   // format the total price into dollars and cents
   var formattedPrice = "$" + ( price / 100 );

   // if total is an even dollar amount ...
   if ( price % 100 == 0 ) {
	  // add zero cents (i.e. $6 becomes $6.00)
	  formattedPrice += ".00";
   }
   // else if the cents are divisible by 10 ...
   else if ( price % 10 == 0 ) {
	  // add a zero (i.e. $6.5 becomes $6.50)
	  formattedPrice += "0";
   }
   return formattedPrice;
}
function runPricing( quantity, fees )
{
   var menuNum;
   var totalPrice = 0;

   // Loop thru all possible menus on the page
   for ( menuNum=0; menuNum<99; ++menuNum )
   {
	  // construct a menu name
	  var m = "menu_choice_" + menuNum;
	  var n = "menu_name_" + menuNum;

	  // if this menu does not exist ...
	  if ( ! document.getElementById( m ) )
      {
		 // quit the loop, we are done
		 break;
	  }

	  // get the value for this menu
	  var e = document.getElementById( m );
	  var v = e.value;

	  // extract the number part of the id (ie. everything after the dash)
	  var x = v.substring( v.lastIndexOf( '-' ) + 1 );

	  var componentPrices = x.split( "/" );
	  

	 var swatchName = document.getElementById( n ).value;
	 var components = swatchName.split( "__" );
	 var mf;

	 for ( i in components )
	 {
		if ( i > 0 )
		{
		   swatchName = mf + "__" + components[i];
		   
		   var componentPrice = parseInt( componentPrices[ i - 1 ] );
		   
         // Update price (set to blank if no charge)
		   document.getElementById( 'price_' + swatchName ).innerHTML = ( componentPrice > 0 ) ? fmtPrice( componentPrice ) : "";

         if ( typeof e.selectedIndex != "undefined" )
         {
				// Extract the textual swatch selection
				var t = e.options[ e.selectedIndex ].text;
				if ( t.indexOf( "-" ) > 0 )
				  t = t.substring( 0, t.lastIndexOf( "-" ) );

				var d = document.getElementById( '_' + swatchName );
				var s = d.innerHTML;
				if ( s.indexOf( "-" ) > 0 )
				  s = s.substring( 0, s.indexOf( "-" ) );

				d.innerHTML = s + " - " + t;
         }

		   // add this price to the total
		   totalPrice += componentPrice;
		}
		else
		{
		   mf = components[i];
		}
	 }
   }

   // Now update the price control on the page
   document.getElementById("unitPrice").innerHTML = fmtPrice( totalPrice );
   if ( document.getElementById("totalPrice") )
   {
	   document.getElementById("totalPrice").innerHTML = fmtPrice( ( totalPrice * quantity ) + ( fees * 100 ) );
   }
}

