/*****************************************************************************

Name:			ecom.js
Author:			RVH
Date: 			06/02/2007

Content:
	This javascript code will provide a link between the client browser and the AJAX.NET
	proxy classes. Can be used for any type of caddy (MiniKart or FullKart)

*****************************************************************************/

// TODO: put this function in a general ajax javascript include!!
function displayResponseInContextElement(response)
{
	// Show error if any...
    if (response.error == null && response.context != null)
	    response.context.innerHTML = response.value;    
    else
      alert(response.error.description);

    $("[id$=LoadingAnimation]").hide();
    RefreshArticlesCount();
} 

function displayErrorResponse(response)
{
	if (response.error != null)
    alert(response.error.description);    
}


//*** MAIN FUNCTIONALITY ****************************************************

// Refreshes all caddy elements that exist on the current page.
function refreshCaddy() {
    $("#AJAX_MiniKart").each(function () {
        var template = $(this).attr("template");

        if (template == '') {
            alert("Missing template for a caddy!");
            return;
        }

        CaddyBridge.GetMiniKartHTML(template, displayResponseInContextElement, this);
        var results = CaddyBridge.GetMiniKartData();
        if (0 < results.value.Rows.length) {
            $("#ShoppingBasket").show();
        }
    });
    $("#AJAX_FullKart").each(function () {
        var template = $(this).attr("template");

        if (template == '') {
            alert("Missing template for a caddy!");
            return;
        }

        CaddyBridge.GetFullKartHTML(template, displayResponseInContextElement, this);
        var results = CaddyBridge.GetMiniKartData();
        if (0 < results.value.Rows.length) {
            $("#ShoppingBasket").show();
        }
    });
}

function RefreshArticlesCount() {
    var articlesCount = $("[id$=ShopItem]").length;
    if (articlesCount > 0) {
        $("#articlesCount").text(articlesCount).show();
        $("#articlesLabel").show();
    }
    else {
        $("#articlesCount").hide();
        $("#articlesLabel").hide();
    }
}

// a callback that can be used to refresh the caddy when AJAX call is complete
function refreshCaddyResponse(response)
{	
	if (response.error != null)
		alert(response.error.description);
	else
	{
		refreshCaddy();
	}
}


// Takes a productID and calls the AJAX AddProduct proxy method to add a product to the caddy.
// Afterwards, all caddy elements must be refreshed.
function addProductToCaddy(productID)
{
    $("[id$=LoadingAnimation]").show();
	CaddyBridge.AddProduct(productID, addProductToCaddyResponse);		
}
function addProductToCaddyResponse(response)
{
	if (response.error != null)
      alert(response.error.description);    
	else
	{
		refreshCaddy();
	}
    $("[id$=LoadingAnimation]").hide();
}

// Takes a productID and calls the AJAX AddProduct proxy method to add a product to the caddy.
// Afterwards, all caddy elements must be refreshed.
function removeProductToCaddy(productID) {
    $("[id$=LoadingAnimation]").show();
    CaddyBridge.RemoveProduct(productID, removeProductToCaddyResponse);
}
function removeProductToCaddyResponse(response) {
    if (response.error != null)
        alert(response.error.description);
    else {
        refreshCaddy();
    }
    $("[id$=LoadingAnimation]").hide();
}

// Takes a productID and calls the AJAX AddProduct proxy method to add a product to the caddy.
// Afterwards, redirect immediatly to Identify-page
function addOneShotProductToCaddy(productID,lang)
{	
	CaddyBridge.AddProduct(productID, addOneShotProductToCaddyResponse,lang);
}
function addOneShotProductToCaddyResponse(response)
{
  if (response.error != null)
      alert(response.error.description);    
	else
	{
    window.location="/ecommerce/"+response.context+"/identify.aspx";
  }
  
}


// Clears the content of the caddy.
// Afterwards, all caddy elements must be refreshed.
function clearCaddy()
{
    CaddyBridge.ClearCaddy(refreshCaddyResponse);
    $("TotalCaddie").hide();
}


// Installs a new payment method for a given product id.
// Afterwards, all caddy elements must be refreshed.
function setPaymentMethod(pid, pType, pFreq)
{
	CaddyBridge.SetPaymentMethod(pid, pType, pFreq, refreshCaddyResponse);
}

// Removes a product from the caddy
function DecreaseProduct(pid)
{
	CaddyBridge.DecreaseProduct(pid, refreshCaddyResponse);
}

// Removes a product entirely from the caddy (all its instances are removed)
function removeProduct(pid)
{
	CaddyBridge.RemoveProduct(pid, refreshCaddyResponse);
}
//***************************************************************************

function addProductToCaddyWithCheck(productID,quantity)
{	
if(quantity<10)addProductToCaddy(productID);
else alert("Veuillez nous contacter pour des commandes avec plus de 10 produits.")
}

/* used for subscriboorskip */
function CheckInput(lang){

if(document.getElementById("emailaddress").value.length > 0)
	{
		if(CheckEmailFormat(document.getElementById("emailaddress").value)==1)
		{
			document.getElementById("subscribeorskipform").submit();
		}
		else
		{
			if(lang=="nl")
				alert("Uw E-mail adres is niet juist of bevat een onaanvaardbaar teken. Een goed voorbeeld: uw_naam@provider.be");
			else
				alert("Votre Adresse E-mail est incorrecte. il manque un . ou un @. Exemple : nom_utilisateur@provider.com");
		}		
	}
else document.getElementById("subscribeorskipform").submit();
}

function CheckEmailFormat(strString) 
{ 
	var REG = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

	if (strString.match(REG)) 
	{ 
		return true;
	} 
	else 
	{ 
		return false;
	} 
}

function Subscribe(lang){
	if(document.getElementById("emailaddress").value.length == 0)
	{
		if(lang=="nl")
			alert("Uw E-mail adres is niet juist of bevat een onaanvaardbaar teken. Een goed voorbeeld: uw_naam@provider.be");
		else
			alert("Votre Adresse E-mail est incorrecte. il manque un . ou un @. Exemple : nom_utilisateur@provider.com");
		}
	else CheckInput(lang);

}
