var isFirefox;
var isSafari;
var isSafari3;
var isOpera;
var isMozilla;
var isIE;
var isIE7;
var isIE6;

var navig =  navigator.userAgent.toLowerCase();
isFirefox = (navig.indexOf("firefox")>=0 || navigator.userAgent.indexOf("iceweasel")>=0);
isSafari = (navig.indexOf("safari")>=0);
isSafari3 = (navig.indexOf("safari")>=0 && navigator.userAgent.indexOf("version/3")>0);
isOpera = (!isIE && (navig.indexOf("opera")>=0));
isMozilla = (!isIE && !isFirefox && !isSafari && !isOpera && (navig.indexOf("mozilla")>=0));
isIE = navig.indexOf("msie") != -1;
    
if(navigator.userAgent.indexOf("MSIE 7") != -1) 
	isIE7 = true; 
else if(navigator.userAgent.indexOf("MSIE 6") != -1) 
	isIE6 = true;

// ---------------------------------------- Command -----------------------------------
var Command = function()
{
	this.name = "";
	this.type = "";
	this.parameters = new Object();
	this.module = null;
}

Command.getAvailableCommands = function()
{
	var availableCommands = new Array();
	
	return availableCommands;
}

// ------------------------------------------ ArrayList class ----------------------------------------------------------------

function ArrayList()
{
   this.aList = []; //initialize with an empty array
}
        
ArrayList.prototype.Count = function()
{
   return this.aList.length;
}

ArrayList.prototype.GetLength = function()
{
   return this.aList.length;
}
        
ArrayList.prototype.Add = function( object )
{
   //Object are placed at the end of the array
   return this.aList.push( object );
}

ArrayList.prototype.GetAt = function( index ) //Index must be a number
{
   if( index > -1 && index < this.aList.length )
      return this.aList[index];
   else
      return undefined; //Out of bound array, return undefined
}
        
ArrayList.prototype.Clear = function()
{
   this.aList = [];
}

ArrayList.prototype.RemoveAt = function ( index ) // index must be a number
{
   var count = this.aList.length;
            
   if ( count > 0 && index > -1 && index < this.aList.length ) 
   {
      switch( index )
      {
         case 0:
            this.aList.shift();
            break;
         case count - 1:
            this.aList.pop();
            break;
         default:
            var head   = this.aList.slice( 0, index );
            var tail   = this.aList.slice( index + 1 );
            this.aList = head.concat( tail );
            break;
      }
   }
}

ArrayList.prototype.Remove = function ( object ) // index must be a number
{
   var index = this.IndexOf(object, 0);
   var count = this.aList.length;
            
   if ( count > 0 && index > -1 && index < this.aList.length ) 
   {
      switch( index )
      {
         case 0:
            this.aList.shift();
            break;
         case count - 1:
            this.aList.pop();
            break;
         default:
            var head   = this.aList.slice( 0, index );
            var tail   = this.aList.slice( index + 1 );
            this.aList = head.concat( tail );
            break;
      }
   }
}

ArrayList.prototype.Insert = function ( object, index )
{
   var count       = this.aList.length;
   var returnValue = -1;

   if ( index > -1 && index <= count ) 
   {
      switch(index)
      {
         case 0:
            this.aList.unshift(object);
            returnValue = 0;
            break;
         case count:
            this.aList.push(object);
            returnValue = count;
            break;
         default:
            var head      = this.aList.slice(0, index - 1);
            var tail      = this.aList.slice(index);
            this.aList    = this.aList.concat(tail.unshift(object));
            returnValue = index;
            break;
      }
   }
            
   return returnValue;
}

ArrayList.prototype.IndexOf = function( object, startIndex )
{
   var count       = this.aList.length;
   var returnValue = -1;
            
   if ( startIndex > -1 && startIndex < count ) 
   {
      var i = startIndex;

      while( i < count )
      {
         if ( this.aList[i] == object )
         {
            returnValue = i;
            break;
         }
                    
         i++;
      }
   }
            
   return returnValue;
}

ArrayList.prototype.LastIndexOf = function( object, startIndex )
{
   var count       = this.aList.length;
   var returnValue = - 1;
            
   if ( startIndex > -1 && startIndex < count ) 
   {
      var i = count - 1;
                
      while( i >= startIndex )
      {
         if ( this.aList[i] == object )
         {
            returnValue = i;
            break;
         }
                    
         i--;
      }
   }
            
   return returnValue;
}

var requestInfos = new ArrayList();
// ----------------------------------- ServerRequest ------------------------------- 
var ServerRequest = function(serverUrl)
{
	this.serverUrl = serverUrl;
} 

ServerRequest.prototype.onServerRequestFinished = function(commands, responseXML)
{
}

ServerRequest.prototype.run = function(commands)
{
	var XHR = null; 
	     
    if(window.XMLHttpRequest) // Firefox 
    {
        XHR = new XMLHttpRequest(); 
    }
    else if(window.ActiveXObject) // Internet Explorer 
    {
        XHR = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else 
    {
        alert("XMLHTTPRequest not suppoted"); 
        return; 
    }
    
    this.XHR = XHR;
    
	if (this.XHR)
    {
    	var XHR = this.XHR; 
 			
 			var requestInfo = new Object();
 			requestInfo.XHR = XHR;
 			requestInfo.serverRequest = this;
 			requestInfo.commands = commands;
 			
 			requestInfos.Add(requestInfo);
 			
   // 	XHR.ServerRequest = this;
   // 	XHR.commands = commands;
    	
        XHR.onreadystatechange = function()
		{
			 if(XHR.readyState == 4)
		     {
		        if(XHR.status == 200) 
		        {
		        	var requestInfo = null;
		        	for (var index = 0; index < requestInfos.GetLength(); index++)
		        	{
		        		requestInfo = requestInfos.GetAt(index);
		        		if (requestInfo.XHR == XHR)
		        		{
		        			requestInfos.Remove(requestInfo);
		        			break;
		        		}
		        	}
									        	
		        	var responseXML = clean(XHR.responseXML.documentElement);

		          	requestInfo.serverRequest.onServerRequestFinished(requestInfo.commands, responseXML);
		        }
		        else 
		            alert("Error code " + XHR.status);
		     }
		};
 
 		var query = "";   
    	for(var index = 0; index < commands.length; index++)
    	{
    		var command = commands[index];
    		if (index > 0)
    			query += "&";
    			
			query += "command" + index + "=" + command.name;
		        
		    for (var paramName in command.parameters)
		    {
		        query += "&" + paramName + index + "=" + escape(command.parameters[paramName]);
		    }
    	}
    	
	    var url = this.serverUrl;
	    if (commands[0].method == "GET")
	    {
	    	url +="?" + query;
	    }
	    
	    XHR.open(commands[0].method, url, true);
	    
	    if (commands[0].method == "POST")
	    {
	    	XHR.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
	    	XHR.send(query);
	    }
	    else
	    	XHR.send(null); 	
    }
}

function displayProp(elem)
{
	for(var n in elem)
	{
		alert(n);
	}	
}

function go(c)
{
	if(!c.data.replace(/\s/g,''))
		c.parentNode.removeChild(c);
}

function clean(d)
{
	if (d)
	{
		var bal=d.getElementsByTagName('*');
	
		for(i=0; i<bal.length;i++)
		{
			a=bal[i].previousSibling;
			if(a && a.nodeType==3)
				go(a);
			b=bal[i].nextSibling;
			if(b && b.nodeType==3)
				go(b);
		}
	}
	return d;
}

function getData(node)
{
 	var TextInObject = (node.text) ? node.text : (node.textContent) ? node.textContent : node.innerText;
 	return TextInObject ? TextInObject.replace(/\r\n/g," " ) : null;
}

function setData(node, data)
{
// 	(node.text) ? node.text = data : (node.textContent) ? node.textContent = data : node.innerText = data;
	//(node.text!=undefined) ? (node.text = data) : (node.textContent!=undefined) ? (node.textContent = data) : (node.innerText = data);
	(node.textContent!=undefined) ? (node.textContent = data) : (node.text!=undefined) ? (node.text = data) : (node.innerText = data);
}

function setContent(element, content)
{
	if (isIE) 
		element.innerHTML = content;
	else 
	{
		rng = document.createRange();
		
		rng.setStartBefore(element);
		htmlFrag = rng.createContextualFragment(content);
		while (element.hasChildNodes()) 
			element.removeChild(element.lastChild);
		element.appendChild(htmlFrag);
	}
}

function isDefined(object, variable)
{
	return (typeof(eval(object)[variable]) != 'undefined');
}

function isUnDefined(object, variable)
{
	return (typeof(eval(object)[variable]) != 'undefined');
}

function importXML(text)
{
	if (isIE)
  	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(text);
	}
	else
  	{
		try //Firefox, Mozilla, Opera, etc.
		{
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(text,"text/xml");
		}
		catch(e)
		{
			alert(e.message);
			return;
		}
	}
	return xmlDoc;
}

function importXMLFile(fileXml)
{
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = createTable;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) createTable()
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}

	xmlDoc.load(fileXml);
}

function getFirstChildObject(node)
{
	var child = node.childNodes[0];
		 
	while (child && child.nodeType != 1)
	 child = child.nextSibling;
	 
	return child; 
}

function getChildObject(node, index)
{
	var child = node.childNodes[0];
	var item = -1;		 
	do
	{
		if (item != -1)
			child = child.nextSibling;
			
		while (child && child.nodeType != 1)
			child = child.nextSibling;

		item++;	
	}
	while(item < index);
	
	return child; 
}


function nextObject(node)
{
	node = node.nextSibling;
		 
	while (node && node.nodeType != 1)
	 node = node.nextSibling;
	 
	return node; 
}

function getElementPosition(obj)
{
    var curleft = 0;
    var curtop = 0;

    if (obj.offsetParent)
    {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent)
        {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

function getElementSize(Elem)
{
    return [Elem.offsetWidth,Elem.offsetHeight];
}

function getWindowSize() 
{
  var width = 0, height = 0;
  if( typeof( window.innerWidth ) == 'number' ) 
  {
    //Non-IE
    width = window.innerWidth;
    height = window.innerHeight;
  } 
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
  {
    //IE 6+ in 'standards compliant mode'
    width = document.documentElement.clientWidth;
    height = document.documentElement.clientHeight;
  } 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
  {
    //IE 4 compatible
    width = document.body.clientWidth;
    height = document.body.clientHeight;
  }
  return [width,height];
}

function getScrollXY() 
{
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) 
  {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } 
  else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } 
  else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

Array.prototype.insert=function(i,value)
{
	if(i>=0)
	{
		var a=this.slice(),b=a.splice(i);
		a[i]=value;
		return a.concat(b);
	}
}

function mouseX(evt) {if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft); else return null;}
function mouseY(evt) {if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return null;}

function getSiblingIndex(element)
{
	if (element && element.parentNode)	
	{
		var index = 0;
		while(index < element.parentNode.childNodes.length && element.parentNode.childNodes[index] != element)
			index++;
			
		if (index  < element.parentNode.childNodes.length)	
			return index;
	}
	
	return -1;
}

function include(fileName) 
{
	if (document.getElementsByTagName) 
	{
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = fileName;
		
		heads = document.getElementsByTagName("head");
	/*	
		script.onreadystatechange = function () {
        if (script.readyState == 'complete') {
            alert('JS onreadystate fired');
        }
    	}

	    script.onload = function () 
		{
	        alert('JS onload fired');
	    }
		*/
		if (heads && heads.length > 0) 
		{
			heads[0].appendChild(script);
		}
	}
}

function onLoadLink(elem, src, handler)
{
	alert(elem);
	//elem.onreadstatechange = function(){if (elem.readyState == 'complete'){handler(elem,src)}}
	elem.onreadystatechange = function () 
	{
        if (elem.readyState == 'complete') 
		{
            alert('JS onreadystate fired');
        }
    }
	
	elem.onload = function(){handler(elem,src);}	
}

function _importNode(doc, oNode, bImportChildren) 
{
	var oNew;    
	if(oNode.nodeType == 1) 
	{        
		oNew = doc.createElement(oNode.nodeName);
		for(var i = 0; i < oNode.attributes.length; i++) 
		{ 
			if(oNode.attributes[i].nodeValue != null && oNode.attributes[i].nodeValue != '') 
			{
	            var attrName = oNode.attributes[i].name;
				if(attrName == "class")
				{   
					oNew.setAttribute("className",oNode.attributes[i].value); 
				} 
				else
				{
					oNew.setAttribute(attrName, oNode.attributes[i].value);
				}
			}
		}
		
		if(oNode.style != null && oNode.style.cssText != null) 
		{
			oNew.style.cssText = oNode.style.cssText;
		}
	}
	else if(oNode.nodeType == 3) 
	{
		oNew = doc.createTextNode(oNode.nodeValue);
	}
	
	if(bImportChildren && oNode.hasChildNodes())
	{
		for(var oChild = oNode.firstChild; oChild; oChild = oChild.nextSibling) 
		{
			oNew.appendChild(_importNode(doc,oChild, true));
		}
	}
	return oNew;
}

function addLoadListener(func) 
{
   if (window.addEventListener) 
   {
      window.addEventListener("load", func, false);
   } 
   else if (document.addEventListener) 
   {
      document.addEventListener("load", func, false);
   } 
   else if (window.attachEvent) 
   {
      window.attachEvent("onload", func);
   } 
   else if (typeof window.onload != "function") 
   {
      window.onload = func;
   }
   else 
   {
      var oldonload = window.onload;
      window.onload = function() {
         oldonload();
         func();
      };
   }
}

var _gatc = document.createElement('script'); 
_gatc.type = "text/javascript"; 
_gatc.src =((location.protocol=="https:")?"https://ssl.":"http://www.")+"google-analytics.com/ga.js"; 
document.getElementsByTagName('head')[0].appendChild(_gatc); 


function startTracking() 
{ 
    if (typeof(_gat) == "object") 
	{ 
    	var pageTracker = _gat._getTracker("UA-6002366-1"); 
        pageTracker._trackPageview(); 
		pageTracker._initData();
    } 
} 

if (window.addEventListener)
{ 
	window.addEventListener("load",startTracking,false); 
}
else if (window.attachEvent) 
{ 
	window.attachEvent("onload",startTracking);
}