//  ` Select.js
//  ` Created by: Sandro Bonin
//  ` Date Created: 15/03/2009 at 10:39:40
//  ` ChangeList: 1347 - Gestione Video
//  ` Modificato 15/01/2012 alle 18:15:38 by: Sandro Bonin Bug:  1440-Modifiche varie 

var xmlHttp
function showUser(Function,Params,Element,Session,InFocus,Params2){ //funzione da passare a 4d, parametri per 4d, elemento dell'html da riempire, sessione, eventuale id oggetto da mettere in focus
  xmlHttp=GetXmlHttpObject()
  if (xmlHttp==null) {
    alert ("Browser does not support HTTP Request")
    return
  }
  var Params1=Params.substring(0,31500)
 if (null==Params2){
 var Params2=""  
 }
//  alert("caso x   ")
//  if(Params.length<63000){
//	var Params2="caso 1   "+Params.substring(31501,31510)
//  } else {
//  	var Params2="caso 2   "+Params.substring(31501,43000)
//  }
//var Params2="123"
  var url="/4DACTION/Web_ajax"
  url=url+"?Element="+Element+"&Params="+Params1+"&Session="+Session+"&Fun="+Function+"&Num="+Math.random()+"&Params2="+Params2
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
  xmlHttp.onreadystatechange= function() {
    stateChanged(Element,InFocus);
  }
}

function stateChanged(Element,InFocus) { 
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
    document.getElementById(Element).innerHTML=xmlHttp.responseText;
    var InFocus = InFocus||null;//verifico che il parametro 2 ci sia
    if (null!==InFocus) {// ho passato anche il parametro focus
      if (InFocus!=''){
          // cerco l'elemento e lo metto in focus
         document.getElementById(InFocus).focus()
      }
    }
	if(document.getElementById('activityIndicator')!=null){
	 document.getElementById('activityIndicator').style.visibility = 'hidden';
	} 
	if(document.getElementById('activityIndicatorB')!=null){
	 document.getElementById('activityIndicatorB').style.display = 'none';
	} 
  } 
}

function GetXmlHttpObject() {
  var xmlHttp=null;
  try{
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    //Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function LoadDivS(id,dim){
  document.getElementById(id).style.visibility = 'visible'
   if (dim=='auto'){
  document.getElementById(id).style.height=dim	
 }
  else {
   document.getElementById(id).style.height=dim+"px"
  }
}

function UnLoadDivS(id,dim){
  document.getElementById(id).style.visibility = 'hidden'
  document.getElementById(id).style.height=dim+"px"	
}
	
function isEmpty(e) {
  if ((e.value == null) || (e.value == "")) {
    return true;
  }
  //controlla se è blank
  s = e.value;
  for(var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  return true;
}

function isCorrect(e,minLen,maxLen) {
  if ((e.value == null) || (e.value == "")) {
    return false;
  }
  //controlla se è blank
  s = e.value;
  if (s.length < minLen || s.length > maxLen) {
    return false;
  }
  else return true;
}

function dataattuale() {
  var data = new Date()
  mm = data.getMonth() + 1;
  return data.getDate()+"/"+mm+"/"+data.getFullYear()
}	

// MyWindow
var dragapproved=false
var minrestore=0
var initialwidth,initialheight
var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all

function iecompattest(){
  return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function drag_drop(e){
  var IdName=temp1
  if (ie5&&dragapproved&&event.button==1){
    //	alert("e"+e);
    document.getElementById("mywindow_"+IdName).style.left=tempx+event.clientX-offsetx+"px"
    document.getElementById("mywindow_"+IdName).style.top=tempy+event.clientY-offsety+"px"
  }
  else if (ns6&&dragapproved){
    document.getElementById("mywindow_"+IdName).style.left=tempx+e.clientX-offsetx+"px"
    document.getElementById("mywindow_"+IdName).style.top=tempy+e.clientY-offsety+"px"
  }
}

function initializedrag(e,IdName){
  temp1=IdName
  offsetx=ie5? event.clientX : e.clientX
  offsety=ie5? event.clientY : e.clientY
  document.getElementById("mywindowcontent_"+IdName).style.display="none" //extra
  tempx=parseInt(document.getElementById("mywindow_"+IdName).style.left)
  tempy=parseInt(document.getElementById("mywindow_"+IdName).style.top)

  dragapproved=true
  document.getElementById("mywindow_"+IdName).onmousemove=drag_drop
}

function loamywindow(IdName,titolo,URL,width,height,forframe,Params,Session,Centrale,InFocus ){
  if (!ie5&&!ns6)
    window.open(url,"","width=width,height=height,scrollbars=1")
  else{
    //alert(document.getElementById("MyWindowMain_"+IdName).className); window.screen.height; var larghezza = window.screen.width;
    document.getElementById("mywindowtitle_"+IdName).innerHTML=titolo
    document.getElementById("mywindow_"+IdName).style.display=''
    document.getElementById("mywindow_"+IdName).style.width=initialwidth=width+"px"
    document.getElementById("mywindow_"+IdName).style.height=initialheight=height+"px"
    document.getElementById("mywindow_"+IdName).style.left="30px"
    document.getElementById("mywindow_"+IdName).style.top=ns6? window.pageYOffset*1+30+"px" : iecompattest().scrollTop*1+30+"px"

    if (Centrale=='1'){// la finestra va aperta centralmente
      document.getElementById("mywindow_bg_"+IdName).style.display='block' // eccendo centrale è un avviso e oscuro il resto della pagina
      document.getElementById("mywindow_"+IdName).style.left=(window.screen.width/2)-width/2+"px"
      document.getElementById("mywindow_"+IdName).style.top=ns6? window.pageYOffset*1+(((window.screen.height/2)-height/2)-150)+"px" : iecompattest().scrollTop*1+(((window.screen.height/2)-height/2)-150)+"px"
    }
    if (forframe==0){
      showUser('MyWindow',Params,'mywindowframe_'+IdName,Session,InFocus)
    }else{
      document.getElementById("mywindowframe_"+IdName).innerHTML="<iframe id='mywframe' src='"+URL+"' width='100%' height='100%'></iframe>"
    }
  }
}

function maximize(IdName){
  if (minrestore==0){
    minrestore=1 //maximize window
    document.getElementById("maxname_"+IdName).setAttribute("src","/images/MyWRestore.png")
    document.getElementById("mywindow_"+IdName).style.width=ns6? window.innerWidth-20+"px" : iecompattest().clientWidth+"px"
    document.getElementById("mywindow_"+IdName).style.height=ns6? window.innerHeight-20+"px" : iecompattest().clientHeight+"px"
  }else{
    minrestore=0 //restore window
    document.getElementById("maxname_"+IdName).setAttribute("src","/images/MyWMaximize.png")
    document.getElementById("mywindow_"+IdName).style.width=initialwidth
    document.getElementById("mywindow_"+IdName).style.height=initialheight
  }
  document.getElementById("mywindow_"+IdName).style.left=ns6? window.pageXOffset+"px" : iecompattest().scrollLeft+"px"
  document.getElementById("mywindow_"+IdName).style.top=ns6? window.pageYOffset+"px" : iecompattest().scrollTop+"px"
}

function closeit(IdName){
  document.getElementById("mywindow_"+IdName).style.display="none"
  document.getElementById("mywindow_bg_"+IdName).style.display='none'// nascondo il div per l'oscuramento
}

function stopdrag(IdName){
  dragapproved=false;
  document.getElementById("mywindow_"+IdName).onmousemove=null;
  document.getElementById("mywindowcontent_"+IdName).style.display="" //extra
}

// MySearchPopUp
var abilitato=0;
var strin=""; 
var temp2="";    
function search(Params,boxId,resultsId,BoxSearch){
  temp2=Params
  SearchTemp3=resultsId
  SearchTemp4=boxId
  	if(BoxSearch==null){
	 BoxSearch = 'MSPP';
	} 
  SearchTemp4=BoxSearch
 //	alert(SearchTemp4);
  document.onkeydown = keyDown;
  //analizza se il browser e' compatibile con Javascript
  browserName=navigator.appName.charAt(0);
  browserVer=parseInt(navigator.appVersion);
  if (browserName=="M" && browserVer >= 3) abilitato=1;
  if (browserName=="N" && browserVer >= 3) abilitato=2;
  if (browserName=="N" && browserVer >= 4) abilitato=3;
  if (browserName=="M" && browserVer >= 4) abilitato=4;
  document.getElementById(boxId).style.display=""
}

function searchend(){
  document.onkeydown = searchout;
  //	document.getElementById("MySearchPopUp_box").style.display="none"
}

function searchout(){
}

function keyDown(e) {
  var Params=temp2
 // alert(e+' sonqua '+Params);
  if (abilitato==3) {
    //var ns4=1;
    var nKey=e.which; var ieKey=0;
    var realkey = String.fromCharCode(e.which);
    strin="Codice tasto in Netscape: "+nKey+". Premuto tasto "+realkey
    //document.getElementById("MSPP").value
  }
  if (abilitato==4) {
    //var ns4=0;
    var ieKey=event.keyCode; var nKey=0;
    var realkey = String.fromCharCode(event.keyCode);
    strin="Codice tasto in Explorer: " + ieKey+". Premuto tasto "+realkey
  }
 // alert(strin+'\n'+document.getElementById(SearchTemp4).value);
  //document.getElementById("stesto").innerHTML=document.getElementById("MSPP").value + realkey;
   showUser('MySearchPopUp',Params + document.getElementById(SearchTemp4).value + realkey,SearchTemp3,document.getElementsByName("Session")[0].value);
}
//document.onkeydown = keyDown
//if (ns4) document.captureEvents(Event.KEYDOWN)


function checklistproc(nome){
  if( document.getElementsByTagName ) {
    var Lt = document.getElementsByName(nome);
  } else {
    return []; 
  }
  for(var i=0; i<Lt.length; i++) {
    Lt[i].style.display='';
  }
}

function checkUncheck(check) {
  var checks = document.getElementsByTagName('input');
  for(i = 0; i < checks.length; i++) {
    if(checks[i].type.toUpperCase() == 'CHECKBOX')
    checks[i].checked = check.checked;                
  }
}

// MyTabSet
function MyTabSetAction(iTab, nTab) {
  var tTab = document.getElementsByName(iTab+"MyTab");
  //alert(tTab.length+' sonqua '+nTab+' - ');
  for(i=0; i < tTab.length; i++){
    document.getElementsByName(iTab+"MyTab")[i].className= 'inactive';
    document.getElementById(iTab+"DivMyTab"+(i+1)).style.display = 'none';
  }
  document.getElementsByName(iTab+"MyTab")[nTab-1].className= 'active';
  document.getElementById(iTab+"DivMyTab"+(nTab)).style.display = 'block';			
}
