//calls[0] = Array('div_name','url',animacao,'');
function callMultiAjaxData(calls, i){
	var xmlhttp = Array();
	BoxContainer = calls[i][0]; 
	url = calls[i][1]; 
	showWaitImg = calls[i][2];
	someFunction = calls[i][3];
	theDiv = document.getElementById(BoxContainer);
	if(showWaitImg){
		theDiv.innerHTML = showWaitMessage();
	}

	try{
	    xmlhttp[i] = new XMLHttpRequest();
	}catch(ee){
	    try{
	        xmlhttp[i] = new ActiveXObject("Msxml2.XMLHTTP");
	    }catch(e){
	        try{
	            xmlhttp[i] = new ActiveXObject("Microsoft.XMLHTTP");
	        }catch(E){
	            xmlhttp[i] = false;
	        }
	    }
	}
	xmlhttp[i].open("GET", url,true);
	xmlhttp[i].onreadystatechange=function() {
	    if (xmlhttp[i].readyState==4){
			theDiv.innerHTML = xmlhttp[i].responseText;	
			if(i<calls.length-1){
				callMultiAjaxData(calls, i+1)
			}
	    }
	}
	xmlhttp[i].send(null);
}







function callAjaxData(BoxContainer, url, showWaitImg, someFunction){
	theDiv = document.getElementById(BoxContainer);
	if(showWaitImg){
		theDiv.innerHTML = showWaitMessage();
	}

	try{
	    xmlhttp = new XMLHttpRequest();
	}catch(ee){
	    try{
	        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	    }catch(e){
	        try{
	            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	        }catch(E){
	            xmlhttp = false;
	        }
	    }
	}
	xmlhttp.open("GET", url,true);
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4){
			theDiv.innerHTML = xmlhttp.responseText;	
			if(someFunction != 0){
				eval(someFunction);
			}
	    }
	}
	xmlhttp.send(null);
}

function montaQuery(formulario){
	querystring = "";
	for(i=0; i<document.forms[formulario].elements.length; i++){
		fieldtype = document.forms[formulario].elements[i].type;
		if(
			(fieldtype != 'checkbox' && fieldtype != 'radio' && fieldtype != 'button' && fieldtype != 'select-multiple') || 
			(fieldtype == 'checkbox' && document.forms[formulario].elements[i].checked) ||
			(fieldtype == 'radio' && document.forms[formulario].elements[i].checked) 
		){
			querystring += document.forms[formulario].elements[i].name+'='+escape(document.forms[formulario].elements[i].value);
			if(querystring.length > 0){
				querystring += "&";
			}
		}
		if(fieldtype == 'select-multiple'){
			for(j=0; j<document.forms[formulario].elements[i].options.length; j++){
				querystring += document.forms[formulario].elements[i].name+'='+escape(document.forms[formulario].elements[i].options[j].value);
				if(querystring.length > 0){
					querystring += "&";
				}
			}
		}
	}
	return querystring;
}

function callAjaxData2(BoxContainer, url){
//	theDiv = eval(BoxContainer);
	theDiv = document.getElementById(BoxContainer);
	theDiv.innerHTML = showWaitMessage();	
	params = url.substring(url.indexOf('?')+1, url.length);
	url = url.substring(0, url.indexOf('?'));
	params = params;
	try{
	    xmlhttp = new XMLHttpRequest();
	}catch(ee){
	    try{
	        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	    }catch(e){
	        try{
	            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	        }catch(E){
	            xmlhttp = false;
	        }
	    }
	}

	xmlhttp.open('POST', url, true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	
	xmlhttp.send(params);
	xmlhttp.onreadystatechange=function() {
	    if (xmlhttp.readyState==4){
			theDiv.innerHTML = xmlhttp.responseText;	
	    }
	}
}

// Usada para fazer o submit de um formulário via ajax. Monta a querystring através do parse dos 
// campos e faz a chamada da URL passando os valores por GET
// form_name: nome do formulário que se está manipulando
// containter: nome do div, layer, etc.. onde se deseja exibir o resultado do submit
// url: url de destino, seria o action do form
// some_funcion: função javascript a ser executada após a carga do conteúdo obtido via ajax
function submitForm(form_name, container, url, some_function){
	query_string = montaQuery(form_name);
	callAjaxData2(container,url+'?'+query_string,true,'');
}

function showWaitMessage(){
	txt = "<img src=/sistema/clientes/1/imagens/loading2.gif width=32 height=32 border=1 style=\"position:absolute; top:50%; left:30%;\">\n";
	return txt;
}

