
function scJQGeneralAdd() {
  $('input:text.sc-js-input').listen();
  $('input:password.sc-js-input').listen();
  $('input:checkbox.sc-js-input').listen();
  $('select.sc-js-input').listen();
  $('textarea.sc-js-input').listen();

} // scJQGeneralAdd

function scFocusField(sField) {
  var $oField = $('#id_sc_field_' + sField);

  if (0 == $oField.length) {
    $oField = $('input[name=' + sField + ']');
  }

  if (0 == $oField.length && document.F1.elements[sField]) {
    $oField = $(document.F1.elements[sField]);
  }

  if (false == scSetFocusOnField($oField) && 0 < $("#id_ac_" + sField).length) {
    scSetFocusOnField($("#id_ac_" + sField));
  }
} // scFocusField

function scSetFocusOnField($oField) {
  if (0 < $oField.length && 0 < $oField[0].offsetHeight && 0 < $oField[0].offsetWidth && !$oField[0].disabled) {
    $oField[0].focus();
    return true;
  }
  return false;
} // scSetFocusOnField

function scEventControl_init(iSeqRow) {
  scEventControl_data["logo" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["active" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["title" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["actas" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["address" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["phone" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["fax" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["email" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["response" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["work" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["vat" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["doy" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["invaddress" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["remarks" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["login" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
  scEventControl_data["emaillogin" + iSeqRow] = {"blur": false, "change": false, "autocomp": false, "original": ""};
}

function scEventControl_active(iSeqRow) {
  if (scEventControl_data["active" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["active" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["title" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["title" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["actas" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["actas" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["address" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["address" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["phone" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["phone" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["fax" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["fax" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["email" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["email" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["response" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["response" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["work" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["work" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["vat" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["vat" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["doy" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["doy" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["invaddress" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["invaddress" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["remarks" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["remarks" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["login" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["login" + iSeqRow]["change"]) {
    return true;
  }
  if (scEventControl_data["emaillogin" + iSeqRow]["blur"]) {
    return true;
  }
  if (scEventControl_data["emaillogin" + iSeqRow]["change"]) {
    return true;
  }
  return false;
} // scEventControl_active

function scEventControl_onFocus(oField, iSeq) {
  var fieldId, fieldName;
  fieldId = $(oField).attr("id");
  fieldName = fieldId.substr(12);
  scEventControl_data[fieldName]["blur"] = true;
  scEventControl_data[fieldName]["change"] = false;
} // scEventControl_onFocus

function scEventControl_onBlur(sFieldName) {
  scEventControl_data[sFieldName]["blur"] = false;
  if (scEventControl_data[sFieldName]["change"]) {
        if (scEventControl_data[sFieldName]["original"] == $("#id_sc_field_" + sFieldName).val()) {
          scEventControl_data[sFieldName]["change"] = false;
        }
  }
} // scEventControl_onBlur

function scEventControl_onChange(sFieldName) {
  scEventControl_data[sFieldName]["change"] = false;
} // scEventControl_onChange

function scEventControl_onAutocomp(sFieldName) {
  scEventControl_data[sFieldName]["autocomp"] = false;
} // scEventControl_onChange

var scEventControl_data = {};

function scJQEventsAdd(iSeqRow) {
  $('#id_sc_field_active' + iSeqRow).bind('blur', function() { sc_form_profil_active_onblur(this, iSeqRow) })
                                    .bind('focus', function() { sc_form_profil_active_onfocus(this, iSeqRow) });
  $('#id_sc_field_actas' + iSeqRow).bind('blur', function() { sc_form_profil_actas_onblur(this, iSeqRow) })
                                   .bind('focus', function() { sc_form_profil_actas_onfocus(this, iSeqRow) });
  $('#id_sc_field_title' + iSeqRow).bind('blur', function() { sc_form_profil_title_onblur(this, iSeqRow) })
                                   .bind('focus', function() { sc_form_profil_title_onfocus(this, iSeqRow) });
  $('#id_sc_field_address' + iSeqRow).bind('blur', function() { sc_form_profil_address_onblur(this, iSeqRow) })
                                     .bind('focus', function() { sc_form_profil_address_onfocus(this, iSeqRow) });
  $('#id_sc_field_phone' + iSeqRow).bind('blur', function() { sc_form_profil_phone_onblur(this, iSeqRow) })
                                   .bind('focus', function() { sc_form_profil_phone_onfocus(this, iSeqRow) });
  $('#id_sc_field_fax' + iSeqRow).bind('blur', function() { sc_form_profil_fax_onblur(this, iSeqRow) })
                                 .bind('focus', function() { sc_form_profil_fax_onfocus(this, iSeqRow) });
  $('#id_sc_field_email' + iSeqRow).bind('blur', function() { sc_form_profil_email_onblur(this, iSeqRow) })
                                   .bind('focus', function() { sc_form_profil_email_onfocus(this, iSeqRow) });
  $('#id_sc_field_response' + iSeqRow).bind('blur', function() { sc_form_profil_response_onblur(this, iSeqRow) })
                                      .bind('focus', function() { sc_form_profil_response_onfocus(this, iSeqRow) });
  $('#id_sc_field_vat' + iSeqRow).bind('blur', function() { sc_form_profil_vat_onblur(this, iSeqRow) })
                                 .bind('focus', function() { sc_form_profil_vat_onfocus(this, iSeqRow) });
  $('#id_sc_field_doy' + iSeqRow).bind('blur', function() { sc_form_profil_doy_onblur(this, iSeqRow) })
                                 .bind('focus', function() { sc_form_profil_doy_onfocus(this, iSeqRow) });
  $('#id_sc_field_remarks' + iSeqRow).bind('blur', function() { sc_form_profil_remarks_onblur(this, iSeqRow) })
                                     .bind('focus', function() { sc_form_profil_remarks_onfocus(this, iSeqRow) });
  $('#id_sc_field_logo' + iSeqRow).bind('blur', function() { sc_form_profil_logo_onblur(this, iSeqRow) })
                                  .bind('focus', function() { sc_form_profil_logo_onfocus(this, iSeqRow) });
  $('#id_sc_field_invaddress' + iSeqRow).bind('blur', function() { sc_form_profil_invaddress_onblur(this, iSeqRow) })
                                        .bind('focus', function() { sc_form_profil_invaddress_onfocus(this, iSeqRow) });
  $('#id_sc_field_work' + iSeqRow).bind('blur', function() { sc_form_profil_work_onblur(this, iSeqRow) })
                                  .bind('focus', function() { sc_form_profil_work_onfocus(this, iSeqRow) });
  $('#id_sc_field_login' + iSeqRow).bind('blur', function() { sc_form_profil_login_onblur(this, iSeqRow) })
                                   .bind('focus', function() { sc_form_profil_login_onfocus(this, iSeqRow) });
  $('#id_sc_field_emaillogin' + iSeqRow).bind('blur', function() { sc_form_profil_emaillogin_onblur(this, iSeqRow) })
                                        .bind('focus', function() { sc_form_profil_emaillogin_onfocus(this, iSeqRow) });
} // scJQEventsAdd

function sc_form_profil_active_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_active();
  scCssBlur(oThis);
}

function sc_form_profil_active_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_actas_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_actas();
  scCssBlur(oThis);
}

function sc_form_profil_actas_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_title_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_title();
  scCssBlur(oThis);
}

function sc_form_profil_title_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_address_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_address();
  scCssBlur(oThis);
}

function sc_form_profil_address_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_phone_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_phone();
  scCssBlur(oThis);
}

function sc_form_profil_phone_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_fax_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_fax();
  scCssBlur(oThis);
}

function sc_form_profil_fax_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_email_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_email();
  scCssBlur(oThis);
}

function sc_form_profil_email_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_response_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_response();
  scCssBlur(oThis);
}

function sc_form_profil_response_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_vat_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_vat();
  scCssBlur(oThis);
}

function sc_form_profil_vat_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_doy_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_doy();
  scCssBlur(oThis);
}

function sc_form_profil_doy_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_remarks_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_remarks();
  scCssBlur(oThis);
}

function sc_form_profil_remarks_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_logo_onblur(oThis, iSeqRow) {
  scCssBlur(oThis);
}

function sc_form_profil_logo_onfocus(oThis, iSeqRow) {
  scCssFocus(oThis);
}

function sc_form_profil_invaddress_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_invaddress();
  scCssBlur(oThis);
}

function sc_form_profil_invaddress_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_work_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_work();
  scCssBlur(oThis);
}

function sc_form_profil_work_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_login_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_login();
  scCssBlur(oThis);
}

function sc_form_profil_login_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function sc_form_profil_emaillogin_onblur(oThis, iSeqRow) {
  do_ajax_form_profil_mob_validate_emaillogin();
  scCssBlur(oThis);
}

function sc_form_profil_emaillogin_onfocus(oThis, iSeqRow) {
  scEventControl_onFocus(oThis, iSeqRow);
  scCssFocus(oThis);
}

function scJQUploadAdd(iSeqRow) {
  $("#id_sc_field_logo" + iSeqRow).fileupload({
    datatype: "json",
    url: "form_profil_mob_ul_save.php",
    dropZone: "",
    formData: function() {
      return [
        {name: 'app_dir', value: '<?php echo $this->Ini->path_aplicacao; ?>'},
        {name: 'app_name', value: 'form_profil_mob'},
        {name: 'upload_dir', value: '<?php echo $this->Ini->root . $this->Ini->path_imag_temp; ?>/'},
        {name: 'upload_url', value: '<?php echo $this->Ini->path_imag_temp; ?>/'},
        {name: 'upload_url', value: '<?php echo $this->Ini->path_imag_temp; ?>/'},
        {name: 'upload_type', value: 'single'},
        {name: 'param_name', value: 'logo' + iSeqRow},
        {name: 'upload_file_height', value: '0'},
        {name: 'upload_file_width', value: '0'},
        {name: 'upload_file_aspect', value: 'S'},
        {name: 'upload_file_type', value: 'I'},
        {name: 'upload_file_row', value: iSeqRow}
      ];
    },
    progress: function(e, data) {
      var loader, progress;
      if (data.lengthComputable && window.FormData !== undefined) {
        loader = $("#id_img_loader_logo" + iSeqRow);
        progress = parseInt(data.loaded / data.total * 100, 10);
        loader.show().find("div").css("width", progress + "%");
      }
      else {
        loader = $("#id_ajax_loader_logo" + iSeqRow);
        loader.show();
      }
    },
    done: function(e, data) {
      var fileData, thumbDisplay, checkDisplay, var_ajax_img_thumb;
      if (data.result[0].body) {
        fileData = $.parseJSON(data.result[0].body.innerText);
      }
      else {
        fileData = eval(data.result);
      }
      $("#id_sc_field_logo" + iSeqRow).val("");
      $("#id_sc_field_logo_ul_name" + iSeqRow).val(fileData[0].sc_random_prot);
      $("#id_sc_field_logo_ul_type" + iSeqRow).val(fileData[0].type);
      var_ajax_img_logo = '<?php echo $this->Ini->path_imag_temp; ?>/' + fileData[0].sc_random_prot;
      var_ajax_img_thumb = '<?php echo $this->Ini->path_imag_temp; ?>/' + fileData[0].sc_thumb_prot;
      thumbDisplay = ("" == var_ajax_img_logo) ? "none" : "";
      $("#id_ajax_img_logo" + iSeqRow).attr("src", var_ajax_img_thumb);
      $("#id_ajax_img_logo" + iSeqRow).css("display", thumbDisplay);
      if (document.F1.temp_out1_logo) {
        document.F1.temp_out_logo.value = var_ajax_img_thumb;
        document.F1.temp_out1_logo.value = var_ajax_img_logo;
      }
      else if (document.F1.temp_out_logo) {
        document.F1.temp_out_logo.value = var_ajax_img_logo;
      }
      checkDisplay = ("" == fileData[0].sc_random_prot.substr(12)) ? "none" : "";
      $("#chk_ajax_img_logo" + iSeqRow).css("display", checkDisplay);
      $("#txt_ajax_img_logo" + iSeqRow).html(fileData[0].sc_random_prot.substr(12));
      $("#txt_ajax_img_logo" + iSeqRow).css("display", checkDisplay);
      $("#id_ajax_link_logo" + iSeqRow).html(fileData[0].sc_random_prot.substr(12));
      if (window.FormData !== undefined)
      {
        $("#id_img_loader_logo" + iSeqRow).hide();
      }
      else
      {
        $("#id_ajax_loader_logo" + iSeqRow).hide();
      }
    }
  });

} // scJQUploadAdd


function scJQElementsAdd(iLine) {
  scJQEventsAdd(iLine);
  scEventControl_init(iLine);
  scJQUploadAdd(iLine);
} // scJQElementsAdd

var scBtnGrpStatus = {};
function scBtnGrpShow(sGroup) {
  var btnPos = $('#sc_btgp_btn_' + sGroup).offset();
  scBtnGrpStatus[sGroup] = 'open';
  $('#sc_btgp_btn_' + sGroup).mouseout(function() {
    setTimeout(function() {
      scBtnGrpHide(sGroup);
    }, 1000);
  });
  $('#sc_btgp_div_' + sGroup + ' span a').click(function() {
    scBtnGrpStatus[sGroup] = 'out';
    scBtnGrpHide(sGroup);
  });
  $('#sc_btgp_div_' + sGroup).css({
    'left': btnPos.left
  })
  .mouseover(function() {
    scBtnGrpStatus[sGroup] = 'over';
  })
  .mouseleave(function() {
    scBtnGrpStatus[sGroup] = 'out';
    setTimeout(function() {
      scBtnGrpHide(sGroup);
    }, 1000);
  })
  .show('fast');
}
function scBtnGrpHide(sGroup) {
  if ('over' != scBtnGrpStatus[sGroup]) {
    $('#sc_btgp_div_' + sGroup).hide('fast');
  }
}
