// CR BORIS: Use jquery
    var xmlHttp_login;

    function submit_login() {
        var username = escape(document.getElementById("userNameInput").value);
        var password = escape(document.getElementById("passWordInput").value);

        try {
            xmlHttp_login = new XMLHttpRequest();
        } catch (error) {
            try {
                xmlHttp_login = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(error1) {
                xmlHttp_login = null;
            }
        }

        if(xmlHttp_login != null) {
            xmlHttp_login.onreadystatechange = process_ajax_login;
            var ajax_path = "login.php?user="+username+"&pass="+password+"&ajax=1";
            xmlHttp_login.open("GET",ajax_path,true);
            xmlHttp_login.send(null);
            return false;
        }
        return true;
    }

    function process_ajax_login() {
        if(xmlHttp_login.readyState==4) {
            if(xmlHttp_login.status == 200 || xmlHttp_login.status == 304) {
                var xmlDoc_login = xmlHttp_login.responseXML.documentElement;
                var status = xmlDoc_login.getElementsByTagName("status")[0].childNodes[0].nodeValue;
                var messageText = xmlDoc_login.getElementsByTagName("message")[0].childNodes[0].nodeValue;

                switch(status) {
                    case "ok":
                        switch_content('login_area_logged.inc.php', 'login-area');
                        break;
                    case "error":
                        document.getElementById("loginErrorMsg").innerHTML = messageText;
                        break;
                }
            } else {
                document.getElementById("loginErrorMsg").innerHTML = "An Error has occured.";
            }
        }
    }



    function submit_login2() {
        var xmlHttp_login2;
        var username = escape(document.getElementById("userNameInput2").value);
        var password = escape(document.getElementById("passWordInput2").value);

        document.getElementById("loginErrorMsg2").innerHTML = '';

        try {
            xmlHttp_login2 = new XMLHttpRequest();
        }
        catch (error) {
            try {
                xmlHttp_login2 = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(error1) {
                xmlHttp_login2 = null;
            }
        }

        if(xmlHttp_login2 != null) {
            xmlHttp_login2.onreadystatechange = function() {
                if(xmlHttp_login2.readyState==4) {
                    if(xmlHttp_login2.status == 200 || xmlHttp_login2.status == 304) {
                        var xmlDoc_login2 = xmlHttp_login2.responseXML.documentElement;
                        var status = xmlDoc_login2.getElementsByTagName("status")[0].childNodes[0].nodeValue;
                        var messageText = xmlDoc_login2.getElementsByTagName("message")[0].childNodes[0].nodeValue;

                        switch(status) {
                            case "ok":
                                switch_content('box_music_market_news.inc.php', 'right-col-box');
                                switch_content('login_area_logged.inc.php', 'login-area');
                                break;
                            case "error":
                                document.getElementById("loginErrorMsg2").innerHTML = messageText;
                                break;
                        }
                    } else {
                        document.getElementById("loginErrorMsg2").innerHTML = "An Error has occured.";
                    }
                }
            };

            var ajax_path = "login.php?user="+username+"&pass="+password+"&ajax=1";
            xmlHttp_login2.open("GET",ajax_path,true);
            xmlHttp_login2.send(null);
            return false;
        }
        return true;
    }


    var xmlHttp_logout;
    function submit_logout() {
        try {
            xmlHttp_logout = new XMLHttpRequest();
        } catch (error) {
            try {
                xmlHttp_logout = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(error1) {
                xmlHttp_logout = null;
            }
        }

        if(xmlHttp_logout != null) {
            xmlHttp_logout.onreadystatechange = process_ajax_logout;
            var ajax_path = "logout.php?&ajax=1";
            xmlHttp_logout.open("GET",ajax_path,true);
            xmlHttp_logout.send(null);
            return false;
        }
        return true;
    }

    function process_ajax_logout() {
        if(xmlHttp_logout.readyState==4) {
            if(xmlHttp_logout.status == 200 || xmlHttp_logout.status == 304) {
                var xmlDoc_logout = xmlHttp_logout.responseXML.documentElement;
                var status = xmlDoc_logout.getElementsByTagName("status")[0].childNodes[0].nodeValue;
                var messageText = xmlDoc_logout.getElementsByTagName("message")[0].childNodes[0].nodeValue;

                // document.getElementById("loginBox").innerHTML = messageText;
                switch_content('login_area_logoff.inc.php', 'login-area');
            } else {
                // document.getElementById("loginBox").innerHTML = "An Error has occured.";
            }
        }
    }

    function submit_forgot_password() {
        var email = escape(document.getElementById("forgot_password_email").value);

        var url = "passwordreset.php?email="+email+"&ajax=1";
        $.ajax({
            cache: false,
            url: url,
            beforeSend: function(){
                $("#pwd_load_indicator_bg").removeClass("div_off");
                $("#pwd_load_indicator_bg").css("height", $(window).height() + 20+ "px");
                $("#pwd_load_indicator_bg").addClass("div_on");
                
                $("#pwd_load_indicator").removeClass("div_off");
                $("#pwd_load_indicator").css("margin-top", $(window).height()/2 + "px");
                $("#pwd_load_indicator").addClass("div_on");
            },
            success: function (data) {
                $("#pwd_load_indicator_bg").removeClass("div_on");
                $("#pwd_load_indicator_bg").addClass("div_off");
                $("#pwd_load_indicator").removeClass("div_on");
                $("#pwd_load_indicator").addClass("div_off");
  
                var status = data.getElementsByTagName("status")[0].childNodes[0].nodeValue;
                var messageText = data.getElementsByTagName("message")[0].childNodes[0].nodeValue;
                switch(status) {
                    case "ok":
                        $('.message').html("Password reset was successful, please check your email");
                        $('.message').slideDown();
                        break;
                    case "error":
                        $('.error').html(messageText);
                        $('.error').slideDown();
                        break;
                }
            },
        error: function () {
            $("#pwd_load_indicator_bg").removeClass("div_on");
            $("#pwd_load_indicator_bg").addClass("div_off");
            $("#pwd_load_indicator").removeClass("div_on");
            $("#pwd_load_indicator").addClass("div_off");
  
            throw "Server return error in function perform_registration.";            
        }
  
        });
    }


    function create_channel() {
        var xmlHttp_request;
        var channel_name = encodeURIComponent(document.getElementById("NewChannelName").value);
        var channel_desc = encodeURIComponent(document.getElementById("NewChannelDesc").value);

        document.getElementById("ErrorCreateChannelName").innerHTML = "";
        document.getElementById("ErrorCreateChannelDesc").innerHTML = "";

        try {
            xmlHttp_request = new XMLHttpRequest();
        } catch (error) {
            try {
                xmlHttp_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(error1) {
                xmlHttp_request = null;
            }
        }

        if(xmlHttp_request != null) {
            xmlHttp_request.onreadystatechange = function() {
                if(xmlHttp_request.readyState==4) {
                    if(xmlHttp_request.status == 200 || xmlHttp_request.status == 304) {
                        var xmlDoc_response = xmlHttp_request.responseXML.documentElement;
                        var status = xmlDoc_response.getElementsByTagName("status")[0].childNodes[0].nodeValue;
                        var messageText = xmlDoc_response.getElementsByTagName("message")[0].childNodes[0].nodeValue;

                        switch(status) {
                            case "ok":
                                switch_content('box_create_channel_p2.inc.php', 'right-col-box');
                                switch_content('ajax/box_mychannel_select.php', 'left-col-box2');
                                load_my_channels_page(1,1);
                                refresh_channels_list();    
                                break;
                            case "errorName":
                                document.getElementById("ErrorCreateChannelName").innerHTML = messageText;
                                break;
                            case "errorDesc":
                                document.getElementById("ErrorCreateChannelDesc").innerHTML = messageText;
                                break;
                        }
                    } else {
                        alert("An error has occured in nwtwork communications. Please check your internet connection.");
                    }
                }
            };

            var ajax_path = "channel.php?action=add&channelname="+channel_name+"&channeldesc="+channel_desc+"&ajax=1";
            xmlHttp_request.open("GET",ajax_path,true);
            xmlHttp_request.send(null);
            return false;
        }
        return true;
    }

    function delete_channel() {
        var xmlHttp_request;

        try {
            xmlHttp_request = new XMLHttpRequest();
        } catch (error) {
            try {
                xmlHttp_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(error1) {
                xmlHttp_request = null;
            }
        }

        if(xmlHttp_request != null) {
            xmlHttp_request.onreadystatechange = function() {
                if(xmlHttp_request.readyState==4) {
                    if(xmlHttp_request.status == 200 || xmlHttp_request.status == 304) {
                        var xmlDoc_response = xmlHttp_request.responseXML.documentElement;
                        var status = xmlDoc_response.getElementsByTagName("status")[0].childNodes[0].nodeValue;
                        var messageText = xmlDoc_response.getElementsByTagName("message")[0].childNodes[0].nodeValue;

                        switch(status) {
                            case "ok":
                                switch_content('ajax/box_delete_channel_p2.inc.php', 'right-col-box');
                                switch_content('ajax/box_mychannel_select.php', 'left-col-box2');
                                break;
                            case "error":
                                //document.getElementById("ErrorCreateChannelName").innerHTML = messageText;
                                break;
                        }
                    } else {
                        alert("An error has occured in nwtwork communications. Please check your internet connection.");
                    }
                }
            };

            var ajax_path = "channel.php?action=remove&ajax=1";
            xmlHttp_request.open("GET",ajax_path,true);
            xmlHttp_request.send(null);
            return false;
        }
        return true;
    }

