      //Set initial variables used
      var messageChatArray = {}, responseChatArray = {}, currentChatArray = {},  arrayInUse = 0, messageChatCount = 1,  
      userStartChatMessage = "<font class='messageUser'>You: </font>", 
      triedToExitChat = false, endOfCurrentChatArray = false, currentChatTime, currentChatTimeTyping, currentChatTimeClear,
      chatTypingMessage = '<p>Agent is typing...</p>', chatEndedMessage = '<strong>/*** CHAT HAS ENDED ***/</strong>', 
      startChatMessage = "<font class='messageCPU'>Heidi says: </font>", finishChatMessage = "<br /><br />";
      
      //Messages stored in an array ["string to pass", amount of time to leave the previous statement up for, amount of time for agent to type]
      messageChatArray[0] = ["Hey wait!  We sure hate to see you go.  Don't forget, you're getting a FREE trial of an affiliate website that gives you everything others pay hundreds of dollars for – all just for the price of your own dotcom address!  I know you like the sound of getting checks every month from ebay, Amazon, Sony and Travelocity!", 58000, 6000 ];
      messageChatArray[1] = ["Please type 'HI' or 'HELLO' in the space below to let me know you are there...", 32000, 6000 ];
      messageChatArray[2] = ["There's few legitimate businesses you could try for FREE that will let you make money from home day and night with a marketplace that includes the entire world.", 11000, 5000 ];
      messageChatArray[3] = ["Just let me know you are there...", 15000, 5000 ];
      messageChatArray[4] = ["Whenever you are ready, just type 'HI' or 'HELLO' and we can get started...", 11000, 3000 ];
      messageChatArray[5] = ["This will only take a few moments.", 11000, 4000 ];
      responseChatArray[0] = ["Hi, thanks for chatting with me today...  Most people are amazed when they see their own affiliate website for the first time.  And they really like it when the money starts coming in too! If it turns out that you're not satisfied for any reason, you'll be able to just call or email us and we'll refund the $9.95, and you can still keep your dotcom address.", 1000, 9000 ];
      responseChatArray[1] = ["We've been building custom websites for a long time and we decided to release this Free Trial offer for a limited time because we wanted to make it affordable for more people to make money in this economy.  We're securely linked up with GoDaddy, so we reserve and purchase the dotcom you choose in an instant, automatically.  If you're not happy during your 10-day trial and want to quit, you still get to keep your dotcom name just for giving affiliate marketing a try.  That's why it's risk free. What's the worst that can happen?  If you'd like to get started now, you can just click the X at the top of this box and you'll go right back to our secure order form.", 29000, 9000 ];
      responseChatArray[2] = ["We'll work really hard to keep your business. We make it easy to get started and our support staff is always available to you.  We give a great guarantee because once you see how to make money this way, you won't want to stop. If you're ready to get started now, just click the X at the top of this box to go back to our secure order form." , 40000, 15000 ];
      responseChatArray[3] = ["These companies pay out millions of dollars in affiliate commission every month.  Not everybody can corner the market, but you have to admit it would sure be nice you get your slice of the pie. I hope you decide to get started before they decide to retire this special Trial offer!  Oh, and don't forget to click the X at the top of this box and you'll go right back to our secure order form.", 31000, 12000 ];
      

      //Launches on trying to leave the page event
      function goodbyeChat(e) {
         //Makes sure they havent done this already
         if(!triedToExitChat){
                    setChatArrayInUse(0);
                    setCPUMessageText(currentChatArray[0][0]);
                      
            	if(!e){ e = window.event; }
            	//Check Based on the whether they submitted the form so the form can be submitted
            	if(!document.getElementById('submit').disabled){
                     document.getElementById('departure').style.display = 'block';

                     setChatTimedMessaging(); 
                     triedToExitChat = true;
                  	//e.cancelBubble is supported by IE - this will kill the bubbling process.
                  	e.cancelBubble = true;
                  	
                  	//e.stopPropagation works in Firefox.
                  	if (e.stopPropagation) {
                  		e.stopPropagation();
                  		e.preventDefault();
                  	}
                  	retVal = '********************************************\n************************************************\n\n';
                  	retVal += 'Hey wait! We sure hate to see you go. \n';
                  	retVal += 'Click cancel below to chat with an agent.';
                  	retVal += '\n\n************************************************\n*********************************************';
                  	return retVal; //This is displayed on the dialog
                  
              	}
            }
       }
    //Listener for on page close/page change   
    window.onbeforeunload=goodbyeChat;

      //Sets the Computer or Agent Message to the divScroll
      function setCPUMessageText(message){
            document.getElementById('htmlInput').innerHTML += startChatMessage + message + finishChatMessage;
      }
      
      //Sets variables involved with running through the array and displaying the message
      function addToChatMessage(){
      try{
              getChatMessageEndOfList();
              if( endOfCurrentChatArray ){ clearAgentTypingMessage();  }
              else{ 
                     setCPUMessageText(currentChatArray[messageChatCount][0]);
                     messageChatCount += 1; 
                     getChatMessageEndOfList();
                     scrollChatDIV();
                     setChatTimedMessaging();
                  }
         }catch(ex){
              clearAgentTypingMessage(); 
          }
      }
      
      //Causes the time in between periods for the setting of the divScroll and the Agent Typing
      function setChatTimedMessaging(){
           clearAgentTypingMessage();
           currentChatTimeClear = setTimeout("clearAgentTypingMessage()", currentChatArray[messageChatCount][1] + currentChatArray[messageChatCount][2] );
           currentChatTimeTyping = setTimeout("setAgentTyping()", currentChatArray[messageChatCount][1] );
           currentChatTime = setTimeout("addToChatMessage()", currentChatArray[messageChatCount][1]  + currentChatArray[messageChatCount][2]  );  
      }
      
      //Sets status of the current array
      function getChatMessageEndOfList(){
      try{
          if( currentChatArray[messageChatCount][0] == undefined){ endOfCurrentChatArray = true; }
          else{  endOfCurrentChatArray = false; }
          }catch(ex){
            endOfCurrentChatArray = true;
          }
      }
      
      //Changes the current array in use
      function setChatArrayInUse(arrayChatToUse){
        if(arrayChatToUse==0){ currentChatArray = messageChatArray;}
        if(arrayChatToUse==1){ currentChatArray = responseChatArray; }  
        arrayChatInUse = arrayChatToUse;
      }
      
      //Sets the scroll DIV to the bottom
      function scrollChatDIV(){
          var objDiv = document.getElementById("divScroll");
              objDiv.scrollTop = objDiv.scrollHeight;
      }
 
      //Sets user chat message to divScroller
      function userChatAddMessage(){
          document.getElementById('htmlInput').innerHTML += userStartChatMessage + document.getElementById('userMessage').value + finishChatMessage;
          document.getElementById('userMessage').value = '';
          scrollChatDIV();
            
            //Switches arrays if first message sent by user
            if(arrayChatInUse!=1){
              clearTimeout(currentChatTime);
              clearTimeout(currentChatTimeTyping);
              clearTimeout(currentChatTimeClear);
              setChatArrayInUse(1);
              messageChatCount = 0;
              setChatTimedMessaging();
            }
            else if(arrayChatInUse==1 && endOfCurrentChatArray){
                chatEnded();
            }
      }
      
      //Closes the chat window
      function closeChatWindow(){
         document.getElementById('departure').style.display = 'none';
      }
      
      //Closes the box if a message is sent and we have finished displaying all the messages
      function chatEnded(){
         document.getElementById('htmlInput').innerHTML += chatEndedMessage;
         scrollChatDIV();
         setTimeout("closeChatWindow()", 5000);
      }
      
      //Sets the Agent Typing message on
      function setAgentTyping(){
        element = document.getElementById('agentTypingMessage');
                  element.innerHTML = chatTypingMessage;
      }
      
      //Sets the Agent Typing message off
     function clearAgentTypingMessage(){
        element = document.getElementById('agentTypingMessage');
                  element.innerHTML = '';
      }
      
      