/*
# jsFunctions.js : Javascript Lib
#
# Change Log
#
# 	20-Jun-2007 : Initial contents.
#
# DANIEL ® ANTUNES
# eMail: me@daniel-antunes.com.br
# Icq: 107888741
# Url: http://www.daniel-antunes.com.br/
# GNU/Linux user: #238553
# Written in (vi)mproved
*/

// To fill an string with a specific character ( Ex.: to fill '9' with '0' up to 2 = '09' )
String.PAD_LEFT  = 0;
String.PAD_RIGHT = 1;
String.PAD_BOTH  = 2;

String.prototype.pad = function(size, pad, side) {
  var str = this, append = "", size = (size - str.length);
  var pad = ((pad != null) ? pad : " ");
  if ((typeof size != "number") || ((typeof pad != "string") || (pad == ""))) {
    throw new Error("Wrong parameters for String.pad() method.");
  }
  if (side == String.PAD_BOTH) {
    str = str.pad((Math.floor(size / 2) + str.length), pad, String.PAD_LEFT);
    return str.pad((Math.ceil(size / 2) + str.length), pad, String.PAD_RIGHT);
  }
  while ((size -= pad.length) > 0) {
    append += pad;
  }
  append += pad.substr(0, (size + pad.length));
  return ((side == String.PAD_LEFT) ? append.concat(str) : str.concat(append));
}

// To format numbers with currency
// Parameters (<decimals>,<decimal divisor>,<thousand divisor>)
Number.prototype.format = function(d_len, d_pt, t_pt) {
  var d_len = d_len || 0;
  var d_pt = d_pt || ".";
  var t_pt = t_pt || ",";
  if ((typeof d_len != "number")
    || (typeof d_pt != "string")
    || (typeof t_pt != "string")) {
    throw new Error("wrong parameters for method 'String.pad()'.");
  }
  var integer = "", decimal = "";
  var n = new String(this).split(/\./), i_len = n[0].length, i = 0;
  if (d_len > 0) {
    n[1] = (typeof n[1] != "undefined") ? n[1].substr(0, d_len) : "";
    decimal = d_pt.concat(n[1].pad(d_len, "0", String.PAD_RIGHT));
  }
  while (i_len > 0) {
    if ((++i % 3 == 1) && (i_len != n[0].length)) {
      integer = t_pt.concat(integer);
    }
    integer = n[0].substr(--i_len, 1).concat(integer);
  }
  return (integer + decimal);
}



// Delay for a specified amount of time
function sleep(seconds){
	var initDate=new Date();
	var curDate=null;
	do{
		curDate=new Date();
	}while(curDate-initDate<seconds*1000);
} 
// Generate an uncacheable URL
function cacheLess(url){
	var d=new Date();
	var c=(url.indexOf("?")>=0)?"&":"?";
	return url+c+encodeURI(((Math.random()).toString()).replace(/\./,'')+d.getTime());
}
// Change Color

function changeColor(obj,foreground,background){
	if ((typeof(foreground)).toLowerCase()=='undefined') obj.style.color="";
	else obj.style.color=foreground;
	if ((typeof(background)).toLowerCase()=='undefined') obj.style.background="";
	else obj.style.background=background;
}
// Variables for encodeBase64 function

var END_OF_INPUT = -1;
			
var base64Chars = new Array(
			    'A','B','C','D','E','F','G','H',
			    'I','J','K','L','M','N','O','P',
			    'Q','R','S','T','U','V','W','X',
			    'Y','Z','a','b','c','d','e','f',
			    'g','h','i','j','k','l','m','n',
			    'o','p','q','r','s','t','u','v',
			    'w','x','y','z','0','1','2','3',
			    '4','5','6','7','8','9','+','/'
				);
			
	function setBase64Str(str){
		base64Str = str;
		base64Count = 0;
	}
	function readBase64(){    
		if (!base64Str) return END_OF_INPUT;
		if (base64Count >= base64Str.length) return END_OF_INPUT;
		var c = base64Str.charCodeAt(base64Count) & 0xff;
		base64Count++;
		return c;
	}
	
	function encodeBase64(str){
		setBase64Str(str);
		var result = '';
		var inBuffer = new Array(3);
		var lineCount = 0;
		var done = false;
		while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
			inBuffer[1] = readBase64();
			inBuffer[2] = readBase64();
			result += (base64Chars[ inBuffer[0] >> 2 ]);
			if (inBuffer[1] != END_OF_INPUT){
				result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
				if (inBuffer[2] != END_OF_INPUT){
					result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
					result += (base64Chars [inBuffer[2] & 0x3F]);
				} else {
					result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
					result += ('=');
					done = true;
				}
			} else {
				result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
				result += ('=');
				result += ('=');
				done = true;
			}
			lineCount += 4;
			if (lineCount >= 76){
				result += ('\n');
				lineCount = 0;
			}
		}
	return result;
	}

// variables for queue function
	var queue=new Array();
	var nqueue=0;
	var returnFunction=null;


// queued an item 	
	function AddItem(url,elemento,rF) {
		if ((typeof(rF))=='undefined') returnFunction=null;
		else returnFunction=rF;
		queue[queue.length]=[url,elemento];
		if((nqueue+1)==queue.length) {
			ExecItem();
		}
	}

// Execute all itens queued
	function ExecItem() {
//		alert(queue[nqueue][0]+','+queue[nqueue][1]);
		ajaxGet(queue[nqueue][0],queue[nqueue][1],true);
		nqueue ++;
		if(nqueue<queue.length) {
			setTimeout("ExecItem()",20);
		}
	}

	var ie=false;

	function ajaxGet(url,elemento,exibe_carregando){ // Coloca o retorno de uma URL em um Elemento enquanto isso exibe Carregando...
		var ajax = new ajaxObject2();
		if (ajax) {

			url=antiCacheRand(url);

			// gambi para recriar o objeto -- falar com o Daniel
//			if (ie) 
//				ajax=new ActiveXObject("Microsoft.XMLHTTP");

			with (ajax) {
				open("GET", url ,true);
				onreadystatechange = ajaxOnReady;

	//	  setRequestHeader('Content-Type','text/xml'); 
//			setRequestHeader('encoding','ISO-8859-1'); 
				setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		//	setRequestHeader('Content-length', strData.length );
//		  setRequestHeader("Content-Type", "text/html; charset=iso-8859-1");//"application/x-www-form-urlencoded");
				setRequestHeader("Cache-Control", "no-cache");
				setRequestHeader("Pragma", "no-cache");

				if(exibe_carregando){ 
					put("Carregando ...");   
				}
				send(null);
				return true;				
			}

		}

		function antiCacheRand(aurl){
			var dt = new Date();
			if(aurl.indexOf("?")>=0){// já tem parametros
				return aurl + "&" + encodeURI(Math.random() + "_" + dt.getTime());
			}else{ return aurl + "?" + encodeURI(Math.random() + "_" + dt.getTime());}
		}

		function ajaxOnReady(){
			if (ajax.readyState==4){
				if(ajax.status == 200){
					var texto=ajax.responseText;
					if(texto.indexOf(" ")<0) texto=texto.replace(/\+/g," ");
 				//texto=unescape(texto); //descomente esta linha se tiver usado o urlencode no php ou asp
					put(texto);
					if (returnFunction!=null) returnFunction();
		 //			extraiScript(texto);
				}else{
					if(exibe_carregando){put("Falha no carregamento. " + httpStatus(ajax.status));}
				}
							
				ajax = null;
			}else if(exibe_carregando){//para mudar o status de cada carregando
				put("Carregando .." );
			}
		}

		function put(valor){ //coloca o valor na variavel/elemento de retorno
			if((typeof(elemento)).toLowerCase()=="string"){ //se elemento for nome da string
				if(valor!="Falha no carregamento"){
					eval(elemento + '= unescape("' + escape(valor) + '")');
				}
			}else if(elemento.tagName.toLowerCase()=="input"){
				valor = escape(valor).replace(/\%0D\%0A/g,"");
				elemento.value = unescape(valor);
			}else if(elemento.tagName.toLowerCase()=="select"){        
				select_innerHTML(elemento,valor);
			}else if(elemento.tagName){
				elemento.innerHTML = valor;
				//alert(elemento.innerHTML)
			}    
		}

	} // fim da função ajaxGet()

// Ajax Class
function ajaxObject(){
	if (typeof(XMLHttpRequest)!='undefined'){
		this.httpRequest=new XMLHttpRequest();
	}
	else{
		var arr=[	'Microsoft.XMLHTTP',
							'Msxml2.XMLHTTP',
							'Msxml2.XMLHTTP.6.0',
							'Msxml2.XMLHTTP.4.0',
							'Msxml2.XMLHTTP.3.0'];
		for (var n=0;n<arr.length;n++){
			try {
				this.httpRequest=new ActiveXObject(arr[n]);
			}
			catch(e){
				this.httpRequest=null;
			}
		}
	}
}

function ajaxObject2(){
	if (typeof(XMLHttpRequest)!='undefined'){
		return new XMLHttpRequest();
	}
	else{
		var arr=[	'Microsoft.XMLHTTP',
							'Msxml2.XMLHTTP',
							'Msxml2.XMLHTTP.6.0',
							'Msxml2.XMLHTTP.4.0',
							'Msxml2.XMLHTTP.3.0'];
		for (var n=0;n<arr.length;n++){
			try {
				ie=true;
				return new ActiveXObject(arr[n]);
			}
			catch(e){
				return null;
			}
		}
	}
}

function precisa_login(){
	alert('Para acessar reservas Hertz, forneça seu login e senha.');
}

