﻿var IE = document.all?true:false

$(document).ready(function() {
    // Artikel item mouseover/mouseout.
    $(".articleItem").hover(function() {
        $(".articleItem-header", this).addClass("articleItem-header-active");
        $(".articleItem-content", this).addClass("articleItem-content-active");
        $("a:first", this).addClass("active");
    }, function() {
        $(".articleItem-header", this).removeClass("articleItem-header-active");
        $(".articleItem-content", this).removeClass("articleItem-content-active");
        $("a:first", this).removeClass("active");
    }).click(function() {
        location.href = $("a:first", this).attr("href");
    });

	//Menu mouseovers
	$("#menu div").hover(
		function(){
			var className = $(this).attr("class")
			var imgsrc = $('img', this).attr("src")
			if(className.indexOf('_on')==-1){
				$(this).attr("class",className + '_on');
				imgsrc = imgsrc.replace('.gif','_o.gif');
				$('img', this).attr("src",imgsrc);
			}
		},
		function(){
			var className = $(this).attr("class").replace('_on','');
			var imgsrc = $('img', this).attr("src").replace('_o.gif','.gif');
			$(this).attr("class",className);
			$('img', this).attr("src",imgsrc);
		}
	);
});

function ShowMailaFriend() {AnimateThis('mailafriend')}
function ShowShoppingcart() {
		AnimateThis('shoppingcart')
		document.location.href=PROJECT_URL + 'winkelwagen/'
}

function AnimateThis(id) {
	if(!IE){ //werkt niet in IE ???
		$('#' + id).animate({opacity: 0}, 1).animate({opacity: 1}, 500);
	}
}

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 setFlashWidth(divid, newW){
	document.getElementById(divid).style.width = newW+"px";
}

function setFlashHeight(divid, newH){
	document.getElementById(divid).style.height = newH+"px";  
}

function setFlashSize(divid, newW, newH){
	setFlashWidth(divid, newW);
	setFlashHeight(divid, newH);
}

// Versie 1.2 (WdS)
function updateArticleDetail(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,
        dataType: "html",
        data: "{}",
        success: function(html) {
            $('#' + htmlCont).html(html);         
        }
    });
}

// Versie 1.3.2 (WdS)
function toggleCheckbox(id, show, hide) {       
    if ($('#' + id).attr('value') == ',1,') {
        $('#' + show).css('display', 'block');
        if (hide) $('#' + hide).css('display', 'none');
    } else {
        $('#' + show).css('display', 'none');
        if (hide) $('#' + hide).css('display', 'block');
    }
}

function RemoveViewedArticles(path) {
    $.get(path + 'xml/RemoveViewedArticles');
    $('#viewedarticles').html('');
}

// Versie 1.2 (JS)
// Versie 1.3.2 (WdS) #viewedarticles-' + id wordt verwijdert inplaats van leeggemaakt
function RemoveSingleViewedArticles(path, id) {
    $.get(path + 'xml/RemoveSingleViewedArticles?id=' + id);
    $('#viewedarticles-' + id).remove('');        
    if ($('#viewedarticles').html().indexOf('viewedarticles-') == -1) {
        $('#viewedarticles').html('');
    }
}

// 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 (JS)
function AddRelatedArticlesToOrder(path, checkout_url) {
    try {
        var timeout = 0;
        var error = false;
        // Alle aangevinkte artikelen opvragen en in winkelwagentje plaatsen.
        if ($("[name=addToOrder]:checked").size() == 0) {
            $('#marker_relatedArticles').html('U heeft geen items aangevinkt<br>');
            error = true;
        } else {
            //Haal ook de gegevens op van het huidige product
        var articleId = $('#articleId').val();
        var articleSpecId = $('#specId').val();
        var articleAmount = $('#amount-' + articleId).val();
        timeout += 600;
        $.ajax({
            type: 'GET',
            url: path + 'xml/AddToShoppingCart',
            data: 'articleId=' + articleId + '&amount=' + articleAmount + '&printingText=&specId=' + articleSpecId
        });
        
        
            //einde ophalen gegevens van het huidige product
            
            $("[name=addToOrder]:checked").each(function() {
                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') {
                    $('#marker_relatedArticles').html('U heeft geen specificaties gekozen bij een van de aangevinkte producten');
                    //alert('Specificatie is niet ingevuld')
                    error = true;
                } else {
                    timeout += 600;
                    $.ajax({
                        type: 'GET',
                        url: path + 'xml/AddToShoppingCart',
                        data: 'articleId=' + id + '&amount=' + amount + '&printingText=&specId=' + specId
                    });
                }
            });
        }
        if (!error){
            setTimeout(function() { location.href = checkout_url; }, timeout);
            return false
        }else{
            return false;
        }
    } catch (err) {
        alert("Er is iets foutgegaan. Probeer het opnieuw door deze pagina te herladen. " + err.message);
    }
}

function viewURL(url){
	window.location = url;
}



//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.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");
	}
}