function $(id)
{
	return document.getElementById(id);
}

function _onResponseStateChange(call)
{
	var xReq = call.xReq;
	
    if (xReq.readyState != 4) 
	{ 
      return;
    }

    if (xReq.readyState == 4) 
	{ 
      if(xReq.status == 200)
	  {
		  var callbackFunction = call.callbackFunction;
	      if (!callbackFunction) 
		  {
	        setTimeout(function() {
	          _onResponseStateChange(call);
	        }, 100);
	      }
	      var content = xReq.responseText;	
		  if(call.requestMethod == "POST")
		  {
		  	var cntArr = content.split("%@@%");
			
			if(cntArr.length > 0)
				callbackFunction(cntArr[0]);
			else
				callbackFunction(content);
		  }else
		  {
		  	callbackFunction(content);
		  }		  	      	      
		  call = null;
	  }
    }
}

Humax.declareNamespace("HxLib");

HxLib.WebRequest = function(){}

HxLib.WebRequest.prototype = 
{	
	get : function(url, callbackFunction)
	{
		this._requestServer(url, "GET", null, callbackFunction);
	},
	
	post : function(url, args, callbackFunction)
	{
		this._requestServer(url, "POST", args, callbackFunction);
	},
	
	formToString : function(formID)
	{
		var str = "";
		for(var i =0; i < document.forms[formID].elements.length; i++)
		{
			str += document.forms[formID].elements[i].id + "=" + escape(document.forms[formID].elements[i].value) + "&";
		}
		return str;
	},
	
	_createXmlHttpRequest : function()
	{
		try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
		try { return new XMLHttpRequest(); } catch(e) {}
		alert("XMLHttpRequest not supported");
		return null;
	},		
	
	_requestServer : function(url, requestMethod, args, callbackFunction)
	{
		var xReq = this._createXmlHttpRequest();
		xReq.onreadystatechange = function() {
		  _onResponseStateChange(call);
		}
		
		var call = {xReq: xReq,
		            callbackFunction: callbackFunction,		            
		            url: url,
					requestMethod : requestMethod};				
		
		xReq.open(requestMethod, url, true);
		
		if(requestMethod == "GET")
		{
			xReq.send(null);
		}else if(requestMethod == "POST")
		{			
			xReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");			
			xReq.send(args);
		}
	}			
}

Humax.declareClass("HxLib.WebRequest", HxLib.WebRequest);

HxLib.VAlign = 
{	
	Top : "top",
    Middle : "middle",
    Bottom : "bottom"
}

HxLib.HAlign = 
{	
	Left : "left",
    Center : "center",
    Right : "right"
}

HxLib.WebControl = function() 
{
	this.$styleText = "";
	this.$styleClass = "";
}

HxLib.WebControl.prototype = 
{
	$styleText : "",
	$styleClass : "",
	
	render : function(){},
	
	getStyleText : function(){return this.$styleText;},
	setStyleText : function(styleText) {this.$styleText = styleText;},
	
	getStyleClass : function() {return this.$styleClass;},
	setStyleClass : function(styleClass) {this.$styleClass = styleClass;}	
}

Humax.declareClass("HxLib.WebControl", HxLib.WebControl);

HxLib.Cell = function(text, styleClass, styleText, span) 
{	
	if(text) this._text = text;
	if(styleClass) this.$styleClass = styleClass;
	if(styleText) this.$styleText = styleText;
	if(span) this._span = parseInt(span);
	this.$styleText = null;		
}

HxLib.Cell.prototype = 
{
	_text : "",
	_hAlign : HxLib.HAlign.Left,
	_vAlign : HxLib.HAlign.Right,
	_span : 1,
	
	render : function()
	{
		var toHtml = new String("<td");
		if(this.$styleClass) toHtml += " class='" + this.$styleClass + "'";		
		if(this.$styleText) toHtml += " style='" + this.$styleText + "'";
		if(this._span > 1) toHtml += " colspan='" + this._span + "'";
		if(this._text == "") this._text = "&nbsp;";
		toHtml += ">" + this._text + "</td>";
		
		return toHtml;
	},
	
	getText : function() {return this._text;},
	setText : function(text) {this._text = text;},
	
	getHAlign : function() {return this._hAlign;},
	setHAlign : function(hAlign) { this._hAlign = hAlign;},
	
	getVAlign : function() {return this._vAlign;},
	setVAlign : function(vAlign) { this._vAlign = vAlign;},
	
	getSpan : function() {return this._span;},
	setSpan : function(span) { this._span = span;}
}

Humax.declareClass("HxLib.Cell", HxLib.Cell, HxLib.WebControl);

HxLib.NullCell = function(){}

HxLib.NullCell.prototype = 
{
	render : function() {return ""}
}

Humax.declareClass("HxLib.NullCell", HxLib.NullCell, HxLib.Cell);

HxLib.GridRow = function(styleClass, hAlign, vAlign, span) 
{
	if(styleClass) this.$styleClass = styleClass;
	if(hAlign) this._hAlign = hAlign;
	if(vAlign) this._vAlign = vAlign;
	if(span) this._span = parseInt(span);
	this.$styleText = null;
	this.cells = [];
}

HxLib.GridRow.prototype = 
{
	cells : null,
	_text : "",
	_hAlign : HxLib.HAlign.Left,
	_vAlign : HxLib.HAlign.Right,
	_span : 1,
	
	render : function()
	{
		var toHtml = new String("<tr");
		if(this.$styleClass) toHtml += " class='" + this.$styleClass + "'";
		if(this.$styleText) toHtml += " style='" + this.$styleText + "'";
		toHtml += ">";
		
		if(this._span > 1) toHtml += "<th rowSpan='" + this._span + "'>" + this._text + "</th>";
		
		for(var i = 0; i < this.cells.length; i++)
		{
			toHtml += this.cells[i].render();
		}		
		
		toHtml += "</tr>";
		
		return toHtml;
	},
	
	fillNullCell : function(columnCount)
	{
		for(var i = 0; i < columnCount; i++)
			this.cells.push(new HxLib.NullCell());
	},
	
	fillNullByEmptyCell : function(emptyStyle)
	{
		for(var i = 0; i < this.cells.length; i++)		
		{
			if(this.cells[i].getType().is("HxLib.NullCell"))
			{
				if(i == 0) 
				{ 
				    break;
				    //this.cells[i] = new HxLib.Cell("","",emptyStyle,1); continue;
				}
				var cellSpan = this.cells[i-1].getSpan();				
				if(cellSpan > 1){i = (i-1) + cellSpan; continue;}
				this.cells[i] = new HxLib.Cell("","",emptyStyle,1);
			}						
		}
	},
	
	getText : function() {return this._text;},
	setText : function(text) {this._text = text;},
	
	getHAlign : function() {return this._hAlign;},
	setHAlign : function(hAlign) { this._hAlign = hAlign;},
	
	getVAlign : function() {return this._vAlign;},
	setVAlign : function(vAlign) { this._vAlign = vAlign;},
	
	getSpan : function() {return this._span;},
	setSpan : function(span) { this._span = span;}
}

Humax.declareClass("HxLib.GridRow", HxLib.GridRow, HxLib.WebControl);

HxLib.HtmlGrid = function(styleClass, borderWidth, cellSpacing, cellPadding)
{
	this.rows = [];
	if(styleClass) this.$styleClass = styleClass;
	if(borderWidth) this._borderWidth = parseInt(borderWidth);
	if(cellSpacing) this._cellSpacing = parseInt(cellSpacing);
	if(cellPadding) this._cellPadding = parseInt(cellPadding);	
}

HxLib.HtmlGrid.prototype = 
{
	rows : null,
	_borderWidth : 0,
	_cellSpacing : 0,
	_cellPadding : 0,
	_columnCount : 0,
	_enableNullFiller : false,
	_emptyCellStyle : "",
	
	render : function()
	{
		if(this._enableNullFiller)
		{
			for(var i = 0; i < this.rows.length; i++)
			{
				var rowSpan = this.rows[i].getSpan();
				if(rowSpan > 1)
				{
				    i += rowSpan; continue;
				}
				this.rows[i].fillNullByEmptyCell(this._emptyCellStyle);
			}			
		}
		
		var toHtml = new String("<table");
		toHtml += " border='" + this._borderWidth + "'";
		if(this.$styleClass) toHtml += " class='" + this.$styleClass + "'";
		if(this.$styleText) toHtml += " style='" + this.$styleText + "'";
		toHtml += " cellSpacing='" + this._cellSpacing + "'";
		toHtml += " cellPadding='" + this._cellPadding + "'";
		toHtml += ">";				
				
		for(var i = 0; i < this.rows.length; i++)
		{			
			toHtml += this.rows[i].render();
		}
		
		toHtml += "</table>"
		
		return toHtml;
	},
	
	newRow : function(styleClass, hAlign, vAlign, span)
	{
		var gr = null;
		
		if(styleClass && hAlign && vAlign && span) 
			gr = new HxLib.GridRow(styleClass, hAlign, vAlign, span);
		else
			gr = new HxLib.GridRow();
			
		gr.fillNullCell(this._columnCount);
		return gr;
	},	
	
	getBorderWidth : function(){return this._borderWidth;},
	setBorderWidth : function(borderWidth) {this._borderWidth = borderWidth;},
	
	getCellSpacing : function() {return this._cellSpacing;},
	setCellSpacing : function(cellSpacing) {this._cellSpacing = cellSpacing;},
	
	getCellPadding : function() {return this._cellPadding;},
	setCellPadding : function(cellPadding) {this._cellPadding = cellPadding;},
	
	getColumnCount : function() {return this._columnCount;},
	setColumnCount : function(columnCount) {this._columnCount = columnCount;},
	
	getEnableNullFiller : function(){return this._enableNullFiller;},
	setEnableNullFiller : function(enable){this._enableNullFiller = enable;},
	
	getEmptyCellStyle : function(){return this._emptyCellStyle;},
	setEmptyCellStyle : function(emptyCellStyle){this._emptyCellStyle = emptyCellStyle;}
}

Humax.declareClass("HxLib.HtmlGrid", HxLib.HtmlGrid, HxLib.WebControl);

HxLib.WindowEx = function() {}

HxLib.WindowEx.getWindowInnerWidth = function()
{
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
  {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
  {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;

}

HxLib.WindowEx.getWindowInnerHeight = function()
{
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
  {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
  {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

Humax.declareClass("HxLib.WindowEx",HxLib.WindowEx);