var imageDomain = "https://dankennedy.com/images/";   
preMessage = "Write your questions here...";
parameters = 'width=475,height=400,resizable=yes';
var lpPosY = 100;  

$(document).ready(function(){
    if( $('.lp_msgfield') ) {
        $('.lp_msgfield').val( preMessage );
    
        $('.lp_msgfield').focus(function(){
            if ( $(this).val() == preMessage )
            $(this).val("");
        });
            
        $('.lp_msgfield').blur(function(){
            if ( $(this).val() == "" )
                $(this).val( preMessage );
        });
        
    }
    
    if ( $('.submitLP') ) {
        $('.submitLP').click( function(){
            var formURL = "https://server.iad.liveperson.net/hc/38423120/?";
            var windowName = "chat38423120"; 
            var message = $(this).parent().children('.lp_msgfield').val()          
            var info = Array( new data( 'cmd', 'file' ), new data( 'file', 'chatFrame' ), new data( 'site', '38423120' ), new data( 'x', '53' ), new data( 'y', '5' ), new data( 'SESSIONVAR!survey_pre-chat_enable', 'Off' ), new data( 'SESSIONVAR!source', 'chatbox' ), new data( 'imageUrl', imageDomain ), new data( 'SESSIONVAR!message', message ) );
            
            if ( message == "" )
                alert( 'Before you start your chat, please enter your question' );
            else {
                for( var i = 0; i < info.length; i++ ) {
                    formURL += ( i != 0 ) ?  '&' : '';
                    formURL += escape( info[i].getKey() ) + '=' + escape( info[i].getValue() );
                }
                window.open( formURL, windowName, parameters );            
            }
        });
    }        
});

function data( key, value ) {
    this.key = key;
    this.value = value;
    this.getKey = function(){ return this.key };
    this.getValue = function(){ return this.value };
}

function ChatBox( msg, param, img ) {
    
    preMessage = ( msg == "" ) ? preMessage : msg;
    parameters = ( param == "" ) ? parameters : param;
    imageDomain = ( img == "" ) ? imageDomain : img;
    
    this.showBox = function(){
        var chatbox = "\n<div class='chat_container'>\n\t<img src='https://server.iad.liveperson.net/hc/38423120/?cmd=repstate&site=38423120&channel=web&ver=1&imageUrl=" + imageDomain + "' width='150px' height='80px' />\n\t<textarea class='lp_msgfield' name='SESSIONVAR!message' rows='5'></textarea>\n\t<input type='button' value='Chat with Us' src='images/chatnow.gif' class='submitLP' />\n\t<div class='credit'>\n\t\t<a href='http://utg.infusionsoft.com/go/UTIG/bglazer/' target='_blank'>Ari Galper's ChatWise</a>\n\t</div>\n</div>\n";        
        document.write( chatbox );      
    }  
}