
function FixRibbonAndWorkspaceDimensions()
{
	var windowHeight = GetViewportHeight();
	var headerHeight = jQuery('#Header').height();
	if ( jQuery('#Header2').length > 0 )
	{
		var header2Height = jQuery('#Header2').height();
	}
	else
	{
		var header2Height = 0;
	}
	var ribbonHeight = jQuery('#CrifRibbon').height();
	var containerHeight = jQuery('.OD_container').height();
	var newHeight = windowHeight - headerHeight - header2Height - ribbonHeight - 24; //	il numero è la somma di tutti i margin top e bottom presenti nel sito
	if ( jQuery('#s4-statusbarcontainer').length > 0 )
	{
		if ( jQuery('#pageStatusBar').html().length > 0 )
		{
			newHeight -= jQuery('#s4-statusbarcontainer').height();
		}
	}
	jQuery('#CrifRibbon').css('width','951px');
	jQuery('.OD_container').css('height',newHeight+'px');
	ResizeRibbonAndWorkspace();
}

function addStatus(f, g, e) {
	ULSA13:;
	var a = document.getElementById("pageStatusBar");
	if (a != null) {
		a.setAttribute("aria-live", "polite");
		a.setAttribute("aria-relevant", "all");
		var b = _createStatusMarkup(f, g, true);
		if (!e) a.appendChild(b);
		else {
			var c = a.getElementsByTagName("SPAN"),
				d = c.length > 0 ? c[0] : null;
			if (d != null) a.insertBefore(b, d);
			else a.appendChild(b)
		}
		if (a.childNodes.length == 1) {
			StatusIdWithTopPriority = b.id;
			StatusColorWithTopPriority = 1
		}
		a.style.display = "block";
	//	OD - update height
		FixRibbonAndWorkspaceDimensions();
		return b.id
	}
}


// Libreria JS custom
jQuery.noConflict();

jQuery(function() {
	var printButtons = jQuery('.printer_friendly2>a');
	if (printButtons.length > 0) {
		printButtons.each(function() {
			jQuery(this).click(function() {
				window.print();
				return false;
			});
		});
	}
});

jQuery (function() {
	var div_src = jQuery('.srch-sitesearchmaintop');
	if ( div_src.length > 0 ) {
		var imgs_src = new Array('siteicon_16x16.png','STS_List_GenericList16.gif','STS_ListItem16.gif','ITGEN.PNG','sts_list16.gif','html16.png');
		var imgTags = div_src.find('img');
		if ( imgTags.length > 0 ) {
			imgTags.each(function() {
				if ( jQuery.inArray(jQuery(this).attr('src').replace('/_layouts/images/',''),imgs_src) != -1 ) {
					jQuery(this).attr('src','/PublishingImages/html16.gif');
				}
			});
		}

		var imgSX = jQuery('#SRP_PrevImg');
		var imgDX = jQuery('#SRP_NextImg');
		if ( imgSX.length > 0 ) {
			imgSX.html('&lt;');
		}
		if ( imgDX .length > 0 ) {
			imgDX.html('&gt;');
		}

		setTimeout(
			function(){
			//	div_src.css('visibility','inherit');
				div_src.css('display','block');
				jQuery('#SRP').css('display','block');
			}
			,100);
	}
	var form = jQuery('#aspnetForm');
	var formOnSubmit = '' + form.attr('onsubmit');
	if ( form.length > 0 && formOnSubmit.indexOf('WebForm_OnSubmit') >= 0 ) {
		G_init_patch_form = setInterval(
			function(){
				if (jQuery('input[type=button]').length > 0) {
					clearInterval(G_init_patch_form);
					patch_form();					
				}
			}
			,250);
	}
});

jQuery (function() {
	var menuMIC = jQuery('.menuMettInConto li');
	if ( menuMIC.length > 0 )
	{
		menuMIC.each(function(indx) {
			if ( indx > 0 )
			{
				var thisItem = jQuery(this);
				thisItem.css({
								'background': 'url("/PublishingImages/Consumatori/menuMICpipe.gif") no-repeat center left transparent',
								'padding-left': '12px'
							});
			}
		});
	}
});

jQuery (function () {
	var webpartTitleBox = jQuery('.webpartTitleBox');
	if ( webpartTitleBox.length > 0 )
	{
		webpartTitleBox.each(function() {
			var thisItem = jQuery(this);
			var text = jQuery.trim(thisItem.text());
			if ( text == '' || text == '&nbsp;' || text.length == 0 )
			{
				thisItem.remove();
			}
		});
	}
	var arrBoxes = ['firstBoxGenericPage','secondBoxGenericPage','thirdBoxGenericPage','firstBoxBlueGenericPage','secondBoxOrangeGenericPage'];
	var arrBoxesClasses = ['orange298Box','blue298Box','redLongBox','blue298Box','orange298Box'];
	for (var x=0; x<arrBoxes.length; x++)
	{
		var strDiv = arrBoxes[x];
		var divBox = jQuery('#'+strDiv+' .ms-rtestate-field');
		if ( divBox.length > 0 )
		{
			var text = jQuery.trim(divBox.text());
			if ( text != '' && text != '&nbsp;' && text.length != 0 )
			{
				jQuery('#'+strDiv).attr('class',arrBoxesClasses[x]);
			}
			else
				jQuery('#'+strDiv).remove();
		}
	}
});

jQuery (function() {
	var boxHome = jQuery('.twoColumnsBox');
	if ( boxHome.length > 0 )
	{
		var boxSx = jQuery('.firstBoxBackground');
		var boxDx = jQuery('.secondBoxBackground');
		if ( boxSx.length > 0 && boxSx.length > 0 )
		{
			var hSx = boxSx.height();
			var hDx = boxDx.height();
			if ( hSx > hDx )
			{
				boxDx.css({
							'height': hSx + 'px',
							'min-height': hSx + 'px'
						 });
			}
			else
			{
				boxSx.css({
							'height': hDx + 'px',
							'min-height': hDx + 'px'
						 });
			}
		}
	}
});

jQuery(function() {
	var boxTableStd = jQuery('table.ms-rteTable-tableBorderStandard');
	if ( boxTableStd.length > 0 )
	{
		var trStd = boxTableStd.find('tr');
		trStd.each(function(i) {
			if (Math.round(i/2)!=i/2)
			jQuery(this).css('backgroundColor','#F5FAFB');
		});
		var boxTr = boxTableStd.find('tr');
		boxTr.each(function() {
			var thisTR = jQuery(this);
			var boxTh = thisTR.find('th');
			var boxTd = thisTR.find('td');
			lenTh = boxTh.length;
			lenTd = boxTd.length;
		//	console.log(lenTh+" | "+lenTd)
			if ( lenTh > 0 )
			{
				var partTh = 100 / lenTh;
				boxTh.css('width',Math.floor(partTh)+'%');
			}
			if ( lenTd > 0 )
			{
				var partTd = 100 / lenTd;
				boxTd.css('width',Math.floor(partTd)+'%');
			}
		});
	}
});

jQuery(function() {
	var boxBlueLong = jQuery('.ms-rteElement-boxBluGradient, .ms-rteStyle-boxBluGradient');
	if ( boxBlueLong.length > 0 )
	{
		boxBlueLong.each(function() {
			var content = jQuery(this).html();
			var newContent = '<div class="boxBackground"><div class="boxTop"><div class="boxBottom"><div class="boxContent">' + content + '</div></div></div></div>';
			var io = jQuery(this);
			io.html(newContent);
		});
	}
	jQuery('span.ms-rteStyle-boxBluGradient').each(function(){
		jQuery(this).replaceWith('<div class="ms-rteStyle-boxBluGradient">'+jQuery(this).html()+'</div>');
	});
	var boxBlueLong = jQuery('.ms-rteElement-boxBluGradient, .ms-rteStyle-boxBluGradient');
	if ( boxBlueLong.length > 0 )
	{
		boxBlueLong.each(function() {
			var io = jQuery(this);
			var imgBg = new Image();
			imgBg.onload = function()
			{
			//	alert(this.width + ' x ' + this.height);
				var boxContent = io.find('.boxContent');
				if ( (io.height()-15+5) < this.height ) boxContent.css('height',(this.height-15+5));
			}
			var imgFile = io.find('.boxTop').css('backgroundImage').replace('url("','').replace('")','');
			imgBg.src = imgFile;
		});
	}
});

function control_fields() {
	var form = jQuery('#aspnetForm');
	var countSpanFound = 0;
	var errorField = false;
	var emptyField = true;
	var txtErrorMessage = "";
	var txtErrorMessage_2 = "";

//	controllo campi obbligatori normali
	form.find('span').each(function(i) {
		var text = '' + jQuery(this).text();
	});

//	controllo cambi obbligatori doppio asterisco "(**)"
	form.find('span').each(function(i) {
		var text = '' + jQuery(this).text();

		if ( text.indexOf('<!--') < 0 && text.indexOf('(*)') >= 0 ) {
			var inputField = jQuery(this)
										.closest('td')
										.next('td')
										.find('input,textarea,select');
			if ( jQuery.trim(inputField.val()).length == 0 || jQuery.trim(inputField.val()) == "-" ) {
				errorField = true;
				inputField.css({'border':'1px solid red'});
			}
		}

		if ( text.indexOf('<!--') < 0 && text.indexOf('(**)') >= 0 ) {
			if ( countSpanFound > 0 ) {
				var inputField = jQuery(this)
											.closest('td')
											.next('td')
											.find('input');
				if ( jQuery.trim(inputField.val()).length > 0 ) emptyField = false;
				else {
					inputField.css({'border':'1px solid red'});
				}
			}
			else {
				txtErrorMessage_2 = text;
			}
			countSpanFound++;
		}

	});
	if ( errorField ) txtErrorMessage += "Prego compili tutti i campi obbligatori.\n";
	if ( emptyField ) txtErrorMessage += txtErrorMessage_2;

	if ( !emptyField && txtErrorMessage.length == 0 ) {
		WebForm_OnSubmit();
	} else {
		alert(txtErrorMessage);
	}
}

function patch_form() {
//	onsubmit = "javascript:return WebForm_OnSubmit();"
	var form = jQuery('#aspnetForm');
	jQuery('input[type=button]')
								.removeAttr('onclick')
								.click(function(){
								//	jQuery('#aspnetForm').submit();
									control_fields();
								});
	
	form.removeAttr('onsubmit');
	form.submit(function(){
		return control_fields();
	});
//	form.attr('onsubmit','javascript:return control_fields();');
}

function ResizeRibbonAndWorkspace()
{
//	gestione RIBBON
	var ribbon_content = jQuery('#CrifRibbon');
	var contents = jQuery(".OD_container");

	if ( ribbon_content.length > 0 )
	{
	//	jQuery("body").css('overflow','hidden').css('overflowX','hidden').css('overflowY','hidden');
	//	contents.css('height','auto').css('overflow','hidden').css('overflowX','hidden').css('overflowY','hidden');
		contents.css('width','980px').css('overflow','auto').css('overflowX','auto').css('overflowY','auto');
	//	jQuery("body #MSO_ContentTable").css('position','static !important');
	}
	else
	{
		jQuery("body").css('height','auto').css('overflow','auto').css('overflowX','auto').css('overflowY','auto');
		contents.css('height','auto').css('overflow','hidden').css('overflowX','hidden').css('overflowY','hidden');
	}
}
