
// if it's carpet purchase unit is 1ft x indicated roll width
// for tiles/planks purchase unit is cartons

function updateSqFt(unitPrice, purchaseUnitUnit, restrictWholePurchaseUnit, productTypeID)
{
	var width = document.attr.selectwidth.options[document.attr.selectwidth.selectedIndex];
	var purchaseUnitSize = parseFloat(document.getElementById('coverage').innerHTML);
	var quantity=document.getElementById('quantity').value;

	if (! isNumeric(quantity))
	{
		alert("Please input a number.");
		quantity = 0;
	}
	
	if (restrictWholePurchaseUnit && quantity < purchaseUnitSize)
		quantity = purchaseUnitSize;
	var rem = (quantity % purchaseUnitSize);
	
	if (restrictWholePurchaseUnit && rem < purchaseUnitSize && rem > 0 )
	{
		if (quantity < purchaseUnitSize)
			quantity = purchaseUnitSize;
		var rem = (quantity % purchaseUnitSize);
		quantity = quantity - rem;
		rem = purchaseUnitSize;
		quantity = quantity + rem;
	}
	var purchaseUnits=quantity/purchaseUnitSize;
	document.getElementById('quantity').value = quantity;
	var totalPrice = quantity * unitPrice;
	totalPrice = totalPrice.toFixed(2);
	document.getElementById('cart-price').innerHTML = "$" + totalPrice;
	document.getElementById('purchase-units').value = purchaseUnits.toFixed(2);
	document.getElementById('product-title').innerHTML = quantity + " sq/ft " + document.getElementById('sample-title').innerHTML + " : " + purchaseUnits + " " + purchaseUnitUnit;

	if (productTypeID == 1)
		updateWidth(unitPrice,purchaseUnitUnit);

	updateShipping(productTypeID);
}

function updateWidth(unitPrice, purchaseUnitUnit)
{
	var width = parseFloat(document.attr.selectwidth.options[document.attr.selectwidth.selectedIndex].value);
	//document.getElementById('coverage').innerHTML = width;
	var sqFt = parseFloat(document.getElementById('quantity').value);
	var linearFt = sqFt/width;
	document.getElementById('purchase-units').value = linearFt.toFixed(2);
	document.getElementById('debuggg').innerHTML = "linearFt " + linearFt + " x width " + width + " = sqFt " +sqFt ;
	//updatePurchaseUnits(unitPrice, purchaseUnitUnit);
}

function updatePurchaseUnits(unitPrice, purchaseUnitUnit)
{
   var width = document.attr.selectwidth.options[document.attr.selectwidth.selectedIndex];
   var purchaseUnitSize = parseFloat(document.getElementById('coverage').value);
   var purchaseUnits = sqFt / purchaseUnitSize;
   var sqFt = document.getElementById('quantity').value;
   document.getElementById('purchase-units').value = purchaseUnits.toFixed(2);
}

function uQuantity(unitPrice, restrictWholePurchaseUnit, productTypeID)
{
	
	var quantity = document.getElementById('quantity').value;
	//document.getElementById('yourPrice').innerHTML="$" + (quantity * unitPrice).toFixed(2);

	if (productTypeID != 1 && productTypeID != 3)
	{
		var purchaseUnitSize = document.getElementById('coverage').innerHTML;
		purchaseUnitSize = parseFloat(purchaseUnitSize);
	}
	
	if (!isNumeric(quantity))
	{
		alert("Please input a number.");
		quantity = 0;
	}
	
	if (restrictWholePurchaseUnit && quantity < purchaseUnitSize)
		quantity = purchaseUnitSize;

	var rem = (quantity % purchaseUnitSize);
	
	if (restrictWholePurchaseUnit && rem < purchaseUnitSize && rem > 0 )
	{
		if (quantity < purchaseUnitSize)
			quantity = purchaseUnitSize;
		var rem = (quantity % purchaseUnitSize);
		quantity = quantity - rem;
		rem = purchaseUnitSize;
		quantity = quantity + rem;
	}
	document.getElementById('quantity').value = quantity;
	if (productTypeID != 1 && productTypeID != 3)
		document.getElementById('purchase-units').value = (quantity / purchaseUnitSize).toFixed(0);
	document.getElementById('yourPrice').innerHTML="$" + (quantity * unitPrice).toFixed(2);
}

function updateShipping(productTypeID)
{
	if (productTypeID != 1)
	{
		var shipping_price = 0;
		var weight = document.getElementById('weight').innerHTML;
		var purchase_units = document.getElementById('purchase-units').value;
		var total_weight=weight*purchase_units;
		if (total_weight < 99 && isNumeric(total_weight))
		{
			showAddToCart();
			shipping_price = 129;
		}
		else if (isNumeric(total_weight))
		{
			showAddToCart();
		
			if (total_weight >=3500)
				hideAddToCart();
			if (total_weight < 3500)
				shipping_price = 359;
			if (total_weight < 3000)
				shipping_price = 299;
			if (total_weight < 2000)
				shipping_price = 259;
			if (total_weight < 1000)
				shipping_price = 199;
			if (total_weight < 500)
				shipping_price = 129;
		}						
		document.getElementById('shipprice').innerHTML = "$" + shipping_price;
		document.getElementById('product-shipping').innerHTML = "$" + shipping_price;
	}
	else // carpet
	{
		var shipping_price = 0;
		var width = document.attr.selectwidth.options[document.attr.selectwidth.selectedIndex].value;
		var purchase_units = document.getElementById('purchase-units').value;
		
		var area = purchase_units * width; // linear ft * width
		
		if (area < 630 && area > 0)
			shipping_price = 149;
		else if (area >= 630)
			shipping_price = 99;
			
		document.getElementById('shipprice').innerHTML = "$" + shipping_price;
		document.getElementById('product-shipping').innerHTML = "$" + shipping_price;
	}
}

function hideAddToCart()
{
	buttons=document.getElementById('shipping-and-buttons');
	notice=document.getElementById('high-weight-notice');
	buttons.className='invis';
	notice.className='';
}

function showAddToCart()
{
	buttons=document.getElementById('shipping-and-buttons');
	notice=document.getElementById('high-weight-notice');
	buttons.className='';
	notice.className='invis';
}


/// ------------ random crap from before follows:

function MM_CheckFlashVersion(reqVerStr,msg){
  with(navigator){
    var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);
    var isWin = (appVersion.toLowerCase().indexOf("win") != -1);
    if (!isIE || !isWin){
      var flashVer = -1;
      if (plugins && plugins.length > 0){
        var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";
        desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;
        if (desc == "") flashVer = -1;
        else{
          var descArr = desc.split(" ");
          var tempArrMajor = descArr[2].split(".");
          var verMajor = tempArrMajor[0];
          var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");
          var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;
          flashVer =  parseFloat(verMajor + "." + verMinor);
        }
      }
      // WebTV has Flash Player 4 or lower -- too low for video
      else if (userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 4.0;

      var verArr = reqVerStr.split(",");
      var reqVer = parseFloat(verArr[0] + "." + verArr[2]);

      if (flashVer < reqVer){
        if (confirm(msg))
          window.location = "http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
      }
    }
  }
}

function isNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}

function startGallery() {
var myGallery = new gallery($('myGallery'), {
timed: true
});
}
window.addEvent('domready', startGallery);

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

      function MM_reloadPage(init) {  //reloads the window if Nav4 resized
        if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
          document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
        else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
      }
      MM_reloadPage(true);


      function flash_visible(type){
        document.getElementById("flashcontent").style.display = type;

      }

// JavaScript Document

// DropDownMenu by Miha Hribar
// http://hribar.info

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function prepareMenu() {
    // first lets make sure the browser understands the DOM methods we will be using
  	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;
  	
  	// lets make sure the element exists
  	if (!document.getElementById("menu")) return false;
  	var menu = document.getElementById("menu");
  	
  	// for each of the li on the root level check if the element has any children
  	// if so append a function that makes the element appear when hovered over
  	var root_li = menu.getElementsByTagName("li");
  	for (var i = 0; i < root_li.length; i++) {
  	    var li = root_li[i];
  	    // search for children
  	    var child_ul = li.getElementsByTagName("ul");
  	    if (child_ul.length >= 1) {
  	        // we have children - append hover function to the parent
  	        li.onmouseover = function () {
  	            if (!this.getElementsByTagName("ul")) return false;
  	            var ul = this.getElementsByTagName("ul");
  	            ul[0].style.display = "block";
  	            return true;
  	        }
  	        li.onmouseout = function () {
  	            if (!this.getElementsByTagName("ul")) return false;
  	            var ul = this.getElementsByTagName("ul");
  	            ul[0].style.display = "none";
  	            return true;
  	        }
  	    }
  	}
  	
  	return true;
}

function setProgressPercent(percent)
{
	document.getElementById("pb").innerHTML="<td bgcolor=\'blue\'><img width='"+(percent*2)+"' height=5 src='images/clear.gif'></td><td width="+(100-percent)+"%></td>";
	document.getElementById("pi").innerHTML=" "+percent+"% done";
}
function insertProgressBar()
{
	document.write("<table border=1 width=200 height=5 cellpadding=0 cellspacing=0><tr id='pb'><td background='black' width=0%></td><td width=100%></td></tr></table><div id='pi'>0% done</div>");
}

addLoadEvent(prepareMenu);



function shipsame(form){

if(form.sameasbilling.checked){

     form.ship_name.value = form.bill_name.value;
     form.ship_company.value = form.bill_company.value;
     form.ship_add1.value = form.bill_add1.value;
     form.ship_add2.value = form.bill_add2.value;
     form.ship_city.value = form.bill_city.value;
     form.ship_zip.value = form.bill_zip.value;
     
     if(form.bill_state.type == "select-one"){
          var bStateIdx = form.bill_state.selectedIndex;
          form.ship_state.options[bStateIdx].selected = true;
     }
     else{
          form.ship_state.value = form.bill_state.value;
     }
}
else{
     form.ship_name.value = "";
     form.ship_company.value = "";
     form.ship_add1.value = "";
     form.ship_add2.value = "";
     form.ship_city.value = "";
     if(form.ship_state.type == "select-one"){
          form.ship_state.options[0].selected = true;
     }
     else{
          form.ship_state.value = "";
     }
     form.ship_city.value = "";
     form.ship_zip.value = "";
}
}
