﻿var IE = document.all?true:false

$(document).ready(function() {

	//Hoofdnavigatie mouseovers
	$("#nav img:not(:.flag)").hover(
		function(){
			var imgsrc = $(this).attr("src")
			if(imgsrc.indexOf('_o.gif')==-1){
				imgsrc = imgsrc.replace('.gif','_o.gif');
				$(this).attr('src',imgsrc);
			}
		},
		function(){
			var thisclass = $(this).attr("class")
			if(thisclass.indexOf('active')==-1){
				var imgsrc = $(this).attr("src").replace('_o.gif','.gif');
				$(this).attr('src',imgsrc);
			}
		}
	);


	//Menu mouseovers
	$("#menu li").hover(
		function(){
			var addClass = '';
			if($(this).attr('class').indexOf('first')>-1) {addClass=' first'}
			if($(this).attr('id').indexOf('activestate')==-1){
				$(this).attr('class','active' + addClass);
				var imgsrc = $(this).find('img').attr("src").replace('_active.gif','.gif').replace('.gif','_active.gif');
				if(imgsrc.indexOf('.png')==-1 && imgsrc.indexOf('blank')==-1){ //geen pngs doen...
					$(this).find('img').attr('src',imgsrc);
				}
			}
		},
		function(){
			var addClass = '';
			if($(this).attr('class').indexOf('first')>-1) {addClass=' first'}
			var thisid = $(this).attr("id")
			if(thisid.indexOf('activestate')==-1 && thisid.indexOf('activestatesub')==-1){
				$(this).attr('class','nonactive' + addClass);
				var imgsrc = $(this).find('img').attr("src").replace('_active.gif','.gif');
				$(this).find('img').attr('src',imgsrc);
			}
		}
	);

	//article mouseovers
	$(".content .article").hover(
		function(){
			$(this).children('div').attr('class','active');
			var imgsrc = $(this).find('.button').attr("src")
			if(imgsrc){
				if(imgsrc.indexOf('_o.gif')==-1){
					imgsrc = imgsrc.replace('.gif','_o.gif');
					$(this).find('.button').attr('src',imgsrc);
				}
			}
		},
		function(){
			$(this).children('div').attr('class','border');
			var imgsrc = $(this).find('.button').attr("src")
			if(imgsrc){
				imgsrc = imgsrc.replace('_o.gif','.gif');
				$(this).find('.button').attr('src',imgsrc);
			}
		}
	);
	
	//homepage toplist mouseactions
	$(".tabs li").click(
		function(){
			var thisid = $(this).attr('id');
			$(this).parent().children().each(
				function(){
					var thisid = $(this).attr('id');
					if(thisid){
						$(this).attr('class','tab');
					}
					//verberg alle containers
					$('#container_'+ thisid).css('display','none');
				}
			)
			$('#container_'+ thisid).css('display','block');
			$(this).attr('class','active tab');
		}
	);

	//Footer mouseovers
	$(".footer_logos img").hover(
		function(){
			var imgsrc = $(this).attr("src");
			if(!imgsrc) imgsrc = '';
			if(imgsrc.indexOf('_mo.gif')==-1){
				imgsrc = imgsrc.replace('.gif','_mo.gif');
				$(this).attr('src',imgsrc);
			}
		},
		function(){
			var imgsrc = $(this).attr("src");
			if(imgsrc) $(this).attr('src',imgsrc.replace('_mo.gif','.gif'));
		}
	);
	
});

function ShowMailaFriend() {AnimateThis('mailafriend')}

function AnimateThis(id) {
	if(!IE){ //werkt niet in IE ???
		$('#' + id).animate({opacity: 0}, 1).animate({opacity: 1}, 500);
	}
}

function showMessage(id){
	display('item-'+ id +'-ok','item-'+ id)
	setTimeout("display('item-"+ id +"','item-"+ id +"-ok')",3000)
}

function ShowShoppingcart() {
    /*
    $('#shoppingcart').animate({
        opacity: 0
    }, 1);
    $('#shoppingcart').animate({
        opacity: 1
    }, 500);
    */
}

function mOn(obj) {
	var ext = obj.src.slice(-3)
	if(obj.src.indexOf('_o.'+ ext)==-1){
		if(obj.src.indexOf('_a.gif')==-1){
			obj.src=obj.src.replace('.'+ ext,'_o.'+ ext)
		}
	}
}
function mOut(obj) {
	var ext = obj.src.slice(-3)
	obj.src=obj.src.replace('_o.'+ext,'.'+ext)
}

function increaseAmount(textid) {
    if (isNaN($('#' + textid).val())) {
        $('#' + textid).val(1);
    } else {
        if (eval($('#' + textid).val()) < 99) {
            $('#' + textid).val(eval($('#' + textid).val()) + 1);
        }
    }
    return true;
}

function decreaseAmount(textid) {
    if (isNaN($('#' + textid).val())) {
        $('#' + textid).val(1);
    } else {
        if (eval($('#' + textid).val()) > 1) {
            $('#' + textid).val(eval($('#' + textid).val()) - 1);
        }
    }
    return true;
}

function toggleCheckbox(id, show, checked){
	//wanneer undefined, bepaal of checkbox aan of uitgezet moet worden...
	if(checked===undefined){
		if(document.getElementById('check_'+id).src.indexOf('_o.gif')>-1){
			checked = false;
		}else{
			checked = true;
		}
	}
	if(checked){
		$('#'+ id).attr('value','1');
		mOn(document.getElementById('check_'+ id));
		$('#'+ show).css('display','block');
	}else{
		$('#'+ id).attr('value','');
		mOut(document.getElementById('check_'+ id));
		$('#'+ show).css('display','none');
	}
}

function toggleTop(obj, id, arr){
	if(arr!=undefined) {
		var a = arr.split(',')
		for(i=0;i<a.length;i++){
			if(document.getElementById(a[i])) document.getElementById(a[i]).style.display='none'
			$('#'+ a[i]).parent().attr('class','');
		}
	}
	$('#'+ id).css('display', 'block');
	$(obj).parent().attr('class','active');
}

// Versie 1.2 (WdS)
function submitNewletterForm(inputId,trueCont,falseCont,wrongCont,path) {
	if(isEmail($('#' + inputId).attr('value'))){
    $.ajax({
        type: "POST",
        url: path + "xml/SubscribeNewsletter?mail=" + $('#' + inputId).attr('value'),
        dataType: "html",
				data: "{}",
        success: function(txt) {            
            if (txt.toUpperCase() == 'TRUE') {
								$('#' + wrongCont).css("display", "none");
                $('#' + trueCont).css("display","block");
                $('#' + falseCont).css("display","none");
            }
            else {
								$('#' + wrongCont).css("display", "none");
                $('#' + trueCont).css("display", "none");
                $('#' + falseCont).css("display", "block");                
            }
        }
    });
	}else{
		$('#' + wrongCont).css("display", "block");
		$('#' + trueCont).css("display", "none");
    $('#' + falseCont).css("display", "none");
	}
}

// Versie 1.2 (JS)
function RemoveSingleViewedArticles(path,id) {
		$.get(path + 'xml/RemoveSingleViewedArticles?id='+id);
		$('#viewedarticles-'+id).html('');
}

// Versie 1.2 (JS)
function RemoveViewedArticles(path) {
    $.get(path + 'xml/RemoveViewedArticles');
    $('#viewedarticles').html('');
}

function printing(cmd){
	if(cmd){ //activate printing
		radio('1','radio0','bedrukking');
		$('#cont_printing').css('display','block');
//		$('#containter_pricesum').css('display','block');
		$('#pricelisting').css('display','block');
	}else{ //deactivate printing
		radio('','radio1','bedrukking');
		$('#cont_printing').css('display','none');
//		$('#containter_pricesum').css('display','none');
		$('#pricelisting').css('display','none');
//		initializePricelisting();
		//clear
		fill('printingText','naam,nr',1,'defaultNames');
	}
}

// Versie 1.3 (GPS)
function fill(dest, source, clear, predefined){
	var arr = source.split(',');
	var textToInsert = ''; var currentValue = '';
	if(predefined){
		var predefinedObj = document.getElementById(predefined);
		var predefinedArr = predefinedObj.options[predefinedObj.selectedIndex].value.split(',');
	}
	if(!clear){
		$.each(arr, function(count, item){
			if(textToInsert!='') textToInsert += ', ';
			if(predefined){
				currentValue = predefinedArr[count];
				$('#'+ item).val(predefinedArr[count])
			}else{
				currentValue = $('#'+ item).val()
			}
			textToInsert += $('#'+ item +'title').val() + currentValue.toUpperCase();
		})
	}else{ //restore
		predefinedObj.selectedIndex = 0;
		$.each(arr, function(count, item){
			$('#'+ item).val($('#'+ item +'value').val());
		});
	}
	$('#' + dest).val(textToInsert);
}

// Versie 1.2 (WdS)
function updateArticleDetail(articleId, orgSpec1, orgSpec2, htmlCont, path) {
    var selectedSpec01, selectedSpec02, printing
    if (document.getElementById(orgSpec1)) {
        selectedSpec01 = document.getElementById(orgSpec1).options[document.getElementById(orgSpec1).selectedIndex].value
    } else {
        selectedSpec01 = 0
    }
    if (document.getElementById(orgSpec2)) {
        selectedSpec02 = document.getElementById(orgSpec2).options[document.getElementById(orgSpec2).selectedIndex].value
    } else {
        selectedSpec02 = 0
    }
		if(!printing) printing = 0
    $.ajax({
        type: "POST",
        url: path + 'xml/UpdateArticleDetail?Id=' + articleId + '&spec01Id=' + selectedSpec01 + '&spec02Id=' + selectedSpec02,
        dataType: "html",
				data: "{}",
        success: function(html) {
					document.getElementById(htmlCont).innerHTML = html;
					initializePricelisting();
        }
    });
}

// Versie 1.3.2 (JS)
function updateArticleFromAddToOrder(articleId, orgSpec1, orgSpec2, htmlCont, path) {
    var selectedSpec01, selectedSpec02
    if (document.getElementById(orgSpec1)) {
        selectedSpec01 = document.getElementById(orgSpec1).options[document.getElementById(orgSpec1).selectedIndex].value
    } else {
        selectedSpec01 = 0
    }
    if (document.getElementById(orgSpec2)) {
        selectedSpec02 = document.getElementById(orgSpec2).options[document.getElementById(orgSpec2).selectedIndex].value
    } else {
        selectedSpec02 = 0
    }
    $.ajax({
        type: "POST",
        url: path + 'xml/UpdateArticleDetail?Id=' + articleId + '&spec01Id=' + selectedSpec01 + '&spec02Id=' + selectedSpec02 + '&viewName=ArticleAddToOrder',
        data: {},
        dataType: "html",
        success: function(html) {            
            $('#' + htmlCont).html(html);
        }
    });
}

// Versie 1.3.4 (GPS)
//function AddRelatedArticlesToOrder(path, checkout_url) {
//    try {
//        var error = false;
//				//Haal ook de gegevens op van het huidige product
//				var articleId = $('#articleId').val();
//				var articleSpecId = $('#specId').val();
//				var articleAmount = $('#amount-' + articleId).val();
//				var articlePrintingText = $('#printingText').val();
//
//				$.ajax({
//						type: 'GET',
//						url: path + 'xml/AddToShoppingCart',
//						data: 'articleId=' + articleId + '&amount=' + articleAmount + '&printingText='+ articlePrintingText +'&specId=' + articleSpecId,
//						success: function(html) {
//							document.getElementById('shoppingcart').innerHTML = html;
//						}
//				});
//				//einde ophalen gegevens van het huidige product
//
//				$("[name=addToOrder]").each(function() {
//					if($(this).val()!=''){
//						var tmpStr = $(this).attr('id');
//						var id = tmpStr.substr(tmpStr.indexOf('-') + 1);
//						var specId = $('#specId-' + id).val();
//						var amount = $('#amount-' + id).val();
//						if ($('#spec1-' + id).val() == '0' || $('#spec2-' + id).val() == '0') {
//								//alert('Specificatie is niet ingevuld')
//								error = true;
//						} else {
//								$.ajax({
//										type: 'GET',
//										url: path + 'xml/AddToShoppingCart',
//										data: 'articleId=' + id + '&amount=' + amount + '&printingText=&specId=' + specId,
//										success: function(html) {
//											document.getElementById('shoppingcart').innerHTML = html;
//										}
//								});
//						}
//					}
//				});
//				if (error){
//					$('#item-'+ articleId +'-ok-text').css('display','none');
//					$('#item-'+ articleId +'-ok-error').css('display','block');
//					return false;
//				}
//    } catch (err) {
//        alert("Er is iets foutgegaan. Probeer het opnieuw door deze pagina te herladen. " + err.message);
//    }
//}

// Versie 1.3 (JS)
function AddRelatedArticlesToOrder(path, checkout_url, related) {
    try {
        var timeout = 0;
        var error = false;
				var id = 0;
				var tmpStr = '';
				var newCart = '';

				//gegevens van hoofdartikel
				var articleId = $('#articleId').val();
				var articleSpecId = $('#specId').val();
				var articleAmount = $('#amount-' + articleId).val();
				var articlePrintingText = $('#printingText').val();
				if(!articlePrintingText) articlePrintingText = '';

				//check of alle specs ingevuld zijn...
				$("[name=addToOrder]:checked").each(function() {
					tmpStr = $(this).attr('id');
				 	id = tmpStr.substr(tmpStr.indexOf('_') + 1);
					if ($('#spec1-' + id).val() == '0' || $('#spec2-' + id).val() == '0') {

						display('item-'+ articleId +'-ok-error','item-'+ articleId +'-ok-text');
						display('item-'+ articleId +'-ok','item-'+ articleId);
						error = true;
					}
				});

				// Indien geen error, dan alle (eventueel) aangevinkte artikelen aan winkelwagen toevoegen.
				if(!error) {
					display('item-'+ articleId +'-ok-text','item-'+ articleId +'-ok-error');

					$("[name=addToOrder]:checked").each(function() {
							tmpStr = $(this).attr('id');
							id = tmpStr.substr(tmpStr.indexOf('_') + 1);
							var specId = $('#specId-' + id).val();
							var amount = $('#amount-' + id).val();
							if ($('#spec1-' + id).val() == '0' || $('#spec2-' + id).val() == '0') {
									error = true;
							} else {
									timeout += 600;
									$.ajax({
											type: 'GET',
											url: path + 'xml/AddToShoppingCart',
											data: 'articleId=' + id + '&amount=' + amount + '&printingText=&specId=' + specId
									});
							}
					});

					//Haal ook de gegevens op van het huidige product
					timeout += 600;
					$.ajax({
							type: 'GET',
							url: path + 'xml/AddToShoppingCart',
							data: 'articleId=' + articleId + '&amount=' + articleAmount + '&printingText='+ articlePrintingText +'&specId=' + articleSpecId,
							success: function(html) {
								document.getElementById('shoppingcart').innerHTML = html;
							}
					});
					//einde ophalen gegevens van het huidige product
					display('item-'+ articleId +'-ok','item-'+ articleId);
        }
				setTimeout("display('item-"+ articleId +"','item-"+ articleId +"-ok')", 2000);
				return false;
    } catch (err) {
        alert("Er is iets foutgegaan. Probeer het opnieuw door deze pagina te herladen. " + err.message);
    }
}


//update amount bij gerelateerd artikel en zet addToOrder bitje aan
//function addToList(id){
//	if(($('#specId-'+ id).val() + '')!='0'){
//		$('#amount-'+ id).val(parseInt($('#amount-'+ id).val()) + 1);
//		$('#addToOrder-'+ id).val('1');
//		$('#error_relatedarticles').html('');
//		$('#related-button-'+ id).css('display','none');
//	}else{
//		$('#error_relatedarticles').html(' - U heeft geen specificatie(s) geselecteerd.');
//	}
//	initializePricelisting();
//}

//toon prijslijst bij detail van artikel
//function initializePricelisting(){
//	var listing = "<table cellpadding=0 cellspacing=0 border0 width=100%>" + 
//								 "<col style='width:120px;' />" + 
//								 "<col />"
//	$('.combi .article').each(
//		function(){
//			var itemId = $(this).find('.relatedId').val();
//			var selected = $('#addToOrder-'+ itemId).val();
//			if(selected){
//				var amount = $('#amount-'+ itemId).val();
//				var price = $('#related-price-'+ itemId).val();
//				price = parseInt(amount) * parseFloat(price.replace(',','.'));
//				var total = $('#totalPrice').val();
//				total = parseFloat(total.replace(',','.'));
//				
//				listing += "<tr><td>"+ amount +"x "+ $('#related-title-'+ itemId).val() +"</td><td>"+ currencySign +" "+ price.toFixed(2)  +"</td></tr>";
//				
//				total = (total + price).toFixed(2);
//				$('#totalPrice').val(total);
//				total = currencySign + ' ' + total;
//				$('#total_price').text(total.replace('.',','));
//				$('#pricelisting').css('display','block');
//			}
//		}
//	);
//	listing += "</table>"
//	$('#extra_lines').html(listing);
//
//}

function expandFlash(bool){
	if(bool){
		$('#container_info').css('display','none');
		$('#container_tellafriend').css('display','none');
		$('#container_image').attr('class','articleimage_wide')
	}else{
		$('#container_image').attr('class','articleimage')
		$('#container_info').css('display','block')
		$('#container_tellafriend').css('display','block');
	}
}

function openBig(url){
	if(url!='') $('#bigimage').attr('href',url);
	document.getElementById('bigimage').onclick();
}

function toggleOrder(id){
	$('#item-'+id).css('display','block');
	$('#line-'+id).css('color','#333333');
	var thisurl = $('#arrow-'+id).attr('src');
	if(thisurl.indexOf('_o.gif')==-1){
		$('#arrow-'+id).attr('src',thisurl.replace('.gif','_o.gif'));
	}
}

//functie voor setten van 1 checkbox, geen komma's in value
//v = waarde true
//w = waarde false
function checkThis(v, w, id, input) {
	var obj = document.getElementById(input)
	if(obj.value.indexOf(v)==-1){
		obj.value=v;
		document.getElementById(id).src = document.getElementById(id).src.replace('.gif','_o.gif')
	}else{
		obj.value=w;
		document.getElementById(id).src = document.getElementById(id).src.replace('_o.gif','.gif')
	}
}

// Versie 1.7 (JS)
function addCouponCode(val, path) {
	$('#error-coupon').hide();
	$.ajax({
		type: "POST",
		url: path + 'xml/AddCouponCode?typeid=5&code=' + val,
		data: {},
		dataType: "html",
		success: function(html) {
			if (html == 'True') {
				location.reload(true);
			} else {
				$('#error-coupon').show();
			}
		}
	});
}

// Versie 1.7 (JS)
function removeCouponCode(val, path) {
	$('#error-coupon').hide();
	$.ajax({
		type: "POST",
		url: path + 'xml/RemoveCouponCode?code=' + val,
		data: {},
		dataType: "html",
		success: function(html) {
			if (html == 'True') {
				location.reload(true);
				
			} else {
				$('#error-coupon').show();
			}
		}
	});
}	

function setDateOfBirth() {
    if (document.getElementById("dob_dd").options[document.getElementById("dob_dd").selectedIndex].value == "" || document.getElementById("dob_mm").options[document.getElementById("dob_mm").selectedIndex].value == "" || document.getElementById("dob_yy").options[document.getElementById("dob_yy").selectedIndex].value == "") {
        document.getElementById("dateofbirth_DD").value = '';
    } else {
        document.getElementById("dateofbirth_DD").value = document.getElementById("dob_dd").options[document.getElementById("dob_dd").selectedIndex].value + '-' + document.getElementById("dob_mm").options[document.getElementById("dob_mm").selectedIndex].value + '-' + document.getElementById("dob_yy").options[document.getElementById("dob_yy").selectedIndex].value
    }
}