// JavaScript Document


function doNav(categoryid, linkobject) {
	
	new Effect.toggle('subcat' + categoryid, 'blind');
	
	if (!$('subcat' + categoryid).visible()) {
		linkobject.style.background = 'url(images/sidebar-ah2-on.jpg)';	
	}
	else {
		linkobject.style.background = 'url(images/sidebar-ah2-off.jpg)';	
	}
	
	
	var url = 'products.asp';
	var pars = 'categoryid=' + categoryid;
	
	var myAjax = new Ajax.Updater( 'content', url, { 
			method: 'post', 
			parameters: pars,
			onLoading:showLoader,
			onComplete:timeShow
			});
	
	
	return;
}

function doSubNav(categoryid, objectgroupid, linkobject) {
	

	
	var url = 'products.asp';
	var pars = 'categoryid=' + categoryid + '&objectgroupid=' + objectgroupid;
	
	 var myAjax = new Ajax.Updater( 'content', url, { 
			method: 'post', 
			parameters: pars,
			asynchronous:true,
			onLoading:showLoader,
			onComplete:timeShow
			});
	
	
	return;
}

function doProduct(productid) {
	

	
	var url = 'product.asp';
	var pars = 'productid=' + productid;
	
	 var myAjax = new Ajax.Updater( 'content', url, { 
			method: 'post', 
			parameters: pars,
			asynchronous:true,
			onLoading:showLoader,
			onComplete:timeFade
			});
	
	
	return;
}

function showLoader() {

 Element.hide('content');
 Element.show('spinner');

}


function timeShow() {

	setTimeout('Showit()',1000);
	
}

function Showit(divid) {

	Element.hide('chooseprice');
	$$('.prodprice').each(function(e){Element.hide(e)});
	if (divid != 'price') {
		Element.show(divid);
	}
	Element.show('sbutton');
	
	if (divid == 'price') {
		Element.show('chooseprice');
		Element.hide('sbutton');
	}


}

function timeFade() {

	setTimeout('Fadeit()',1000);
	
}

function Fadeit() {

	Element.hide('spinner');
	new Effect.Appear('content', { duration: .2 });
	

}

function statusFade() {

	new Effect.Appear('status', { duration: .3 });
	

}

function addTax(statedd) {
	
	sstateselect = document.forms.checkout.sstate
	
	if (sstateselect.options[sstateselect.selectedIndex].value == 'TN') { 
		var tot = document.forms.checkout.total.value;
		var tottax = Number(tot) + Number(tot * .0925);
		Element.show('tax');
		$('total').innerHTML = '$' + tottax.toFixed(2);
	}
	else {
		
	
		if (statedd.options[statedd.selectedIndex].value == 'TN') {
			
			var tot = document.forms.checkout.total.value;
			var tottax = Number(tot) + Number(tot * .0925);
			Element.show('tax');
			$('total').innerHTML = '$' + tottax.toFixed(2);
			
		}
		else {
			var tot = Number(document.forms.checkout.total.value);
			$('total').innerHTML = '$' + tot.toFixed(2);
			Element.hide('tax');
		}
	}
}


function addtoCart(productid, productprice, productqty, linkobj) {
	

	
	var url = 'cart.asp';
	var pars = 'productid=' + productid + '&productprice=' + productprice + '&productqty=' + productqty + '&cmd=add';
	
	
	 var myAjax = new Ajax.Updater( 'cblock', url, { 
			method: 'post', 
			parameters: pars,
			asynchronous:true,
			onComplete:statusFade
			});
	
	linkobj.innerHTML = 'Add to Quantity (Item in Cart)';
	
	return;
}

	var fadeimages=new Array()
	fadeimages[0]=["../images/fade1.jpg", "", ""]
	fadeimages[2]=["../images/fade2.jpg", "", ""]
	fadeimages[3]=["../images/fade3.jpg", "", ""]



/***********************************************
* Ultimate Fade-In Slideshow (v1.5): © Dynamic Drive (http://www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/
 
var fadeimages=new Array()
//SET IMAGE PATHS. Extend or contract array as needed
fadeimages[0]=["photo1.jpg", "", ""] //plain image syntax
fadeimages[1]=["photo2.jpg", "http://www.cssdrive.com", ""] //image with link syntax
fadeimages[2]=["photo3.jpg", "http://www.javascriptkit.com", "_new"] //image with link and target syntax
 
var fadeimages2=new Array() //2nd array set example. Remove or add more sets as needed.
//SET IMAGE PATHS. Extend or contract array as needed
fadeimages2[0]=["photo1.jpg", "", ""] //plain image syntax
fadeimages2[1]=["photo2.jpg", "http://www.cssdrive.com", ""] //image with link syntax
fadeimages2[2]=["photo3.jpg", "http://www.javascriptkit.com", "_new"] //image with link and target syntax
 
var fadebgcolor="white"
 
////NO need to edit beyond here/////////////
 
var fadearray=new Array() //array to cache fadeshow instances
var fadeclear=new Array() //array to cache corresponding clearinterval pointers
 
var dom=(document.getElementById) //modern dom browsers
var iebrowser=document.all
 
function fadeshow(theimages, fadewidth, fadeheight, borderwidth, delay, pause, displayorder){
this.pausecheck=pause
this.mouseovercheck=0
this.delay=delay
this.degree=10 //initial opacity degree (10%)
this.curimageindex=0
this.nextimageindex=1
fadearray[fadearray.length]=this
this.slideshowid=fadearray.length-1
this.canvasbase="canvas"+this.slideshowid
this.curcanvas=this.canvasbase+"_0"
if (typeof displayorder!="undefined")
theimages.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)
this.theimages=theimages
this.imageborder=parseInt(borderwidth)
this.postimages=new Array() //preload images
for (p=0;p<theimages.length;p++){
this.postimages[p]=new Image()
this.postimages[p].src=theimages[p][0]
}
 
var fadewidth=fadewidth+this.imageborder*2
var fadeheight=fadeheight+this.imageborder*2
 
if (iebrowser&&dom||dom) //if IE5+ or modern browsers (ie: Firefox)
document.write('<div id="master'+this.slideshowid+'" class="imgs" style="position:relative;width:'+fadewidth+'px;height:'+fadeheight+'px;overflow:hidden;"><div id="'+this.canvasbase+'_0" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;-khtml-opacity:10;background-color:'+fadebgcolor+'"></div><div id="'+this.canvasbase+'_1" style="position:absolute;width:'+fadewidth+'px;height:'+fadeheight+'px;top:0;left:0;filter:progid:DXImageTransform.Microsoft.alpha(opacity=10);-moz-opacity:10;background-color:'+fadebgcolor+'"></div></div>')
else
document.write('<div><img name="defaultslide'+this.slideshowid+'" src="'+this.postimages[0].src+'"></div>')
 
if (iebrowser&&dom||dom) //if IE5+ or modern browsers such as Firefox
this.startit()
else{
this.curimageindex++
setInterval("fadearray["+this.slideshowid+"].rotateimage()", this.delay)
}
}

function fadepic(obj){
if (obj.degree<100){
obj.degree+=10
if (obj.tempobj.filters&&obj.tempobj.filters[0]){
if (typeof obj.tempobj.filters[0].opacity=="number") //if IE6+
obj.tempobj.filters[0].opacity=obj.degree
else //else if IE5.5-
obj.tempobj.style.filter="alpha(opacity="+obj.degree+")"
}
else if (obj.tempobj.style.MozOpacity)
obj.tempobj.style.MozOpacity=obj.degree/101
else if (obj.tempobj.style.KhtmlOpacity)
obj.tempobj.style.KhtmlOpacity=obj.degree/100
}
else{
clearInterval(fadeclear[obj.slideshowid])
obj.nextcanvas=(obj.curcanvas==obj.canvasbase+"_0")? obj.canvasbase+"_0" : obj.canvasbase+"_1"
obj.tempobj=iebrowser? iebrowser[obj.nextcanvas] : document.getElementById(obj.nextcanvas)
obj.populateslide(obj.tempobj, obj.nextimageindex)
obj.nextimageindex=(obj.nextimageindex<obj.postimages.length-1)? obj.nextimageindex+1 : 0
setTimeout("fadearray["+obj.slideshowid+"].rotateimage()", obj.delay)
}
}
 
fadeshow.prototype.populateslide=function(picobj, picindex){
var slideHTML=""
if (this.theimages[picindex][1]!="") //if associated link exists for image
slideHTML='<a href="'+this.theimages[picindex][1]+'" target="'+this.theimages[picindex][2]+'">'
slideHTML+='<img src="'+this.postimages[picindex].src+'" border="'+this.imageborder+'px">'
if (this.theimages[picindex][1]!="") //if associated link exists for image
slideHTML+='</a>'
picobj.innerHTML=slideHTML
}
 
 
fadeshow.prototype.rotateimage=function(){
if (this.pausecheck==1) //if pause onMouseover enabled, cache object
var cacheobj=this
if (this.mouseovercheck==1)
setTimeout(function(){cacheobj.rotateimage()}, 100)
else if (iebrowser&&dom||dom){
this.resetit()
var crossobj=this.tempobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
crossobj.style.zIndex++
fadeclear[this.slideshowid]=setInterval("fadepic(fadearray["+this.slideshowid+"])",50)
this.curcanvas=(this.curcanvas==this.canvasbase+"_0")? this.canvasbase+"_1" : this.canvasbase+"_0"
}
else{
var ns4imgobj=document.images['defaultslide'+this.slideshowid]
ns4imgobj.src=this.postimages[this.curimageindex].src
}
this.curimageindex=(this.curimageindex<this.postimages.length-1)? this.curimageindex+1 : 0
}
 
fadeshow.prototype.resetit=function(){
this.degree=10
var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
if (crossobj.filters&&crossobj.filters[0]){
if (typeof crossobj.filters[0].opacity=="number") //if IE6+
crossobj.filters(0).opacity=this.degree
else //else if IE5.5-
crossobj.style.filter="alpha(opacity="+this.degree+")"
}
else if (crossobj.style.MozOpacity)
crossobj.style.MozOpacity=this.degree/101
else if (crossobj.style.KhtmlOpacity)
crossobj.style.KhtmlOpacity=obj.degree/100
}
 
 
fadeshow.prototype.startit=function(){
var crossobj=iebrowser? iebrowser[this.curcanvas] : document.getElementById(this.curcanvas)
this.populateslide(crossobj, this.curimageindex)
if (this.pausecheck==1){ //IF SLIDESHOW SHOULD PAUSE ONMOUSEOVER
var cacheobj=this
var crossobjcontainer=iebrowser? iebrowser["master"+this.slideshowid] : document.getElementById("master"+this.slideshowid)
crossobjcontainer.onmouseover=function(){cacheobj.mouseovercheck=1}
crossobjcontainer.onmouseout=function(){cacheobj.mouseovercheck=0}
}
this.rotateimage()
}



function validateItem() {
    var elem;
    var errs=0;
		
	if (!validatePresent(document.forms.product.productoptionid,  'optErr'))    errs += 1;

    if (errs>1)  alert('Please choose an option for this product.');
    if (errs==1) alert('Please choose an option for this product.');

    return (errs==0);
  };
  
function validateCatalog() {
	var elem;
	var errs=0;
		
	if (!validatePresent(document.forms.catalog.firstname,  'firstnameErr'))    errs += 1;
	if (!validatePresent(document.forms.catalog.lastname,  'lastnameErr'))    errs += 1;
	if (!validatePresent(document.forms.catalog.address1,  'address1Err'))    errs += 1;
	if (!validatePresent(document.forms.catalog.city,  'cityErr'))    errs += 1;
	if (!validatePresent(document.forms.catalog.state,  'stateErr'))    errs += 1;
	if (!validatePresent(document.forms.catalog.zip,  'zipErr'))    errs += 1;
	if (!validatePresent(document.forms.catalog.email,  'emailErr'))    errs += 1;	
	
	if (errs>0)  alert('Please complete all required fields.');
	 
	return (errs==0);
};
  
function validateCheckout() {
    var elem;
    var errs=0;
		
	if (!validatePresent(document.forms.checkout.bname,  'bnameErr'))    errs += 1;
	if (!validatePresent(document.forms.checkout.bemail,  'bemailErr'))    errs += 1;
    if (!validatePresent(document.forms.checkout.baddress1,  'baddress1Err'))    errs += 1;
	if (!validatePresent(document.forms.checkout.bcity,  'bcityErr'))    errs += 1;
	if (!validatePresent(document.forms.checkout.bstate,  'bstateErr'))    errs += 1;
	if (!validatePresent(document.forms.checkout.bzip,  'bzipErr'))    errs += 1;
	if (!validatePresent(document.forms.checkout.bphone,  'bphoneErr'))    errs += 1;
	
	 	 
	if (document.forms.checkout.shipp.checked) {
		
		if (!validatePresent(document.forms.checkout.sname,  'snameErr'))    errs += 1;
		if (!validatePresent(document.forms.checkout.saddress1,  'saddress1Err'))    errs += 1;
		if (!validatePresent(document.forms.checkout.scity,  'scityErr'))    errs += 1;
		if (!validatePresent(document.forms.checkout.sstate,  'sstateErr'))    errs += 1;
		if (!validatePresent(document.forms.checkout.szip,  'szipErr'))    errs += 1;
		if (!validatePresent(document.forms.checkout.sphone,  'sphoneErr'))    errs += 1;
	
	}
	
	
	if (!validatePresent(document.forms.checkout.pname,  'pnameErr'))    errs += 1;
	if (!validatePresent(document.forms.checkout.ptype,  'ptypeErr'))    errs += 1;
	if (!validatePresent(document.forms.checkout.p,  'pErr'))    errs += 1;
	
	
    if (errs>0)  alert('Please complete all required fields.');
	 
    return (errs==0);
  };
// ----------------------------------------------------------------------
// Javascript form validation routines.
// ----------------------------------------------------------------------

var nbsp = 160;    // non-breaking space char
var node_text = 3; // DOM text node-type
var emptyString = /^\s*$/
var glb_vfld;      // retain vfld for timer thread

// -----------------------------------------
//                  trim
// Trim leading/trailing whitespace off string
// -----------------------------------------

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '')
};


// -----------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// -----------------------------------------

function setFocusDelayed()
{
  glb_vfld.focus()
}

function setfocus(vfld)
{
  // save vfld in global variable so value retained when routine exits
  glb_vfld = vfld;
  setTimeout( 'setFocusDelayed()', 100 );
}


// -----------------------------------------
//                  msg
// Display warn/error message in HTML element
// commonCheck routine must have previously been called
// -----------------------------------------

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // setting an empty string can give problems if later set to a 
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could 
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message)) 
    dispmessage = String.fromCharCode(nbsp);    
  else  
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.innerHTML = dispmessage;  
  
};

// -----------------------------------------
//            commonCheck
// Common code for all validation routines to:
// (a) check for older / less-equipped browsers
// (b) check if empty fields are required
// Returns true (validation passed), 
//         false (validation failed) or 
//         proceed (don't know yet)
// -----------------------------------------

var proceed = 2;  

function commonCheck    (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
	
  var elem = document.getElementById(ifld);
	
  if (emptyString.test(vfld.value)) {
    if (reqd) {
      msg(ifld, "error", "<img src=images/valerr.jpg width=17 height=14>");  
      setfocus(vfld);
      return false;
    }
    else {
      msg (ifld, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}

// -----------------------------------------
//            validatePresent
// Validate if something has been entered
// Returns true if so 
// -----------------------------------------

function validatePresent(vfld,   // element to be validated
                         ifld )  // id of element to receive info/error msg
{
  var stat = commonCheck (vfld, ifld, true);
  if (stat != proceed) return stat;

  msg (ifld, "warn", "");  
  return true;
};

// -----------------------------------------
//               validateEmail
// Validate if e-mail address
// Returns true if so (and also if could not be executed because of old browser)
// -----------------------------------------

function validateEmail  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/
  if (!email.test(tfld)) {
    msg (ifld, "error", "ERROR: not a valid e-mail address");
    setfocus(vfld);
    return false;
  }

  var email2 = /^[A-Za-z][\w.-]+@\w[\w.-]+\.[\w.-]*[A-Za-z][A-Za-z]$/
  if (!email2.test(tfld)) 
    msg (ifld, "warn", "Unusual e-mail address - check if correct");
  else
    msg (ifld, "warn", "");
  return true;
};


// -----------------------------------------
//            validateTelnr
// Validate telephone number
// Returns true if so (and also if could not be executed because of old browser)
// Permits spaces, hyphens, brackets and leading +
// -----------------------------------------

function validateTelnr  (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);  // value of field with whitespace trimmed off
  var telnr = /^\+?[0-9 ()-]+[0-9]$/
  if (!telnr.test(tfld)) {
    msg (ifld, "error", "ERROR: not a valid telephone number. Characters permitted are digits, space ()- and leading +");
    setfocus(vfld);
    return false;
  }

  var numdigits = 0;
  for (var j=0; j<tfld.length; j++)
    if (tfld.charAt(j)>='0' && tfld.charAt(j)<='9') numdigits++;

  if (numdigits<6) {
    msg (ifld, "error", "ERROR: " + numdigits + " digits - too short");
    setfocus(vfld);
    return false;
  }

  if (numdigits>14)
    msg (ifld, "warn", numdigits + " digits - check if correct");
  else { 
    if (numdigits<10)
      msg (ifld, "warn", "Only " + numdigits + " digits - check if correct");
    else
      msg (ifld, "warn", "");
  }
  return true;
};

// -----------------------------------------
//             validateNumberic
// Returns true if OK 
// -----------------------------------------

function validateNumeric (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);
  var ageRE = /^[0-9]{1,4}$/
  if (!ageRE.test(tfld)) {
    msg (ifld, "error", "Must be numeric and 4 digits or less.");
    setfocus(vfld);
    return false;
  }

   msg (ifld, "warn", "");  
  return true;};
  
  // -----------------------------------------
//             validateCurrency
// Returns true if OK 
// -----------------------------------------

function validateCurrency (vfld,   // element to be validated
                         ifld,   // id of element to receive info/error msg
                         reqd)   // true if required
{
  var stat = commonCheck (vfld, ifld, reqd);
  if (stat != proceed) return stat;

  var tfld = trim(vfld.value);
  var currencyRE = /^((\$\d*)|(\$\d*\.\d{2})|(\d*)|(\d*\.\d{2}))$/
  if (!currencyRE.test(tfld)) {
    msg (ifld, "error", "Must be in dollars. ");
    setfocus(vfld);
    return false;
  }

   msg (ifld, "warn", "");  
  return true;};




 