var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if (input.value.length >= len && !containsElement(filter,keyCode)) {
     input.value = input.value.slice(0, len);
     input.form[(getIndex(input)+1) % input.form.length].focus();
  }

function containsElement(arr, ele) {
  var found = false, index = 0;
  while (!found && index < arr.length)
    if (arr[index] == ele)
       found = true;
    else
       index++;
       return found;
}

function getIndex(input) {

  var index = -1, i = 0, found = false;
  
  while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
      else i++;
      return index;
  }
  return true;
}


function closeNav()
	{
		var x = document.getElementsByTagName('div');
		for (var i=0;i<x.length;i++)
		{
			if (x[i].className == 'menu')
				x[i].style.display = "none";
		}
	}
  
  function clickNav(obj)
  {
  	if(obj.style.display == "none")
  	{
  		closeNav();
  		obj.style.display = "block";
  	}
  	else
  		closeNav();
  }
/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
function isNum(argvalue) {
argvalue = argvalue.toString();

if (argvalue.length == 0)
return false;

for (var n = 0; n < argvalue.length; n++)
if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
return false;

return true;
}

function valNum(arg) {
	var argvalue = arg.value.toString();
	var error = 0;
	if (argvalue.length == 0)
		return true;
	for (var n = 0; n < argvalue.length; n++)
		if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
			error = 1;
	
	if (error == 1) {
		alert("Wrong quantity value");
		arg.value = "";
	}		
	else
		return true;
}

function GoToContinue() {
  var update = document.frmCart.update.value;
  if (update == 1){	
		var where_to= confirm("You didn't save changes to quantities. To proceed without saving changes, please hit 'OK'.");
		if (where_to== true){  	  
			document.frmCart.action = "catalog.asp";
			document.frmCart.submit();
		}
	}else{
		document.frmCart.action = "catalog.asp";
		document.frmCart.submit();
	}
					
}
	
function GoToCheckout() {
  var update = document.frmCart.update.value;
  if (update == 1){	
		var where_to= confirm("You didn't save changes to quantities. To proceed without saving changes, please hit 'OK'.");
		if (where_to== true){  	  
			document.frmCart.action = "customerinfo.asp";
			document.frmCart.submit();
		}
	}else{
	//	var where_to= confirm("You won't be able to do any changes to the order after proceeding to checkout.");
	//	if (where_to== true){  	 
			document.frmCart.action = "customerinfo.asp";
			document.frmCart.submit();
	//	}
	}
}

function GoToSave() {
  document.frmCart.clean.value = "clean";
  document.frmCart.update.value = "0";
  document.frmCart.action = "cart.asp";
  document.frmCart.submit();
}

function GoToCart() {
 
  document.frmCart.action = "cart.asp";
  document.frmCart.submit();
}	
	

function AddToCart() {
  document.frmCart.action = "catalog.asp";
  document.frmCart.submit();
}	
	
function GoToClear(){
	var where_to= confirm("This action will remove all selected items from your cart. Are you sure you want to do this?");
	if (where_to== true){
		document.frmCart.clean.value = "empty";
		document.frmCart.action = "catalog.asp";
		document.frmCart.submit();
	}else{
		return true;
	}
}

function GoToCancel(){
	var where_to= confirm("This action will remove all selected items from your cart. Are you sure you want to do this?");
	if (where_to== true){		
		document.frmCart.action = "default.asp?clean=empty";
		document.frmCart.submit();
	}else{
		return true;
	}
}

function GoToConfirmation() {
	var where_to= confirm("Please print your order for your records before submitting.");
	if (where_to== true){
		window.print();
	}		
	document.frmCart.action = "confirmation.asp";
	document.frmCart.submit();
}	

function GoToEdit(){
	document.frmCart.action = "customerinfo.asp";
	document.frmCart.submit();
}

function infoVerify(theForm){
    if(theForm.Email.value == "" || theForm.Email.value == null){
		alert("Please enter Email.");
		theForm.elements["Email"].focus();	
	}else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.Email.value))){
        alert("Please enter valid Email.");
		theForm.elements["Email"].focus();   
	}else if(theForm.Name.value == "" || theForm.Name.value == null){
		alert("Please enter Name.");
		theForm.elements["Name"].focus();	 
	}else if(theForm.Company.value == "" || theForm.Company.value == null){
		alert("Please enter Company.");
		theForm.elements["Company"].focus();
	}else if(theForm.Address.value == "" || theForm.Address.value == null){
		alert("Please enter Address.");
		theForm.elements["Address"].focus();
	}else if(theForm.City.value == "" || theForm.City.value == null){
		alert("Please enter City.");
		theForm.elements["City"].focus();
	}else if(theForm.State.options[theForm.State.selectedIndex].value == "-" ){
		alert("Please select State.");
		theForm.elements["State"].focus();		
	}else if(theForm.Zip.value == "" || theForm.Zip.value == null){
		alert("Please enter Zip Code.");
		theForm.elements["Zip"].focus();
	}else if(theForm.Phone.value == "" || theForm.Phone.value == null){
		alert("Please enter Phone.");
		theForm.elements["Phone"].focus();
					
	}else{
		 theForm.ExEmail.value = "";
	     theForm.submit();
	}	
}

function exinfoVerify(theForm){
       
	if(theForm.ExEmail.value == "" || theForm.ExEmail.value == null){
		alert("Please enter Email.");
		theForm.elements["ExEmail"].focus();	
	}else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.ExEmail.value))){
        alert("Please enter valid Email.");
		theForm.elements["ExEmail"].focus();   
	}else if(theForm.Name.value == "" || theForm.Name.value == null){
		alert("Please enter Name.");
		theForm.elements["Name"].focus();	 
	}else if(theForm.Company.value == "" || theForm.Company.value == null){
		alert("Please enter Company.");
		theForm.elements["Company"].focus();
	}else if(theForm.Address.value == "" || theForm.Address.value == null){
		alert("Please enter Address.");
		theForm.elements["Address"].focus();
	}else if(theForm.City.value == "" || theForm.City.value == null){
		alert("Please enter City.");
		theForm.elements["City"].focus();
	}else if(theForm.State.options[theForm.State.selectedIndex].value == "-" ){
		alert("Please select State.");
		theForm.elements["State"].focus();	
	}else if(theForm.Zip.value == "" || theForm.Zip.value == null){
		alert("Please enter Zip Code.");
		theForm.elements["Zip"].focus();
	}else if(theForm.Phone.value == "" || theForm.Phone.value == null){
		alert("Please enter Phone.");
		theForm.elements["Phone"].focus();
					
	}else{		
	     theForm.submit();
	}	
}


function BannerScroll(bigSize, banners) {
	var scrollDiv, scrollMain, scrollCp, i, j, theLink = [], theLink2 = [], scrollInt = null, scrollMe, scrollLeft = 0, pauseButton, runStop, paused = false, skip = false, whereToInsertCode, whereToInsert, nextBan, nextButton, prevButton, prevBan, set, smallScrollBox, rand, imageLink, thumbOver, bannerPos = [], banTotal, lastWidth = 0, centerPos, banAmount, start, end, fastScroll = false, resizeInt, jumpTo, jumpToSpot, windowResize, scrollContain, lastCenter = 0, smallImg = [], thumbRight, scrollPosBox, scrollPosBox2, smallScrollLeft, scrollPosWidth, smallScrollLeft2, elapsed = 0, lastTime = 0, bannerHeight, smallBanHeight, smallScroller, setSizes, disableEvents, enableEvents;
	
	smallScroller = function () {
		smallScrollLeft = (scrollLeft / 20);
		if (-smallScrollLeft > smallScrollBox.offsetWidth) {
			smallScrollLeft = (scrollLeft + (banTotal / 2)) / 20;
		}
		scrollPosBox.style.left = -smallScrollLeft + 'px';
		smallScrollLeft2 = -smallScrollLeft + scrollPosWidth;
		if (smallScrollLeft2 > smallScrollBox.offsetWidth) {
			smallScrollLeft2 = (smallScrollLeft2 - smallScrollBox.offsetWidth) - scrollPosWidth;
			scrollPosBox2.style.left = smallScrollLeft2 - 2 + 'px';
		} else {
			scrollPosBox2.style.left = -scrollPosWidth + 'px';
		}
	};
	
	setSizes = function () {
		lastWidth = document.body.offsetWidth;
		centerPos = (scrollMain.offsetWidth < 412) ? 0 : (scrollMain.offsetWidth - 414) / 2;
		scrollContain.style.width = scrollMain.offsetWidth - 14 + 'px';
		start = bannerPos[2] + centerPos;
		end = bannerPos[banners.length + 2] + centerPos;
		scrollPosWidth = (scrollContain.offsetWidth / 20);
		scrollPosBox.style.width = scrollPosWidth + 'px';
		scrollPosBox2.style.width = scrollPosWidth + 'px';
	};
	
	windowResize = function () {
		return function () {
			if (lastWidth !== document.body.offsetWidth) {
				setSizes();
				scrollLeft += centerPos - lastCenter;
				scrollDiv.style.left = scrollLeft + 'px';
				lastCenter = centerPos;
				smallScroller();
			}
		};
	};
	
	rand = function () {
		return Math.random() - 0.5;
	};
	
	disableEvents = function () {
		nextButton.onclick = null;
		prevButton.onclick = null;
		pauseButton.onclick = null;
		scrollDiv.onmouseover = null;
		scrollDiv.onmouseout = null;
	};
	
	enableEvents = function () {
		nextButton.onclick = nextBan();
		prevButton.onclick = prevBan();
		pauseButton.onclick = runStop();
		scrollDiv.onmouseover = runStop();
		scrollDiv.onmouseout = runStop();
	};
	
	// Speed should be positive to scroll left and negative to go right
	scrollMe = function (speed) {
		return function () {
			if (fastScroll) {
				disableEvents();
				if (speed > 0) {
					scrollLeft = (scrollLeft > (end + 40)) ? scrollLeft - speed : start;
				} else {
					scrollLeft = (scrollLeft < (start - 40)) ? scrollLeft - speed : end;
				}
				if (scrollLeft === start || scrollLeft === end) {
					skip = false;
				}
				if (!skip) {
					if ((speed > 0 && scrollLeft < (jumpToSpot + 40)) || (speed < 0 && scrollLeft > (jumpToSpot - 40))) {
						window.clearInterval(scrollInt);
						scrollInt = null;
						scrollLeft = jumpToSpot;
						fastScroll = false;
						enableEvents();
					}
				}
			} else {
				var now = new Date().getTime();
				elapsed = now - lastTime;
				lastTime = now;
				scrollLeft = (scrollLeft > end) ? scrollLeft - speed * elapsed / 1000 * 70 : start;
			}
			scrollDiv.style.left = scrollLeft + 'px';
			smallScroller();
		};
	};
	
	jumpTo = function (pos) {
		return function () {
			this.blur();
			var leftDist, rightDist, direction;
			if (scrollInt !== null) {
				window.clearInterval(scrollInt);
				scrollInt = null;
				paused = true;
				pauseButton.className = 'show-play';
			}
			jumpToSpot = bannerPos[pos] + centerPos;
			if (jumpToSpot > scrollLeft) {
				leftDist = scrollLeft - jumpToSpot;
				rightDist = (end - scrollLeft) + (jumpToSpot - start);
			} else {
				leftDist = (end - jumpToSpot) + (scrollLeft - start);
				rightDist = jumpToSpot - scrollLeft;
			}
			direction = (leftDist > rightDist) ? -40 : 40;
			if (direction > 0) {
				skip = (jumpToSpot > scrollLeft) ? true : false;
			} else {
				skip = (jumpToSpot < scrollLeft) ? true : false;
			}
			fastScroll = true;
			scrollInt = window.setInterval(scrollMe(direction), 20);
			return false;
		};
	};
	
	nextBan = function () {
		return function () {
			this.blur();
			if (scrollInt !== null) {
				window.clearInterval(scrollInt);
				scrollInt = null;
				paused = true;
				pauseButton.className = 'show-play';
			}
			for (i = 2; i < banners.length + 2; i++) {
				if (scrollLeft > bannerPos[i] + centerPos) {
					jumpToSpot = bannerPos[i] + centerPos;
					break;
				} else if (i === banners.length + 1) {
					jumpToSpot = start;
				}
			}
			skip = (jumpToSpot > scrollLeft) ? true : false;
			fastScroll = true;
			scrollInt = window.setInterval(scrollMe(40), 20);
		};
	};
	
	prevBan = function () {
		return function () {
			this.blur();
			if (scrollInt !== null) {
				window.clearInterval(scrollInt);
				scrollInt = null;
				paused = true;
				pauseButton.className = 'show-play';
			}
			for (i = banners.length + 2; i > 1; i--) {
				if (scrollLeft < bannerPos[i] + centerPos) {
					jumpToSpot = bannerPos[i] + centerPos;
					break;
				} else if (i === 2) {
					jumpToSpot = bannerPos[banners.length + 1] + centerPos;
				}
			}
			skip = (jumpToSpot < scrollLeft) ? true : false;
			fastScroll = true;
			scrollInt = window.setInterval(scrollMe(-40), 20);
		};
	};
	
	runStop = function () {
		return function () {
			if (!paused) {
				if (scrollInt !== null) {
					window.clearInterval(scrollInt);
					scrollInt = null;
					if (this.id === 'scroller-run-stop') {
						this.blur();
						pauseButton.className = 'show-play';
						paused = true;
					}
				} else {
					lastTime = new Date().getTime();
					scrollInt = window.setInterval(scrollMe(1), 10);
					if (this.id === 'scroller-run-stop') {
						this.blur();
						pauseButton.className = 'show-pause';
						paused = false;
					}
				}
			} else {
				if (this.id === 'scroller-run-stop') {
					this.blur();
					lastTime = new Date().getTime();
					scrollInt = window.setInterval(scrollMe(1), 10);
					pauseButton.className = 'show-pause';
					paused = false;
				}
			}
		};
	};
	
	thumbOver = function (num) {
		return function () {
			smallImg[num].className = (smallImg[num].className === 'scroller-thumb') ? 'top-nav-hidden' : 'scroller-thumb';
		};
	};
	
	// init
	if (banners) {
		banners.sort(rand);
		banAmount = banners.length * 2;
		bannerHeight = (bigSize) ? 200 : 150;
		smallBanHeight = (bigSize) ? 40 : 30;
		imageLink = (bigSize) ? 'http://supply2store.com/ad/' : 'http://supply2store.com/ad/mini/';
		
		for (i = 0; i < (banAmount); i++) {
			if (i === 0) {
				bannerPos[i] = 0;
				banTotal = 0;
			} else {
				banTotal += 406;
				bannerPos[i] = banTotal * -1;
			}
			if (i === (banAmount - 1)) {
				banTotal += 406;
			}
		}
		
		scrollMain = createEl('div', {'id' : 'scroller-main-container'});
		scrollMain.style.height = bannerHeight + 36 + 'px';
		scrollCp = createEl('div', {'id' : 'scroller-control-panel'}, scrollMain);
		
		pauseButton = createEl('div', {'id' : 'scroller-run-stop'}, scrollCp);
		pauseButton.className = 'show-pause';
		pauseButton.onclick = runStop();
		
		nextButton = createEl('div', {'id' : 'scroller-next-button'}, scrollCp);
		nextButton.onclick = nextBan();
		
		prevButton = createEl('div', {'id' : 'scroller-prev-button'}, scrollCp);
		prevButton.onclick = prevBan();
		
		smallScrollBox = createEl('div', {'id' : 'scroller-small-box'}, scrollCp);
		scrollPosBox = createEl('div', {'class' : 'scroller-position-box'}, smallScrollBox);
		scrollPosBox2 = createEl('div', {'class' : 'scroller-position-box'}, smallScrollBox);
		
		scrollContain = createEl('div', {'id' : 'scroller-banners-container'}, scrollMain);
		scrollContain.style.height = bannerHeight + 'px';
		
		scrollDiv = createEl('div', {'id' : 'scroller-banners'}, scrollContain);
		scrollDiv.style.width = banTotal + 'px';
		scrollDiv.onmouseover = runStop();
		scrollDiv.onmouseout = runStop();
		
		j = 0;
		thumbRight = (banners.length * 20) - 15;
		for (set in banners) {
			if (banners[set]) {
				theLink[j] = createEl('a', {'href' : banners[set].url}, scrollDiv);
				theLink[j].innerHTML = '<img src="' + imageLink + banners[set].imageId + '.jpg" alt="' + banners[set].alt + '" width="400" height="' + bannerHeight + '">';
				theLink2[j] = createEl('a', {'href' : '#'}, smallScrollBox);
				theLink2[j].innerHTML = j + 1;
				theLink2[j].onmouseover = thumbOver(j);
				theLink2[j].onmouseout = thumbOver(j);
				theLink2[j].onclick = (j < 2) ? jumpTo(j + banners.length) : jumpTo(j);
				smallImg[j] = createEl('img', {'src' : imageLink + 'small/' + banners[set].imageId + '.jpg', 'alt' : banners[set].alt, 'class' : 'top-nav-hidden', 'width' : 80, 'height' : smallBanHeight}, scrollContain);
				smallImg[j].style.right = thumbRight + 'px';
				thumbRight -= 20;
				j++;
			}
		}
		
		for (i = 0; i < theLink.length; i++) {
			scrollDiv.appendChild(theLink[i].cloneNode(true));
		}
		
		if (document.getElementById('breadcrumbs_top')) {
			whereToInsertCode = document.getElementById('breadcrumbs_top').nextSibling;
			whereToInsert = whereToInsertCode.parentNode;
		} else {
			whereToInsert = document.getElementById('main-panel');
			whereToInsertCode = whereToInsert.firstChild;
		}
		
		if (whereToInsertCode) {
			whereToInsert.insertBefore(scrollMain, whereToInsertCode);
		} else if (whereToInsert) {
			whereToInsert.appendChild(scrollMain);
		}
		
		setSizes();
		scrollLeft = start;
		smallScroller();
		scrollDiv.style.left = scrollLeft + 'px';
		scrollPosBox.style.left = -smallScrollLeft + 'px';
		scrollPosBox2.style.left = -scrollPosWidth + 'px';
		
		scrollInt = window.setInterval(scrollMe(1), 10);
		resizeInt = window.setInterval(windowResize(), 100);
	}
}



function exEmailVerify(theForm){
       
	if(theForm.ExEmail.value == "" || theForm.ExEmail.value == null){
		alert("Please enter Email.");
		theForm.elements["ExEmail"].focus();	
	}else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(theForm.ExEmail.value))){
        alert("Please enter valid Email.");
		theForm.elements["ExEmail"].focus();   
	}else{		
	     theForm.submit();
	}	
}