// Functie voor het aanmaken van het ajax object
function ajaxinit()
{
	var xmlhttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	 try {
	  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
	  try {
	   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } catch (E) {
	   xmlhttp = false;
	  }
	 }
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}

  	return xmlhttp;
}

// Functie voor het wijzigen van de afbeeldingen
function changeImage(image, link, url, id)
{
    document.getElementById('imageShow').src = image;
    document.getElementById('imageLink').href = link;

    var http = ajaxinit();
    if(http)
    {
        http.open('GET',url + '/product/images:' + id + ':' + encodeBase64(link) ,true);
        http.onreadystatechange = function() {
            if (http.readyState == 4)
            {
                if (http.status == 200)
                {
                    response = http.responseText;
                    document.getElementById("imageBox").innerHTML = response;
                } else {
                    alert("There was a problem retrieving the XML data:\n" + http.statusText);
                    return false;
                }
            }
        }
        http.send(0);
    }
}

/* Functies webshop */
// Functie voor het toevoegen van een product
function addProduct(articleID, type, url)
{
    var amount = 0;
	
    // We gaan het aantal ophalen
    if(document.getElementById("productAmount"))
    {
        if(document.getElementById("productAmount").value > 0)
        {
            amount = document.getElementById("productAmount").value;
        } else {
            return false;
        }
    } else {
        amount = 1;
    }

    // We gaan nu de request string maken
    var str = "addProduct=true&id=" + articleID + "&amount=" + amount + "&type=" + type;

    // We gaan de request aanmaken en uitvoeren
    var http = ajaxinit();
    if(http)
    {
        http.open('POST',url + '/product/',true);
        http.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
        http.onreadystatechange = function() {
            if (http.readyState == 4)
            {
                if (http.status == 200)
                {
                    response = http.responseText;
                    optionsArray = response.split('#');

                    // We gaan door de options array heen lopen
                    for(i=0;i < optionsArray.length;i++)
                    {
                        // We gaan nu de gegevens van deze option ophalen
                        optionsArray[i] = decodeBase64(optionsArray[i]);
                    }

                    if(optionsArray[0] != '' && optionsArray[1] != '')
                    {
                        // We gaan de gegevens invullen
                        document.getElementById("articlesCount").innerHTML = optionsArray[0] + " artikelen";
                        document.getElementById("priceShow").innerHTML = "&euro;&nbsp;" + optionsArray[1];
                        if(optionsArray[2]!='')
                        {
                            //alert(optionsArray[2]);
                        }
                    }

                    if(document.getElementById("productAmount"))
                    {
                        document.getElementById("productAmount").value = "1";
                    }
					
					alert("Het product is aan uw winkelwagen toegevoegd");
                } else {
                    alert("There was a problem retrieving the XML data:\n" + http.statusText);
                    return false;
                }
            }
        }
        http.send(str);
    }
}


// toevoeg functie voor pagina's zonder winkelmandje
function addProduct_simple(articleID, type, url)
{
    var amount = 0;
	
    // We gaan het aantal ophalen
    if(document.getElementById("productAmount"))
    {
        if(document.getElementById("productAmount").value > 0)
        {
            amount = document.getElementById("productAmount").value;
        } else {
            return false;
        }
    } else {
        amount = 1;
    }

    // We gaan nu de request string maken
    var str = "addProduct=true&id=" + articleID + "&amount=" + amount + "&type=" + type;

    // We gaan de request aanmaken en uitvoeren
    var http = ajaxinit();
    if(http) {
        http.open('POST',url + '/product/',true);
        http.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
        http.onreadystatechange = function() {
            if (http.readyState == 4)
            {
                if(http.status == 200) {
					alert("Het product is aan uw winkelwagen toegevoegd");
				} else {
                    alert("Er is een probleem opgetreden bij het verzenden van de productinformatie");
                    return false;
                }
            }
        }
        http.send(str);
    }
}


// Functie voor het verwijderen van een product
function delProduct(url, site, id)
{
    // Even vragen of de gebruiker het zeker weet
    if(window.confirm('Weet u zeker dat u dit product uit de winkelwagen wilt verwijderen ?'))
    {
        // We gaan nu de request string maken
        var str = "delProduct=true&productId=" + id;

        // We gaan de request aanmaken en uitvoeren
        var http = ajaxinit();
        if(http)
        {
            http.open('POST',url + '/winkelwagen',true);
            http.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
            http.onreadystatechange = function() {
                if (http.readyState == 4)
                {
                    if (http.status == 200)
                    {
                        showShoppingCart(url, site);
                        listShoppingCartList(url, site);
                        //listProduct(url, site);
                    } else {
                        alert("There was a problem retrieving the XML data:\n" + http.statusText);
                        return false;
                    }
                }
            }
            http.send(str);
        }
    } else {
        if(document.getElementById("amount[" + id + "]").value < 1)
        {
            document.getElementById("amount[" + id + "]").value = "1";
        }
    }
}

// Functie voor het aanpassen van de aantallen
function changeProductAmount(url, site, productId, productAmount)
{
    // We gaan de send str maken
    var str = "changeAmount=true&site=" + site + "&productId=" + productId + "&productAmount=" + productAmount;

    // We gaan de request aanmaken en uitvoeren
    var http = ajaxinit();
    if(http)
    {
        http.open('POST',url + '/winkelwagen',true);
        http.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
        http.onreadystatechange = function() {
            if (http.readyState == 4)
            {
                if (http.status == 200)
                {
                    showShoppingCart(url, site);
                    listShoppingCartList(url, site);
                    //listProduct(url, site);
                } else {
                    alert("There was a problem retrieving the XML data:\n" + http.statusText);
                    return false;
                }
            }
        }
        http.send(str);
    }
}

// Functie voor het aanpassen van de aantallen in een redelijke afstand
function changeAmount(url, site, productId, amount, prevAmount)
{
    // We gaan even controleren of de huidige nog overeenkomt met de oude
    if(document.getElementById("amount[" + productId + "]").value != prevAmount)
    {
        // We gaan een siteTimeout maken
        setTimeout("changeAmount('" + url + "', '" + site + "', '" + productId + "', '" + amount + "', '" + amount + "')", 500);
    } else {
        if(amount > 0)
        {
            // We gaan de update uitvoeren
            changeProductAmount(url, site, productId, amount);
        } else {
            delProduct(url, site, productId);
        }
    }
    
}

// Functie voor het ophalen van de producten lijst
function listProduct(url, type)
{
    document.getElementById("articlesCount").innerHTML = "Laden...";
    document.getElementById('priceShow').innerHTML = "Laden...";
	
    // We gaan nu de request string maken
    var str = "listProduct=true&type=" + type;

    // We gaan de request aanmaken en uitvoeren
    var http = ajaxinit();
    if(http)
    {
        http.open('POST',url + '/product',true);
        http.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
        http.onreadystatechange = function() {
            if (http.readyState == 4)
            {
                if (http.status == 200)
                {
                    response = http.responseText;
                    optionsArray = response.split('#');

                    // We gaan door de options array heen lopen
                    for(i=0;i < optionsArray.length;i++)
                    {
                        // We gaan nu de gegevens van deze option ophalen
                        optionsArray[i] = decodeBase64(optionsArray[i]);
                    }

                    if(optionsArray[0] != '' && optionsArray[1] != '')
                    {
                        // We gaan de gegevens invullen
                        document.getElementById("articlesCount").innerHTML = optionsArray[0] + " artikelen";
                        document.getElementById("priceShow").innerHTML = "&euro;&nbsp;" + optionsArray[1];
                    }

                } else {
                    alert("There was a problem retrieving the XML data:\n" + http.statusText);
                    return false;
                }
            }
        }
        http.send(str);
    }
}

// Functie voor het updaten van de winkelwagen
function showShoppingCart(url, site)
{
    // We gaan de send str maken
    var str = "getPrices=true&site=" + site;

    // We gaan de request aanmaken en uitvoeren
    var http = ajaxinit();
    if(http)
    {
        http.open('POST',url + '/winkelwagen',true);
        http.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
        http.onreadystatechange = function() {
            if (http.readyState == 4)
            {
                if (http.status == 200)
                {
                    response = http.responseText;
                    optionsArray = response.split('#');

                    // We gaan door de options array heen lopen
                    for(i=0;i < optionsArray.length;i++)
                    {
                        // We gaan nu de gegevens van deze option ophalen
                        optionsArray[i] = decodeBase64(optionsArray[i]);
                    }

                    if(optionsArray[0] != '' && optionsArray[1] != '')
                    {
                        // We gaan de gegevens invullen
                        document.getElementById("total").innerHTML = optionsArray[3];
                        document.getElementById("tax").innerHTML = optionsArray[2];
                        document.getElementById("send").innerHTML = optionsArray[1];
                        document.getElementById("overalTotal").innerHTML = optionsArray[0];
                    }
                } else {
                    alert("There was a problem retrieving the XML data:\n" + http.statusText);
                    return false;
                }
            }
        }
        http.send(str);
    }
}

// Functie voor het opnieuw maken van de winkelwagen inhoud
function listShoppingCartList(url, site)
{
    // We gaan de send str maken
    var str = "getProductList=true&site=" + site;

    // We gaan de request aanmaken en uitvoeren
    var http = ajaxinit();
    if(http)
    {
        http.open('POST',url + '/winkelwagen',true);
        http.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
        http.onreadystatechange = function() {
            if (http.readyState == 4)
            {
                if (http.status == 200)
                {
                    response = http.responseText;
                    document.getElementById("productList").innerHTML = response;
                } else {
                    alert("There was a problem retrieving the XML data:\n" + http.statusText);
                    return false;
                }
            }
        }
        http.send(str);
    }
}
