function changeStyle(url) {
    document.getElementById('stylesheet').href = url;
}

function changePage(page) {
    if(page == "newsletter" || page == "articles" || page == "sermons"){
	page = "downloads";
    }
    changeStyle("css/" + page + ".css");
    $('.selected').removeClass('selected');
    $('.nav_' + page + '').addClass('selected');

}

function changeSecondary(page) {
    console.log(page);
    $('#sub_navigation selected').removeClass('selected');
    $('.nav_' + page + '').addClass('selected');

}


function changeLink() {
    if (!$('.nav_login').hasClass('selected')) {
        $('.nav_login').addClass('selected');
    } else {
        $('.nav_login').removeClass('selected');
    }
}

function initialize() {
    loadPage(getPage());
}

function getPage() {
    var hash = location.hash;
    hash = hash.slice(2);
    if (hash) {
        return hash;
    } else {
        return 'home';
    }

}

function loadMenu() {
    var xmlhttp;
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("navigation").innerHTML = xmlhttp.responseText;
            changePage(getPage());
            $('#loginContainer').hide();
        }
    }
    xmlhttp.open("GET", "menu.php", true);
    xmlhttp.send();
}

function loadPage(page, div) {
    if (div === undefined) {
        div = "content";
    }
    if (page == "downloads") {
	page = "newsletter";
    }

    var xmlhttp;
    if (page.length == 0) {
        document.getElementById(div).innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if (div == "content") {
            	changePage(page);
            }
            document.getElementById(div).innerHTML = xmlhttp.responseText;
    	    changeSecondary(page);
        }
    }
    xmlhttp.open("GET", "" + page + ".php", true);
    xmlhttp.send();
}

function showHide(element) {
    if (document.getElementById(element).style.display == 'none') {
        document.getElementById(element).style.display = 'block';
        if (element == 'loginContainer') {
            $("#email").focus()
        }
    } else {
        document.getElementById(element).style.display = 'none';
    }
}

function login() {
    var username = $('#email').val();
    var password = $('#password').val();
    $.ajax({
        type: "POST",
        url: "login.php",
        data: "name=" + username + "&pwd=" + password,
        success: function (html) {
            if (html == 'true') {
                $('#processing').html("Login Success").removeClass("redProcessing");
                loadPage(getPage());
                loadMenu();

            } else {
                $('#processing').html(html).addClass("redProcessing");
                $('#loginContainer :input').removeAttr('disabled');
                $('#email').val("");
                $('#password').val("");
            }
        },
        beforeSend: function () {
            $('#loginContainer :input').attr('disabled', true);
            $('#processing').html("Processing Login").removeClass("redProcessing");
        }
    });
}

function confirmReg(username) {
    $.ajax({
        type: "POST",
        url: "confirmReg.php",
        data: "username=" + username,
        success: function (html) {
            if (html == 'true') {
                loadPage(getPage());
            } else {

            }
        },
        beforeSend: function () {
            $('#loginContainer :input').attr('disabled', true);
            $('#processing').html("Processing Login").removeClass("redProcessing");
        }
    });
}

function register() {
    var username = $('#username').val();
    var password = $('#password').val();
    var password2 = $('#password2').val();
    $.ajax({
        type: "POST",
        url: "register.php",
        data: "name=" + username + "&pwd=" + password + "&pwd2=" + password2,
        success: function (html) {
            if (html == 'true') {
                $('#processing').html("Registration Complete").removeClass("redProcessing");
                loadPage('registationDetails', 'loginContainer');

            } else {
                $('#processing').html(html).addClass("redProcessing");
                $('#loginContainer :input').removeAttr('disabled');
            }
        },
        beforeSend: function () {
            $('#loginContainer :input').attr('disabled', true);
            $('#processing').html("Processing Login").removeClass("redProcessing");
        }
    });
}

function logout() {
    $.ajax({
        type: "POST",
        url: "logout.php",
        success: function (html) {
            if (html == 'true') {
                loadPage(getPage());
                loadMenu();
            }
        }
    });
}
