 /**
  * 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();
 }

/* * * * * * * * * * * * * * */
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;
}

/**
 * FCKeditor inizialized
 */
// var _FCK_EDITOR_ENABLED_ = true;
// // var _FCK_EDITOR_ENABLED_ = false;
//
// function ini_fck(fck_field_name, fck_width, fck_height) {
//
// 	if (_FCK_EDITOR_ENABLED_)	{
//
// 		var oFCKeditor = new FCKeditor(fck_field_name);
// 		oFCKeditor.Height = (fck_height) ? fck_height : "300";
// 		oFCKeditor.Width = (fck_width) ? fck_width : "100%";
// 		oFCKeditor.BasePath = "/FCK/"
// 		oFCKeditor.ReplaceTextarea() ;
//   }
// }


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);
}
// http://wwww.sda.it/ResourceServlet.html?execute2=ActionTracking.doGetTrackingHome
// http://wwww.sda.it/ResourceServlet.html?execute2=ActionTracking.doGetTrackingHome&id_ldv=455555