var searchFieldIsActive = false;

if (!document.getElementById){
	document.getElementById = function(){return null;}
	document.getElementsByTagName = function(){return null;}
}

//DOM ready
$(document).ready(function(){
	//Product Category Selector
	$("#leftnav.group0 > h2.prodGroupHeader:first, #leftnav.group1 > h2.prodGroupHeader:last").addClass('selProdGroupHeader');
	$("#leftnav .prodGroupHeader").click(function(){
		$(this).next("ul").slideToggle('fast');
		$(this).toggleClass("selProdGroupHeader");
	});
	
			
	
	
	//Function for keep living picture
	
	function placeKeepLivingImage(){
		//Get height of content
		var $r = $("#sub #related").height();
		$("#sub #content #related").height($r+114); //Always add 114px to related col height for picture
		
		var $h = $("#sub #content").height();
		if($("#sub #content #left").height()>$h)
			$h = $("#sub #content #left").height();
		if($("#sub #content #related").height()>$h)
			$h = $("#sub #content #left").height();	
		$("#sub #content #related").height($h);
		
		//Display image
		$(".absolute-bottom").show();
	}		
	
	//FAQ section
	$("dl#faq dd:gt(0)").hide();
	$("dl#faq a").click(function(){			
		$(this).parent().next("dd").slideToggle("fast").siblings('dd:visible').slideUp('fast');
		return false;
	});		
	
	placeKeepLivingImage();	
	
});

// Load functions
onLoadList = new Array();

// Example: addOnLoad('someFunction');
function addOnLoad(func){
	onLoadList[onLoadList.length] = func;
}

function runOnLoad(){
	for(i in onLoadList){
		var addPar = "";
		// alert(onLoadList[i])
		if (onLoadList[i].indexOf("(") == -1) addPar = "()";
		eval(onLoadList[i]+addPar);
	}
}

// Init
addOnLoad('clearSearch');
addOnLoad('clearSubscription');
addOnLoad('parseLinks');
addOnLoad('fixOpera');
if(document.all && document.getElementById){
	addOnLoad('SetSearchFieldIsActive');
	document.onkeydown = checkDown;
}
window.onload = runOnLoad;


function parseLinks() {
	// make sure the browser supports the object
	if (document.getElementsByTagName) {
		// get the links
		var a = document.getElementsByTagName('a');
		
		// loop through each link (negatively for increased performance)
		for (var i=a.length;i!=0;i--) {
		
			// make alias variable to ease fingers
			var c = a[i-1];
			
			// make sure URL has a href
			if (!c.href) continue;
			
			// convert www links to externals, docs, pdfs
								
			if((c.target.indexOf('_blank') != -1 || c.href.indexOf('.txt') != -1) && !isImgLink(c))c.className='external';
			if(c.href.indexOf('.doc') != -1 && !isImgLink(c))c.className='externalDoc';
			if(c.href.indexOf('.pdf') != -1 && !isImgLink(c))c.className='externalPdf';
			//if(c.href.indexOf('mailto:') == -1 && !isImgLink(c))c.className='arrow';
			
			
			if (c.className.indexOf('external') == -1) continue;
			
			c.target = '_blank';
			
			// the handler that handles clicking
			/*
			c.onclick = function() {
			
				// open the window
				// var win = window.open(this.href,'','width=530,height=450,scrollbars=1,toolbar=1,status=1,resizable=1');
				var win = window.open(this.href);
				// if the window doesn't open, continue normally
				if (win) return false;
			}
			*/
		}
	}
}


//But not if imgLink
function isImgLink(obj){
	var imgLink = false;
	if(obj.childNodes.length > 0){
		for(i=0;i < obj.childNodes.length;i++){
			if(obj.childNodes[i].nodeName=='IMG'){
				imgLink=true;
				break;
			}
		}	
	}
	return imgLink;
}


//checks input field for valid e-mail, new toplvl's allowed (ie. info). alert message set by <input type="hidden" name="strTxt"> for language versioning
function validateEmail(strValue,strTxt) {
	var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;
  	if(objRegExp.test(strValue)){return true;}
  	else{alert(strTxt);return false;}
}

function clearSearch(){
	var sf = (document.getElementById('searchField'))?document.getElementById('searchField'):null;
	if(sf){
		sf.onfocus = function(){
			if(sf.value=='Search'){sf.value=''}
		}
	}
}

function clearSubscription(){
	var wName = (document.getElementById('idName'))?document.getElementById('idName'):null;
	var wAddress = (document.getElementById('idAddress'))?document.getElementById('idAddress'):null;
	var wZip = (document.getElementById('idZip'))?document.getElementById('idZip'):null;
	var wCountry = (document.getElementById('idCountry'))?document.getElementById('idCountry'):null;
	if(wName){
		wName.onfocus = function(){
			if(wName.value!=''){
				wName.value = '';
			}
		}
	}
	if(wAddress){
		wAddress.onfocus = function(){
			if(wAddress.value!=''){
				wAddress.value = '';
			}
		}
	}	
	if(wZip){
		wZip.onfocus = function(){
			if(wZip.value!=''){
				wZip.value = '';
			}
		}
	}		
	if(wCountry){
		wCountry.onfocus = function(){
			if(wCountry.value!=''){
				wCountry.value = '';
			}
		}
	}		
}
function fixOpera(){
	if(navigator.userAgent.indexOf('Opera') != -1){
		document.getElementById('serviceNav').style.width = '250';
		document.getElementById('langNav').style.width = '200';
		document.getElementById('searchForm').style.width = '200';
	}
}

function setGroup(obj){
	obj.parentNode.className = obj.className;
}

// Go search on enter

function SetSearchFieldIsActive(){
	document.getElementById('TextBox_search').onfocus = function(){
		searchFieldIsActive = true;
	}
	document.getElementById('TextBox_search').onblur = function(){
		searchFieldIsActive = false;
	}
}

function goSearch(){
	searchStr = document.getElementById('TextBox_search').value;
	if(searchStr != ''){
		searchUrl = document.getElementById('searchUrl').value;
		location.href = searchUrl + 'searchS?QUERY=' + searchStr;
	}
}

function checkDown(e) {
	if(searchFieldIsActive){
		var ieKey=event.keyCode; 
		if (ieKey == 13){
			goSearch();
			return false;
		}
	}
}
