var fetchIntervall;
var fetchStatusIntervall;
var i=0;
var messageBoxCache = "";
var jabberUrl = "http://www.agency4net.de/include/jabber/";

/*
check Status
*/
function fetchStatus(){
	ajaxQuestionStatus(jabberUrl+"jabber.php?action=status","fetch");
}

if(location.href=="http://www.agency4net.de/"){
	fetchStatusIntervall = window.setInterval("fetchStatus()",5000);
}

/*
create Temp Username
*/
function preGenerate(){
	var now = new Date();
	var userOne = now.getTime();
	var userOne = (userOne.toString(16));
	//var userName= userOne+generatePassword(10);
	return userOne+generatePassword(10);
}

var userName = preGenerate();

function generatePassword(stringLenght)
{
	var keylist="abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var userAdd='';
	for(i=0;i<stringLenght;i++)
	{
		userAdd+=keylist.charAt(Math.floor(Math.random()*keylist.length));
	}
	return userAdd;
}

function fetchMessage(){
	if(document.getElementById("indicator").innerHTML==""){
		ajaxQuestionFetch(jabberUrl+"jabber.php?action=fetch&username="+userName,"fetch");
	}
}

function clearMessageBox()
{
	document.getElementById("messageBox").innerHTML="";
	document.getElementById("indicator").innerHTML="";
}

function setFetchIntervall()
{
	window.clearInterval(fetchStatusIntervall);	
	ajaxQuestionSendChat(jabberUrl+"jabber.php?action=register&username="+userName,"send");
	fetchIntervall = window.setInterval("fetchMessage()",2000);
	document.getElementById('supportInfo').style.display = 'none';
	document.getElementById('supportInfo2').style.display = 'none';
	document.getElementById('supportChat').style.display = 'block';
}

function stopFetchIntervall()
{
	window.clearInterval(fetchIntervall);
	fetchStatusIntervall = window.setInterval("fetchStatus()",5000);
	document.getElementById('supportInfo').style.display = 'block';
	document.getElementById('supportInfo2').style.display = 'none';
	document.getElementById('supportChat').style.display = 'none';	
}

function getTimeString()
{
	var nowTimeObject = new Date();
	var TimeOutput = '';
	
    
  	if (nowTimeObject.getHours() / 10 <1 )
  	{
  		TimeOutput += '0'+nowTimeObject.getHours()+':';
  	}
  	else
  	{
  		TimeOutput += nowTimeObject.getHours()+':';
  	}
  	
  	if (nowTimeObject.getMinutes() / 10 <1 )
  	{
  		TimeOutput += '0'+nowTimeObject.getMinutes()+':';
  	}
  	else
  	{
  		TimeOutput += nowTimeObject.getMinutes()+':';
  	}
  	
  	if (nowTimeObject.getSeconds() / 10 <1 )
  	{
  		TimeOutput += '0'+nowTimeObject.getSeconds();
  	}
  	else
  	{
  		TimeOutput += nowTimeObject.getSeconds();
  	} 	  	
  	
  	return TimeOutput;  	  	
}

function sendMessage(colorHex)
{
	var jetzt = new Date();							
	messageBoxCache = document.getElementById("messageBox").innerHTML;	
	document.getElementById("messageBox").innerHTML = "<div style=\"border:1px solid #cccccc;padding:5px;margin:5px;background-color:#ffffff;\"><div style=\"color:#"+colorHex+";border-bottom:1px solid #cccccc;\"><strong>Gast</strong> <small>"+getTimeString()+"</small></div><div>"+nl2br(document.getElementById("messageInput").value)+"</div></div>"+messageBoxCache;
	ajaxQuestionSendChat(jabberUrl+"jabber.php?action=send&username="+userName+"&message="+nl2brUrl(document.getElementById("messageInput").value),"send");
	document.getElementById("messageInput").value="";
}

function nl2br(str) {
	if(typeof(str)=="string") return str.replace(/(\r\n)|(\n\r)|\r|\n/g,"<br />");
	else return str;
}

function nl2brUrl(str) {
	if(typeof(str)=="string") return str.replace(/(\r\n)|(\n\r)|\r|\n/g," ");
	else return str;
}

/*
Ajax Fetch
*/
function ajaxQuestionFetch(putUrl,putArt){  			
		if(navigator.appName.search("Microsoft") > -1){
		    ajaxReplacedFetch = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
		    ajaxReplacedFetch = new XMLHttpRequest();
		}    	    	    	    
	    ajaxReplacedFetch.open('get',putUrl+'&refresh='+preGenerate(),true);        	    	    
		document.getElementById("indicator").innerHTML = "<img src=\"empty.gif\" alt=\"loading...\" />";
		ajaxReplacedFetch.onreadystatechange = ajaxResponseFetch;
	    ajaxReplacedFetch.send(null);   
}	

function ajaxResponseFetch(){
    if(ajaxReplacedFetch.readyState==4){
    	if(ajaxReplacedFetch.status == 200){
	        ajaxResponseValueFetch = ajaxReplacedFetch.responseText;  
	        if(ajaxResponseValueFetch!=''){
				var messageBoxCacheFetch = document.getElementById("messageBox").innerHTML;	
				document.getElementById("messageBox").innerHTML = ajaxResponseValueFetch+messageBoxCacheFetch;			
			}
			document.getElementById("indicator").innerHTML="";
		}
    }
}

/*
Ajax Send
*/
function ajaxQuestionSendChat(putUrl,putArt){  			
		if(navigator.appName.search("Microsoft") > -1){
		    ajaxReplacedSendChat = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
		    ajaxReplacedSendChat = new XMLHttpRequest();
		}    	    	    	    
	    ajaxReplacedSendChat.open('get',putUrl+'&refresh='+preGenerate(),true);        	    	    
		ajaxReplacedSendChat.onreadystatechange = ajaxResponseSendChat;
	    ajaxReplacedSendChat.send(null);   
}	

function ajaxResponseSendChat(){
    if(ajaxReplacedSendChat.readyState==4){
    	if(ajaxReplacedSendChat.status == 200){
    	}
    }
}

/*
Ajax Status
*/
function ajaxQuestionStatus(putUrl,putArt){  			
		if(navigator.appName.search("Microsoft") > -1){
		    ajaxReplacedStatus = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
		    ajaxReplacedStatus = new XMLHttpRequest();
		}    	    	    	    
	    ajaxReplacedStatus.open('get',putUrl+'&refresh='+preGenerate(),true);        	    	    
		ajaxReplacedStatus.onreadystatechange = ajaxResponseStatus;
	    ajaxReplacedStatus.send(null);   
}	

function ajaxResponseStatus(){
    if(ajaxReplacedStatus.readyState==4){
    	if(ajaxReplacedStatus.status == 200){
	        ajaxResponseValueStatus = ajaxReplacedStatus.responseText;  
	        if(ajaxResponseValueStatus=='online'){
				document.getElementById('supportInfo').style.display = 'none';
				document.getElementById('supportInfo2').style.display = 'block';
				document.getElementById('supportChat').style.display = 'none';
			}
			else{
				document.getElementById('supportInfo').style.display = 'block';
				document.getElementById('supportInfo2').style.display = 'none';
				document.getElementById('supportChat').style.display = 'none';			
			}
			ajaxResponseValueStatus = '';
    	}
    }
}