function isArray(o){
	return(typeof(o.length)=="undefined")?false:true;
}
String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/g, '');
}

function submeterContato(){

		
	document.getElementById('btcontatoform').onclick = function(){
		alert('Aguarde o fim do processamento (no máximo 30 segundos), se nada acontecer contate o suporte do site'); 
		return false;
	}
    $("#contatoform").ajaxSubmit({
 	   		
        target: '',
        url: './gestor/modulos/contato/?acao=set2', 
        type: 'post', 
         success: function(resposta) {
			res = (resposta.trim()).split('|');
			resformated = '';
			if(isArray(res)) {
				if(res.length<=1){ document.contatoform.reset(); }
				for(i=0;i<res.length;i++){
					resformated += res[i].trim()+'\n';
				}
			}else{
				resformated = resposta;
				document.contatoform.reset();
			}
    		alert(resformated);
			document.getElementById('btcontatoform').onclick = function(){
				submeterContato(); 
				return false;
			}
        }	
   			
    		
    });
	
}



function submeterProfessor(){
	

    $("#professor").ajaxSubmit({
 	   		
        target: '',
        url: './gestor/modulos/professor/?acao=set2', 
        type: 'post',
        success: function(resposta) {
			
		
   			if(resposta==1){
   				alert('Seu Cadastro foi Realizado com Sucesso! Aguarde &ccedil;&atilde;o.');
   				location.href="./?p=principal";
   			}else{
   				alert('Erro ao cadastrar, verifique se todos os campos estão preenchidos corretamente.');
   			}

        }
    		
    });
	
}



function submeterNews(){
	

    $("#formNewsCliente").ajaxSubmit({
 	   		
        target: '',
        url: './gestor/modulos/newsletter_emails/?acao=set2', 
        type: 'post', 
        success: function(resposta) {
			res = (resposta.trim()).split('|');
			resformated = '';
			if(isArray(res)) {
				if(res.length<=1){ document.formNewsCliente.reset(); }
				for(i=0;i<res.length;i++){
					resformated += res[i].trim()+'\n';
				}
			}else{
				resformated = resposta;
				document.formNewsCliente.reset();
			}
    		alert(resformated);
        }
    		
    });
	
}


////////Funções do ChatOnline //////////

function replaceAll(str, de, para){
    var pos = str.indexOf(de);
    while (pos > -1){
		str = str.replace(de, para);
		pos = str.indexOf(de);
	}
    return (str);
}

//////// Funções do Chat //////////
function submeterFormChat(campoConteudo,form,divChat,chamado,URLFORM,URL_LISTE_CHAT){
	
	if(atualizar==1){
		if(document.getElementById(campoConteudo).value!=''){
		   
		    $(form).ajaxSubmit({
		 	   		
		        target: '',
		        url: URLFORM, 
		        type: 'post', 
		        success: function(resposta) {
		    		
		    		document.getElementById(campoConteudo).value='';
		    	
			   		resposta=replaceAll(resposta,"&amp;","&");
					
					resp=resposta.split(',,');
		
					if(resp[1]=='respAjax'){
						mostraResposta(resp[0]);
						atualizaChat(divChat,chamado,URL_LISTE_CHAT);

		            }
		        }
		    
		    		
		    });
		    
		}else{
			alert('Mensagem Inválida');
		}
	}
    return false;

}

function submeterMsgOff(){
	
    $("#formOff").ajaxSubmit({
	   		
        target: '',
        url: "../../gestor/modulos/contato/?acao=set2", 
        type: 'post', 
        success: function(resposta) {
    		if(resposta==1){
    			alert("Mensagem enviada com sucesso");
    			document.getElementById("formOff").reset();
    		}else{
    			alert("Preencha os campos corretamente");
    		}
        }
    
    		
    });
    
	return false;
	
}


function atualizaChat(_divChat,_chamado,_URL_LISTE_CHAT){
	
	if(atualizar==1){
	
	$.ajax({
		   type: "GET",
		   url: _URL_LISTE_CHAT+"&proxID="+proxID+"&chamado="+_chamado,
		   success: function(conteudoLista){
		
		   		conteudoLista=conteudoLista.split('|');
		   		
		   		if(conteudoLista[0]>1){
		   			
		   			proxID=conteudoLista[0];
   		
					descer=false;
				    if(scrollTop==document.getElementById(_divChat).scrollTop){
				    	descer=true;
					}
				    $("#"+_divChat).append(conteudoLista[1]);

				    if(descer==true){
				    	document.getElementById(_divChat).scrollTop=1000000;
				    	scrollTop=document.getElementById(_divChat).scrollTop;
				    }else{
				    	ST=document.getElementById(_divChat).scrollTop;
				    	document.getElementById(_divChat).scrollTop=1000000;
				    	scrollTop=document.getElementById(_divChat).scrollTop;
				    	document.getElementById(_divChat).scrollTop=ST;
					}
			    
		   		}
		   }
		});
	
	}
	
}
//////////////////////////////////


function finalizarChat(_divChat,_chamado){
	

	if(atualizar==1){
		$.ajax({
 			   type: "GET",
 			   url: "./gestor/suportes/atualizarChamado.php?status=f&cod="+_chamado,
 			   success: function(res2){

		   			atualizar=0;
		   			
					descer=false;
				    if(scrollTop==document.getElementById(_divChat).scrollTop){
				    	descer=true;
					}
				    $("#"+_divChat).append("<br/><center><strong style='color:#f00'>Chat Finalizado!</strong></center>");

				    if(descer==true){
				    	document.getElementById(_divChat).scrollTop=1000000;
				    	scrollTop=document.getElementById(_divChat).scrollTop;
				    }else{
				    	ST=document.getElementById(_divChat).scrollTop;
				    	document.getElementById(_divChat).scrollTop=1000000;
				    	scrollTop=document.getElementById(_divChat).scrollTop;
				    	document.getElementById(_divChat).scrollTop=ST;
					}
   				
   				
 			   }
 		
 		});
	
	}
	return false;
	
}


function getAtivo(URL,_divChat,_chamado){
	
	if(atualizar==1){
	
		$.ajax({
			   type: "GET",
			   url: URL,
			   success: function(res){
			
			   		if(res==1){

			   			$.ajax({
				 			   type: "GET",
				 			   url: "../../gestor/suportes/getStatusChamado.php?cod="+_chamado,
				 			   success: function(res3){
			   						
			   						if(trim(res3)!='a'){
			   							
			   							if(trim(res3)=='u'){
			   								msgFinal='Usuário Inativo';
			   							}
			   							if(trim(res3)=='i'){
			   								msgFinal='Atendente Inativo';
			   							}
			   							if(trim(res3)=='f'){
			   								msgFinal='Usuário finalizou o Chat';
			   							}
			   							if(trim(res3)=='t'){
			   								msgFinal='Atendente finalizou o Chat';
			   							}

							   			atualizar=0;
							   			
										descer=false;
									    if(scrollTop==document.getElementById(_divChat).scrollTop){
									    	descer=true;
										}
									    $("#"+_divChat).append("<br/><center><strong style='color:#f00'>"+msgFinal+"</strong></center>");
					
									    if(descer==true){
									    	document.getElementById(_divChat).scrollTop=1000000;
									    	scrollTop=document.getElementById(_divChat).scrollTop;
									    }else{
									    	ST=document.getElementById(_divChat).scrollTop;
									    	document.getElementById(_divChat).scrollTop=1000000;
									    	scrollTop=document.getElementById(_divChat).scrollTop;
									    	document.getElementById(_divChat).scrollTop=ST;
										}
										
			   						}

				 			   }
				 		
				 			});
			   			
			   			
			   		}else{
			   			
			   			$.ajax({
				 			   type: "GET",
				 			   url: "../../gestor/suportes/getStatusChamado.php?cod="+_chamado,
				 			   success: function(res3){
			   				
			   						if(trim(res3)=='a'){		
			   			
							   			$.ajax({
							 			   type: "GET",
							 			   url: "../../gestor/suportes/atualizarChamado.php?status=i&cod="+_chamado,
							 			   success: function(res2){
							   				}
								 		
							 			});	

			   						}
			   						
			   						atualizar=0;
						   			
									descer=false;
								    if(scrollTop==document.getElementById(_divChat).scrollTop){
								    	descer=true;
									}
								    $("#"+_divChat).append("<br/><center><strong style='color:#f00'>Atendente inativo</strong></center>");
				
								    if(descer==true){
								    	document.getElementById(_divChat).scrollTop=1000000;
								    	scrollTop=document.getElementById(_divChat).scrollTop;
								    }else{
								    	ST=document.getElementById(_divChat).scrollTop;
								    	document.getElementById(_divChat).scrollTop=1000000;
								    	scrollTop=document.getElementById(_divChat).scrollTop;
								    	document.getElementById(_divChat).scrollTop=ST;
									} 
			   						
						 		}
							 		
			 			});
					    
			   		}
			   }
		
		});
	
	}
	
}

//////////////////////////////////

function iniciarChat(){
		
    $("#formChat").ajaxSubmit({

    	target: '',
        url: './gestor/modulos/usuario/?acao=set', 
        type: 'post', 
        success: function(resposta) {
    		//alert(resposta);
 	   		resp=resposta.split(',,');

 	   		if(resp[0]==0){
 	   			alert("Dados incorretos ou Departamento inativo no momento. Tente Novamente mais tarde!");
 	   		}else{
 	   			location.href='chat.php';
 	   		}
        }

    });

    return false;
}
