var CosMooF = new Class({

	date2str: function(val)
	{
		var date = new Date();
		date.parse(val);
		var res = date.format('%d/%m/%Y');
		if(res=="invalid date")
			res = "----------";
		return res;
	},
	
	str2date: function(date)
	{
		//TODO
		return date;
	
	},
	
	bool2str: function(val)
	{
		if(val==1)
			return "SI";
		else
			return "NO";
	},
	
	openClosediv: function(id1, id2)
	{
		if (id2!=null) 
		{
			document.getElementById(id2).style.display = 'none';
		}
		if (id1!=null) 
		{
			document.getElementById(id1).style.display = 'block';
		}
	},
	
	formattaPrezzo: function(val)
	{
		val = String(val).replace(",", ".");
		val = parseFloat(val);
		if(isNaN(val))
			return "&euro; 0,00";
		else
		{
			val = this.troncaDecimali(val, 2);
			val = val.replace(".", ",");
			return "&euro; "+val;
		}
		
	},
	
	troncaDecimali: function(val, decimali) 
	{
		var round = 1;
		for(i=0;i<decimali;i++)
			round = round*10;
		var temp = String((Math.round(val*round))/round);
		var arr = temp.split(".");
		if(arr.length==1)
			return temp+".00";
		else if(arr[1].length==decimali)
			return temp;
		else if(arr[1].length<decimali)
		{
			var dec = arr[1];
			while(dec<decimali)
				dec = dec+"0";
			return arr[0]+"."+dec;
		}
	},
	
	swap_up: function(element)
	{
		string = new String(element.src);
		element.src = string.replace('_off.','_on.');
	},
	
	swap_down: function(element)
	{
		string = new String(element.src);
		element.src = string.replace('_on.','_off.');
	},
	
	/*
	 * forza il submit se premo invio
	 * Es:
	 * <FORM ACTION="../cgi-bin/mycgi.pl">
	 *   name:     <INPUT NAME=realname SIZE=15><BR>
	 *   password: <INPUT NAME=password TYPE=PASSWORD SIZE=10
	 *          onKeyPress="return submitenter(this,event)"><BR>
	 *   <INPUT TYPE=SUBMIT VALUE="Log In">
	 *   </FORM>
	 */
	
	submitenter: function(myfield,e)
	{
	    var keycode;
	    if (window.event) keycode = window.event.keyCode;
	    else if (e) keycode = e.which;
	    else return true;
	    
	    if (keycode == 13)
	       {
	       myfield.form.submit();
	       return false;
	       }
	    else
	       return true;
	},
	
	
	/*check indirizzo Mail
	 *
	 */
	
	checkMail: function(string)
	{
	    var espressione = /^([A-z._\-])+@([A-z._\-])+\.([A-z])/;
	    return espressione.test(string);
	},
	
	
	/*
	 * Pulisco una stringa per passarla in get via js
	 */
	clearGetString: function(string)
	{
		string = string.replace(/\r\n/g,"\n");
	    var utftext = "";
	
	    for (var n = 0; n < string.length; n++) {
	
	        var c = string.charCodeAt(n);
	
			if (c==43)
			{
				utftext += '&#43;';
			}
			else if(c==38)
			{
				utftext += '&#38;';
			}
	        else if (c < 128) {
	            utftext += String.fromCharCode(c);
	        }
	        else if((c > 127) && (c < 2048)) {
	            utftext += '&#'+c+';';
				//utftext += String.fromCharCode((c >> 6) | 192);
	            //utftext += String.fromCharCode((c & 63) | 128);
	        }
	        else {
				utftext += '&#'+c+';';
	            //utftext += String.fromCharCode((c >> 12) | 224);
	            //utftext += String.fromCharCode(((c >> 6) & 63) | 128);
	            //utftext += String.fromCharCode((c & 63) | 128);
	        }
	
	    }
	    return escape(utftext);
	},
	
	
	/*
	 * Esegue il submit di una form in una nuova finestra
	 * 
	 **/
	doSubmit: function(nomeForm,nomepagina) 
	{
	    var submitForm = document.getElementById(nomeForm);
	    displayWindow = window.open('', nomepagina, "width=490,height=490,align=center,scrollbars=yes,resizable=yes");
	    submitForm.submit();
	},
	
	
	/***apre un popup***/
	popup: function(url, width, height)
	{
		if (document.all)
		{
			var x = window.screenLeft;
			var y = window.screenTop;
			var w = window.document.body.offsetWidth;
			var h = window.document.body.offsetHeight;
		}
		else
		{
			var x = window.screenX;
			var y = window.screenY;
			var w = window.outerWidth;
			var h = window.outerHeight;
		}
		
		var cntx = x + Math.round((w - width) / 2);
		var cnty = y + Math.round((h - height) / 2);
		
		winContent = window.open (url, null, 'left=' + cntx + ',top=' + cnty + ',width=' + width + ',height=' + height  + ',resizable=yes')
		winContent.focus()
	}
});

var CosMooF = new CosMooF();

//Parser per query string
String.extend({
	toQueryObject: function(obj){
		var o = {};
		$A(obj.replace(/(^.*\?)|(#.*$)/g,'').split('&')).each(function(p){
			p = p.split("=");
			o[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
		});
		
		return o;
	},
	
	toSimpleUrl: function(obj){
		var arr = obj.split('?');
		return arr[0];
	}
});
