var asmxLocation = 'http://' + window.location.hostname + '/AskTheTherapist.asmx';


$(document).ready(function () {

    if ($('#content').length > 0) {

        // if theres only one columnSide in the #content div make the main column full width
        if (($('#content .columnSide').length == 1) && ($('#content .columnCentre').length == 1)) {
            $('#content .columnCentre').addClass('columnFull');
            $('#content .columnCentre').removeClass('columnCentre');
        }

    }

    initialiseMenus();

});

function initialiseMenus(){

	$('.menu li').each( function() {
		var thisLi = $(this);
		if ((thisLi.children('ul').length > 0) && (thisLi.children('.switchOpen').length == 0) && (thisLi.children('.switchStatic').length == 0))  thisLi.children('a').addClass('switchClosed');
	})
	
	$('.menu .thisPage').each( function() {
		var thisItem = $(this);
		
		// apply the open class to the links in direct parents
		thisItem.parents('li').children('a').removeClass('switchClosed');
		thisItem.parent().parents('li').children('a').addClass('switchOpen');
		thisItem.parent().parents('li').children('a').addClass('selectedItem');

	});
	
	
	// close any open panes set to close
	$('.switchClosed').next('ul').hide();
	$('.switchStatic').addClass('selectedItem');
	
	
	$('.switchOpen, .switchClosed').click( function(e) {

		e.preventDefault();
		var thisItem = $(this);
		
		if (thisItem.is('.switchOpen')) {
		
			thisItem.next('ul').slideUp('slow', function() {
				thisItem.addClass('switchClosed');
				thisItem.removeClass('switchOpen');
			});
			
		} else {
		
			thisItem.next('ul').slideDown('slow', function() {
				thisItem.removeClass('switchClosed');
				thisItem.addClass('switchOpen');
			});
			
		}
	
	})

}

// ============== String Maniputlations ============ //

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


 
function DoSearch(e) {
    if (e.keyCode == 13) {
        $('#searchlink').click();
        return false;
    }
    return false;
}



/****************************************
AJAX
****************************************/

function signin(usernamebox, passwordbox) {

    var username = $('#' + usernamebox).val();
    var password = $('#' + passwordbox).val();
    var cont = true;

    if (username == '') {
        cont = false;
        $('#' + usernamebox).parent().append('<span class="error">You must enter a username</span>');
    }

    if (password == '') {
        cont = false;
        $('#' + passwordbox).parent().append('<span class="error">You must enter a password</span>');
    }

    if (!cont) {
        return;
    }



    $.ajax({
        type: "POST",
        /*url: "/askthetherapist/AskTheTherapist.asmx/SignIn",*/
        url: asmxLocation + "/SignIn",
        data: "{'username' : '" + username + "', 'password' : '" + password + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d == "0") {
                var nav = window.location.toString().toLowerCase();
                var nav2 = nav.replace("loggedout", "LoggedIn");
                window.location.replace(nav2);
            }
            else {
                alert('Your username or password is incorrect, please try again');
            }
        },
        error: function (result) {
            alert("error: " + result.status);

        }
    });
}

function register(usernamebox, firstnamebox, lastnamebox, emailbox, confemailbox, passwordbox, confpasswordbox) {
    var username = $('#' + usernamebox).val();
    var firstname = $('#' + firstnamebox).val();
    var lastname = $('#' + lastnamebox).val();
    var email = $('#' + emailbox).val();
    var confemail = $('#' + confemailbox).val();
    var password = $('#' + passwordbox).val();
    var confpassword = $('#' + confpasswordbox).val();

    var cont = true;

    if (username == '') {
        cont = false;
        $('#' + usernamebox).parent().append('<span class="error">You must enter a username</span>');
    }

    if (firstname == '') {
        cont = false;
        $('#' + firstnamebox).parent().append('<span class="error">You must enter your first name</span>');
    }

    if (lastname == '') {
        cont = false;
        $('#' + lastnamebox).parent().append('<span class="error">You must enter your last name</span>');
    }

    if (email == '') {
        cont = false;
        $('#' + emailbox).parent().append('<span class="error">You must enter your email address</span>');
    }

    if (email != confemail) {
        cont = false;
        $('#' + confemailbox).parent().append('<span class="error">Your confirmation email address must match your email address</span>');
    }

    if (password == '') {
        cont = false;
        $('#' + passwordbox).parent().append('<span class="error">You must enter a password</span>');
    }

    if (password != confpassword) {
        cont = false;
        $('#' + confpasswordbox).parent().append('<span class="error">Your confirmation password must match your password</span>');
    }

    if (!cont) {
        return false;
    }

    $.ajax({
        type: "POST",
        /*url: "AskTheTherapist.asmx/Register",*/
        url: asmxLocation + "/Register",
        data: "{'username' : '" + username + "', 'firstname' : '" + firstname + "', 'lastname' : '" + lastname + "', 'email' : '" + email + "', 'confemail' : '" + confemail + "', 'password' : '" + password + "', 'confpassword' : '" + confpassword + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            if (msg.d == "0") {
                alert('Please check your email for a confirmation message');
                window.location.replace("/LoggedOut.aspx");
            }
            else {
                alert('Not registered');
            }
        },
        error: function (result) {
            alert("error: " + result.status);

        }
    });
}

function getRandomRant(divid) {
    $.ajax({
        type: "POST",
        url: asmxLocation + "/getRandomRant",
        /*url: "/askthetherapist/AskTheTherapist.asmx/getRandomRant",*/
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $('#' + divid).html(msg.d);
        }
    });
}


function getRandomRave(divid) {
    $.ajax({
        type: "POST",
        url: asmxLocation + "/getRandomRave",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            $('#' + divid).html(msg.d);
        }
    });
}


function addrant(textb, userid) {

    var ranttext = $('#' + textb).val();

    $.ajax({
        type: "POST",
        url: asmxLocation + "/AddRant",
        data: "{'text' : '" + ranttext + "', 'user' : '" + userid + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
        },
        error: function (result) {
            alert("error: " + result.status);

        }
    });


    alert('Your rant has been added');
}


function addrave(textb, userid) {

    var ranttext = $('#' + textb).val();

    $.ajax({
        type: "POST",
        url: asmxLocation + "/AddRave",
        data: "{'text' : '" + ranttext + "', 'user' : '" + userid + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
        },
        error: function (result) {
            alert("error: " + result.status);

        }
    });


    alert('Your rave has been added');
}    
