var holderOptions = new Object;

holderOptions['diamond'] = 'without with(+7.50)';
holderOptions['trianglelarge'] = 'without with(+9.00)';
holderOptions['bottle'] = 'without with(+6.50)';
holderOptions['round'] = 'without with(+9.00)';
holderOptions['roundhappyholi'] = 'without with(+6.50)';
holderOptions['ripplesmall'] = 'without with(+5.50)';
holderOptions['ripplemedium'] = 'without with(+6.50)';
holderOptions['flattaperlarge'] = 'without with(+6.50)';
holderOptions['squaretapermed'] = 'without with(+9.00)';
holderOptions['roundlarge'] = 'without with(+9.00)';
holderOptions['glass'] = 'without  with(+5.50)';
holderOptions['teardrop'] = 'without with(+6.50)';
holderOptions['rectanglelarge'] = 'without with(+9.00)';
holderOptions['ripplelarge'] = 'without with(+6.50)';
holderOptions['squaretaperlarge'] = 'without with(+9.00)';
holderOptions['rectangle'] = 'without with(+9.00)';
holderOptions['triangle'] = 'without with(+9.00)';
holderOptions['tree'] = 'without with(+7.50)';
holderOptions['flattapersmall'] = 'without with(+5.50)';


var basePrice = new Object;

basePrice['balllarge'] = 10.00;
basePrice['diamond'] = 14.5;
basePrice['ballsmall'] = 7.5;
basePrice['trianglelarge'] = 23.0;
basePrice['bottle'] = 14.5;
basePrice['round'] = 12.0;
basePrice['roundhappyholi'] = 16.0;
basePrice['ripplesmall'] = 10.5;
basePrice['ripplemedium'] = 14.5;
basePrice['flattaperlarge'] = 16;
basePrice['squaretapermed'] = 5.25;
basePrice['roundlarge'] = 30;
basePrice['glass'] = 7.5;
basePrice['teardrop'] = 14.5;
basePrice['rectanglelarge'] = 23.0;
basePrice['ripplelarge'] = 18.5;
basePrice['squaretaperlarge'] = 6.75;
basePrice['rectangle'] = 12.0;
basePrice['triangle'] = 12.0;
basePrice['pyramidrdsmall'] = 5;
basePrice['tree'] = 14.5;
basePrice['pillar'] = 12;
basePrice['ballmedium'] = 8.5;
basePrice['pyramidmed'] = 6.5;
basePrice['pyramidsmall'] = 4.5;
basePrice['flattapersmall'] = 9.0;

var humanColor = new Object;
humanColor['grassgreen'] = 'grass green';
humanColor['darkgreen'] = 'dark green';
humanColor['blue'] = 'blue';
humanColor['purple'] = 'purple';
humanColor['red'] = 'red';
humanColor['yellow'] = 'yellow';
humanColor['orange'] = 'orange';
humanColor['Bordeaux'] = 'Bordeaux';
humanColor['pink'] = 'pink';
humanColor['brown'] = 'brown';
humanColor['anthracite'] = 'anthracite';
humanColor['grey'] = 'grey';
humanColor['ivory'] = 'ivory';
humanColor['anthraciteivory'] = 'anthracite/ivory';
humanColor['darkgreenivory'] = 'dark green/ivory';
humanColor['blueivory'] = 'blue/ivory';
humanColor['purpleivory'] = 'purple/ivory';
humanColor['redivory'] = 'red/ivory';
humanColor['ivoryyelloworange'] = 'ivory/yellow/orange';
humanColor['ivoryroseBordeaux'] = 'ivory/rose/Bordeaux';
humanColor['ivorygrassgreendarkgreen'] = 'ivory/grass green/dark green';
humanColor['ivorylightblueblue'] = 'ivory/light blue/blue';


function compuColor(str) {
   return str.replace(/\W/g,'');
}

function showZoom() {
   var div = document.getElementById('divZoom');
   var img = document.getElementById('imgZoom');
   var caption = document.getElementById('spanZoomCaption');
   
   var basename = this.src.replace(/^.*\/(\w+)_small(_flat)?.jpg$/, '$1') ;
	var flat = '';
	if (RegExp.$2) {
		var flat = RegExp.$2;
	}
   var bigjpg = 'images/color/' + basename + '_big' + flat + '.jpg';

   img.src = bigjpg;
   
   // alert(newleft);
   var newleft = 70 + this.offsetLeft;
   div.style.left = newleft.toString() + 'px';
   caption.innerHTML = humanColor[basename];
   div.style.display = '';
}

function hideZoom() {
   var div = document.getElementById('divZoom');
   div.style.display = 'none';
}


function respectSelectedColor() {
   var selectColor = document.getElementById('Color');
	var flat = '';
	var ggs = document.getElementById('grassgreensmall');
   if (ggs) {
		if (/_flat/.test(ggs.src)) {
			flat = '_flat';
		}
   }
   var img = document.getElementById('choiceImage');
   img.src = 'images/color/' + compuColor(selectColor.value) + '_small' + flat + '.jpg';
}

function selectThisColor() {
   var basename = this.src.replace(/^.*\/(\w+)_small(_flat)?.jpg$/, '$1') ;
   var selectColor = document.getElementById('Color');
   selectColor.value = humanColor[basename];
   respectSelectedColor();
}

function respectSelectedSize() {
   var selectItem = document.getElementById('Size');
   var selectHolder = document.getElementById('Holder');
   if (selectHolder) {
      var prevVal = selectHolder.value;
      var newVal = '';
      var firstWith = '';
      var kids = selectHolder.getElementsByTagName('option');
      for (var lcv1 = 0; lcv1 < kids.length; lcv1++) {
	 var kid = kids[lcv1];
	 var re = new RegExp(kid.value.replace(/\W/g,'.'), '');
	 kid.style.display = (re.test(holderOptions[selectItem.value])) ? '' : 'none';
	 if (kid.style.display == '') {
	    if (kid.value == prevVal) { newVal = prevVal; }
	    if (! /without/.test(kid.value) ) { firstWith = kid.value; }
	 }
      }
      if (newVal == '') {
	 newVal = firstWith;
      }
      selectHolder.value = newVal;
   }
   computePrice();
}

function computePrice() {
   var selectItem = document.getElementById('Size');
   var selectQty = document.getElementById('vwquantity');
   var selectHolder = document.getElementById('Holder');
   var holderCost = '0';
   if (selectHolder) {
      if ( /\((\+[\d\.]+)\)/.test(selectHolder.value) ) {
	 holderCost = RegExp.$1;
      }
   }
   var price = parseInt(selectQty.value) * (basePrice[selectItem.value] + parseFloat(holderCost) );
   var prettyPrice = price.toString();
   
   if ( /\.\d\d\d/.test(prettyPrice) ) {
      // weird rounding error...  
      prettyPrice = (Math.round(parseFloat(prettyPrice) * 100) / 100).toString();; 
   }

   if ( /\.\d$/.test(prettyPrice) ) {
      prettyPrice += '0'; 
   } else if ( ! /\./.test(prettyPrice) ) {
      prettyPrice += '.00'; 
   }

   document.getElementById('total').innerHTML = prettyPrice;
}


function preloadImages() {
   preload_image_object = new Array();
   // set image url
   image_url = new Array();
   
   image_url[0] = 'images/color/grassgreen_big.jpg';
   image_url[1] = 'images/color/grassgreen_big_flat.jpg';
   image_url[2] = 'images/color/darkgreen_big.jpg';
   image_url[3] = 'images/color/darkgreen_big_flat.jpg';
   image_url[4] = 'images/color/blue_big.jpg';
   image_url[5] = 'images/color/blue_big_flat.jpg';
   image_url[6] = 'images/color/purple_big.jpg';
   image_url[7] = 'images/color/purple_big_flat.jpg';
   image_url[8] = 'images/color/red_big.jpg';
   image_url[9] = 'images/color/red_big_flat.jpg';
   image_url[10] = 'images/color/yellow_big.jpg';
   image_url[11] = 'images/color/yellow_big_flat.jpg';
   image_url[12] = 'images/color/orange_big.jpg';
   image_url[13] = 'images/color/orange_big_flat.jpg';
   image_url[14] = 'images/color/Bordeaux_big.jpg';
   image_url[15] = 'images/color/Bordeaux_big_flat.jpg';
   image_url[16] = 'images/color/pink_big.jpg';
   image_url[17] = 'images/color/pink_big_flat.jpg';
   image_url[18] = 'images/color/brown_big.jpg';
   image_url[19] = 'images/color/brown_big_flat.jpg';
   image_url[20] = 'images/color/anthracite_big.jpg';
   image_url[21] = 'images/color/anthracite_big_flat.jpg';
   image_url[22] = 'images/color/grey_big.jpg';
   image_url[23] = 'images/color/grey_big_flat.jpg';
   image_url[24] = 'images/color/ivory_big.jpg';
   image_url[25] = 'images/color/ivory_big_flat.jpg';
   image_url[26] = 'images/color/anthraciteivory_big.jpg';
   image_url[27] = 'images/color/darkgreenivory_big.jpg';
   image_url[28] = 'images/color/blueivory_big.jpg';
   image_url[29] = 'images/color/purpleivory_big.jpg';
   image_url[30] = 'images/color/redivory_big.jpg';
   
   var i = 0;
   for(i=0; i<=35; i++) {
      preload_image_object[i] = new Image();
      preload_image_object[i].src = image_url[i];
   }
}

