
var httpObj;
var busy = false;
function getXMLHTTPObject()
{
 	httpObj=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');
    if(!httpObj){return};
    return httpObj;
}
function sendRequest(url,data,method)
{
	if(busy)
	{
		alert("System is working!");
		return;
	}
	busy = true;
    if(!method){method='post'};
    httpObj=getXMLHTTPObject();
    httpObj.open(method,url,true);
    httpObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    httpObj.onreadystatechange=displayStatus;
    httpObj.send(data);    
}
function replacePageByID(text)
{
	var firstbegin = text.indexOf('<');
	var constart = text.indexOf('>');
	var firstTag = text.substring(firstbegin,constart);
	if(firstTag.indexOf("html") >= 0)
	{
		document.body.innerHTML= text;
		return;
	}
	var conend = text.lastIndexOf('<');
	var realcon = text.substring(constart+1,conend);
	//alert(realcon);
	if(firstTag.indexOf("redirectTo") >=0)
	{
		//alert("Here is the content :::   "+realcon);
		//window.location.href = realcon;
		//document.location = realcon;
		location.replace(realcon);
		return;
	}
	var divstart = text.indexOf('=')+1;
	var divid = text.substring(divstart,constart);
	var firstQuote = divid.indexOf('"');
	var secondQuote = divid.indexOf('"',firstQuote+1);
	divid = divid.substring(firstQuote+1,secondQuote);
    var divElem = document.getElementById(divid);
    divElem.innerHTML = realcon;
}
function displayStatus()
{
    if(httpObj.readyState==4)
    {
      if (httpObj.status == 200) 
      {
    	  replacePageByID(httpObj.responseText);
          busy = false;
      }
    }
}
function switchContentByElementID(teidstr,ceidstr,activeId)
{
	var ceids = ceidstr.split("#");
	var teids = teidstr.split("#");
	var tabnum = teids.length;
	
	for(i = 0; i< tabnum; i++)
	{
		var celem = document.getElementById(ceids[i]);
		var telem = document.getElementById(teids[i]);
		if(i == activeId)
		{
			celem.style.display='';
			telem.style.backgroundColor = 'white';
			telem.style.borderBottomColor = 'white';
		}
		else
		{
			celem.style.display="none";
			telem.style.backgroundColor = '#DDE';
			telem.style.borderBottomColor = '#778';
		}
	}
}
function printView()
{
	var elem = document.getElementById("internal_canvas");
	if(!elem)
	{
		alert("error!");
	}
	newWin = window.open();
	newWin.document.write("<html><head><title>AFanqi</title></head><body>");
	newWin.document.write(elem.innerHTML);
	newWin.document.writeln("</body></html>");
	newWin.document.close();
}

