//	********************************************************************************************************
//		class.exceptionHandler.js
//	********************************************************************************************************
//
//		CREATED FROM:	Amin Hatteh (idaSys)
//		CREATED AT:	18.08.2008
//		
//		VERSION 1.01
//		UPDATES: 28.08:08 - Remote Procedure Call hinzugefügt
 
//
//	--------------------------------------------------------------------------------------------------------

//private:
var objectOperator = null;
//class CExceptionHandler
//{
	function CExceptionHandler()
	{
		try
		{
			this.aktivate = declared_aktivate;
			this.setObjectOperator = declared_setObjectOperator;
			this.javascriptErrorHandler = declared_javascriptErrorHandler;
			this.formatedDateTime = declared_formatedDateTime;
			
			this.createRequestObject = declared_createRequestObject
			this.sendException = declared_sendException;
			this.handleResponse = declared_handleResponse;
			this.httpRequest = this.createRequestObject();

			this.isNumeric = declared_isNumeric;
			this.setEmailReciver = declared_setEmailReciver;
			this.emailReciver ="";
			
			//XML
			this.arrayToString = declared_arrayToString;
			this.createXmlNode = declared_createXmlNode;
			this.openXmlNode = declared_openXmlNode;
			this.closeXmlNode = declared_closeXmlNode;
			this.getXmlPrefix = declared_getXmlPrefix;
			this.xmlLevel = 0;
			
			//aktivieren
			this.aktivate();

		}
		catch(e)
		{
			//alert(e.message);
		}
	}
	
	function declared_aktivate()
	{
		// Fehler abfangen in Internet Explorer und kompatiblen
		window.onerror = this.javascriptErrorHandler;
		
		// Fehler abfangen in Netscape und kompatiblen
		window.onError = this.javascriptErrorHandler;
	}

	function declared_setObjectOperator(curObjectOperator)
	{
		objectOperator = curObjectOperator;
	}
	
	function declared_javascriptErrorHandler(errmsg, filename, linenum)
	{		
		try
		{								
			var err = objectOperator.openXmlNode('Javascript','','');
			err += objectOperator.createXmlNode("zeit", objectOperator.formatedDateTime('Y-m-d H:i:s'),'','');
			err += objectOperator.createXmlNode("errornum", "512",'','');
			err += objectOperator.createXmlNode("errortype", "Javascriptfehler",'','');
			err += objectOperator.createXmlNode("errormsg", errmsg,'','');
			err += objectOperator.createXmlNode("scriptname", filename,'','');
			err += objectOperator.createXmlNode("linenum", linenum,'','');				
			
			//Dateneinfügen			
			/*
			if(document.getElementsByTagName("body")[0])
			{
				err += objectOperator.openXmlNode('daten','','');
				err +=document.getElementsByTagName("body")[0].innerHTML;
				err += objectOperator.closeXmlNode('daten','','');	
			}
			*/
			
			objectOperator.sendException(encodeURI(err));
			
			return false;
		}
		catch(e)
		{
			//alert(e);
		}
	}	
			
	function declared_formatedDateTime(format) 
	{
		try
		{
			var now = new Date();		
			var returnStr = '';
			for (var i = 0; i < format.length; i++) 
			{
				switch(format.charAt(i))
				{
					case "d": 
						returnStr += (now.getDate() < 10 ? '0' : '') + now.getDate(); 
					break;
					case "m": 
						returnStr += (now.getMonth() < 11 ? '0' : '') + (now.getMonth() + 1); 
					break;
					case "Y": 
						returnStr += now.getFullYear(); 
					break;
					case "H": 
						returnStr += (now.getHours() < 10 ? '0' : '') + now.getHours(); 
					break;
					case "i": 
						returnStr += (now.getMinutes() < 10 ? '0' : '') + now.getMinutes(); 
					break;
					case "s": 
						returnStr += (now.getSeconds() < 10 ? '0' : '') + now.getSeconds(); 
					break;
					default: 
						returnStr += format.charAt(i);
				}
			}
			return returnStr;
		}
		catch(e)
		{
			//alert(e);
		}
	}
	
	function declared_setEmailReciver(emailReciver)
	{
		this.emailReciver = emailReciver;
	}
	
	function declared_isNumeric(sText)
	{
	   var validChars = "0123456789.";
	   for (i = 0; i < sText.length; i++) 
	   { 
	      if (validChars.indexOf(sText.charAt(i) == -1)) 
	      {
	         return false;
	      }
	   }
	   return true;
	   
	   }
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// Remote Procedure Call 
	///////////////////////////////////////////////////////////////////////////////////////////////
	function declared_createRequestObject() 
	{
	    var requestObject;
	    var browser = navigator.appName;
	    if(browser == "Microsoft Internet Explorer"){
	        requestObject = new ActiveXObject("Microsoft.XMLHTTP");
	    }else{
	        requestObject = new XMLHttpRequest();
	    }
	    return requestObject;
	}
	
	function declared_sendException(exception) 
	{
		var stEmailReciver = "";
		if(this.emailReciver)
		{
			stEmailReciver ='emailReciver=' + this.emailReciver + '&';
		}
	    this.httpRequest.open('get', 'includes/exceptionHandler/rpcException.php?' + stEmailReciver + 'exception=' + exception);
	    this.httpRequest.onreadystatechange = this.handleResponse;
	    this.httpRequest.send(null);
	}
	
	
	function declared_handleResponse() 
	{
	    if(objectOperator.httpRequest.readyState == 4)
	    {
	        var response = objectOperator.httpRequest.responseText;
	        //alert("exception Response = " + response);
	    }
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// XML Functionen
	///////////////////////////////////////////////////////////////////////////////////////////////	
	
	function declared_arrayToString(data)
	{
		var returnStr = "";
		//foreach(data as key => curData)
		for(var key=0; key < data.length; key++)
		{	
			curData = data[key];
			
			if( typeof( curData) == "object")
				returnStr +=  this.openXmlNode(key, "Array", curData.length) + this.arrayToString(curData) + this.closeXmlNode(key);
			else 
			{
				if(this.isNumeric(curData))
					returnStr += this.createXmlNode(key, curData, "Typ", "int");
				else 
					returnStr += this.createXmlNode(key, curData, "Typ", "str");
			}
		}	
		return returnStr;	
	}
	
	function declared_createXmlNode(key, keyValue, attribut, attributValue)
	{
		var xmlResult = this.openXmlNode(key, attribut, attributValue) + keyValue + "</" + key + ">";
		this.xmlLevel--;
		return (xmlResult);
	}
	
	function declared_openXmlNode(key, attribut, attributValue)
	{
		var xmlResult = this.getXmlPrefix() + "<" + key;
		if(attribut != "")
			xmlResult += " " + attribut + "='" + attributValue + "'";
			
		xmlResult += ">";	
		this.xmlLevel++;
		return (xmlResult);
	}
	
	function declared_closeXmlNode(key)
	{
		this.xmlLevel--;
		var xmlResult = this.getXmlPrefix() + "</" + key + ">";
		return (xmlResult);
	}
	
	function declared_getXmlPrefix()
	{
		var prefix ="\n";
		for(i = 0; i < this.xmlLevel; i++)
			prefix +="\t";
		return prefix;
	}
//}


