// JavaScript Document

<!--

// All Rights Reserved, Copyright © Brite Inc.

var TAL_ID = null;
var STARS_RATING = null;

///////////////////////////////////////////////////////////////////////////
// DROP DOWN FUNCTIONS
///////////////////////////////////////////////////////////////////////////
//drop downs  

sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}

		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

//More images roll over
sfHover2 = function() {
	if (document.getElementById("moreImagesBox")){
		var sfEls = document.getElementById("moreImagesBox").getElementsByTagName("DIV");
	
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover2";
			}
	
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover2\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover2);

// color roll over
sfHover3 = function() {
	if (document.getElementById("colorsBox")){
		var sfEls = document.getElementById("colorsBox").getElementsByTagName("P");
	
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover3";
			}
	
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover3\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover3);

// product tools roll over
sfHover4 = function() {
	if(document.getElementById("productTools")){
		var sfEls = document.getElementById("productTools").getElementsByTagName("DIV");
	
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover4";
			}
	
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover4\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover4);

// take a look roll over
sfHover5 = function() {
	if(document.getElementById("sections_block")){
		var sfEls = document.getElementById("sections_block").getElementsByTagName("DIV");
	
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover5";
			}
	
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover5\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover5);

// a-frames section page roll over
sfHover6 = function() {
	if(document.getElementById("aframes")){
		var sfEls = document.getElementById("aframes").getElementsByTagName("DIV");
	
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover6";
			}
	
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover6\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover6);

// item page color roll over
sfHover7 = function() {
	if(document.getElementById("optionsBlock")){
		var sfEls = document.getElementById("optionsBlock").getElementsByTagName("P");
	
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover7";
			}
	
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover7\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover7);

///////////////////////////////////////////////////////////////////////////
// END DROP DOWN FUNCTIONS
///////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////
// AJAX FUNCTIONS
/////////////////////////////////////////////////////////////////////////////////////
// JavaScript Document
//Get HTTP Object for AJAX Processing
function getHTTPObject(){
	if (window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
		return new XMLHttpRequest();
	else {
		alert("Your browser does not support AJAX.");
		return null;
	}
}

// setOutput()
// Loads the output of an AJAX call to a element with the given id
// args: id - id of element to load output into, ajaxHandle - AJAX object 
function setOutput(id, ajaxHandle){
	if(ajaxHandle.readyState == 4){
		if( ajaxHandle.responseText != null && ajaxHandle.responseText != "" ){
			document.getElementById(id).innerHTML = ajaxHandle.responseText;
		}
	}
}

// runAJAXasync()
// Runs a 'asyncronous' AJAX call and load the out put to a given location
// args: pageName - page name of AJAX script, outputID - id of element to load output into
//		 argsArray - array of arguments for AJAX call
function runAJAXasync(pageName,outputID,argsArray){
	var urlArgs = "";
	
	//Build string of arguments for AJAX call
	for(var i=0;i<argsArray.length;i++){
		urlArgs += argsArray[i][0] + "=" + argsArray[i][1] + "&";
	}
	//Adds date to arguments to force browser to reload the called page
	urlArgs += "pseudoParam="+new Date().getTime();
	
	httpObject = getHTTPObject();
	//Send comment data to submit_comment.php return all comments
	if (httpObject != null) {
		httpObject.open("GET", "/components/scripts/ajax/"+pageName+".php?"+urlArgs, true);
		httpObject.send(null);
		httpObject.onreadystatechange = function () { setOutput(outputID,httpObject); return false; };
	} else {
		document.write('Failed to get httpObject');
	}
}

// runAJAXasync()
// Runs a 'syncronous' AJAX call and load the out put to a given location
// args: pageName - page name of AJAX script, argsArray - array of arguments for AJAX call
function runAJAXsync(pageName,argsArray){
	var urlArgs = "";
	
	//Build string of arguments for AJAX call
	for(var i=0;i<argsArray.length;i++){
		urlArgs += argsArray[i][0] + "=" + argsArray[i][1] + "&";
	}
	//Adds date to arguments to force browser to reload the called page
	urlArgs += "pseudoParam="+new Date().getTime();
	
	httpObject = getHTTPObject();
	//Send comment data to submit_comment.php return all comments
	if (httpObject != null) {
		httpObject.open("GET", "/components/scripts/ajax/"+pageName+".php?"+urlArgs, false);
		httpObject.send(null);
		return httpObject.responseText;
	} else {
		document.write('Failed to get httpObject');
	}
}

/////////////////////////////////////////////////////////////////////////////////////
// END AJAX FUNCTIONS
/////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////
// GENERAL FUNCTIONS
///////////////////////////////////////////////////////////////////////////

// new2DArray()
// Sets up a new 2D array
// args: length - length of array, width - width of array
// return: the new array
function new2DArray(length, width){
	returnArray = new Array(length);
	for(var i=0;i<length;i++){
		returnArray[i] = new Array(width);	
	}
	return returnArray;
}

// clear field on focus
function autoclear(thefield,thedefault) {
	if (thefield.value==thedefault) thefield.value = "";
}

//bookmark this page
function bookmarkThisPage(){
	if (window.sidebar) // firefox
		alert("Your browser does not support this function. Please click OK then press CTRL+D on your keyboard to bookmark this page.");
	else if(window.opera && window.print){ // opera
		alert("Your browser does not support this function. Please click OK then press CTRL+D on your keyboard to bookmark this page.");
	} 
	else if(document.all)// ie
		window.external.AddFavorite('http://www.USEntranceMats.com', 'USEntranceMats');
}

function bookmark_us(url, title){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
}
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

// isNumeric()
// checks if a value is numeric without a decimal
// args: value - value to check
function isNumeric(value){
  var anum=/(^\d+$)/;

  if (anum.test(value) && value != null )
 	 return true;
	 
  return false;
}


// openWindow(url)
// Opens the whats this pop
// args: what - the content to load
function windowOpen(url,height,width){
	window.open('http://www.usentrancemats.com/'+url,'','resizable=yes,toolbar=no,location=no,scrollbars=yes,status=no,width='+width+',height='+height+',top=30,left=30');
}

//Desc: Switch tab box display style when tab is clicked
//Args: name - id of clicked tab
function switchTabs(name){
	if( document.getElementById("featBox") ){ document.getElementById("featBox").style.display = "none"; }
	if( document.getElementById("descBox") ){document.getElementById("descBox").style.display = "none"; }
	if( document.getElementById("reviewsBox") ){document.getElementById("reviewsBox").style.display = "none"; }
	if( document.getElementById("colorsBox") ){document.getElementById("colorsBox").style.display = "none"; }
	if( document.getElementById("imagesBox") ){document.getElementById("imagesBox").style.display = "none"; }
	document.getElementById(name).style.display = "inline";
}

//Shows the element with the given id if it exists
function showElement(id){
	if( document.getElementById(id) ){ document.getElementById(id).style.display = "inline"; }	
}

//Hides the element with the given id if it exists
function hideElement(id){
	if( document.getElementById(id) ){ document.getElementById(id).style.display = "none"; }	
}

//Hides or show the element with the given id if it exists
function showRelated(){
	if( document.getElementById('nbRelatedBlock') ){ 
		if( document.getElementById('nbRelatedBlock').style.display == "inline" ){ 
			document.getElementById('nbRelatedBlock').style.display = "none"; 
			document.getElementById('nbRelated').style.background = "url(/images/layout/nb-related.gif)";
		}else if( document.getElementById('nbRelatedBlock').style.display == "none" ){ 
			document.getElementById('nbRelatedBlock').style.display = "inline"; 
			document.getElementById('nbRelated').style.background = "url(/images/layout/nb-related-over.gif)";
		}
	}	
}

// getScrollY
// Gets the y scroll position and returns
function getScrollY() {
  scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

///////////////////////////////////////////////////////////////////////////
// END GENERAL FUNCTIONS
///////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////
// AUTO COMPLETE FUNCTIONS
///////////////////////////////////////////////////////////////////////////

// Auto complete for the quick search
// Args: searchTerm - the string that is in the quicksearch input
// Returns: a list of products that match the term
function autocomplete(searchTerm){
	if( searchTerm.length > 0 ){
		var returnData = "";
		searchArgs = new2DArray(2,1);
		showElement('suggestions');	
		
		searchArgs[0][0] = "queryString";
		searchArgs[0][1] = searchTerm;
		
		returnData = runAJAXsync("auto_complete",searchArgs);
					
		if( returnData != 0 ){
			document.getElementById('autoSuggestionsList').innerHTML = returnData;
		} else {
			hideElement('suggestions');	
		}
	} else {
		hideElement('suggestions');	
	}
}

// Used with the autocomplete - sets the quicksearch input to the sku name that is selected
// Args: term - the sku name
function fill(term){
	document.getElementById('inputString').value = term;	
}

// clear field on focus
function autoclear(thefield,thedefault) {
	if (thefield.value==thedefault) thefield.value = "";
}
///////////////////////////////////////////////////////////////////////////
// END AUTO COMPLETE FUNCTIONS
///////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////
// REVIEW FUNCTIONS
///////////////////////////////////////////////////////////////////////////

function switchReviews(id){
	hideElement('viewReviews');
	hideElement('writeReviews');
	hideElement('readReviews');
	showElement(id);
}

function submitReview(sku_grp_id){
	var reviewArgs = new2DArray(5,2);
	var recommend = null;
	var reviews = "";
	var stars = STARS_RATING;
	var reviewer = "";
	
	for(var i=0;i<document.review.recommend.length;i++){
		if (document.review.recommend[i].checked){
      		recommend = document.review.recommend[i].value;
      	}
   	}
	
	if( stars == null ){
		alert("You must select a number of stars.");
	}else if( document.review.reviewer.value == "" ){
		alert("Please enter your name for the review.");
	}else if( recommend == null ){
		alert("You have not seleted if you would recommend this product.");
	}else{
		switchReviews('readReviews');
	
		//Set up arguments for ajax call
		reviewArgs[0][0] = "sku_grp_id";
		reviewArgs[0][1] = sku_grp_id;
		reviewArgs[1][0] = "stars";
		reviewArgs[1][1] = stars;
		reviewArgs[2][0] = "reviewer";
		reviewArgs[2][1] = document.review.reviewer.value;
		reviewArgs[3][0] = "review";
		reviewArgs[3][1] = document.review.reviewText.value;
		reviewArgs[4][0] = "recommend";
		reviewArgs[4][1] = recommend;
		
		//Run AJAX call
		reviews = runAJAXsync("submit_review",reviewArgs);
		$('readReviews').innerHTML = reviews;
		
		//Reset review form
		STARS_RATING = null;
		starsOut();
		//document.review.reviewer.value = "";
		document.review.reviewText.value = "";
		for(var i=0;i<document.review.recommend.length;i++){
      		document.review.recommend[i].checked = false;
   		}
	}
}

function starsOver(starNumber){
	var stars = $$('.rateStars');	
	
	for(var i=0;i<stars.length;i++){
		if( (i+1) <= starNumber ){
			stars[i].src = "/images/graphics/rate-star-on.gif";
		} else {
			stars[i].src = "/images/graphics/rate-star-off.gif";
		}
	}
}

function starsOut(){
	var stars = $$('.rateStars');
	
	for(var i=(stars.length-1);i>=0;i--){
		if( (i+1) > STARS_RATING ){
			stars[i].src = "/images/graphics/rate-star-off.gif";
		} else {
			stars[i].src ="/images/graphics/rate-star-on.gif";
		}
	}
}

function starsSelect(starNumber){
	if( starNumber == STARS_RATING ){
		STARS_RATING = STARS_RATING-1;
		starsOut();
	} else {
		STARS_RATING = starNumber	;
		starsOut();
	}
}

//////////////////////////////////////////////////////////////////////////
// END REVIEW FUNCTIONS
///////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////
// TAKE A LOOK 
//////////////////////////////////////////////////////////////////

// tal()
// Displays the talk a look div
function tal(id){
	//Check id against the globale TAL_ID to see if 
	//this 'take a look' box is already open
	if( id != TAL_ID ){
		//Fade out previously open take a look box
		if( TAL_ID != null ) {
			talClose(TAL_ID);
		}
		
		//Set position and display of the 'take a look' box
		//$("talBlock"+id).style.top = (getScrollY()+60) + "px";
		//$("talBlock"+id).style.left = (window.getWidth() / 2)-180 + "px";
		$("talBlock"+id).style.display = "inline";
		
		//Set opacity of 'take a look' box to zero
		$("talBlock"+id).set('opacity',"0.0");
		
		//Fade in 'take a look' box
		var myFx = new Fx.Tween("talBlock"+id);
		myFx.set('tween', {duration: '2000'});
		myFx.start('opacity','0.0','1.0');
		
		//If there is an iframe fade it in as well
		if( $("taliframe"+id) ){
			$("taliframe"+id).style.display = "inline";
			
			var myFx2 = new Fx.Tween("taliframe"+id);
			myFx2.set('tween', {duration: '2000'});
			myFx2.start('opacity','0.0','1.0');
		}
		
		//Reset global variable
		TAL_ID = id
	}
}

// talClose()
// Closed the take a look div
function talClose(id){
	if( TAL_ID == id ){
		TAL_ID = null;	
	}
	
	//Fade out the 'take a look' box
	var myFx = new Fx.Tween("talBlock"+id);
	myFx.set('tween', {duration: '2000'});						 
	myFx.start('opacity','1.0','0.0');
	
	//Set 'take a look' box display to none
	window.setTimeout( function () { $("talBlock"+id).style.display = "none"; }, 1000);
	
	//If there is an iframe fade it in as well
	if( $("taliframe"+id) ){
		
		var myFx2 = new Fx.Tween("taliframe"+id);
		myFx2.set('tween', {duration: '2000'});
		myFx2.start('opacity','1.0','0.0');
		window.setTimeout( function () { $("taliframe"+id).style.display = "none"; }, 1000);
	}
}

// talSwapImg()
// Changes the take a look box image
function talSwapImg(id,img){
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	
	// loop through all anchor tags and set up onclick calls
	for (var i=0; i<anchors.length; i++){
		if (anchors[i].getAttribute("rev") && (anchors[i].getAttribute("rev") == id)){
			//alert( anchor.getAttribute("rev") );
			if( anchors[i].getAttribute("href") == img ){
				anchors[i].className = "talSelected";
			}else{
				anchors[i].className = "";
			}
		}
	}
	
	document.getElementById("talImg200"+id).src = img;
}


function launchTAL(id,img){
	talSwapImg(id,img);
	tal(id);
}
//////////////////////////////////////////////////////////////////
// ENDTAKE A LOOK 
//////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////
// PAGE INITIALIZATION
//////////////////////////////////////////////////////////////////

// initStars()
// initialize stasr
function initStars(){
	var stars = $$('.rateStars');	
	
	for(var i=0;i<stars.length;i++){
		stars[i].onmouseover = function () { starsOver(this.getAttribute('alt')); return false; }	
		stars[i].onmouseout = function () { starsOut(); return false; }
		stars[i].onclick = function () { starsSelect(this.getAttribute('alt')); return false; }
	}
}

// initTAL()
// initialize the take a look functions
function initTAL(){
	var talP = $$('.talP');
	var talCloseButton = $$('.talClose');
	
	//Attach tal function to the take a look button
	for(var i=0;i<talP.length;i++){
		talP[i].onclick = function () { tal(this.getAttribute('title')); return false; }
	}
	
	//Attach talClose function to close button
	for(var i=0;i<talCloseButton.length;i++){
		talCloseButton[i].onclick = function () { talClose(this.getAttribute('rev')); return false; }
	}
}

// initMoreImgTAL()
// initialize the more images javascript for the 'take a look' box
function initMoreImgTAL(){
	//INITIALIZE STEPS LINKS AND STYLES LINKS
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	
	// loop through all anchor tags and set up onclick calls
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		
		if (anchor.getAttribute("rel") && (anchor.getAttribute("rel") == "talMoreImg")){
			anchor.onclick = function () {talSwapImg(this.getAttribute("rev"),this.getAttribute("href")); return false;}
		}else if (anchor.getAttribute("rel") && (anchor.getAttribute("rel") == "prodMoreImg")){
			anchor.onclick = function () {launchTAL(this.getAttribute("rev"),this.getAttribute("href")); return false;}
		}
	}
}

// loadImages()
// Image preloader
function loadImages(){
  if( document.images ){
	  pic1= new Image(27,160); 
	  pic1.src='/images/layout/videos-over.gif';
	  pic2= new Image(27,160); 
	  pic2.src='/images/layout/new-products-over.gif';
	  pic3= new Image(32,32); 
	  pic3.src='/images/graphics/page-loading.gif';
	  pic4= new Image(26,69); 
	  pic4.src='/images/layout/btn-home-over.gif';
	  pic5= new Image(26,97); 
	  pic5.src='/images/layout/btn-about-over.gif';
	  pic6= new Image(26,112); 
	  pic6.src='/images/layout/btn-contact-over.gif';
	  pic7= new Image(26,95); 
	  pic7.src='/images/layout/btn-products-over.gif';
	  pic8= new Image(26,140); 
	  pic8.src='/images/layout/btn-supersavers-over.gif';
	  pic9= new Image(26,125); 
	  pic9.src='/images/layout/btn-quickship-over.gif';
	  pic10= new Image(26,137); 
	  pic10.src='/images/layout/btn-helpcenter-over.gif';
  }
}

// loadImages()
// Image preloader
function loadShowMeImages(){
  if( document.images ){
	  pic1= new Image(26,138); 
	  pic1.src='/images/layout/nb-qs.gif';
	  pic2= new Image(26,140); 
	  pic2.src='/images/layout/nb-viewall.gif';
	  pic3= new Image(26,137); 
	  pic3.src='/images/graphics/nb-bestsellers.gif';
	  pic4= new Image(26,139); 
	  pic4.src='/images/layout/nb-onsale.gif';
  }
}


// hideLoading()
// Hides the loading screen if it exists
function hideLoading(){ 
	if( document.getElementById('loading') )
		document.getElementById('loading').className = "loading-invisible";
}

// setAutoComplete
// attachs the auto complete function
function setAutoComplete(){
	if( document.getElementById('inputString') )
	document.getElementById('inputString').onkeyup = function() { autocomplete(this.value); return false; }	
}

// hideJavaAlert
// hides the javascript alert if it is present on the page
function hideJavaAlert(){
	if( document.getElementById('javaAlert') ){ document.getElementById('javaAlert').style.display = "none"; }
}


// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
function addLoadEvent(func){	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

addLoadEvent(loadImages);
//addLoadEvent(setAutoComplete);
addLoadEvent(hideLoading);
//////////////////////////////////////////////////////////////////
// END PAGE INITIALIZATION
//////////////////////////////////////////////////////////////////

// -->

