//
// Copyright (c) 2004 Gulliver S.r.L.
// All Rights Reserved.
// DATE: 17-03-2004
// AUTORE: Fabio Pierani
//----------------------------------------------------------------------------------------------------
//
// Variabili
//
        var basketCookieName = "basket";
		var basketCookiePersistenceInHours = 960;
		var basketCookie;
		var basketArray, basketArrayLength;
		var maxBasketArrayLength = 50;
		var spanBasketLength;
		var attributes = new Array();
		var oCodDetail = "";

//----------------------------------------------------------------------------------------------------
//
// Funzione di preload immagini
//

function lubrificantiIndustriali() {
    MM_preloadImages('mobil_lubrificanti/olio-mobil_10_1.jpg',
					 'mobil_lubrificanti/olio-mobil_14_1.jpg',
					 'mobil_lubrificanti/olio-mobil_04_1.jpg',
					 'mobil_lubrificanti/olio-mobil_05_1.jpg',
					 'mobil_lubrificanti/olio-mobil_06_1.jpg',
					 'mobil_lubrificanti/olio-mobil_07_1.jpg',
					 'mobil_lubrificanti/olio-mobil_08_1.jpg');
}

//----------------------------------------------------------------------------------------------------
// Funzione ricerca e catalogo
//----------------------------------------------------------------------------------------------------

function cambiaTipoA() {
	document.searchForm.A.checked = true;
	document.searchForm.B.checked = false;
}

function cambiaTipoB() {
	document.searchForm.A.checked = false;
	document.searchForm.B.checked = true;
}

function loadSearchResultsPage() {
  var searchTextCtrl = document.searchForm.searchTextCtrl;
  var searchTextCtrlValue = searchTextCtrl.value;

  if (searchTextCtrlValue == '') {
    alert("Devi specificare qualcosa da cercare prima di avviare una ricerca. ");
    document.searchForm.searchTextCtrl.focus();
  } else {
    if (document.searchForm.A.checked == true) {
		var a = document.searchForm.A.value;
		document.searchResultsPageForm.kindOfSearch.value = a;
	}
	if (document.searchForm.B.checked == true) {
		var b = document.searchForm.B.value;
		document.searchResultsPageForm.kindOfSearch.value = b;
	}
    document.searchResultsPageForm.searchText.value = searchTextCtrlValue;
    document.searchResultsPageForm.submit();
  }
}

function loadSearchResultsPageStatic() {
  var searchTextCtrl = document.searchForm.searchTextCtrl;
  var searchTextCtrlValue = searchTextCtrl.value;
  if (searchTextCtrlValue == '') {
    alert("Devi specificare qualcosa da cercare prima di avviare una ricerca. ");
    document.searchForm.searchTextCtrl.focus();
  } else {
    window.document.location.href="/olio_mobil/publicbridge.jsp?GB_METHOD=getSearchResultsPage&GB_CUSTOMIZEDFOR=OLIO_MOBIL&GB_TYPER=OLIO_MOBIL_SRESULTSPAGE&GB_LANG=IT&pageIndex=1&searchText=" + searchTextCtrlValue + "&kindOfSearch=desc";
  }
}

function loadCataloguePage(sectionCode) {
  document.cataloguePageForm.sectionCode.value = sectionCode;
  document.cataloguePageForm.submit();
}

function loadCataloguePageStatic(sectionCode) {
  window.document.location.href="/olio_mobil/publicbridge.jsp?GB_METHOD=getCataloguePage&GB_CUSTOMIZEDFOR=OLIO_MOBIL&GB_TYPER=OLIO_MOBIL_CATPAGE&GB_LANG=IT&pageIndex=1&sectionCode=" + sectionCode + "&subsection1Code=&subsection2Code=&subsection3Code=&subsection4Code=";
}

function returnSite(code) {
    window.document.location.href="/olio_mobil/publicbridge.jsp?GB_METHOD=getSearchResultsPage&GB_CUSTOMIZEDFOR=OLIO_MOBIL&GB_TYPER=OLIO_MOBIL_SRESULTSPAGE&GB_LANG=IT&pageIndex=1&searchText=" + code + "&kindOfSearch=code"
}

//----------------------------------------------------------------------------------------------------
// Funzione apertura pagina
//----------------------------------------------------------------------------------------------------

function doLogon() {
  var userTextCtrlValue = document.logonForm.userTextCtrl.value;
  var passwordCtrlValue = document.logonForm.passwordCtrl.value;

  if (userTextCtrlValue == '') {
    alert("Devi specificare un nome utente ed una password per l'accesso. ");
    document.logonForm.userTextCtrl.focus();
  } else if (passwordCtrlValue == '') {
    alert("Devi specificare un nome utente ed una password per l'accesso. ");
    document.logonForm.passwordCtrl.focus();
  } else {
        document.brexForm.USER.value = userTextCtrlValue;
        document.brexForm.PWD.value = passwordCtrlValue;
    	document.brexForm.submit();
  }
}

function loadNewsPage() {
  document.pageForm.GB_TYPER.value = "OLIO_MOBIL_SPECIALI";
  document.pageForm.submit();
}

function loadContactPage() {
  document.pageForm.GB_TYPER.value = "OLIO_MOBIL_AUTOMOTIVE";
  document.pageForm.submit();
}

function loadAziendaPage() {
  document.pageForm.GB_TYPER.value = "OLIO_MOBIL_AZIENDA";
  document.pageForm.submit();
}

function loadServiziPage() {
  document.pageForm.GB_TYPER.value = "OLIO_MOBIL_SERVIZI";
  document.pageForm.submit();
}

function loadCertificatiPage() {
  document.pageForm.GB_TYPER.value = "OLIO_MOBIL_CERTIFICATI";
  document.pageForm.submit();
}

function loadContactPageStatic() {
  window.document.location.href="/olio_mobil/publicbridge.jsp?GB_METHOD=getPage&GB_CUSTOMIZEDFOR=OLIO_MOBIL&GB_TYPER=OLIO_MOBIL_AUTOMOTIVE&GB_LANG=IT";
}

function loadServiziPageStatic() {
  window.document.location.href="/olio_mobil/publicbridge.jsp?GB_METHOD=getPage&GB_CUSTOMIZEDFOR=OLIO_MOBIL&GB_TYPER=OLIO_MOBIL_SERVIZI&GB_LANG=IT";
}

function loadAziendaPageStatic() {
  window.document.location.href="/olio_mobil/publicbridge.jsp?GB_METHOD=getPage&GB_CUSTOMIZEDFOR=OLIO_MOBIL&GB_TYPER=OLIO_MOBIL_AZIENDA&GB_LANG=IT";
}

function loadCertificatiPageStatic() {
  window.document.location.href="/olio_mobil/publicbridge.jsp?GB_METHOD=getPage&GB_CUSTOMIZEDFOR=OLIO_MOBIL&GB_TYPER=OLIO_MOBIL_CERTIFICATI&GB_LANG=IT";
}

function loadHome() {
    document.location.href="index.htm";
}

function loadDatail(cod) {
    document.detailForm.searchText.value = cod;
    document.detailForm.submit();
}

function loadPopDatail(cod) {
    oCodDetail = cod;
    openBrWindow("pop-detail.htm", "dettaglioArticolo", "width=640,height=480");
}

//----------------------------------------------------------------------------------------------------
//
// Fuinzioni del cestello
//

       function doBasketOpen() {
			if (basketArrayLength == 0) {
				alert("Non hai nessun prodotto nel cestello! ");
			} else {
			  document.productsListPageByPrefixesForm.GB_TYPER.value = "OLIO_MOBIL_BASKET";
			  document.productsListPageByPrefixesForm.prefixes.value = basketArray.join(",");
			  document.productsListPageByPrefixesForm.submit();
			}
		}

		function doBasketClear() {
			if (basketArrayLength == 0) {
				alert("Non hai nessun prodotto nel cestello! ");
			} else {
				if (confirm("Sei sicuro di voler svuotare il cestello? ")) {
				  var prefix, td1, td2;
					while (basketArrayLength > 0) {
						prefix = basketArray.pop();
						basketArrayLength--;
						td1 = MM_findObj('_TD1_' + prefix);
						if (td1) {
						  td2 = MM_findObj('_TD2_' + prefix);
						  td1.innerHTML = '<a href="javascript:addToBasket(' + "'" + prefix + "')" + '">Metti nel cestello</a>';
						  td2.innerHTML = '<img src="mobil_lubrificanti/gstuff31.gif" width="22" height="22" alt="" border="0"/>';
						}
					}
		    		basketCookie.data = basketArray.join("|");
		    		basketCookie.store();
					spanBasketLength.innerHTML = 'Il tuo cestello &egrave; <strong>vuoto</strong><br/>';
				}
			}
		}

		function doBasketBudget() {
			if (basketArrayLength == 0) {
				alert("Non hai nessun prodotto nel cestello! ");
			} else {
			  document.productsListPageByPrefixesForm.GB_TYPER.value = "OLIO_MOBIL_BUDGET";
			  document.productsListPageByPrefixesForm.prefixes.value = basketArray.join(",");
			  document.productsListPageByPrefixesForm.submit();
			}
		}

        function testCookies() {
    	 var exp = new Date();
    	 exp.setTime(exp.getTime() + 1800000);
    	 // first write a test cookie
    	 setCookie("cookies", "cookies", exp, false, false, false);
    	 if (document.cookie.indexOf('cookies') != -1) {
    	   //alert("Got Cookies!");
    	 }
    	 else {
    		alert("Abilitare i cookie per abilitare tutte le funzioni del sito");
    	 }
    	 // now delete the test cookie
    	  exp = new Date();
    	  exp.setTime(exp.getTime() - 1800000);
    	  setCookie("cookies", "cookies", exp, false, false, false);
    	 }

    	function setCookie(name, value, expires, path, domain, secure) {
    	 var curCookie = name + "=" + escape(value) +
    		((expires) ? "; expires=" + expires.toGMTString() : "") +
    		((path) ? "; path=" + path : "") +
    		((domain) ? "; domain=" + domain : "") +
    		((secure) ? "; secure" : "");
    	 document.cookie = curCookie;
    	}


        function init() {

          testCookies();

		  spanBasketLength = MM_findObj('_SPAN_BASKET_LENGTH');

		  basketCookie = new Cookie(document, basketCookieName, basketCookiePersistenceInHours);
		  if (!(basketCookie.load() && basketCookie.data)) {
		    basketArray = new Array();
		    basketArrayLength = basketArray.length;
		  } else {
		    basketArray = basketCookie.data.split("|");
		  	if (basketArray.length > maxBasketArrayLength) {
		      basketArray = basketArray.slice(0, maxBasketArrayLength - 1);
		    }
		    basketArrayLength = basketArray.length;
		  }

		  if (basketArrayLength == 1) {
		    spanBasketLength.innerHTML = 'Hai <strong>1</strong> prodotto nel cestello<br/>';
		  } else if (basketArrayLength > 1) {
		    spanBasketLength.innerHTML = 'Hai <strong>' + basketArrayLength + '</strong> prodotti nel cestello<br/>';
		  } else {
		    spanBasketLength.innerHTML = 'Il tuo cestello &egrave; <strong>vuoto</strong><br/>';
		  }
		}
