//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, startYear, endYear) {
	dateSelectLimits[id] = {"startDate": "1/1/" + startYear, "endDate": "31/12/" + endYear};
	
	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);
	
	if (currDate.compareDate(startDate) >= 0 && currDate.compareDate(endDate) <= 0) {
		//day change
		$("#" + id + "_day").val(currDate.getDate()).change(function() {redoDaySelect(id);});
		
		//month change
		$("#" + id + "_month").val(currDate.getMonth() + 1).change(function() {redoDaySelect(id);});
		
		//year change
		$("#" + id + "_year").val(currDate.getFullYear()).change(function() {redoDaySelect(id);});
	}
	else {
		$("#" + id).val(dateSelectLimits[id]["startDate"]);
	}
}

function redoDaySelect(id) {
	var i;
	var currDate = new Date();
	
	currDate.setDate(1);
	currDate.setMonth($("#" + id + "_month").val()-1);
	currDate.setYear($("#" + id + "_year").val());
	
	//Do days
	var saveDay = $("#" + id + "_day").val();
	$("#" + id + "_day").html("");
	for (i = 1; i <= currDate.getDaysInMonth(); i++) {
		$("#" + id + "_day").append('<option value=\"' + i + '\">' + i + '</option>');
	}
	
	if (saveDay > currDate.getDaysInMonth())
		saveDay = currDate.getDaysInMonth();
	
	$("#" + id + "_day").val(saveDay);
	
	//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;
}







