//dateselect fields date limits associative array
var dateSelectLimits = new Object();

function doStatusMsg() {
  doStatusMsgId('statusMsg');
}

function doStatusMsgId(id) {
  //Make the status messages more cool
  $("#" + id)
    .hide()
    //.css("top", $(this).parent().height())
    //.css("right", $(this).parent().width())
    .fadeIn(1000, function() {
      setTimeout(
        function() {$("#" + id).fadeOut("slow", function() {$("#" + id).remove();});}, 
        config["statusMessage"]["timeout"] * 1000
      );
    });
}

function initTimeSelect(id) {
  
}

function initDateSelect(id, startDate, endDate, limitToToday) {
  
  if (startDate.indexOf('/') == -1) {
    startDate = "1/1/" + startDate;
  }
  
  if (endDate.indexOf('/') == -1) {
    endDate = "1/1/" + endDate;
  }
  
  dateSelectLimits[id] = {"startDate": startDate, "endDate": endDate};
  
  var currDate = new Date();
  currDate.setDateTime($("#" + id).val(), true);

  var startDate = new Date();
  startDate.setDateTime(dateSelectLimits[id]["startDate"], true);
  
  var endDate = new Date();
  endDate.setDateTime(dateSelectLimits[id]["endDate"], true);
  
  //day change
  $("#" + id + "_day").val(currDate.getDate()).change(function() {redoDaySelect(id, limitToToday);});
  
  //month change
  $("#" + id + "_month").val(currDate.getMonth() + 1).change(function() {redoDaySelect(id, limitToToday);});
  
  //year change
  $("#" + id + "_year").val(currDate.getFullYear()).change(function() {redoDaySelect(id, limitToToday);});
  
  $("#" + id).val(dateSelectLimits[id]["startDate"]);
  
  redoDaySelect(id, limitToToday);
}


function redoDaySelect(id, limitToToday) {
  var i;
  var currDate = new Date();
  
  currDate.setDate(1);
  currDate.setMonth($("#" + id + "_month").val()-1);
  currDate.setYear($("#" + id + "_year").val());
  
  if (limitToToday) {
    var actualDate = new Date();
    if (actualDate.compareDate(currDate) < 0) {
      currDate = actualDate;
    }
  }
  
  //Do days
  var saveAux = $("#" + id + "_day").val();
  $("#" + id + "_day").html("");
  var days = currDate.getDaysInMonth();
  if (limitToToday && currDate.getFullYear() == actualDate.getFullYear() && currDate.getMonth() == actualDate.getMonth()) {
    days = actualDate.getDate();
  }
  for (i = 1; i <= days; i++) {
    $("#" + id + "_day").append('<option value=\"' + i + '\">' + i + '</option>');
  }
  
  if (saveAux > days) {
    saveAux = days;
  }
  
  $("#" + id + "_day").val(saveAux);
  
  
  //Do months
  var saveAux = $("#" + id + "_month").val();
  $("#" + id + "_month").html("");
  var months = 12;
  if (limitToToday && currDate.getFullYear() == actualDate.getFullYear()) {
    months = actualDate.getMonth() +1;
  }
  for (i = 1; i <= months; i++) {
    $("#" + id + "_month").append('<option value=\"' + i + '\">' + i + '</option>');
  }
  
  if (saveAux > months) {
    saveAux = months;
  }
  
  $("#" + id + "_month").val(saveAux);
  
  //Redo date in hidden field
  $("#" + id ).val($("#" + id + "_day").val() + "/" + $("#" + id + "_month").val() + "/" + $("#" + id + "_year").val());
}

function eventGetTarget(e) {
  var target;
  if (e.target)
    target = e.target;
  else
    target = e.srcElement;
  
  return target;
}

//######################## Other utils ################################

function doClock() {
  var today = new Date();
  var y, m, d, h, n, s;
  
  y = today.getFullYear();
  var months = new Array("Jan", "Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez");
  
  m = months[today.getMonth()];
  d = today.getDate(); if (d < 10) d = "" + d;
  h = today.getHours(); if (h < 10) h = "0" + h;
  n = today.getMinutes(); if (n < 10) n = "0" + n;
  s = today.getSeconds(); if (s < 10) s = "0" + s;
  
  var c = document.getElementsByName("clock1");
  if (c.length == 1) {
     c[0].value = d + " " + m + " " + y + " - " + h + ":" + n + ":" + s;
      window.setTimeout("doClock();", 1000);
  }
}

//Packing
function pack(input) {
  return encode64(serialize(input));
}

function unpack(input) {
  return unserialize(decode64(input));
}

function serialize(input) {
  var serializer = new HTML_AJAX_Serialize_PHP();
  
  return serializer.serialize(input);
}

function unserialize(input) {
  var serializer = new HTML_AJAX_Serialize_PHP();
  
  return serializer.unserialize(input);
}

// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
  var dumped_text = "";
  if(!level) level = 0;
  
  //The padding given at the beginning of the line.
  var level_padding = "";
  for(var j=0;j<level+1;j++) level_padding += "    ";
  
  if(typeof(arr) == 'object') { //Array/Hashes/Objects 
    for(var item in arr) {
      var value = arr[item];
      
      if(typeof(value) == 'object') { //If it is an array,
        dumped_text += level_padding + "'" + item + "' ...\n";
        dumped_text += dump(value,level+1);
      } else {
        dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
      }
    }
  } else { //Stings/Chars/Numbers etc.
    dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
  }
  return dumped_text;
}







