// Search box functions

function ClearSearch() {
	try {
		var element = window.document.getElementById('SearchKeywords');
		if(element.value=='Search'){
			element.value='';
		}else{}
	} catch(e) {}
}

function WriteSearch() {
	try {
		var element = window.document.getElementById('SearchKeywords');
		if(element.value==''){
			element.value='Search';
		}else{}
	} catch(e) {}
}


ReplaceSearch = function() {
	try {
		var elements = window.document.getElementById('SearchKeywords');
		if(window.addEventListener){	
			elements.addEventListener('focus', ClearSearch, true);
			elements.addEventListener('blur', WriteSearch, true);	
		}else{
			elements.attachEvent("onfocus",ClearSearch);
			elements.attachEvent("onblur",WriteSearch);
		}
	} catch(e) {}
}



// cookie functions

function createCookie(name,value,days) {
	try {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	} catch(e) {}
}

function readCookie(name) {
	try {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	} catch(e) {}
}

function eraseCookie(name) {
	try {
		createCookie(name,"",-1);
	} catch(e) {}
}


// Managing style cookies, attaching to links

function SetStyleCookie(name,value,days) {
	try {
		eraseCookie(name);
		//alert('cookie erased');
		createCookie(name,value,days);
	} catch(e) {}
}

function ResetStyle() {
	try {
		SetStyleCookie('css','',30);
	} catch(e) {}
}

function BWStyle() {
	try {
		SetStyleCookie('css','bw',30);
	} catch(e) {}
}

function WBStyle() {
	try {
		SetStyleCookie('css','wb',30);
	} catch(e) {}
}


function ResetSize() {
	try {
		SetStyleCookie('size','',30);
	} catch(e) {}
}

function LargeStyle() {
	try {
		SetStyleCookie('size','large',30);
	} catch(e) {}
}

function LargerStyle() {
	try {
		SetStyleCookie('size','larger',30);
	} catch(e) {}
}

AttachCookieLinks = function() {
	try {
		var Link1 = window.document.getElementById('DefaultStyle');
		if(window.addEventListener){	
			Link1.addEventListener('click', ResetStyle, true);
		}else{
			Link1.attachEvent("onclick",ResetStyle);
		}
		var Link2 = window.document.getElementById('BlackOnWhite');
		if(window.addEventListener){	
			Link2.addEventListener('click', BWStyle, true);
		}else{
			Link2.attachEvent("onclick",BWStyle);
		}
		var Link3 = window.document.getElementById('WhiteOnBlack');
		if(window.addEventListener){	
			Link3.addEventListener('click', WBStyle, true);
		}else{
			Link3.attachEvent("onclick",WBStyle);
		}

		var Link4 = window.document.getElementById('DefaultSize');
		if(window.addEventListener){	
			Link4.addEventListener('click', ResetSize, true);
		}else{
			Link4.attachEvent("onclick",ResetSize);
		}
		var Link5 = window.document.getElementById('Large');
		if(window.addEventListener){	
			Link5.addEventListener('click', LargeStyle, true);
		}else{
			Link5.attachEvent("onclick",LargeStyle);
		}
		var Link6 = window.document.getElementById('Larger');
		if(window.addEventListener){	
			Link6.addEventListener('click', LargerStyle, true);
		}else{
			Link6.attachEvent("onclick",LargerStyle);
		}
	} catch(e) {}
}






// Pop-up window at 500x400
function PopWin500x400(url) {
	newWindow = window.open(url,"","toolbar=no,location=no,scrollbars=yes,width=500,height=400");
}

// Generic Pop-up window where you pass width and height
function PopWin(url,strWidth,strHeight) {
	newWindow = window.open(url,"","toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight);
}








// Check the browser
function browserSniffer() {

	this.browserVersion = parseInt(navigator.appVersion);
	this.browserPlatform = navigator.platform;
	this.browserName = navigator.appName;

	this.isOpera = (this.browserName.indexOf('Opera') > -1) ? true : false;
	this.isNavigator = (this.browserName.indexOf('Netscape') > -1) ? true : false;
	this.isExplorer = (this.browserName.indexOf('Explorer') > -1) ? true : false;
	
	this.isMac = (this.browserPlatform.indexOf('Mac') > -1) ? true : false;
	
	this.isFour = (this.browserVersion == 4) ? true : false;
	
	this.isIE3 = ((this.browserVersion == 3) && (this.isExplorer)) ? true : false;
	this.isNS4 = ((this.browserVersion == 4) && (this.isNavigator)) ? true : false;
	this.isNS6 = ((this.browserVersion == 5) && (this.isNavigator)) ? true : false;
}




// Switch an item's display property
function ShowHideItem(strItemID,strDisplay) {
	var coll = document;
	try{
		var obj = coll.getElementById(strItemID);
		obj.style.display=strDisplay;
	}
	catch(e){}
}



// Pass multiple IDs to toggle display, as well as type of item
function ShowHideMultiple(strItemID,strDisplay,strType) {
	// Determine the browser
  	var sniffer = new browserSniffer();

	// Create arrays of each comma-separated list of items
	var ItemArray = strItemID.split(/,/)
	var DisplayArray = strDisplay.split(/,/)
	var TypeArray = strType.split(/,/)

	ItemCount = ItemArray.length

	for (var i=0; i < ItemCount; i++ ) { 
		var strDisplay = DisplayArray[i]
		if (strDisplay=='block') {
			// If it is Mozilla, use appropriate styles based on type passed in
			if (sniffer.isNavigator) {
				if (TypeArray[i]=='tr') {
					strDisplay = 'table-row'
				} else if (TypeArray[i]=='td') {
					strDisplay = 'table-cell'
				} else if (TypeArray[i]=='li') {
					strDisplay = 'list-item'
				} else if (TypeArray[i]=='table') {
					strDisplay = 'table'
				} else {}
			} else {}

			// Now go through and find the first and second <select> and set it to display again
			try {
				document.getElementById(ItemArray[i]).getElementsByTagName('select')[0].style.display = 'inline';
			} catch(e) {}
			try {
				document.getElementById(ItemArray[i]).getElementsByTagName('select')[1].style.display = 'inline';
			} catch(e) {}
		} else if (strDisplay=='none') {
			// Hide the first and second <select> to account for a bug in IE where it doesn't properly hide them
			try {
				document.getElementById(ItemArray[i]).getElementsByTagName('select')[0].style.display = strDisplay;
			} catch(e) {}
			try {
				document.getElementById(ItemArray[i]).getElementsByTagName('select')[1].style.display = strDisplay;
			} catch(e) {}
		} else {}
		// Now run the proper toggle function
		ShowHideItem(ItemArray[i],strDisplay);
	}
}



function SelectShowHideMultiple(strSelectID,strSelectValue,strItemID,strDisplay,strType) {

	if (strSelectValue=='*') {
		ShowHideMultiple(strItemID,strDisplay,strType)
		//alert(strSelectValue);
	} else {
		if (document.getElementById(strSelectID).value==strSelectValue) {
			ShowHideMultiple(strItemID,strDisplay,strType)
			//alert(document.getElementById(strSelectID).value);
		} else {}
	}
}



function ShowHideSelectsInIdent(parentID,strDisplay){
	try{
		// Return select elements within a specific ID, show them
		var nestedSels = document.getElementById(parentID).getElementsByTagName('select');
		try{
			for(var i=0;i<nestedSels.length;i++){
				mySel = nestedSels[i];
				//mySel.style.display=strDisplay;
				mySel.style.visibility=strDisplay;
			}
		}
		catch(e){}
	}
	catch(e){}
}



// functions to zoom in on products, then zoom back out

function ZoomProduct() {
	try {
		ShowHideItem('ZoomImg','block');
		ShowHideSelectsInIdent('Configurator','hidden');
	} catch(e) {}
}

function CloseZoomProduct() {
	try {
		ShowHideItem('ZoomImg','none');
		ShowHideSelectsInIdent('Configurator','visible');
	} catch(e) {}
}


AddProductZoomControls = function() {
	try {
		var zoom = window.document.getElementById('ZoomLink');
		if(window.addEventListener){	
			zoom.addEventListener('click', ZoomProduct, true);
		}else{
			zoom.attachEvent("onclick",ZoomProduct);
		}

		var closeZoom = window.document.getElementById('CloseZoom');
		if(window.addEventListener){	
			closeZoom.addEventListener('click', CloseZoomProduct, true);
		}else{
			closeZoom.attachEvent("onclick",CloseZoomProduct);
		}
	} catch(e) {}
}






// Attaching functions to events


if(window.addEventListener) { // gecko, safari, konqueror and standard
	window.addEventListener('load', ReplaceSearch, false);
	window.addEventListener('load', AttachCookieLinks, false);
	window.addEventListener('load', AddProductZoomControls, false);
} else if(document.addEventListener) {  // opera 7
	document.addEventListener('load', ReplaceSearch, false);
	document.addEventListener('load', AttachCookieLinks, false);
	document.addEventListener('load', AddProductZoomControls, false);
} else if(window.attachEvent) { // win/ie
	window.attachEvent('onload', ReplaceSearch);
	window.attachEvent('onload', AttachCookieLinks);
	window.attachEvent('onload', AddProductZoomControls);
} else { // mac/ie5
	if(typeof window.onload == 'function') {
		var existing = onload;
		window.onload = function() {
			existing();
			ReplaceSearch();
			AttachCookieLinks();
			AddProductZoomControls();
		}
	} else {
		window.onload = function() {
			ReplaceSearch();
			AttachCookieLinks();
			AddProductZoomControls();
		}
	}
}