function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}		 

function setVisible(obj){
	obj = document.getElementById(obj);
	obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible';
	obj.style.display = (obj.style.display == 'block') ? 'none' : 'block';
}

function setInvisible(obj){
	obj = document.getElementById(obj);
	obj.style.visibility = (obj.style.visibility == 'hidden') ? 'visible' : 'hidden';
	obj.style.display = (obj.style.display == 'none') ? 'block' : 'none';
}

function preInput(func, val, id) {
	if(func == 'focus') {
		if(id.value == val) { id.value = ""; }	
	} else {
		if(id.value == "") { id.value = val; }
	}
}

function setActive(target) {
	if(target == "htmlb") {
		var intEmail = document.getElementById('intEmail');
		if(intEmail) {
    		addEvent(intEmail,"focus",function() { preInput('focus','Uw emailadres',intEmail); });
    		addEvent(intEmail,"blur",function() { preInput('blur','Uw emailadres',intEmail); });
    	}
	}
}

function resetIt() {
	var tds = document.aboFrm.getElementsByTagName('a');
	for(var i=0;i<tds.length;i++) {
		if(tds[i].className.substring(0,8) == "cart_add") {
			tds[i].className = "cart_add";
		} 
	}	
	document.getElementById('optContent').innerHTML = "";
	document.getElementById('oldPrice').innerHTML = "";
	document.getElementById('newPrice').innerHTML = "";
	document.getElementById('prDisc').innerHTML = "";
    document.getElementById('xart_id').value = "";
	document.getElementById('ovDesc').innerHTML = "";
	document.getElementById('productPrice').innerHTML = document.getElementById('fullPrice').value;
	document.navFrm.M5WebArg.value = "";
}

function setTab(target,preselection) {
	var aCount = document.tabFrm.getElementsByTagName('a');
	var split = "";
	var supplier = "";
	var table = "";
	for(var i=0;i<=aCount.length-1;i++) {
		split = aCount[i].id.split("_");
		if(preselection == false) {
			if(split[1] != target) {
				aCount[i].className = "supplier";		
			} else {
				supplier = split[2];
				aCount[i].className = "supplier active";
			}
		} else {			
			if(split[2] == preselection) {
				supplier = split[2];
				aCount[i].className = "supplier active";
			} else {
				aCount[i].className = "supplier";
			}
		}
	}
	var elements = document.aboFrm.getElementsByTagName('div');
	for(var x=0;x<=elements.length-1;x++) {
		if(elements[x].id.substring(0,9) == "raprdlist") {
			table = elements[x].id.split("_");
			if(table[2] == supplier) {
				document.getElementById(elements[x].id).style.display = 'block';
				document.getElementById(elements[x].id).style.visibility = 'visible';
			} else {
				document.getElementById(elements[x].id).style.display = 'none';
				document.getElementById(elements[x].id).style.visibility = 'hidden';
			}
		}
	}
}

function changeTotals(qt,amount,target) {

   var total = qt * amount;
	total = Math.round(total*100)/100;
	total = total + '';
	total2 = total.split(".");
	if(total2.length >= 2) {
		if(total2[1].length == 1) {
			total = total + '0';	   
		}
		document.getElementById('totalSum').value = '€ '+total;
	} else {
		document.getElementById('totalSum').value = '€ '+total+'.00';
	}

	// setTotal();
}

function setTotal() {
	var price = "";
	var total = 0;
	var values = document.bagFrm.getElementsByTagName('input');
	for(var i=0;i<=values.length-1;i++) {
		if(values[i].className == "total") {
			price = values[i].value.split(" ");
			price = parseFloat(price[1]);
			total = total + price;
		}
	}
	total = total + '';
	var total2 = total.split(".");
	if(total2.length >= 2) {
		if(total2[1].length == 1) {
			total = total + '0';	   
		}
		document.getElementById('totalSum').value = '€ '+total;
	} else {
		document.getElementById('totalSum').value = '€ '+total+'.00';
	}	
}

function selectMethod(nr) {
	document.getElementById('nr'+nr).checked = 'true';
	var input = document.getElementsByTagName('input');
	for(var i=0;i<input.length;i++) {
		var name = input[i].getAttribute("name");
		if(name == "pmradio" || name == "shc_id") {
			var string = input[i].id.substring(2,3);
			document.getElementById('label'+string).className = 'payOption';
		}
	}
   document.getElementById('label'+nr).className = 'payOption active';
}

function switchAbo(target, firstswitch) {
	var trs = document.aboFrm.getElementsByTagName('tr');
	for(var x=0;x<trs.length;x++) {
		if(trs[x].className == 'rowActive') { trs[x].className = 'row'; }
	}
	var tds = document.aboFrm.getElementsByTagName('a');
	for(var i=0;i<tds.length;i++) {
		if(tds[i].className.substring(0,8) == "cart_add" && tds[i].id != target) {
			tds[i].className = "cart_add";
		} else if(tds[i].id == target) {
			tds[i].className = "cart_add active";
			// document.getElementById(target+'_tr').className = 'rowActive';
		} else if(firstswitch == true && tds[i].id == document.getElementById('xart_id').value) {
			tds[i].className = "cart_add active";
		}
	}
}

function calc(art_id,behaviour,extra) {
	if(art_id != 0) {
		var price = document.getElementById(art_id+'_pr').innerHTML;
	} else {
		var price = extra;	
		price = price.replace(/&euro;/i,"€");
	}
	price = price.replace(/€/i,"");
	price = parseFloat(price);
	var oldPrice = document.getElementById('newPrice').innerHTML;
	oldPrice = oldPrice.replace(/€/i,"");
	oldPrice = parseFloat(oldPrice);
	if(behaviour == 1) {
		price = oldPrice + price;
	} else {
		price = oldPrice - price;
	}
	price = price.toFixed(2);
	document.getElementById('newPrice').innerHTML = '&euro; '+price;
}

function finalCalc(xart_id, price, bonus) {
	var xart = xart_id.split(",");
	var addon = true;
	var articles = document.getElementById('xart_id').value;
	var article = articles.split(",");
	for(var i=0;i<=xart.length-1;i++) {
		for(var x=0;x<=article.length-1;x++) {
			if(article[x] == xart[i]) { addon = false; }	
		}
		if(addon == true) { 
			document.getElementById('xart_id').value += xart[i]+",";
		}
		addon = true;
	}
	calc(0,1,price);
	document.navFrm.M5WebArg.value = '%1Fxart_id='+document.getElementById('xart_id').value+document.getElementById('xart_acc').value+'%1FM5PostUrl=';
}

function setMainPrice(art_id,behaviour,bonus) {
	var prString = parseFloat(document.getElementById('price_of').value);
	var oldPrice = document.getElementById('productPrice').innerHTML;
	oldPrice = oldPrice.replace(/€/i,"");
	oldPrice = parseFloat(oldPrice);
	if(bonus && bonus != "") {
		var toAdd = parseFloat(bonus);
	} else {
		var toAdd = parseFloat(document.getElementById(art_id+'_bonus').value);
	}
	if(behaviour == 1) {
		if(toAdd > oldPrice) {
			prString += (toAdd - oldPrice);
		   document.getElementById('price_of').value = prString;
			var newPrice = 0;
		} else {
			var newPrice = oldPrice - toAdd;
		}
	} else {
	   if(document.getElementById('price_of').value != "") {
			var newPrice = ((oldPrice + toAdd) - prString);
			document.getElementById('price_of').value = 0; 
		} else {
			var newPrice = oldPrice + toAdd;
		}
	}
	if(newPrice < 0) {
		document.getElementById('productPrice').style.display = 'none';
	   document.getElementById('productPrice').style.visibility = 'hidden';
		document.getElementById('productPrice2').style.display = 'block';
	   document.getElementById('productPrice2').style.visibility = 'visible';
	} else {
		document.getElementById('productPrice').style.display = 'block';
	   document.getElementById('productPrice').style.visibility = 'visible';
		document.getElementById('productPrice2').style.display = 'none';
	   document.getElementById('productPrice2').style.visibility = 'hidden';
	}
	newPrice = newPrice.toFixed(2);
	document.getElementById('productPrice').innerHTML = '&euro; '+newPrice;
}


/* 
Function addAc:
	article: 	Geselecteerde artikel
	behaviour:	-- 0 = Geen ajax request / add to xart_id
					-- 1 = Flush xart_id / Wel een ajax request / add to xart_id
					-- 2 = Geen ajax request / add to xart_acc (Accessoires)
   main:			Hoofdartikel

	article[0] + behaviour[1] = Plaats bestelling en ga naar UCART
*/

function addAc(article,behaviour,main,exception) {
	var skip = false;
	if(exception && exception != 1) {
		var ca_ids = document.getElementById('xart_id').value;
 	   var ca_id = ca_ids.split(",");
		for(var a=0;a<=ca_id.length-1;a++) {
			if(ca_id[a].substring(0,exception.length) == exception) {
				var removeMe = ca_id[a];
				skip = true;
			} 
		}
	}
	var split = "";
	if(behaviour == 1 && article != 0) { document.getElementById('xart_id').value = ""; switchAbo(article, false); }
	if(article == 0 && behaviour == 1 && main != 0) { var next = true; }
	if(next == true) {
		if(exception == 1) { 
			document.navFrm.M5WebArg.value = '%1Fxart_id='+document.aboFrm.ab_art.value+","+document.getElementById('xart_id').value+'%1FM5PostUrl=';
		}
 		document.navFrm.bag_add.value = main; 
 		if(document.getElementById('xart_acc') && document.getElementById('xart_acc').value != '') 
 		    document.navFrm.bag_add.value += ',' + document.getElementById('xart_acc').value; 

		document.navFrm.M5WebArg.value = '%1Fxart_id='+document.getElementById('xart_id').value+'%1FM5PostUrl=';
		document.navFrm.M5NextUrl.value = 'UCART';
		document.navFrm.M5CurrUrl.value = 'UCART';
		document.navFrm.ord_num.value= '1';
		m5submit(document.navFrm);
	} else {
		if(behaviour == 2) {
		   var M5WebArg = document.getElementById('xart_acc').value;
		} else {
			var M5WebArg = document.getElementById('xart_id').value;
		}
		if(M5WebArg != "") {	split = M5WebArg.split(","); }
		if(behaviour != 1) { var currRow = document.getElementById(article); }
		var addArt = true;
		var newStr = "";
	   for(var i=0;i<=split.length-1;i++) {
			if(split[i] == article){ addArt = false; } 
		}
		if(addArt == true) {
			if(behaviour != 1) { currRow.className = 'rowActive'; }
			if(behaviour == 2) {
				document.getElementById('xart_acc').value += article+',';
			} else {
				document.getElementById('xart_id').value += article+',';
				if(behaviour == 3) { calc(article,1); setMainPrice(article,1); }
			}
		} else {
			for(var i=0;i<=split.length-1;i++) {
				if(split[i] != article && split[i] != "") {
					newStr = newStr + split[i] + ",";
				}
			}
			if(behaviour != 1) {	currRow.className = 'row'; }
			if(behaviour == 2) {
				document.getElementById('xart_acc').value = newStr;
			} else {
				document.getElementById('xart_id').value = newStr;
				if(behaviour == 3) { calc(article,0); setMainPrice(article,0); }
			}
		}
		document.navFrm.M5WebArg.value = '%1Fxart_id='+document.getElementById('xart_id').value+document.getElementById('xart_acc').value+'%1FM5PostUrl=';
		if(behaviour == 1 && main != 0) {
			document.navFrm.art_id.value = main;
			document.navFrm.ord_num.value= '1';
			if(exception && exception == 'mark') {
				getData(pmshop.toLowerCase(),main,article,'mark');
			} else {
				getData(pmshop.toLowerCase(),main,article);
			}
		}	
		if(skip == true && removeMe != article) {
			if(behaviour == 0 && main == 0) {
				document.getElementById(removeMe).className = 'row';
				var xart_id = document.getElementById('xart_id').value;
				var string = "";
				xart = xart_id.split(',');
				for(var z=0;z<=xart.length-1;z++) {
					if(xart[z] != removeMe && xart[z] != "") { string = string + xart[z] + ','; }		
				}
				document.getElementById('xart_id').value = string;
			} else {
				if(document.getElementById(removeMe+'_option')) document.getElementById(removeMe+'_option').className = 'noDisplay';
				if(document.getElementById(removeMe+'_rev')) document.getElementById(removeMe+'_rev').className = 'revItem noDisplay';	
				if(document.getElementById(removeMe)) document.getElementById(removeMe).className = 'row';
				var xart_id = document.getElementById('xart_id').value;
				var string = "";
				xart = xart_id.split(',');
				for(var z=0;z<=xart.length-1;z++) {
					if(xart[z] != removeMe && xart[z] != "") { string = string + xart[z] + ','; }		
				}
				document.getElementById('xart_id').value = string;
				document.navFrm.M5WebArg.value = '%1Fxart_id='+string+document.getElementById('xart_acc').value+'%1FM5PostUrl=';
				if(main != '1') { calc(removeMe,0); setMainPrice(removeMe,0); }
			}
		}
	}
}

function switchit(elm) {
	obj = document.getElementById(elm);
	if (obj) {
		if (obj.style.display == 'block') obj.style.display = 'none';
		else obj.style.display = 'block';
	}
	return true;
}

function switchToVisible(obj,action) {
	obj = document.getElementById(obj);
  	if(action == 1) {
		obj.style.visibility = 'visible';
		obj.style.display = 'block';
	} else {
		obj.style.visibility = 'hidden';
		obj.style.display = 'none';
	}
}

var xmlhttp;
function getData(pmshop,product1,product2,exception) {
    if (!xmlhttp) xmlhttp = aaGetXmlHttpRequest();
    if (!xmlhttp) return;
	document.getElementById('optContent').innerHTML = "";
	document.getElementById('ajaxTxt').value = 'Zoeken naar opties...';
	switchToVisible('loader',1);
	switchToVisible('loadingFrame',1);
	if(exception && exception == 'mark') {
		var url = '/' + pmshop + '/mcsmambo.p?M5NextUrl=RAOPT&fScope=false&art_id='+product1+'&xart_id='+product2+'&exception='+exception;
	} else {
	 	var url = '/' + pmshop + '/mcsmambo.p?M5NextUrl=RAOPT&fScope=false&art_id='+product1+'&xart_id='+product2;
	}
   xmlhttp.open('GET', url, true);
   xmlhttp.onreadystatechange = printData;
   xmlhttp.send(null);
   return false;
}

// Functie voor het visualiseren van postcode resultaat
function printData() {
	if(!xmlhttp.readyState == 4) {
	} else if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      var resultStr = xmlhttp.responseText;
		result = resultStr.split('*****');
		if(result.length == 1) {
			document.getElementById('ajaxTxt').value = 'Geen opties gevonden voor dit abonnement';
		} else {
			switchToVisible('loadingFrame',0);
			document.getElementById('optContent').innerHTML = result[1];
			document.getElementById('ovDesc').innerHTML = document.getElementById('ovDesc').innerHTML + document.getElementById('allOptions').innerHTML;
			switchToVisible('aboRevFld', 1);
			document.getElementById('aboRev').innerHTML = document.getElementById('optionsRev').innerHTML;
		}
		switchToVisible('loader',0);
   }
}

function showOption(article) {
	if(document.getElementById(article+'_option').className == 'noDisplay') {
		document.getElementById(article+'_option').className = 'blockDisplay';	
		document.getElementById(article+'_rev').className = 'revItem blockDisplay';	
	} else {
		document.getElementById(article+'_option').className = 'noDisplay';	
		document.getElementById(article+'_rev').className = 'revItem noDisplay';	
	}
}

function barGen() {
	var strlen = document.getElementById('barC').value.length;
	var str = document.getElementById('barC').value;
	var targetdiv = document.getElementById('barGen');
	targetdiv.innerHTML = '<img src="/css/59/ui/objects/bc/star.gif" alt="" />'; 
	for(var i=0;i<strlen;i++) {
		targetdiv.innerHTML += '<img src="/css/59/ui/objects/bc/'+str.substring(i,i+1).toUpperCase()+'.gif" alt="" />'; 
	}
	targetdiv.innerHTML += '<img src="/css/59/ui/objects/bc/star.gif" alt="" />'; 
}

function closeDimmer() {
	setInvisible('dimmer');
	setInvisible('dimContent');
}
  
function getAbo(product, shop, evnt) {
   if (!xmlhttp) xmlhttp = aaGetXmlHttpRequest();
   if (!xmlhttp) return;
	var url = '/'+shop+'/mcsmambo.p?M5NextUrl=RAPRD&art_id='+product+'&showPhones=FALSE';
	setVisible('dimmer');
	setVisible('dimContent');
	document.getElementById('dimContainer').innerHTML = '';
   xmlhttp.open('GET', url, true);
   xmlhttp.onreadystatechange = printAbo;
   xmlhttp.send(null);
   if (evnt && evnt.preventDefault())
     evnt.preventDefault();
   return false;
}

// Functie voor het visualiseren van postcode resultaat
function printAbo() {
	if(!xmlhttp.readyState == 4) {
	} else if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      var resultStr = xmlhttp.responseText;
		var result = resultStr.split("*****");
		if(result.length != 1) {
			document.getElementById('dimContainer').innerHTML = '<a id="dimCloser" onClick="closeDimmer();"></a>'+result[1];
		}
   }
}

function setAboPrice(values,prPrice,prDesc,exc) {
	var aboval = values.split(",");
	document.getElementById('oldPrice').innerHTML = aboval[0];
	document.getElementById('newPrice').innerHTML = aboval[1];
	var discTxt = document.getElementById('discTxt').value;
	discTxt = discTxt.replace(/--1--/i, '<span class="bold">'+aboval[2]+'</span>');
	if(aboval[3] != "") {
		discTxt = discTxt.replace(/--2--/i, '<span class="bold">'+aboval[3]+'</span>'); 
	} else {
		discTxt = discTxt.replace(/--2--/i, '<span class="bold">&euro; 0.00</span>'); 
	}
	document.getElementById('prDisc').innerHTML = discTxt;
	document.getElementById('productPrice').innerHTML = prPrice;
	if(exc && exc == "priceof") {
		document.getElementById('ovDesc').innerHTML = prDesc + document.getElementById('ovDesc').innerHTML;
	} else {
		document.getElementById('ovDesc').innerHTML = prDesc;
	}
	document.getElementById('price_of').value = 0;
	if(exc && exc != "") {
		var bStr = document.getElementById('tBonus').value;
		bStr = bStr.split(",");
		var pr1 = document.getElementById(bStr[1]+'_combiPrice').innerHTML;
		pr1 = pr1.replace(/€/i,"");
		pr1 = parseFloat(pr1);
		var pr2 = parseFloat(bStr[0]);
		var pr3 = pr2 - pr1;
		if(pr3 > 0) {
			document.getElementById('price_of').value = pr3;
		}
	}
}

function showDisclaimer() {
	setVisible('dimmer');
	setVisible('dimContent');
	document.getElementById('dimContainer').innerHTML = '<a id="dimCloser" onClick="closeDimmer();"></a><div class="scrollContent">'+document.getElementById('m5disclaimer').innerHTML+'</div>';
}

function prEnlarge(image,art_id) {	
	var popContent = document.createElement('img');
		 popContent.setAttribute('id','li_'+art_id);
		 popContent.setAttribute('src',image);

	document.getElementById('ppLarge_'+art_id).appendChild(popContent);
	switchToVisible('ppLarge_'+art_id,1);	
}

function prClose(art_id) {
	document.getElementById('ppLarge_'+art_id).removeChild(document.getElementById('li_'+art_id));
	switchToVisible('ppLarge_'+art_id,0);
}

function modClass(t,v) {
    $('#'+t).addClass(v); 
}
