function display_shipping() {
	if (document.getElementById('shipcheck').checked) {
	document.getElementById('shipinfo').style.display = '';
	} else {
	document.getElementById('shipinfo').style.display = 'none';
	}
}

function Product(id, inven, sku) {
	this.id = id;
	this.inven = inven;
	this.sku = sku;
	this.p_options = [];
	this.p_inventory = [];
}

function ProductOption(id, name, title) {
	this.id = id;
	this.name = name;
	this.title = title;
	this.optionValues = [];
}

function ProductOptionValue(id, name, price, pricetype) {
	this.id = id;
	this.name = name;
	this.price = price;
	this.pricetype = pricetype;
}

function ProductInventory(id, sku, qty, idAry) {
	this.id = id;
	this.sku = sku;
	this.qty = qty;
	this.idAry = idAry;
}

var noInvenMsg = " - out of stock";
var productIds = [];
var curProduct = new Product();

function renderProductOptions() {
	var optionDiv = document.getElementById('productOptions');
	var str = '';
	for (var id in curProduct.p_options) {
		if (id.substr(0,3)=="id_") { 
			var option = curProduct.p_options[id];
			str += (option.name+":<select id=\""+option.id+"\" title=\""+option.name+"\"");
			str += (" name=\"options["+option.name+"]\" onchange=\"optionChange(this);\">\n");
			str += ("<option value=\"\">"+option.title+"</option>\n");
			for (var opt_id in option.optionValues) {
				if (opt_id.substr(0,3)=="id_") { 
					var val = option.optionValues[opt_id];
					str += ("<option title=\""+val.name+"\" value=\""+val.id+"\">"+val.name+"</option>\n");
				}
			}
			str += ("</select><br />\n\n");
		}
	}
	optionDiv.innerHTML = str;
}

function checkEnable() {
	var disable = false;
	for (var i=0; i<productIds.length; i++) {
		var sel = document.getElementById(productIds[i].substr(3, (productIds[i].length-3)));
		if (sel.disabled == true || sel.value == "") {
			disable = true;
			break;
		}
	}
	document.getElementById('addToCart').disabled = disable;
}

function popProductString() {
	var hidden = document.getElementById('prodIdsStr');
	var str = '';
	for (var i=0; i<productIds.length; i++) {
		var val = document.getElementById(productIds[i].substr(3, (productIds[i].length-3))).value;
		if (i!=0) { str += ","+val; } else { str += val; }
	}
	hidden.value = str;
}

function optionChange(selectObj) {
	var i=0; var is_found = false; foundVal = false;
				
	for (var i=0; i<productIds.length; i++) {
		var next_id = null; var nextObj = null;
		try { 
			next_id = productIds[i+1]; 
			nextObj = document.getElementById(next_id.substr(3,(next_id.length-1))); 
		} catch (e) { ; }
		var id = productIds[i];
		var optionObj = document.getElementById(id);
		
		if (('id_'+selectObj.id) == id) {
			if (selectObj.value != "") {
				if (next_id != null && next_id != undefined) {	
					if (nextObj != undefined) { 
						nextObj.disabled = false; 
						checkInven(nextObj.id);
						nextObj.selectedIndex = 0;
					}
				}
				if (productIds.length >= (i+1)) {
					for (var j=(i+2); j<productIds.length; j++) {
						document.getElementById(productIds[j].substr(3,(productIds[j].length-1))).disabled = true;
					}
				}
			} else {
				for (var j=(i+1); j<productIds.length; j++) {
					var cur_id = productIds[j].substr(3,(productIds[j].length-1));
					document.getElementById(cur_id).selectedIndex = 0;
					document.getElementById(cur_id).disabled = true;
				}
			}
		}
	}
	checkEnable();
}

function checkInven(nextOptionId) {
	var selProdIds = ''; var selProdIdsAry = []; var noInvenStrIds = [];
	// get list of all selected product option ids so far
	for (var i=0; i<productIds.length; i++) { 
		if (productIds[i]!=nextOptionId) {
			var val = document.getElementById(productIds[i].substr(3,(productIds[i].length-3))).value;
			if (val!='') { selProdIds += ("_"+val); selProdIdsAry.push(val); }
		} 
	}

	// iterate through all of the html select options to see if any need to be disabled
	if (nextOptionId) {
		if (isNaN(nextOptionId)) {

		} else {
		
			var objOptions = document.getElementById(nextOptionId).options;
			for (var i=1; i<objOptions.length; i++) {
				var totalInven = 0;
				var markEmpty = false;
				var val = objOptions[i].value;
				
				for (var id in curProduct.p_inventory) {
					if (id.substr(0,6)=="idstr_") { 
						var invenObj = curProduct.p_inventory[id];
						if (id.substr(5,(selProdIds.length+(val.length+1))) == (selProdIds+'_'+val)) {
							totalInven += invenObj.qty;
						}
					}
				}
				if (totalInven == 0 && inv_status == 2) {
					objOptions[i].innerHTML = (objOptions[i].title + noInvenMsg);
					objOptions[i].disabled = true;
				} else {
					objOptions[i].innerHTML = objOptions[i].title;
					objOptions[i].disabled = false;
				}
			}
		}
	}
}

// this function is need to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
}   

function stripe(id) {

    // the flag we'll use to keep track of 
    // whether the current row is odd or even
    var even = false;
  
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "#fff";
    var oddColor = arguments[2] ? arguments[2] : "#eee";
  
    // obtain a reference to the desired table
    // if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { return; }
    
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
    
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {

	    // avoid rows that have a class attribute
        // or backgroundColor style
	    if (!hasClass(trs[i]) && ! trs[i].style.backgroundColor) {
 
         // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
        
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute
            // or backgroundColor style
	        if (! hasClass(mytd) && ! mytd.style.backgroundColor) {
        
		      mytd.style.backgroundColor = even ? evenColor : oddColor;
              
            }
          }
        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
   }
}
