var scriptLoaded = 0 ;
var cookiestatus = "success" ;
var baseurl_dyns =  "http://www.apexchat.com/" ;


function init() { 

  if (id == 'toyotaofdanvers' || id == 'patpeckhonda' || id == 'hondaoffreehold' || id == 'hasselbmw' )
    LoadRunRemoveScriptOnce (id) ;
  else   
   setInterval ("LoadRunRemoveScript('" + id + "')", 1000) ;
   
} 

function start() { 
   
    LoadRunRemoveScriptOnce (id) ;

} 


function LoadRunRemoveScript (id){
 removeScript('rs'); 
 
 var remoteScript=document.createElement('script'); 
 remoteScript.id = 'rs'; 
 remoteScript.setAttribute('type','text/javascript'); 

 
 var url = baseurl_dyns + "dyns.ashx?id="+id + "&rurl=" + document.referrer + "&dummy=" + new Date().getTime() ;
 remoteScript.setAttribute('src',url); 
 var hd=document.getElementsByTagName('head')[0]; 
 hd.appendChild(remoteScript); 
 
 
}

function LoadRunRemoveScriptOnce (id){
 removeScript('rs'); 
 //alert ('lrrso') ;
 var remoteScript=document.createElement('script'); 
 remoteScript.id = 'rs'; 
 remoteScript.setAttribute('type','text/javascript'); 
 var url = baseurl_dyns + "dyns.ashx?id="+id + "&rurl=" + document.referrer + "&dummy=" + new Date().getTime() ;
 
// var url = baseurl_dyns + "checkonce.ashx?id="+id + "&rurl=" + document.referrer + "&dummy=" + new Date().getTime() ;
 remoteScript.setAttribute('src',url); 
 var hd=document.getElementsByTagName('head')[0]; 
 hd.appendChild(remoteScript); 
 
 
}




function removeScript(id) 
{ 
var hd=document.getElementsByTagName('head')[0]; 

var scriptToRemove = document.getElementById(id) ;
if (scriptToRemove != null) {
    hd.removeChild(scriptToRemove); 
    scriptLoaded = 0 ;
    }
    
} 

var previousImage = "" ;
function doStuffWith(onlineOffline)
 
{ 
    if (previousImage != onlineOffline) {
      document.getElementById('divChatButton').innerHTML = onlineOffline ;
      previousImage = onlineOffline ;
      if (onlineOffline.indexOf('Chat_Open') != -1) 
      {
       // alert ("chat") ;
        //InvitationDiv2 (id, 1, 'Hello, how may I help you?', 1) ;
      }
    }
    
}

function autoInitiatedCall (operatorName, companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteStyle, AutoInviteDelay, AgentPic)
{

    var existingWindow = document.getElementById ('chat_forecolor') ;
      
      if (existingWindow == null) {    
      
        if (AutoInviteOn == "1")
        {
            var chatStartedCookie = readCookie ('chatStarted') ;
            var lastInvitation = readCookie ('lastInvite') ;
            if (lastInvitation == null && chatStartedCookie == null && cookiestatus != "fail"){
            
                if (AutoInviteStyle == "1") 
                {
                    ChatUI_ShowColorPanel (companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteDelay) ;   
                    if (AutoInviteDelay == 0 )
                        cookiestatus = createCookie ('lastInvite', 'yes', 1000) ;
                    else  
                        cookiestatus = createCookie ('lastInvite', 'yes', AutoInviteDelay) ;                 
                }
          
                if (AutoInviteStyle == "2" )  
                {
                
                    setTimeout ("InvitationDiv2 ('" + companyLogin + "', " + AutoInviteOn + ", '" + AutoInviteMessage + "', " + AutoInviteDelay + ", '" + AgentPic + "')" , 3000) ;           
                    if (AutoInviteDelay == 0 )
                        cookiestatus = createCookie ('lastInvite', 'yes', 1000) ;
                    else  
                        cookiestatus = createCookie ('lastInvite', 'yes', AutoInviteDelay) ;
                }
           
                if (AutoInviteStyle == "3" )  
                {
                    setTimeout ("InviteWithClick2Call ('" + companyLogin + "', " + AutoInviteOn + ", '" + AutoInviteMessage + "', " + AutoInviteDelay + ", '" + AgentPic + "')" , 1000) ;                   
                    if (AutoInviteDelay == 0 )
                        cookiestatus = createCookie ('lastInvite', 'yes', 1000) ;
                    else  
                        cookiestatus = createCookie ('lastInvite', 'yes', AutoInviteDelay) ;
                }
            }
        }
         
           
        
       }
       else
       
          if (AutoInviteDelay == 0 )
                cookiestatus = createCookie ('lastInvite', 'yes', 1000) ;
            else  
                cookiestatus = createCookie ('lastInvite', 'yes', AutoInviteDelay) ;

        
      
}


function agentInitiatedCall (operatorName, companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteStyle, AutoInviteDelay, AgentPic)
{
    if (AutoInviteStyle == "1") 
        ChatUI_ShowColorPanel (companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteDelay) ;
          
    if (AutoInviteStyle == "2" )  {
        setTimeout ("InvitationDiv2 ('" + companyLogin + "', " + AutoInviteOn + ", '" + AutoInviteMessage + "', " + AutoInviteDelay + ", '" + AgentPic + "')" , 3000) ;           
    }
           
    if (AutoInviteStyle == "3" )  {
        setTimeout ("InviteWithClick2Call ('" + companyLogin + "', " + AutoInviteOn + ", '" + AutoInviteMessage + "', " + AutoInviteDelay + ", '" + AgentPic + "')" , 1000) ;
    }

}

function Chat_OpenWindow () {
	var chaturl = baseurl_dyns + "cr.aspx?login=" + id + "&ngn=true";
	var newChat = window.open (chaturl, "test", "resizable=no, width=700, height=500") ;
	cookiestatus = createCookie ('chatStarted', 'yes', 100) ;
	return false; 
}  

function OpenChatWindow (companyLogin) {
	var chaturl = baseurl_dyns + "cr.aspx?login=" + companyLogin + "&ngn=true";
	var newChat = window.open (chaturl, "test", "resizable=no, width=700, height=500") ;
	
	var chat_forecolor=document.getElementById("chat_forecolor");
	document.body.removeChild(chat_forecolor);
	cookiestatus = createCookie ('chatStarted', 'yes', 100) ;
	return false; 
}  


function OpenChatWindowWithInitialMessage (companyLogin) {
	var initialmsg =document.getElementById("initialmsg").value;
	var chaturl = baseurl_dyns + "cr.aspx?login=" + companyLogin + "&ngn=true&initialmsg=" + initialmsg;
	var newChat = window.open (chaturl, "test", "resizable=no, width=700, height=500") ;
	
	var chat_forecolor=document.getElementById("chat_forecolor");
	
	document.body.removeChild(chat_forecolor);
	cookiestatus = createCookie ('chatStarted', 'yes', 100) ;
	return false; 
}  

function ProcessClick2Call (companyLogin) {
	var initialmsg =document.getElementById("PH").value;
	initialmsg = initialmsg.replace('(', '').replace (')', '').replace ('-', '').replace (' ', '') ;
	
	if (IsNumeric(initialmsg ))
	{
	    var chaturl = baseurl_dyns + "ClicktoCall/c2c.aspx?login=" + companyLogin + "&initialmsg=" + initialmsg;
	    var newChat = window.open (chaturl, "test", "resizable=no, width=300, height=330") ;
	
	    var chat_forecolor=document.getElementById("chat_forecolor");
	
	    document.body.removeChild(chat_forecolor);
	}
	else 
	  alert ('Invalid Phone number. Enter a valid phone and try again!') ;
	  
	return false; 
}  


function Chat_OpenDemoWindow () {
	var chaturl = baseurl_dyns + "cr.aspx?login=demo";
	var newChat = window.open (chaturl, "test", "resizable=no, width=700, height=500") ;
	return false; 
}  

function Chat_OpenFreeWindow () {
	var chaturl = baseurl_dyns + "cr.aspx?login=" + id + "&ngn=false";
	var newChat = window.open (chaturl, "test", "resizable=no, width=700, height=500") ;
	return false; 
}  

function Message_OpenWindow () {
	var chaturl = baseurl_dyns + "OfflineMessage.aspx?login=" + id ;
	var newChat = window.open (chaturl, "test", "resizable=yes, width=400, height=450") ;
	return false; 
}  


function ChatUI_ShowColorPanel(companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteDelay)
{
	var chat_forecolor=document.createElement("div");
	chat_forecolor.id="chat_forecolor";
	chat_forecolor.style.zIndex = 1 ;
	chat_forecolor.style.position= "absolute";
	chat_forecolor.style.display = "none";
	chat_forecolor.style.border= "solid 1px #B0C4DE";
	chat_forecolor.style.padding= "3px";
	chat_forecolor.style.backgroundColor= "#B0C4DE";
	chat_forecolor.style.width = "460px" ;
	chat_forecolor.style.height = "0px" ;
	
	 if (AutoInviteOn == 1)
      OutgoingMessage = AutoInviteMessage ;
    else
      OutgoingMessage = "Hello, How can I help you?" ;
	
	chat_forecolor.onmousedown=new Function("event","(event||window.event).cancelBubble=true;");
	document.body.appendChild(chat_forecolor);

	var temp_html = '';
	var colors = new Array("#A5B6DE");				
	var total = colors.length;
	var width = 8;

	temp_html += "<table cellpadding=5 bgcolor=#3366FF cellspacing=0 style='cursor: hand;'>";	
	for (var i=0; i<total; i++) {
		if ((i % width) == 0) 
			temp_html += "<tr>"; 
		temp_html += '<td align=center style="padding:2px;border:solid 1px #ffffff;" onmouseover="this.style.backgroundColor=\'#D2DBEE\';this.style.border=\'1px solid #00107b\';" onmouseout="this.style.backgroundColor=\'\';this.style.border=\'1px solid #ffffff\'" onClick=ChatUI_SetForeColor("'+colors[i]+'")>';
		temp_html += '<div style="background-color:'+colors[i]+'; border:solid 1px #808080; width:13px; height:13px; font: 9px Arial #ffffff;">Close</div>';
		temp_html += '</td>';
		
		if ( ((i+1)>=total) || (((i+1) % width) == 0)) { 
			temp_html += "</tr>";
		}
	}	
					
	temp_html += "</table>";
	  
      
      
    
   	chat_forecolor.innerHTML = temp_html ;
	
	chat_forecolor.style.display='block';

	chat_forecolor.style.left='110px';
	chat_forecolor.style.top='10px';
	
	//Html_AttachEvent(document,"mousedown",ChatUI_ForeColor_OnDocumentMouseDown);
	AnimateDiv ('chat_forecolor', 215, 205, companyLogin, OutgoingMessage) ;
	//chat_forecolor.appendChild(IF) ;
 if(window.addEventListener){ // Mozilla, Netscape, Firefox
	document.addEventListener("mousedown", ChatUI_ForeColor_OnDocumentMouseDown, false) ;

 } else { // IE
	document.attachEvent("mousedown", ChatUI_ForeColor_OnDocumentMouseDown) ;
}

}



function InvitationDiv2 (companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteDelay, AgentPic)
{
    
    if (document.body != null) {
    var chat_forecolor=document.createElement("div");
	chat_forecolor.id="chat_forecolor";
	chat_forecolor.style.zIndex = 1 ;
	chat_forecolor.style.position= "absolute";
	chat_forecolor.style.display = "none";
	//chat_forecolor.style.padding= "8px";
	//chat_forecolor.style.border= "solid 1px #999999" ;
	chat_forecolor.backgroundImage = "url('http://www.apexchat.com/images/bluegrd.gif')" ;
	chat_forecolor.backgroundRepeat = "repeat-x"; 
    chat_forecolor.style.backgroundColor= "white";
	chat_forecolor.style.width = "300px" ;
	//chat_forecolor.style.height = "130px" ;
	
	
//	var operatorPic = 'nick2.gif' ;
//	if (companyLogin == 'overlakeroofing')
//	  operatorPic = 'lisa_ol.gif' ;
//	  
	chat_forecolor.onmousedown=new Function("event","(event||window.event).cancelBubble=true;");
	document.body.appendChild(chat_forecolor);

	var temp_html = '';

    var OutgoingMessage ;
    if (AutoInviteOn == 1)
      OutgoingMessage = AutoInviteMessage ;
    else
      OutgoingMessage = "Agent: How can I help you?" ;
      
    temp_html += '<div id = "rootDiv" style="border:solid 1px #999999; position:relative; background-image: url(\'http://www.apexchat.com/images/bluegrd.gif\'); background-repeat: repeat-x; "><table><tr>'
    temp_html += '            <td ><img id="imgOperator" style ="position:relative" src="http://www.apexchat.com/images/AgentPics/' + AgentPic+ '" />'
    temp_html += '            </td>'
    temp_html += '            <td style="font-size:14px"><b>LIVE AGENT AVAILABLE</b>'
    temp_html += '            </td>'
               
    temp_html += '        </tr>'
    temp_html += '        <tr>'
    temp_html += '            <td colspan=2><div id="outgoingmessage" style="color: #339900; font-size:12px; TEXT-ALIGN: center;"></div><br><div style="background-repeat: no-repeat; cursor: hand; font-family: Tahoma, Arial, Helvetica, sans-serif;font-size: 11px;color: #FFFFFF;text-decoration: none;text-align:center; padding-top:5px;background-image:url(http://www.apexchat.com/images/but_bg.gif); width:128px;height:42px;" OnClick = "OpenChatWindow(\'' + companyLogin + '\');">Start Chat</div> '
    temp_html += '            <div align="right" style="font-size:12px"><a href="javascript:ChatUI_ForeColor_OnDocumentMouseDown()">Close</a>  </div><div align="left" style ="font-size:x-small" ><a href="http://www.ApexChat.com" target="_blank" style ="font-size:x-small" >ApexChat.com</a> </div></td>'
    temp_html += '        </tr>'
    temp_html += '    </table>'
	temp_html += '';
	temp_html += '</DIV>' ;
   
   	chat_forecolor.innerHTML =  temp_html ;
	
	chat_forecolor.style.display='block';
	chat_forecolor.style.bottom='0px';
	chat_forecolor.style.right='0px';
	AnimateDiv3 ('chat_forecolor', 0 , 250, companyLogin, OutgoingMessage) ;
 if(window.addEventListener){ // Mozilla, Netscape, Firefox
	//document.addEventListener("mousedown", ChatUI_ForeColor_OnDocumentMouseDown, false) ;

 } else { // IE
	document.attachEvent("mousedown", ChatUI_ForeColor_OnDocumentMouseDown) ;
}
}
else 
{
    setTimeout( "InvitationDiv2('"+companyLogin+"',"+AutoInviteOn+", '"+AutoInviteMessage+"', " + AutoInviteDelay +", '" + AgentPic + "')",1000);
}

}  


function InviteWithMessageBox (companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteDelay, AgentPic)
{
    
    if (document.body != null) {
    var chat_forecolor=document.createElement("div");
	chat_forecolor.id="chat_forecolor";
	chat_forecolor.style.zIndex = 1 ;
	chat_forecolor.style.position= "absolute";
	chat_forecolor.style.display = "none";
	//chat_forecolor.style.padding= "8px";
	//chat_forecolor.style.border= "solid 1px #999999" ;
	chat_forecolor.backgroundImage = "url('http://www.apexchat.com/images/bluegrd.gif')" ;
	chat_forecolor.backgroundRepeat = "repeat-x"; 
    chat_forecolor.style.backgroundColor= "white";
	chat_forecolor.style.width = "300px" ;
	//chat_forecolor.style.height = "130px" ;
	
	
//	var operatorPic = 'nick2.gif' ;
//	if (companyLogin == 'overlakeroofing')
//	  operatorPic = 'lisa_ol.gif' ;
//	  
	chat_forecolor.onmousedown=new Function("event","(event||window.event).cancelBubble=true;");
	document.body.appendChild(chat_forecolor);

	var temp_html = '';

    var OutgoingMessage ;
    if (AutoInviteOn == 1)
      OutgoingMessage = AutoInviteMessage ;
    else
      OutgoingMessage = "Agent: How can I help you?" ;
      
    temp_html += '<div id = "rootDiv" style="border:solid 1px #999999; position:relative; background-image: url(\'http://www.apexchat.com/images/bluegrd.gif\'); background-repeat: repeat-x; "><table><tr>'
    temp_html += '            <td ><img id="imgOperator" style ="position:relative" src="http://www.apexchat.com/images/AgentPics/' + AgentPic + '" />'
    temp_html += '            </td>'
    temp_html += '            <td style="font-size:14px"><b>LIVE AGENT AVAILABLE</b>'
    temp_html += '            </td>'
               
    temp_html += '        </tr>'
    temp_html += '        <tr>'
    temp_html += '            <td colspan=2><div id="outgoingmessage" style="color: #339900; font-size:12px; TEXT-ALIGN: center;"></div><br><div style="background-repeat: no-repeat; cursor: hand; font-family: Tahoma, Arial, Helvetica, sans-serif;font-size: 11px;color: #FFFFFF;text-decoration: none;text-align:center; padding-top:5px;background-image:url(http://www.apexchat.com/images/but_bg.gif); width:128px;height:42px;" OnClick = "OpenChatWindow(\'' + companyLogin + '\');">Start Chat</div> '
    temp_html += '            <div align="right" style="font-size:12px"><a href="javascript:ChatUI_ForeColor_OnDocumentMouseDown()">Close</a>  </div><div align="left" style ="font-size:x-small" ><a href="http://www.ApexChat.com" target="_blank" style ="font-size:x-small" >ApexChat.com</a> </div></td>'
    temp_html += '        </tr>'
    temp_html += '    </table>'
	temp_html += '';
	temp_html += '</DIV>' ;
   
   	chat_forecolor.innerHTML =  temp_html ;
	
	chat_forecolor.style.display='block';
	chat_forecolor.style.bottom='0px';
	chat_forecolor.style.right='0px';
	AnimateDiv3 ('chat_forecolor', 0 , 250, companyLogin, OutgoingMessage) ;
 if(window.addEventListener){ // Mozilla, Netscape, Firefox
	//document.addEventListener("mousedown", ChatUI_ForeColor_OnDocumentMouseDown, false) ;

 } else { // IE
	document.attachEvent("mousedown", ChatUI_ForeColor_OnDocumentMouseDown) ;
}
}
else 
{
    setTimeout( "InviteWithMessageBox('"+companyLogin+"',"+AutoInviteOn+", '"+AutoInviteMessage+"', " + AutoInviteDelay +", '" + AgentPic + "')",1000);
}

}  



function InviteWithClick2Call (companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteDelay, AgentPic)
{
    
    if (document.body != null) {
    //remove old window
    var old_window=document.getElementById("chat_forecolor");
	
	if (old_window != null)
	    document.body.removeChild(chat_forecolor);
	
    var chat_forecolor=document.createElement("div");
	chat_forecolor.id="chat_forecolor";
	chat_forecolor.style.zIndex = 1 ;
	chat_forecolor.style.position= "absolute";
	chat_forecolor.style.display = "none";
	//chat_forecolor.style.padding= "8px";
	//chat_forecolor.style.border= "solid 1px #999999" ;
	chat_forecolor.backgroundImage = "url('http://www.apexchat.com/images/bluegrd.gif')" ;
	chat_forecolor.backgroundRepeat = "repeat-x"; 
    chat_forecolor.style.backgroundColor= "white";
	chat_forecolor.style.width = "300px" ;
	//chat_forecolor.style.height = "130px" ;
	
	
//	var operatorPic = 'nick2.gif' ;
//	if (companyLogin == 'overlakeroofing')
//	  operatorPic = 'lisa_ol.gif' ;
//	  
	chat_forecolor.onmousedown=new Function("event","(event||window.event).cancelBubble=true;");
	document.body.appendChild(chat_forecolor);

	var temp_html = '';

    var OutgoingMessage ;
    if (AutoInviteOn == 1)
      OutgoingMessage = AutoInviteMessage ;
    else
      OutgoingMessage = "Agent: How can I help you?" ;
      
   
    temp_html += '  <div id = "rootDiv" style="border:solid 1px #999999; position:relative; background-image: url(\'http://www.apexchat.com/images/bluegrd.gif\'); background-repeat: repeat-x; "><table><tr>' ;
    temp_html += '                <td ><img id="imgOperator" style ="position:relative" src="http://www.apexchat.com/images/AgentPics/' + AgentPic + '" />' ;
    temp_html += '                </td>' ;
    temp_html += '                <td style="font-size:14px"><b>LIVE AGENT AVAILABLE</b>' ;
    temp_html += '                </td>' ;               
    temp_html += '            </tr>' ;
    temp_html += '            <tr>' ;
    temp_html += '                <td colspan=2><div id="outgoingmessage" style="color: #339900; font-size:12px; TEXT-ALIGN: center;"></div><br>' ;
    temp_html += '                    <label style="font-family:Arial; font-size:xx-small"><input type="radio" value="1" checked="true" name="number" id="number1" onclick="setLabelText(\'Message: \');"> Start Chat</label> <label style="font-family:Arial; font-size:xx-small"><input type="radio" value="2" name="number" id="Radio1" onclick="setLabelText(\'Your Phone: \');" >Connect by Phone </label>' ;
    temp_html += '                    <br><label style="font-family:Arial; font-size:xx-small" id="lblTextBox">Message: </label><input id="initialmsg" type="text" /><input type="text" style="display:none;" name="PH" id="PH" value="" onkeyup="FormatPhone(this);">' ;
    temp_html += '               <br><div id="callButton" style="background-repeat: no-repeat; cursor: hand; font-family: Tahoma, Arial, Helvetica, sans-serif;font-size: 11px;color: #FFFFFF;text-decoration: none;text-align:center; padding-top:5px;background-image:url(http://www.apexchat.com/images/but_bg.gif); width:128px;height:42px;" OnClick = "ProcessCall(\'' + companyLogin + '\');">Start Chat</div> ' ;
    temp_html += '                <div align="right" style="font-size:12px"><a href="javascript:ChatUI_ForeColor_OnDocumentMouseDown()">Close</a>  </div><div align="left" style ="font-size:x-small" ><a href="http://www.ApexChat.com" target="_blank" style ="font-size:x-small" >ApexChat.com</a> </div></td>' ;
    temp_html += '            </tr>' ;
    temp_html += '       </table>' ;	
    temp_html += '	</DIV>' ;
   

   	chat_forecolor.innerHTML =  temp_html ;
	
	chat_forecolor.style.display='block';
	chat_forecolor.style.bottom='0px';
	chat_forecolor.style.right='0px';
	AnimateDiv3 ('chat_forecolor', 0 , 250, companyLogin, OutgoingMessage) ;
 if(window.addEventListener){ // Mozilla, Netscape, Firefox
	//document.addEventListener("mousedown", ChatUI_ForeColor_OnDocumentMouseDown, false) ;

 } else { // IE
	document.attachEvent("mousedown", ChatUI_ForeColor_OnDocumentMouseDown) ;
}
}
else 
{
    setTimeout( "InviteWithClick2Call('"+companyLogin+"',"+AutoInviteOn+", '"+AutoInviteMessage+"', " + AutoInviteDelay +", '" + AgentPic + "')",1000);
}

}  


function ProcessCall (companyLogin) 
{
    var rdValue = document.getElementById ('number1').checked ;
    
    if (rdValue== true)
    {   
        OpenChatWindowWithInitialMessage (companyLogin) ;
    }
    else
    {
        ProcessClick2Call (companyLogin) ;
    }

}
 function AnimateDiv (a,iHeight,iWidth, companyLogin, msg)
  { a = document.getElementById(a);
    
    xincr = 2 ;
    yincr = 7;     
    var i = parseInt(a.style.width)+xincr ;
    var j = parseInt(a.style.height)+yincr;  
   
   if(i <= iWidth)
   {
     a.style.width = i+"px";
   }
   else
   {
     a.style.width = iWidth+"px";     
   }
   
   if(j <= iHeight)
   {
     
     a.style.height = j+"px";     
   }
   else{   
     a.style.height = iHeight+"px";   	
   
       var IF = document.createElement("iframe") ;
	   IF.id = "chatiframe" ;
	   IF.scrolling = "no" ;
	   IF.frameborder = 0 ;
	   IF.style.zIndex =  888888;
	   IF.style.width = "205px" ;
	   IF.style.height= "210px" ; //should be 185px
	   IF.src = baseurl_dyns + "rmInvSmall.aspx?login=" + companyLogin ;
       a.appendChild(IF) ;
     
   }
   
   if(!((i > iWidth) && (j > iHeight)))      
     setTimeout( "AnimateDiv('"+a.id+"',"+iHeight+","+iWidth+", '" + companyLogin +"')",1);
   else{
     setTimeout ("SetOutgoingMessage2('" + msg + "')", 500) ;
     
   }
   
    }
  
  
  function SetOutgoingMessage2 (msg){
  //  var msgDiv=document.getElementById("outgoingmessage");
    var rootDiv = document.getElementById("chat_forecolor");
   // msgDiv.innerHTML = msg ; 
   // if(!window.addEventListener)
   //     Drag.init(rootDiv);
}    


////////////////////
/// animate div 2 //
////////////////////     
function AnimateDiv2 (a,iHeight,iWidth, companyLogin)
  { a = document.getElementById(a);
    
    xincr = 1 ;
    yincr = 7;     
   var i = parseInt(a.style.left)+xincr ;
   
   if(i <= iWidth)
   {a.style.left = i +"px";}
   else
   {a.style.left= "20px";}
   
   //if(!(i > iWidth))      
   setTimeout( "AnimateDiv2('"+a.id+"',"+iHeight+","+iWidth+", '" + companyLogin +"')",1);
    
    }
    
    
    
    //AnimateDiv3
    
function AnimateDiv3 (a,iHeight,iWidth, companyLogin, msg)
  { a = document.getElementById(a);
    
    xincr = 2 ;
    yincr = 7;     
    var i = parseInt(a.style.width)+xincr ;
    var j = iHeight +yincr;  
   
   if(i <= iWidth)
   {
     a.style.width = i+"px";
   }
   else
   {
     a.style.width = iWidth+"px";     
   }
   
   var direction = "down" ;
   var currentclip= j +"px"
   a.style.clip=(direction=="down")? "rect(0 auto "+currentclip+" 0)" : "rect("+currentclip+" auto auto 0)"
   
   if(!(j > 290))      
     setTimeout( "AnimateDiv3('"+a.id+"',"+ j +","+iWidth+", '" + companyLogin +"', '" + msg + "')",10);
   else
   {
     setTimeout ("SetOutgoingMessage('" + msg + "')", 1000) ;
     
     }
    }
  
function SetOutgoingMessage (msg){
    var msgDiv=document.getElementById("outgoingmessage");
    var rootDiv = document.getElementById("chat_forecolor");
    msgDiv.innerHTML = msg ; 
    
    var imgDiv=document.getElementById("imgOperator"); 
    imgDiv.style.left=0 ;
    imgDiv.style.top=0 ;
    
    ShakeImage () ;
     
  //  if(!window.addEventListener)
  //      Drag.init(rootDiv);
}    
    
var rector=3
var a=1
function ShakeImage (imgDiv) {

var imgDiv=document.getElementById("imgOperator"); 
if (imgDiv !=  null){
  if ((!document.all&&!document.getElementById) )
  return
if (a==1){
//imgDiv.style.top=parseInt(imgDiv.style.top)+rector
}
else if (a==2){
imgDiv.style.left=parseInt(imgDiv.style.left)+rector
}
else if (a==3){
//imgDiv.style.top=parseInt(imgDiv.style.top)-rector
}
else{
imgDiv.style.left=parseInt(imgDiv.style.left)-rector
}
if (a<4)
a++
else
a=1
setTimeout("ShakeImage()",500)
}
}

    


    
function ChatUI_ForeColor_OnDocumentMouseDown()
{
	

	
	var chat_forecolor=document.getElementById("chat_forecolor");
	document.body.removeChild(chat_forecolor);
	
    cookiestatus = createCookie ('chatStarted', 'yes', 60) ;
    if(window.removeEventListener) // Mozilla, Netscape, Firefox
	    document.removeEventListener("mousedown", ChatUI_ForeColor_OnDocumentMouseDown, false) ;
    else 
        document.detachEvent ("mousedown", ChatUI_ForeColor_OnDocumentMouseDown) ;
}

//get absolute or relative parent
function GetStandParent(e)
{
	if(e.currentStyle)
	{
		for(var pe=e.parentElement;pe!=null;pe=pe.parentElement)
		{
			var sp=pe.currentStyle.position;
			if(sp=='absolute'||sp=='relative')
				return pe;
		}
	}
	else
	{
		var view=e.ownerDocument.defaultView;
		for(var pe=e.parentNode;pe!=null&&pe.nodeType==1;pe=pe.parentNode)
		{
			var sp=view.getComputedStyle(pe, '').getPropertyValue("position")
			if(sp=='absolute'||sp=='relative')
				return pe;
		}
	}
	return (e.ownerDocument||e.document).body;
}



function ChatUI_SetForeColor () {
    ChatUI_ForeColor_OnDocumentMouseDown();
}


function setLabelText(setting) {
    document.getElementById('lblTextBox').innerHTML = setting ;
    if (document.getElementById('number1').checked == true)
    {
        document.getElementById('callButton').innerHTML = 'Start Chat' ;        
        document.getElementById('PH').style.display = 'none'
        document.getElementById('initialmsg').style.display = 'block'
    }
    else
    {
        document.getElementById('callButton').innerHTML = 'Phone Connect' ;
        document.getElementById('PH').style.display = 'block'
        document.getElementById('initialmsg').style.display = 'none'
    }
    
}

function FormatPhone(F)
{
var PH = F.value;
PH = PH.replace("(","");
PH = PH.replace(")","");
PH = PH.replace("-","");
PH = PH.replace(" ","");

      if(PH.length < 3)
      {
            F.value = "(" + PH;
      }
      if(PH.length > 3 && PH.length <= 6)
      {
            F.value = "(" + PH.substring(0,3) + ") " + PH.substring(3,6);
      }
      if(PH.length > 6)
      {
            F.value = "(" + PH.substring(0,3) + ") " + PH.substring(3,6) + "-" + PH.substring(6,10);
      }
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function createCookie(name,value,mins) {
  try {
	    if (mins) {
		    var date = new Date();
		    date.setTime(date.getTime()+(mins*60*1000));
		    var expires = "; expires="+date.toGMTString();
	    }
	    else var expires = "";
	    document.cookie = name+"="+value+expires+"; path=/";
	   return "success" ;
	}
	catch (err)
	{
	    return "fail" ;
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
