﻿/**
 * Description: general javascript actions and functions for the Data Management module
 * Created: nuno.a
 * Modified: antonio.r
 * Date: 22/Out/2011
 **/


/* todo comepletar descrição*/
function onLoadSearchPagination(element) {

  if ( $( "."+element+" > a" ).length >= 1 ) {

    /* todo meter translates */
    var int_countPages = $( "." + element + " a" ).length,
        str_anchorHref = $( "." + element + " a:first" ).attr( "href" ),
        int_actualPage = parseInt( $( "." + element + " a b" ).html() ),
        str_htmlPagination;

    if ( int_actualPage >= 2 ) {
      str_htmlPagination = "<span class='firstPage'><a href=' " + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&Page=1'>firstPage</a></span><span class='previousPage'><a href=' " + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&Page=" + (int_actualPage - 1) + "'>previousPage</a></span><ul class='pagination'>";
      str_htmlPagination += "<li class='firstItem' ><a href='" + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&Page=" + (int_actualPage - 1) + " '>" + (int_actualPage - 1) + "</a> - </li>";
    }
    else {
      str_htmlPagination = "<ul class='pagination'>";
    }

    if ( int_actualPage != int_countPages )
      str_htmlPagination += "<li class='activeItem' ><a href='" + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&Page=" + int_actualPage + " '>" + int_actualPage + "</a> - </li>";
    else if ( int_actualPage == int_countPages )
      str_htmlPagination += "<li class='activeItem' ><a href='" + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&Page=" + int_actualPage + " '>" + int_actualPage + "</a></li>";

    if ( (int_actualPage + 1) <= int_countPages )
      str_htmlPagination += "<li ><a href='" + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&Page=" + (int_actualPage + 1) + " '>" + (int_actualPage + 1) + "</a> - </li>";

    if ( (int_actualPage + 2) <= int_countPages )
      str_htmlPagination += "<li class='lasItem' ><a href='" + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&Page=" + (int_actualPage + 2) + " '>" + (int_actualPage + 2) + "</a></li>";



    if ( int_actualPage == int_countPages ) {
      str_htmlPagination += "</ul>";
    }
    else {
      str_htmlPagination += "</ul><span class='nextPage'><a href=' " + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&Page=" + (int_actualPage + 1) + " '>nextPage</a></span><span class='lastPage'><a href=' " + str_anchorHref.split( "&" )[0] + "&" + str_anchorHref.split( "&" )[1] + "&Page=" + int_countPages + "'>lastPage</a></span>";
    }

    $( "." + element ).append( str_htmlPagination );
    $( "." + element + " > a" ).addClass( "displayNone" );
  }
}

function searchFocusBlur(str_formID , obj_editables , str_formName) {
  var $obj_form,
      obj_formArray;
  if ( str_formID != null ) {
    $obj_form = document.getElementById( str_fomID );
  }
  else {
    obj_formArray = document.getElementsByName( str_formName );
    $obj_form = obj_formArray[0];
  }

  if ( $obj_form != undefined ) {
    for ( var e = 0 ; e < $obj_form.elements.length ; e++ ) {
      if ( obj_editables[$obj_form.elements[e].type] ) {
        $obj_form.elements[e].onfocus = function () {
          searchDefaultValue( this );
        }
        $obj_form.elements[e].onblur = function () {
          searchDefaultValue( this );
        }
      }
    }
  }
}

function searchDefaultValue($obj_input) {
  if ( $obj_input.value == $obj_input.defaultValue ) {
    $obj_input.value = '';
  } else if ( $obj_input.value == '' ) {
    $obj_input.value = $obj_input.defaultValue;
  }
}
