
// 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(purchaseUnitUnit)
{
	var unitPrice = parseFloat(document.getElementById('u_sale_price').innerHTML);
	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(restrictWholePurchaseUnit, productTypeID)
{
	var unitPrice = parseFloat(document.getElementById('u_sale_price').innerHTML);
	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 MultiDimensionalArray(iRows,iCols)
{
var i;
var j;
   var a = new Array(iRows);
   for (i=0; i < iRows; i++)
   {
       for (j=0; j < iCols; j++)
       a[i] = new Array(iCols);
       {
           a[i][j] = "";
       }
   }
   return(a);
} 

function clickColor(i)
{
	document.getElementById('productColor').selectedIndex = i;
	updateSeriesColor();
} 

function updateSeriesColor()
{
	index=document.getElementById('productColor').options[document.getElementById('productColor').selectedIndex].value;
	cname=document.getElementById('clname');
	price=document.getElementById('u_sale_price');
	rprice=document.getElementById('u_reg_price');
	ccoverage=document.getElementById('coverage');
	img=document.getElementById('img_path');
	specs=document.getElementById('specs');
	ccrumb_title=document.getElementById('crumb_title');
	pproduct_id=document.getElementById('product_id');
	pproductColor=document.getElementById('productColor');
	
	cname.innerHTML=jscolors[index]['color_name'];
	ccrumb_title.innerHTML=jscolors[index]['color_name'];
	price.innerHTML=jscolors[index]['unit_sale_price'];
	rprice.innerHTML=jscolors[index]['unit_regular_price'];
	if (ccoverage != null)
		ccoverage.innerHTML=jscolors[index]['coverage'];
	pproduct_id.value=jscolors[index]['product_id'];
	//productColor.value=jscolors[index]['color_name'];
	img.innerHTML="<img border='0' src='http://c1548112.cdn.cloudfiles.rackspacecloud.com/"+jscolors[index]['image_name']+"'>";
	document.getElementById('img_path').href="images/product_images/large/"+jscolors[index]['image_name'];
	showSpec(index);
}


function showSpec(color)
{
	nummcolors = document.getElementById('numcolors').innerHTML;
		for (var i = 0; i < nummcolors; i++)
		{
			if (i == color)
				document.getElementById(i).style.display="";
			else
				document.getElementById(i).style.display="none";
		}
}

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';
}

/**
 * Concatenates the values of a variable into an easily readable string
 * by Matt Hackett [scriptnode.com]
 * @param {Object} x The variable to debug
 * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
 * @param {String} sep The separator to use between [default: a single space ' ']
 * @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
 */
function print_r(x, max, sep, l) {

	l = l || 0;
	max = max || 10;
	sep = sep || ' ';

	if (l > max) {
		return "[WARNING: Too much recursion]\n";
	}

	var
		i,
		r = '',
		t = typeof x,
		tab = '';

	if (x === null) {
		r += "(null)\n";
	} else if (t == 'object') {

		l++;

		for (i = 0; i < l; i++) {
			tab += sep;
		}

		if (x && x.length) {
			t = 'array';
		}

		r += '(' + t + ") :\n";

		for (i in x) {
			try {
				r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
			} catch(e) {
				return "[ERROR: " + e + "]\n";
			}
		}

	} else {

		if (t == 'string') {
			if (x == '') {
				x = '(empty)';
			}
		}

		r += '(' + t + ') ' + x + "\n";

	}

	return r;

};



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



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
});
}

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;

      }


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>");
}


function shipsame(form){

if(form.ship_to_billing_address.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_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 = "";
}
}
