﻿//<![CDATA[

bws = 0;
if(navigator.appName == "Netscape") bws = 1;   
else if(navigator.appName == "Opera") bws = 2;

function getText(el)
{
    txt = el.innerText || el.textContent;
    return txt;
}

function setText(el, txt)
{
    if(el.innerText != undefined) el.innerText = txt;
    else if(el.textContent != undefined) el.textContent = txt;
}

function scrollDivSms()
{
    timerDown = "";
    el=document.getElementById("divSms");
    
    if(getText(el)!= "")
    {
        clearTimeout(timerDown); 
        
        el.scrollLeft += 1;
        timerDown=setTimeout("scrollDivSms()",50);

        if(el.scrollLeft>=el.scrollWidth-el.offsetWidth) el.scrollLeft=0;
    }
}

function getDurataVideo(s)
{
    s +="";
    var v = (s.charCodeAt(s.length - 2) - 97) * 26 + (s.charCodeAt(s.length - 1) - 97);
    return ((parseInt(v/60))+"."+(v%60 < 10 ? "0" : "")+(parseInt(v%60))+"min");
}

function setVideoSet(ibt)
{
   if (ibt.src.indexOf("_over.gif")==-1)
   {
       var el, i, j;
        
       for (i = 1; i < 5; i++) 
       {
           el = document.getElementById("ibtVideo"+i);
           if(el == ibt) ibt.src = el.src.substr(0, el.src.length-4)+"_over.gif";
           else if((j = el.src.indexOf("_"))>-1) el.src = el.src.substr(0, j)+".gif";
       }
       
       CallServer("S:"+ibt.id.substr(ibt.id.length-1));
    }
}

function myGridView(containerId, html)
{
    var cel = document.getElementById(containerId), i = html.indexOf('§'), param, p, arw="";
        
    if(i > -1)
    {
        param = html.substr(0, i);
        cel.setAttribute("param", param);
        p = param.split(';');//0=filter;1=sort;2=start;3=pagesize;4=totrec

        if(p[4]==0) alert("Nessun elemento trovato");
        else
        {
            if (parseInt(p[2]) > 0) arw = "<img id='btFirst' src='img/ffirst.png' title='prima pagina' onclick='scrollGrid(\""+containerId+"\",this.id)' style='cursor:pointer' />&nbsp;<img id='btPrev' src='img/fprev.png' title='pagina precedente' onclick='scrollGrid(\""+containerId+"\",this.id)' style='cursor:pointer'/>";
            if (parseInt(p[2]) + parseInt(p[3]) < parseInt(p[4])) arw += "&nbsp;<img id='btNext' src='img/fnext.png' title='pagina seguente' onclick='scrollGrid(\""+containerId+"\",this.id)' style='cursor:pointer'/>&nbsp;<img id='btLast' src='img/flast.png' title='ultima pagina' onclick='scrollGrid(\""+containerId+"\",this.id)' style='cursor:pointer'/>";
            if(arw != "") arw = "<table border='0' cellpadding='0px' cellspacing='0px' style='width:100%;height:12px;background-image:"+cel.style.backgroundImage+"'><tr><td colspan='3' align='right'>" + arw + "&nbsp;&nbsp;</td></tr></table>";
        }
    }
    cel.innerHTML = html.substr(i+1).replace(/§/g, "'")+arw;
}

function scrollGrid(containerId, arrowId)
{
    var cel = document.getElementById(containerId), dir = "", 
        p = cel.getAttribute("param").split(';');//0=filter;1=sort;2=start;3=pagesize;4=totrec

    if(arrowId == "btFirst") p[2] = 0;
    else if(arrowId == "btLast") p[2] = parseInt((p[4]-1)/p[3])*p[3];
    else if(arrowId == "btPrev") p[2] = parseInt(p[2]) - parseInt(p[3]);
    else if(arrowId == "btNext") p[2] = parseInt(p[2]) + parseInt(p[3]);
    
    CallServer("G:"+containerId+";"+p[0]+";"+p[1]+";"+p[2]);
}

function ChooseVideo(id, svideo, autore, titolo, descr, tag, voto, ncomm)
{
    var s = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0' width='320' height='280' id='mikflv' align='middle'>";
	s += "<param name='allowScriptAccess' value='sameDomain' />";
	s += "<param name='allowFullScreen' value='false' />";
	s += "<param name='movie' value='mikflv.swf' />";
	s += "<param name='quality' value='high' />";
	s += "<param name='bgcolor' value='#ffffff' />";
	s += "<param name='FlashVars' value='v="+svideo+"' />";
	s += "<embed src='mikflv.swf' FlashVars='v="+svideo+"' quality='high' bgcolor='#ffffff' width='320' height='280' name='mikflv' align='middle' allowScriptAccess='sameDomain' allowFullScreen='false' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />";
	s += "</object>";
    
    setText(document.getElementById("lblAutore1"), autore);
    setText(document.getElementById("lblTitolo1"), titolo);
    setText(document.getElementById("lblDescr1"), descr);
    setText(document.getElementById("lblDurata1"), getDurataVideo(svideo+""));
    setText(document.getElementById("lblTag"), tag);
    document.getElementById("txUrl").value = "http://www.youschool.it?v=" + svideo;
    document.getElementById("txCode").value = "<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' width='320' height='280' id='mikflv' align='middle'><param name='movie' value='http://www.youschool.it/mikflv.swf'/><param name='FlashVars' value='v=" + svideo + "'/><embed src='http://www.youschool.it/mikflv.swf' FlashVars='v=" + svideo + "' width='320' height='280' name='mikflv' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'/></object>";
    document.getElementById("imgInviaUrl").href = "mailto:?subject=youschool.it%20-%20Guarda%20questo%20video!&body=www.youschool.it?v=" + svideo;
    document.getElementById("imgAbuso").href = "mailto:ys_admin@youschool.it?subject=youschool.it:%20segnalazione%20abuso,%20video:%20"+svideo+"&body=[inserire le ragioni della segnalazione]";
    document.getElementById("tdComm0").setAttribute("idVideo", id);
    setText(document.getElementById("spComm0"), ncomm > -1 ? "["+ncomm+"]commenti" : getText(document.getElementById("spComm"+id)));
    document.getElementById("imgVoto0").src = voto > 0 ? "img/stella_"+voto+".png" : "img/stella_0.png";
    
    document.getElementById("clip_frm").innerHTML = s;
    CallServer("v:"+id);
}

function floatDiv(id, css, html)
{
    var dv = null;
	if((dv = document.getElementById(id)) == null)
	{
	    dv = document.body.ownerDocument.createElement("div");
	    dv.id = id;
	}
    dv.style.cssText = css;
    dv.innerHTML = html;
	document.body.appendChild(dv);
	return dv;
}

function recPwd()
{
    floatDiv("divPwd", 
        "left:"+parseInt(window.screen.width/2)+"px;top:"+parseInt(window.screen.height/2)+"px;height:88px;"+
	    "visibility:visible;width:422px;position:absolute;background-color:#96b5e1;border:ridge aqua 3px;font-family:Verdana;font-size:10px;color:#ffffff",
	    "<table><tr><td rowspan='4'><img src='img/logo.gif' alt='Recupera la password di YouSchool' width='141' height='80' /></td><td align='right' valign='top' class='Stile24'><span style='cursor:pointer;color:#ff0000' onclick='document.getElementById(\"divPwd\").style.visibility=\"hidden\"'>|x|</span></td></tr><tr><td class='stile29'>Inserisci l\'email indicata in fase di registrazione:</td></tr><tr><td><input type='text' id='txEmail0' style='width:267px' /></td></tr><tr><td align='center'><input type='button' id='btOk' value='ok' style='width:62px' onclick='this.style.visibility=\"hidden\";CallServer(\"R:\"+document.getElementById(\"txEmail0\").value)' /></td></tr></table><BGSOUND SRC='suoni/apri.wav' AUTOSTART='TRUE'>");
     document.getElementById("txEmail0").focus();
}

function showNews(ctrl, e, titolo, testo)
{
    floatDiv("divNews", 
        (bws==0 ? "left:"+parseInt(e.x+ctrl.offsetLeft)+"px;top:"+parseInt(e.y+ctrl.offsetTop)+"px;height:210px;" : 
                  "left:"+e.pageX+"px;top:"+e.pageY+"px;height:210px;")+
	    "visibility:visible;width:376px;position:absolute;background-color:#006fba;border:ridge aqua 3px;font-family:Verdana;font-size:10px;color:#ffffff",
	    "<table width='100%'><tr><td><b>"+titolo+"</b></td><td style='text-align:right;vertical-align:top;cursor:pointer'><span onclick='document.getElementById(\"divNews\").style.visibility=\"hidden\"'>|x|</span></td></tr><tr><td colspan='2'>"+testo+"</td></tr></table><BGSOUND SRC='suoni/apri.wav' AUTOSTART='TRUE'>");
}

function putComment()
{
    var ctrl = document.getElementById("imgInsCommento"), w=464, h=82;

    floatDiv("divComm", 
	    "left:"+(ctrl.offsetLeft-w)+"px;"+
	    "top:"+(ctrl.offsetTop-h)+"px;"+
	    "visibility:visible;width:464px;height:82px;position:absolute;background-color:#006fba;border:ridge aqua 3px;font-family:Verdana;font-size:10px",
	    "<table width='100%' height='100%' border='0'><tr style='color:#ffffff'><td><span><b>Inserisci un commento<b> <i>(max 160car.)</i></span>[<a href='policy.html' title='Consulta la Policy di YouSchool.it' target='_blank'>?</a>]</td>"+
        "<td align='right'><span style='cursor:pointer' onclick='document.getElementById(\"divComm\").style.visibility=\"hidden\"'>|x|</span></td></tr>"+
        "<tr><td valign='bottom' rowspan='2'><textarea id='txTesto' cols='60' rows='3' onblur='if(this.value.length>160) this.value=this.value.substr(0,160)' style='width:400px;height:48px;overflow:hidden;background-color:#ffff99;color:#000000''></textarea></td><td></td></tr>"+
        "<tr><td valign='bottom'><input type='button' id='btOk' value='ok' style='width:48px;height:30px' onclick='this.style.visibility=\"hidden\";CallServer(\"C:\"+document.getElementById(\"txTesto\").value+\"§\"+document.getElementById(\"tdComm0\").getAttribute(\"idVideo\"))' /></td></tr></table><BGSOUND SRC='suoni/apri.wav' AUTOSTART='TRUE'>");
	document.getElementById('txTesto').focus();
}

function showHello(txt)
{
    var el = document.getElementById("ibtLogin"), s = txt.split("§");
    
    floatDiv("divHello", "left:"+parseInt(el.offsetLeft-144)+"px;top:"+parseInt(el.offsetTop-36)+"px;width:"+parseInt(7*(s[1].length))+"px;"+
        "visibility:visible;height:36px;position:absolute;background-color:#006fba;border:ridge aqua 3px;font-family:Verdana;font-size:10px;color:#ffffff",
        "<table width='100%' height='100%' border='0'><tr><td>Ciao, "+s[0]+"</td><td style='text-align:right;vertical-align:top;cursor:pointer'><a href='mailto:ys_admin@youschool.it?subject=[Youschool.it]%20Nuova%20frase%20di%20benvenuto%20da:%20"+s[0]+"&body=Scrivi%20la%20tua%20frase:%20' title='Invia una frase che dar&aacute; il benvenuto, a te e agli altri utenti!'><img src='img/bt_newhello.png' style='border-style:none;vertical-align:bottom'></a>&nbsp;<span onclick='document.getElementById(\"divHello\").style.visibility=\"hidden\"'>|x|</span></td></tr><tr><td colspan='2' style='color:Yellow;white-space:pre'>"+s[1]+"</td></tr></table><BGSOUND SRC='suoni/hello.wav' AUTOSTART='TRUE'>");
}

function setUserInfo(s)
{
    var v = s.split('§');
    var el0 = document.getElementById("ibtLogin"), el1 = document.getElementById("imgUpload"), 
        el2 = document.getElementById("imgInsCommento"), el3 = document.getElementById("spPref"), el4 = document.getElementById("ibtVideo4"); 

    setText(document.getElementById("lblUser"), v[3]);
    el0.src = v[0] == "1" ? "img/bt_admin.jpg" : "img/bt_accedi.jpg";
    el0.title = "Utente riconosciuto: " + v[1] + " " + v[2];
    el0.onclick = v[0] == "1" ? Function("window.open('ys_admin.aspx')") : Function("CallServer('H:')");
    document.getElementById("imgRegistra").title = "Clicca per gestire le informazioni del tuo account e/o effettuare il Log Out.";
    el1.src = "img/carica_video.gif";
    el1.title = "clicca per caricare un tuo video su youschool.it";
    el1.onclick = Function("upVideo('" + v[0] + "')");
    el2.src = "img/scrivi_commento.gif";
    el2.title = "clicca per inserire un commento";
    el2.onclick = Function("putComment()");
    document.getElementById("imgPref").src = "img/preferiti_logo.gif";
    el3.onclick = Function("AddPrefs()");
    el3.title = "Aggiungi ai tuoi video preferiti";
    el4.title = "Video preferiti da "+v[3];
    el4.onclick = Function("setVideoSet(this)");
    el1.style.cursor = el2.style.cursor = el3.style.cursor = el4.style.cursor = "pointer";
    document.getElementById("txUid").value = document.getElementById("txPwd").value = "";
    
    if(v[5]=="H") showHello(v[3]+"§"+v[4]);    
}

function addNumCommento(id)
{
    var el = document.getElementById(id), txt = getText(el), n = txt.substring(1, txt.indexOf(']'));
    setText(el, "["+(parseInt(n)+1)+"]commenti");
}

function visDatiUtente(s)
{
    var v = s.split("§");
    document.getElementById("txNome").value = v[0]; document.getElementById("txCognome").value = v[1];
    document.getElementById("selTipo").selectedIndex = v[2]; document.getElementById("txScuola").value = v[3];
    document.getElementById("txCitta").value = v[4]; document.getElementById("txProv").value = v[5];
    document.getElementById("txEmail").value = v[6]; document.getElementById("txUser").value = v[7];
    document.getElementById("btOk").value = "Modifica"; document.getElementById("btEntra").value = v[0]=="" ? "Entra" : "Esci";
    document.getElementById("txUid").value = v[7]; document.getElementById("txPwd").value = document.getElementById("txPassword").value = ""; 
}
 
function ReceiveServerData(rValue)
{   
    var s = rValue.substr(0, 2);// Token Usati: V,P,L,C,S,N,H,U,E,R,D,M,m
    if(s == "V:")
    {   //vota video
        document.getElementById("divVoto").style.visibility = "hidden";
        alert("Grazie per aver votato questo video");
    }
    else if(s == "P:") alert("Video aggiunto ai Preferiti"); //aggiungi video preferiti
    else if(s == "L:") 
    {   //Login
        if(rValue.indexOf('§')==-1)
        {
            setText(document.getElementById("lblUser"), "Visitatore");
            document.getElementById("ibtLogin").src = "img/bt_login.gif";
            alert(rValue.substr(2)); 
        }
        else setUserInfo(rValue.substr(2)+"§H");
    }
    else if(s == "C:") 
    {   //lascia commento
        if(document.getElementById("divComm") != null) document.getElementById("divComm").style.visibility = "hidden";
        if(document.getElementById("spComm"+rValue.substr(2)) != null) addNumCommento("spComm"+rValue.substr(2));
        if(document.getElementById("tdComm0").getAttribute("idVideo") == rValue.substr(2)) addNumCommento("spComm0");
        alert("Grazie per aver lasciato un commento.");
    }
    else if(s == "S:") myGridView("tdVideo", rValue.substr(2)); //show grid video
    else if(s == "N:") myGridView("tdNews", rValue.substr(2)); //show grid news
    else if(s == "H:") showHello(rValue.substr(2)); //show hello msg
    else if(s == "U:") alert(rValue.substr(2)); //registrazione utente
    else if(s == "E:") //Entra utente
    {
        if(rValue.indexOf('§') > -1) visDatiUtente(rValue.substr(2));
        else alert(rValue.substr(2));
    }
    else if(s == "R:") //recupero password
    {
        if(document.getElementById("divPwd") != null) document.getElementById("divPwd").style.visibility = "hidden";
        alert(rValue.substr(2));
    }
    else if(s == "D:") //cancellazione utente
    {
        if(rValue.length==2) document.getElementById("btCanc").value = "Sei SICURO ?";
        else if(rValue.charAt(2)=='§') { visDatiUtente("§§0§§§§§");  document.getElementById("btCanc").value = "Cancellati"; alert(rValue.substr(3))}
        else alert(rValue.substr(2)); 
    }
    else if(s == "M:") 
    {   //nuovo argomento/messaggio Forum
        if(document.getElementById("divMsg") != null) document.getElementById("divMsg").style.visibility = "hidden";
        alert("Grazie per aver inserito un "+(rValue.substr(2) =="0" ? "argomento" : "messaggio"));
        CallServer("G:tdMsg;"+rValue.substr(2)+";;0");
    }
    else if(s == "m:") myGridView("tdMsg", rValue.substr(2)); //show grid espandi argomento Forum
}

function doLogin()
{
    var el1 = document.getElementById("txUid"), el2 = document.getElementById("txPwd");
    if(el1.getAttribute("val") != el1.value && el2.getAttribute("val") != el2.value)
    {
        el1.setAttribute("val", el1.value);el2.setAttribute("val", el2.value);
        CallServer("L:"+el1.value+"§"+el2.value)
    }
}

function doVoto(n)
{
    CallServer("V:"+n+"§"+document.getElementById("txUrl").value.split('=', 2)[1], "");
}

function votaVideo(ctrl, e)
{
    var s = "onclick='js:doVoto(this.title)'";
    floatDiv("divVoto", 
	    "left:"+(bws==0 ? e.x+ctrl.offsetLeft : e.pageX)+"px;"+
	    "top:"+(bws==0 ? e.y+ctrl.offsetTop : e.pageY)+"px;"+
	    "visibility:visible;width:124px;height:150px;position:absolute;background-color:#ffffff;border:solid blue 1px;",
	    "<table><tr><td style='cursor:pointer'><img src='img/stella_1.gif' title='1' "+s+" border='0' onmouseover='js:this.src=\"img/stella_1_1.gif\"' border='0' onmouseout='js:this.src=\"img/stella_1.gif\"'></td></tr><tr><td style='cursor:pointer'><img src='img/stella_2.gif' title='2' "+s+" border='0' onmouseover='js:this.src=\"img/stella_2_1.gif\"' border='0' onmouseout='js:this.src=\"img/stella_2.gif\"'></td></tr><tr><td style='cursor:pointer'><img src='img/stella_3.gif' title='3' "+s+" border='0' onmouseover='js:this.src=\"img/stella_3_1.gif\"' border='0' onmouseout='js:this.src=\"img/stella_3.gif\"'></td></tr><tr><td style='cursor:pointer'><img src='img/stella_4.gif' title='4' "+s+" border='0' onmouseover='js:this.src=\"img/stella_4_1.gif\"' border='0' onmouseout='js:this.src=\"img/stella_4.gif\"'></td></tr><tr><td style='cursor:pointer'><img src='img/stella_5.gif' title='5' "+s+" border='0' onmouseover='js:this.src=\"img/stella_5_1.gif\"' border='0' onmouseout='js:this.src=\"img/stella_5.gif\"'></td></tr><table>");
}

function upVideo()
{
    window.open('ys_dlgUpvideo.aspx','upVideo','height=450px,width=606px,left=80px,top=100px,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no');
}

function ReadComment(id)
{
    var d = new Date();
    open('ys_dlgReadComment.aspx?v='+id+'&d='+d.getMilliseconds(),'_blank','height=452px,width=448px,left=100px,top=100px,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no');
}

function AddPrefs()
{
    CallServer("P:"+document.getElementById("tdComm0").getAttribute("idVideo"), "");
}

function cercaVideo()
{
    var s = document.getElementById('txCerca').value;
    if( s != "")
    {
        var  el, j, i;
        for (i = 1; i < 5; i++) 
        {
           el = document.getElementById("ibtVideo"+i);
           if((j = el.src.indexOf("_"))>-1) el.src = el.src.substr(0, j)+".gif";
        }
        CallServer("S:5;"+s);
    }
}

function putMsg(ctrl, type)
{
    var w=464, h=142;
    
    floatDiv("divMsg", 
	    "left:"+ctrl.offsetLeft+"px;"+
	    "top:"+(ctrl.offsetTop-h)+"px;"+
	    "visibility:visible;width:"+w+"px;height:"+h+"px;position:absolute;background-color:#006fba;border:ridge aqua 3px;font-family:Verdana;font-size:10px",
	    "<table width='100%' height='100%' border='0'><tr style='color:#ffffff'><td><span><b>Inserisci un "+(type==0 ? "argomento" : "messaggio")+"<b></span>&nbsp;&nbsp;[<a href='policy.html' title='Consulta la Policy di YouSchool.it' target='_blank' style='color:yellow'>?</a>]</td>"+
        "<td align='right'><span style='cursor:pointer' onclick='document.getElementById(\"divMsg\").style.visibility=\"hidden\"'>|x|</span></td></tr>"+
        "<tr><td valign='bottom' rowspan='2'><textarea id='txTesto' cols='60' rows='8' style='width:400px;height:108px;overflow:hidden;background-color:#ffff99;color:#000000''></textarea></td><td></td></tr>"+
        "<tr><td valign='bottom'><input type='button' id='btOk' value='ok' style='width:48px;height:30px' onclick='this.style.visibility=\"hidden\";CallServer(\"M:"+type+"§\"+document.getElementById(\"txTesto\").value)' /></td></tr></table><BGSOUND SRC='suoni/apri.wav' AUTOSTART='TRUE'>");
	document.getElementById('txTesto').focus();
}
// ]]>
