function getObj(name){
  if (document.getElementById){
  	return document.getElementById(name);
  } else if (document.all) { 
    return document.all[name];
  }
  else if (document.layers){
   	return document.layers[name];
  }
}

function toggle_loginbox(name){
  getObj('loginbox_user').style.display='none';
  getObj('loginbox_company').style.display='none';
  getObj('loginbox_reseller').style.display='none';
  getObj(name).style.display='inline';
}

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height){
    window.open(URLStr, '', 'left='+left+',top='+top+',screenX='+left+',screenY='+top+',width='+width+',height='+height+',scrollbars');
}

function ChiamaNumero(){
    popUpWindow("call.php?numero="+document.chiamaNumero.numero.value,150,150,300,200);
}

function notValid(parm,val) {
  if (parm == "") return false;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) != -1) return true;
  }
  return false;
}

function setCookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime(today.getTime() );
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires) );
	document.cookie = name + '=' + escape(value) +
		( (expires) ? ';expires='+expires_date.toGMTString() : '') +
		( (path) ? ';path=' + path : '') +
		( (domain) ? ';domain=' + domain : '') +
		( (secure) ? ';secure' : '');
}

function getParentWinObj(name){
    if (opener.document.getElementById){
        return opener.document.getElementById(name);
    }else if (opener.document.all){
        return opener.document.all[name];
    } else if (opener.document.layers) {
        return opener.document.layers[name];
    }
}

// funzioni custom per le movebox 
function aggiungi(elemento){
	obj = $(elemento);
  obj.show();

  boxname = obj.id.replace('movebox_','');

  new Ajax.Request("savemovebox.php", {
                method:'get',
                parameters: boxname+'=show'
            }
        );

	rendisortable();
}

function rimuovi(elemento){
	obj = $(elemento);
  obj.hide();

  boxname = obj.id.replace('movebox_','');

  new Ajax.Request("savemovebox.php", {
                method:'get',
                parameters: boxname+'=none'
            }
        );


	rendisortable();
}

// funzioni custom per le movebox 
function ordina(newsequence){
	rendisortable();

  if (newsequence === undefined) {
    obj = $('summary_boxes').show();
    // al primo accesso salva subito le preferenze cosė da avere un array completo di tutti i box nel db
    new Ajax.Request("savemovebox.php", {
                method:'get',
                parameters:Sortable.serialize('summary_boxes')
            }
        );

    return;
  } else {
    Sortable.setSequence('summary_boxes',newsequence);
    obj = $('summary_boxes').show();
  }

}

function rendisortable(){
// <![CDATA[
   Sortable.create("summary_boxes", {
     dropOnEmpty:true,
     handle:'handle',
     containment:["summary_boxes"],
     constraint:false,
     onUpdate:function(){
        var myAjax = new Ajax.Request("savemovebox.php", {
                method:'get',
                parameters:Sortable.serialize('summary_boxes')
            }
        )
     }
   });
}


// varie, scorporate da html
function MM_callJS(jsStr) {
    return eval(jsStr)
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function MM_openBrWindow(theURL,winName,features) {
		theURL += "?mese=" + document.date.mese.options[document.date.mese.selectedIndex].value;
		theURL += "&anno=" + document.date.anno.options[document.date.anno.selectedIndex].value;
		features += ", scrollbars=yes, resizable=yes, hotkeys=no, menubar=no, personalbar=no, screenX=50, screenY=50";
	  window.open(theURL,winName,features);
	  return false;
}


function showTab(idTab){
    for (i=1;i<=20;i++){
        block = getObj('block_'+i);
        tab = getObj('tab_'+i);

        if (block !== null)
            block.style.display='none';

        if (tab !== null)
            tab.className='';
    }

    //alert(idTab);
    getObj('block_'+idTab).style.display='block';
    getObj('tab_'+idTab).className='selected';
}


// non va per ora, probabilmente non esiste ancora il dom quando lo chiama
function initTab(url) {
        var handled = false;

        for (i=1;i<=20;i++){
            if ((url.indexOf('tab_'+i)!=-1) && (getObj('block_'+i) !== null)){
                handled = true;
                showTab(i);
            }
        }

        if (handled === false) showTab('1');
    }


