var destDiv = null;
var errorDestDiv = null;
var finishedLastRequest = true;
var sourceForm = null;

function submitFormWithAJAX(form, destination_div, error_destination_div, showResultName) {
	//alert(form.action);
	destDiv = destination_div;
	errorDestDiv = error_destination_div;
	sourceForm = form;
	var params = "";
	for(i = 0; i < form.elements.length; i++){
		if(form.elements[i].name != null) {
			if((("checkbox" == form.elements[i].type || "radio" == form.elements[i].type) && form.elements[i].checked) ||
			    ("checkbox" != form.elements[i].type && "radio" != form.elements[i].type)){
				var my_value=encodeURI(form.elements[i].value);
				if(i == 0) {
					params = params + form.elements[i].name+"="+my_value;
				}else{
					params = params + "&"+ form.elements[i].name+"="+my_value;
				}					
			}
		}
	}
	//alert(params);
	if(finishedLastRequest) {
		finishedLastRequest = false;
		
		callbackName = showResultName;
		if (callbackName == null || "" == callbackName) 
			callbackName = "showResult";
		return new AJAXRequest("post", form.action, params, eval(callbackName));	
	}

}
function showResult(myAJAX){
    if (myAJAX.readyState == 4) {
        if (myAJAX.status == 200) {
        	pResponseText = myAJAX.responseText;
        	var errorindex = pResponseText.indexOf("<error-from-ajax>");
        	if(errorindex != -1) {
        		var enderrorindex = pResponseText.lastIndexOf("</error-from-ajax>");
        		var errorMsg = pResponseText.substring(errorindex+17,enderrorindex);
        		var index = enderrorindex + 18;
        		if(document.getElementById(errorDestDiv)){
        			document.getElementById(errorDestDiv).innerHTML = errorMsg;
        		}
				pResponseText = pResponseText.substring(index);
        	}
        	if(document.getElementById(destDiv)){
				document.getElementById(destDiv).innerHTML = pResponseText;
        	}
		} else {
            alert("There was a problem retrieving the XML data:\n" + myAJAX.statusText);
        }
        finishedLastRequest = true;
    } 			
}

function showDecisionResult(myAJAX){
    if (myAJAX.readyState == 4) {
        if (myAJAX.status == 200) {
        	pResponseText = myAJAX.responseText;
        	var errorindex = pResponseText.indexOf("<error-from-ajax>");
        	if(errorindex != -1) {
        		var enderrorindex = pResponseText.lastIndexOf("</error-from-ajax>");
        		var errorMsg = pResponseText.substring(errorindex+17,enderrorindex);
        		var index = enderrorindex + 18;
        		document.getElementById(errorDestDiv).innerHTML = errorMsg;
        		displayPopin(errorDestDiv,'-50');
        	}
        	if(errorindex == -1){
        		document.getElementById(destDiv).innerHTML = pResponseText;
        		document.getElementById(errorDestDiv).style.display = "none";
        	}
		} else {
            alert("There was a problem retrieving the XML data:\n" + myAJAX.statusText);
        }
        finishedLastRequest = true;
    } 			
}

function showComparePopin(myAJAX){
    if (myAJAX.readyState == 4) {
        if (myAJAX.status == 200) {
        	pResponseText = myAJAX.responseText;
        	var errorindex = pResponseText.indexOf("<error-from-ajax>");     
        	//alert(pResponseText);   	
        	if(errorindex != -1) {
        		var enderrorindex = pResponseText.lastIndexOf("</error-from-ajax>");
        		var errorMsg = pResponseText.substring(errorindex+17,enderrorindex);
        		var index = enderrorindex + 18;
        		document.getElementById(errorDestDiv).innerHTML = errorMsg;
        		displayPopin(errorDestDiv,'-440','40');
        		if (sourceForm != null) { 
        			formName = new String(sourceForm.name);
        			if (formName.indexOf("CheckCompare_") != -1)
	        			sourceForm.addCompare.checked = false;
        		}
        	}
        	if(errorindex == -1){
        		document.getElementById(destDiv).innerHTML = pResponseText;
        		document.getElementById(errorDestDiv).style.display = "none";
        	}
		} else {
            alert("There was a problem retrieving the XML data:\n" + myAJAX.statusText);
        }
        finishedLastRequest = true;
    } 			
}

function doCompare(myAJAX){
    if (myAJAX.readyState == 4) {
        if (myAJAX.status == 200) {
        	pResponseText = myAJAX.responseText;
        	var errorindex = pResponseText.indexOf("<error-from-ajax>");     
        	//alert(pResponseText);   	
        	if(errorindex != -1) {
        		var enderrorindex = pResponseText.lastIndexOf("</error-from-ajax>");
        		var errorMsg = pResponseText.substring(errorindex+17,enderrorindex);
        		var index = enderrorindex + 18;
        		document.getElementById(errorDestDiv).innerHTML = errorMsg;
        		displayPopin(errorDestDiv,'-440','40');
        	}
        	if(errorindex == -1){
        		redirectDoCompare();
        	}
		} else {
            alert("There was a problem retrieving the XML data:\n" + myAJAX.statusText);
        }
        finishedLastRequest = true;
    } 			
}

/*
function processAddToCart( myAJAX ) {
    if (myAJAX.readyState == 4) {
        if (myAJAX.status == 200) {
            var response = ;
            var xmlDocument =  Sarissa.getDomDocument();
		    xmlDocument = (new DOMParser()).parseFromString(response, "text/xml");
		    var shoppingCartData  = xmlDocument.getElementsByTagName("shoppingCartData");		    
		    var numberOfItems = null;
		    var grandTotal = null;
		    var numberAdjustments = null;
		    var numberOfPromos = null;
		    var currency = null;
		    var itemId1 = null;
		    var itemId2 = null;
		    var itemId3 = null;
		    var itemCatId1 = null;
		    var itemCatId2 = null;
		    var itemCatId3 = null;		    
		    var itemImage1 = null;
		    var itemImage2 = null;
		    var itemImage3 = null;
		    var errorMsg = null;

	        for(var i=0; i< shoppingCartData.length; i++) {
		        shoppingCartDataAttrs = shoppingCartData[i].attributes;		        
		        tmpNumberOfItems = shoppingCartDataAttrs.getNamedItem("numberOfItems").nodeValue;
		        tmpNumberOfPromos = shoppingCartDataAttrs.getNamedItem("numberOfPromos").nodeValue;
		        tmpGrandTotal = shoppingCartDataAttrs.getNamedItem("grandTotal").nodeValue;
		        tmpNumberAdjustments = shoppingCartDataAttrs.getNamedItem("numberAdjustments").nodeValue;
		        tmpCurrency = shoppingCartDataAttrs.getNamedItem("currency").nodeValue;
		        tmpitemId1 = shoppingCartDataAttrs.getNamedItem("itemId1").nodeValue;
		        tmpitemId2 = shoppingCartDataAttrs.getNamedItem("itemId2").nodeValue;
		        tmpitemId3 = shoppingCartDataAttrs.getNamedItem("itemId3").nodeValue;
		        tmpitemCatId1 = shoppingCartDataAttrs.getNamedItem("itemCatId1").nodeValue;
		        tmpitemCatId2 = shoppingCartDataAttrs.getNamedItem("itemCatId2").nodeValue;
		        tmpitemCatId3 = shoppingCartDataAttrs.getNamedItem("itemCatId3").nodeValue;
		        tmpitemImage1 = shoppingCartDataAttrs.getNamedItem("itemImage1").nodeValue;
		        tmpitemImage2 = shoppingCartDataAttrs.getNamedItem("itemImage2").nodeValue;
		        tmpitemImage3 = shoppingCartDataAttrs.getNamedItem("itemImage3").nodeValue;
		        tmperrorMsg = shoppingCartDataAttrs.getNamedItem("errorMsg").nodeValue;

		        if(tmpNumberOfItems != null){
		        	numberOfItems = tmpNumberOfItems; 
		        }
		 		if(tmpNumberOfPromos != null){
		        	numberOfPromos = tmpNumberOfPromos; 
		        }
		        if(tmpGrandTotal != null){
		        	grandTotal = tmpGrandTotal; 
		        }
		        if(tmpNumberAdjustments != null){
		        	numberAdjustments = tmpNumberAdjustments; 
		        }
		        if(tmpCurrency != null){
		        	currency = tmpCurrency; 
		        }
		        if(tmpitemId1 != null){
		        	itemId1 = tmpitemId1; 
		        }
		        if(tmpitemId2 != null){
		        	itemId2 = tmpitemId2; 
		        }
		        if(tmpitemId3 != null){
		        	itemId3 = tmpitemId3; 
		        }
		        if(tmpitemCatId1 != null){
		        	itemCatId1 = tmpitemCatId1; 
		        }
		        if(tmpitemId2 != null){
		        	itemCatId2 = tmpitemCatId2; 
		        }
		        if(tmpitemId3 != null){
		        	itemCatId3 = tmpitemCatId3; 
		        }
		        if(tmpitemImage1 != null){
		        	itemImage1 = tmpitemImage1; 
		        }
		        if(tmpitemImage2 != null){
		        	itemImage2 = tmpitemImage2; 
		        }
		        if(tmpitemImage3 != null){
		        	itemImage3 = tmpitemImage3; 
		        }		            		        		        		        		        
		        if(tmperrorMsg != null){
		        	errorMsg = tmperrorMsg; 
		        }		            		        		        		        		        
		    };

		
			if(errorMsg == null || errorMsg == 'null'){
		 		makeMicroCartHTML(numberOfItems, numberOfPromos, grandTotal);
		 		makeMiniCartHTML(numberOfItems, numberOfPromos, grandTotal, itemId1, itemId2, itemId3, itemCatId1, itemCatId2, itemCatId3, itemImage1, itemImage2, itemImage3);
			}else{
				try{
					mErrorMessage = errorMsg;
					document.getElementById("with-or-without-errors").innerHTML = "<font color='red'>"+escape(errorMsg)+"</font>";
				}catch(e){
					//useless IE error.
				}
		 		makeMicroCartHTML(numberOfItems, numberOfPromos, grandTotal);
		 		makeMiniCartHTML(numberOfItems, numberOfPromos, grandTotal, itemId1, itemId2, itemId3, itemCatId1, itemCatId2, itemCatId3, itemImage1, itemImage2, itemImage3);
			}
        } else {
            alert("There was a problem retrieving the XML data:\n" + myAJAX.statusText);
        }
    }
}

function makeMiniCartHTML( numberOfItems, numberOfPromos, grandTotal, itemId1, itemId2, itemId3, itemCatId1, itemCatId2, itemCatId3, itemImage1, itemImage2, itemImage3) {
	var myHTML = "";
	
	if(document.getElementById("ajaxAddToCart2") != null)  {
	if(itemId1 != null && itemId1 != ""){
	myHTML += '<div class="product_box">' +
		'<img src="'+itemImage1+'" width="48" height="48" alt="" class="vignette" /> ' +
		'<a href="/control/product/~category_id='+itemCatId1+'/~product_id='+itemId1+'" title="Voir plus d\'info sur le produit" class="underline">+ d\'infos</a>' +
	'</div> ';
	} else {
	myHTML += '<div class="product_box">' +
		'<img src="/images/espaceur.gif" width="48" height="48" alt="" class="vignette" /> ' +
		'<a href="#" title="Voir plus d\'info sur le produit" class="underline">+ d\'infos</a>' +
		'</div> ';
	}
	if(itemId2 != null && itemId2 != ""){
	myHTML += '<div class="product_box central">' +
		'<img src="'+itemImage2+'" width="48" height="48" alt="" class="vignette" /> ' +
		'<a href="/control/product/~category_id='+itemCatId2+'/~product_id='+itemId2+'" title="Voir plus d\'info sur le produit" class="underline">+ d\'infos</a>' +
	'</div> ';
	} else {
	myHTML += '<div class="product_box">' +
		'<img src="/images/espaceur.gif" width="48" height="48" alt="" class="vignette" /> ' +
		'<a href="#" title="Voir plus d\'info sur le produit" class="underline">+ d\'infos</a>' +
		'</div> ';
	}
	if(itemId3 != null && itemId3 != ""){
	myHTML += '<div class="product_box">' +
		'<img src="'+itemImage3+'" width="48" height="48" alt="" class="vignette" /> ' +
		'<a href="/control/product/~category_id='+itemCatId3+'/~product_id='+itemId3+'" title="Voir plus d\'info sur le produit" class="underline">+ d\'infos</a>' +
	'</div> ';
	} else {
	myHTML += '<div class="product_box">' +
		'<img src="/images/espaceur.gif" width="48" height="48" alt="" class="vignette" /> ' +
		'<a href="#" title="Voir plus d\'info sur le produit" class="underline">+ d\'infos</a>' +
		'</div> ';
	}
	var tmpValue = (numberOfItems - numberOfPromos);		
	myHTML += '<hr class="clear" />' +
	'<div class="summary">'+
		'<span class="color_6CB813">'+tmpValue+' </span> ref/Total TTC' + 
		'<span class="total">'+grandTotal+'</span>' +
	'</div>';
	
	document.getElementById("ajaxAddToCart2").innerHTML = myHTML;
	}
}


function makeMicroCartHTML( numberOfItems, numberOfPromos, grandTotal) {
		if(numberOfItems > 0 ){

			tmpValue = (numberOfItems - numberOfPromos);
			if(numberOfItems - numberOfPromos > 1){
				tmpValue = tmpValue + " produits";
			}else {
			    tmpValue = tmpValue + " produit";
			}		
			tmpValue = tmpValue + " dans mon panier ";
		
		}else{
			tmpValue = ' mon panier est vide';
		}    
        document.getElementById("ajaxAddToCart").innerHTML = tmpValue;
}

function buildWindow(message1, message2, message3, message4, url) {
	return '<div id="theContinueShoppingWindow" style="border:2px solid #ccc;background:#fff;width:150px;height:100px;color:#333;text-align:left;padding:5px">' +
						     '<b style="font-size:14px;">'+message1+'</b><br/>' + 
						     '<div style="padding:4px 0;">'+message2+'</div> <div style="padding:4px 0;">' +
						     '<a style="color:#007856" href="#" onclick="document.getElementById(\'theContinueShoppingWindow\').style.visibility=\'hidden\';">' + 
						     message3 + '</a></div><div style="padding:4px 0;">' +
						     '<a style="color:#007856" href="'+url+'">' +
						     message4 + '</a></div></div>';
}						    

function _hbAddCart(HBXPRODUCTNAME, HBXPRODUCTPRICE, HBXPRODUCTQTY, HBXTIMEZONE, HBXACCTNO, HBXCATEGORY){
	try{
		if (HBXCATEGORY == null || HBXCATEGORY == ""){
			HBXCATEGORY='CATEGORIES';  //default category value
		}
	    _hbSet('epg', 'n');
	    _hbSet('cam', '0');
	    _hbSet('pv', '0');
	    _hbSet('abd_type', 'cart_add');
	    _hbSet('product', HBXPRODUCTNAME);
	    _hbSet('price', HBXPRODUCTPRICE);
	    _hbSet('quantity', HBXPRODUCTQTY);
	    _hbSet('brand', 'BRAND');  //default brand value
	    _hbSet('category', HBXCATEGORY);
	    _hbSet('store', '1'); //default store value
	    _hbSet('tz', HBXTIMEZONE);
		_hbSet('aid', HBXACCTNO);
	    _hbPageView("AddCartPage", "/commerce"); //content category and page name   
	}catch(e){}
}
*/