/**
* @author Coen van der Maade
* @version $Id: global.js,v 1.31 2007/11/06 09:22:51 jkennedy Exp $
*/

/* Functionality to toggle on and off the checkboxes in the club_philips_my_email_preferences fragment */
function toggleOverallInterest() {
  var forms = document.getElementsByTagName("form");
  for (var i=0; i<forms.length; i++){
	if (forms[i].name == "mySubscriptions.form"){
		var form_name = "mySubscriptions.form";
	}
	else{
		var form_name = "registration";
	}
  }
  if (document[form_name].interestConsumerElectronics.checked ||
      document[form_name].interestHouseholdProducts.checked   ||
      document[form_name].interestPersonalCareMale.checked    ||
      document[form_name].interestPersonalCareFemale.checked  ||
      document[form_name].interestKitchenAppliances.checked)
      { (document[form_name].interestNonStreamium.checked = true);}
  else {
  (document[form_name].interestNonStreamium.checked = false);
  }    
}

function toggleInterestNonStreamium() {
  var forms = document.getElementsByTagName("form");
  for (var i=0; i<forms.length; i++){
	if (forms[i].name == "mySubscriptions.form"){
		var form_name = "mySubscriptions.form";
	}
	else{
		var form_name = "registration";
	}
  }
  state = document[form_name].interestNonStreamium.checked;
  document[form_name].interestConsumerElectronics.checked = state;
  document[form_name].interestHouseholdProducts.checked = state;
  document[form_name].interestPersonalCareMale.checked = state;
  document[form_name].interestPersonalCareFemale.checked = state;
  document[form_name].interestKitchenAppliances.checked = state;
}
/* End functionality to toggle on and off the checkboxes in the club_philips_my_email_preferences fragment */

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return "";
}


function updateLayout(){
	centerOnScreen();
	hideAllMenus();
	sZ(gE('nav'),1)
	sE(gE("container"));
	sE(gE("head"));
}

function centerOnScreen(){
	var containerDiv = gE('container');
	var headerDiv = gE('head');

	var x = getInnerWidth(self)/2 - 385;
	sX(containerDiv, x);
	sX(headerDiv, x);
}

function getInnerWidth(win) {
  var winWidth;
  if (document.compatMode == "CSS2Compat" || navigator.userAgent.indexOf('Gecko') != -1) {
    winWidth = parseInt(win.document.defaultView.getComputedStyle(document.documentElement, null).getPropertyValue("width"));
  }
  else if (document.compatMode == "CSS1Compat") {
    winWidth = win.document.documentElement.clientWidth;
  }
  else if (navigator.appName == 'Netscape') {
    winWidth = win.innerWidth;
  }
  else {
    winWidth = win.document.body.clientWidth;
  }
  return winWidth;
}

function getInnerHeight(win){
  var winHeight;

  if (document.compatMode == "CSS2Compat" || navigator.userAgent.indexOf('Gecko') != -1) {
	winHeight = parseInt(win.document.defaultView.getComputedStyle(document.documentElement, null).getPropertyValue("height"));
  }
  else if (document.compatMode == "CSS1Compat") {
    winHeight = win.document.documentElement.clientHeight;
  }
  else if (navigator.appName == 'Netscape') {

    winHeight = win.innerHeight;
  }
  else {
    winHeight = win.document.body.clientHeight;
  }
  return winHeight;
}


function getOffsetTop(anchorName, layerRef, deep) {
	if (document.layers){
		return layerRef.document.anchors[anchorName].y;
   }
	else {
      if (document.all) {
         var element = document.all[anchorName];
      }
      else {
         element = document.getElementById(anchorName);
      }
		var offsetTop = 0;

      do {
         offsetTop += element.offsetTop;
         element = element.offsetParent;
      } while (deep == true && element != document.body && element != null);
      return offsetTop;
	}
}

function getOffsetWidth(anchorName, layerRef, deep) {
	if (document.layers){
		return layerRef.document.anchors[anchorName].x;
   }
	else {
      if (document.all) {
         var element = document.all[anchorName];
      }
      else {
         element = document.getElementById(anchorName);
      }
		var offsetWidth = 0;

      do {
         offsetWidth += element.offsetWidth;
         element = element.offsetParent;
      } while (deep == true && element != document.body && element != null);
      return offsetWidth;
	}
}

function testOut(){
	var out = ""
	for(x=0;x<testOut.arguments.length;x++){
		out += testOut.arguments[x] + ": ";
		out += eval(testOut.arguments[x]) + "\n ";
	}
	wH(gE("testdiv"), "<span style=\"color:green\">" + out + "</span>");
}

function getSessionIdFromURL(){
  var sessionid="";
  if (document.cookie.indexOf("jsessionid") == -1) {
    var re = new RegExp(/(;jsessionid=\w+)[;?].*/);
    var m = re.exec(document.location.href);
    if (m!=null && m.length>0){
      sessionid = m[1];
    }
  }
  return sessionid;
}

function getCatalogLocaleWhenCookiesDisabled(){
  var result = "";
  if (document.cookie.indexOf("jsessionid") == -1) {
    var re = new RegExp(/(country=\w+).*/);
    var m = re.exec (document.location.href);
    if (m!=null && m.length>0){ result +=m[1];}
    re = new RegExp(/(language=\w+).*/);
    m = re.exec (document.location.href);
    if (m!=null && m.length>0){result +="&"+m[1];}
    re = new RegExp(/(catalogType=\w+).*/);
    m = re.exec (document.location.href);
    if (m!=null && m.length>0){result +="&"+m[1];}
  }
  return result;
}

function openNewWindow(url){
	window.open(url);
}
/* Check if there is a shopping cart then open a new popup window with the given URL*/
/* TODO: Check is this function is used because check is not necessary because of popup basket stays available in originale window.*/
function checkCartStatus(url){
	if(document.getElementById("leaveDomain")){
		if(!confirm(document.getElementById("leaveDomain").innerHTML)){
			return;
		}
		 openNewWindow(url); 
	}
	 openNewWindow(url); 
}


/* 
	Check if the language or country changes. 
*/
function checkForCountryLanguageSwitch(open_link){
	/*	If the to link contains 'setLocale' the country/language dropdown is used so the country or language changes
		return false because the function setlocale in the lib_global.js will call the switchHandler again and then the empty
		basket popup will show. So we don't have to show it now
	*/
	if(open_link.match("setlocale")!= null){
		return false;
	}
	/* 
		Compare the country and language parameters in the present URL with the country and language parameters in the target URL.
		If they are the same return false otherwise return true.
		Get the country and language from the target URL.
	 */
	if(open_link.indexOf("?")!=-1){
		/* Get the country and language from the target URL. */
		var toQuery = open_link.split("?");
		var toQueryParams = toQuery[1].split("&");
		for (var i = 0; i < toQueryParams.length; i++){
			var pos = toQueryParams[i].indexOf('=');
			var toLanguage;
			var toCountry
			if (pos != -1 && (toQueryParams[i].match("language") || toQueryParams[i].match("country"))){
				if (toQueryParams[i].substring(0,pos) == "language"){toLanguage = toQueryParams[i].substring(pos+1);}
				if (toQueryParams[i].substring(0,pos) == "country"){toCountry = toQueryParams[i].substring(pos+1);}
			}
			/* 
				If no language and country available in the query param try to get them using _page.getlocale() from lib_global.js
				Added to also support the CPEP site (since CPEP doesn't have a country and language in the query params.)
			*/
			else if(toLanguage == null || toCountry == null){
				var locale = _page.getlocale();
				var pos = locale.indexOf('_');
				toCountry = locale.substring(0, pos).toUpperCase();
				toLanguage = locale.substring(pos+1);
			}
		}
		/* Get the country and language from the current URL. */
		var orgQuery = location.search.substring(1);
		var orgQueryParams = orgQuery.split("&");
		for (var i = 0; i < orgQueryParams.length; i++){
			var pos = orgQueryParams[i].indexOf('=');
			var orgLanguage;
			var orgCountry;
			if (pos != -1 && (orgQueryParams[i].match("language") || orgQueryParams[i].match("country"))){
				if (orgQueryParams[i].substring(0,pos) == "language"){orgLanguage = orgQueryParams[i].substring(pos+1);}
				if (orgQueryParams[i].substring(0,pos) == "country"){orgCountry = orgQueryParams[i].substring(pos+1);}
			}
			/* 
				If no language and country available in the query param try to get them from the cookie	(catalogLocale)
				Added to also support the CPEP site (since CPEP doesn't have a country and language in the query params.)
			*/
			else if(orgLanguage == null || orgCountry == null){
				var start = document.cookie.indexOf("catalogLocale=");
				if(start!=-1){
					start += 14;
					var end = start +5;
					locale=document.cookie.substring(start,end);
					var pos = locale.indexOf('_');
					orgCountry = locale.substring(0, pos).toUpperCase();
					orgLanguage = locale.substring(pos+1);
				}
			}
		}
		/* Compare the language and country. */
		if(toLanguage != null && orgLanguage != null && toCountry  != null && orgCountry != null){
			if (toLanguage != orgLanguage || toCountry != orgCountry){
				return true;
			}
			else {
				return false;
			}
		}
	}
	/*
		When the target URL does not contain 'setLocale' and does not have any parameters we return false.
		There might be extra checks (for new links) added in the future.
	*/
	else{
		return false;
	}
}

/* Check of given URL goes outside the /consumer domain or not. */
function checkIfExternalLink(open_link){
	/* First check if the link stays in the total philips domain */
	/* _page.setlocale and /ConsumerLanding.go are special links used by the consumersite */
	if (open_link.match("philips.com") || open_link.match("_page.setlocale") || open_link.match("/ConsumerLanding.go") || open_link.match("/Group.go") || open_link.match("/ClubPhilips.go")){
		/* Next check if the link stays in the consumer or entertainment domain */
		/* gdc1.ce and added for testing purposes and can be either extended or removed in the future */
		if (open_link.match("consumer") || open_link.match("entertainment") || open_link.match("gdc1.ce") || open_link.match("nlyehvsce5ws") 
		|| open_link.match("_page.setlocale") || open_link.match("/ConsumerLanding.go") || open_link.match("/Group.go") || open_link.match("/ClubPhilips.go")){
			
			return false;
		}
		
		return true;
	}else{
	
		return true;
	}
}

/* Explicitly empty the shopping cart.*/
function emptyCart(){
	var form = document.getElementById('emptyBasket');
	if (form != null){
	 form.submit();
	}
}

/*	Override of the GBM switchHandler */ 
function EplatformSwitchhandler(open_link, target, language_switch){
	/* 
		EPlatform code, checks if the shopping cart has items, if the link is external and if the target is not a new window.
		If all those are true it shows a confirm box warning the user his cart will be emptied if he continues.
	 */
	if(document.getElementById("shoppingCartActive") && document.getElementById("leaveDomain") && (target == "" || target =="_self")){
		/* Check if the target link is an external link. */
		if(checkIfExternalLink(open_link)){
			var message = "leaveDomain";
		}
		/* 
			Check if the country or the language changes when moving to target link. 
			If there is already a message available use that one.
		*/
		if (checkForCountryLanguageSwitch(open_link) && message !="leaveDomain") {
			var message = "shoppingCartActive";
		}	
		/* If message not empty show pop-up. */
		if (message && message != ""){
			if(!confirm(document.getElementById(message).innerHTML)){
				return false;
			}
		/* When popup is confirmed empty the shopping cart.. */	
		emptyCart();
		}
	}
	/* GBM code from the original switchHandler */
	// Check if there is a language switch
	if(language_switch=="" || typeof(language_switch)=="undefined"){
		open_page = true;
	}else{
		// Check if person wants to switch language
		if(confirm(_page.text["confirmation2"].replace("{LANGUAGE}", language_switch))){
			open_page = true;
		}else{
			open_page = false;
		}
	}
	if(open_page){
		if(target=="" || typeof(target)=="undefined"){
			// Open in same window if no extra parameters are send
			target = "_self";
			extra = "";
		}else if(target=="popup"){
			target = "_blank";
			extra = "height=500,width=700,toolbar=yes,scrollbars=yes";
		}else{
			extra="";
		}
		w=window.open(open_link, target, extra);
	
		if(target=="popup"){
			//Center popup window
			var intwidt;
			var intheight;
			intwidth=screen.availWidth;
			intheight=screen.availHeight;
			intwidth=parseInt(intwidth);
			intheight=parseInt(intheight);
			if(intwidth>0&&intheight>0){
				w.moveTo(((intwidth-popup_width) / 2), ((intheight - popup_height) / 2));
				w.focus();
			}
		}
	}else{
		// Don't follow link
		//return false;
	}
	/* END OF: GBM code from the original switchHandler */
	return false;
}	
_page.switchHandler = EplatformSwitchhandler

/*	END OF: Overrides the GBM switchHandler to add a check if the cart is empty before moving another localation, 
 *	if it is not, gives the option of clearing the cart to continue or cancelling the request.
*/ 

//function search(){
//	var form = document.getElementById("searchform");
//	var inputs = form.getElementsByTagName("input");
//	for (var i=0; i<inputs.length; i++){
//		if (inputs[i].name == "q"){
//			search_value = inputs[i].value;
//		}
//		else if (inputs[i].name == "s"){
//			search_division = inputs[i].value;
//		}
//	}
//	/*var search_division = document.getElementById("s").value;*/
//	/* encode the search value as it might contain special characters, which don't work in IE */
//	var encoded_search_value = encodeURI(search_value);
//	query = "q=";
//	query += encoded_search_value;
//	query += "&s=";
//	query += search_division;
//	query += "&l=";
//	if (getQueryVariable("country") != "" || getQueryVariable("language") !=""){	
//		query += getQueryVariable("country").toLowerCase() + "_" + getQueryVariable("language").toLowerCase();
//	}
//	else{
//		query += "global";
//	}
//	query += "&language=";
//	query += getQueryVariable("language").toLowerCase();
//	query += "&country=";
//	query += getQueryVariable("country").toUpperCase();
//	query += "&catalogType=";
//	query += getQueryVariable("catalogType");
//	
//	window.location= "/c/search/search_results.jsp?" + query;
//}
//_page.searchHandler = search

/*
 * Some vars and functions useful for displaying expiry/start dates for cards
 *
 */
today = new Date();
months = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
year = today.getFullYear();
years  = new Array(year-5,year-4, year-3, year-2, year-1, year, year+1, year+2, year+3, year+4, year+5);
		
// take an array and resort it so that the supplied split point is the
// first item in the array, with the items before the split point
// added behind
// mode determines what to return - 0 returns concat array, -1 returns
// slice before the splitpoint, and 1 returns the after slice
function resort(array, splitpoint, mode){
	var start = array.slice(splitpoint);
	var finish = array.slice(0, splitpoint);
	var returnable = mode == 0 ? start.concat(finish):mode > 0 ? start:finish;
	return returnable;
}
// takes an array of (months or years), and writes them out as a string
// of options, whose value and code is identical
function writeOptions(array, selected){
	var optionsString = "";
	for(var index = 0; index < array.length; index++ ){
		optionsString+="<option ";
		if( array[index] == selected ){
			optionsString+="selected "
		}
		optionsString+="value=\""+array[index]+"\" >"+array[index]+"</option>";
	}
	return optionsString;
}

//This was added for the Brazil "pop-up" banners that were included for CPEP rel 3
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

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_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  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 processSubmitBanner(id,url,myForm) {

     if (myForm == 'banner1') {

         document.forms.banner1.serviceId.value=id;   
   	     document.forms.banner1.serviceUrl.value=url; 		   		
   	     document.forms.banner1.submit();
     }
     if (myForm == 'banner2') {

         document.forms.banner2.serviceId.value=id;   
   	     document.forms.banner2.serviceUrl.value=url; 		   		
   	     document.forms.banner2.submit();
     }

   	
}


/*	THE CODE BELOW USE JQURY JAVASCRIPT LIBRARY !!!!!!

	Check www.jquery.com for the latest version to download and to see 
	documentation on how to use.
*/
 
/* 
	Iterates through elements (with the class/id pattern passed as param) 
	and returns the value of the tallest (height) element.
	This can then be used (by the following function) to set ALL of the 
	specified elements to the same height so we don`t have layout issues.
*/
function getHighestElement(Element) {
	var tallestElement = 0;
	$(Element).each(function(i) {
		var currElemHeight = $(this).height();
		if(currElemHeight > tallestElement){
			tallestElement = currElemHeight;
		}
	})
	return tallestElement;
}
/*
	This function uses the above funtion to get the highest pixel value for
	the passed element. It then sets ALL of those elements in the page to the 
	same height.
*/
function setHighestDiv(Element) {
	var heightToSet = getHighestElement(Element);
	$(Element).each(function(i) {
		$(this).height(heightToSet);
	})
}

// Used to hide the popup layer on the product detail page
function pceHideMe(layer) { 
	$(layer).fadeOut("slow");
	$(layer).remove();	
}
// Below: functions used in series page
function showallseries(obj1,obj2)
{
 var objdetail1 = document.getElementById(obj1);
 var objdetail2 = document.getElementById(obj2);
  objdetail2.style.display = 'block';
  objdetail1.style.display = 'none';
}
function pcePopuplyr(arg1,arg2,arg3) {
	pageToLoad = "/c/common/fragments/pLayer_glossary.jsp";
	/*
		if glassary-popuplayer exists, clear content and load new.
	*/
	var glossaryLayer = $("#glossary-popuplayer");
	if($(glossaryLayer).length > 0) {
		glossaryLayer.remove();
	}
	$("body").append("<div id='glossary-popuplayer'></div>");
	$("#glossary-popuplayer").load(pageToLoad,
		{	titletext:arg1,
			imageurl:arg2,
			bodytext:arg3
		})	
	if($.browser.safari) {
		$( function() { $("#glossary-popuplayer").show(); } ); 
	} else {
		$("#glossary-popuplayer").fadeIn(50);
	}
    centerPosition('#glossary-popuplayer', 200)
}

function centerPosition(objlyr, topPos)
{
	var screenCenter = $('body').width() / 2;
	var centerPos = screenCenter -  ($(objlyr).width()/ 2);
   	$(objlyr).css({left:centerPos, top:topPos});
   	window.scrollTo(0,topPos - 50);
}
function closeit() {
	pceHideMe('#glossary-popuplayer');
	$("#glossary-popuplayer").remove();
}
// Above: functions used in series page

/* 
* 	Positions the where to buy button
*  	Takes the current width of the page and the button and calculates where it should sit on the right hand side.
*  	Also, moves the button down in the page if there is a header banner in the page.
*/ 

var alreadyPositioned = false;
function positionWhereToBuy() {
	if(!alreadyPositioned) {
		var j = jQuery.noConflict();
		var minimumBuyWidth = 170;
		var where2buyTop = 100;
		var pageStartPos = $("#p-body-content").offset().left;
		var pagewidth = $("#p-body-content").width();
		var where2buyButtonWidth = $(".where2buy").width();
		if(where2buyButtonWidth < minimumBuyWidth) {
			where2buyButtonWidth = minimumBuyWidth;
			$(".where2buy").width(minimumBuyWidth);
		}
		var where2buyLeft = (pageStartPos + pagewidth) - (where2buyButtonWidth + 15);
		if($(".headerItem").size() > 0 ) { where2buyTop = 152 }
		$(".where2buy>dd").css({width:where2buyButtonWidth});
		$(".where2buy").fadeIn(100).css({left:where2buyLeft, top:where2buyTop});
		
		// if price is in page, position it!
		if($("#productsummary p.sugRetailPrice").is("p")) {
			positionPrice(where2buyLeft, where2buyTop);
		}
		alreadyPositioned = true;
	}
}

function positionPrice(where2buyLeft, where2buyTop) {
	var priceWidth = $("#productsummary p.sugRetailPrice").width();
	var posLeft = where2buyLeft - (priceWidth + 15);
	if(j.browser.mozilla || j.browser.safari) {
		var posTop = where2buyTop-8;
	} else {
		var posTop = where2buyTop + 3;
	}
	$("#productsummary p.sugRetailPrice").show().fadeIn(3000).css({left:posLeft, top:posTop});
}

// closes the movie window
function closeMovie() {
	$("#movieLayer").fadeOut("slow");
	$("#movieLayer").remove();
}

// closes the 360view window
function close360view() {
	$("#viewLayer360").fadeOut("slow");
	$("#viewLayer360").remove();
}

// position where to buy for product detail page
function positionPdpWhereToBuy() {
	if(!alreadyPositioned) {
		var minimumBuyWidth = 170;
		var where2buyTop = 100;
		var pageStartPos = $("#p-body-content").offset().left;
		var pagewidth = $("#p-body-content").width();
		var where2buyButtonWidth = $(".pdpwhere2buy").width();
		if(where2buyButtonWidth < minimumBuyWidth) {
			where2buyButtonWidth = minimumBuyWidth;
			$(".pdpwhere2buy").width(minimumBuyWidth);
		}
		var where2buyLeft = (pageStartPos + pagewidth) - (where2buyButtonWidth + 15);
		if($(".headerItem").size() > 0 ) { where2buyTop = 152 }
		$(".pdpwhere2buy>dd").css({width:where2buyButtonWidth});
		$(".pdpwhere2buy").fadeIn(100).css({left:where2buyLeft, top:where2buyTop});
		
		// if price is in page, position it!
		if($("#productsummary p.sugRetailPrice").is("p")) {
			positionPrice(where2buyLeft, where2buyTop);
		}
		alreadyPositioned = true;
	}
}

// closes the Buy layer window
		function closeBuy() { 
			jQuery("#buyLayer").fadeOut("slow", function() {
				jQuery("#buyLayer").remove();
			});
		}

// checking the PDP bottom active
var pdpactvBuybutton = false;
function chkbuybtn(optn) {
	if (optn=='1') {
		pdpactvBuybutton = true;
	}
	else {
		pdpactvBuybutton = false;
	}
	return;
}

// checking the Decision page bottom alignment
var dpBuyPositioned = false;
var divelemtopposition, pdtlyrid, pdtlyrhght
function dpBuylyrpositon(optn,postnval,lyrid) {
	if (optn=='1') {
		dpBuyPositioned = true;
		divelemtopposition=postnval;
		pdtlyrid = lyrid.id
		pdtlyrhght = lyrid.offsetHeight
	}
	else {
		dpBuyPositioned = false;
		divelemtopposition=postnval;
	}
	return;
}

//sets page output preference for this session to HTML, suppresses flash version check and closes popup layer
function stopFlashDetection() {
	$.post("/c/common/fragments/page_output_preference.jsp",  { pageOutputPreference: "FLASH", offerFlashUpgrade: "false" });
	layer="#flashcheck"
	if($.browser.msie || $.browser.safari) {
		$( function() { $(layer).hide(); } ); 
	} else {
		$(layer).fadeOut("slow");
	}
}

//enables future flash version checks in this session and opens flash update page
function goFlashUrl()
{
	window.open('http://www.adobe.com/go/getflashplayer','','');	
	layer="#flashcheck"
	if($.browser.msie || $.browser.safari) {
		$( function() { $(layer).hide(); } ); 
	} else {
		$(layer).fadeOut("slow");
	}
	$.post("/c/common/fragments/page_output_preference.jsp",  { pageOutputPreference: "FLASH", offerFlashUpgrade: "true"});
}

//enables future flash version checks in this session
function setOutputPreferenceFlash() {
	$.post("/c/common/fragments/page_output_preference.jsp",  { pageOutputPreference: "FLASH", offerFlashUpgrade: "true" });		
}

//disables future flash version checks in this session
function setOutputPreferenceHtml() {
	$.post("/c/common/fragments/page_output_preference.jsp",  { pageOutputPreference: "HTML", offerFlashUpgrade: "false" });	
}

// load movie into new window
function loadMovie( jspToLoad, closeCaption, itemType, productid, featureid, language, country, catalogType, assetId ) {
	loadContentIntoMovieWindow(jspToLoad, {"itemType":itemType, "Close":closeCaption, "productid":productid, "featureid":featureid, "language":language, "country":country, "catalogType":catalogType, "assetId":assetId } );
}

function loadGallery(jspToLoad, pScene7AssetId, pInitialAssetIndex, pProductid, pClose) {
	loadContentIntoMovieWindow(jspToLoad, {"scene7AssetId":pScene7AssetId, "initialAssetIndex":pInitialAssetIndex, "productid":pProductid, "Close":pClose })
}

function onlineshopurl(obj,txt) {
	var storedetail = txt.innerHTML;
	if (storedetail.indexOf("Philips")!=-1) {
		window.location.href=obj;
	}
	else {
	window.open(obj,'','');
	}
}                                                                                                                                                                                                                          



















// if inpage navigation block is in page, set click event so that we can hide the buy layer if it's open
if(document.getElementById("ipnNavigation")) {
	$("#ipnNavigation").mousedown(function(){
		hideOpenLayers();
	});
}


function hideOpenLayers() {
	
	if( $("#popupWindow").length > 0 ) $("#popupWindow").remove();
	if( $("#buyLayer").length > 0 ) $("#buyLayer").remove();
	if( $("#movieLayer").length > 0 ) $("#movieLayer").remove();
	if( $("#viewLayer360").length > 0 ) $("#viewLayer360").remove();
	if( $(".popuplayer").length > 0 ) $(".popuplayer").remove();
	if( $(".popuplayer2").length > 0 ) $(".popuplayer2").remove();
	
}

function loadLayer(oElemClicked, pUrlToLoad, aPassedParams) {
	
	// remove possible open buy layer before creating new one
	if( $(".popuplayer").length < 1 ) {
		
		// fade any open layers out
		hideOpenLayers();
		
		// append popup layer to DOM and load page into it
		$("body").append("<div class='popuplayer' id='popuplyr'></div>");
		centerPosition('.popuplayer', 200);
		$.get( pUrlToLoad, aPassedParams, 
			function(data) {
				$(".popuplayer").append(data).fadeIn(500);
			}
		);
		} else {
			 $(".popuplayer").remove();
			 return false;
		}
}

function loadDownloadLayer(oElemClicked, pUrlToLoad, aPassedParams) {
	if( $(".popuplayer2").length < 1 ) { 
		// fadeout any open layers
		hideOpenLayers();
		var vTop = $(oElemClicked).offset().top - 25;
		var vLeft = $(oElemClicked).offset().left - 400;
		// append popup layer to DOM and load page into it
		$("body").append("<div class='popuplayer2' id='popuplyr2'></div>");
		$(".popuplayer2").css({left:vLeft, top:vTop});
		$.get( pUrlToLoad, aPassedParams, 
			function(data) {
				$(".popuplayer2").append(data).fadeIn(500);
			}
		);
	} else {
		$(".popuplayer2").remove(); 
		return false;
	}
}

function loadDetailBuyLayer(oElemClicked, aPassedParams) {	

	if( $("#buyLayer").length < 1 ) {
		// fadeout any open layers
		hideOpenLayers();
		$(document.body).append("<div id='buyLayer'><div class='genericbuyLayer' id='buyLayerContent'><div class='localheader clearfix'><span style='padding-top:3px;'>Buy</span><span class='close'><a href='javascript:void(0)' onclick='closeBuy()'>Close</a><a href='#' onclick='closeBuy()'><img src='/consumerfiles/assets/img/where2buyClose.gif' /></a></span></div><div id='progress'><img src='/consumerfiles/assets/img/progress_bar.gif' vspace='10' /><br>Loading</div><div class='bottom clearfix'></div></div></div>");

		$.get("/c/common/fragments/buyLayer.jsp", aPassedParams,
			function(data){
				$("#buyLayer").empty().append(data).fadeIn(500);
			}
		);
		var vLeft = $(oElemClicked).offset().left - 210;
		if(oElemClicked.id == 'buy2') {
			var vLeft = $(oElemClicked).offset().left - 225;
			var vTop = $(oElemClicked).offset().top - $("#buyLayer").height(); 
		} else {
			var vLeft = $(oElemClicked).offset().left - 230;
			var vTop = $(oElemClicked).offset().top - 5;
		}
		$("#buyLayer").css({left:vLeft, top:vTop});
	} else {
		$("#buyLayer").remove();
		return false;
	}
}
function loadCampaignBuyLayer(oElemClicked, aPassedParams) {	
	if( $("#buyLayer").length < 1 ) {
		// fadeout any open layers
		hideOpenLayers();
		
		$("body").append("<div id='buyLayer'><div class='genericbuyLayer' id='buyLayerContent'><div class='localheader clearfix'><span style='padding-top:3px;'>Buy</span><span class='close'><a href='javascript:void(0)' onclick='closeBuy()'>Close</a><a href='#' onclick='closeBuy()'><img src='/consumerfiles/assets/img/where2buyClose.gif' /></a></span></div><div id='progress'><img src='/consumerfiles/assets/img/progress_bar.gif' vspace='10' /><br>Loading</div><div class='bottom clearfix'></div></div></div>");
		$.get("/c/common/fragments/buyLayer.jsp", aPassedParams,
			function(data){
				$("#buyLayer").empty().append(data).fadeIn(500);
			}
		);
		var vLeft = $(oElemClicked).offset().left - 10;
		if(oElemClicked.id == 'buy2') {
			var vTop = $(oElemClicked).offset().top - $("#buyLayer").height(); 
		} else {
			var vTop = $(oElemClicked).offset().top - 5;
		}
		$("#buyLayer").css({left:vLeft, top:vTop});
	} else {
		$("#buyLayer").remove();
		return false;
	}
}

function changeTabAndShowFeature(obj) {
	// hide open layers before opening this one.
	hideOpenLayers();
	var featureID = obj.id.substring(4);
	$("#tabscontent").children().hide();
	$("#tab_pce_productdetails > li").removeClass("current");
	$("#tab_features").addClass("current");
	$("#tab_features_content").show();
	showFeature(featureID);
	var featureTop = $("."+featureID).offset().top;
	window.scroll(0,featureTop-20);
}

function showFeature(featureid) {
	// hide open layers before opening this one.
	hideOpenLayers();
	
	$(".pce_features table").removeClass('pce_featureTable');
	$(".pce_features table").addClass('hidden');
	$(".pce_features a").removeClass('open');
	$(".pce_features a").addClass('close');
	
	$(".pce_features ."+featureid).removeClass('hidden');
	$(".pce_features ."+featureid).addClass('pce_featureTable');
	$(".pce_features ."+featureid+" a").removeClass('close');
	$(".pce_features ."+featureid+" a").addClass('open');
}

// product detail page tab switcher
function switchDetailPageTabs(oElemClicked, reposWindow) {
	hideOpenLayers();
	if($(oElemClicked).attr("class") != "current" 
		&& $(oElemClicked).attr("id") != "back" 
		&& $(oElemClicked).attr("id") != "support" 
		&& $(oElemClicked).attr("id") != "buy-button"
		&& $(oElemClicked).attr("id") != "pricing" 
		&& $(oElemClicked).attr("id") != "experience") {
			$("#tabscontent").children().hide();
			$("#tab_pce_productdetails > li").removeClass("current");
			$(oElemClicked).addClass("current");
			var currentTabContent = "#" + $(oElemClicked).attr("id") + "_content";
			$(currentTabContent).show();
		if(reposWindow) {window.scrollTo(0,0);}
	}
}

function showSpecFeature(obj) {
	var className = obj.className;
	if(className == 'featureTable_pce close'){
		$(obj).parent().parent().parent().parent().removeClass('hidden');
		$(obj).parent().parent().parent().parent().addClass('pce_featureTable');
		$(obj).removeClass('featureTable_pce close');
		$(obj).addClass('featureTable_pce open');
	}
	if(className == 'featureTable_pce open'){
		$(obj).parent().parent().parent().parent().removeClass('pce_featureTable');
		$(obj).parent().parent().parent().parent().addClass('hidden');
		$(obj).removeClass('featureTable_pce open');
		$(obj).addClass('featureTable_pce close');
	}

}

// used on the product detail specs tab
function changeGalleryImage(imageToReplace, replacementImage) {
	$("#" + imageToReplace).fadeOut("fast", function() {
		$("#" + imageToReplace).attr("src",replacementImage).fadeIn("slow");
	});
}

// submit form if model number is not empty
function submitIfNotEmpty(){
	if( $("#modelNumber").value != '' ) { 
		$("#groupId").disabled = true;
	}
	$("#drillerKillerForm").submit();
}

// function to submit for if user hits enter key
function checkEnter(e){ 
	var characterCode
	if(e && e.which){ 
		e = e
		characterCode = e.which 
	} else {
		e = event
		characterCode = e.keyCode
	}
	if(characterCode == 13){ 
		submitIfNotEmpty();
	}
}

// clear form field
var focuscount = 0;
function clearfield(obj) {
	formfield = obj;
	if(focuscount > 0) {
		formfield.select();
	} else {
		focuscount += 1;
		formfield.value = "";
	}
}

// This function works with swfobject so that library must be loaded before this will work
function isFlashVersionAboveOrEqualTo(requiredMajorVersion, requiredMinorVersion, requiredReleaseVersion) {
	
	if(arguments.length == 1) {
		var aStringVersion = arguments[0].split(",");
		this.requiredMajorVersion = parseInt( aStringVersion[0] );
		this.requiredMinorVersion = parseInt( aStringVersion[1] );
		if( isNaN(this.requiredMinorVersion) ) {this.requiredMinorVersion = 0};		
		this.requiredReleaseVersion = parseInt( aStringVersion[2] );
		if( isNaN(this.requiredReleaseVersion) ) {this.requiredReleaseVersion = 0};
	} else {
		this.requiredMajorVersion = requiredMajorVersion;
		this.requiredMinorVersion = requiredMinorVersion;
		this.requiredReleaseVersion = requiredReleaseVersion;
	}
	
	// get browser flash version
	var version = swfobject.getFlashPlayerVersion();

	if (version.major >= this.requiredMajorVersion) {
		if (version.minor >= this.requiredMinorVersion) {
			if (version.release >= this.requiredReleaseVersion) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	} else {
		return false;
	}
}










var ie6PNGfix = function(obj, img) {
	$(obj).attr("writing-mode", "tb-rl");
	$(obj).css("background-image", "none");
	$(obj).css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"',sizingMethod='scale')");
}
function createMovieWindow(ParamValues) {
                
                var closeElement = ParamValues.Close;
                
                /*
                 * This code should be removed when value campaign is over!
                 * Checks if value campaign curl is in window and 
                 * stops curl functionality when movie layer is opened
                 */
                if( $("#v-camp-frame").length > 0 ) {
                                if ("undefined"!=(typeof $("#v-camp-frame")[0].ValueCampaignFlash_toggleCurl)){
                                                $("#v-camp-frame")[0].ValueCampaignFlash_toggleCurl(false);
                                }
                }
                 
                 
                 
                 var movieWindowHTML = '<div id="popupWindow"><div id="popupWindow-content-frame"><div id="popupWindow-content" class="clearfix"><div id="popupWindow-closeButton" class="clearfix align_right"><table><tr><td class="closeButton_td1"><div class="closeButton"><a href="javascript:void(0)" onclick="removeMovieWindow()" class="closeButtonText">'+closeElement+'</a></div></td><td class="closeButton_td2"><a href="javascript:void(0)" onclick="removeMovieWindow()"><img src="/consumerfiles/assets/img/popupWindow/btn-close-popupWindow.png" alt=""/></a></td></tr></table></div><div id="popupWindow-content-loaded" class="clearfix"></div></div></div><div id="popupWindow-bottom"></div></div>';                                                       
                $("body").append(movieWindowHTML);
                centerPosition($("#popupWindow"), 313);
                
                
}

function removeMovieWindow() {
	$("#popupWindow").remove();
	
	
	/*
	 * This code should be removed when value campaign is over!
	 * Checks if value campaign curl is in window and 
	 * Restarts curl functionality when movie layer is closed if it is
	 */
	if( $("#v-camp-frame").length > 0 ) {
		if ("undefined"!=(typeof $("#v-camp-frame")[0].ValueCampaignFlash_toggleCurl)){
			setTimeout( '$("#v-camp-frame")[0].ValueCampaignFlash_toggleCurl(true)', 2000 );
		}
	}
	
	
}
function loadContentIntoMovieWindow(contentPage, paramValues) {
	
	hideOpenLayers();
	
	createMovieWindow(paramValues);
	
	//$("#popupWindow-content-loaded").empty();
	$("#popupWindow-content-loaded").load(contentPage, paramValues, function(){
	
	/* commented .get and helpers as this load seems to work better with ie6 */
	//$.get(contentPage, paramValues, function(data){
		//$("#popupWindow-content-loaded").empty();
		//$("#popupWindow-content-loaded").append(data);
		
		$("#popupWindow").show();
		
		/* check for ie 6 and fix PNG transparency */
		var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
		if(isIE6) {
			// this sets the height of the layer that needs the PNG fixed; necessary for pngfix
			$("#popupWindow-content-frame").css({"height":$("#popupWindow-content-frame").height()});
			// this resizes the content window as it is set to 551px for IE6 in cl.css
			$("#popupWindow-content").css({"height":$("#popupWindow-content").height()});
			ie6PNGfix($("#popupWindow-content-frame"), "/consumerfiles/assets/img/popupWindow/bg-popupWindowTop-ie6.png");
			ie6PNGfix($("#popupWindow-bottom"), "/consumerfiles/assets/img/popupWindow/bg-popupWindowBottom-ie6.png");
		}
		/* This typeof checks that the sIFRinit function exists...
		 * It will only exist if the sifr-ce-config.js file is loaded...
		 * and that is only loaded for the locales listed in masterhtml.jsp
		 */
		if(typeof sIFRinit != 'undefined') {
			sIFRinit();
		};
	});

}

// Function to create cookie
function createCookie(name,value,days) {
	var date, expires;
	if (days) {
		date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	} else { 
		expires = "";
	}	
	// document.cookie = name+"="+value+expires+"; path=/;domain=philips.com";
	document.cookie = name+"="+value+expires+"; path=/";
};

// Function to overcome 'Buy now layer appearing below 360 image'
function custom_clear_layers(){
 $(".widget_where-to-buy").hide();
 //document.getElementById("p-tab-consumer").className="p-active";
 if(document.getElementById("p-navigator-body-wrapper")==null){
 }
 else{
 document.getElementById("p-navigator-body-wrapper").style.display="none";
 }
}

/* 
*	creates popup window, loading in the file passed to it.
*	featues is a comma-separated list of - you guessed it - 
*	featues! location=no,status=no,scrollbars=yes etc
*	center is a boolean to position the window in the middle
*	of the screen. empty or no true will do nothing.
*/

function window_popup(content, x, y, title) {
	var thisWindow;
	thisWindow = window.open(content,title,"width="+x+",height="+y+",scrollbars=auto,screenX=0,screenY=0,toolbar=no,location=no,menubar=no");
	thisWindow.moveTo((screen.width-x)/2,(screen.height-y)/3);
	thisWindow.window.focus();
	}


function openWindow(url) {
	var newWindow = window.open(url, '_blank');
	newWindow.focus();
	return false;
}