// JavaScript Document

var TAL_ID = null;
var STARS_RATING = null;

<!--
/////////////////////////////////////////////////////////////////////////////////////
// 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
/////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
// DROP DOWNS
/////////////////////////////////////////////////////////////////////////////////////////

// All Rights Reserved, Copyright © Brite Inc.
//drop downs 
sfHover = function() {
	if(document.getElementById("nav")){
		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);

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

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("section_block")){
		var sfEls = document.getElementById("section_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);

// item page color roll over
sfHover6 = 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+=" sfhover6";
			}
	
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover6\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover6);

// product tools roll over
sfHover7 = function() {
	if(document.getElementById("videosBox")){
		var sfEls = document.getElementById("videosBox").getElementsByTagName("DIV");
	
		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 DOWNS
/////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////
// 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.schooldesksandchairs.com', 'School Desks and Chairs');
}

// openWindow(url)
// Opens the whats this pop
// args: what - the content to load
function windowOpen(url,height,width){
	window.open('http://www.schooldesksandchairs.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("colorsBox") ){document.getElementById("colorsBox").style.display = "none"; }
	if( document.getElementById("imagesBox") ){document.getElementById("imagesBox").style.display = "none"; }
	if( document.getElementById("reviewsBox") ){document.getElementById("reviewsBox").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"; }	
}

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

/////////////////////////////////////////////////////////////////////////////////////////
// END GENERAL FUNCTIONS DROP DOWNS
/////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
// 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 reviewer = document.review.reviewer.value;
	var reviews = "";
	var stars = STARS_RATING;
	var code = document.review.code.value;
	
	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( reviewer == "" ){
		alert("You must enter your name.");
	}else if( recommend == null ){
		alert("You have not seleted if you would recommend this product.");
	}else if( code.toLowerCase() != "2cfnaw" ){
		alert("You have entered an incorrect code, please try again and resubmit your review.");
	}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.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
///////////////////////////////////////////////////////////////////////////

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

// loadImages()
// Image preloader
function loadImages(){
  if( document.images ){
  pic1= new Image(69,30); 
  pic1.src='/images/layout/btn_home_over.gif'; 
  pic2= new Image(87,30); 
  pic2.src='/images/layout/btn_about_over.gif';
  pic3= new Image(103,30); 
  pic3.src='/images/layout/btn_contact_over.gif';
  pic4= new Image(112,30); 
  pic4.src='/images/layout/btn_quickship_over.gif';
  pic5= new Image(130,30); 
  pic5.src='/images/layout/btn_green_over.gif';
  pic6= new Image(7,7); 
  pic6.src='/images/layout/arrowD.gif';
  pic7= new Image(32,32); 
  pic7.src='/images/graphics/page-loading.gif';	
  }
}

// initStars()
// initialize review stars
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; }
	}
}

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

// 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(hideLoading);
//////////////////////////////////////////////////////////////////
// END PAGE INITIALIZATION
//////////////////////////////////////////////////////////////////

// -->

