/**
 * gestione work in progress (start)
 */
 var win;
 function myOpenWorkInProgressWindow(){
  var X = 0;
  var Y = 0;
  if (screen.availWidth){
   X = (screen.availWidth / 2);
   Y = (screen.availHeight / 2);  }
  var params = 'width=200,height=200,resizable=0,scrollbars=0,status=0,' + X + ',' + Y;
  var theMatrix = Math.round(Math.random() * 10000);
  var theUrl = '/weborder';
  theUrl += '/tiles/work_in_progress.jsp' + '?matrix=' + theMatrix;
  myEngineWin = window.open( theUrl, 'workInProgressWin', params );
  window.onfocus = function(){if (myEngineWin.closed == false){myEngineWin.focus();};};
  return myEngineWin;
 }

 function openProgressWindow(button){
  //if(button) button.disabled=true;
  if(button) button.style.display='none';
  win = myOpenWorkInProgressWindow();
 }

 window.onunload = function myCloseProgressWindow() {
  if (win && win.open && !win.closed) win.close();
 }
 /**
 * gestione work in progress (end)
 */


function openWebCatalog(){
  document.getElementsByName('iFrameWebCatalog')[0].style.display='block';
}

function openCalendar(elemName){
  var elem = document.getElementsByName(elemName)[0];
  if (elem.disabled==false){
   var cal= new calendar1(elem);
   cal.year_scroll = true;
   cal.time_comp = false;
   cal.popup();
  }
}


function letturaChar(){
  k = window.event.keyCode;
  if (k == 13) {// enter key pressed
   document.lettura.codiceEan128.focus();
  }
  document.getElementById('saveButton').style.display='none';
}


 function toolbarCustomer(operazione){
  operationType = document.getElementsByName('operationType')[0];
  if (operazione=='details' || operazione=='showOrders' || operazione=='createPdf' || operazione=='createOrder'){
   if (verificaCheckBoxClienti()){
    operationType.value=operazione;
    document.getElementById('portafoglioClienti').target="_self";
    if (operazione=='createPdf'){
     document.getElementById('portafoglioClienti').target="_blank";
    }
    document.getElementById('portafoglioClienti').dbButton.click();
   }
  }else{
    operationType.value=operazione;
    document.getElementById('portafoglioClienti').dbButton.click();
  }
 }

 function toolbarOrdini(operazione){
  operationType = document.getElementsByName('operationType')[0];
  document.portafoglioOrdini.target="_self";
  if (operazione=='createPdf'|| operazione=='viewDetails'){
   if (verificaCheckBoxOrdini()){
    operationType.value=operazione;
    if (operazione=='createPdf'){
     document.portafoglioOrdini.target="_blank";
    }
    document.portafoglioOrdini.dbButton.click();
   }
  }else{
    operationType.value=operazione;
    document.searchOrdini.dbButton.click();
  }
 }

 function verificaCheckBoxClienti(){
  if (document.getElementById('portafoglioClienti')==null) return false;
  var values = document.getElementById('portafoglioClienti').selectedCustomerCode;
  var selected =0;
  if (values!=null){
   if( values.length==null){selected=1;}
   for (i=0;i<values.length;i++){
    if (values[i].checked) selected = 1;
   }
  }
  if(selected==0){
   alert("Selezionare un cliente");
   return false;
  }
  return true;
 }


 function verificaCheckBoxOrdini(){
  if (document.portafoglioOrdini==null) return false;
  var values = document.portafoglioOrdini.selectedHeaderID;
  var selected =0;
  if (values!=null){
   if( values.length==null){selected=1;}
   for (i=0;i<values.length;i++){
    if (values[i].checked) selected = 1;
   }
  }
  if(selected==0){
   alert("Selezionare un ordine");
   return false;
  }
  return true;
 }


function controllaData (str_date) {
        var arr_date = str_date.split('-');
	if (arr_date.length != 3){
          alert("Invalid date format: '" + str_date + "'.\nFormat accepted is dd-mm-yyyy.");
          return -1;
	}
	if (!arr_date[0]){
          alert("Invalid date format: '" + str_date + "'.\nNo day of month value can be found.");
          return -1;
	}
	if (!RE_NUM.exec(arr_date[0])){
          alert("Invalid day of month value: '" + arr_date[0] + "'.\nAllowed values are unsigned integers.");
          return -1;
	}
	if (!arr_date[1]){
          alert("Invalid date format: '" + str_date + "'.\nNo month value can be found.");
          return -1;
	}
	if (!RE_NUM.exec(arr_date[1])){
          alert("Invalid month value: '" + arr_date[1] + "'.\nAllowed values are unsigned integers.");
          return -1;
	}
	if (!arr_date[2]){
          alert("Invalid date format: '" + str_date + "'.\nNo year value can be found.");
          return -1;
	}
	if (!RE_NUM.exec(arr_date[2])){
          alert("Invalid year value: '" + arr_date[2] + "'.\nAllowed values are unsigned integers.");
          return -1;
	}

	var dt_date = new Date();
	dt_date.setDate(1);

	if (arr_date[1] < 1 || arr_date[1] > 12){
          alert("Invalid month value: '" + arr_date[1] + "'.\nAllowed range is 01-12.");
          return -1;
	}
	dt_date.setMonth(arr_date[1]-1);

	if (arr_date[2] < 100) arr_date[2] = Number(arr_date[2]) + (arr_date[2] < NUM_CENTYEAR ? 2000 : 1900);
	dt_date.setFullYear(arr_date[2]);

	var dt_numdays = new Date(arr_date[2], arr_date[1], 0);
	dt_date.setDate(arr_date[0]);
	if (dt_date.getMonth() != (arr_date[1]-1)){
          alert("Invalid day of month value: '" + arr_date[0] + "'.\nAllowed range is 01-"+dt_numdays.getDate()+".");
          return -1;
	}
	return (0)
}

