//verifica o browser
NS = (document.layers)? true:false;
IE = (document.all)? true:false;

var DivNameLoad='idCarregando'; /* identificação da DIV de loading */

function makeRequest(){
	var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }
    if (!http_request) {
        return false;
    }
    return http_request;
}//fim da function


/* **********************************************
*
*/

function ajaxChkForm(DivID, URLparam, StrParam, URLreturn){
    
    var xmlhttp=makeRequest();
	var metodo='GET';

	if(!StrParam){StrParam=null;}	
    else{metodo='POST';}
    
    if(xmlhttp===false){
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    
    xmlhttp.open(metodo,URLparam, true);
    xmlhttp.onreadystatechange = function(){
	    if (xmlhttp.readyState == 4 && xmlhttp.status==200) {
	        var resposta = xmlhttp.responseText;
	        
	        if(resposta){
				alert('Seu registro foi salvo com sucesso!');
	        	if(URLreturn){ajaxRespost(DivID,URLreturn);}
	        }else{
				alert('Ocorreu um erro, seu registro não pode ser salvo!\n\n'+resposta);
	        }
	    }//fim do if
	}//fim da function onreadystatechange
	
	xmlhttp.setRequestHeader('Content-type' , 'application/x-www-form-urlencoded');
  	xmlhttp.send(StrParam);
}//fim da function 

function ajaxCheck(DivID, URLparam, StrParam, URLreturn){
    
    var xmlhttp=makeRequest();
	var metodo='GET';

	if(!StrParam){StrParam=null;}	
    else{metodo='POST';}
    
    if(xmlhttp===false){
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    
    xmlhttp.open(metodo,URLparam, true);
    xmlhttp.onreadystatechange = function(){
	    if (xmlhttp.readyState == 4 && xmlhttp.status==200) {
	        var resposta = xmlhttp.responseText;
	        
	        if(resposta){
	        	alert('Ocorreu um erro, seu registro não pode ser salvo!\n\n'+resposta);
	        }else{
				alert('Seu registro foi salvo com sucesso!');
	        	if(URLreturn){ajaxRespost(DivID,URLreturn);}
	        }
	    }//fim do if
	}//fim da function onreadystatechange
	
	xmlhttp.setRequestHeader('Content-type' , 'application/x-www-form-urlencoded');
  	xmlhttp.send(StrParam);
}//fim da function 


/* ***********************************************************
*
*/
function ajaxRespost(DivID,URLparam,param){
    var xmlhttp=makeRequest();
	var metodo='GET';
	
	if(!param){param=null;}	
    else{metodo='POST';}
	
	if(param){metodo='POST';}
    
    if(xmlhttp===false){
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    xmlhttp.open(metodo,URLparam, true);
    xmlhttp.onreadystatechange = function(){
	    //cria uma div temporária no body do syte
		//if(xmlhttp.readyState==1){
		//	showLoad();
		//}//fim do if
	    
	    if (xmlhttp.readyState == 4 && xmlhttp.status==200) {
			// --- carraga o objt
	        $(DivID).innerHTML = xmlhttp.responseText;
	       
	        // --- exclui a div com mensagem na barra 'idCarregando' que foi criada --- // 
		   	// removeLoad();
		    
		    // --- posiciona no topo --- //
		    //location.href='index.php#topopg';
	    }//fim do if
	}//fim da function onreadystatechange
	
	xmlhttp.setRequestHeader('Content-type' , 'application/x-www-form-urlencoded');
  	xmlhttp.send(param);
}//fim da function 

/* ************************************
*
*/
function ajaxRespostValue(DivID,URLparam){
    
    var xmlhttp=makeRequest();
	var metodo='GET';
	var StrParam=null;	

    if(xmlhttp===false){
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    
    xmlhttp.open(metodo,URLparam, true);
    xmlhttp.onreadystatechange = function(){
	    if (xmlhttp.readyState == 4 && xmlhttp.status==200) {

	        if(DivID){$(DivID).value = xmlhttp.responseText;}

	    }//fim do if
	}//fim da function onreadystatechange
	
	xmlhttp.setRequestHeader('Content-type' , 'application/x-www-form-urlencoded');
  	xmlhttp.send(StrParam);
}//fim da function 

/* ******************************************************************
 *  
 * ******************************************************************/
function limpar_div(ID,Valuehtml){
  var InnerTxt=' ';	

	if(Valuehtml){
		var InnerTxt=Valuehtml;
	}	
	if($(ID)){$(ID).innerHTML=InnerTxt;}
	
}

function EnterSubmit(event){
    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    alert(keyCode);
    if ( keyCode == 13 ) {
       $('validateuser').submit();
    }
}

/* ******************************************************************
 * Imprime o conteudo de uma DIV 
 * ******************************************************************/
function imprimir(DivID){
	var a = window.open();
	a.document.open("text/html");
	a.document.write('<html><head>');
	a.document.write('<link rel="stylesheet" type="text/css" href="css/estilosprint.css">');
	a.document.write('<link rel="stylesheet" type="text/css" href="css/estilo_galeria.css">');
	a.document.write('<link rel="stylesheet" type="text/css" href="css/calendario.css">');
	a.document.write('</head><body>');
	a.document.write('<div id="container"><div id="reload">');
	a.document.write($(DivID).innerHTML);
	a.document.write('</div></div>');
	a.document.write('</body></html>');
	a.document.close();
	a.print();
}//fim do function

/* ******************************************************************
 * Imprime o conteudo de uma DIV 
 * ******************************************************************/
function enviaMail(DivID){
	if(!$('bnNomeRemetente').value){
		alert('Nome do remetente deve ser informado!');
		return false;
	}
	if(!checkMail($('bnEmailRemetente').value)){
		alert('Email do remetente deve ser informado ou não é válido!');
		return false;
	}
	if(!checkMail($('bnEmailDestinatario').value)){
		alert('Email do destinatário deve ser informado ou não é válido!');
		return false;
	}
	$('bnDivMsg').value=$(DivID).innerHTML;
	submit('sendmailto','../includes/sendmail.php',document.frmSend);
	alert('email enviado!');
	limpar_div('sendmailto');
	
}//fim do function

/* ******************************************************************
 * Valida email 
 * ******************************************************************/
function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
                    return true;
                }
    }else{
        return false;
        }
}

/* ************************************************************
 * fucao seleciona todos
 * ***********************************************************/
function CheckAll() {
	var val; 
	val = document.frm.checkall.checked; 
	dml = document.frm; 
	len = dml.elements.length; 
	var i=0;
	for(i=0;i<len;i++){
		dml.elements[i].checked=val;
	}
}

/** Cria uma div de loading como "Carregando ..." **/ 
function showLoad(){
	var body = $tag('body')[0];
	var div = document.createElement('div');
	div.setAttribute("id",DivNameLoad);
	body.appendChild(div);
	$(DivNameLoad).innerHTML="Carregando as informações, por favor aguarde ...";
};

/** Remove a div de loading **/ 
function removeLoad() {
	var body = $tag('body')[0];
	var div = $(DivNameLoad);
	if (div){
		body.removeChild(div);
		objDiv=null;
	}
};

function serialize(nv){
	var nv=function(e){
		if(e.name){
			return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);
		}else{ return '';}
	};
	var i=collect($tag('input'),function(i){
		if((i.type!='radio' && i.type!='checkbox') || i.checked) return nv(i);
	});
	var s=collect($tag('select'),nv);
	var t=collect($tag('textarea'),nv);
	return i.concat(s).concat(t).join('&');
};

/* fim da function serialize */ 
function submit(id,url,frm){
	ajaxRespost(id, url, serialize(frm));
};

function collect(a,f){
	var n=[];
	for (var i=0;i<a.length;i++){
		var v=f(a[i]);
		if(v!=null)n.push(v);
	}
	return n;
};

function $tag(e){
	if(typeof e=='string') {
		e=document.getElementsByTagName(e);
	}
	return e;
};

function $n(e){
	if(typeof e=='string') {
		e=document.getElementsByName(e);
	}
	return e;
};


function $(e){
	if(typeof e=='string') {
		e=document.getElementById(e);
	}
	return e;
};


function salvaAnexo(id){
	alert('Anexo salvo com Sucesso.');
	ajaxRespost('reload','includes/f2.php?y='+id);
};

