// #########################################  PUBLIC SIDE JS FUNCTIONS  #################################################

	// ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
	window.name="MediacommerceWindow";

	var screenWidth;
	var screenHeight;
	var windowWidth;
	var windowHeight;
	var windowLeft;
	var windowTop;
	screenWidth = screen.width;
	screenHeight = screen.height;
	
	//costanti per funzione onlychars
	var ONLYNUM_0 = "0123456789"; //solo numeri
	var ONLYNUM_2 = ONLYNUM_0 + "%,."; //numeri + "percentuale, virgola, punto"
	var ONLYNUM_3 = ONLYNUM_0 + ","; //numeri + "virgola"
	var ONLYTXT_0 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //solo lettere
	var ONLYTXT_1 = ONLYTXT_0 + ONLYNUM_0; //lettere + numeri
	var ONLYTXT_2 = ONLYTXT_1 + "@.-_"; //lettere + numeri + "chiocciola, punto, segno meno, underscore" (utilizzare per campi email)
	var ONLYTXT_3 = ONLYTXT_1 + "!"+String.fromCharCode(34)+"£$%&/()='?ì^€èé[+*]òç@à°#ù,;.:-_"; //lettere + numeri + "determinati caratteri"
	var ONLYTXT_4 = ONLYTXT_1 + " "; //lettere + numeri + "spazio"
	var ONLYTXT_5 = ONLYTXT_1 + "/"; //lettere + numeri + "barra"
	var ONLYTXT_6 = ONLYTXT_0 + " "; //lettere + "spazio"
	var ONLYTXT_7 = ONLYTXT_4 + ","; //lettere + numeri + spazio + "virgola" 
	var ONLYTXT_8 = ONLYTXT_3 + " "; //lettere + numeri + determinati caratteri + "spazio"
	
	
	
	// ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
	function esempio(imageName, larghezza, altezza) {
		windowWidth = larghezza;
		windowHeight = altezza;
		windowLeft = screenWidth / 2 - windowWidth / 2;
		windowTop = screenHeight / 2 - windowHeight / 2;
		searchWin = window.open('zoom.asp?ingrandimento='+ imageName,'','scrollbars=no,resizable=no,width=' + windowWidth + ',height=' + windowHeight + ',left=' + windowLeft + ',top=' + windowTop + ',status=no,location=no,toolbar=no');
	}
	// ——————————————————————————————————————————————————————————————————————————————————————————————————————————————
	function PictureZoom(imageName) {
		windowWidth = 1;
		windowHeight = 1;
		zoomWin = window.open('picture_zoom.asp?img='+ imageName,'zoomWin','scrollbars=no,resizable=no,width=' + windowWidth + ',height=' + windowHeight + ',status=no,location=no,toolbar=no');
	}
	// ——————————————————————————————————————————————————————————————————————————————————————————————————————————————


//NUOVE FUNZIONI

//Verifica il tipo di browser
//routine tratta da http://www.quirksmode.org/js/detect.html
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


/* ******************** JavaScript Cookies ******************** */
function makeCookie(name, value, p) {
	var s, k;
	s = escape(name) + '=' + escape(value);
	if (p) for (k in p) {
alert('ok');
	/* convert a numeric expires value to a relative date */

	if (k == 'expires')
	p[k] = isNaN(p[k]) ? p[k] : reldate(p[k]);

	/* The secure property is the only special case
	here, and it causes two problems. Rather than
	being '; protocol=secure' like all other
	properties, the secure property is set by
	appending '; secure', so we have to use a
	ternary statement to format the string.

	The second problem is that secure doesn't have
	any value associated with it, so whatever value
	people use doesn't matter. However, we don't
	want to surprise people who set { secure: false }.
	For this reason, we actually do have to check
	the value of the secure property so that someone
	won't end up with a secure cookie when
	they didn't want one. */

	if (p[k])
	s += '; ' + (k != 'secure' ? k + '=' + p[k] : k);
	}
	document.cookie = s;
	return readCookie(name) == value;
}

function readCookie(name) {
	var s = document.cookie, i;
	if (s)
	for (i=0, s=s.split('; '); i<s.length; i++) {
	s[i] = s[i].split('=', 2);
	if (unescape(s[i][0]) == name)
	return unescape(s[i][1]);
	}
	return null;
}

function rmCookie(name) {
	return !makeCookie(name, '', { expires: -1 });
}

function reldate(days) {
	var d;
	d = new Date();

/* We need to add a relative amount of time to
the current date. The basic unit of JavaScript
time is milliseconds, so we need to convert the
days value to ms. Thus we have
ms/day
= 1000 ms/sec *  60 sec/min * 60 min/hr * 24 hrs/day
= 86,400,000. */

	d.setTime(d.getTime() + days*86400000);
	return d.toGMTString();
}
/* ************************************************************ */




function setCookie(sNome, sValore, iGiorni) {
var dtOggi = new Date()
var dtExpires = new Date()
	dtExpires.setTime
	(dtOggi.getTime() + 24 * iGiorni * 3600000)
	document.cookie = sNome + "=" + escape(sValore) + "; expires=" + dtExpires.toGMTString();
}

// restituisce il valore del cookie sNome
function getCookie(sNome) {
// genera un array di coppie "Nome = Valore"
// NOTA: i cookies sono separati da ';'
var asCookies = document.cookie.split("; ");
	// ciclo su tutti i cookies
	for (var iCnt = 0; iCnt < asCookies.length; iCnt++) {
		// leggo singolo cookie "Nome = Valore"
		var asCookie = asCookies[iCnt].split("=");
		if (sNome == asCookie[0]) { 
			return (unescape(asCookie[1]));
		}
	}

	// SE non esiste il cookie richiesto
	return("");
}

function delCookie(sNome) {
	setCookie(sNome, "");
}


	// ——————————————————————————————————————————————————————————————————————————————————————————————————————————————



function go_search(idForm) {
var objForm = document.getElementById(idForm);
	if (objForm.search.value.length<2) {
		alert('Devi inserire almeno 2 caratteri nella ricerca');
		objForm.search.focus();
		return;
	}else{
		objForm.searchtext.value=objForm.search.value;
		objForm.submit();
	}
}

function onlynum(e)
//Funzione che accetta soltanto numeri(48-57)
//oltre ai tasti controllo DEL(8), TAB(9), LEFT ARROW(37), RIGHT ARROW(39)
   {//alert(e.keyCode);
   if (e.which)
      {//alert('witch');
      if((e.which!=8 && e.which!=9) && (e.which<48 || e.which>57))
      return false;
      }
   else if(e.keyCode)
      {//alert('code');
      if((e.keyCode!=8 && e.keyCode!=9 && e.which!=37 && e.which!=39) && (e.keyCode<48 || e.keyCode>57))
      return false;
      }
   return true;
   }

function onlynum2(e) {
//Funzione che accetta soltanto numeri(48-57), la percentuale(37), la virgola(44), il punto(46)
//oltre ai tasti controllo TAB(9)
   if (e.which) {//alert('e.which='+e.which);
		if((e.which!=8 && e.which!=9) && (e.which<48 || e.which>57))
		return false;
   }
   else if(e.keyCode){//alert('e.keyCode='+e.keyCode);
      if((e.keyCode!=8 && e.keyCode!=9 && e.keyCode!=37 && e.keyCode!=39 && e.keyCode!=44) && (e.keyCode<48 || e.keyCode>57))
      return false;
   }
   return true;
}

function onlynum3(e) {
//Funzione che accetta soltanto numeri(48-57), la virgola(44)
//oltre ai tasti controllo DEL(8), TAB(9)
   if (e.which) {//alert('e.which='+e.which);
		if((e.which!=8 && e.which!=9 && e.which!=44) && (e.which<48 || e.which>57))
		return false;
   }
   else if(e.keyCode){//alert('e.keyCode='+e.keyCode);
      if((e.keyCode!=8 && e.keyCode!=9 && e.keyCode!=44) && (e.keyCode<48 || e.keyCode>57))
      return false;
   }
   return true;
}

function onlychars(e,chars) {
//Funzione che accetta soltanto determinati caratteri
   var key;
   var keychar;
   var browser;

   browser=BrowserDetect.browser;

   if (e.which) {//alert('e.which='+e.which);
   	   key=e.which;
	   switch(browser){
		   case "Firefox":
		     	if ((key==8) || (key==13)) {
    				return true;
					break;
				}
		   case "Chrome":
		     	if ((key==13)) {
    				return true;
					break;
				}
		   case "Safari":
		     	if ((key==13)) {
    				return true;
					break;
				}
	   }
   }
   else if(e.keyCode){//alert('e.keyCode='+e.keyCode);
   	   key=e.keyCode;
	   switch(browser){
		   case "Explorer":
		     	if ((key==13) || (key==27)) {
    				return true;
				}
				break;
		   case "Firefox":
		     	if ((key==9) || (key==46) || (key==46) || (key==27) || (key==35) || (key==36) || (key==37) || (key==39)) {
    				return true;
				}
				break;
	   }
   }
   keychar = String.fromCharCode(key);

// control keys
  if ((key==null) || (key==0))
    return true;
  // numbers
  else if (((chars).indexOf(keychar) > -1))
    return true;
   
   return false;
}

function ucfirst(obj) {
	obj.value=Left(obj.value.toUpperCase(),1)+Right(obj.value,Len(obj.value)-1);
	return;
}

function Mid(String, Start, Length)
{
if (String == null)
return (false);

if (Start > String.length)
return '';

if (Length == null || Length.length == 0)
return (false);

return String.substr((Start - 1), Length);
}

//******************
function InStr(String1, String2)
{
var a = 0;

if (String1 == null || String2 == null)
return (false);

String1 = String1.toLowerCase();
String2 = String2.toLowerCase();

a = String1.indexOf(String2);
if (a == -1)
return 0;
else
return a + 1;
}

//******************
function Len(string)
{
if (string == null)
return (false);

return String(string).length;
}

//******************
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

//******************
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

//******************
function Trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
//******************

function Ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
//******************

function Rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
//******************

function Replace(StringToReplace, StringToChange, StringChangedIn) {
	return StringToReplace.replace(StringToChange, StringChangedIn);
}

//*****************************
function LeggiCampoForm(IdCampo)
{
	if (document.all) //IE4,5,6,7
		if (eval("document.all." + IdCampo + ".type") == "text")
			return (eval("document.all." + IdCampo + ".value"));
		else return (eval("document.all." + IdCampo));
	
	if (document.layers) //NETSCAPE 4
		if (document.layers[IdCampo].type == "text")
			return document.layers[IdCampo].value;
		else return document.layers[IdCampo];

	if (document.getElementById &&!document.all) //MOZILLA
		if (eval("document.getElementById('"+IdCampo+"').type") == "text")
			return eval("document.getElementById('"+IdCampo+"').value");
		else {
			//alert(eval("document.getElementById('"+IdCampo+"')"));
			return eval("document.getElementById('"+IdCampo+"')");
		}
}

//*****************************
function ScriviCampoForm(IdCampo,Valore){

	if (document.all) { //IE4,5,6,7
		//alert("IE");
		document.all[IdCampo].value = Valore;
//		eval( "document.all." + IdCampo + ".value='" + Valore + "'");
	}
	if (document.layers) { //NETSCAPE 4 
		//alert("NETSCAPE");
		document.layers[IdCampo].value = Valore;
	}
	if (document.getElementById &&!document.all) {	//MOZILLA
		//alert("MOZILLA");
		eval("document.getElementById('"+IdCampo+"').value = Valore");
	}
}

//eof******************
function formatCurrency(number) {
var num, dec, charpos, chr
	
	dec="00";
	charpos= -1;
	num="";
	
	//se number è nullo lo mette a zero
	if(number.length==0) number="0";
	
	//imposta num fino alla prima virgola
	for(var i=0;i<number.length;i++) {
		if(number.charAt(i)==",") {
			charpos=i;
			break;
		}
		num=num+number.charAt(i);
	}

	num=parseInt(num);
	if(isNaN(num)) num=0;

	//estrae i decimali
	if (charpos!= -1) {
		dec="";
		for(var i=charpos+1;i<number.length;i++) {
			chr=number.charAt(i);
			if(chr!=",") dec=dec+chr;
		}
	}

	//formatta i decimali a 2 cifre
	if(dec.length<2) {
		for(i=dec.length;i<2;i++) {
			dec=dec+"0";
		}
	}

	//formatta i decimali a 2 cifre
	dec=dec.substring(0,2)
	
	return num+","+dec;
}

function FormatCurrency2(number) {
	//alert(isNaN(number));
var num
	if (isNaN(number)) number=0;
	num=number.toFixed(2);
	num=num.replace(".",",");
	return num;
}
function roundTo(value, decimalpositions){
    var i = value * Math.pow(10,decimalpositions);
    i = Math.round(i);
    return i / Math.pow(10,decimalpositions);
}

function wichBrowser() {
	if(navigator.appVersion.indexOf("MSIE")!=-1)
		return "IE";
	else if(navigator.userAgent.indexOf("Firefox")!=-1)
		return "FF";
	else
		return "Other";
}

function button_disable(formname,value) {
var o;
	for (i=0; i<document.forms[formname].length; i++) {
		o = document.forms[formname].elements[i];
		if(o.type='button') o.disabled = value;
	}
}

function buttons_disable(value) {
var o;
	for (j=0; j<document.forms.length; j++) {
		o=document.forms[j];
		button_disable(o.name,value);
	}
}

//Executing JavaScript on page load
//http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function FormatZero(num, format) {
var strnum="";
	switch(format) {
		case "FORMAT_STORE":
			str="00000";
			break;
		case "FORMAT_ACCOUNT":
			str="000000";
			break;
		case "FORMAT_ITEM":
			str="000000";
			break;
		default:
			strnum=num;
			return strnum;
	}
	for(var i=1;i<=Len(str)-Len(num);i++) {
		strnum=strnum+"0";
	}
	strnum=strnum+num;
	return strnum;
}

function URLEncode(CODE){

var plaintext = CODE;

	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					
	var HEX = "0123456789ABCDEF";
	
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" + ch + "' cannot be encoded using standard URL encoding.\n" +
				        "(URL encoding only supports 8-bit characters.)\n" +
						"A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	// VALORE CODIFICATO
	return encoded;
};

