/* Ajax Repository */
function getConn()
{
 var xmlHttp;
 try
 {
  //for browsers that support XMLHttpRequest()
  xmlHttp = new XMLHttpRequest();
  return xmlHttp;  
 }
 catch (e)
 {
   //for versions of IE that use ActiveX
   try
   {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    return xmlHttp;    
   }
   catch (e)
   {
     try
     {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      return xmlHttp;
     }
     catch (e)
     {      
      return false;
     } 
   }
 }
}
function ajaxConn(url,uid)
{
 var xmlHttp = getConn();
 if (!xmlHttp){
 alert("You're using a browser that doesn't support AJAX!");
 return false;
 }  
 //create and assign function to receive information from server
 xmlHttp.onreadystatechange = function(){
   /* check the state of request 
   0 for request not initialized
   1 for request set up
   2 for request sent
   3 for request being processed
   4 for request completed
   */
   if(xmlHttp.readyState == 4)
   {
   var a = document.getElementById('leftpin')?document.getElementById('leftpin'):document.all['leftpin'];        
   a.innerHTML = xmlHttp.responseText;   
   }
  }
  
  //fetch data from server  
  var thePath = url+"/userlist.php?userID="+uid;      
  xmlHttp.open("GET",thePath,true);
  xmlHttp.send(null);  
}

/*Insert Function */
function ajaxInsert(sendID, recvID, msg, url)
{
 var xmlHttp = getConn();
 if (!xmlHttp){
 alert("You're using a browser that doesn't support AJAX!");
 return false;
 }
 
 xmlHttp.onreadystatechange = function(){ 
   if(xmlHttp.readyState == 4)
   {   
    //ajaxReceive(sendID,recvID,url);
   }
  }
  
  //fetch data from server
  var thePath = url+"/messages.php?senderID="+sendID+"&receiverID="+recvID+"&mesg="+msg;
  xmlHttp.open("POST",thePath,true);
  xmlHttp.send(null);   
}

/*GET Message Function */
function ajaxReceive(sendID, recvID, url)
{
 var xmlHttp = getConn();
 if (!xmlHttp){
 alert("You're using a browser that doesn't support AJAX!");
 return false;
 }
 
 xmlHttp.onreadystatechange = function(){ 
   if(xmlHttp.readyState == 4)
   {
    var a = document.getElementById('displaypin')?document.getElementById('displaypin'):document.all['displaypin'];            
    a.innerHTML = xmlHttp.responseText;            
    var c = document.getElementById('message')?document.getElementById('message'):document.all['message'];
    c.focus();
    nDate = new Date();
    var b = document.getElementById('dateLine')?document.getElementById('dateLine'):document.all['dateLine'];
    b.innerHTML =  nDate.toLocaleString();
    //chat(url,sendID,recvID,"");    
   }
   
  }
  
  //fetch data from server
  var thePath = url+"/receive.php?senderID="+sendID+"&receiverID="+recvID;
  xmlHttp.open("GET",thePath,true);
  xmlHttp.send(null);    
}

function submitValues()
{
 var msg = document.conBox.message.value;
 var sendID = document.conBox.sender.value;
 var recvID = document.conBox.receiver.value;
 var theUrl = document.conBox.theUrl.value;
 ajaxInsert(sendID, recvID, msg, theUrl);
 var e = document.getElementById('message')?document.getElementById('message'):document.all['message'];
 e.value = "";
 e.focus(); 
}

function chat(url,sendID,recvID,theUrl,calledBy)
{
 //Add the request to be processed by listeners
 if (calledBy)
 {
  addChatRequest(url,sendID,recvID,theUrl);
 }
 var thePath;
 thePath = url+"?senderID="+sendID+"&receiverID="+recvID+"&thePath="+theUrl;
 if(!calledBy)
 {
  thePath = url+"?senderID="+sendID+"&receiverID="+recvID+"&thePath="+theUrl+"&cid=yes"; 
 }
 //var myWindID = "win"+sendID+"-"+recvID;
 //var gas = window.open(thePath,myWindID,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width=500,height=400,top=5,left=5,screenX=5,screenY=5',true);  
 var gas = window.open(thePath,'','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width=500,height=400,top=5,left=5,screenX=5,screenY=5');
 gas.focus();  
}

function addChatRequest(url,sendID,recvID,theUrl)
{
var xmlHttp = getConn();
 if (!xmlHttp){
 alert("You're using a browser that doesn't support AJAX!");
 return false;
 }
 
 xmlHttp.onreadystatechange = function(){ 
   if(xmlHttp.readyState == 4)
   { 
    //var themsg = xmlHttp.responseText;            
    //return themsg;   
    //alert(xmlHttp.responseText);                 
   }   
  }  
  //fetch data from server
  var thePath = theUrl+"/mydata.php?theUrl="+url+"&senderID="+sendID+"&receiverID="+recvID+"&mainUrl="+theUrl+"&acType=ins";
  xmlHttp.open("POST",thePath,true);
  xmlHttp.send(null);
  
  //return xmlHttp.onreadystatechange;
}

function chatObserver(url,usrId)
{
 var xmlHttp = getConn();
 if (!xmlHttp){
 alert("You're using a browser that doesn't support AJAX!");
 return false;
 }
 
 xmlHttp.onreadystatechange = function(){ 
   if(xmlHttp.readyState == 4)
   {            
    var retMsg = xmlHttp.responseText;
    var myArr = retMsg.split(",");  
    //alert(myArr);            
 if (myArr[0] == 'true')
 { 
   chat(myArr[1],myArr[2],myArr[3],myArr[4],false); 
 }
 else{
  return false;
 }               
   }
   
  }  
  //fetch data from server
  var thePath = url+"/mydata.php?theUserId="+usrId+"&acType=ret";
  xmlHttp.open("GET",thePath,true);
  xmlHttp.send(null);
}

function launchHelp(thesite)
{
 var myWidth = 800;
 var myHeight = 600;
 var myleft = (myWidth - (screen.availWidth / 2))/2;
 var mytop = (myHeight - (screen.availHeight  / 2))/4; 
 var winSetting = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+myWidth+',height='+myHeight+',top='+mytop+',left='+myleft+',screenX='+myleft+',screenY'+mytop;
 var gas = window.open(thesite,'PgSymbolsHelp',winSetting);
 gas.focus();
 gas.defaultStatus = "Progenics :: The Symbol of Excellence!"; 
}
