//------- 
// Generic XMLHttpRequest Routines
//-------
var ajax = null;

function GenerateAjax() {
  try {
	  ajax = new XMLHttpRequest();
	} catch (e) {
	  try {
	    ajax = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
      ajax = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	}
	if (!ajax && typeof ajax !='undefined') {
	  ajax = new XMLHttpRequest();
	}

	if (ajax == null) {
		document.writeln("error Generate XMLHttpRequest()<br/>\n");
	}

  // onReadyStateChange evnet handler
  ajax.onreadystatechange = loadHandler;
}
  // onReadyStateChange evnet handler
	function loadHandler () {
  	// '4' means finished data recieve
  	if (ajax.readyState == 4) {
  		  //DebugDisp( oDiv );
  	}
	}
  // end of onreadystatechange Handler

// async : true --> read asyncronus 
//			false --> syncronus read
function AjaxOpen( filename, async ) {
  try {
  	ajax.open('GET', filename , async);
		ajax.setRequestHeader('Content-Type', 'text/xml');
		ajax.setRequestHeader('Pragma', 'no-cache');
		ajax.setRequestHeader('Cache-Control', 'no-cache');
		ajax.send(null);
  }
  catch (e) {
  	document.write("&lt;h4&gt;Error occured while XMLHttpRequest.open()&lt;/h4&gt;");
  //document.write( location.href + "<br/>\n" );
  }
}
// XMLHttpRequest end
//-----

function CallCenter() {
	this.CallCenterID  = "";
	this.GroupCount = 0;
	this.setCallCenterID = function( stID ) { this.CallCenterID = stID; }
	this.setGroupCount = function( nCnt ) { this.GroupCount= nCnt; }
}

// Agent Object
//function Agent( agentID, title, status, inprogress, type, group) {
function Agent() {
	this.AgentID = "";
	this.Title = "";
	this.Status = "";
	this.Inprogress = "";
	this.Type = "";
	this.Group = "";
	this.setAgentID = function ( agtID ) { this.AgentID = agtID; }
	this.setTitle = function ( title ) { this.Title = title; }
	this.setStatus = function ( status ) { this.Status = status; }
	this.setInprogress = function ( inprogress ) { this.Inprogress = inprogress; }
	this.setType = function ( type ) { this.Type = type; }
	this.setGroup = function ( group ) { this.Group = group; }
}

function OutlineFormat( st ) {
	st = st.replace( "\"", "" );
	st = st.replace( "\"", "" );
	st = st.replace( "\"", "" );
	st = st.replace( "\"", "" );
	st = st.replace( "\"", "" );
	st = st.replace( "\"", "" );
	st = st.replace( "\"", "" );
	st = st.replace( "\"", "" );
	st = st.replace( "\"", "" );
	st = st.replace( "\"", "" );

	st = st.replace( "/", "" );
	
	return st;
}

// Return  boolean : true  means Parse SUCCESS
//						  false meanse Parse Error (unknown state CallCenter, Agent Object)
function parseOPML( stTarget, callcenter, arAgt ) {
	var stText = stTarget;
	var stTemp, stSearch;
	
	// 1st Stage, Check Data Formart; quite simple
	var index = stText.indexOf("<opml>");
	var to;
	if (index == -1) {
		return false;
	}
	
	// 2nd Stage,  Getting Callcenter infomarion ( Fill CallCener object)
	stSearch = "<callCenterID>";
	index = stText.indexOf(stSearch);
	if (index == -1) {
		return false;
	}
	index += stSearch.length;

	to = stText.indexOf( "<", index +1);
	stTemp = stText.substring(index , to );

	callcenter.setCallCenterID( stTemp );

	//  GroupCount
	stSearch = "<groupcount>";
	index = stText.indexOf( stSearch );
	if (index == -1) {
		return false;
	}
	index += stSearch.length;
	
	to = stText.indexOf( "<", index +1);
	stTemp = stText.substring( index , to );

	callcenter.setGroupCount( stTemp );
	
	// 3rd Stage, parse Agnet / Group element
	var i = 0;
	var agt;
	stSearch = "<outline ";
	index = stText.indexOf( stSearch );
	while (index != -1) {
		index += stSearch.length;
		
		to = stText.indexOf( ">", index +1 );
		stTemp = stText.substring( index, to );
		stTemp = OutlineFormat( stTemp );

		var stVals = stTemp.split( " " );
		var stGrp;
		
		agt = new Agent();
		for (var j=0; j< stVals.length; j++) {
			var arParam = stVals[j].split( "=" ); // split value name and value
			if  ( arParam[0] == "text" ) {
				agt.setAgentID( String(arParam[1])  );
			}
			else if  ( arParam[0] == "userstatus" ) {
				agt.setStatus(  String(arParam[1])  );
			}
			else if  ( arParam[0] == "inprogress" ) {
				agt.setInprogress(   String(arParam[1] )  );
			}
			else if  ( arParam[0] == "type" ) {
				agt.setType(  String(arParam[1])  );
				if ( arParam[1] =="group" ) {
					stGrp = agt.AgentID;
				}
			}
			agt.setGroup( stGrp  );

		}
//		console.log(  "exit for,  agtid " + agt.AgentID );
		arAgt[i++] = agt;
		
		//  prepare for next loop
		index = stText.indexOf( stSearch,  index + 1);
	}
}

function CalcAliveAgent( oAgts ) {
	var nAlive = 0;
	
	for (var i=0; i < oAgts.length; i++ ) {
		if ( oAgts[i].Status == "ONLINE") {
			nAlive++;
		}
		if ( oAgts[i].Inprogress == "true") {
			nAlive--;
		}
	} 
	
	return nAlive;
} 

function DebugOut( oAgts ) {
	for (var i=0; i< oAgts.length; i++ ) {
		document.write( i + ": " + oAgts[i].AgentID + " , " + oAgts[i].Group + "<br/>");
	}
}
