
var appliedFilters = new Array();
var i = 0;
var url = "";
var s = '';
var selKey = "prods" + "familyid";
var numberOfProduct = '30';

//--------------------------------------------------------------------------------------------------------------
// A Generic Function for Preventing the Default Browser Action from Occurring 
//--------------------------------------------------------------------------------------------------------------
stopDefault = function(e){
    //Prevent the default browser action(W3C) 
    if (e && e.preventDefault) {
        e.preventDefault();
    }
    //A shortcut for stoping the browser action in IE 
    else {
        window.event.returnValue = false;
    }
    return false;
};

function addArray(s){
	appliedFilters[i]=s;
	i++;
	resetCookie();
}

function resetCookie()
{
	var selProducts = readCookie(selKey);
	if(selProducts != null)
    {
		var selProducts = selProducts.replace(selProducts, '');
		dartyCookie(selKey, selProducts);
		
	}
}

function getLocation(s){
	var url = pu;
	var urlRewrite = url.indexOf('DartyProductListDisplayView');
	if(urlRewrite == -1) {
		if((appliedFilters.length == 0) && (pageSize == numberOfProduct) && (so == 0))
			url = url + '?';
	}
	for(cnt = 0; cnt<appliedFilters.length; cnt++){ 
	    url = url + appliedFilters[cnt];
	}
	url = url + s;
	if(pageSize != numberOfProduct){
		url = url + '&pageSize=' + pageSize;
	}
	if(so != 0){
		url = url + '&so=' + so;
	}
	window.location = url;
}

function getFilterParameter(){
	var parameter = '';
	for(cnt = 0; cnt<appliedFilters.length; cnt++){ 
	    parameter = parameter + appliedFilters[cnt];
	}
	if(pageSize != numberOfProduct){
		parameter = parameter + '&pageSize=' + pageSize;
	}
	if(so != 0){
		parameter = parameter + '&so=' + so;
	}
	
	return parameter;
}

function getAllParameter(){
	var parameter = getFilterParameter();
	if (currentpnum != null && currentpnum != '0' && currentpnum != '1'){
		parameter = parameter + '&pnum=' + currentpnum;
	}
	return parameter;
}

function addParameterToUrl(oldUrl, param){
	var newUrl = oldUrl;
	if (param != '') {
		if (newUrl.indexOf('?') > -1){
			newUrl = newUrl + param
		}
		else {
			if (param.indexOf('&') == 0){
				newUrl = newUrl + '?' + param.substring(1, param.length);
			}
			else {
				newUrl = newUrl + '?' + param;
			}
		}
	}
	return newUrl;
}

function populateCheckBox()
	{
	var url1=window.location+"";
	var str1=/pageSize/;
	var str2=/pageno/;
	if(url1.search(str1) == -1 && url1.search(str2) == -1)
	{
		resetCookie();
	}
	
	var prods = readCookie(selKey);
	if(prods!=null)
	{
		var ca = prods.split(',');
		for(var i=0;i < ca.length-1;i++) 
		{
			var c = ca[i];
			var codics = c.split(' ');
			var	codic=codics[1];
			if(codic !=null)
			{
				var fObj = document.forms[ 'selectprod' ];
				for(var j=0; j < fObj.elements.length; j++ ) 
				{
					elmObj= fObj.elements[ j ];
				   	if(elmObj.value == codic)
					{
						elmObj.checked=true;
					}
					
				}
				
			}


		}

	}
}

function removeParam(s){
	var url = pu;
	for(cnt = 0; cnt<appliedFilters.length; cnt++){ 
	    if(appliedFilters[cnt] == s){
	    	appliedFilters[cnt] = '';
	    } 
	    url = url + appliedFilters[cnt];
	}
	if(pageSize != numberOfProduct){
		url = url + '&pageSize=' + pageSize;
	}
	if(so != 0){
		url = url + '&so=' + so;
	}
	window.location = url;
	resetCookie();
}


function pagination(s){
	var indexCode;
	var pageUrl =pu;
	var urlRewrite = pageUrl.indexOf('DartyProductListDisplayView');
	var parameter = getFilterParameter();
	if(urlRewrite == -1) {
		if (s != 1) {
			suf = 'page' + s + '.html';
			pageUrl = pageUrl.substring(0, pageUrl.lastIndexOf('/') + 1) + suf;
		}
	} else {
		pageUrl = pageUrl + '&pnum=' + s;
	}
	return addParameterToUrl(pageUrl, parameter);
	
} 

function removePaginationFilter(oldUrl){
	var newUrl = oldUrl;
	if (newUrl != null){
		newUrl = removeParameterFromUrl(newUrl, 'pnum=\\d');
		var reg1=new RegExp('page\\d', 'g');
		newUrl = newUrl.replace(reg1, 'index');
	}
	return newUrl;
}

function setPageSize(s){
 	var pageUrl = pu;
	var urlRewrite = pageUrl.indexOf('DartyProductListDisplayView');
	var addParameter = '&pageSize=' + s.options[s.selectedIndex].value; 
	if(urlRewrite == -1) {
		if(pageUrl.indexOf('index.html') > -1){
			pageUrl = pageUrl.replace(new RegExp('index\.html', 'g'), "index.pl?");
		} 			
	}
	// remove old so paramter from the url
	pageUrl = removeParameterFromUrl(pageUrl, 'pageSize=[0-9]+');
	if (s.options[s.selectedIndex].value != numberOfProduct){
		pageUrl = pageUrl + addParameter;
	}
	// remove the pagination filter
	pageUrl = removePaginationFilter(pageUrl);
	window.location = pageUrl;
}

function removeParameterFromUrl(oldUrl, parameterRemove){
	var newUrl = oldUrl;
	if (newUrl != null){
		var reg1=new RegExp('&' + parameterRemove, 'g');
		var reg2=new RegExp(parameterRemove + '&', 'g');
		var reg3=new RegExp('\\?' + parameterRemove, 'g');
		newUrl = newUrl.replace(reg1, '');
		newUrl = newUrl.replace(reg2, '');
		newUrl = newUrl.replace(reg3, '');
	}
	return newUrl;
}

function setSortOrder(s){
 	var pageUrl = pu;
	var urlRewrite = pageUrl.indexOf('DartyProductListDisplayView');
	var addParameter = '&so=' + s.options[s.selectedIndex].value; 
	if(urlRewrite == -1) {
		if(pageUrl.indexOf('index.html') > -1){
			pageUrl = pageUrl.replace(new RegExp('index\.html', 'g'), "index.pl?");
		} 			
	}
	// remove old so paramter from the url
	pageUrl = removeParameterFromUrl(pageUrl, 'so=[0-9]+');
	if (s.options[s.selectedIndex].value != 0){
		pageUrl = pageUrl + addParameter;
	}
	window.location = pageUrl;
}

function helpPage(s){	
	for(cnt = 0; cnt<appliedFilters.length; cnt++){ 
	    s = s + appliedFilters[cnt];
	}
	if(pageSize != numberOfProduct){
		s = s + '&pageSize=' + pageSize;
	}
	if(so != 0){
		s = s + '&so=' + so;
	}
	window.location = s;
}

function checkProduit(obj) {
	var prods = readCookie(selKey);
	var text = obj.id + ' ' + obj.value + ',';
	if (prods == null) 
		prods = '';
	if (prods.indexOf(text) == -1) {
		if (obj.checked)
			prods += text;
	} else {
		if (!obj.checked)
			prods = prods.replace(text, '');
	}
	
	dartyCookie(selKey, prods);
}  

function tagXitiChapitre3(chapitre3){
	try {
		Cookie.set("DARTY_XITI_CHAPTER3", chapitre3);
    } catch (e) {
       // Do nothing : it's only XITI
       // DEBUG : alert("error" + e);
	}
}   

	function check_selectprod() {
		url = pcu;
		var fObj = document.forms[ 'selectprod' ];
		for(var i=0; i < fObj.elements.length; i++ ) {
			elmObj= fObj.elements[ i ];
			if (elmObj.checked) 
				checkProduit(elmObj);
		}
		
     	var prods = readCookie(selKey);
		if(prods==null){
			prods=' ';
		}
     	var plist = prods.split(',');
     	var chk = plist.length - 1;
		if ( chk <= 1 || chk > 4 ) {
			alert( 'Veuillez sélectionner de 2 à 4 modèles' );
			return false;
		}

		// There's a trailing ,
		for(var i = 0; i < plist.length - 1; i++ ) {
			var arg = plist[i].split(' ');
			url=url + '&p=' + arg[1];
		}
		if (document.selectprod.checkbox && document.selectprod.checkbox.checked){ 
			document.selectprod.checkbox.checked = false;
		}
		return true;
	}
	
	function selectprodsubmit(){
		if(check_selectprod()){
			dartyCookie(selKey, "");
			document.selectprod.submit();
		}
	}
	
	function action(){
		window.location=url;
		return false;
	}

	function codicPopup(codic) {
		var dom = DARTY.util.Dom;
		var displayPop = DARTY.util.PopUp.displayPopUp;
		var toprodlistpopup = dom.id('toprodlistpopup');
		var toLivDhtmlPopup = dom.id('toLivDhtmlPopup');
		
		if (toprodlistpopup) {
			displayPop({							
			type: 'iframe',
			url: popupUrl + 'codic=' + codic + '&noValidButton=1',
            width: 430,
            height: 430,							
			title: 'Retrait dans votre magasin',
			scrolling: 'auto'
         	});
    	}
    	
	}
	
	function codicLivraisonPopup(codic) {
		var dom = DARTY.util.Dom;
		var displayPop = DARTY.util.PopUp.displayPopUp;
				
    	displayPop({							
		type: 'iframe',
		url:  servletBasePath + 'DartyLivraisonView?storeId=10001&codic=' + codic + '&u=' + universId,
        width: 400,
        height: 530,							
		title: 'Livraison par Darty'
        });
	}  
	
	function applyPriceFilter() {
		var minimumPrice = parseFloat(minPrice.replace(',','.'));
		var maximumPrice = parseFloat(maxPrice.replace(',','.'));
		var minValue = (trim(document.getElementById('pmin').value));
		var maxValue = (trim(document.getElementById('pmax').value));
		var length = priceUrl.length;
		if(minValue.length > 0 || maxValue.length > 0) {
			if(minValue.length>0 && maxValue.length==0) {
				minValue = parseInt(minValue);
				maxValue = 0;
			} else if(minValue.length==0 && maxValue.length>0) { 
				maxValue = parseInt(maxValue);
				minValue = 0;
			} else{
				minValue = parseInt(minValue);
				maxValue = parseInt(maxValue);
			}
		
			if(minValue == 0 && maxValue == 0) {
				alert("Veuillez indiquer une tranche de prix valide"); 
			} else if(maxValue != 0 && minValue > maxValue) {
				alert("Veuillez indiquer une tranche de prix valide");
			} else if(maxValue != 0  && maxValue < minimumPrice) {
				alert("Il n'y a pas de mod\u00E8le \u00E0 moins de "+ maxValue + " \u20AC.");
			} else if(minValue != 0 && minValue > maximumPrice) {
			    alert("Il n'y a pas de mod\u00E8le \u00E0 plus de " +minValue + " \u20AC.");
			} else {		
				var param = '';
				if(minValue == 0) {
					param = "pmax=" + maxValue;
				} else if(maxValue == 0) {
					param = "pmin=" + minValue;
				} else {
					param = "pmin=" + minValue + "&pmax=" + maxValue;
				}
				if(priceUrl.lastIndexOf("&") != priceUrl.length-1) priceUrl = priceUrl + "&"+ param;
				else priceUrl = priceUrl + param;

				tagXitiChapitre3("prix");

				window.location = priceUrl;		
			}
		}
	}
	
	
	function validatePminPrice(event){
		var pmin = document.getElementById('pmin');
	    allowDigits(pmin);
	}
	
	function validatePmaxPrice(event){
		var pmax = document.getElementById('pmax');
	    allowDigits(pmax);
	}
	
	function allowDigits(parameter) { 
		var param = parameter;
		var first = true;
		param.onkeypress = param.onkeyup = function(event){
			if (((window.event && window.event.keyCode == '13') || (event && event.keyCode == '13')) && first){
				first = false;
				applyPriceFilter();
  			}	
  			if(param && (!event || !window.event)){
  				first = true;
    		 	str =  param.value.replace(/[^0-9]+/, "");
	   		 	param.value = str;
	    	}
      	}; 
    }
	//-- Delivery filter popup call 
	function displayDeliveryFilterPop(o) {
	  //-- debug
	  //return true;
	  //-- debug	  
	  var dom = DARTY.util.Dom;
	  var displayPop = DARTY.util.PopUp.displayPopUp;  	 
	  /*
	   o = tag A reference
	  */	  
	  var  popFilterViewUrl, deliveryType;
	  if(o) {
	    popFilterViewUrl = o.href;
	    deliveryType = o.getAttribute('darty:delivery_type');
	  }		  
	  //--	  
	  var POPUP_SIZE = (function(){  
	    var result;
	    switch (deliveryType) {	    
	        case 'LIVRE':	
	            if (dom.isOldBrowser()) { result =  { WIDTH: 446,HEIGHT: 200}; }
	            else if (dom.isIE()) { result =  {WIDTH: 446,HEIGHT: 190}; }
	            else { result =  {WIDTH: 446,HEIGHT: 200}; }
	            break;
	        case 'MIXTE':
	        default:
	            if (dom.isOldBrowser()) { result =  {WIDTH: 446,HEIGHT: 195}; }
	            else if (dom.isIE()) { result =  { WIDTH: 446,HEIGHT: 180}; }
	            else { result =  {WIDTH: 446,HEIGHT: 192 }; }
	            break;	           
	        case 'EXPED':      
	            if (dom.isOldBrowser()) { result =  { WIDTH: 446, HEIGHT: 195 }; }
	            else { result =  { WIDTH: 446, HEIGHT: 195 }; }
	            break;                
	    }
	    return result;
	  })();		  		  
	  //--
	  displayPop({							
		type: 'iframe',
		url: popFilterViewUrl,
        width: POPUP_SIZE.WIDTH,
        height: POPUP_SIZE.HEIGHT,						
		title: 'Livraison &agrave; domicile',					
		scrolling: 'auto'
 	  });   
	  //--
	  return dom.stopDefault({src: o});
	}
	
	//--	
	function displaySlotsPop(o){
	    /*
	       o = { 
	          codic: [codic],
	          ffmCityRelId: [ffmCityRelId],
	          typeLivraison: ['CC'|'LIVRE'|'EXPED']
	       }
	    */
	    //--	
	    var codic = o.codic,
	    	ffmCityRelId = o.ffmCityRelId,
	    	typeLivraison = o.typeLivraison,	    	
	    	url = (function(){
	         	 var result;
		         switch(typeLivraison) {
		           case 'LIVRE':
		           	result = "/webapp/wcs/stores/servlet/DartyDeliverySelectorView?storeId=10001&langId=-2&";
		           	break;
		           case 'EXPED':
		           	result = "/webapp/wcs/stores/servlet/DartyShippingSelectorView?storeId=10001&langId=-2&";
		           	break;
		           case 'MIXTE':
		           	result = "/webapp/wcs/stores/servlet//DartyMixedDeliverySelectorView?storeId=10001&langId=-2&";
		           	break;
		           case 'CC':
		            result = "";
		            break; 	
		         }
		         return result;
	    	})(),
	       	arrParams = [], 
	       	paramsStr = "",
	    	dom = DARTY.util.Dom,
	    	displayPop = DARTY.util.PopUp.displayPopUp;
	    //--
	    if(codic) {
	      arrParams.push("codic=" + codic);
	    }
	    if(ffmCityRelId) {
	       arrParams.push("ffmCityRelId=" + ffmCityRelId);
	    }
	    paramsStr = arrParams.join("&");
	    //--	    
		var POPUP_SLOTS_SIZE = (function(){
		    //-- 
		    if(typeLivraison === 'LIVRE') {
			    if (DARTY.util.Dom.isOldBrowser()) { 
			        return { WIDTH: 446, HEIGHT: 475 }	
			       }
			    else {
			         if (DARTY.util.Dom.isIE()) { return { WIDTH: 446, HEIGHT: 470 } }
			         else { return { WIDTH: 446, HEIGHT: 470 } } 
			       }
			   } 
			 else { 
			   return { WIDTH: 446, HEIGHT: 208 } 
			 }
		})();
		//--
	    displayPop({
	        type: 'iframe',
	        url: url + paramsStr,
	        width: POPUP_SLOTS_SIZE.WIDTH,
	        height: POPUP_SLOTS_SIZE.HEIGHT,
	        title: 'Livraison &agrave; domicile',	        
	        // le parametre est ensuite gere lors du resize des popup                                      
	        scrolling: 'auto'
	    });	    
	}
	
//--
window.bundle_clicked = false;
function toggleBundle(elt){  
    var className = elt.className;  
    //--
    if(window.bundle_clicked) {
      if( className === 'bundle_off') {
        elt.className = 'bundle_on';
      } else {
        elt.className = 'bundle_off';
      }      
    }
	//--
	window.bundle_clicked = false;
}


	