var win=null;

function OpenNewWindow(mypage,myname,w,h,scroll,pos)
{
    if(pos=="random")
    {
        LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
        TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
    }
    if(pos=="center")
    {
        LeftPosition=(screen.width)?(screen.width-w)/2:100;
        TopPosition=(screen.height)?(screen.height-h)/2:100;
    }
    else if((pos!="center" && pos!="random") || pos==null)
    {
        LeftPosition=0;
        TopPosition=20
    }

    settings='width='+w+', height='+h+', top='+TopPosition+', left='+LeftPosition+', scrollbars='+scroll+', location=no, directories=no, status=no, menubar=no, toolbar=no, resizable=no';
    win=window.open(mypage,myname,settings);

    if(win.focus)
    {
        win.focus();
    }
}

function CloseNewWindow()
{
    if(win!=null && win.open)
        win.close()
}

function DeleteRecord(url){
    if(strtodisplay==""){
        strtodisplay = "E' sicuro di voler cancellare il record specificato?";
    }
    if(confirm(strtodisplay)){
        location = url;
    }
    return true;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function GotoRelation(valore, url){
    if(valore != ""){
        location.href = url+valore ;
    }else{
        alert("Nessun elemento correlato");
    }

}

function CheckAll()
{
    for (var i=0;i<document.forms.APP_INDEX_FORM.elements.length;i++)
    {
        var e = document.APP_INDEX_FORM.elements[i];
        if ((e.name != 'allbox') && (e.type=='checkbox'))
        e.checked = document.APP_INDEX_FORM.allbox.checked;
    }
}

function butRemove_onclick(deststr, sorgstr)
{
    var dest = document.forms["APPLICATION_FORM"].elements[deststr];
    var sorg = document.forms["APPLICATION_FORM"].elements[sorgstr];
    selection= new Array;

    for (index = 0 ; index < dest.length ; index++)
    {
         //alert("ciclo "+index);
         if(dest.options[index].selected==true)
         {
             //alert ('trovato la selezione a '+index+' sorg ? lungo '+sorg.length);
             selection[selection.length]=dest.options[index].value;
         }
    }

    for (fooIndex = 0; fooIndex < selection.length; fooIndex++)
    {
        for (fooIndex2 = 0; fooIndex2 < dest.length; fooIndex2++)
        {
            if (dest.options[fooIndex2].value == selection[fooIndex])
            {
                 //alert(selection[fooIndex]);
                dest.options[fooIndex2]=null;
            }
        }
    }

    current_predef = -1;
    for (index = 0 ; index < dest.length ; index++)
    {
        //alert("ciclo "+index);
        if(dest.options[index].selected==true)
        {
            selection[selection.length]=index;
        }
        a = dest.options[index].value.split("|");
        if(a[1]=="p"){
            current_predef = index;
            //alert(current_predef)
        }
    }

    if(dest.length==1 || current_predef==-1 && dest.options[0]){
        dest.options[0].selected = true;
        dest.options[0].selected = false;
    }
}
var current_predef;
function butAdd_onclick(deststr, sorgstr, alert_message)
{

    var dest = document.forms["APPLICATION_FORM"].elements[deststr];
    var sorg = document.forms["APPLICATION_FORM"].elements[sorgstr];

    selection= new Array;

    old_dest_len = dest.length;

    for (index = 0 ; index < sorg.length ; index++)
    {
        //alert("ciclo "+index);
        if(sorg.options[index].selected==true)
        {
            //alert ('trovato la selezione a '+index+' sorg ? lungo '+sorg.length);
            selection[selection.length]=index;
            //alert ('selezione ? lungo '+selection.length);
         }
    }
    //alert (selection.length+" ? la misura dei selezionati");
    var aaa = new Array()
    for (fooIndex = 0; fooIndex < selection.length; fooIndex++)
    {
        //  These are the elements to be added
        mytext  = sorg.options[selection[fooIndex]].text;
        myvalue = sorg.options[selection[fooIndex]].value;
        //alert (myvalue);
        //  First of all we check if the option selected is already present in the destination
        found=false;

        if (dest.length>0)
        {
            for (indexCounter = 0; indexCounter < dest.length; indexCounter++)
            {
                //alert (indexCounter+" dest="+dest.length);
                aaa = dest.options[indexCounter].value.split("|");
                //alert("a0 vale; " + aaa[0]);
                if (myvalue == aaa[0])
                {
                    found=true;
                    break;
                    //alert ('add: trovato');
                }
                for(j=0; j< aaa.length; j++)
                {
                    aaa.shift()
                }

                if (dest.options[indexCounter].value == myvalue)
                {
                    found=true;
                    break;
                    //alert ('add: trovato');
                }
            }
        }

        if (found == false)
        {
            inserted=false;
            //alert (dest.length+" ? la misura");
            if (dest.length>=0)
            {
                for (indexCounter = 0;indexCounter <= dest.length -1; indexCounter++)
                {
                    if (dest.options[indexCounter].text.toLowerCase() > mytext.toLowerCase())
                    {
                        var lastoption = new Option();
                         dest.options[dest.length] = lastoption;
                        break;
                        //alert ('trovato');
                    }
                }
                for (indexCounter2 = dest.length -1 ; indexCounter2 > indexCounter ; indexCounter2--)
                {
                    //alert("indexCounter= "+indexCounter+" indexcounter2= " + indexCounter2);
                    dest.options[indexCounter2].text = dest.options[indexCounter2 - 1].text;
                    dest.options[indexCounter2].value = dest.options[indexCounter2 - 1].value;
                }
                var option = new Option(mytext,myvalue);
                dest.options[indexCounter] = option;
                //dest.options[indexCounter].selected = true;
            }

        }
        else
        {
            //alert('The element specified is already present!');
            if ("" != alert_message)
                alert(mytext + " " + " e' gia' presente!");
        }
    }
    if(old_dest_len==0){
        dest.options[0].selected = true;
        dest.options[0].selected = false;
    }
}