/**
* Artico 5
* @copyright 	Copyright (c) 2010, Marco Moser
* @author       Marco Moser <marco@marcomoser.it>
* @version 	7/1/10
*/

/*
* form onsubmit="return checkForm(this,{name: 'Nome',email :1,privacy: 'Dichiarazione Privacy'},'Check form')"
*/
function checkForm(myform,cklist,msg) {
    var ef='';
    var flist={};
    // verifica i valori<>null
    for(var ii=0;ii<myform.elements.length;ii++)
    {
        var ee=myform.elements[ii];
        //todo if checkbox (cklist[ee.name]==2 && !ee.checked)
        if (cklist[ee.name])
        {
            flist[ee.name]=1;
            if (!ee.value) ef+='- '+cklist[ee.name]+'\n';
        }
    }
    // verifica l'esistenza
    for(var ckname in cklist)
    {
    	if (!flist[ckname]) ef+='- '+ckname+'\n';
    }
    if (ef)
    {
    	if (!msg) msg='Verifica le seguenti voci:\n'; else msg+='\n';
        alert(msg+ef);
        return false;
    }
    if (ii<=0) return false;
    return true;
}

// toggle on/off
function onoffId(anId,tog) {
    var el=document.getElementById(anId);
    if (tog=='on') el.style.display='';
    else if (tog=='off') el.style.display='none';
    else el.style.display=el.style.display?'':'none'; // toggle
    return void(0);
}

function wopen(url) {
    wopenUrl(url);
    return void(0);
}

function wopenHref2(anode,opt) {
    var href=anode.getAttribute('href');
    wopenUrl(href,opt);
    return false;
}
function wopenHref2Repos() {
    var toks=/#_ayy(\d+)/.exec(window.location.hash);
    if (toks)
    {
    	document.documentElement.scrollTop=toks[1];
    	window.setTimeout('document.documentElement.scrollTop='+toks[1],1000); // render lento
    }
}

// {width:1024,height:768,qsadd:'_as=a5dlg'} xor null
function wopenUrl(url,opt) {
    var width=700;
    var height=400;
    var target='_blank';
    if (opt)
    {
        if (opt.width) width=opt.width;
        if (opt.height) height=opt.height;
        if (opt.target) target=opt.target;
        if (opt.qsadd) 
        {
            if (url.indexOf('?')==-1) url+='?'+opt.qsadd;
            else url+='&'+opt.qsadd; //ocio no amp
        }
    }
    if (document.documentElement.scrollTop)
    {
    	var keeposname='_ayy'+document.documentElement.scrollTop;
    	window.location.hash='#'+keeposname;
    }
    window.open(url,target,'menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,height='+height+',width='+width);
    return void(0);
}

/**
* nodo.id -> il nodo A diventa .atabs_selected xor ''
* nodo.'c'+id -> il nodo TBODY diventa display xor none
* @param tabid [tab_R1] 1 based special value [tab_R]
* @example tab <a id="tab_R1" href="#" onclick="return nextTab(this.id)">Generale</a> attiva tbody <tbody id="ctab_R1">
*/
function nextTab(tabid)
{
    var mm;
    if (!(mm=tabid.match(/(.+)(\d+)/))) { mm=[tabid,tabid,-1]; } // apre tutto
    var prefix=mm[1];
    var idx=mm[2];
    for(var ii=1;ii<20;ii++)
    {
        var tab=document.getElementById(prefix+ii);
        if (!tab) break;
        var tabbed=document.getElementById('c'+prefix+ii);
        if (idx==-1 || ii==idx)
        {
            tab.className='atabs_selected';
            tabbed.style.display='';
        }
        else
        {
            tab.className='';
            tabbed.style.display='none';
        }
    }
    return false;
}

// area di notifica, implementazione di default, richiede un div id="a_popupnotice"
// override by template
function a_popupnotice_ifn(msg)
{
    if (!msg) return;
    alert(msg);
    /* var node=document.getElementById('a_popupnotice'); if (!node) return; // boh forse insert if ! node.innerHTML=msg; */
}
// stoppa la coda eventi js
function a_stopPropagation(event) 
{
    event=event||window.event;
    event.cancelBubble = true;
    if (event.stopPropagation) event.stopPropagation();
}


