function Utils(){}

Utils.changeLanguage = function()
{
  $.ajax({
    url: "../PHPClasses/SetSessionVar.php",
    dataType: "html",
    type: "POST",
    data: "operation=changeLanguage",
    success: function(){
      window.location.reload();
    },
    error: function(){
      alert("Error! Change Language");
    }
  });
}

Utils.fixPng = function()
{
  var nAgt = navigator.userAgent;
  var fullVersion  = ''+parseFloat(navigator.appVersion);

  // In MSIE, the true version is after "MSIE" in userAgent
  if ((verOffset = nAgt.indexOf("MSIE")) != -1)
  {
    var verOffset, ix;
    fullVersion = nAgt.substring(verOffset+5);

    // trim the fullVersion string at semicolon/space if present
    if ((ix = fullVersion.indexOf(";")) != -1)
    {
      fullVersion = fullVersion.substring(0, ix);
    }
    if ((ix = fullVersion.indexOf(" ")) != -1)
    {
      fullVersion = fullVersion.substring(0,ix);
    }

    if (fullVersion.indexOf("6.0") != -1)
    {
      $(document).pngFix();
    }
  }
}


/*
 * Return true if the mail address is writing properly,
 * otherwise return false.
 */
Utils.checkRequestedEmail = function(mail)
{
  var filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;

  if (mail == "" || !filtro.test(mail))
  {
    return false;
  }
  else
  {
    return true;
  }
}

Utils.changeLangOnSamePage = function(newLang)
{
  $.ajax({
      url: '../PHPClasses/SetSessionVar.php',
      type: 'POST',
      dataType: 'text',
      data: 'operation=changeLanguage&newLang=' + newLang,
      success: function(){
        window.location.reload();
      },
      error: function(){
        alert('Error changing language');
      }
    });
}


Utils.composeTicketMessage = function()
{
  var messageHeader = $("#topicId option:selected").text();
  
  if ($("#sw_version").val() != '')
  {
    messageHeader += " - v" + $("#sw_version").val();
  }
  if ($("#sw_serial").val() != '')
  {
    messageHeader += " - Serial Number: " + $("#sw_serial").val();
  }
  
  $("#message").val(messageHeader + "\n\n" + $("#messageBody").val());
  
  $('#submitTicketForm').submit();
}

Utils.changeHelpImg = function(select)
{  
  var selected = $(select).val();
  
  
  if (selected == '' || selected == '1')
  {
    $("#helpTopicLogo").show();
    $("#helpTopicBox").hide();
  }
  else
  {
    var img = new Image();
    var imgPath = "";
    
    if (selected == '3')
    {
      imgPath = "../rsc/Photo/Box/Lp-box-small.png";

    }
    else if (selected == '4' || selected == '7')
    {
      imgPath = "../rsc/Photo/Box/Re-box-small.png";
    }
    else if (selected == '5')
    {
      imgPath = "../rsc/Photo/Box/Dj-box-small.png";
    }
    else if (selected == '6')
    {
      imgPath = "../rsc/Photo/Box/Dm-box-small.png";
    }
    
    $(img).load(function() {
      $(".loadingImg").attr('src', img.src);
      
      if ($(".loadedImg").attr('src') != img.src)
      {
        $(".loadedImg").fadeOut('slow', function(){
          //$("#loadedImg").attr('src', img.src);
          $(".loadingImg").fadeIn('slow');

          if ($("#help-img-1").hasClass('loadedImg'))
          {
            $("#help-img-1").removeClass('loadedImg');
            $("#help-img-1").addClass('loadingImg');
            $("#help-img-2").removeClass('loadingImg');
            $("#help-img-2").addClass('loadedImg');
          }
          else
          {
            $("#help-img-1").addClass('loadedImg');
            $("#help-img-1").removeClass('loadingImg');
            $("#help-img-2").addClass('loadingImg');
            $("#help-img-2").removeClass('loadedImg');
          }
        });
      }
    });
    
    img.src = imgPath;
    
    $("#helpTopicLogo").hide();
    $("#helpTopicBox").show();
  }
    
}
