//The browser detection function. 
//This function can be used for other purposes also.

var secs;
// msecs x delay must be 1000
var msecs = 100;
var delay = 10;
var timerID = null;
var timerRunning = false;
var i;
var loadingText,notFoundText,timeOutText;
var textOutputFunctionName;

function UserAgent() 
{
  var b=navigator.appName.toUpperCase();

  if (b=="NETSCAPE") this.b="ns";
  else if (b=="MICROSOFT INTERNET EXPLORER") this.b="ie";
  else if (b=="OPERA") this.b="op";
  else this.b=b;

  this.version=navigator.appVersion;
  this.v=parseInt(this.version);

  this.ns=(this.b=="ns" && this.v>=4);
  this.ns4=(this.b=="ns" && this.v==4);
  this.ns5=(this.b=="ns" && this.v==5);

  this.ie=(this.b=="ie" && this.v>=4);
  this.ie4=(this.version.indexOf('MSIE 4')>0);
  this.ie5=(this.version.indexOf('MSIE 5')>0);
  this.ie55=(this.version.indexOf('MSIE 5.5')>0);
  this.ie6=(this.version.indexOf('MSIE 6')>0);

  this.op = (this.b=="op");
  this.op4 = (this.b=="op" && this.v==4);
  this.op5 = (this.b=="op" && this.v==5);
}

at=new UserAgent();

//if you want to create the frame or layer dynamically, do not
//specify a name, do something like this, new exchanger();

function exchanger(name)
{
  //hold the dynamically created iframe or layer
  this.lyr = null;

  //to remember if the iframe or layer is created dynamically.
  this.isDynamic = false;

  this.name=name||"";
  this.fakeid=0;

  if (name == null || name=="")
  {
    this.isDynamic = true;
    this.create();
  }
  else
  {
    this.name=name;
    if (at.ns4)
    {
      this.lyr = window.document.layers[this.name];
    }
  }
}

//this function should not be called directly
exchanger.prototype.create=function()
{
  if (at.ns4) 
  {
    this.lyr=new Layer(0);
    this.visibility = "hide";
  }
  else if (at.ie || at.ns5) 
  {
    this.lyr=document.createElement("IFRAME");
    this.lyr.width=650;
    this.lyr.height=0;
    this.lyr.marginWidth=0;
    this.lyr.marginHeight=0;
    this.lyr.frameBorder=0;
//    this.lyr.style.visibility="hidden";
    this.lyr.style.position="absolute";
    this.lyr.src="";
    this.name="JNIFrame"+window.frames.length;
    //this will make IE work.
    this.lyr.setAttribute("id",this.name);
    //this will make netscape work.
    this.lyr.setAttribute("name",this.name);
    document.body.appendChild(this.lyr);
  }
}

exchanger.prototype.sendData=function(url)
{
  this.fakeid += 1;
  var newurl = "";
  if (url.indexOf("?") >= 0)
    newurl = url + "&fakeId" + this.fakeid;
  else
    newurl = url + "?fakeId" + this.fakeid;

  if (this.isDynamic||at.ns4)
    this.lyr.src=newurl;
  else
  {
    if (at.ie || at.ns5 || at.op)
    {
      window.frames[this.name].document.location.replace(newurl);
    }
  }
}

exchanger.prototype.getsrc=function()
{
	return this.lyr.name;
}

exchanger.prototype.retrieveData=function(varName)
{
  if (at.ns4) 
  {
    return eval("this.lyr." + varName);
  }
  else if (at.ie || at.ns5 || at.op) 
  {
    return eval("window.frames['" + this.name + "']." + varName);
  }
}

var checkLoadedParaString;
var checkLoadedFunctionName;
var checkLoadedLoading;
var checkLoadedNotFound;
var checkLoadedTimeOut;

//call this function when data needs to be sent to server
function StartRemoting(paraString,functionName,loading,notFound,timeOut){
	/*
	if ((!is_ie)||(!is_ie5_5up)){
		window.status="Not support the Remote Script!";
		
		return false;		
	}
	*/
	
	var bufferLoaded = false;
	
	if (theBuffer!=null){
		bufferLoaded=true;
	}

	if (bufferLoaded){
		var iFrameName = document.getElementById(theBuffer.getsrc()).id;
		loadingText = loading;
		notFoundText = notFound;
		timeOutText = timeOut;
		
		textOutputFunctionName=functionName;
	
		if (iFrameUpdatedChecking()){
			window.frames[iFrameName].BlankPageArray[1][0]="";
			window.frames[iFrameName].BlankPageArray[0][0]=false;
		}	
		
		theBuffer.sendData(paraString);
		
		InitializeTimer(10);
		StartTheTimer();
	}else{
		checkLoadedParaString=paraString;
		checkLoadedFunctionName=functionName;
		checkLoadedLoading=loading;
		checkLoadedNotFound=notFound;
		checkLoadedTimeOut=timeOut;

	    secs = 15;
	    StopTheClock();
	    timerRunning = true;
		waitForTheBuffer();
	}
}

function theBufferLoaded(){
	StartRemoting(checkLoadedParaString,checkLoadedFunctionName,checkLoadedLoading,checkLoadedNotFound,checkLoadedTimeOut)
}

function waitForTheBuffer(){
	if ((secs==0)||!(timerRunning)){
        timerRunning = false;
		StopTheClock();
		theBufferLoaded();
    }else{
	    if (exchanger!=null){
    		StopTheClock();
       	}
       	
		msecs = msecs - 1;
		if (msecs==0){
	        secs = secs - 1;
	    }
	    
		timerID = setTimeout("waitForTheBuffer()", 250);
	}
}
 
//call this function to check what the server returns.
function showReturnData(){
	if (iFrameUpdatedChecking()){
		if (theBuffer.retrieveData("BlankPageArray[1][0]")!=""){
			if (textOutputFunctionName!=null)
   				textOutputFunctionName(theBuffer.retrieveData("BlankPageArray"),"arrayList");
		}else{
			if (textOutputFunctionName!=null)
    			textOutputFunctionName(notFoundText,"notFound");
		}
	}
}
		
function InitializeTimer(second) {
	//textOutputFunctionName(loadingText,"newText");
    if (textOutputFunctionName != null) {
	    textOutputFunctionName(loadingText,"newText");
	}    

    // Set the length of the timer, in seconds
    secs = second;
    StopTheClock();
    timerRunning = true;
}

function StopTheClock()	{
    if(timerRunning)
        clearTimeout(timerID);
    timerRunning = false;
}

function StartTheTimer() {
	if ((secs==0)||!(timerRunning)){
 		if (textOutputFunctionName!=null)
	       textOutputFunctionName(timeOutText,"newText");
	    
	    if (!(timerRunning)){
	    	textOutputFunctionName("","newText");
	    }
	    
        timerRunning = false;
		StopTheClock();
    }else{
	    if (iFrameUpdatedChecking()){
		    showReturnData();
    		StopTheClock();
       	}
       	
		//self.status = secs;
		msecs = msecs - 1;
		if (msecs==0){
	        secs = secs - 1;
	    }
	        
        if (timerRunning){
	        showReturnData();
	        timerID = self.setTimeout("StartTheTimer()", delay);
		
			/*
			if (msecs==0){
				if (textOutputFunctionName!=null)
			    	textOutputFunctionName(".","continues");
		    }
		    */
		}
    }
}	
	
iFrameUpdatedChecking=function(){
	var flag=false;
	
	if (theBuffer.retrieveData("BlankPageArray")!=null)
		if(theBuffer.retrieveData("BlankPageArray[0][0]")==true)
			flag=true;
			
	return flag;
}

function StopRSClock(){
	if (timerRunning){
		timerRunning=false;
		StopTheClock();
	}
}

function CheckClockIsRunning(){
	if (timerRunning){
		return true;
	}else{
		return false;
	}
}

