/**
 * funcoes de ajax
 *
 * rotinas de controle de ajax
 *
 * @package     Biblioteca
 * @subpackage  Javascript
 * @creation    2006/05/03
 * @copyright   NetMake Solucoes em Informatica
 * @author      Diogo Silva Toscano De Brito <diogo@netmake.com.br>
 *
 * $Id: ajax.js,v 1.1.1.1 2009-11-16 20:50:31 diogo Exp $
 */

function getDataAjax(url, str_func_retorno)
{
	xmlhttp = getXmlHttp(); 
    xmlhttp.open('GET', url, true);  
    xmlhttp.onreadystatechange=function() { 
      if (xmlhttp.readyState==4){  
      	  if(str_func_retorno)
      	  {
      	  	retornoText = xmlhttp.responseText;
      	  	
      	  	if (retornoText.indexOf('<!-- SESSAO INSPIROU -->') > -1)
      	  	{
      	  		form_logout = getFormLogout(window);
      	  		form_logout.submit();
      	  	}
      	  	else
      	  	{
      	  		str_func_retorno(retornoText);
      	  	}	
      	  }      	  
      } 
    }
    xmlhttp.send(null); 
}

function postDataAjax(url, str, str_func_retorno, str_html)
{
	//protege caracteres especiais
	if(str_html && str_html == 'S')
	{
		str_param = str;
	}else
	{
		str_param = "";
		if(str.indexOf('&'))
		{
			arr_param = str.split('&');
			
			for(it=0; it<arr_param.length; it++)
			{
				if(arr_param[it].indexOf('='))
				{
					arr_valor = arr_param[it].split('=');
					
					str_variavel = "";
					if(arr_valor[0])
					{
						str_variavel = arr_valor[0];
					}
					
					str_valor = "";
					if(arr_valor[1])
					{
						str_valor = arr_valor[1];
					}
					
					if(str_param != '')
					{
						str_param += '&';
					}
					
					str_param += str_variavel + '=' + escape(str_valor);
				}
			}
		}else
		{
			arr_valor = str.split('=');
					
			if(str_param != '')
			{
				str_param += '&';
			}
			
			str_param += arr_valor[0] + '=' + escape(arr_valor[1]);
		}
	}
	
	xmlhttp = getXmlHttp(); 
	xmlhttp.open('POST', url, true);
	//xmlhttp.setRequestHeader("Referer"     , document.referrer);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	//xmlhttp.setRequestHeader("User-Agent"  , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
	//xmlhttp.setRequestHeader("Pragma"      , "no-cache");
	xmlhttp.send(str_param);
	xmlhttp.onreadystatechange=function() { 
      if (xmlhttp.readyState==4){  
      	  if(str_func_retorno)
      	  {
      	  	str_func_retorno(xmlhttp.responseText);
      	  }      	  
      } 
    }
}


function getXmlHttp() {  
    try {  
        xmlhttp = new XMLHttpRequest();  
    } catch(e1) { 
        try { 
    	    xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); 
            }catch(e2) { 
    	try {  
    	    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');  
    	}catch(e3){ 
                xmlhttp = false;  
    	}  
       } 
    }  
    return xmlhttp; 
} 


function getFormLogout(win)
{
	frm = null;
	
	for (ifr = 0;  ifr < win.document.frames.length; ifr++)
	{
		
		if (win.document.frames.item(ifr).name == "nmFrmBot")
		{	
			frm = win.document.form_logout;
			break;
		}
	}
	
	if (frm == null)
	{
		return getFormLogout(win.parent);		
	}
	else
	{
		return frm;
	}
}