//variabili
w_oldvalue = "";

errorMsg = "";
focusVar = null;
m_nPageNumbers=1;
m_nCurrentPage=1;

obj_cms_images = null;
m_nCurrent_cms_images_row = 0;

obj_cms_images_type = null;
m_nCurrent_cms_images_type_row = 0;

obj_cms_attachments = null;
m_nCurrent_cms_attachments_row = 0;

obj_cms_correlations = null;
m_nCurrent_cms_correlations_row = 0;

w_cms_correlato = "";

function EmptyRow_cms_images() {
    var bRes = true;
    var frm = document.getElementById("FrmMain");
    var i = m_nCurrent_cms_images_row;
    bRes = bRes && Empty(GetCtrl("file_ima",m_nCurrent_cms_images_row).value);
    bRes = bRes && Empty(GetCtrl("descr_ima",m_nCurrent_cms_images_row).value);
    return bRes;
  } // End function EmptyRow_cms_images


//

/************************************
 * Immagini da allegare: cms_images *
 ************************************/

function Trs_cms_images() {
  if ( arguments.length > 1 ) {
    this.cprownum = arguments[0];
    /*this.ord_ima = arguments[1];*/
    this.file_ima = arguments[2];
    /*this.descr_ima = arguments[3];*/
  } else {
    this.cprownum = arguments[0];
    /*this.ord_ima = 10;*/
    this.file_ima = "";
    /*this.descr_ima = "";*/
  }
}

function Trs_cms_images_type() {
  if ( arguments.length > 1 ) {
    this.cprownum = arguments[0];
 /*   this.ord_ima = arguments[1];*/
    this.type_ima = arguments[2];
    this.file_ima = arguments[3];
    /*this.descr_ima = arguments[4];*/
  } else {
    this.cprownum = arguments[0];
 /*   this.ord_ima = 10; */
    this.type_ima = "";
    this.file_ima = "";
    this.descr_ima = "";
  }
}

function HtoTrs_cms_images() {
  var frm = document.getElementById("FrmMain");
  var idx = 0;
  for (var i=0; i<m_oTrs_cms_images.length;i++) {
    if (!RowIsDeleted("cms_images", m_oTrs_cms_images[i].cprownum)) {
      /*m_oTrs_cms_images[i].ord_ima = HtW(GetCtrl_cms_images("ord_ima", idx).value, "N");*/
      /*m_oTrs_cms_images[i].file_ima = HtTrs(GetCtrl_cms_images("old_file_ima", idx).value, "C");*/
      m_oTrs_cms_images[i].descr_ima = HtTrs(GetCtrl_cms_images("descr_ima", idx).value, "C");
      idx++;
    }
  }
}

function HtoTrs_cms_images_type() {
  var frm = document.getElementById("FrmMain");
  var idx = 0;
  for (var i=0; i<m_oTrs_cms_images_type.length;i++) {
    if (!RowIsDeleted("cms_images_type", m_oTrs_cms_images_type[i].cprownum)) {
      /*m_oTrs_cms_images_type[i].ord_ima = HtW(GetCtrl_cms_images_type("ord_ima", idx).value, "N");*/
      m_oTrs_cms_images_type[i].type_ima = HtTrs(GetCtrl_cms_images_type("type_ima", idx).value, "C");
      /*m_oTrs_cms_images_type[i].file_ima = HtTrs(GetCtrl_cms_images_type("old_file_ima", idx).value, "C");*/
      m_oTrs_cms_images_type[i].descr_ima = HtTrs(GetCtrl_cms_images_type("descr_ima", idx).value, "C");
      idx++;
    }
  }
}

/*function TrsToH_cms_images() {
  var frm = document.getElementById("FrmMain");
  var idx = 0;
  for (var i=0; i<m_oTrs_cms_images.length;i++) {
    if (!RowIsDeleted("cms_images", m_oTrs_cms_images[i].cprownum)) {
      frm["file_ima[]"][idx].value = m_oTrs_cms_images[i].file_ima;
      frm["descr_ima[]"][idx].value = m_oTrs_cms_images[i].descr_ima;
      idx++;
    }
  }
}*/

function CheckObligatory_cms_images() {
  var frm = document.getElementById("FrmMain");
  var bRes = true;
  errorMsg = "";
  focusVar = null;

  //var oldimg = GetCtrl("old_file_ima", m_nCurrent_cms_images_row);
  //if (oldimg.value == "")
    bRes = bRes && Obligatory(GetCtrl_cms_images('file_ima', m_nCurrent_cms_images_row), "", "Il file dell'immagine da allegare è obbligatorio!");
  /*bRes = bRes && Obligatory(GetCtrl_cms_images('descr_ima', m_nCurrent_cms_images_row), "", "La descrizione dell'immagine da allegare è obbligatoria!");*/

  return errorMsg;
}

function CheckObligatory_cms_images_type() {
  var frm = document.getElementById("FrmMain");
  var bRes = true;
  errorMsg = "";
  focusVar = null;

  //var oldimg = GetCtrl("old_file_ima", m_nCurrent_cms_images_type_row);
  //if (oldimg.value == "")
  bRes = bRes && Obligatory(GetCtrl_cms_images_type('file_ima', m_nCurrent_cms_images_type_row), "", "Il file dell'immagine da allegare è obbligatorio!");
 /* bRes = bRes && Obligatory(GetCtrl_cms_images_type('descr_ima', m_nCurrent_cms_images_type_row), "", "La descrizione dell'immagine da allegare è obbligatoria!");*/

  return errorMsg;
}

function Set_cms_images_Events() {
}

function Set_cms_images_type_Events() {
}

function GetCtrl_cms_images(name, rowidx) {
  var frm = CtrlById("FrmMain");
  var n = frm[name+'[]'];
  var idx = 0;
  for (var i=0; i<m_oTrs_cms_images.length;i++) {
    if (!RowIsDeleted("cms_images", m_oTrs_cms_images[i].cprownum)) {
      idx++;
    }
  }
  return (idx == 1 ? n : n[rowidx]);
}

function GetCtrl_cms_images_type(name, rowidx) {
  var frm = CtrlById("FrmMain");
  var n = frm[name+'[]'];
  var idx = 0;
  for (var i=0; i<m_oTrs_cms_images_type.length;i++) {
    if (!RowIsDeleted("cms_images_type", m_oTrs_cms_images_type[i].cprownum)) {
      idx++;
    }
  }
  return (idx == 1 ? n : n[rowidx]);
}
/***********************************/
/*  controls  */
function CtrlById(id) {
  return document.getElementById(id)
}

function FormatChar(workvar,len,picture) {
  return workvar
}

function WtH(workvar,type,len,dec,picture) {
  if (picture==null) { picture='' }
  if (len==null) { len=0 }
  if (dec==null) { dec=0 }
  switch (type) {
    case 'L':
      return FormatBoolean(workvar,picture)
      break
    break
    case 'C': case 'M' :
      return FormatChar(workvar,len,picture)
      break
    case 'N':
      if (len==0 && dec==0 && picture=='')
        return Strtran(workvar.toString(),".",decSep)
      else
        return FormatNumber(workvar,len,dec,picture)
      break
    case 'D':
      return FormatDate(workvar,picture)
      break
    case 'T':
      return FormatDateTime(workvar,picture)
      break
  }
}

function Obligatory(ctrl, defa, msg)
{
  var bRes = true;
  var v = ctrl.value;
  if ( ctrl.length > 0 ) {
    switch (ctrl[0].type) {
      case "radio":
        v = getRadioValue(ctrl);
        break;
      case "select-one":
        v = getComboValue(ctrl);
        break;
    }
  }
  if ( defa == null ) {
    defa = "";
  }
  if ( msg == null || EmptyString(msg) ) {
    msg = "Dato obbligatorio";
  }
  switch (typeof(defa)) {
    case 'number':
      v = HtW(v,'N',10,2);
      break;
    case 'boolean':
      v = HtW(v, "L");
      break;
  }
  if ( ctrl!= null && v == defa) {
    errorMsg = msg;
    focusVar = ctrl;
    return false;
  } else
    return true;
}

function Check_Email(ctrl)
{
  var bRes = true;
  if ( ctrl.value == "" )
    return bRes;
  var re;
  re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
  if (re.test(ctrl.value) == false) {
    bRes = false;
    errorMsg = "L'e-mail non è corretta";
    focusVar = ctrl;
  }
  return bRes;
}

function HtW(obj,type) {
  switch (type) {
    case 'L':
      return CharToBool(obj)
      break
    break
    case 'C': case 'M' :
      return Trim(obj)
      break
    case 'N':
      return Val(Strtran(Strtran(obj, milSep, ''), decSep, '.'))
      break
    case 'D':
      return HtmlToWork_Date(obj)
      break
    case 'T':
      return HtmlToWork_DateTime(obj)
      break
  }
}

function HtmlToWork_Date(strDate) {
  if (strDate=='' || strDate=='  -  -') {
      return new Date(100,0,1,0,0,0,0)
  }
  var day=strDate.substr(0,2)
  var month=strDate.substr(3,2)
  var year=strDate.substr(6)
  var objDate= new Date(year,month-1,day)
  return objDate
}

function HtmlToWork_DateTime(strDate) {
  if (strDate=='' || strDate=='  -  -') {
      return new Date(100,0,1,0,0,0,0)
  }
  var day=strDate.substr(0,2)
  var month=strDate.substr(3,2)
  var year=strDate.substr(6,4)
  var h=strDate.substr(11,2)
  var m=strDate.substr(14,2)
  var s=strDate.substr(17)
  var objDate= new Date(year,month-1,day,h,m,s)
  return objDate
}

/**********************************/
/* -----------------04/08/2008 15.04-----------------
data controls
 --------------------------------------------------*/
function Row_Char_OnFocus(evt, childname, idx) {
  if (ChangeRow(childname, idx)) {
    var e = evt || window.event;
    if (e) {
      var field = e.target || e.srcElement;
      if (!field && typeof(evt.value) != "undefined")
        field = evt;
      if ( field ) {
        if ( field.type == "text" || field.type == "textarea" ) {
          field.select();
        }
      }
    }
  }
}

function ChangeRow(childname, newRow) {
  var bRes = true;
  eval("var idx = m_nCurrent_"+childname+"_row;");
  if ( newRow != idx ) {
    errorMsg = "";
    eval("errorMsg = CheckObligatory_"+childname+"();");
    if ( errorMsg != "") {
      alert(errorMsg);
      if ( focusVar != null ) {
        if ( typeof(focusVar.length) =='undefined' || focusVar.length == 0 )
          focusVar.focus()
        else
          focusVar[0].focus()
      }
      bRes = false;
    } else {
      eval("m_nCurrent_"+childname+"_row = newRow;");
    }
  }
  return bRes;
}

function AddRow(childname, obj, trs) {
  if (ChangeRow(childname, obj.rows.length-1)) {
    errorMsg = "";
    eval("errorMsg = CheckObligatory_"+childname+"();");
    if ( errorMsg != "") {
      alert(errorMsg);
      if ( focusVar != null && focusVar.type != "hidden") {
        if ( typeof(focusVar.length) =='undefined' || focusVar.length == 0 )
          focusVar.focus()
        else
          focusVar[0].focus()
      }
    } else {
      InitRow(childname, obj, trs);
    }
  }
}


function TrsHtml(childname, obj, trs) {
  var frm = CtrlById("FrmMain");
  for (var i = obj.rows.length-1; i>=0;i--) {
    obj.deleteRow(i);
  }
  var rowidx = 0
  for (var i=0; i < trs.length; i++) {
    if (!RowIsDeleted(childname, trs[i].cprownum)) {
      AddTrsTableRow(obj);
      eval('Html_'+childname+'_Row('+i+','+rowidx+');');
      eval("m_nCurrent_"+childname+"_row = "+rowidx+";");
      if(frm.current_mode.value != "view" && frm.current_mode.value != "save") {
        eval("Set_"+childname+"_Events();");
      }
      rowidx++;
    }
  }
  if ( rowidx == 0 ) {
    // sono state cancellate tutte le righe
    InitRow(childname, obj, trs);
  }
  eval("m_nCurrent_"+childname+"_row = 0;");
}

function RowIsDeleted(childname, idx) {
  var frm = CtrlById("FrmMain");
  var v = "," + CtrlById("delete_"+childname).value + ","
  if ( v.indexOf("," + idx.toString() + ",") >= 0 ) {
    return true;
  }
  return false;
}
function AddTrsTableRow(obj) {
  // inserimento di una riga nella tabella HTML
  var currTableRow = obj.insertRow(obj.rows.length)
  // inserimento di una cella nella riga della tabella
  var currTableCell = currTableRow.insertCell(0)
}

function InitRow(childname, obj, trs) {
  var frm = CtrlById("FrmMain");
  var s = "", idx;
  AddTrsTableRow(obj);
  var l = trs.length;
  eval("trs[l] = new Trs_"+childname+"(trs[l-1].cprownum+1);");
  eval("Html_"+childname+"_Row(l, obj.rows.length-1);");
  eval("Set_"+childname+"_Events();");
}

function DeleteRow(childname, obj, trs, idx) {
  //if ( obj.rows.length > 1 ) {
    var frm = CtrlById("FrmMain");
    var a2 = []
    var cnt = 0
    var delidx = -1
    eval("HtoTrs_"+childname+"();");
    for (var i=0; i < trs.length;i++) {
      if ( trs[i].cprownum == idx ) {
        CtrlById("delete_"+childname).value+=idx.toString()+","
      }
    }
    TrsHtml(childname, obj, trs);
  /*} else {
    alert("Non è possibile cancellare la riga");
  }*/
}

function HtTrs(obj, type) {
  var res = HtW(obj, type);
  switch (type) {
    case 'C': case 'M' :
      return Strtran(res, '"', '&quot;');
      break;
  }
  return res;
}


