//
//Online chat functions
//

var submittedCustomerName;
var submittedCustomerEmail;
var submittedCustomerQuestion;
var submittedChatTopic;

// Array of all open windows
var windows = new Array();

var chatBaseURL = "http://webchat.automationdirect.com:8080/webchat";
var groupIdString = "workgroup.k8.automationdirect.com";


function getWebChatStatus()
    {
    var webChatStatus = 'false';
    var webStatusObj = new Ajax.Request('/ajax',
            {
                method:'get',
                parameters: {
                    'fctype':'adc.falcon.FormControllers.ChatFormCtrl',
                    'cmd':'ChatStatus'
                },
                onSuccess:
                function(transport) {
                    var json = transport.responseText.evalJSON();
                    var webStatus = json.ResultSet.web.isonline;
                    webChatStatus = webStatus;
                }
            });
    return webChatStatus;
    }



function initChatWindow()
{			  
    $('messageview').hide();
    var getInitialGroupStatus = new Ajax.Request('/ajax',
    {
        method:'get',
        parameters: {
            'fctype':'adc.falcon.FormControllers.ChatFormCtrl',
            'cmd':'ChatStatus'
        },
        onSuccess: function(transport) {
            var json = transport.responseText.evalJSON();
            var techStatus = json.ResultSet.techsupport.isonline;
            var salesStatus = json.ResultSet.sales.isonline;
            var webStatus = json.ResultSet.web.isonline;
            var acctStatus = json.ResultSet.accounting.isonline;
            if (techStatus == true || salesStatus == true || webStatus == true || acctStatus == true)
            {
                getInitialView('online');
            }
            else
            {
                getInitialView('alloffline');
            }
        }
    });
}	
	
function getInitialView(theView)
{
    var theMessage = new Ajax.Updater('initview', '/ajax', {
        method: 'post',
        parameters: {
            'fctype':'adc.falcon.FormControllers.ChatFormCtrl',
            'cmd':'InitialChatView',
            'initialview':theView
        }
    });
    $('chatstatus').show();
    $('initview').show();
}
	
function processInitChatForm()
{ 
    var chatError = false;
    var chatTopic = $F('chattopic');
    var submittedCustomerName = $F('custname');
    if (submittedCustomerName.length < 1)
    {
        chatError = true;
        $('custnameerror').update('Please enter your name.');
        $('custnameerror').show();
    }
    else		
    {
        $('custnameerror').hide();
    }
    var submittedCustomerEmail = $F('custemail');
    if (submittedCustomerEmail.length < 1)
    {
        chatError = true;
        $('custemailerror').update('Please enter your e-mail address.');
        $('custemailerror').show();
    }
    else
    {
        $('custemailerror').hide();
    }
    var submittedCustomerQuestion = $F('chatquestion');
    var selectedWorkgroup;
    $('chattopicerror').hide();
    
    switch (chatTopic)
    {
        case "tech":
            selectedWorkgroup = 'tech';			
            break;
        case "presales":
            selectedWorkgroup = 'tech';
            break;
        case "orderstatus":
            selectedWorkgroup = 'sales';
            break;
        case "quote":
            selectedWorkgroup = 'sales';
            break;            
        case "acctgeneral":
            selectedWorkgroup = 'acct';
            break;
        case "acctinvoice":
            selectedWorkgroup = 'acct';
            break;
        case "acctstatus":
            selectedWorkgroup = 'acct';
            break;
        case "acctopen":
            selectedWorkgroup = 'acct';
            break;            
        case "webordering":
            selectedWorkgroup = 'web';
            break;
        case "webhelp":
            selectedWorkgroup = 'web';
            break;			
        default : 
            chatError = true;
            $('chattopicerror').show();
            $('chattopicerror').update('Please select a help topic');
    }
    if (chatError == false)
    {
	
        if (selectedWorkgroup == "web"){
            var webStatusLookup = new Ajax.Request('/ajax',
            {
                method:'get',
                parameters: {
                    'fctype':'adc.falcon.FormControllers.ChatFormCtrl',
                    'cmd':'ChatStatus'
                },
                onSuccess:
                function(transport) {
                    var json = transport.responseText.evalJSON();
                    var webStatus = json.ResultSet.web.isonline;
                    if (webStatus == true) {
                        
                        getStatusMessage('web', 'online');
                    }
                    else {
                        getStatusMessage('web', 'offline');
                    }
                }
            });
        }

        if (selectedWorkgroup == "sales"){
            var salesStatusLookup = new Ajax.Request('/ajax', 
            {
                method:'get',
                parameters: {
                    'fctype':'adc.falcon.FormControllers.ChatFormCtrl',
                    'cmd':'ChatStatus'
                },
                onSuccess:
                function(transport) {
                    var json = transport.responseText.evalJSON();
                    var salesStatus = json.ResultSet.sales.isonline;
                    if (salesStatus == true) {
                        getStatusMessage('sales', 'online');
                    }
                    else {
                        getStatusMessage('sales', 'offline');
                    }
                }
            });
        }

        if (selectedWorkgroup == "tech"){
            var techStatusLookup = new Ajax.Request('/ajax', 
            {
                method:'get',
                parameters: {
                    'fctype':'adc.falcon.FormControllers.ChatFormCtrl',
                    'cmd':'ChatStatus'
                },
                onSuccess:
                function(transport) {
                    var json = transport.responseText.evalJSON();
                    var techStatus = json.ResultSet.techsupport.isonline;
                    if (techStatus == true) {
                        getStatusMessage('tech', 'online');
                    }
                    else {
                        getStatusMessage('tech', 'offline');
                    }
                }
            });
        }
        
        if (selectedWorkgroup == "acct"){
            var techStatusLookup = new Ajax.Request('/ajax', 
            {
                method:'get',
                parameters: {
                    'fctype':'adc.falcon.FormControllers.ChatFormCtrl',
                    'cmd':'ChatStatus'
                },
                onSuccess:
                function(transport) {
                    var json = transport.responseText.evalJSON();
                    var acctStatus = json.ResultSet.accounting.isonline;
                    if (acctStatus == true) {
                        getStatusMessage('acct', 'online');
                    }
                    else {
                        getStatusMessage('acct', 'offline');
                    }
                }
            });
        }        
    }
    return false;
}
	
	
function launchTechChat(submittedCustomerName, submittedCustomerEmail, submittedCustomerQuestion)
{
    var techStatusLookup = new Ajax.Request('/ajax', {
        method:'get',
        parameters: {
            'fctype':'adc.falcon.FormControllers.ChatFormCtrl',
            'cmd':'ChatStatus'
        },
        onSuccess: function(transport) {
            var json = transport.responseText.evalJSON();
            var techStatus = json.ResultSet.techsupport.isonline;
            if (techStatus == true)
            {
                getStatusMessage('tech', 'online');
            }
            else
            {
                getStatusMessage('tech', 'offline');
            }
        }
    });
}
		
	
function getStatusMessage(workgroup, groupstatus)
{
    $('initview').hide();
    $('messageview').show();
    submittedCustomerName = $F('custname');
    submittedCustomerEmail = $F('custemail');
    submittedCustomerQuestion = $F('chatquestion');    
    submittedTopic = $F('chattopic');
    var theMessage = new Ajax.Updater('messageview', '/ajax', {
        method: 'post',
        parameters: {
            'fctype':'adc.falcon.FormControllers.ChatFormCtrl',
            'cmd':'ChatMessage',
            'workgroup':workgroup,
            'groupstatus':groupstatus,
            'topic':submittedTopic,
            'cn':submittedCustomerName,
            'ce':submittedCustomerEmail,
            'ci':submittedCustomerQuestion
        }
    });
}	
	
function openChat(workgroup, submittedCustomerName, submittedCustomerEmail, submittedCustomerQuestion)
{
    destinationURL = chatBaseURL + "/start.jsp?workgroup=" + workgroup;
    destinationURL += "&cn=" + submittedCustomerName + "&ce=" + submittedCustomerEmail;
    destinationURL += "&cq=" + submittedCustomerQuestion;
    launchWin('framemain', destinationURL, '500', '400');
    hideChatStatus();
}

function hideChatStatus()
{
    $('chatstatus').style.display = 'none';
}  

	
// Open a window given its unique name, url, width and height.
function launchWin(name, url, width, height)
{
    var defaultOptions = "location=no,status=no,toolbar=no,personalbar=no,menubar=no,directories=no,";
    var winleft = (screen.width - width) / 2;
    var winUp = (screen.height - height) / 2;
    defaultOptions += "scrollbars=no,resizable=yes,top=" + winUp + ",left=" + winleft + ",";
    defaultOptions += "width=" + width + ",height=" + height;
    hideChatStatus();
    //launchWinWithOptions(name, url, defaultOptions);
    window.open(url, '', defaultOptions);
}

		
// Open a window with given name, url, and options list
function launchWinWithOptions(name, url, options) 
{
    if (! windowExists(name)) 
    {
        var winVar = window.open(url, name, options);
        windows[windows.length] = winVar;
        return winVar;
    }
    else
    {
        //var theWin = getWindow(name);
        //theWin.focus();
        alert('A chat window is already open.  Please close it if you wish to start over.');
    }
}

