var error = 0;
var isGuideNegoziOpen = false;

function openBoxGuide()
{
	if(!isGuideNegoziOpen)
	{
		document.getElementById("dillo_amico_guide").style.display = 'inline';
		window.location.href = '#masterContent2';
	}
	isGuideNegoziOpen = true;
}

function closeBoxGuide()
{
	if(isGuideNegoziOpen)
		document.getElementById("dillo_amico_guide").style.display = 'none';
	isGuideNegoziOpen = false;
	resetFormElementSegnala();
}

function trimSegnala(stringa)
{
    while (stringa.substring(0,1) == ' '){
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' '){
        stringa = stringa.substring(0, stringa.length-1);
    }
    return stringa;
}

function resetElementSegnala(id, setValue, errorText, errorEmailText)
{
	document.getElementById(id).style.color = '#666666';
	var value = document.getElementById(id).value;
	if(value == '' || value == errorText || value == errorEmailText)
		document.getElementById(id).value = setValue;
}

function checkInputTextSegnala(id, errorText, errorEmailText, isEmail)
{
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	var value = trimSegnala(document.getElementById(id).value);
	if(value == '' || value == errorText)
	{
		document.getElementById(id).style.color = '#FF0000';
		document.getElementById(id).value = errorText;
		error = error + 1;
	}else
	{
		if(isEmail)
		{
			if(!value.match(emailExp))
			{
				document.getElementById(id).style.color = '#FF0000';
				document.getElementById(id).value = errorEmailText;
				error = error + 1;
			}
		}
	}
}

function resetElementValueSegnala(id, defaultText)
{
	document.getElementById(id).value = defaultText;
	document.getElementById(id).style.color = '#666666';
}

function resetFormElementSegnala()
{
    //resetta il valore degli elementi
	resetElementValueSegnala('txtTuoNomeInvitaAmico', '');
	resetElementValueSegnala('txtSuoNomeInvitaAmico', '');
	resetElementValueSegnala('txtTuaEmailInvitaAmico', '');
	resetElementValueSegnala('txtSuaEmailInvitaAmico', '');
}

function sendAmico(large)
{
	error = 0;
	checkInputTextSegnala('txtTuoNomeInvitaAmico', 'Inserisci il tuo nome', '', false);
	checkInputTextSegnala('txtSuoNomeInvitaAmico', 'Inserisci il suo nome', '', false);
	checkInputTextSegnala('txtTuaEmailInvitaAmico', 'Inserisci la tua email', 'Email errata', true);
	checkInputTextSegnala('txtSuaEmailInvitaAmico', 'Inserisci la sua email', 'Email errata', true);
	
	if(error == 0)
	{

	var linkPagina = escape( document.location.href.substring( 0, document.location.href.indexOf('?')+1 ) ) ;

		linkPagina += escape( 'zone=' + zone );

		if (document.location.href.indexOf('idEIPub') != -1) {

			linkPagina += escape('&idEIPub=') + eiid;

		} else {

			linkPagina += escape('&renderall=on&idLSPub=') + lsid;
		}

	
		var dataString = "type=" + $("#emailInvitaAmico input[name='typeInvitaAmico']").val() +
			"&sender=" + $("#emailInvitaAmico input[name='txtTuoNomeInvitaAmico']").val() +
			"&sender_email=" + $("#emailInvitaAmico input[name='txtTuaEmailInvitaAmico']").val() +
			"&receiver=" + $("#emailInvitaAmico input[name='txtSuoNomeInvitaAmico']").val() +
			"&receiver_email=" + $("#emailInvitaAmico input[name='txtSuaEmailInvitaAmico']").val() +
			"&title=" + $("#emailInvitaAmico input[name='titleInvitaAmico']").val() +
			"&link=" + linkPagina;

		$.ajax({
		   type: "POST",
		   url: "/jsp/invitaAmico.jsp",
		   data: dataString,
		   success: function(msg){

				//mostro il feedback...
				var winFeedbackNewsletter = document.getElementById("winFeedbackNewsletter");
				if (winFeedbackNewsletter != undefined && winFeedbackNewsletter != null)
					showHideElement('winFeedbackNewsletter', 'hidden');
			
				showHideElement('winFeedbackSegnalaAmico','visible');
		   },
		   error: function(msg){

				//mostro il feedback...
				var winFeedbackNewsletter = document.getElementById("winFeedbackNewsletter");
				if (winFeedbackNewsletter != undefined && winFeedbackNewsletter != null)
					showHideElement('winFeedbackNewsletter', 'hidden');
			
				showHideElement('winFeedbackSegnalaAmico','visible');
		   }
		});
	}
}
