// languaga switch
function switchLang(lang)
{
	document.langswitch.language.value = lang;
	document.langswitch.submit();
}

// pre-course form navigation
function precourseform_navigate(sendto)
{
    document.getElementById('id_send_to').value = sendto
    document.precourseform.submit();
}


function user_courselist(userid,objid)
{
    // look up course list for this user
    var ajax_callback = function(http_response)
    {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                // populate fields
                info_json = eval('(' + http_request.responseText + ')');
                // loop propeties of object (hash array) and populate drop down
                obj = document.getElementById(objid);
                obj.options[obj.options.length] = new Option('---------','');
                for (i in info_json)
                {
                    var cid = info_json[i]['id'];
                    var coursename = info_json[i]['course']
                    // add to drop down
                    obj.options[obj.options.length] = new Option(unescape(coursename),cid);
                    
                }
            }
        }
        document.body.style.cursor = 'default';
    }

    // url
    url = '/courselist_lookup/'+userid;
    // change cursor
    document.body.style.cursor = 'wait';
    // ajax call
    if (window.XMLHttpRequest) { // Mozilla, Safari, IE7...
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE6 and older
       http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    http_request.onreadystatechange = ajax_callback;
    http_request.open('GET', url, true);
    http_request.send(null);

}

function user_conferencelist(userid)
{
    // look up course list for this user
    var ajax_callback = function(http_response)
    {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                // populate fields
                info_json = eval('(' + http_request.responseText + ')');
                // loop propeties of object (hash array) and populate drop down
                obj = document.getElementById('id_conference');
                obj.options[obj.options.length] = new Option('---------','');
                for (i in info_json)
                {
                    var cid = info_json[i]['id'];
                    var confname = info_json[i]['conference']
                    // add to drop down
                    obj.options[obj.options.length] = new Option(unescape(confname),cid);
                    
                }
            }
        }
        document.body.style.cursor = 'default';
    }

    // url
    url = '/conferencelist_lookup/'+userid;
    // change cursor
    document.body.style.cursor = 'wait';
    // ajax call
    if (window.XMLHttpRequest) { // Mozilla, Safari, IE7...
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE6 and older
       http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    http_request.onreadystatechange = ajax_callback;
    http_request.open('GET', url, true);
    http_request.send(null);

}


function sendAjaxLogin()
{
    // change cursor
    document.body.style.cursor = 'wait';
    // clear err msg
    document.getElementById('ajaxloginerr').style.display = 'none';
    // disable submit button
    document.getElementById('login_submit').disabled=true;

    var ajax_callback = function(http_response)
    {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                // populate fields
                response = eval('(' + http_request.responseText + ')');

                if (response == false)
                {
                    // show error msg
                    document.getElementById('ajaxloginerr').style.display = '';
                    // change cursor
                    document.body.style.cursor = 'pointer';
                    // activate submit button
                    document.getElementById('login_submit').disabled=false;
                }
                else
                {
                    // login good redirect
                    window.location = response;
                }
            }
        }
        // change cursor
        document.body.style.cursor = 'default';
    }

    // url
    username = document.getElementById('ajax_username').value
    password = document.getElementById('ajax_password').value

    if (password.length > 0)
    {
        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
        newpass = ''
        strcount = 1
        for (var i=0;i<password.length;i++)
        {
            newpass += password.charCodeAt(i);
            if (strcount < password.length)
            {
                var rnum = Math.floor(Math.random() * chars.length);
        		randomchar = chars.substring(rnum,rnum+1);
                newpass += randomchar
            }
            strcount++;
        }
        password = newpass
    }

    url = '/login/ajax/?username=' + username + '&password=' + password;
    // ajax call
    if (window.XMLHttpRequest) { // Mozilla, Safari, IE7...
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE6 and older
       http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    http_request.onreadystatechange = ajax_callback;
    http_request.open('GET', url, true);
    http_request.send(null);
}

// Function used to submit any form on the site
//
// It will disable the submit button, add an hidden input to the form with the name of the submit
// button passed (used in some cases for different post actions. ex - assign questionnaire (if request.POST.__contains__('btnsubmit_course'):))

function submitForm(btn)
{
    var hiddenField = document.createElement("input");
    hiddenField.type = 'hidden';
    hiddenField.name = btn.name;
    hiddenField.value = btn.name;
    
    // add to the form
    btn.form.appendChild(hiddenField);
    // disable submit button
    btn.disabled = true;
    // submit the form
    btn.form.submit();
}

// function that adds an onclick event on submit buttons
function addSubmitOnclick()
{
    var document_forms = document.forms;
    for (var f=0;f<document_forms.length;f++)
    {
        // get current form elements
        currElements = document_forms[f].elements;
        for (var e=0;e<currElements.length;e++)
        {
            if (currElements[e].type == 'submit')
            {
                // add onclick event
                currElements[e].onclick = function() { submitForm(this); return false; }
            }
        }
    }
}

$(function () {
    $('div.sticker marquee').marquee('pointer').mouseover(function () {
        $(this).trigger('stop');
    }).mouseout(function () {
        $(this).trigger('start');
    }).mousemove(function (event) {
        if ($(this).data('drag') == true) {
            this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
        }
    }).mousedown(function (event) {
        $(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
    }).mouseup(function () {
        $(this).data('drag', false);
    });
});

/* Function used to share link to Facebook */
function fbs_click() {
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}


