﻿var reg_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

function cleanStringWithSeparator(string_to_clean,separator) {
    var tmp_ar = cleanStringWithSeparatorAr(string_to_clean,separator);
    var rv = "";
    for(var i = 0; i < tmp_ar.length; i++) {
        if(i > 0) rv += separator;
        rv += tmp_ar[i];
    }
    return rv;
}
function cleanStringWithSeparatorAr(string_to_clean,separator) {
    var tmp_ar = string_to_clean.split(separator);
    var rv = new Array();
    for(var i = 0; i < tmp_ar.length; i++) {
        if(tmp_ar[i].length) {
            rv.push(tmp_ar[i]);
        }
    }
    return rv;
}
function cleanStringWithSeparatorArCheckEmail(string_to_clean,separator) {
    var tmp_ar = string_to_clean.split(separator);
    var rv = new Array();
    for(var i = 0; i < tmp_ar.length; i++) {
        if(tmp_ar[i].length && reg_email.test(tmp_ar[i])) {
            rv.push(tmp_ar[i]);
        }
    }
    return rv;
}
function contactUs(subject) {
    tb_remove();
    if(!subject) subject = "";
    openInIframe("Contattaci","contactUs.aspx?subject="+subject,450,290);
}
function inviteFriends(subject) {
    tb_remove();
    if(!subject) subject = "";
    openInIframe("Dillo ad un amico","inviteFriends.aspx",460,320);
}
function mustRegister() {
    alert("Per inviare messaggi privati, devi prima registrarti.\n\nLa registrazione e' immediata e gratuita.");
    registerUserCommon(); 
}
function sendPM(user_to) {
    if(!userId) {
     mustRegister();
     return;
    }
    tb_remove();
    openInIframe("Messaggio privato", "sendPM.aspx?user="+user_to, 400, 320);
    return false;
}

function sendPersonal(user_to) {
    if(!userId) {
     mustRegister();
     return;
    }
    tb_remove();
    openInIframe("Messaggio privato", "sendPM.aspx?user="+user_to+"&type=personal", 400, 320);
    return false;
}
function sendNewPM(pm_id) {
    if(!userId) {
     mustRegister();
     return;
    }
    tb_remove();
    if(pm_id) openInIframe("Nuovo messaggio privato","sendPM.aspx?id="+pm_id);
    else openInIframe("Nuovo messaggi privato","sendPM.aspx",400,320);
}
function userInfo(user) {
    tb_remove();
    openInIframe("Informazioni sull'utente","user.aspx?user="+user,450,320);
    return false;
}
function showError(str) {
    alert(str);
}
function addZero(str) {
    str = String(str);
    if(str.length == 1) str = "0"+str;
    return str;
}
function showMenu(td_id) {
    var menu_div = document.getElementById(td_id.id+"_ext");
    menu_div.style.display = "";
    menu_div.style.visibility = "visible";
}
function hideMenu(td_id) {
    var menu_div = document.getElementById(td_id.id+"_ext");
    menu_div.style.display = "none";
    menu_div.style.visibility = "hidden";
}
function showLoginForm() {
    thickHtml("Accesso", "loginForm.aspx", 275, 115)
    return false;
}
function showPasswordRecovery() {
    thickHtml("Recupero della password", "passwordRecovery.aspx", 400, 65)
    return false;
}
function sendLogin() {
    var param = new Object();
    param.user = document.getElementById("id_login_user").value;
    param.password = document.getElementById("id_login_password").value;
    getAjaxResponse(null,"Include/logIn.aspx",param,function(response) {
        if(response.substr(0,1) != " ") {
            alert(response);
            refreshPage();
        } else {
            alert(response.substr(1));                             
        }
    });
}
function requestPassword() {
    var param = new Object();
    param.user = document.getElementById("id_login_user").value;
    getAjaxResponse(null,"Include/logIn.aspx",param,function(response) {
        alert(response);
        tb_remove();
    });
}
function sendLogout() {
    if(window.confirm("Vuoi davvero disconnetterti dal sito?\n\nIn questo modo non risulterai piu' un utente registrato.")) {
     var param = new Object();
     getAjaxResponse(null,"Include/logOut.aspx",param,function(response) {
         alert("Hai appena effettuato l'uscita. Da questo momento non risulti piu' un utente registrato.");
         location.href = "/";
     });
    }
}
function refreshPage() {
    if(location.href.indexOf("?") > -1) location.reload();
    else location.href = location.href.replace("#","")+"?refreshed="+Math.random();
}
function deleteUserCommon() {
    if(window.confirm("Vuoi davvero cancellare il tuo profilo? \n\nIn questo modo, tutti i tuoi dati verranno irreversibilmente persi.")) {
     if(window.confirm("Sei sicuro di voler davvero cancellare il tuo profilo?")) {
      if(!window.confirm("Vuoi interrompere il processo di cancellazione dati e recuperare il tuo profilo? Sei ancora in tempo!")) {
       if(window.confirm("Forse ti sembremo assillanti, ma vogliamo essere sicuri che questa operazione non sia accidentale.\n\nVuoi allora davvero canncellare il tuo profilo?\n\nQuesta e' l'ultima richiesta.")) {
        getAjaxResponse(null,"Include/deleteUserCommon.aspx",null,function(response) {
         alert("Il tuo profilo e' stato cancellato correttamente.\n\nCi mancherai :)");
         refreshPage();
        });
       }
      }
     }
    }
}
function modifyProfile() {
    openInIframe("Modifica profilo","modify.aspx");
    return false;
}
function registerUserCommon() {
    openInIframe("Registrazione","register.aspx");
    return false;
}
function showMiniRegistration() {
    thickHtml("Inserisci i tuoi dati", "miniRegisterForm.aspx", 275, 95)
    return false;
}
var miniRegCallback = null;
function sendShortRegistration() {
    var param = new Object();
    param.txt_short_userid = document.getElementById("id_reg_userid").value;
    param.txt_short_email = document.getElementById("id_reg_email").value;
    getAjaxResponse(null,"Include/shortRegistration.aspx",param,function(response) {
        if(response.substr(0,1) == " ") {
            alert(response.substr(1));
        } else {
            alert(response);
            miniRegCallback();
            tb_remove();            
            miniRegCallback = null;
        }
    });
}
function showMiniRegistration() {
    thickHtml("Inserisci i tuoi dati", "miniRegisterForm.aspx", 275, 95)
    return false;
}
function enterPressed(e) {
    var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
    if (keyCode == 13) return true;
    return false;
}
function sendContent(alert_send,content_url) {
    var email_addresses = cleanStringWithSeparatorArCheckEmail(document.getElementById("inputSendContent").value,",");
    
    if(email_addresses.length) {
        var email_addresses_string = "";
            
        for(var i = 0; i < email_addresses.length; i++) {
            alert_send += "\n- "+email_addresses[i];
            if(i > 0) email_addresses_string += ",";
            email_addresses_string += email_addresses[i];
            
        }
        
        if(window.confirm(alert_send)) {
            var param = new Object();
            param.content_url = content_url;            
            param.emails = email_addresses_string;
            getAjaxResponse(null,"Include/sendContent.aspx",param,function(response) {
               alert(response);
            })
        }
    }
}