 /**
  * Open a window popup
  * @Author: Francesco Rabottini <dev@webzone.it>
  *
  * @param	(String) url	the url to be opened
  * @param	(int) width		the width of the window
  * @param	(int) height	the height of the window
  * @param	(String) mode	"[stlmrS]" default: ""
  *				possible values:
  *				o	s: status
  *				o	t: toolbar
  *				o	l: location
  *				o	m: menubar
  *				o	r: resizable
  *				o	S: scrollbars=yes
  *
  * @return	(Void)	focus the last opened window popup
  */
 function openpopup(url, w, h, mode)
 {
 	_top  = (screen.height - h) / 2
 	_left = (screen.width  - w) / 2;
 	option = "top="+_top+",left="+_left+",width="+w+",height="+h;
 	if (! mode) { mode = ""; }
 	opt = new Array("status","toolbar","menubar","location","resizable", "Scrollbars");
 	for (i=0; i< opt.length;i++) {
 		if (mode.indexOf(opt[i].substring(0,1)) < 0) {
 			option += ","+opt[i]+"=no";
 		} else {
 			option += ","+opt[i]+"=yes";
 		}
 	}
 	win = window.open(url, null, option.toLowerCase());
 	win.focus();
 }

var FlashNews = Class.create();

FlashNews.prototype = {

	clock: 0,
	appearDuration: .6,
	fadeDuration: 0,
	timeOUT: 8,
	panels:Array(),
	perCicle: 1,
	cicles: 0,
	currentPos: 0,
	initialize: function() {
		if (! $("flashnews")) return false;
		Element.hide("flashnews");
		this.panels = $$("div.flashnews");
		this.panels.invoke('hide');
		Element.show("flashnews");
		$("flashnews").style.visibility = "visible";
		this.cicles = Math.ceil(this.panels.length / this.perCicle) - 1;
		this.start();
	},

	start: function() {

		if (this.currentPos < 0) { this.currentPos = this.cicles; }
		if (this.currentPos > this.cicles) { this.currentPos = 0; }

		start = this.perCicle * this.currentPos;
		end = start + this.perCicle;

// 		$("flashlabel").update("pos: "+this.currentPos+"; start: "+ start+"; end: "+end)

		if (end > this.panels.length) {
			end = this.panels.length
		}

		counter = 0;
		for (i=start; i < end; i++) {

			nws = this.panels[i];
			params = {
				delay: this.appearDuration * counter,
				duration: this.appearDuration,
				from: 0,
				to: 1
			};
			new Effect.BlindDown(nws, params);
			counter++;
		}
		this.clock = window.setTimeout("myFlashNews.next()", this.timeOUT * 1000 + this.fadeDuration * this.perCicle * 1000);
	},
	next: function() {

		this.panels.invoke('hide');
		window.clearTimeout(this.clock);
		this.currentPos++;
		this.start();
	},
	prev: function() {

		this.panels.invoke('hide');
		window.clearTimeout(this.clock);
		this.currentPos--;
		this.start();
	}
}
function initFlashNews() { myFlashNews = new FlashNews(); }
//Event.observe(window, 'load', initFlashNews, false);



/* * * * * * * * * * * * * * */
function checkAll(btn)	{
	itemId = btn.form['itemId[]'];
	if (itemId.length)	{
		for (i=0; i<itemId.length; i++)	{
			itemId[i].checked = btn.checked;
		}
	} else {
		itemId.checked = btn.checked;
	}
}

function deleteSelected(btn, notConfirmAction)	{
	itemId = btn.form['itemId[]'];
	if (itemId.length == undefined && itemId.checked)	{
		itemChecked = 1;
	} else {
		itemChecked = 0;
		for (i=0; i<itemId.length; i++)	{
			if (itemId[i].checked)	{
				itemChecked++;
			}
		}
	}
	if (itemChecked == 0)	{
		alert('Devi selezionare almeno un elemento da eliminare');
		return false;
	}
	if (notConfirmAction)	{
		btn.form.submit();
		return false;
	}
	if (confirm("\n\nHai scelto di Eliminare [ "+itemChecked+" ] elemeto/i\n\nVuoi davvero completare l'operazione?\n\n"))	{
		btn.form.submit();
	}

	return false;
}


function syncToRole(btn)	{
	itemId = btn.form['itemId[]'];
	if (itemId.length == undefined && itemId.checked)	{
		itemChecked = 1;
	} else {
		itemChecked = 0;
		for (i=0; i<itemId.length; i++)	{
			if (itemId[i].checked)	{
				itemChecked++;
			}
		}
	}
	if (itemChecked == 0)	{
		alert('Devi selezionare almeno un utente da sincronizzare');
		return;
	}
	if (btn.form.role.value == '')	{
		alert('Devi selezionare un ruolo');
		return;
	}
	if (confirm('Vuoi davvero sincronizzare i ['+itemChecked+'] utenti selezionati ?'))	{
		btn.form.action = btn.form.action.replace('Delete','SyncToRole');
		btn.form.submit();
	}
}

function help(id, show)	{
	dhelp = document.getElementById(id);
	if (dhelp)	{
		dhelp.style.display = show ? "block":"none";
	}
	return false;
}

function updateBilling(id) {
	param = {billing: id, value: $F(id)};
	var url = "/shop/orders/updateajax";
	updateOrder(url, param);
}

function updateTracking(id) {
	param = {tracking: id, value: $F(id)};
	var url = "/shop/orders/updateajax/";
	updateOrder(url, param);
}

function updateOrder(url, param) {

	var notice = $('notice');
	notice.update("")
		.setStyle({background: "#ddd url(images/loading.gif) center center no-repeat", padding: "20px 0px"});

	new Ajax.Request(url, {
		method: 'get',
		parameters: param || {},
		onSuccess: function(transport) {
			var notice = $('notice')
			notice.update(transport.responseText)
				.setStyle({ background: '#ddd' })
				.show()
		}
	});
}

function checkSDA(id) {

	tracking = $F(id);
	if (tracking.length != 7 && tracking.length != 13) {
		alert("Codice errato !");
		return;
	}
	url = "http://wwww.sda.it/ResourceServlet.html?execute2=ActionTracking.doGetTrackingHome&id_ldv=";
	openpopup(url + tracking, 500, 600);
}

function newsletterAdd(btn) {

	btn.disabled = true;
	new Ajax.Request("/users/newsletter/add", {
		method: 'post',
		parameters: { "email" : $F('newsletter_npt')} || {},
		onComplete: function(transport) {
			alert(transport.responseText);
			btn.disabled = false;
		}
	});
}

