// TECL Javascript

//Dollar function
function $() {
	var elements = [];
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') {
			element = document.getElementById(element);
		}
		if (arguments.length == 1) {
			return element;
		}
		elements.push(element);
	}
	return elements;
}



//CONFIRMATION AND ERROR BOXES POPUP

//function to display the confirmation and error boxes

function confirmationPopup (action) {

//define initial variables to be used in the script

var confirmBox = document.getElementById('confirmationBox');
if (action != "openerror") {
	var confirmBg = document.getElementById('confirmationBg');
}
var viewportHeight='';


//get viewfinder height for all browsers

if (typeof window.innerWidth != 'undefined')
{
	viewportHeight = window.innerHeight;
}
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
	viewportHeight = document.documentElement.clientHeight;
}
else
{
	viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
}


if (action == "open") {
	
	//stretch the dimmed layer vertically and show it
	
	confirmBg.style.height=document.body.parentNode.scrollHeight+"px";
	confirmBg.style.display= "block";
	
	//show the confirmBox by setting the left margin to it's original 0px- IE6 won't show the transparent PNG with the display: none/block method
	
	confirmBox.style.marginLeft= "0px";
	
	//center the confirmationbox on the screen
	
	confirmBox.style.marginTop = ((viewportHeight-confirmBox.offsetHeight)/2)+75+"px";
	confirmBox.style.marginLeft = (document.body.clientWidth-confirmBox.offsetWidth)/2+"px";
	
	//scroll to the top of the page
	
	document.body.parentNode.scrollTop="0";

}
else if (action == "openerror") {
	
	//show the confirmBox by setting the left margin to it's original 0px- IE6 won't show the transparent PNG with the display: none/block method
	
	confirmBox.style.marginLeft= "0px";
	
	//center the confirmationbox on the screen
	
	confirmBox.style.marginTop = ((viewportHeight-confirmBox.offsetHeight)/2)+75+"px";
	confirmBox.style.marginLeft = (document.body.clientWidth-confirmBox.offsetWidth)/2+"px";
	
	//scroll to the top of the page
	
	document.body.parentNode.scrollTop="0";

}
else {

	confirmBg.style.display= "none";
	
	//hide the confirmBox using a negative left margin - IE6 won't show the transparent PNG with the display: none/block method
	
	confirmBox.style.marginLeft= "-10000px";
}


}




function convertToNum(aNum) 
{
	//remove any points
	if(aNum.indexOf(".") > 0)
	{ aNum=aNum.replace(/\./g,""); }
	//remove any spaces
	//aNum=aNum.replace(/\s/g,"");
	//replace any commas
	aNum=aNum.replace(/,/g,".");
	return aNum;
}

function convertToString(aNum) 
{
	var tempNum = aNum + "";
	//replace any commas
	if(tempNum.indexOf(",") > 0)
	{ tempNum=tempNum.replace(/,/g,"."); }
	//remove any spaces
	//aNum=aNum.replace(/\s/g,"");
	//replace any points
	tempNum=tempNum.replace(/\./g,",");
	return tempNum;
}


function clearText(theText)
{
	theText.value = "";
}

/**
 * 
 * @param oldElm a reference to the input element
 * @param iType value of the type property: 'text' or 'password'
 * @param iValue the default value, set to 'password' in the demo
 * @param blankValue true if the value should be empty, false otherwise
 * @param noFocus set to true if the element should not be given focus
 * @return
 */
function changeInputType(oldElm, iType, iValue, blankValue, noFocus) {   
  if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) {
    	 return;
    }
  var newElm = document.createElement('input');
  newElm.type = iType;
  if(oldElm.name) {newElm.name = oldElm.name ;}
  if(oldElm.id) { newElm.id = oldElm.id; }
  if(oldElm.className) {newElm.className = oldElm.className;}
  if(oldElm.size) {newElm.size = oldElm.size;}
  if(oldElm.tabIndex) {newElm.tabIndex = oldElm.tabIndex;}
  if(oldElm.accessKey) {newElm.accessKey = oldElm.accessKey;}
  
  newElm.onfocus = function(){return function()
  {
    if(this.hasFocus) {return;}
    var newElm = changeInputType(this,'password',iValue,
      (this.value.toLowerCase()==iValue.toLowerCase())?true:false);
    if(newElm) {newElm.hasFocus=true;}
  }}();
  newElm.onblur = function(){return function(){
    if(this.hasFocus) {
    	if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) {
      	changeInputType(this,'text',iValue,false,true);
    	}
    }
  }}();
 // hasFocus is to prevent a loop where onfocus is triggered over and over again
  newElm.hasFocus=false;
  oldElm.parentNode.replaceChild(newElm,oldElm);
  if(!blankValue) {newElm.value = iValue;}
  if(!noFocus || typeof(noFocus)=='undefined') {
    window.tempElm = newElm;
    setTimeout("tempElm.hasFocus=true;tempElm.focus();",1);
  }
  return newElm;
}


function trim(value) {
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}

function checkKey(form,event)
{
	if(event.keyCode == 13)
	{
		form.submit();
	}
}


/*Test to see if cookies are enabled*/
function testSessionCookie () 
{
  document.cookie ="testSessionCookie=Enabled";
  if (getCookieValue ("testSessionCookie")=="Enabled") {
    return true;
   }
  else {
    return false;
  }
}

/*Write session cookie*/
function writeSessionCookie (cookieName, cookieValue) 
{
  if (testSessionCookie()) 
  {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
    return true;
  }
  else { return false; }
}


// go to page selected from options menu
function GoUrl(s)
{
  var d = s.options[s.selectedIndex].value;
  window.top.location.href = d;
  s.selectedIndex=0;
}

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

//function to set the basket cookie
function setBasketCookie(cookie,value,days)
{		
	//writeSessionCookie(cookie,value);
	createCookie(cookie,value,days);
}

//function to set the offer cookie
function setOfferCookie(cookie,value,days)
{		
	createCookie(cookie,value,days);
}
	
//functions used in UpdateLTOUser.isml
function replaceAll( str, searchTerm, replaceWith, ignoreCase )   {
   var regex = "/"+searchTerm+"/g";
   if( ignoreCase ) { regex += "i"; }
   return str.replace( eval(regex), replaceWith );
}

function setValue(form)
{	
	if(document.forms[form].Day.options[document.forms[form].Day.options.selectedIndex].value == "dd" ||
	   document.forms[form].Day.options[document.forms[form].Day.options.selectedIndex].value == "mm")
	{ 
		document.forms[form].AddressForm_Birthday.value = "";		
	}
	else
	{  		
		var str = document.forms[form].Day.options[document.forms[form].Day.options.selectedIndex].value+"/"+document.forms[form].BirthMonth.options[document.forms[form].BirthMonth.options.selectedIndex].value+"/"+document.forms[form].BirthYear.value;																														   
		document.forms[form].AddressForm_Birthday.value=str;	
	}
	var year  = document.forms[form].BirthYear.value;
	var year1 = replaceAll(year,">","","true");
	var year2 = replaceAll(year1,"<","","true");		
	document.forms[form].BirthYear.value = year2;
}




// Validates Quantity field: only numbers allowed & replace dot '.' with comma ','


function NumbersOnly(e, decimal) {
	var key;
	var keychar;
	
	if (window.event) {
	   key = window.event.keyCode;
	}
	else if (e) {
	   key = e.which;
	}
	else {
	   return true;
	}
	keychar = String.fromCharCode(key);
	
	if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
	   return true;
	}
	else if ((("0123456789,.").indexOf(keychar) > -1)) {
	   return true;
	}
	else if (decimal && (keychar == ".")) { 
	  return true;
	}
	else {
	   return false;
	}
}


function CommaReplace(x)
{
var y=document.getElementById(x).value;
document.getElementById(x).value=y.replace('.',',');
  
}





// NEWS IMAGES POPUP WITH DIMMED LAYER

//function to display the news detail images in a dimmed layer

function newsimagePopup (action,which) {

//define initial variables to be used in the script

var imageBox = document.getElementById('imageBox');
var imageBg = document.getElementById('imageBg');
var viewportHeight='';

//set the correct imagepath for the popup image

document.getElementById('newspopupimage').src= which;

//get viewfinder height for all browsers

if (typeof window.innerWidth != 'undefined')
{
	viewportHeight = window.innerHeight;
}
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
	viewportHeight = document.documentElement.clientHeight;
}
else
{
	viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
}


if (action == "open") {
	
	//stretch the dimmed layer vertically and show it
	
	imageBg.style.height=document.body.parentNode.scrollHeight+"px";
	imageBg.style.display= "block";
	
	//show the confirmBox by setting the left margin to it's original 0px- IE6 won't show the transparent PNG with the display: none/block method
	
	imageBox.style.marginLeft= "0px";
	
	//center the confirmationbox on the screen
	
	imageBox.style.marginTop = "195px";
	imageBox.style.marginLeft = (document.body.clientWidth-imageBox.offsetWidth)/2+"px";
	
	//scroll to the top of the page
	
	document.body.parentNode.scrollTop="0";

}
else {

	imageBg.style.display= "none";
	
	//hide the confirmBox using a negative left margin - IE6 won't show the transparent PNG with the display: none/block method
	
	imageBox.style.marginLeft= "-10000px";
}


}



//TOOLTIP POPUP

//
function toggleToolTip(action, tipDiv, Link, Text, offsetLeft, offsetTop) {
	
	var toolTipDiv = $(tipDiv);
	var linkDiv = $(Link);
	var selectHider = $('selecthider');
	
	if (action == 'show') {
		//prefill the tooltip div with the content
		
		toolTipDiv.innerHTML= Text;
		
		//position the tooltip
		//alert(document.getElementById(tipDiv).offsetWidth);
		
		toolTipDiv.style.display= "inline";
		toolTipDiv.style.top= linkDiv.parentNode.offsetTop + offsetTop + "px";		
		toolTipDiv.style.left= linkDiv.parentNode.offsetWidth + 25 + offsetLeft + "px";
		
		//show tooltip
		
		toolTipDiv.style.display= "inline";
		
		//position the iframe(which hides the select boxes in IE6 using the "iframe shim" method) & adjust it's size according to the tooltip
		
		//alert(document.getElementById(tipDiv).offsetHeight + 10 + "px");	
	
		selectHider.style.height= toolTipDiv.offsetHeight + "px";
		selectHider.style.display= "block";
		selectHider.style.marginTop= linkDiv.parentNode.offsetTop + offsetTop + "px";
		selectHider.style.marginLeft= linkDiv.parentNode.offsetWidth + 25 + offsetLeft + "px";
		
	}
	else {
		
		toolTipDiv.style.display= "none";
		selectHider.style.display= "none";
		
		}
	
}

function toolTip (action, tipDiv, Link, Text) {
	toggleToolTip(action, tipDiv, Link, Text, 0, 0);
}



function confirmationDeletePopup (action,itemCode,parametername) 
{

//define initial variables to be used in the script

var confirmBox = document.getElementById('confirmationBox');
if (action != "openerror") {
	var confirmBg = document.getElementById('confirmationBg');
}
var viewportHeight='';


//get viewfinder height for all browsers

if (typeof window.innerWidth != 'undefined')
{
	viewportHeight = window.innerHeight;
}
else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
	viewportHeight = document.documentElement.clientHeight;
}
else
{
	viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
}


if (action == "open") {
	
	//stretch the dimmed layer vertically and show it
	var param = document.getElementById(parametername);
	param.value=itemCode;
	
	
	confirmBg.style.height=document.body.parentNode.scrollHeight+"px";
	confirmBg.style.display= "block";
	
	//show the confirmBox by setting the left margin to it's original 0px- IE6 won't show the transparent PNG with the display: none/block method
	
	confirmBox.style.marginLeft= "0px";
	
	//center the confirmationbox on the screen
	
	confirmBox.style.marginTop = ((viewportHeight-confirmBox.offsetHeight)/2)+75+"px";
	confirmBox.style.marginLeft = ((document.body.clientWidth-confirmBox.offsetWidth)/2)-document.getElementById('frame').offsetLeft +"px";
	
	//scroll to the top of the page
	
	document.body.parentNode.scrollTop="0";

}
else if (action == "openerror") {
	
	//show the confirmBox by setting the left margin to it's original 0px- IE6 won't show the transparent PNG with the display: none/block method
	
	confirmBox.style.marginLeft= "0px";
	
	//center the confirmationbox on the screen
	
	confirmBox.style.marginTop = ((viewportHeight-confirmBox.offsetHeight)/2)+75+"px";
	confirmBox.style.marginLeft = (document.body.clientWidth-confirmBox.offsetWidth)/2+"px";
	
	//scroll to the top of the page
	
	document.body.parentNode.scrollTop="0";

}
else {

	confirmBg.style.display= "none";
	
	//hide the confirmBox using a negative left margin - IE6 won't show the transparent PNG with the display: none/block method
	
	confirmBox.style.marginLeft= "-10000px";
}


}


// Companytype selector javascripts

function showDifferent(form, selectId)
{
	var el = document.getElementById(selectId + "_CompanyType");
	var tf = document.getElementById(selectId + "_CompanyType_Different");
	//var selValue = document.forms[form].CompanyType.options[document.forms[form].CompanyType.options.selectedIndex].value;
	
	var selectedIndex = el.options.selectedIndex;
	var selValue = el.options[selectedIndex].value;
		
	if(selValue == "anders")
	{ 
		tf.style.display='inline';
		//document.forms[form].CompanyType_Different.style.display='inline';		
	}else{
		tf.style.display='none';
		//document.forms[form].CompanyType_Different.style.display='none';	
	}
}		

function setCompanyType(type, selectId)
{	
	var elCompanyType = document.getElementById(selectId + "_CompanyType");
	
	types = elCompanyType.options;
	var arLen=types.length;
	for ( var i=0; i<arLen; i++ )
	{
		if(type == types[i].value)
		{
			types[i].selected = true;
			showDifferent('', selectId);
			break;
		}
	}
}	

//Automatically set the delivery date 2 business days ahead.

var holidays = [];
var daysToAdd = [];

function isHoliday(currDate) {
	for (x in holidays) {
		if (currDate.getTime() == holidays[x].getTime()) {
			return x;
		}
	}
	return -1;
}

function addDays(currDate, days) {
	return new Date(currDate.getFullYear(), currDate.getMonth(), currDate.getDate() + days);
}

function getEasterDate(year) {
	var a = year % 19;
	var b = Math.floor(year / 100);
	var c = year % 100;
	var d = Math.floor(b / 4);
	var e = b  % 4;
	var f = Math.floor ((b + 8) / 25);
	var g = Math.floor ((b - f + 1) / 3);
	var h = (19*a + b - d - g + 15) % 30;
	var i = Math.floor (c / 4);
	var k = c % 4;
	var L = (32 + 2*e + 2*i - h - k) % 7;
	var m = Math.floor ((a + 11*h + 22*L) / 451);
	var month = Math.floor ((h + L - 7*m + 114) / 31) - 1;
	var day = ((h + L - 7*m + 114) % 31) + 1;	//+1 voor Pasen, +2 voor paasmaandag
	
	var easter = new Date(year, month, day);
	
	return easter;
}

function initHolidays() {
	var today = new Date();
	var year = today.getFullYear();
	var easter = getEasterDate(year);
	
	holidays[0] = new Date(year, 0, 1);		//Nieuwjaar
	daysToAdd[0] = 1;
	holidays[1] = addDays(easter, 1);		//Paasmaandag
	daysToAdd[1] = 1;
	holidays[2] = new Date(year, easter.getMonth(), easter.getDate() + 39);	//Hemelvaart
	daysToAdd[2] = 1;
	holidays[3] = new Date(year, easter.getMonth(), easter.getDate() + 50);	//Pinkstermaandag
	daysToAdd[3] = 1;
	holidays[4] = new Date(year, 3, 30);	//Koninginnedag
	daysToAdd[4] = 1;
	holidays[5] = new Date(year, 11, 25);	//Kerstdag
	daysToAdd[5] = 2;
	holidays[6] = new Date(year, 11, 26);	//Tweede kerstdag
	daysToAdd[6] = 1;
	holidays[7] = new Date(year + 1, 0, 1);		//Nieuwjaar
	daysToAdd[7] = 1;
}

function makeDate(currDate) {
	var date = currDate.getDate() < 10 ? "0" + currDate.getDate() : currDate.getDate();
	var month = currDate.getMonth() < 9 ? "0" + (currDate.getMonth() + 1) : currDate.getMonth() + 1;
	
	return date + "/" + month + "/" + currDate.getFullYear();
}

function addBusinessDays(currDate, days, shippingMethod) {
	currDate = addDays(currDate, days);
	
	var weekdag = currDate.getDay();
	var isHD = isHoliday(currDate);
	
	if (weekdag == 0) {
		currDate = addBusinessDays(currDate, 2, shippingMethod);
	} else if (weekdag == 6) {
		//When shipping method is parcel, deliveries are made on saturday.
		if (shippingMethod.toLowerCase() != "parcel") {
			currDate = addBusinessDays(currDate, 2, shippingMethod);
		}
	} else if (isHD >= 0) {
		currDate = addBusinessDays(currDate, daysToAdd[isHD], shippingMethod);
	}
	
	return currDate;
}

function findBusinessDay(wishdate, days, target, shippingMethod) {
	var datesplit = [];
	datesplit = wishdate.split('/');

	//Javascript needs to know we're passing along decimals. Otherwise "08" and "09" return 0
	var dag = parseInt(datesplit[0], 10);
	var maand = parseInt(datesplit[1], 10);
	var jaar = parseInt(datesplit[2], 10);
	
	var currDate = new Date(jaar, maand - 1, dag);
	
	var weekdag = currDate.getDay();
	
	if (weekdag == 6) {	//Zaterdag
		currDate = addDays(currDate, 1);
	} else {
		var isHD = isHoliday(currDate);
		if (isHD >= 0) {
			currDate = addDays(currDate, daysToAdd[isHD]);
		}
	}
	
	currDate = addBusinessDays(currDate, days, shippingMethod);
	
	document.getElementById(target).value = makeDate(currDate);
}

function clickEvent (ce, linkLocation, addToCartCommand) {
	var targ;
	if (!ce)
	  {
		  ce=window.event;
	  }
	
	//Find out what element was clicked.
	if (ce.target)
	  {
	  targ=ce.target;
	  }
	else if (ce.srcElement)
	  {
	  targ=ce.srcElement;
	  }
	if (targ.nodeType==3) // defeat Safari bug
	  {
	  targ = targ.parentNode;
	  }
	
	//Get the class name of the clicked element
	var tclass; 
	tclass = targ.className;
	
	if (tclass == "cart")
	{
		eval(addToCartCommand);
	}
	else 
	{
		location.href = linkLocation;
	}
	
}

function formatZip(elementId) {
	var element = $(elementId);
	var value = element.value;
	
	var checkRE = new RegExp("[0-9]{4}\\s[A-Z]{2}");
	
	//De postcode heeft nog niet het juiste formaat. Pas het aan.
	if (!checkRE.test(value)) {
		//Probeer eerst met uppercase.
		value = value.toUpperCase();
		
		if (!checkRE.test(value)) {
			//Als er geen spaties staan, voeg er dan 1 toe.
			checkRE.compile("([0-9]{4})[A-Z]{2}");
			if (checkRE.test(value)) {
				var nrs = checkRE.exec(value)[1];
				value = value.replace(new RegExp("[0-9]{4}"), nrs + " ");
			}
			else {
				//Ofwel staat er meer dan 1 spatie, ofwel is de postcode compleet verkeerd.
				//Als het er meerdere zijn, laat ze dan weg.
				checkRE.compile("[0-9]{4}( {2,})[A-Z]{2}");
				if (checkRE.test(value)) {
					value = value.replace(new RegExp(" +"), " ");
				}
			}
		}
		element.value = value;
	}
}

function pointReplace(x) {
	return x.replace(',', '.');
}


function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		document.getElementById("countdown").innerHTML = limitNum - limitField.value.length;
	}	
}
