﻿/**
 * Description: general javascript actions and functions for the Data Management module
 * User: nuno.a
 * Edited: antonio.r
 * Date: 22/Out/2011
 **/



function onLoadDMForms(str_formId) {
  if ( $( "#FormSave" + str_formId + " select" ).length >= 1 )
    $( "#FormSave" + str_formId + " select" ).select_skin();
  if (str_formId == 29) {
     $( "#FormSave" + str_formId + " input[type=file]" ).filestyle( {
      image: "Files/Templates/Designs/DeltaQ/images/browseRecrutamento.jpg",
      imageheight : 17,
      imagewidth : 54,
      width : 54
    } );
    $("#FormSave" + str_formId + " input[type=file]" ).addClass( "forceInputFile" );
    $("#FormSave" + str_formId + " input.file" ).css({
      "border":" 0",
      "background-color":"#000000",
      "color":"#A3A3A3",
      "width":"112px"
    }).val('Selecione um ficheiro');
    $("#FormSave" + str_formId + " input.file" ).parent().height(34);
  }
}
function onLoadRegisterMachineForm(obj_hiddenFields) {
  var str_userConvertedEmail = "",
      str_userEmail = obj_hiddenFields.str_userEmail;

  str_userEmail = str_userEmail.replace( /&/g , '' );
  str_userEmail = str_userEmail.replace( /#/g , '' );
  str_userEmail = str_userEmail.split( ";" );
  for ( i = 0 ; i < str_userEmail.length - 1 ; i++ ) {
    str_userConvertedEmail += String.fromCharCode( str_userEmail[i] );
  }

  $( "input#DMForms_RegistoMaquina_NomeDoCliente" ).val( obj_hiddenFields.str_userName );
  $( "input#DMForms_RegistoMaquina_Email" ).val( str_userConvertedEmail );

  $( "input#DMForms_RegistoMaquina_Voucher" ).parents( "tr" ).hide();
  $( "#DMForms_RegistoMaquina_FR_Voucher").parents( "tr" ).hide();
  $( "select[name^='DMForms']" ).select_skin();
  $( "input[type=file]" ).filestyle( {
    image: "Files/Templates/Designs/DeltaQ/images/browseBg.gif",
    imageheight : 22,
    imagewidth : 82,
    width : 100
  } );
  $( "input[type=file]" ).addClass( "forceInputFile" );
  $( "input.file" ).hide();
  $( "input.file" ).parent().addClass("customUploadTableCell");
  $( "input.file" ).next().append('<div class="customUpload">' + obj_hiddenFields.str_browseButton + '</div>');
  /* helpdeskStart - nuno.b - 30/01/2012 @ 12:42 (correct the position of the dropdown of the years) #4779 */
  if ($('#DMForms_RegistoMaquina_FR_DateDAchat_year').length > 0) { $('#DMForms_RegistoMaquina_FR_DateDAchat_year').parent().css('margin', '0'); }
  /* helpdeskEnd */
}

function onLoadConfirmationRegisterMachineForm() {

  var str_machineModel = $('#DMForms_RegistoMaquina_Modelo_da_maquina').val(),
        $obj_selectMachine;


    $('ul.variants li').each(function(){
      if($(this).attr('title') == str_machineModel){
        $obj_selectMachine = $(this);
      }else{
        $(this).hide();
      }
    });
    $obj_selectMachine.find('a').click();
    $("li.registerMachineItem").unbind('click').unbind('mouseenter mouseleave');

    $('.heading').hide();

}

function updateFormValidation(str_popUpMessage , str_stringToAdd) {
  bol_submitForm = false;
  if ( str_popUpMessage != '' ) str_popUpMessage += '<br/>';
  str_popUpMessage += str_stringToAdd;
  return str_popUpMessage;
}

function validateRegisterMachine() {
  var bol_submitForm = true,
      int_licencePlateLength = 6,
      str_errorMessage = '',
      str_machineLoadedFr = $( "#DMForms_RegistoMaquina_FR_ModeleDeLaMachine" ).val(),
      str_machineLoaded = $( "#DMForms_RegistoMaquina_Modelo_da_maquina" ).val();

  // todo finalizar com os critérios de validação do desenvolvimento
  // todo fazer chamada ajax para validar se a matrícula/voucher estão correctos e se ok, submete o formulário

  if ( $( "#DMForms_RegistoMaquina_Modelo_da_maquina" ).val() == '' ) {
    str_errorMessage = updateFormValidation( str_errorMessage , obj_errorMessagesRegisterMachines.str_noMachineSelected );
    bol_submitForm = false;
  }
  else {
    if ( str_machineLoaded.search( "alQimia" ) != -1 || str_machineLoaded.search( "Qosmo" ) != -1 || (str_machineLoaded.search( "Qool" ) != -1 && str_machineLoaded.search( "Qool 1.1" ) == -1) ) {
      int_licencePlateLength = 10;
    }
  }

  if ( $( "#DMForms_RegistoMaquina_Matricula" ).val() == '' || $( "#DMForms_RegistoMaquina_Matricula" ).val().length != int_licencePlateLength || $( "#DMForms_RegistoMaquina_FR_Rnregistrement" ).val() == '' || $( "#DMForms_RegistoMaquina_FR_Rnregistrement" ).val().length != int_licencePlateLength ) {

    if(str_machineLoaded.search( "Qosmo" ) != -1 || (str_machineLoaded.search( "Qool" ) != -1 && str_machineLoaded.search( "Qool 1.1" ) == -1)){
      if($( "#DMForms_RegistoMaquina_Matricula" ).val().length < 6){
        str_errorMessage = updateFormValidation( str_errorMessage , obj_errorMessagesRegisterMachines.str_maxLicencePlate + " 6-" + int_licencePlateLength + " " + obj_errorMessagesRegisterMachines.str_digits );
        bol_submitForm = false;
      }
    }
    else{
      if(str_machineLoadedFr.search( "Qosmo" ) != -1 || (str_machineLoadedFr.search( "Qool" ) != -1 && str_machineLoadedFr.search( "Qool 1.1" ) == -1)){
        if($( "#DMForms_RegistoMaquina_FR_Rnregistrement" ).val().length < 6){
          str_errorMessage = updateFormValidation( str_errorMessage , obj_errorMessagesRegisterMachines.str_maxLicencePlate + " 6-" + int_licencePlateLength + " " + obj_errorMessagesRegisterMachines.str_digits );
          bol_submitForm = false;
        }
      }
      else {
        str_errorMessage = updateFormValidation( str_errorMessage , obj_errorMessagesRegisterMachines.str_licencePlate + " " + int_licencePlateLength + " " + obj_errorMessagesRegisterMachines.str_digits );
        bol_submitForm = false;
      }
    }
  }
 /* todo comentado porque não deve ser validado o campo voucher --> #4554 nuno.b
    if ( (str_machineLoaded.search( "alQimia" ) != -1 || str_machineLoaded.search( "Qosmo" ) != -1 || str_machineLoaded.search( "Qool 1.1" ) != -1) && ($( "#DMForms_RegistoMaquina_Voucher" ).val() == '' || $( "#DMForms_RegistoMaquina_Voucher" ).val().length < 8) ) {
    str_errorMessage = updateFormValidation( str_errorMessage , obj_errorMessagesRegisterMachines.str_voucherRequested );
    bol_submitForm = false;
  }*/

  if ( !bol_submitForm ) alert( str_errorMessage );
  return bol_submitForm;
}
