// JavaScript Document

//Image Rollover Code
image_shop = new Image();
image_shop.src = "images/top_nav/shop_o.gif";

image_company = new Image();
image_company.src = "images/top_nav/company_o.gif";

image_community = new Image();
image_community.src = "images/top_nav/community_o.gif";

image_contact = new Image();
image_contact.src = "images/top_nav/contact_o.gif";

var displayed_sku_index;
var displayed_model_index = 0;
var max_skus_per_model = 0;
var current_available_quantity;

function SetDropDownValue( ddl, val )
{
	for (var i = 0; i < ddl.options.length; ++i)
 		if (ddl.options[i].value == val)
    	ddl.selectedIndex = i;
}

function SetRadioValue( radio_div, radio_name, radio_text )
{
		var radios = document.getElementById (radio_div);
		if (radios) {
		  var inputs = radios.getElementsByTagName ('input');
		  if (inputs) {
			for (var i = 0; i < inputs.length; ++i) {
			  if (inputs[i].type == 'radio' && inputs[i].name == radio_name)
				inputs[i].checked = inputs[i].value == radio_text;
			}
		  }
		}
}

function ToggleTextClass(id, classname)
{
	document.getElementById(id).className = classname;
}

function showModel( id )
{
		ToggleTextClass('lnk_mdl_' + displayed_model_index, "subhead");
		displayed_model_index = id;
		models[displayed_model_index].show();
		ToggleTextClass('lnk_mdl_' + displayed_model_index, "highlighted");
}

function DisplaySKU( id )
{
		displayed_sku_index = id;
		models[displayed_model_index].SKUs[displayed_sku_index].show();
}

function NextImage()
{
	models[displayed_model_index].SKUs[displayed_sku_index].nextImage();
}

function PrevImage()
{
		models[displayed_model_index].SKUs[displayed_sku_index].prevImage();
}

function modelObj(i,n,d)
{
	this.id = i;
	this.name = n;
	this.description = d;
	this.SKUs = new Array();
	
	modelObj.prototype.show = function show()
	{
		if (document.getElementById) 
		{ // DOM3 = IE5, NS6
			document.getElementById('lyr1').innerHTML = 
				"<span class=\"subhead\">" + this.name + "</span>"
				+ "<p>"+ this.description + "</p>";
		}
		else 
		{
			if (document.layers) 
			{ // Netscape 4
				document.lyr1.innerHTML = 
				"<span class=\"subhead\">" + this.name + "</span>"
				+ "<p>"+ this.description + "</p>";
			}
			else 
			{ // IE 4
				document.all.lyr1.innerHTML = 
				"<span class=\"subhead\">" + this.name + "</span>"
				+ "<p>"+ this.description + "</p>";
			}
		}
		
		//Show swatches
		for(iSKU = 0; iSKU < this.SKUs.length; iSKU++)
			document.images["swatch_" + iSKU].src = this.SKUs[iSKU].swatchURL;			
		
		if(this.SKUs.length > max_skus_per_model)
			max_skus_per_model = this.SKUs.length;
		displayed_sku_index = 0;
		this.SKUs[displayed_sku_index].show();
		initScrollLayer();
	}
	
} 

function SKUObj( s, cn, p, q )
{
	this.SKU = s;
	this.images = new Array();
	this.displayedImage;
	this.swatchURL = "../admin/uploads/colors/" + (this.SKU.substring(6)) + ".gif";
	this.colorName = cn;
	this.price = p;
	this.availQuantity = q;
	
	SKUObj.prototype.show = function show()
	{
		current_available_quantity = this.availQuantity;
		
		this.showImage(0);
		
		description = this.colorName + " " + models[displayed_model_index].name;
		
		if (document.getElementById) 
		{ // DOM3 = IE5, NS6
			document.getElementById('cart_color_model').innerHTML = description;
			document.getElementById('cart_price').innerHTML = "$" + this.price;	
			if(this.availQuantity < 1)
			{
				document.getElementById('sold_out_watermark').style.display = "inline";
				document.f_cart.Submit.disabled = true;
				document.f_cart.f_quantity.disabled = true;
			}
			else
			{
				document.getElementById('sold_out_watermark').style.display = "none";
				document.f_cart.Submit.disabled = false;
				document.f_cart.f_quantity.disabled = false;
			}
		}
		else 
		{
			if (document.layers) 
			{ // Netscape 4
				document.cart_color_model.innerHTML = description;
				document.cart_price.innerHTML = "$" + this.price;
				if(this.availQuantity < 1)
				{
					document.sold_out_watermark.style.display = "inline";
					document.f_cart.Submit.disabled = true;
					document.f_cart.f_quantity.disabled = true;
				}
				else
				{
					document.sold_out_watermark.style.display = "none";
					document.f_cart.Submit.disabled = false;
					document.f_cart.f_quantity.disabled = false;
				}
			}
			else 
			{ // IE 4
				document.all.cart_color_model.innerHTML = description;
				document.all.cart_price.innerHTML = "$" + this.price;
				if(this.availQuantity < 1)
				{
					document.all.sold_out_watermark.style.display = "inline";
					document.f_cart.Submit.disabled = true;
					document.f_cart.f_quantity.disabled = true;
				}
				else
				{
					document.all.sold_out_watermark.style.display = "none";
					document.f_cart.Submit.disabled = false;
					document.f_cart.f_quantity.disabled = false;
				}
			}
		}
		
		document.f_cart.f_sku.value = this.SKU;
		document.f_cart.f_description.value = description;
	}
	
	SKUObj.prototype.prevImage = function prevImage()
	{
		if(this.displayedImage == 0)
			this.showImage(this.images.length-1);
		else
			this.showImage(this.displayedImage-1);
	}
	
	SKUObj.prototype.nextImage = function nextImage()
	{
		if(this.displayedImage >= this.images.length-1)
			this.showImage(0);
		else
			this.showImage(this.displayedImage+1);
	}
	
	SKUObj.prototype.showImage = function showImage( id )
	{
		image_url = this.images[id];
		if (document.getElementById) 
		{ // DOM3 = IE5, NS6
			document.getElementById('product_image').innerHTML = 
				"<img src=\""+ image_url +"\" width=\"200\" height=\"200\" vspace=\"10\">";
		}
		else 
		{
			if (document.layers) 
			{ // Netscape 4
				document.product_image.innerHTML = 
					"<img src=\""+ image_url +"\" width=\"200\" height=\"200\" vspace=\"10\">";
			}
			else 
			{ // IE 4
				document.all.product_image.innerHTML = 
				"<img src=\""+ image_url +"\" width=\"200\" height=\"200\" vspace=\"10\">";
			}
		}
		this.displayedImage = id;
	} //end showImage
}

/*************************************************************************
  This code is from Dynamic Web Coding at www.dyn-web.com
  Copyright 2001-4 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

function initScrollLayer() {
  // arguments: id of layer containing scrolling layers (clipped layer), id of layer to scroll, 
  // if horizontal scrolling, id of element containing scrolling content (table?)
  var wndo = new dw_scrollObj('wn', 'lyr1', null);
  
  // pass id's of any wndo's that scroll inside tables
  // i.e., if you have 3 (with id's wn1, wn2, wn3): dw_scrollObj.GeckoTableBugFix('wn1', 'wn2', 'wn3');
  dw_scrollObj.GeckoTableBugFix('wn'); 
}

function ValidateQuantity(q, element, default_quantity)
{
		is_integer = !isNaN(parseInt(q));
		q = parseInt(q);
		if(is_integer && q > 0 && q <= current_available_quantity)
				return 1;
		else if (is_integer && q > current_available_quantity)
			alert("Sorry, we only have " + current_available_quantity + " in stock.");
		else		
			alert("Error: Please enter a valid quantity");
		element.value = default_quantity;
		return 0;
}

function ValidateAddToCart()
{
	numerrs = 0;
	if (!ValidateQuantity (document.f_cart.f_quantity.value)) numerrs += 1;
	return (numerrs == 0);
}