/**********************************/
// ezajax
// author: vberror13@yahoo.com		
/*********************************/ 

function $_(objectID)
{
    return document.getElementById(objectID);
}

function getContent(url,otarget,moddate)
{
	var ran=new String(Math.random()*5);
	ran=ran.split(".")[1];
	url +="&rnd="+ran;
    var xht = createRequestObject();
    if (xht)
    {
        xht.open("GET",url);
        if (!moddate)
        	moddate = new Date(1970,1,1)
        	xht.setRequestHeader( "If-Modified-Since", moddate); 
        	xht.setRequestHeader("Expires", moddate);  
			xht.setRequestHeader("Cache-Control", "no-cache"); 
			xht.onreadystatechange = 
			function()
            {
                if(xht.readyState == 4 )
                {
                    if (xht.status == 200)
                    {
	                   //what do the server response ?
                       var ctype = xht.getResponseHeader("Content-type") 
                       //NOTE: apache return application/xhtml+xml, IIS return text/xml
	                   var rObj= null;
	                   if (ctype.indexOf("xml")>0)
							rObj = xht.responseXML;
	                   else 
							rObj = xht.responseText;
	                    var t = typeof(otarget);
	                    switch(t)
	                    {
		                    case 'function': 
		                    	otarget(rObj);
		                    	break;
		                    case 'object':
			                      otarget.innerHTML = rObj;
			                      break;
			                default:
			                	displayWarningBox("test:"+t+xht.responseText);
	                    }
	                    xht=null;
                     } else 
                     {
                        displayWarningBox(url +"<br />" + xht.responseText);
                     }
                     requesting=false;
                } 
                
            }
         xht.send(null);
    }
    
}
function displayWarningBox(msg)
{
	/*var box = document.createElement("div")
	box.style.position="absolute";
	box.style.left="0px";
	box.style.top="0px";
	box.style.overflow="auto";
	
	box.style.backgroundColor="#ffff00";
	box.innerHTML="<div style='background-color:#ff0000'>Warning:</div><div>Click this to hide</div><div>" +msg +"</div>";
	document.body.appendChild(box);
	box.onclick=function()
	{
		document.body.removeChild(this);
	}*/
}
function createRequestObject() 
{
	var req;
    if(window.XMLHttpRequest)
	{req = new XMLHttpRequest();} 
	else if(window.ActiveXObject) 
	{req = new ActiveXObject("Microsoft.XMLHTTP");} 
	else 
	{alert('Problem creating the XMLHttpRequest object');}
    return req;
}



/*xml to JSON*/

function XmltoArrayOfMaps(objXML)
{
	var arrReturn = new Array();
	
 	var noderoot = objXML.firstChild;
 	if (!noderoot){
	 	 alert("the XML is not well-formatted");
	 	 return null;
 	 }
 	while (noderoot.nodeType!= 1){
	 	noderoot=noderoot.nextSibling;
	 	if (!noderoot) return;
 	}
 		 	
 	//get the children
 	var children = 	f_getElementNodes(noderoot);
 	
 	for (i =0; i < children.length;i++)
 	{
	 		arrReturn[i] = createObject(children[i]);
 	}
 	
 	return arrReturn;
}
function createObject(node)
{
		var returnObj = new Object
		var arrNode=f_getElementNodes(node);
		for (var ich=0; ich<arrNode.length; ich++){
			var propertyName= arrNode[ich].tagName;
			var thischildren= f_getElementNodes(arrNode[ich])
			if (thischildren.length>0)
			{
				//get child of this chlildrend;
				returnObj[propertyName] = new Array();
				for (ix = 0; ix< thischildren.length;ix++)
				{
					var obj = createObject(thischildren[ix]);
					returnObj[propertyName].push(obj);
				}
				
			}else{
				if(arrNode[ich].firstChild)
					returnObj[propertyName]=arrNode[ich].firstChild.nodeValue;
				else 
					returnObj[propertyName]="";
			}
		}
		return returnObj;
}

/* return nodes array with type =1 */
	function f_getElementNodes(n_noderoot)
	{
		var rnodes=new Array();
		var childnode = n_noderoot.firstChild;
		while (childnode)
		{
			if (childnode.nodeType==1)
			{
				rnodes.push(childnode);
			}
			childnode= childnode.nextSibling;
		}
		return rnodes;
	}