﻿/*	
    JavaScript library for TheDutchDesignBakery.com
	Copyright Aspin internet solutions, Groningen.
*/
function newSearch(artId){
    $("#leftContent input[@type='checkbox']").each(function() { 
        this.checked = false; 
    });
    $("#leftContent #" + artId)[0].value="";
}

function clickCategory(id){
    var element=$("#search #category"+id)[0];
    var display=$(element).css("display")=="block";
    var src = $("#categoryToggle" + id).attr("src");
    if(display){
        $("#categoryToggle" + id).attr("src",src.replace(/hide/,"show"));$(element).hide("slow");
    }else{
        $("#categoryToggle" + id).attr("src",src.replace(/show/,"hide"));$(element).show("slow");
    }
}

function toggleDDA(clickedValue){
	if(clickedValue == 1)
		$(".deliveryInfo").attr("style", "display:");
	else
		$(".deliveryInfo").attr("style", "display:none");
	showCorrectCosts();
}


/*
 * Automatic retrieval of sizes and prices
 */
 
function getStock(articleId, colorId, sizeId, busy){
    if (sizeId == null) {
        $("#articleSizes").html(busy);
    }
    $("#articlePrice").html(busy);
    $("#articleStatus").html(busy);
    $("#order")[0].disabled = true;

    $.ajax({
        type: "POST",
        url: "../XMLGetStock.ashx",
        data: "ArticleId=" + articleId + "&ColorId=" + colorId + ((sizeId != null) ? "&SizeId=" + sizeId : ""),
        datatype: "xml",
        success: function(xml){onSuccess_getStock(xml,articleId,colorId,sizeId,busy);},
        error: function(){onError_getStock();}
    });
}

function onSuccess_getStock(xml, articleId, colorId, sizeId, busy){
    if (sizeId == null) {
        var options = "";
        $(xml).find("sizes").children().each(function(){
            options += "<option value=\"" + $(this).attr("id") + "\">" + $(this).attr("text") + "</option>";
        });
        $("#articleSizes").html("<select id=\"ddlSizes\" class=\"list\" onchange=\"getStock('" + articleId + "','" + colorId + "',this.value,'" + busy + "');\">" + options + "</select>");
    }
    
    var offerPrice = $($(xml).find("sizes").children()[0]).attr("offerprice");
    
    if (offerPrice != null) {
        $("#articlePrice").html(
            "<span class=\"offer\">&nbsp;"+$($(xml).find("sizes").children()[0]).attr("price")+"&nbsp;</span> " + offerPrice);
    } else {
        $("#articlePrice").html($($(xml).find("sizes").children()[0]).attr("price"));
    }
    $("#articleStatus").html($($(xml).find("sizes").children()[0]).attr("status"));
    $("#order")[0].disabled = Number($($(xml).find("sizes").children()[0]).attr("stock")) <= 0;
}
function onError_getStock() { }

/*
 * Dynamically add a product to the basket
 */
 
function add2Basket(articleId) {
    var colorId = $("#ddlColors")[0].value;
    var sizeId = $("#ddlSizes")[0].value;
    
    $.ajax({
        type: "POST",
        url: "../XMLAdd2Basket.ashx",
        data: "ArticleId=" + articleId + "&ColorId=" + colorId + "&SizeId=" + sizeId,
        datatype: "xml",
        success: function(xml){onSuccess_add2Basket(xml);},
        error: function(){onError_add2Basket();}
    });
}

function onSuccess_add2Basket(xml){
    location.href='Basket.aspx';
    return false;
}
function onError_add2Basket() { }


/*
 * Handle the actual ordering of a basket
 */
 
function processBasket(form, genericError, targetUrl){

    $("#btnSubmit")[0].disabled = true;
    $("#discountCouponAlert").css({"display":""});

    selectedTitle = $('input[name=orderTitle]:checked').val();
    toggleAdress = $('input[name=toggleAdress]:checked').val();
    orderPayMethodValue = $('#orderPayMethod').val();
    orderPayMethodText = $('#orderPayMethod:selected').text();
    orderGrosAmount = $('input[name=amount]').val();
    
    //Get correct country
    country = (form.orderCountrySelection.value==0? "Nederland": form.orderCountry.value);
    countryDelivery = (form.orderCountryDeliverySelection.value==0? "Nederland": form.orderCountryDelivery.value);
    
    var shipForeign = ((form.orderCountryDeliverySelection.value==1&&toggleAdress==1) || (form.orderCountrySelection.value==1 && toggleAdress==0));
    var giftPaper = ($('input[name=orderPaper]:checked').val()==1);
    var registerNewsletter = ($('input[name=orderNews]:checked').val()==1);

    dataFromForm = 
        "orderTitle="+selectedTitle+"&orderInitials="+form.orderInitials.value+"&orderInsertion="+form.orderInsertion.value+"&orderLastname="+form.orderLastname.value+
        "&orderMail="+form.orderMail.value+"&orderPhone="+form.orderPhone.value+"&orderComments="+form.orderComments.value+
        "&orderAddress="+form.orderAddress.value+"&orderZip="+form.orderZip.value+"&orderCity="+form.orderCity.value+"&orderCountry="+country+
        "&orderNameDelivery="+form.orderNameDelivery.value+"&orderAddressDelivery="+form.orderAddressDelivery.value+"&orderZipDelivery="+form.orderZipDelivery.value+
        "&orderCityDelivery="+form.orderCityDelivery.value+"&orderCountryDelivery="+countryDelivery+"&toggleAdress="+toggleAdress+
        "&orderPayMethodValue="+orderPayMethodValue+"&orderPayMethodText="+orderPayMethodText+"&orderGrosAmount="+orderGrosAmount+
        "&orderCurrency="+form.currency.value+"&orderLanguage="+form.language.value + "&shipForeign=" + ((shipForeign) ? 1 : 0) + 
        "&orderDiscountCoupon="+form.orderDiscountCoupon.value+"&giftPaper=" + ((giftPaper) ? 1 : 0)+
        "&registerNewsletter="+((registerNewsletter) ? 1 : 0);

    $.ajax({
        type: "POST",
        url: "../XMLOrderBasket.ashx",
        data: dataFromForm,
        datatype: "xml",
        success: function(xml){onSuccess_processBasket(xml, form, targetUrl);},
        error: function(){onError_processBasket(genericError);}
    })
}

function onSuccess_processBasket(xml, form, targetUrl){
    id = $(xml).find("orderId").text();
    sha1 = $(xml).find("sha1").text();
    qsVal = $(xml).find("id").text();
    amount = $(xml).find("amount").text();
    if(id!=""&&sha1!=""){
        $('input[name=orderID]').val(id);
        $('input[name=SHASign]').val(sha1);
        $('input[name=amount]').val(amount);
        $('input[name=accepturl]').val(targetUrl+'?id='+qsVal);
        $('input[name=declineurl]').val(targetUrl+'?id='+qsVal);
        $('input[name=exceptionurl]').val(targetUrl+'?id='+qsVal);
        $('input[name=cancelurl]').val(targetUrl+'?id='+qsVal);
        
        // Automatic redirect to the relevant RIK payment method page
        switch(Number($('#orderPayMethod').val())){
            case 3:
                $('input[name=PM]').val('iDEAL');
                break;
            case 4:
                $('input[name=PM]').val('CreditCard');
                $('input[name=BRAND]').val('MasterCard');
                break;
            case 5:
                $('input[name=PM]').val('CreditCard');
                $('input[name=BRAND]').val('VISA');
                break;
        }

        form.action = $(xml).find("target").text();
        form.submit();
    }
    else {
        $(xml).find("outofstock").children().each(function(){
            $("#"+$(this).attr("key")).css({"display":"block"});
        });
        if($(xml).find("invalidCoupon").text()!=""){
            $("#discountCouponAlert").html($(xml).find("invalidCoupon").text());
            $("#discountCouponAlert").css({"display":"block"});
        }
    }
    $("#btnSubmit")[0].disabled = false;
}
function onError_processBasket(genericError){
    alert("not okay!");
    $("#btnSubmit")[0].disabled = false;
    alert(genericError);
}