var files = "";

function openContact(element_id) {
    // Find the type in the id of the clicked element
    var regexp = new RegExp("[a-z]+$");
    contact_type = regexp.exec(element_id);

    closeTooltips();
    clickedElement = document.getElementById(element_id);

    arr_position = getPosition(clickedElement);
    arr_size = getDimension(clickedElement);

    posX = arr_position['left'] - 454;
    posY = arr_position['top'];

    new ajaxLoader('tooltip', 'customer_service.php?popup=' + contact_type).execute();
    $("#tooltip").css("display", "block");

    $("#tooltip").css('left', posX);
    $("#tooltip").css('top', posY);
}

function prepareUpload(event) {
    files = event.target.files;
}

function processForm() {
    var error = 0;
    var data = new FormData();
    $.each(files, function(key, value) {
        data.append('contact_attachement', value);
    });


    data.append('contact_name', document.getElementById("contact_name").value);
    data.append('contact_email', document.getElementById("contact_email").value);
    data.append('research_link', document.getElementById("research_link").value);
    data.append('research_subject', document.getElementById("research_subject").value);
    data.append('research_date', document.getElementById("research_date").value);
    data.append('contact_subject', document.getElementById("contact_subject").value);
    data.append('contact_message', document.getElementById("contact_message").value);
    data.append('contact_category', document.getElementById("contact_category").value);

    $.ajax({
        url: "customer_service.php?popup=form_submit",
        type: 'POST',
        data: data,
        cache: false,
        processData: false, // Don't process the files
        contentType: false,
        // callback handler that will be called on success
        success: function(response, textStatus, jqXHR) {
            // log a message to the console
            document.getElementById('contact_form').innerHTML = response;
        }
    });
}
function formConfirm() {
    var data = new FormData();
    $.each(files, function(key, value) {
        data.append('contact_attachement', value);
    });

    data.append('confirm', 'true');

    $.ajax({
        url: "customer_service.php?popup=form_submit",
        type: "post",
        cache: false,
        processData: false, // Don't process the files
        contentType: false,
        data: data,
        // callback handler that will be called on success
        success: function(response, textStatus, jqXHR) {
            // log a message to the console
            document.getElementById('contact_form').innerHTML = response;
        },
    });
}

function openChat() {
    try {
        //get the click and pass it to the unreachable div
        $("#chat_button_extern > div").click();
    } catch (err) {

    }
}
function mouseOver() {
    try {
        //check to see if the length of the image given by ladesk is larger then 0 (if it exists or not)
        if ($("#chat_button_extern > div > img").length != 0) {
            //change cursor to the finger
            $('#contact_chat').css('cursor', 'pointer');
        }
        //lighten up image
        $("#chat_button_extern > div").css("opacity", "0.6");
    } catch (err) {

    }
}
function mouseOut() {
    try {
        //change image back
        $("#chat_button_extern > div").css("opacity", "1");
    } catch (err) {

    }
}

var chatcheckcount;
function loadDelay() {
    chatcheckcount = 0;
    setTimeout('changeOffline()', 2000);
}

function changeOffline() {
    try {
        if ($('#chat_button_extern > div > img').length != 0) {
            $("#chat_time").html("Online");
        } else {
            chatcheckcount++;
            //try again 4 times
            if (chatcheckcount < 5) {
                setTimeout('changeOffline()', 3000);
            }
        }
    } catch (err) {

    }
}

loadDelay();