function loadValue(thisfield, defaulttext) {
    if (get_param('q') != null)
        thisfield.value = get_param('q')
    else
        thisfield.value = defaulttext;
}

function clickClear(thisfield, defaulttext) {
    if (thisfield.value == defaulttext) {
        thisfield.value = "";
    }
}

function clickRecall(thisfield, defaulttext) {
    if (thisfield.value == "") {
        thisfield.value = defaulttext;
    }
}

function topbarItemClick(obj, id, position) {
	//Set optional value default
	if (position == null)
		position = 'left';
		
	//Show the window
	showPopupDiv(id);								
	
	//Deselect the link
	obj.blur();
	
	//Set style of item
	obj.parentNode.style.borderBottom = '1px solid #FFF';
	obj.parentNode.style.backgroundColor = '#FFF';
	if (position == 'left') 
		obj.parentNode.style.borderLeft = '1px solid #CCC';
	if (position == 'right') 
		obj.parentNode.style.borderRIGHT = '1px solid #CCC';				

	//Set the event listenner
	if (document.layers) {
		document.captureEvents(Event.MOUSEUP)
	}
	document.getElementById(id).popupWindow = true;
	document.onmouseup = function(e) {
			
		var el=(typeof event!=='undefined')? event.srcElement : e.target
		//set style of item
		if (el.popupWindow == null && childOfPopupDiv(el) == false) {										
			obj.parentNode.style.borderBottom = '0';
			obj.parentNode.style.backgroundColor = 'transparent';
			if (position == 'left') 
				obj.parentNode.style.borderLeft = '0';
			if (position == 'right') 
				obj.parentNode.style.borderRIGHT = '0';						
		}
		
		//Conditionally hide all popups
		hidePopupDivs(e);
	}	
}

function showPopupDiv(id) {
	//Hide all popups
	hidePopupDivs(null);															
	//Show the window
	document.getElementById(id).style.display = 'block';	
}

function hidePopupDiv(id) {
	document.getElementById(id).style.display = 'none';
	document.getElementById(id).popupWindow = null;
}

function hidePopupDivs(e) {
	var popupDivs = document.getElementsByTagName('div');
	if (e == null) {
		for (var i = 0; i < popupDivs.length; i++) {
			if (popupDivs[i].popupWindow != null)
				hidePopupDiv(popupDivs[i].id);						
		}
	} else {	
		if (e.target.popupWindow == null && childOfPopupDiv(e.target) == false)
			for (var i = 0; i < popupDivs.length; i++) {	
				if (popupDivs[i].popupWindow != null) 
					hidePopupDiv(popupDivs[i].id);						
			}
	}
}

function childOfPopupDiv(obj) {	
	var result = false;
	obj = obj.parentNode;
	while (obj != null) {	
		if (obj.popupWindow != null) {						
			result = true;
			break;
		}
		obj = obj.parentNode;
	}
	return result;
}

function validateForm(form) {
	result = true;
	for (var i = 0; i < form.elements.length; i++) {
		objTemp = form.elements[i];
		if (objTemp.title != null) {
			if (objTemp.type.toLowerCase()  != 'button' && objTemp.type.toLowerCase()  != 'submit') {
				//Check if the item has a value
				if ((objTemp.value == '') && (objTemp.title != '')) {
					//alert(objTemp.title + " has not been entered");
					inlineMsg(form.elements[i], objTemp.title + " has not been entered", 2);
					result = false;
					break;
				}
				//Check if item is an 
				var strTemp = objTemp.title.toLowerCase();
				if (strTemp.indexOf('e-mail') != -1 || strTemp.indexOf('email') != -1) {
					if (!validateEmail(objTemp.value)) {
						inlineMsg(form.elements[i], objTemp.title + " is not valid", 2);
						result = false;
						break;
					}
				}
			}
		}
	}
	return result;
}

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGLEFTOFFSET = 0;
var MSGTOPOFFSET = -5;
var MSGHIDE = 3;

function inlineMsg(target,string,autohide) {
	var msg;
	var msgcontent;
	if(!document.getElementById('msg')) {
		msg = document.createElement('div');
		msg.id = 'msg';
		msgcontent = document.createElement('div');
		msgcontent.id = 'msgcontent';
		document.body.appendChild(msg);
		msg.appendChild(msgcontent);
		msg.style.filter = 'alpha(opacity=0)';
		msg.style.opacity = 0;
		msg.alpha = 0;
	} else {
		msg = document.getElementById('msg');
		msgcontent = document.getElementById('msgcontent');
	}
	msgcontent.innerHTML = string;
	msg.style.display = 'block';
	var msgheight = msg.offsetHeight;
	var targetdiv = target;
	targetdiv.focus();
	var targetheight = targetdiv.offsetHeight;
	var targetwidth = targetdiv.offsetWidth;
	var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
	var topposition = topPosition(targetdiv) + targetheight + MSGTOPOFFSET;
	var leftposition = leftPosition(targetdiv) + MSGLEFTOFFSET;
	msg.style.top = topposition + 'px';
	msg.style.left = leftposition + 'px';
	clearInterval(msg.timer);
	msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
	if(!autohide) {
		autohide = MSGHIDE;  
	}
	window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
	var msg = document.getElementById('msg');
	if(!msg.timer) {
		msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
	}
}

// face the message box //
function fadeMsg(flag) {
	if(flag == null) {
		flag = 1;
	}
	var msg = document.getElementById('msg');
	var value;
	if(flag == 1) {
		value = msg.alpha + MSGSPEED;
	} else {
		value = msg.alpha - MSGSPEED;
	}
	msg.alpha = value;
	msg.style.opacity = (value / 100);
	msg.style.filter = 'alpha(opacity=' + value + ')';
	if(value >= 99) {
		clearInterval(msg.timer);
		msg.timer = null;
	} else if(value <= 1) {
		msg.style.display = "none";
		clearInterval(msg.timer);
	}
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
	var left = 0;
	while (target) {
		left += target.offsetLeft;
		target = target.offsetParent;
	}
	return left;
}
  
  
// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
	var top = 0;
	while (target) {
		top += target.offsetTop;
		target = target.offsetParent;
	}
	return top;
}

function validateEmail(address) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(address);
}

//Events
function onLoadEvent(e) {
	//Check if the user is logged into the Intrabench store
	if (isUserLoggedIn()) {
		document.getElementById("loggedIn").style.display = 'inline-block';
		document.getElementById("loggedOut").style.display = 'none';
	} else {
		document.getElementById("loggedIn").style.display = 'none';
		document.getElementById("loggedOut").style.display = 'inline-block';
	}
	
	//Change the style of all Intrabench inputs
	var inputs = document.getElementById("intrabench").getElementsByTagName("input");
	for (var i = 0; i < inputs.length; i++) {
		//Buttons
		if (inputs[i].type == 'submit') {			
			inputs[i].setAttribute("class", "button");
			if (inputs[i].value == 'Go') {
				inputs[i].style.width = "40px";
			}
			
		}
		if (inputs[i].type == 'image') {			
			inputs[i].setAttribute("class", "buttonLink");
			inputs[i].src = '';
			if (inputs[i].value == '') {
				inputs[i].value = "Submit";
			}
		}
		//Text Fields
		if ((inputs[i].type == 'text') || (inputs[i].type == 'password')) {
			inputs[i].setAttribute("class", "inputText");
		}
	}
	
	//Selective styles that cant be set by CSS alone
	//My Account title
	var boldText = document.getElementById("intrabench").getElementsByTagName("b");
	for (var i = 0; i < boldText.length; i++) {		
		if (boldText[i].innerHTML == 'My Account') {			
			boldText[i].setAttribute("class", "myAccountLabel");
		}	
		if (boldText[i].innerHTML == 'Your Orders') {			
			boldText[i].setAttribute("class", "yourOrdersLabel");
		}
		if (boldText[i].innerHTML.indexOf('This payment is') > -1 ) {	
			boldText[i].setAttribute("class", "paymentIsLabel");
		}
	}
	
	//Set elements
	var tds = document.getElementById("intrabench").getElementsByTagName("td");
	for (var i = 0; i < tds.length; i++) {	
		//Search results
		if (tds[i].innerHTML.indexOf("Product Search Result") > -1) {			
			tds[i].setAttribute("class", "searchResultLabel");
		}	
		//Login register
		if (tds[i].innerHTML.indexOf("http://stateofthenation.intrabench.com/?storeRegister1") > -1) {			
			tds[i].setAttribute("class", "searchResultLabel");
			tds[i].style.display = "none";
		}
		//Login register
		if (tds[i].innerHTML.indexOf("https://stateofthenation.intrabench.com/?storeRegister1") > -1) {			
			tds[i].setAttribute("class", "searchResultLabel");
			tds[i].style.display = "none";
		}
	}
	
	//Hide signup when logging in
	
	
	//Hide second tables
	var tables = document.getElementById("intrabench").getElementsByTagName("table");
	if (tables.length > 1) {
		if (tables[1].innerHTML.indexOf('Your shopping basket is empty.<br><br>') > -1) {
			tables[1].setAttribute("class", "subTableEmptyBasket");
		} else {
			if (tables[1].innerHTML.indexOf("Total Records Found") > -1) {
				tables[1].setAttribute("class", "subTableSearch");
			} 
		}
	}
	
	replaceEmbed();
}

//Menu
function setCategory(parent, child) {
	var lis = document.getElementById(parent).getElementsByTagName("li");
	for (var i = 0; i < lis.length; i++) {		
		if (lis[i].id == child) {
			lis[i].style.display = "inline-block";
		} else {
			if (lis[i].id != "") {			
				lis[i].style.display = "none";
			}	
		}
	}
}

//Intrabench Specific
function isUserLoggedIn() {
	var cookieValue = false;
	var searchStr = 'IBLOGIN=';
	
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(searchStr);
		if (offset != -1) {
			offset += searchStr.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			if (unescape(document.cookie.substring(offset, end)).toLowerCase() == 'true') cookieValue = true;
		}	
	}
	return cookieValue;
}

function replaceEmbed () {
	var embeds = document.getElementById("intrabench").getElementsByTagName("embed");
	for (var i = 0; i < embeds.length; i++) {	
		if (embeds[i].src.indexOf('.flv') > -1) {
			var flvSrc = embeds[i].src;
			
			embeds[i].parentNode.id = "flvvideo";
			var so = new SWFObject("http://www.voxpops.com/stateofthenation/OSplayer.swf", "FLVVideo", "320", "180", "8", "#FFFFFF");
			so.addVariable("movie", flvSrc);
			so.addVariable("btncolor", "0x000000");
			so.addVariable("accentcolor", "0x133B50");
			so.addVariable("txtcolor", "0x000000");
			so.addVariable("volume", "80");
			so.addVariable("previewimage", "http://www.voxpops.com/stateofthenation/images/OSPreview.jpg");
			so.addVariable("autoload", "on");
			so.write("flvvideo");
		}
	}
}