﻿$(document).ready(function() {

	$("a").each(function() {
		if (this.href.indexOf(".pdf") > -1)
			$(this).attr("rel", "popup");
	});

	$("a[rel=popup]").click(function() {
		var sUrl = this.href;

		if (sUrl.indexOf("javascript") < 0)
			window.open(sUrl);
		else
			setTimeout(sUrl.replace("javascript:", ""), 0);

		return false;
	});

	$("a[rel=popup]").bind("onkeydown", function() {
		if (sUrl.indexOf("javascript") < 0)
			window.open(sUrl);
		else
			setTimeout(sUrl, 0);

		return false;
	});

	if (!($.browser.msie && $.browser.version < 7)) {
		$("img[hspace]").each(function() {
			if ($(this).attr("hspace") != -1) {
				$(this).css("margin-left", $(this).attr("hspace") + "px");
				$(this).css("margin-right", $(this).attr("hspace") + "px");
			}
			if ($(this).attr("vspace") != -1) {
				$(this).css("margin-top", $(this).attr("vspace") + "px");
				$(this).css("margin-bottom", $(this).attr("vspace") + "px");
			}
		});
	}

	$("div[align]").each(function() {
		if ($(this).attr("align") != -1) {
			var attrValue = $(this).attr("align");
			if (attrValue.toLowerCase() == "center")
				$(this).css("text-align", "center");
		}
	});

	$("img").each(function() {
		if ($(this).attr("alt") == " ") $(this).attr("alt", "");
		if ($(this).attr("align").toLowerCase() == "left") $(this).css("float", "left");
		if ($(this).attr("align").toLowerCase() == "right") $(this).css("float", "right");
	});

	$(".rollover").mouseover(function() {
		this.src = this.src.replace(".jpg", "-hover.jpg");
		this.src = this.src.replace(".gif", "-hover.gif");
		if (!($.browser.msie && $.browser.version < 7))
			this.src = this.src.replace(".png", "-hover.png");
	});
	$(".rollover").mouseout(function() {
		this.src = this.src.replace("-hover.jpg", ".jpg");
		this.src = this.src.replace("-hover.gif", ".gif");
		if (!($.browser.msie && $.browser.version < 7))
			this.src = this.src.replace("-hover.png", ".png");
	});

	/* permette di fare il login premendo il tasto "invio" */
	$("input.inputGoToLogin").keypress(function(event) {
		if (event.keyCode == 13) {
			$("input.autoClickLogin").click();
			return false;
		}
	});

	/* ##### bollo per bacheca ##### */
	$("textarea#messaggioBollo").keypress(function(event) {
		var messaggio = $.trim($("textarea#messaggioBollo").val());
		if (messaggio.length > 160) {
			messaggio = messaggio.substr(0, 160);
			/* reimposto il messaggio con la massima lunghezza */
			$("textarea#messaggioBollo").val(messaggio);
		}
	});
	$("textarea#messaggioBollo").focus(function(event) {
		if ($(this).val().toLowerCase() == "messaggio: (max. 160 car.)")
			$(this).val("");
	});
	$("textarea#messaggioBollo").blur(function(event) {
		if ($(this).val() == "")
			$(this).val("Messaggio: (max. 160 car.)");
	});
	$("input#nickBollo").focus(function(event) {
		if ($(this).val().toLowerCase() == "nickname:")
			$(this).val("");
	});
	$("input#nickBollo").blur(function(event) {
		if ($(this).val() == "")
			$(this).val("Nickname:");
	});
	$("input#nickBollo").keypress(function(event) {
		if (event.keyCode == 13) {
			$("a#inputSubmitBacheca").click();
			return false;
		}
	});

});

/* ##### invio del messaggio della bacheca ##### */
var globalSendingMessage = false;
function sendMessageBacheca(language) {
	var messaggio = $.trim($("textarea#messaggioBollo").val());
	var nick = $.trim($("input#nickBollo").val());

	var sErrorMessage = "";
	if (messaggio == null || messaggio == "" || messaggio.toLowerCase() == "messaggio: (max. 160 car.)")
		sErrorMessage += "- Inserire il messaggio;\r\n";
	if (nick == null || nick == "" || nick.toLowerCase() == "nickname:")
		sErrorMessage += "- Inserire il nickname;\r\n";

	if (sErrorMessage != "") {
		alert(sErrorMessage);
		return false;
	}

	messaggio = removeHTMLTags(messaggio);
	nick = removeHTMLTags(nick);
	if (messaggio.length > 160) {
		messaggio = messaggio.substr(0, 160);
	}
	/* reimposto il messaggio con la massima lunghezza */
	$("textarea#messaggioBollo").val(messaggio);

	/* effettuare le operazioni necessarie */
	var confirmMsg = confirm("Il messaggio sarà pubblicato previa approvazione MyLife.\r\nVuoi inviare il messaggio?");

	if (confirmMsg && !globalSendingMessage) {
	    globalSendingMessage = true;
	    $("div#bolloHP a#inputSubmitBacheca img").attr("src", "/common/images/loader-bacheca.gif");
	    $.ajax({
	        type: "POST",
	        url: "/noticeboardadd.ashx",
	        dataType: "json",
	        cache: false,
	        data: ({
	            fLang: $.trim(language),
	            fNick: $.trim(nick),
	            fText: $.trim(messaggio)
	        }),
	        success: function(data) {
	            var error = data.error;
	            var msg = data.msg;
	            if (error != "") {
	                alert(error);
	            }
	            else {
	                /* una volta inviato il messaggio azzero i campi */
	                $("textarea#messaggioBollo").val("Messaggio: (max. 160 car.)");
	                $("input#nickBollo").val("Nickname:");
	                alert(msg);
	            }
	            globalSendingMessage = false;
	            $("div#bolloHP a#inputSubmitBacheca img").attr("src", "/common/images/btn-invio-bacheca-" + language + ".gif");
	        }
	    });
	}
	return false;
}

function removeHTMLTags(contentText) {
	var returnValue = contentText;
	/*  This line is optional, it replaces escaped brackets with real ones,  i.e. < is replaced with < and > is replaced with > */
	returnValue = returnValue.replace(/&(lt|gt);/g, function(strMatch, p1) {
		return (p1 == "lt") ? "<" : ">";
	});
	returnValue = returnValue.replace(/<\/?[^>]+(>|$)/g, "");
	// alert("Input code:\n" + contentText + "\n\nOutput text:\n" + returnValue);
	return returnValue;
}

/* riempimento input select */
function fillSelect(objSelectorToFill, arrItems) {
	var options = "";
	for (var i = 0; i < arrItems.length; i++) {
		options += "<option value=\"" + arrItems[i][1] + "\">" + arrItems[i][0] + "</option>";
	}
	$(objSelectorToFill).html(options);
}

/* controllo validazione email */
function isEmail(string) {
	if (string == "")
		return false;
	return (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

/* ##### creazione dell'avatar ##### */
function creaAvatar(avatarType, backgroundColor, images) {
	backgroundColor = backgroundColor.replace("#", "");

	$("div#elettronici div.avatar").css("visibility", "visible");

	$("#downloadlink").css("visibility", "visible");
	$("#avatarPreview").css("visibility", "visible");

	$("#downloadlink").attr("href", "/avatar.ashx?preview=false&type=" + avatarType + "&color=" + backgroundColor + "&images=" + images);
	if ($.browser.msie)
		$("#downloadlink").attr("target", "_blank");

	$("#avatarPreview").attr("src", "/avatar.ashx?preview=true&type=" + avatarType + "&color=" + backgroundColor + "&images=" + images);
}
function chiudiAvatar() {
	$("div#elettronici div.avatar").css("visibility", "hidden");

	$("#downloadlink").css("visibility", "hidden");
	$("#avatarPreview").css("visibility", "hidden");
}
function chiudiOverlay() {
	$("#btn_close").click();
}
/* ##### creazione dell'avatar ##### */


/* vecchie funzioni */
/* ##################### tendina delle lingue  ##################### */
var nascondiMenu = false;
function ViewDdlCountryMagazine(divContainerId, IsOver) {
	var objDivList = document.getElementById(divContainerId);
	if (IsOver) {
		nascondiMenu = false;

		objDivList.style.display = 'block';
	} else {
		nascondiMenu = true;

		setTimeout("NascondiMenu('" + divContainerId + "');", 200);
	}
}
function NascondiMenu(divContainerId) {
	if (nascondiMenu)
		document.getElementById(divContainerId).style.display = 'none';
}
/* ##################### Rollover sub menu  ##################### */
function SubMenuRollOver(idImg, isOver) {
	var objImg = document.getElementById(idImg)
	var urlImgNormal = "/common/images/pixel.gif";
	var urlImgOver = "/common/images/css/sub-menu-hover.gif";
	if (isOver)
		objImg.src = urlImgOver;
	else
		objImg.src = urlImgNormal;
}
/* ##################### apertura finestra in popup con le dimensioni  ##################### */
var WindowTemp;
function popup_open(popupfile, popupname, popupwidth, popupheight, resize, status, scroll, tool, menu)
{
	//titlebar=0;
	var winleft = (screen.width - popupwidth) / 2;
	var wintop = (screen.height - popupheight) / 2;
	var params = "width=" + popupwidth + ",height=" + popupheight + ",top=" + wintop + ",left=" + winleft + ",resizable=" + resize + ",status=" + status + ",scrollbars=" + scroll + ",toolbar=" + tool + ",location=0,directories=0,menubar=" + menu + "";
	if((navigator.appName.indexOf("Explorer")!=-1)&&(parseInt(navigator.appVersion.substring(0,1))<4))
	{
		newwin = window.open(popupfile,popupname,params);
	}
	else
	{
		if(WindowTemp)
		{
			if(!WindowTemp.closed)
			{
				WindowTemp.close();
				WindowTemp="";
			}
		}

		newwin = window.open(popupfile,popupname,params);
		WindowTemp = newwin;
		WindowTemp.name = "WindowTemp";
		newwin="";
	}
}
