// hide any DOM element by ID name
function hide(element) {
	document.getElementById(element).style.display='none';
}

// show (un-hide) any DOM element by ID name
function show(element) {
	document.getElementById(element).style.display='';
}

// terms of agreement redirect
function terms(mypage, mychoice) {
	var page = mypage;
	var choice = mychoice;
	
	switch (page) {
		case "register": 
			document.cookie = "termschoice=" + choice + ";path=/";
			window.location = "processchoice.php";
			break;
		case "login":
			document.cookie = "termschoice=" + choice + ";path=/";
			window.location = "processchoice.php?section=login";
			break;
	}
}

// form validation (client side)
function validateForm(myform) {
var errorstr = "";
var whatform = document.getElementById(myform);
var myemail = document.getElementById('useremail').value;
var frmfields = whatform.elements;
	for (var i=0;i<frmfields.length;i++) {
		if (frmfields[i].getAttribute('required') && !frmfields[i].value) {
		frmfields[i].className = 'highlight';
		document.getElementById('errorreq').style.display = 'block';
		errorstr += "error";
		} else if (frmfields[i].getAttribute('required') && frmfields[i].value) {
		frmfields[i].className = 'normal';
		}
	}
	checkEmail(myemail);
	bademail = erroremail;
	if (bademail == "error") {
		document.getElementById('useremail').className = 'highlight';
		document.getElementById('erroremail').style.display = 'block';
	}
 	if ((errorstr != "") || (bademail == "error")) {
	// do not submit the form
	return false;
	} else {
	// form values are valid; submit
	whatform.submit();
	}
}

function checkEmail(myemail) {
emailaddress = myemail;
atPos = emailaddress.indexOf("@") + 1;
stopPos = emailaddress.lastIndexOf(".");
strdomain = emailaddress.slice(atPos, [stopPos]);
erroremail = "";

switch (strdomain) {
	case "siemens": erroremail = "error"; return erroremail; break;
	case "phillips": erroremail = "error"; return erroremail; break;
	default: erroremail = ""; return erroremail; break;
}


}

function showmetric(obj) {
	var fset,i;
	fset=document.getElementsByTagName('fieldset');
	for ( i=0; i<fset.length; i++) {
		if (fset[i].getAttribute('id')) {
			fset[i].style.display = "none";
		}
	}
	var option = obj.options[obj.selectedIndex].value;
	document.getElementById(option + "_set").style.display = "block";
}

// popover / mousefollow
function featurethis(prodcall) {
	
	var proddiv = "proddiv"+prodcall;
	document.getElementById(prodcall).onmouseout = function () { hide(proddiv); }
	document.getElementById(proddiv).style.position = 'absolute';
	document.getElementById(proddiv).style.width = '270px';
	document.getElementById(proddiv).style.backgroundColor= '#505050';
	document.getElementById(proddiv).style.padding = '5px';
	document.getElementById(proddiv).style.zIndex = '650';
		
	hide(proddiv);

	// update the content of the window (in aspx file)

	show(proddiv);
	
	// Simple follow the mouse script [http://javascript.about.com/library/blfollow2.htm]
	var divName = proddiv;  // div that is to follow the mouse
                       		// (must be position:absolute)
	var offX = -285;          // X offset from mouse position
	var offY = 15;          // Y offset from mouse position

	function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
	function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}

	function follow(evt) {if (document.getElementById) {var obj = document.getElementById(divName).style; obj.visibility = 'visible';
	obj.left = (parseInt(mouseX(evt))+offX) + 'px';
	obj.top = (parseInt(mouseY(evt))+offY) + 'px';}}
	document.onmousemove = follow;
}

// from http://www.quirksmode.org/js/findpos.html#
function find_position(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}



// user must click through a reminder
// in order to download anything
function checkdownload() {
var links, i;
// Loop through all anchor tags, check if they contain the class download
links=document.getElementsByTagName('a');
	for (i=0;i<links.length;i++) {
		if (/download/.test(links[i].className)) {
			// add the function popAgreement
			links[i].onclick = function() { popAgreement(this.getAttribute('id'), window.location, this.href, this.getAttribute('rel')); return false; };
		}
	}
}
function popAgreement(thisref,pageref,fileref,materialref) {
	var posdiv = document.getElementById(thisref);
	object_position = find_position(posdiv);
	
	document.getElementById('alertbox').style.left = object_position[0] + posdiv.offsetWidth + -200 + 'px';
	document.getElementById('alertbox').style.top = object_position[1] + -50 + 'px';	
	
	show('alertbox');
	
	document.cookie = "docref=" + pageref + ";path=/";
	document.cookie = "fileref=" + fileref + ";path=/";
	document.cookie = "materialref=" + materialref + ";path=/";
}
function confirmAgreement() { 
	hide('alertbox');
	window.location = "processdownload.php"; 
}




// rollover script
function findimg()
{
 var imgs,i;
// Loop through all images, check if they contain the class roll
 imgs=document.getElementsByTagName('img');
 for(i=0;i<imgs.length;i++)
 {
  if(/roll/.test(imgs[i].className))
  {
// add the function roll to the parent Element of the image
  imgs[i].parentNode.onmouseover=function(){roll(this);};
  imgs[i].parentNode.onmouseout=function(){roll(this);};
  }
 }
}
function roll(o)
{
 var i,isnode,src,ftype,newsrc,nownode;
// loop through all childNodes
 for (i=0;i<o.childNodes.length;i++)
 {
  nownode=o.childNodes[i];
// if the node is an element and an IMG set the variable and exit the loop
  if(nownode.nodeType==1 && /img/i.test(nownode.nodeName))
  {
   isnode=i;
   break;
  }
 }
// check src and do the rollover
 src = o.childNodes[isnode].src;
 ftype = src.substring(src.lastIndexOf('.'), src.length);
 if(/_o/.test(src))
 {
  newsrc = src.replace('_o','');
 }else{
  newsrc = src.replace(ftype, '_o'+ftype);
 }
 o.childNodes[isnode].src=newsrc;
}

//window.onload = function(){ findimg(); }

// Function to load multiple events onload
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}
// add functions to end of list
addLoadEvent(function() {
	findimg();
	checkdownload();
})


// browser detect to load alternate stylesheets (if necessary) quirksmode.org

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();