﻿function onSilverlightError(sender, args) {

	var appSource = "";
	if (sender != null && sender != 0) {
		appSource = sender.getHost().Source;
	}
	var errorType = args.ErrorType;
	var iErrorCode = args.ErrorCode;

	var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";

	errMsg += "Code: " + iErrorCode + "    \n";
	errMsg += "Category: " + errorType + "       \n";
	errMsg += "Message: " + args.ErrorMessage + "     \n";

	if (errorType == "ParserError") {
		errMsg += "File: " + args.xamlFile + "     \n";
		errMsg += "Line: " + args.lineNumber + "     \n";
		errMsg += "Position: " + args.charPosition + "     \n";
	}
	else if (errorType == "RuntimeError") {
		if (args.lineNumber != 0) {
			errMsg += "Line: " + args.lineNumber + "     \n";
			errMsg += "Position: " + args.charPosition + "     \n";
		}
		errMsg += "MethodName: " + args.methodName + "     \n";
	}

	throw new Error(errMsg);
}

/*function setLightBoxTopPosition() {
    document.getElementById("lightboxContainer").style.top = document.documentElement.scrollTop + "px";
    document.getElementById('customPanelContainer').style.top = document.documentElement.scrollTop + "px";
    //document.getElementById("lightBoxObject").style.top = document.documentElement.scrollTop + "px";
}*/

function setLightBoxTopPosition() {
    document.getElementById("lightboxContainer").style.top = document.documentElement.scrollTop + "px";
}

/*function setCustomPanelTopPosition() {
    document.getElementById('customPanelContainer').style.top = document.documentElement.scrollTop + "px";
    document.getElementById('customPanelContent').style.top = (-1 * (document.documentElement.scrollTop)) + "px";
}*/

function setCustomPanelTopPosition() {
    if(document.getElementById('customPanelContainer')!= null)
    {
        document.getElementById('customPanelContainer').style.top = document.documentElement.scrollTop + "px";
        document.getElementById('customPanelContent').style.top = (-1 * (document.documentElement.scrollTop)) + "px";
    }
}

function showLightBox(imageUrl, imageCaption) {

   /* //var myImage = document.createElement('img');
    var myImage = document.getElementById('imageTest');
    myImage.setAttribute('src', imageUrl);
    alert(myImage.width);
    alert(myImage.height + 55);
	
	var lightboxContainer = document.getElementById("lightboxContainer");
    lightboxContainer.style.width = "100%";
    lightboxContainer.style.height = "100%";
    lightboxContainer.style.display = 'block';
    //setLightBoxTopPosition();

    var lightBoxObject = document.getElementById("lightBoxObject");

   	lightBoxObject.Content.Page.InitControl(imageUrl, imageCaption);
	lightBoxObject.width = 800;
	lightBoxObject.height = 600 + 55;
	setLightBoxTopPosition();
	//lightBoxObject.removeAttribute('class');
	//lightBoxObject.setAttribute('width', myImage.width);
	//lightBoxObject.setAttribute('height', (myImage.height + 55));

    //lightboxContainer.appendChild(myImage);*/

    var objLink = document.createElement('a');
    objLink.setAttribute('href', imageUrl);
    objLink.setAttribute('rel', 'lightbox');
    objLink.setAttribute('title', imageCaption);
    Lightbox.prototype.start(objLink);
}

function hideLightBox()
{
	document.getElementById("lightboxContainer").style.width = "0px";
	document.getElementById("lightboxContainer").style.height = "0px";
	document.getElementById("lightBoxObjectBorder").style.border = "none";
	
	var lightBoxObject = document.getElementById("lightBoxObject");
	//lightBoxObject.width = '1';
	//lightBoxObject.height = '1';
}


/*function showCustomAlert(message) {
    var container = document.getElementById('customPanelContainer');
    //var alertMessage = message.toString();
    
    if (container.style.display == 'block')
        container.style.display = 'none';
    else
        container.style.display = 'block';

    if (message != null)
    {
        //document.getElementById('alertMessage').innerHTML = "";
        document.getElementById('alertMessage').innerHTML = message;
    }
}*/

function hideCustomAlert(){
    var container = document.getElementById('customPanelContainer');
    container.style.display = 'none';
}

function smsRegistrationValidation()
{
    var isValid = true;
    var errorMessage = "";
    
    if(document.getElementById("name").value == null || document.getElementById("name").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Nome.";
    }
    
    if(document.getElementById("surname").value == null || document.getElementById("surname").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Cognome.";
    }
    
    var italianDatePattern = /(((0[1-9]|[12][0-9]|3[01])([/])(0[13578]|10|12)([/])(\d{4}))|(([0][1-9]|[12][0-9]|30)([/])(0[469]|11)([/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([/])(02)([/])(\d{4}))|((29)(\.|-|\/)(02)([/])([02468][048]00))|((29)([/])(02)([/])([13579][26]00))|((29)([/])(02)([/])([0-9][0-9][0][48]))|((29)([/])(02)([/])([0-9][0-9][2468][048]))|((29)([/])(02)([/])([0-9][0-9][13579][26])))/g;
    if(document.getElementById("birthDate").value == null || document.getElementById("birthDate").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Data di nascita.";
    }
    else if(document.getElementById("birthDate").value.match(italianDatePattern) == null)
    {
        isValid = false;
        errorMessage += "<br />Formato data di nascita non valido. Usare gg/mm/aaaa";
    }
    
    if(document.getElementById("address").value == null || document.getElementById("address").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Indirizzo.";
    }
    
    if(document.getElementById("city").value == null || document.getElementById("city").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Citt&agrave;.";
    }
    
    var districtPattern = /^[a-z A-Z]{2}$/g;
    if(document.getElementById("district").value == null || document.getElementById("district").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Provincia.";
    }
    else if(document.getElementById("district").value.match(districtPattern) == null)
    {
        isValid = false;
        errorMessage += "<br />Formato della provincia non valido. Usare due lettere.";
    }
    
    var zipCodePattern = /^\d{5}$/g;
    if(document.getElementById("zipCode").value == null || document.getElementById("zipCode").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo C.A.P.";
    }
    else if(document.getElementById("zipCode").value.match(zipCodePattern) == null)
    {
        isValid = false;
        errorMessage += "<br />Campo C.A.P. errato.";
    }
    
    var mobilePhonePattern = /^3\d{8,}$/g;
    if(document.getElementById("mobilePhone").value.match(mobilePhonePattern) == null)
    {
        isValid = false;
        errorMessage += "<br />Formato numero telefonico non valido. Inserire solo il numero senza prefisso.";
    }
    var emailPattern = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/g;
    if(document.getElementById("email").value.match(emailPattern) == null)
    {
        isValid = false;
        errorMessage += "<br />Email non valida.";
    }
    
    if(!document.getElementById("artistico").checked && !document.getElementById("corsa").checked && !document.getElementById("hockey").checked)
    {
        isValid = false;
        errorMessage += "<br />Selezionare almeno una disciplina.";
    }
    
    if(!document.getElementById("privacyConsentYes").checked)
    {
        isValid = false;
        errorMessage += "<br />&Egrave; necessario accettare l'informativa sulla privacy.";
    }
    
    if(isValid == false)
        showCustomAlert("Attenzione: " + errorMessage);
        
    return isValid;
}

function newsletterRegistrationValidation()
{
    var isValid = true;
    var errorMessage = "";
    
    if(document.getElementById("name").value == null || document.getElementById("name").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Nome.";
    }
    
    if(document.getElementById("surname").value == null || document.getElementById("surname").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Cognome.";
    }
    
    var italianDatePattern = /(((0[1-9]|[12][0-9]|3[01])([/])(0[13578]|10|12)([/])(\d{4}))|(([0][1-9]|[12][0-9]|30)([/])(0[469]|11)([/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([/])(02)([/])(\d{4}))|((29)(\.|-|\/)(02)([/])([02468][048]00))|((29)([/])(02)([/])([13579][26]00))|((29)([/])(02)([/])([0-9][0-9][0][48]))|((29)([/])(02)([/])([0-9][0-9][2468][048]))|((29)([/])(02)([/])([0-9][0-9][13579][26])))/g;
    if(document.getElementById("birthDate").value == null || document.getElementById("birthDate").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Data di nascita.";
    }
    else if(document.getElementById("birthDate").value.match(italianDatePattern) == null)
    {
        isValid = false;
        errorMessage += "<br />Formato data di nascita non valido. Usare gg/mm/aaaa";
    }
    
    if(document.getElementById("address").value == null || document.getElementById("address").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Indirizzo.";
    }
    
    if(document.getElementById("city").value == null || document.getElementById("city").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Citt&agrave;.";
    }
    
    var districtPattern = /^[a-z A-Z]{2}$/g;
    if(document.getElementById("district").value == null || document.getElementById("district").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo Provincia.";
    }
    else if(document.getElementById("district").value.match(districtPattern) == null)
    {
        isValid = false;
        errorMessage += "<br />Formato della provincia non valido. Usare due lettere.";
    }
    
    var zipCodePattern = /^\d{5}$/g;
    if(document.getElementById("zipCode").value == null || document.getElementById("zipCode").value == "")
    {
        isValid = false;
        errorMessage += "<br />Compilare il campo C.A.P.";
    }
    else if(document.getElementById("zipCode").value.match(zipCodePattern) == null)
    {
        isValid = false;
        errorMessage += "<br />Campo C.A.P. errato.";
    }
    
    var emailPattern = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/g;
    if(document.getElementById("email").value.match(emailPattern) == null)
    {
        isValid = false;
        errorMessage += "<br />Email non valida.";
    }
    
    if(!document.getElementById("artistico").checked && !document.getElementById("corsa").checked && !document.getElementById("hockey").checked)
    {
        isValid = false;
        errorMessage += "<br />Selezionare almeno una disciplina.";
    }
    
    if(!document.getElementById("privacyConsentYes").checked)
    {
        isValid = false;
        errorMessage += "<br />&Egrave; necessario accettare l'informativa sulla privacy.";
    }
    
    if(isValid == false)
        showCustomAlert("Attenzione: " + errorMessage);
        
    return isValid;
}

function profileValidation()
{
    var isValid = true;
    var errorMessage = "";
    
    if(document.getElementById("name").value == null || document.getElementById("name").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Nome.";
    }
    
    if(document.getElementById("surname").value == null || document.getElementById("surname").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Cognome.";
    }
    
    var italianDatePattern = /(((0[1-9]|[12][0-9]|3[01])([/])(0[13578]|10|12)([/])(\d{4}))|(([0][1-9]|[12][0-9]|30)([/])(0[469]|11)([/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([/])(02)([/])(\d{4}))|((29)(\.|-|\/)(02)([/])([02468][048]00))|((29)([/])(02)([/])([13579][26]00))|((29)([/])(02)([/])([0-9][0-9][0][48]))|((29)([/])(02)([/])([0-9][0-9][2468][048]))|((29)([/])(02)([/])([0-9][0-9][13579][26])))/g;
    if(document.getElementById("birthDate").value == null || document.getElementById("birthDate").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Data di nascita.";
    }
    else if(document.getElementById("birthDate").value.match(italianDatePattern) == null)
    {
        isValid = false;
        errorMessage += "\nFormato data di nascita non valido. Usare gg/mm/aaaa";
    }
    
    if(document.getElementById("address").value == null || document.getElementById("address").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Indirizzo.";
    }
    
    if(document.getElementById("city").value == null || document.getElementById("city").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Citt&agrave;.";
    }
    
    var districtPattern = /^[a-z A-Z]{2}$/g;
    if(document.getElementById("district").value == null || document.getElementById("district").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Provincia.";
    }
    else if(document.getElementById("district").value.match(districtPattern) == null)
    {
        isValid = false;
        errorMessage += "\nFormato della provincia non valido. Usare due lettere.";
    }
    
    var zipCodePattern = /^\d{5}$/g;
    if(document.getElementById("zipCode").value == null || document.getElementById("zipCode").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo C.A.P.";
    }
    else if(document.getElementById("zipCode").value.match(zipCodePattern) == null)
    {
        isValid = false;
        errorMessage += "\nCampo C.A.P. errato.";
    }
    
    var emailPattern = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/g;
    if(document.getElementById("email").value.match(emailPattern) == null)
    {
        isValid = false;
        errorMessage += "\nEmail non valida.";
    }
    
    /*if(!document.getElementById("artistico").checked && !document.getElementById("corsa").checked && !document.getElementById("hockey").checked)
    {
        isValid = false;
        errorMessage += "\nSelezionare almeno una disciplina.";
    }*/
    
    /*if(!document.getElementById("privacyConsentYes").checked)
    {
        isValid = false;
        errorMessage += "\n&Egrave; necessario accettare l'informativa sulla privacy.";
    }*/
    
    var newPassword = document.getElementById("newPassword");
    var confirmNewPassword = document.getElementById("confirmNewPassword");

    //controllo se esistono i campi
    if(newPassword != null  && confirmNewPassword != null)
    {
        if(newPassword.value != '')
        {
            if(newPassword.value != confirmNewPassword.value)
            {
                isValid = false;
                errorMessage += "\nLe password non coincidono.";
            }else if(newPassword.value.length < 6)
            {
                isValid = false;
                errorMessage += "\nLa password è inferiore a 6 caratteri.";
            }
        }
    }
    
    if(isValid == false)
        alert("Attenzione: " + errorMessage);
    else
    {
        if(confirm("Continuare con le modifiche apportate?"))
          return isValid;  
        else
        {
           alert("Le modifiche non saranno salvate.");
           return false
        }
    }     
        
    
}

function exitEditProfile()
{
    if(confirm("Abbandonare questa pagina?\nTutti i dati non salvati saranno persi"))
       document.location.href = 'http://www.fihp.org/' ;
    else
       alert("Si è scelto di continuare con la modifica dei dati.");
}

function xinUserRegistrationValidation()
{
    var isValid = true;
    var errorMessage = "";
    
    if(document.getElementById("name").value == null || document.getElementById("name").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Nome.";
    }
    
    if(document.getElementById("surname").value == null || document.getElementById("surname").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Cognome.";
    }
    
    var italianDatePattern = /(((0[1-9]|[12][0-9]|3[01])([/])(0[13578]|10|12)([/])(\d{4}))|(([0][1-9]|[12][0-9]|30)([/])(0[469]|11)([/])(\d{4}))|((0[1-9]|1[0-9]|2[0-8])([/])(02)([/])(\d{4}))|((29)(\.|-|\/)(02)([/])([02468][048]00))|((29)([/])(02)([/])([13579][26]00))|((29)([/])(02)([/])([0-9][0-9][0][48]))|((29)([/])(02)([/])([0-9][0-9][2468][048]))|((29)([/])(02)([/])([0-9][0-9][13579][26])))/g;
    if(document.getElementById("birthDate").value == null || document.getElementById("birthDate").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Data di nascita.";
    }
    else if(document.getElementById("birthDate").value.match(italianDatePattern) == null)
    {
        isValid = false;
        errorMessage += "\nFormato data di nascita non valido. Usare gg/mm/aaaa";
    }
    
    if(document.getElementById("address").value == null || document.getElementById("address").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Indirizzo.";
    }
    
    if(document.getElementById("city").value == null || document.getElementById("city").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Città;.";
    }
    
    var districtPattern = /^[a-z A-Z]{2}$/g;
    if(document.getElementById("district").value == null || document.getElementById("district").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo Provincia.";
    }
    else if(document.getElementById("district").value.match(districtPattern) == null)
    {
        isValid = false;
        errorMessage += "\nFormato della provincia non valido. Usare due lettere.";
    }
    
    var zipCodePattern = /^\d{5}$/g;
    if(document.getElementById("zipCode").value == null || document.getElementById("zipCode").value == "")
    {
        isValid = false;
        errorMessage += "\nCompilare il campo C.A.P.";
    }
    else if(document.getElementById("zipCode").value.match(zipCodePattern) == null)
    {
        isValid = false;
        errorMessage += "\nCampo C.A.P. errato.";
    }
    
    var mobilePhonePattern = /^3\d{8,}$/g;
    if(document.getElementById("mobilePhone").value.match(mobilePhonePattern) == null)
    {
        isValid = false;
        errorMessage += "\nFormato numero telefonico non valido. Inserire solo il numero senza prefisso.";
    }
    
    if(document.getElementById("mobilePhoneConfirm").value != document.getElementById("mobilePhone").value)
    {
        isValid = false;
        errorMessage += "\nConferma numero telefonico non valida.";
    }    
    
    var emailPattern = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/g;
    if(document.getElementById("email").value.match(emailPattern) == null)
    {
        isValid = false;
        errorMessage += "\nEmail non valida.";
    }
    
    if(document.getElementById("emailConfirm").value != document.getElementById("email").value)
    {
        isValid = false;
        errorMessage += "\nConferma Email non valida.";
    }
    
    if(!document.getElementById("privacyConsentYes").checked)
    {
        isValid = false;
        errorMessage += "\n&Egrave; necessario accettare l'informativa sulla privacy.";
    }
    
    if(isValid == false)
        alert("Attenzione: " + errorMessage);
        
    return isValid;
}

function submitTestForm()
{
    document.getElementById('showAnswerResult').value = 1;
	document.testForm.submit();
}

function send(isAdminArea)
{	
	var booleanReturn = false;
	
	if(document.getElementById('title').value != "")
	{
		document.getElementById('insertDiscussion').value = 1;
		document.newDiscussion.submit();
		
		if(isAdminArea)
		    alert("Discussione Salvata!");
		else
		    alert("Discussione Salvata!\nLa pubblicazione avverrà solo dopo la convalida dell'amministratore.");        
		    
		booleanReturn = true;
	}
	else
	{
		document.getElementById('insertDiscussion').value = 0;
		booleanReturn = false;
		alert("Attenzione!\nControllare i campi.");
	}
		
	return	booleanReturn;
}

function addComment(isAdminArea)
{	
	var booleanReturn = false;
	
	if(document.getElementById('contentValidator').value != 0)
	{
		document.getElementById('insertComment').value = 1;
		if(isAdminArea)
		    alert("Commento Salvato!");
		else
		    alert("Commento Salvato!\nLa pubblicazione avverrà solo dopo la convalida dell'amministratore.");        
		    
		booleanReturn = true;
	}
	else
	{
		document.getElementById('insertComment').value = 0;
		booleanReturn = false;
		alert("Attenzione!\nControllare i campi.");
	}
		
	return	booleanReturn;
}

function showHiddenContainer(containerId)
{
    if(containerId != null)
    {
        if(document.getElementById(containerId).style.display == 'none');
        {
            document.getElementById(containerId).style.display = 'block';
        }
    }
}

function hideHiddenContainer(containerId)
{
    if(containerId != null)
    {
        if(document.getElementById(containerId).style.display == 'block');
        {
            document.getElementById(containerId).style.display = 'none';
        }
    }
}

/*###############################################################################################################
 #								FUNZIONE CHIAMATA AJAX IN WEBTV													#
 ################################################################################################################*/
 function retrieveProgramsNotes()
{
	if(document.getElementById('liveInformationContainer').style.display == 'none')
	{
		showHiddenContainer('liveInformationContainer');
		document.getElementById("webtvInfoContent").innerHTML = "";
	}
	else
	{
		hideHiddenContainer('liveInformationContainer');
	}


	showHiddenContainer("webtvInfoLoader");
	
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			var programs = JSON.parse(xmlhttp.responseText)
			hideHiddenContainer("webtvInfoLoader");
			if(programs[0].notes != null && programs[0].notes != "")
				document.getElementById("webtvInfoContent").innerHTML = programs[0].notes.replace(/\n/g, "<br/>");
			else
				document.getElementById("webtvInfoContent").innerHTML = "Nessuna informazione disponibile per il palinsesto corrente."
		}
	}
	
	var url = "http://www.fihp.org/webtv/webtvInfoRetrieve.php?date=" + new Date().getTime();
	
	xmlhttp.open("GET", url, true);
	xmlhttp.send();
}
								
