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;
		}
	},
	
	number_format: function(number, decimals, dec_point, thousands_sep) {
	    // *     example 1: number_format(1234.5678, 2, '.', '');
	    // *     returns 1: 1234.57     
	 
	    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
	    var d = dec_point == undefined ? "." : dec_point;
	    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
	    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	    
	    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
	},
	
	swap_up: function(element)
	{
		if (element.get('tag')!='img') element = element.getElements('img')[0];
		string = new String(element.src);
		element.src = string.replace('_off.','_on.');
	},
	
	swap_down: function(element)
	{
		if (element.get('tag')!='img') element = element.getElements('img')[0];
		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 = /^([0-9A-z._\-])+@([0-9A-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()
	},
	
	setPlaceholder: function(el)
	{
		el.addEvent('focus', function(ev){
			if (!this.hasClass('placeholder')) return;
			this.removeClass('placeholder');
			this.value = '';
		});
	},
	
	writeEmail: function(ifLink, dom, user){
		var m = user+"@"+dom;
		var toWrite="";
		if(ifLink){
			toWrite = "<a href='mailto:"+m+"'>"+m+"</a>";
		}else{
			toWrite = m;
		}
		document.write(toWrite);
	}
});

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];
	}
});

window.addEvent('domready', function(){
	$$('.doRollover').each(function(el){
		el.addEvent('mouseenter', function(ev){CosMooF.swap_up(this);});
		el.addEvent('mouseleave', function(ev){CosMooF.swap_down(this);});
	});
	$$('.placeholder').each(function(el){
		CosMooF.setPlaceholder(el);
	});
});
