
var scriptLoaded = 0 ;
var cookiestatus = "success" ;
var baseurl_dyns =  "http://www.apexchat.com/" ;




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 ;

var cwu= "" ;
var invlogo = "" ;

function autoInitiatedCall (operatorName, companyLogin, AutoInviteOn, AutoInviteMessage, 
AutoInviteStyle, AutoInviteDelay, AgentPic, AgentAlias, MediaExchange)
{

    var existingWindow = document.getElementById ('chat_forecolor') ;
      
      if (existingWindow == null) {  
       
        if (MediaExchange=='me' || companyLogin == 'floorsusa')
        {
          cwu = 'http://www.ReachLocalLiveChat.com/' ;
          invlogo = 'rl_logo.jpg); width:115px;height:22px;' ;
         // invlogo = 'rl_chat_2.jpg); width:70px;height:22px;' ;
        }
        else
        {         
          cwu = 'http://www.apexchat.com/' ;
          invlogo = 'apexlogo.png); width:0px;height:0px;';
         } 
          
        if (AutoInviteOn == "1")
        {
         
            var chatStartedCookie = readCookie ('chatStarted') ;
            var lastInvitation = readCookie ('lastInvite') ;
            if (lastInvitation == null && chatStartedCookie == null && cookiestatus != "fail"){
            
             
                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" || AutoInviteStyle == "10" || AutoInviteStyle == "11")
                {
                    showInviationOnLeft = true; 
                }
                
                if (AutoInviteStyle == "20" )
                {
                    showTopMiddle = true; 
                }
                
                 if (AutoInviteStyle == "4" || AutoInviteStyle == "5" || AutoInviteStyle == "6" || AutoInviteStyle == "7" || AutoInviteStyle == "20"  )  
                {
                
                    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" || AutoInviteStyle == "20")
                       createCookie('shownGravity', 'yes', 999) ;
                        
                    if (AutoInviteDelay == 0 || readCookie('lastInvite') != null)    
                            setTimeout ("InvitationDivRL ('" + companyLogin + "', " + AutoInviteOn + ", '" + AutoInviteMessage 
                                + "', " + AutoInviteDelay + ", '" + AgentPic + "', " + showMinVersion + ", '" + AgentAlias + "')" , 1500) ;           
                    else                       
                            setTimeout ("InvitationDivRL ('" + companyLogin + "', " + AutoInviteOn + ", '" + AutoInviteMessage 
                                + "', " + AutoInviteDelay + ", '" + AgentPic + "', " + showMinVersion + ", '" + AgentAlias + "')" , AutoInviteDelay*1000) ; 
                                
                    if (AutoInviteDelay == 0 )
                        cookiestatus = createCookie ('lastInvite', 'yes', 1000) ;
                    else  
                        cookiestatus = createCookie ('lastInvite', 'yes', 5) ;
                }
                
                if (AutoInviteStyle == "8" || AutoInviteStyle == "9" || AutoInviteStyle == "10" || AutoInviteStyle == "11" )  
                {
                
                    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 == "9" || AutoInviteStyle == "11")
                       createCookie('shownGravity', 'yes', 999) ;
                        
                    if (AutoInviteDelay == 0 || readCookie('lastInvite') != null)    
                            setTimeout ("InvitationDivRLSmall ('" + companyLogin + "', " + AutoInviteOn + ", '" + AutoInviteMessage 
                                + "', " + AutoInviteDelay + ", '" + AgentPic + "', " + showMinVersion + ", '" + AgentAlias + "')" , 1500) ;           
                    else                       
                            setTimeout ("InvitationDivRLSmall ('" + companyLogin + "', " + AutoInviteOn + ", '" + AutoInviteMessage 
                                + "', " + AutoInviteDelay + ", '" + AgentPic + "', " + showMinVersion + ", '" + AgentAlias + "')" , AutoInviteDelay*1000) ; 
                                
                    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 == "2" )  {
        setTimeout ("InvitationDiv2 ('" + companyLogin + "', " + AutoInviteOn + ", '" + AutoInviteMessage + "', " + AutoInviteDelay + ", '" + AgentPic + "')" , 3000) ;           
    }
           
 

}

 

function OpenChatWindow (companyLogin) {
	var chaturl = cwu + "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 = cwu + "cr.aspx?login=" + companyLogin + "&aa=" + agentAlias + "&ngn=true&ru=" + escape(location.href);
	var chaturlSmall = baseurl_dyns + "crSmall.aspx?login=" + companyLogin + "&aa=" + agentAlias + "&ngn=true&ru=" + escape(location.href) ;
	
//	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 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 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+ '.jpg" />'
    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);
}

}  



var showTopMiddle = false;
function InvitationDivRL (companyLogin, AutoInviteOn, AutoInviteMessage, AutoInviteDelay, AgentPic, showMinVersion, agentAlias)
{
    
    if (document.body != null) {
    
        AttachScrollEvent() ;
        window.onresize= RepositionInvite;
        
        var chat_forecolor=document.createElement("div");
	    chat_forecolor.id="chat_forecolor";
	    chat_forecolor.style.zIndex = 99999 ;
	    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 = "175px" ;

	    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 height="115px" >'
        temp_html += '            <td width="80px" height="115px" style="background-image: url(\'http://www.apexchat.com/images/AgentPics/' + AgentPic+ '.jpg\'); background-repeat: no-repeat;"><img style="display:none;" height="0px" width="0px" src="' + irUrl + '" /><img id="imgOperator" style="position:relative;" height = "0px" width="0px" src="http://www.apexchat.com/images/AgentPics/' + AgentPic+ '.jpg"><//>'
        temp_html += '            </td>'
        temp_html += '            <td align="center" style="font-family: Tahoma, Arial, Helvetica, sans-serif;font-size: 11px;padding: 0px;"><br/><br/>Live Associate 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>'
                   
        temp_html += '        </tr>'
        temp_html += '        <tr height="25px">'
        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-left:10px;"><div style="background-repeat: no-repeat; cursor: hand; width:16px;height:16px; " OnClick = "ChatUI_ForeColor_OnDocumentMouseDown();"><img src="http://www.apexchat.com/images/close.png" /></div>'
        temp_html += '            </td>'  
        temp_html += '            <td align="right" style="border:solid 0px #999999;" ><a href="' + cwu + '" 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/'+ invlogo + '"></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, 180 ) ;
	    else 
		{
		chat_forecolor.style.bottom='0px';
		if (showInviationOnLeft)
		    chat_forecolor.style.left ='0px';
		else
	      chat_forecolor.style.right='0px';
	      
	      if (showTopMiddle)
	      {
	        chat_forecolor.style.left = null ;
	        chat_forecolor.style.right = null ;
	        
	        GetViewPort() ;
	        chat_forecolor.style.top = '0px' ;
	        var leftPos = parseInt (viewportwidth/2) - 125 ;
	       //alert (leftPos) ;
	        chat_forecolor.style.left = leftPos + 'px' ;
	      }
	      
	      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 InvitationDivRLSmall (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 = "140px" ;
	   // 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="" ;
	    
	        temp_html += '<div id = "minRootInvite" OnClick = "OpenChatWindowRL(\'' + companyLogin + '\', \'' + agentAlias + '\');" 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/AgentPics/' + AgentPic+ '_small.jpg" />'
            temp_html += '            </td>'
            temp_html += '            <td style="padding-left: 5px;font-size:12; text-decoration:underline;">Start Live Chat'
            temp_html += '            </td>'
            temp_html += '            <td align="right"><div id="divClose" OnClick = "ChatUI_ForeColor_OnDocumentMouseDown();"><img style="position:relative; padding-left:10px;" src="http://www.apexchat.com/images/close.png" /></div>'
            temp_html += '            </td>'                       
            temp_html += '        </tr>'
            temp_html += '    </table>'
	        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, 40 ) ;
	    else 
		{
		chat_forecolor.style.bottom='0px';
		if (showInviationOnLeft)
		    chat_forecolor.style.left ='0px';
		else
	      chat_forecolor.style.right='0px';
	      
	      AnimateDivSmall ('chat_forecolor', 0 , 140, 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( "InvitationDivRLSmall('"+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' ;
}

    
    //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 AnimateDivSmall (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 > 70))      
     setTimeout( "AnimateDivSmall('"+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) ;
}





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 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, HOff) {

    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 = HOff ;
      
      var msgDiv = null ;
      if (document.getElementById("outgoingmessage") != null)
      {
            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';
                 if (!showInviationOnLeft)
                {
                    inviteDiv.style.bottom='0px';
	                inviteDiv.style.right='0px';
	                inviteDiv.style.top = null ;
	                inviteDiv.style.left = null ;
	            }
	            else 	            
	            {
	                inviteDiv.style.bottom='0px';
	                inviteDiv.style.right= null;
	                inviteDiv.style.top = null ;
	                inviteDiv.style.left = '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; 
          }
} 



function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function AttachScrollEvent(){
    
    XBrowserAddHandler("scroll",RepositionInvite);
  
}

function RepositionInvite()
{
    GetViewPort() ;
    var inviteDiv = null ;
    
    if (document.getElementById('chat_forecolor') != null)
    {
        inviteDiv = document.getElementById('chat_forecolor') ;
        if (showTopMiddle)
	      {
	        inviteDiv.style.left = null ;
	        inviteDiv.style.right = null ;

	        inviteDiv.style.top = '0px' ;
	        var leftPos = parseInt (viewportwidth/2) - 125 ;
	       //alert (leftPos) ;
	        inviteDiv.style.left = leftPos + 'px' ;
	      }
	      else
	      {
                
                inviteDiv.style.top  = viewportheight + f_scrollTop() - 180 + 'px' ; 
          }
    } 
    
  
    
}

function XBrowserAddHandler(eventName,handlerName) 
{ 
if ( window.addEventListener ) 
window.addEventListener(eventName, handlerName, false); 
else if ( window.attachEvent ) 
window.attachEvent("on" + eventName, handlerName); 
else 
window["on" + eventName] = handlerName; 
} 

