
var scriptLoaded = 0 ;
var cookiestatus = "success" ;
var baseurl_dyns =  "http://www.apexchat.com/" ;


function init2() { 

  if (id == 'toyotaofdanvers' || id == 'patpeckhonda' || id == 'hondaoffreehold' || id == 'hasselbmw' )
    LoadRunRemoveScriptOnce (id) ;
  else   
   setInterval ("LoadRunRemoveScript('" + id + "')", 1000) ;
   
} 

function init() { 
   
    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, companyLogin)
 
{ 

    var irUrl = "#" ;
     
    if (readCookie("loadIcon") == null)
         {   
            if (createCookie("loadIcon", "yes", 9999) == "success" )
            {
                irUrl = "http://www.apexchat.com/loadicon.ashx?company="+ companyLogin + "&date=" + new Date().getTime();
           
           }
         }
         
    if (previousImage != onlineOffline) {
      document.getElementById('divChatButton').innerHTML = onlineOffline + '<img style="display:none;" src="' + irUrl + '" />' ;

      previousImage = onlineOffline ;
      if (onlineOffline.indexOf('Chat_Open') != -1) 
      {
       // alert ("chat") ;
        //InvitationDiv2 (id, 1, 'Hello, how may I help you?', 1) ;
      }
    }
    
}

var showInviationOnLeft = false ;

function autoInitiatedCall (operatorName, companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteStyle, AutoInviteDelay, AgentPic, AgentAlias)
{

    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) ;
                   }
                }
                
                if (AutoInviteStyle == "6" || AutoInviteStyle == "7")
                {
                    showInviationOnLeft = true; 
                }
                
                 if (AutoInviteStyle == "4" || AutoInviteStyle == "5" || AutoInviteStyle == "6" || AutoInviteStyle == "7" )  
                {
                
                    var showMinVersion = false ;
                    var inviteMinimized = readCookie ('inviteMinimized') ;
                    if (chatStartedCookie == null && cookiestatus != "fail" && inviteMinimized == null)
                       showMinVersion = false ;
                    else
                        showMinVersion = true ;
                
                    var aliasCookie = readCookie ('agentAlias') ;
                    var agentPicCookie = readCookie ('agentPic') ;
                     var autoInviteCookie = readCookie ('autoInviteMessage') ;
                 //   alert (aliasCookie + ' ' + agentPicCookie) ;
                    if (aliasCookie != null && agentPicCookie != null && autoInviteCookie!= null)
                    {
                        AgentAlias = aliasCookie ;
                        AgentPic = agentPicCookie ; 
                        AutoInviteMessage = autoInviteCookie  ;
                     //   alert (aliasCookie + ' ' + agentPicCookie) ;                      
                    }
                   
                    var agentAliasCookieStatus = createCookie ('agentAlias', AgentAlias, 5) ;
                    var agentPicCookieStatus = createCookie ('agentPic', AgentPic, 5) ;
                     var agentPicCookieStatus = createCookie ('autoInviteMessage', AutoInviteMessage, 5) ;
                    
                    if (AutoInviteStyle == "5" || AutoInviteStyle == "7")
                       createCookie('shownGravity', 'yes', 999) ;
                        
                    setTimeout ("InvitationDivRL ('" + companyLogin + "', " + AutoInviteOn + ", '" + AutoInviteMessage + "', " + AutoInviteDelay + ", '" + AgentPic + "', " + showMinVersion + ", '" + AgentAlias + "')" , 1500) ;           
                    if (AutoInviteDelay == 0 )
                        cookiestatus = createCookie ('lastInvite', 'yes', 1000) ;
                    else  
                        cookiestatus = createCookie ('lastInvite', 'yes', 5) ;
                }
            
        }
         
           
        
       }
       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&aa=none";
	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&aa=none";
	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 IsPopupBlocker() {
    var oWin = window.open ("","testpopupblocker","width=100,height=50,top=5000,left=5000");
    if (oWin==null || typeof(oWin)=="undefined") {
        return true;
    } else {
        oWin.close();
        return false;
    }
}



function OpenChatWindowRL (companyLogin, agentAlias) {
	var chaturl = baseurl_dyns + "cr.aspx?login=" + companyLogin + "&aa=" + agentAlias + "&ngn=true";
	var chaturlSmall = baseurl_dyns + "crSmall.aspx?login=" + companyLogin + "&aa=" + agentAlias + "&ngn=true";
	
//	if (IsPopupBlocker)
	    var newChat = window.open (chaturl, "test", "resizable=no, width=700, height=500") ;
//	else
//	    OpenChatInIFrame ('iframeChat', 400, 350, chaturlSmall) ;
	
	
	InvitationRLMin () ;
	cookiestatus = createCookie ('chatStarted', 'yes', 100) ;
	return false; 
} 


function OpenChatWindowWithInitialMessage (companyLogin) {
	var initialmsg =document.getElementById("initialmsg").value;
	var chaturl = baseurl_dyns + "cr.aspx?login=" + companyLogin + "&aa=none&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 + "&aa=none&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 InvitationDivRL (companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteDelay, AgentPic, showMinVersion, agentAlias)
{
    
    if (document.body != null) {
        var chat_forecolor=document.createElement("div");
	    chat_forecolor.id="chat_forecolor";
	    chat_forecolor.style.zIndex = 999 ;
	    chat_forecolor.style.position= "absolute";
	    //chat_forecolor.style.position= "fixed";
	    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 = "250px" ;
	    chat_forecolor.style.height = "165px" ;

	    chat_forecolor.onmousedown=new Function("event","(event||window.event).cancelBubble=true;");
	    
	    if (document.getElementById('chat_forecolor') == null)
	        document.body.appendChild(chat_forecolor);
	    else
	        return ;

	    var temp_html = '';


        var irUrl = "#" ;
        
        if (readCookie("NewVisitor") == null)
         {   
            if (createCookie("NewVisitor", "yes", 9999) == "success" )
            {
                irUrl = "http://www.apexchat.com/ir.ashx?company="+ companyLogin + "&date=" + new Date().getTime();
                
            }
         }
          
        var OutgoingMessage ;
        if (AutoInviteOn == 1)
          OutgoingMessage = AutoInviteMessage ;
        else
          OutgoingMessage = "Agent: How can I help you?" ;
          
        temp_html += '<div id = "rootDiv" style="border:solid 0px #999999; position:relative; background-image: url(\'http://www.apexchat.com/images/bluegrd.gif\'); background-repeat: repeat-x; "><table><tr>'
        temp_html += '            <td ><img style="display:none;" src="' + irUrl + '" /><img id="imgOperator" style ="position:relative" src="http://www.apexchat.com/images/AgentPics/' + AgentPic+ '" />'
        temp_html += '            </td>'
        temp_html += '            <td valign="bottom" align="center" style="font-family: Tahoma, Arial, Helvetica, sans-serif;font-size: 11px;">Live Agent Available!<br><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 = "OpenChatWindowRL(\'' + companyLogin + '\', \'' + agentAlias + '\');">Start Chat</div> '
        temp_html += '            </td> <td></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>'
        temp_html += '            </td>'
        //temp_html += '            <td valign = "bottom" style="font-size:14px"><div align="right" style="background-repeat: no-repeat; cursor: hand; width:16px;height:16px; padding-top:5px;padding-right:10px;background-image:url(http://www.apexchat.com/images/collapse.png);" OnClick = "InvitationRLMin();"></div>'
        //temp_html += '            </td>'
        temp_html += '        </tr>'
        temp_html += '        <tr >'
        //temp_html += '            <td style="border:solid 1px #999999;  padding-top:10px;padding-left:10px;"><div style="background-repeat: no-repeat; cursor: hand; width:16px;height:16px; padding-top:5px;padding-left:10px;background-image:url(http://www.apexchat.com/images/collapse.png);" OnClick = "ChatUI_ForeColor_OnDocumentMouseDown();"></div><div style="background-repeat: no-repeat; cursor: hand; width:16px;height:16px; padding-top:5px;padding-left:10px;background-image:url(http://www.apexchat.com/images/collapse.png);" OnClick = "InvitationRLMin();"></div>'
        //temp_html += '            </td>'  
        temp_html += '            <td style="border:solid 0px #999999;  padding-top:10px;padding-left:10px;"><div style="background-repeat: no-repeat; cursor: hand; width:16px;height:16px; " OnClick = "ChatUI_ForeColor_OnDocumentMouseDown();">[Close]</div>'
        temp_html += '            </td>'  
        temp_html += '            <td align="right" style="border:solid 0px #999999;  padding-top:10px;" ><a href="http://www.apexchat.com" target="_blank" ><div style="background-repeat: no-repeat; cursor: hand; font-family: Tahoma, sans-serif;font-size: 11px;color: #FFFFFF;text-align:center; padding-top:5px;background-image:url(http://www.apexchat.com/images/apexlogo.png); width:78px;height:18px;"></div></a>'
        temp_html += '            </td>'
        temp_html += '        </tr>'
        temp_html += '    </table>'
	    temp_html += '';
	    temp_html += '</DIV>' ;
       
   	    chat_forecolor.innerHTML =  temp_html ;
    	
	   
	  //  chat_forecolor.style.bottom='0px';
	  //  chat_forecolor.style.right='0px';
	    
	    
         
	    if (readCookie ('shownGravity') == null ) 
	        AnimateGravity('chat_forecolor',OutgoingMessage ) ;
	    else 
		{
		chat_forecolor.style.bottom='0px';
		if (showInviationOnLeft)
		    chat_forecolor.style.left ='0px';
		else
	      chat_forecolor.style.right='0px';
	      
	      AnimateDiv3 ('chat_forecolor', 0 , 250, companyLogin, OutgoingMessage) ; 

}
	    //-----------------------
	    // Add Min Version but keep it invisible
	    //----------------------------------
	        var minChat =document.createElement("div");
	        minChat.id="minChat";
	        minChat.style.zIndex = 1 ;
	        minChat.style.position= "absolute";
		//minChat.style.position= "fixed";
	       

	        minChat.backgroundImage = "url('http://www.apexchat.com/images/bluegrd.gif')" ;
	        minChat.backgroundRepeat = "repeat-x"; 
            minChat.style.backgroundColor= "white";
	        minChat.style.width = "140px" ;

	        minChat.onmousedown=new Function("event","(event||window.event).cancelBubble=true;");
	        if (document.getElementById('minChat') == null)
	            document.body.appendChild(minChat);
	        else 
	            return ;

	       temp_html = '';

              
            temp_html += '<div id = "minRoot" OnClick = "InvitationRLMax();" style="border:solid 1px #999999; position:relative;cursor: hand; background-image: url(\'http://www.apexchat.com/images/bluegrd.gif\'); background-repeat: repeat-x; "><table><tr>'
            temp_html += '            <td ><img id="imgOperatorMin" style ="position:relative" src="http://www.apexchat.com/images/MinChat.png" />'
            temp_html += '            </td>'
            temp_html += '            <td style="font-size:14px">Live Chat'
            temp_html += '            </td>'
            temp_html += '            <td align="right"><img style="position:relative; padding-left:10px;" src="http://www.apexchat.com/images/expand.png" />'
            temp_html += '            </td>'                       
            temp_html += '        </tr>'
            temp_html += '    </table>'
	        temp_html += '</DIV>' ;
           
   	        minChat.innerHTML =  temp_html ;

	        minChat.style.bottom='0px';
	        
	        if (showInviationOnLeft)
	            minChat.style.left = '0px';
	        else 
	            minChat.style.right='0px';
        	
            
            var imgDiv=document.getElementById("imgOperatorMin"); 
            imgDiv.style.left=0 ;
            imgDiv.style.top=0 ;
    
        ShakeImageMin () ;
        
         //-----------------------
	    // add div for Iframe chat
	    //----------------------------------
	        var iframeChat =document.createElement("div");
	        iframeChat.id="iframeChat";
	        iframeChat.style.zIndex = 1 ;
	        iframeChat.style.position= "absolute";
	        iframeChat.style.width = "425px" ;
	        iframeChat.style.height = "350px" ;
            iframeChat.style.display = "none" ;
             iframeChat.style.bottom='0px';
	        iframeChat.style.right='0px';
	        iframeChat.onmousedown=new Function("event","(event||window.event).cancelBubble=true;");
	        document.body.appendChild(iframeChat);
    
        if (showMinVersion == true)
        {
             minChat.style.display = "block";
             chat_forecolor.style.display = "none";
        }
        else
        {
             minChat.style.display = "none";
             chat_forecolor.style.display = "block";
        }
        
        
        if(window.addEventListener){ // Mozilla, Netscape, Firefox
    	
        } 
        else { // IE
	        document.attachEvent("mousedown", ChatUI_ForeColor_OnDocumentMouseDown) ;
        }
    }
    else 
    {
        setTimeout( "InvitationDivRL('"+companyLogin+"',"+AutoInviteOn+", '"+AutoInviteMessage+"', " + AutoInviteDelay +", '" + AgentPic + "', " + showMinVersion + ")",1000);
    }

}  

function InvitationRLMin ()
{

    var chat_forecolor=document.getElementById("chat_forecolor");
    chat_forecolor.style.display  = 'none' ;
    
    cookiestatus = createCookie ('inviteMinimized', 'yes', 10) ;
    var minChat =document.getElementById("minChat");
    minChat.style.display  = 'block' ;
    

}


function InvitationRLMax ()
{
    var chat_forecolor=document.getElementById("chat_forecolor");
    chat_forecolor.style.display  = 'block' ;
    
    cookiestatus = eraseCookie('inviteMinimized') ;
    
    var minChat =document.getElementById("minChat");
    minChat.style.display  = 'none' ;
}

function ProcessCall (companyLogin) 
{
    var rdValue = document.getElementById ('number1').checked ;
    
    if (rdValue== true)
    {   
        OpenChatWindowWithInitialMessage (companyLogin) ;
    }
    else
    {
        ProcessClick2Call (companyLogin) ;
    }

}
 function OpenChatInIFrame (a, iWidth, iHeight, chaturl)
  { a = document.getElementById(a);
    a.style.display = "block" ;
    
    
       var IF = document.createElement("iframe") ;
	   IF.id = "chatiframe" ;
	   IF.scrolling = "no" ;
	   IF.frameborder = 0 ;
	   IF.style.zIndex =  888888;
	   IF.style.width = "425px" ;
	   IF.style.height= "350px" ; //should be 185px
	   IF.src = chaturl ;
       a.appendChild(IF) ;
     
   }
   
  
  


////////////////////
/// 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 + "')", 1) ;
     
     }
    }
  
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"); 
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) + "pt"
}
else if (a==3){
//imgDiv.style.top=parseInt(imgDiv.style.top)-rector
}
else{
imgDiv.style.left=(parseInt(imgDiv.style.left)-rector) + "pt"
}
if (a<4)
a++
else
a=1
setTimeout("ShakeImage()",500) ;
}
}

    
 var b=1   
function ShakeImageMin () {

        var imgDiv=document.getElementById("imgOperatorMin"); 
                                            
        if (imgDiv !=  null){
            if ((!document.all&&!document.getElementById) )
                return
            if (b==1){
                //imgDiv.style.top=parseInt(imgDiv.style.top)+rector
            }
            else if (b==2){
                imgDiv.style.left=(parseInt(imgDiv.style.left)+rector) + "pt"
            }
            else if (b==3){
                //imgDiv.style.top=parseInt(imgDiv.style.top)-rector
            }
            else{
                imgDiv.style.left=(parseInt(imgDiv.style.left)-rector) + "pt"
            }
            if (b<4)
                b++
            else
                b=1
            setTimeout("ShakeImageMin()",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);
}

var delay = 30;
var Hoffset = 0;
var viewportwidth;
var viewportheight;
var iter = 0;
var setId = 0; 
var down = true; 
var up = false; 
 
 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
 function GetViewPort()
 {
    if (typeof window.innerWidth != 'undefined')
    {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
    }
 
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
 
 // older versions of IE
 
 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }

}




function AnimateGravity(a, msg) {

    var name = navigator.appName;

    if(name == "Microsoft Internet Explorer") name = true;
    else name = false;
    var xPos = 20;
    if(name) var yPos = document.body.clientHeight;
    else var yPos = window.innerHeight;
     
     var inviteDiv =document.getElementById(a); 

      inviteDiv.style.top = document.body.scrollTop;
      Hoffset = inviteDiv.offsetHeight + 'px';
      Hoffset = 170 ;
  var msgDiv=document.getElementById("outgoingmessage");
    
    msgDiv.innerHTML = msg ; 
      setId = setInterval('generateGravity()',delay);
  
    createCookie('shownGravity', 'yes', 5) ;
}

function generateGravity() 
{ 
    GetViewPort() ;
   
    var inviteDiv =document.getElementById('chat_forecolor'); 
    if (inviteDiv == null)
      return ;
      
    inviteDiv.style.left = viewportwidth - inviteDiv.offsetWidth + 'px';
    
    if (showInviationOnLeft)
        inviteDiv.style.left = '0px' ;
        
    if ((parseInt(inviteDiv.style.top )+iter < viewportheight - Hoffset-5) && down) 
    { 
        inviteDiv.style.top  = parseInt(inviteDiv.style.top ) + iter + 'px' ; 
        iter++; 
        return; 
    } 
    else 
    { 
        if ((parseInt(inviteDiv.style.top) < viewportheight - Hoffset) && down) 
            { 
               
                //inviteDiv.style.top  = viewportheight +5 - Hoffset + 'px'; 
                inviteDiv.style.top  = viewportheight - Hoffset + 'px'; 
                return; 
            } 
            down = false; 
            
            up = true; 
            if (iter < 0 && parseInt(inviteDiv.style.top) > viewportheight - Hoffset) 
            { 
                clearInterval(setId); 
                inviteDiv.style.top = viewportheight  - Hoffset - 5 + 'px';
                inviteDiv.style.bottom='0px';
	            inviteDiv.style.right='0px';
                setId = 0; 
             } 
             if (parseInt(inviteDiv.style.top) > 0 && up && iter >= 0) 
             { 
               inviteDiv.style.top  = parseInt(inviteDiv.style.top) - iter + 'px'; 
                iter = iter/2 ;
                iter = iter -1; 
               // alert (iter + ' : ' + iter%3) ;
                if (iter%3 == 0) 
                    iter = iter -1 ; 
                return; 
             } 
             //alert ('change dir') ;
             down = true; 
             up = false; 
          }
} 

