//Search JavaScript
function MM_findObj(n, d) { //v4.01
	var p,i,x;  
		if(!d) d=document; 
		if((p=n.indexOf("?"))>0&&parent.frames.length) {
			d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
			if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
			for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
			if(!x && d.getElementById) x=d.getElementById(n); return x;
		}

	function MM_validateForm() { //v4.0
	  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
	  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
		if (val) { nm=val.name; if ((val=val.value)!="") {
		  if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
			if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
		  } else if (test!='R') { num = parseFloat(val);
			if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
			if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
			  min=test.substring(8,p); max=test.substring(p+1);
			  if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
		} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
	  } if (errors) alert('The following error(s) occurred:\n'+errors);
	  document.MM_returnValue = (errors == '');
	}
	
	function MM_jumpMenu(targ,selObj,restore){ //v3.0
		eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	  	if (restore) selObj.selectedIndex=0;
	}

	function encode_utf8( s ) {
	  return encodeURIComponent( s );
	}
	function DoSearch() {
		var searchTerm = encode_utf8(document.getElementById("searchInput").value);
		//alert(searchTerm);
		var path = "/search/search_results.jhtml";
		path = path + "?N=0";
		path = path + "&Ntt=" + searchTerm;
		path = path + "&D=" + searchTerm;

		MM_validateForm('searchInput','','R');

		if (document.MM_returnValue == true) {
			window.location = path;
		}
		return false;
	}


function tmt_findObj(n){
  var x,t; if((n.indexOf("?"))>0&&parent.frames.length){t=n.split("?");
  x=eval("parent.frames['"+t[1]+"'].document.getElementById('"+t[0]+"')");
  }else{x=document.getElementById(n)}return x;
}

function MM_showHideLayers() {
  var i,p,v,obj,args=MM_showHideLayers.arguments;if(document.getElementById){
   for (i=0; i<(args.length-2); i+=3){ obj=tmt_findObj(args[i]);v=args[i+2];
   v=(v=='show')?'visible':(v='hide')?'hidden':v;
   if(obj)obj.style.visibility=v;}} else{
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }}
}
function toggleProductInfo() {

  var i,p,v,obj,args=toggleProductInfo.arguments;if(document.getElementById){
   for (i=0; i<(args.length-2); i+=3){ obj=tmt_findObj(args[i]);v=args[i+2];
   v=(v=='show')?'block':(v='hide')?'none':v;
   if(obj)obj.style.display=v;}} else{
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v='hide')?'none':v; }
    obj.display=v; }}
}

var submit = false;
var timeout = 300;  // 5 minutes.

// Reset 'submit' so that the form can be submitted again.
function resetTimer()
{
  window.clearTimeout(timeoutID);
  window.status = "Enter PIN";
  submit = false;
  document.login.pin.focus();
}

function checkLogin()
{

  // This is to make sure that the form is submitted only once when
  // the user hits several times on the login button.
  if (submit)
  {
    alert("Your request is being processed.\nPlease do not hit reload or refresh.\nThis request may take up to a minute to process.");
    return false;
  }
  timeoutID = window.setTimeout("resetTimer()", timeout*1000);
  submit = true;

  return true;
}

function gotoLink(form)

{

var OptionIndex=form.currentLocale.selectedIndex;

parent.location = form.currentLocale.options[OptionIndex].value;

}





sfHover = function() {
	if (document.getElementById("nav") != null) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}

sfStorehover = function() {
	if (document.getElementById("storeNav") != null) {
		var sfEls = document.getElementById("storeNav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfStorehover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfStorehover\\b"), "");
			}
		}
	}
}
ecathover = function() {
	if (document.getElementById("ecatDrop") != null) {
		var sfEls = document.getElementById("ecatDrop").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" ecathover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" ecathover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
if (window.attachEvent) window.attachEvent("onload", ecathover);
if (window.attachEvent) window.attachEvent("onload", sfStorehover);

function getCookie(Name){
	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) //if cookie found
		return document.cookie.match(re)[0].split("=")[1] //return its value
	return ""
}

function setCookie(name, value){
	document.cookie = name+"="+value
}

function buttHover(elem) {
	elem.style.backgroundPosition = '0% -25px';
	var rightEnd = elem.getElementsByTagName('input')[0];
	rightEnd.style.backgroundPosition = '100% -25px';

}

function buttOff(elem) {
	elem.style.backgroundPosition = 'top left';
	var rightEnd = elem.getElementsByTagName('input')[0];
	rightEnd.style.backgroundPosition = '100% 0%';

}



function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}

function toggle_visibility(id) {
	var e = document.getElementById(id);
	if (getStyle(document.getElementById(id), 'display') == 'block') {
		e.style.display = 'block';
	} else {
		e.style.display = 'none';
	}
	if(e.style.display == 'block') {
		e.style.display = 'none';
	} else {
		e.style.display = 'block';
	}
return false;
}

//<!--FROM FAVORITES LIST PAGE-->
//<!-- Begin
		function sureDelete(val){
		  var message = ""
		  if(val == "/de"){
			message = "Sind Sie sicher, dass Sie diese Liste löschen möchten?";
		  }else if(val == "/fr"){
			message = "Etes vous sûr de vouloir supprimer cette liste?";
		  }else if(val == "/it"){
			message = "Siete sicuri che volete cancellare questa lista ?";
		  }else{
			message = "Are you sure you want to delete this list?";
		  }
		  return confirm(message);
		}		
		
var popUpWin=0;
		function popUpWindow(URLStr, left, top, width, height)
		{
		  if(popUpWin)
		  {
			if(!popUpWin.closed) popUpWin.close();
		  }
		  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
		}

//-->

// ========================== 
// ! PROCESS URL PARAMETERS   
// ========================== 
function getURLParam(strParamName){
	var strReturn = "";
	var strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if ( 
				aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break;
			}
		}
	}
	return unescape(strReturn);
}


// -----------------PAGE MUST INCLUDE /lib/jquery/jquery.js----------------
	$(document).ready(function(){
		
		getLocale = location.pathname.match(/\S+\//);		
		if (typeof thisLocale !="undefined") {
			thisLocale = location.pathname.match(/\S+\//)[0];
		}else{
			thisLocale = "";
		}
		
		
		/*IE 6 fixes*/
		
		$(document).pngFix();
		
		function hideSelect(){
			$("select").css("visibility","hidden");
		}
		
		function showSelect(){
			$("select").css("visibility","visible");
		}

		
// ----------------MODAL DIALOG BOX IN */COMMON/HEADER.JHTML---------------	
		function loadMod(){
			$("#modBg").css({
				"opacity": "0",
				"display":"block",
				"width":document.documentElement.clientWidth,
				"height":document.documentElement.clientHeight
			});
			$("#modBg").fadeTo("fast", 0.7);
			hideSelect();
		}

		modWidth = $("#modBox").width();
		modHeight = $("#modBox").height();
		function centerMod(){
			var wWidth = $("body div:first").width();
			var cWidth = document.documentElement.clientWidth;
			var wHeight = document.documentElement.clientHeight;
 
			var portTextW = $("#port_text").width();
 			var modTop=wHeight/2-modHeight/2;
 
			$("#modBox").css({

				"top": modTop - 80,
				"left": cWidth/2-modWidth/2,
				"display":"none"
			});
			
			//only need force for IE6
			
			$("#modBg").css({
				"height": wHeight,
				"width": cWidth
			});
			
		}

 
		$("#langSelector").bind("change", function (e) {
			if ($("#langSelector").val() != '0'){
				centerMod();
				loadMod();
				$("#modBox").fadeIn("fast");
				
			} else {
				
			}
		});
		$("#mainThumb").click(function () {
			centerMod();
			loadMod();
		});
		
		$("#closeBut").click(function(){
			$("#modBg").fadeOut("fast");
			$("#modBox").fadeOut("fast");
			resetLanguage();
			showSelect();
		});
		$("#modButYes").click(function(){
			localeJump("yes");
		});
		$("#modButNo").click(function(){
			localeJump("no");
		});	

	function localeJump(remember){
		splitListVal = $("#langSelector").val();
		listVal = splitListVal.split("##");
		
		if (remember == "yes") {
			document.cookie="kmtLocale=/"+listVal[0]+"; expires=Wed, 31 Dec 2014 23:59:59 UTC; path=/;";
			parent.location.href = '/index.jhtml';
		} else {
			document.location.href=listVal[1];
		}
	}
	
	function resetLanguage() {
		var x = document.getElementById("langSelector");
		x.options[0].selected = true;
		
	}

// -----------------------------------------------------------------

	$('#checkAll, #checkAllBox').click(
		function(){
			$("td.cartItem input[type='checkbox']").attr('checked', true);
		});
		$("#unCheckAll").click(function(){
			$("td.cartItem input[type='checkbox']").attr('checked', false);
		});
	
	$("a.errorSearch").click(function(){
		$("#searchInput").css("background", "#e3fff0");
		$("#searchInput").fadeOut(200, function(){
			$("#searchInput").fadeIn(200, function(){
				$("#searchInput").fadeOut(200, function(){
					$("#searchInput").fadeIn(200, function(){
						$("#searchInput").fadeOut(200, function(){ 
							$("#searchInput").fadeIn(200, function () {
							$("#searchInput").focus();
							});
						});
					});
				});
			});
		});
	});
	
	
// =========================== 
// ! load media with inline popup   
// =========================== 
	$('.itemPath').css("visibility","hidden");
	$(".loadTheMod").css("cursor","pointer");
	$(".loadTheMod").click(function(){
		theItem = $(this).attr('rel').replace(" ","%20");
		loadMod();
		var patt = /\.\w{3}$/g;
		theExt = theItem.match(patt);
		
		if(theExt == ".flv"){
			plrWidth = "534";
			plrHeight = "339";
		}else{ //theExt == ".swf"
			plrWidth = "800";
			plrHeight = "460";
		}		
		
		
		$(".k_float").css("zIndex","100");
		$("body").append("<div id=\"ilLoader\">&nbsp;</div>");
	
		var iLdrCss = {
	        'position' : 'absolute',
	        'width' : '100%',
	        'height' : '50px',
	        'top' : '200px',
	        'z-index' : '2010',
	        'text-align' : 'center',
	        'background' : 'url(\'/images/gui/spinner-loading.gif\') center no-repeat',
	        'overflow':'visible'
	      }

		$("#ilLoader").css(iLdrCss);
		
		$("#ilLoader").load("/common/inline-fla.jhtml?item="+theItem, function(){  //load item container
			$(this).css("backgroundImage","none");
			$("#itemCont").css({
				"width": plrWidth+"px",
				"height": plrHeight+"px",
				"background": "#FFFFFF"
				});
			//alert(plrWidth);
			plroffStW = $("#itemCont object").width();
			cntrWidth = $("#IlCenter").width();
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				$("#IlCenter").prepend("<img src=\"/images/gui/closeButton.gif\" id=\"IlClsBut\" />");
			}else{
				$("#IlCenter").prepend("<img src=\"/images/gui/closeButton.png\" id=\"IlClsBut\" />");
			}
			clsButWid=$("#closeBut").width();
			clsButHgt=$("#closeBut").height();
			offsetClsButX = ((cntrWidth - plroffStW)/2)-(clsButWid/2);
			offsetClsButY = clsButHgt/2;
			var clsButCss = {
		        'position' : 'absolute',
		        'left' : (offsetClsButX)-15,
		        'top' : '-'+(offsetClsButY)-15,
		        'margin-bottom' : '-15px',
		        'cursor' : 'pointer',
		        'z-index' : '2013',
		        'display' : 'block'
			}

			$(this).find("#IlClsBut").css(clsButCss);
			$(this).find("#IlClsBut").click(function(){
				$("#modBg").fadeOut("fast");
				$("#ilLoader").remove();
				$(".k_float").css("zIndex","9999999");
				showSelect();
			});
		});
		$("ilDlgCont").fadeTo("fast",1.0)
	});
	
	// ----- Replace home getData for caseStudies
	var cases=Math.floor(Math.random()*4)+1;
	$("#outCntrHome #case:empty").load(thisLocale+"case_studies_main.jhtml?show=case"+cases);  //// remove ':empty' once all homepages are updated - #case is empty
	
	
	// --- Accordian pages from IW
	if(!$("#Accordion1").attr('tabindex')){ //temporary fix for transiton of removing spry js
		$(".AccordionPanelContent").css("display","none");
		$(".AccordionPanelTab").toggle(function(){
			$(this).parent().find(".AccordionPanelContent").slideDown("normal", function(){
				$(this).prev().css("background","#c9d8f1 url(/images/gui/tec_tips_up.gif) 555px 50% no-repeat");
				$(this).prev().css("margin-bottom", "0");
			});
		},function(){
			$(this).parent().find(".AccordionPanelContent").slideUp("normal", function(){
				$(this).prev().css("background","#FFFFFF url(/images/gui/tec_tips_down.gif) 555px 50% no-repeat");
				$(this).prev().css("margin-bottom", "2px");
			});
		});
	}
	
}); // --- END jquery ready(function()





