
/* * * * * * * * * * * * * * */
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;
	}
	if (notConfirmAction)	{
		btn.form.submit();
		return;
	}
	if (confirm("\n\nHai scelto di Eliminare [ "+itemChecked+" ] elemeto/i\n\nVuoi davvero completare l'operazione?\n\n"))	{
		btn.form.submit();
	}
}

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

var cacheRolloverImages = new Array();
var idxCacheRolloverImages = 0;
function initrollover() {
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName('a');

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		var relAttribute = String(anchor.getAttribute('rel'));

		if (anchor.getAttribute('href') &&
			(relAttribute.toLowerCase().match('hover')) &&
			anchor.getElementsByTagName('img')) {

			cacheRolloverImages[idxCacheRolloverImages] = new Image();
			cacheRolloverImages[idxCacheRolloverImages].src = "hover_"+anchor.getElementsByTagName('img').item(0).src;

			anchor.onmouseover = function() { changeImage(this); return false }
			anchor.onmouseout = function() { changeImage(this); return false }
		}
	}
}

function changeImage(anchor) {

	if (anchor.getElementsByTagName('img')) {
		image = anchor.getElementsByTagName('img').item(0);
		if (image.src.match(/hover_/)) {
			image.src = image.src.replace(/hover_/, "");
		} else {
			part = image.src.split("/");
			uri = part[0];
			for (i=1;i<part.length - 1; i++) {
				uri += "/" + part[i];
			}
			image.src = uri + "/hover_" + part[part.length-1];
		}
	}
	return false;
}





function updateFinalPrice() {

	var form = $('productbox')

	qt_total = 0;

	form.getInputs("text").each(function(input) {

		input.onkeyup = updateFinalPrice;
		if (input.name != "product" && input.name != "print" && input.id != "charcount") {
			quantiy = parseInt(input.value);
			if (isNaN(quantiy) == false) {
				qt_total += quantiy;
			} else {
				input.value = "";
			}
		}
	});
	item_price = 0
	break_cicle = false
	min_pices = false
	frm_can_submit = false
	Object.values(pr_prices.prices).each( function(pice) {
		if (isNaN(pice.from) == false && pice.from != "") {
			min_pices = min_pices ? min_pices : pice.from;
			if (pice.from > qt_total) {
				break_cicle = true
			} else if (break_cicle == false) {
				//item_price = typeof form.print != "undefined" && form.print.value.length  > 0 ? pice.print : pice.price;
				item_price = pice.price;
			}
		}
	});

	if (parseFloat(item_price) > 0) {
		frm_can_submit = true;
	} else {
		$('total_detail').update("Quantità minima non raggiunta.");
		return;
	}
	total =  parseFloat(pr_prices.price) + parseFloat(item_price) * qt_total;

	$('total_detail').update("€ "+total+" + iva");
}

function validateProductBox(frm) {

	if (frm_can_submit == false) {
		alert("Non hai ancora raggiunto il minimo ordinabile di "+min_pices+" pezzi.");
		return false;
	}

	if (typeof frm.print != "undefined" && frm.print.value.length  < 0) {

		return confirm("Non hai inserito il testo da stampare.\nVuoi comunque continuare?");
	}
	return true;
}

function adjustIFrameSize (iframeWindow) {
  if (iframeWindow.document.height) {
    var iframeElement = document.getElementById(iframeWindow.name);

    if (iframeElement.style.height) {
    	iframeElement.style.height = iframeWindow.document.height + 'px';
    	iframeElement.style.width = iframeWindow.document.width + 'px';
    }
  } else if (document.all) {
    var iframeElement = document.all[iframeWindow.name];
    if (iframeWindow.document.compatMode &&
        iframeWindow.document.compatMode != 'BackCompat')
    {
      iframeElement.style.height = iframeWindow.document.documentElement.scrollHeight + 5 + 'px';
      iframeElement.style.width = iframeWindow.document.documentElement.scrollWidth + 5 + 'px';
    } else {
      iframeElement.style.height = iframeWindow.document.body.scrollHeight + 5 + 'px';
      iframeElement.style.width = iframeWindow.document.body.scrollWidth + 5 + 'px';
    }
  }
}
