function createRequestObject() {
	var ro;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
		ro = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		ro = new XMLHttpRequest();
	}
	return ro;
}

var browser = navigator.appName;
var http = createRequestObject();

if(browser == "Microsoft Internet Explorer"){
	var meth = "POST";
}
else{
	var meth = "GET";
}

$(function() {
	$.nyroModalSettings({
		debug: false,

		minHeight: 150,
		minWidth: 821,
		windowResize: true,
		resizable: true,
		autoSizable: true,
		endShowContent: function(elts, settings) {
			if (settings.type == 'iframe') {
				var iframe = $('iframe', elts.content);
				if (iframe.attr('src').indexOf(window.location.hostname) > -1) {
					iframe.load(function() {
						var body = iframe.contents().find('body');
						$.nyroModalSettings({width:body.width(), height:body.height()});
						return false;
					});
				}
			}
		}
	});
	
	function preloadImg(image) {
		var img = new Image();
		img.src = image;
	}
	
	var mom = document.getElementById("mom").value;
	preloadImg(mom+'img/modal/ajaxLoader.gif');
	preloadImg(mom+'img/modal/prev.gif');
	preloadImg(mom+'img/modal/next.gif');
	preloadImg(mom+'img/modal/close.gif');
	
});

function slideSwitch() {
    var $active = $('#reclame IMG.active');

    if ( $active.length == 0 ) $active = $('#reclame IMG:last');

   		var $next =  $active.next().length ? $active.next()
        : $('#reclame IMG:first');

	    $active.addClass('last-active');

    	$next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
        $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});

function gaNaar(url){
	window.location.replace(url);
}

function mandAanUit(){	
	if(document.getElementById('showa').value > 1){
	
		var show = document.getElementById('showm');
		
		if(show.value == 1){
			show.value = 0;
			var knd = 0;
			document.getElementById('wwm').style.visibility = "hidden";
		}
		else{
			show.value = 1;
			var knd = 1;
			document.getElementById('wwm').style.visibility = "visible";
		}
		
		http = createRequestObject();
		http.onreadystatechange = showFav;
			
		var mom = document.getElementById('mom').value;
			
		http.open(meth,mom+"manip/setMandUit.php?srt="+knd,true);
		http.setRequestHeader('Content-Type','text/plain');
		http.send(null);
		
	}
}

function plusMin(id,srt){
	http = createRequestObject();
	http.onreadystatechange = showPlusMin;
		
	var mom = document.getElementById('mom').value;
		
	http.open(meth,mom+"manip/plusmin.php?id="+id+"&srt="+srt,true);
	http.setRequestHeader('Content-Type','text/plain');
	http.send(null);
}

function showPlusMin(){
	 if(http.readyState == 4) { 
		if(http.status  == 200) {
			
			var items = http.responseText.split("[*]");
			
			document.getElementById("regel_"+items[2]).innerHTML = items[0];
			document.getElementById("winkelmand_totaal").innerHTML = items[1];
			
		}
	}
}

function showMelding(){
	alert('Vergeet niet om na de betaling op "terug naar de winkel" te klikken!');
}

function setVergelijk(nr){
	if(document.getElementById('verg_'+nr).checked == true){
		document.getElementById('verg_'+nr).checked = false;
	}
	else{
		document.getElementById('verg_'+nr).checked = true;
		putInVergelijk(nr);
	}
}

function setDeal(){
	http = createRequestObject();
	var mom = document.getElementById('mom').value;
	var deal = document.getElementById('idealordr').value;

	http.open(meth,mom+"manip/setdeal.php?order="+encodeURIComponent(deal),true);
	http.setRequestHeader('Content-Type','text/plain');
	http.send(null);
}

function setalsFav(id,wat){
	http = createRequestObject();
	http.onreadystatechange = showFav;
		
	var mom = document.getElementById('mom').value;
		
	http.open(meth,mom+"manip/setFav.php?fav="+id+"&wat="+wat,true);
	http.setRequestHeader('Content-Type','text/plain');
	http.send(null);
}

function showFav(){
	 if(http.readyState == 4) { 
		if(http.status  == 200) {
			var info = http.responseText.split("[*]");
			document.getElementById('prodfav_'+info[0]).innerHTML = info[1];
		}
	}
}

function bestelProduct(id,aantal){
	http = createRequestObject();
	http.onreadystatechange = showWinkelmand;
		
	var mom = document.getElementById('mom').value;
		
	http.open(meth,mom+"manip/inWinkelmand.php?id="+id+"&aantal="+aantal,true);
	http.setRequestHeader('Content-Type','text/plain');
	http.send(null);
}

function showWinkelmand(){
	 if(http.readyState == 4) { 
		if(http.status  == 200) {
			document.getElementById("winkelwagen").innerHTML = http.responseText;
			$('#winkelInhoud').jScrollPane({showArrows:false, scrollbarWidth: 3});	
		}
	}
}

function getCats(id,nr,wacht){
	if(id != document.getElementById('catMom').value){
		if(document.getElementById("catWacht").value == "1"){
			
		}
		else{		
			document.getElementById('catMom').value = id;
			
			http = createRequestObject();
			http.onreadystatechange = showCats;
				
			var mom = document.getElementById('mom').value;
				
			http.open(meth,mom+"manip/getcats.php?cat="+id,true);
			http.setRequestHeader('Content-Type','text/plain');
			http.send(null);
			
			for(var i = 1; i < document.getElementById('catAantal').value; i++){
				if(i == nr){
					document.getElementById('cat_'+i).className = "mainCatSel";
				}
				else{
					document.getElementById('cat_'+i).className = "mainCat";
				}
			}
		}
	}
}

function catOut(id){
	if(document.getElementById("catWacht").value == 0){
		if(id == document.getElementById('catMom').value){
			document.getElementById("catWacht").value = 1;
			
			setTimeout('clearWacht("")',150);
		}
	}
}

function showCats(){
	 if(http.readyState == 4) { 
		if(http.status  == 200) {
			document.getElementById("subcats").innerHTML = http.responseText;
			
			document.getElementById("catWacht").value = 1;
			setTimeout('clearWacht("")',150);
		}
	}
}

function clearWacht(){
	document.getElementById("catWacht").value = 0;
}

function putInVergelijk(id){
	
	if(document.getElementById('verg_'+id).checked == true){
		var srt = "on";
	}
	else{
		var srt = "off";	
	}
	
	http = createRequestObject();
		
	var mom = document.getElementById('mom').value;
		
	http.open(meth,mom+"manip/putinvergelijk.php?id="+id+"&srt="+srt,true);
	http.setRequestHeader('Content-Type','text/plain');
	http.send(null);
}

jQuery(document).ready(function() {
    jQuery('#laatstBekeken').jcarousel({
        itemLoadCallback: mycarousel_itemLoadCallback
    });
});


function mycarousel_itemLoadCallback(carousel, state){
	var mom = document.getElementById('mom').value;
    if (state != 'init')
        return;

    jQuery.get(mom+'manip/bekeken.php', function(data) {
        mycarousel_itemAddCallback(carousel, carousel.first, carousel.last, data);

    });
};

function mycarousel_itemAddCallback(carousel, first, last, data){

    var items = data.split('|');
	var totaal = items.length-1;

    for (i = 0; i < items.length; i++) {
	    var info = items[i].split('[*]');
        carousel.add(i, mycarousel_getItemHTML(info[1],info[0],info[2],info[3]));
    }

    carousel.size(items.length);
};

function mycarousel_getItemHTML(url,foto,naam,aanbieding){
	var mom = document.getElementById('mom').value;
	var product = '<a href="'+url+'" onmouseover="showtrail(300,300,\''+naam+'\')" onmouseout="hidetrail();">';
	if(aanbieding == '1'){
	
		product += '<img src="'+mom+'img/trans_ster.png" style="position:absolute; float:left;">';
	}

	product += '<img src="'+foto+'"></a>';
    return product;
};

jQuery(document).ready(function() {
    jQuery('#relevanteProducten').jcarousel({
        itemLoadCallback: relevant_itemLoadCallback
    });
});

function relevant_itemLoadCallback(carousel, state){
	var mom = document.getElementById('mom').value;
	var prodId = document.getElementById('prodId').value;
    if (state != 'init')
        return;

    jQuery.get(mom+'manip/relevant.php?id='+prodId, function(data) {
        relevant_itemAddCallback(carousel, carousel.first, carousel.last, data);

    });
};

function relevant_itemAddCallback(carousel, first, last, data){

    var items = data.split('|');
	var totaal = items.length-1;

    for (i = 0; i < items.length; i++) {
	    var info = items[i].split('[*]');
        carousel.add(i, relevant_getItemHTML(info[1],info[0],info[2],info[3]));
    }

    carousel.size(items.length);
};

function relevant_getItemHTML(url,foto,naam,aanbieding){
	var mom = document.getElementById('mom').value;
	var product = '<a href="'+url+'" onmouseover="showtrail(300,300,\''+naam+'\')" onmouseout="hidetrail();">';

	if(aanbieding == '1'){
	
		product += '<img src="'+mom+'img/trans_ster.png" style="position:absolute; float:left;">';
	}
	
	product += '<img src="'+foto+'"></a>';

    return product;
};

function legen(welks,inhoud){
	if(document.getElementById('inp_'+welks).value == inhoud){
		document.getElementById('inp_'+welks).value = "";	
	}
	else{
		if(document.getElementById('inp_'+welks).value == ""){
			document.getElementById('inp_'+welks).value = inhoud;	
		}
	}
}

function checkVelden(){
	
	var names = new Array();
	names[1] = "naam";
	names[2] = "e-mailadres";
	names[3] = "telefoonnummer";
	names[4] = "adres";
	names[5] = "postcode";
	names[6] = "woonplaats";
	
	for(var i = 1; i <= 6; i++){
		if(document.getElementById('inp_'+i).value == "" || document.getElementById('inp_'+i).value == names[i]){
			document.getElementById('inp_'+i).style.border = "1px solid #FF2222";
			document.getElementById('inp_'+i).focus();
			return false;	
		}
		else{
			document.getElementById('inp_'+i).style.border = "1px solid #d9d9d9";
		}
	}
	
	if(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/.test(document.getElementById('inp_2').value)){	
		document.getElementById('inp_2').style.border = "1px solid #d9d9d9";
	}
	else{
		document.getElementById('inp_2').style.border = "1px solid #FF2222";
		document.getElementById('inp_2').focus();
		return false;	
	}

	if(/^0[0-9]{9}$/.test(document.getElementById('inp_3').value.split(' ').join(''))){
		document.getElementById('inp_3').style.border = "1px solid #d9d9d9";		
	}
	else{
		document.getElementById('inp_3').style.border = "1px solid #FF2222";
		document.getElementById('inp_3').focus();

		return false;	
	}

	if(/^[0-9][0-9][0-9][0-9] ?[A-Z][A-Z]$/.test(document.getElementById('inp_5').value.toUpperCase().split(' ').join(''))){
		document.getElementById('inp_5').style.border = "1px solid #d9d9d9";	
	}
	else{
		document.getElementById('inp_5').style.border = "1px solid #FF2222";
		document.getElementById('inp_5').focus();

		return false;
	}
}

function checkConForm(){
	if(document.getElementById('inp_1').value == "" || document.getElementById('inp_1').value == "naam"){
		document.getElementById('inp_1').style.border = "1px solid #FF2222";
		document.getElementById('inp_1').focus();
		return false;	
	}
	else{
		document.getElementById('inp_1').style.border = "1px solid #d9d9d9";
	}
	if(document.getElementById('inp_2').value == "" || document.getElementById('inp_2').value == "e-mailadres"){
		document.getElementById('inp_2').style.border = "1px solid #FF2222";
		document.getElementById('inp_2').focus();
		return false;	
	}
	else{
		document.getElementById('inp_2').style.border = "1px solid #d9d9d9";
	}
	if(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/.test(document.getElementById('inp_2').value)){	
		document.getElementById('inp_2').style.border = "1px solid #d9d9d9";
	}
	else{
		document.getElementById('inp_2').style.border = "1px solid #FF2222";
		document.getElementById('inp_2').focus();
		return false;	
	}
}