var uchat_popup;
var t1;
var t2;
var root_url = "http://www.uhaul.com/uchat/";

$(document).ready(function() {




    if (getRootURL().indexOf("localhost") > 0) {

        root_url = "";
    }

    if (getRootURL().indexOf("beta.") > 0) {

        root_url = "http://beta.uhaul.com/uchat/";
    }

  

    //Run check for a moderator right away
    check_if_moderator_active();


   
    
    // root_url = getRootURL() + "uchat/";


});



function getRootURL() {
    var baseURL = location.href;
    var rootURL = baseURL.substring(0, baseURL.indexOf('/', 7));

    // if the root url is localhost, don't add the directory as cassani doesn't use it
    if (baseURL.indexOf('localhost') == -1) {
        return rootURL + "/AppName/";
    } else {
        return rootURL + "/";
    }
}


function uchat_show_chat() {


    $("chat_request_dialog").show();

}



function open_new_chat_box(site_name) {

    
    uchat_txt_user_name = document.getElementById('uchat_txt_user_name').value.replace(/'/, "`");
    if (uchat_txt_user_name!="") {

        //Open new window       
        try {
            uchat_popup = window.open("client_popup.aspx?guid_user_session=new&site_name=" + site_name + "&user_name=" + uchat_txt_user_name, 'uchat_popup', 'menubar=no,location=no,status=no,directories=no,resizable=1,width=350,height=425');
            uchat_popup.focus();
        }
        catch (e) {

        }
    }
    else {// no username entered
    
        alert("Please enter a value for username.");
        document.getElementById('uchat_txt_user_name').focus();
    }
    
}




function open_new_chat_box_pick_name() {

    //Check if they already have a chat session opoen, if so return
    try {
        if (uchat_popup != null && !uchat_popup.closed) { //Window open

            alert("You already have a live chat window open");
            uchat_popup.focus();
            return;
        }
    }
    catch (e) {        

    }
    //Open new window
    try {
    
        uchat_popup = window.open(root_url + "client_pick_name.aspx?guid_user_session=new&site_name=" + document.getElementById('uchat_site_name').value, 'uchat_popup', 'menubar=no,location=no,status=no,directories=no,resizable=1,width=350,height=425');
        uchat_popup.focus();
    }
    catch (e) {
    }
}



//Pops up window if moderator logged in
function if_moderator_active_for_site_popup() {

    try {

        $.ajax({

            type: "POST",
            url: root_url + "uchat.asmx/client_are_there_active_admins",
            data: "{'site_name':'" + document.getElementById('uchat_site_name').value + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            success: function(msg) {

                if (msg.d == "0") { //No moderator


                    //return false;
                }
                else { // Got a moderator, show

                    open_new_chat_box_pick_name();
                }
            }
        });
    }
    catch (e) {

        // If errors show it
        open_new_chat_box_pick_name();
    }

}



// Do we have an active moderator to talk to
function check_if_moderator_active() {

    try {

        $.ajax({

            type: "POST",
            url: root_url + "uchat.asmx/client_are_there_active_admins",
            data: "{'site_name':'" + document.getElementById('uchat_site_name').value + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            success: function(msg) {

                if (msg.d == "0") { //No moderator


                    // Hide live chat button

                    document.getElementById('uchat_live_chat').style.display = 'none';

                }
                else { // Got a moderator, show

                    document.getElementById('uchat_live_chat').style.display = '';
                }

                //t2 = setTimeout('check_if_moderator_active()', 5000);
            }
        });
    }
    catch (e) {

        // If errors show it
        document.getElementById('uchat_live_chat').style.display = '';
    }
    
    
    
}



// Is window closed.  End session?
function check_if_closed_window() {

    if (uchat_popup && uchat_popup.closed ) {  //Closed, end session stuff

        //Start checking for active moderators
        //t2 = setTimeout('check_if_moderator_active()', 100);                
        //Close the session
        if (document.getElementById('logged_in_guid').value != "") { // we have a guid, close it

            form_ajax_request_end_session();
        }               
        uchat_popup = null;
    }
    else {

        //t1 = setTimeout('check_if_closed_window()', 3000);        
    }
}



//End the session.
function form_ajax_request_end_session() {


    $.ajax({

        type: "POST",
        url: root_url + "uchat.asmx/user_end_chat",
        data: "{'guid_user_session':'" + document.getElementById('logged_in_guid').value + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json" // not using success callback

    });

    //reset guid
    document.getElementById('logged_in_guid').value = "";
}



function open_existing_chat_box(site_name) {

    if (uchat_popup) {


        if (!uchat_popup.closed) {
            uchat_popup.focus();
        }
    }

}
