function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
	}
	
function setCookie (name, value) {
   var argv = setCookie.arguments;
   var argc = setCookie.arguments.length;
   var expires = (argc > 2) ? argv[2] : null;
   var path = (argc > 3) ? argv[3] : null;
   var domain = (argc > 4) ? argv[4] : null;
   var secure = (argc > 5) ? argv[5] : false;
   document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "; path = /" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}


function getCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;	
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

function updateInfo(num) {
    //alert(getCookie('12'));
    if(navigator.appVersion.indexOf('MSIE') > 0) {				
		var number = document.all["number"];				
	}
	else {				
		var number = document.getElementById("number");				
	}
	number.innerHTML = num;
}



function getNumberOfItem(n) {
    var cart = getCookie("cart");     
    if(cart) {
        reg = new RegExp(n + "=(\\d+)", "i");                            
        m = cart.match(reg);
        if(m) return m[1];
    }
    return 0;            
}

function updateCart(input) {        
    addCart(input.id, input.value, 1);
    setCookie('override', 1);
}

function addCart(n, num, override, title) {  
  var cart = getCookie("cart");       
  if(override == 1) var v = Number(num);
  else {      
      var v = Number(getNumberOfItem(n)) + 1;    
  }  
  v = String(v);       
  var scart = "|";
  cart = cart == null ? '' : cart;
  if (cart.length <3 &&v.length >0) {
    scart += n+"="+v+"|";
  } else {
    var ar = cart.split("|");
    var f = 0;
    for(var i=0; i<ar.length; i++) {
      var s = ar[i];
      if (s.length>2) {
        var p = s.indexOf(n+"=");
        if (p==0) {
          if (v.length>0) {
            scart += n+"="+v+"|";
          }
          f = 1;
        } else {
          scart = scart+s;
          scart = scart+"|";
        }
      }
    }
    if (f==0&&v.length>0) {
      scart += n+"="+v+"|";
    }
  }
  setCookie("cart",scart);
  if(title) alert(title + ' добавлен в корзину');  
  //var all = getCookie('number');
  //all = Number(all) + 1;
  //updateInfo(all);            
  //setCookie('number', all);
}


function rollOver (obj, newSrc) {
	var nome = obj.name;
	document.images('Img' + nome).src = newSrc;
}

function swapImg (name, actSrc, newSrc){

	var src = document.images(name).src;
	
	if (src.search(actSrc) != -1){
		 document.images(name).src = newSrc;
	}
	if (src.search(newSrc) != -1){
		 document.images(name).src = actSrc;
	}
}

function dgClock(){

}

function expTree(obj) {
	var name		= obj.name;
	var stDiv		= eval('div' + name).style.display;
	if (stDiv == 'none') {
		eval('div' + name).style.display = '';
	}
	else {
		eval('div' + name).style.display = 'none';
	}
}

function stampa(obj) {
	if (obj) {
	obj.focus();
	obj.print();
	}
	else {
		alert('Impossibile stampare il documento.');
	}
}

function salva(path) {
	var save;
	save = true;
	
	if (path == null | path == '') {
		save = false;
	}
	
	if (path != null && path.search('.asp' ) != -1 ) {
		if ( path.search('downloadmenu.asp') != -1 )
		{
			document.location.href = '/templates/downloadmenu.asp?path=' + path;
			return
		}
		else
		{
			save = false;
		}
		
	}
	
	if (!save) {
		alert('Impossibile salvare questo documento.');
	}
	else {
		//alert(path);
		document.location.href = 'download.asp?path=' + path;
	} 
}


function openWin (url, name, width, height) {
	var parms;
	//parms = 'fullscreen,height=' + height + ',width=' + width + ',dependent=yes,titlebar=no'
	parms = 'height=' + height + ',width=' + width + ',dependent=yes,titlebar=no,status=no,scrollbars=no'
	popWin = window.open(url, name, parms);
	//popWin.resizeTo(width,height);
	//popWin.moveTo(200,160);
}

function popWin (url, name, width, height, titlebar, status, scrollbars) {
	var parms;
	// titlebar=yes/no, status=yes/no, scrollbars=yes/no
	
	parms = 'height=' + height + ',width=' + width + ',dependent=yes,titlebar=' + titlebar + ',status=' + status + ',scrollbars=' + scrollbars
	popWin = window.open(url, name, parms);
	//popWin.resizeTo(width,height);
	//popWin.moveTo(200,160);
}


// apre un pop up fisso
function openWindow(url) {
    	popUpWin = window.open(url,'giftwrap','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=310,height=270,screenX=300,screenY=100,left=300,top=100');
}

// apre un pop up fisso per l'immagine del prodotto grande
// e' una ridondanza ma purtroppo quella pagina da un errore insolito 
// se si utilizza la funzione popWin, che e'certo meno rigida
function openWindowProdotto(url) {
    	popUpWin = window.open(url,'giftwrap','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=370,height=380,screenX=300,screenY=100,left=300,top=100');
}






