var prodImage = '';
var searchFieldIsActive = false;

if (!document.getElementById){
	document.getElementById = function(){return null;}
	document.getElementsByTagName = function(){return null;}
}

// 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');
if(document.all && document.getElementById){
	addOnLoad('emulateHoverPseudoClass');
	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.className.indexOf('external') == -1) continue;
			
			// 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 emulateHoverPseudoClass() {
	navRoot = document.getElementById('nav').getElementsByTagName('UL')[0];
	if(navRoot){
		for (i=0; i < navRoot.childNodes.length; i++){
			node = navRoot.childNodes[i];
			if (node.nodeName=='LI') {
				node.onmouseover=function() {
					this.className+='over';
		  		}
		  		node.onmouseout=function() {
		  			this.className=this.className.replace('over', '');
				}
			}
		}
		// Operafix, makes menu oversticky
		if(navigator.userAgent.indexOf('Opera') != -1){
			suckers = navRoot.getElementsByTagName('UL');
			for (j=0; j < suckers.length; j++){
				currentHeight = suckers[j].childNodes.length * 60;
				suckers[j].style.height = currentHeight;
				suckers[j].style.background = 'transparent';
				
			}
			document.getElementById('serviceNav').style.width = '250';
			document.getElementById('langNav').style.width = '250';
			document.getElementById('searchForm').style.width = '200';
		}
	}
}
function changeImage(strImage){
	if(document.images){
		prodImage = document.prodpic.src;
		document.prodpic.src=strImage;		
	}
}

function ChangeImageOriginal(){
	if(document.images){
		document.prodpic.src = prodImage;
	}
}


// 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;
		}
	}
}
