	/* variables for getting current mouse x,y coords */
	var px=0;
	var py=0;
	var userid = 0;
	var username = "";
	var curselitem = "";
	
	//initialize tab menu, by passing in ID of UL
	var curitem = new Array(7);
	var interval = 300;
	var element;
	var subelement;
	var content = "home";
	var canSwitch = true;	


	/*  THIS FUNCTION EXECUTES AFTER THE DOM IS READY, BUT BEFORE THE PAGE RENDERS...  */
	$(document).ready(function() {

	    // Track mousemouse co-ords for login box popup
	    $().mousemove(function(e) {
	        px = e.pageX;
	        py = e.pageY;
	    });
	    setupMenu(); //Setup the MNL menu

	    /* Styles the select box for MNL Programs dropdown*/
	    $('.my-skinnable-select').each(
            function(i) {
                selectContainer = $(this);
                // Remove the class for non JS browsers
                selectContainer.removeClass('my-skinnable-select');
                // Add the class for JS Browers
                selectContainer.addClass('skinned-select');
                // Find the select box
                selectContainer.children().before('<div class="select-text">&nbsp;</div>').each(
                    function() {
                        $(this).prev().text(this.options[0].innerHTML);
                    }
                ); //.each
                // Store the parent object
                var parentTextObj = selectContainer.children().prev();
                // As we click on the options
                selectContainer.children().click(function() {
                    // Set the value of the html
                    parentTextObj.text(this.options[this.selectedIndex].innerHTML);
                    var val = this.options[this.selectedIndex].value;
                    if (val != "" && val != curselitem) {
                        curselitem = val;
                        selectProgram(val);
                    }
                }); //.click
            }); // .each


	    setHover(".hover");
	    $("#emailfriend").click(function(e) {
	        $("#emailafriendpop").remove();
	        $("body").append("<div id='emailafriendpop' style='position:absolute;left:" + px + "px;top:" + py + "px;background:#fff;padding:5px;border:solid 1px #000;z-index:510;width:300px;height:150px;'><img src='images/loading-small.gif' alt='loading..' /></div>");
	        $("#emailafriendpop").load('scripts/admin/email/emailafriend.aspx', function() { });
	    });
	    $("#newslettersignup").click(function(e) {
	        $("#newslettersignuppop").remove();
	        $("body").append("<div id='newslettersignuppop' style='position:absolute;left:" + px + "px;top:" + py + "px;background:#fff;padding:5px;border:solid 1px #000;z-index:510;width:300px;height:150px;'><img src='images/loading-small.gif' alt='loading..' /></div>");
	        $("#newslettersignuppop").load('scripts/admin/email/signup.aspx', function() { });
	    });
	    $("#calendar_icon").click(function(e) { window.location = '?Content=Events/Calendar'; });
	    /* Setup the login box popup click function*/
	    $("#loginlink").click(function(e) {
	        $('#voice-login').css({ left: e.pageX - 250, top: e.pageY, 'z-index': '999999' });
	        $("#username").val('');
	        $("#password").val('');
	        $("#voice-login").fadeIn(300);
	        $("#username").focus();
	        $("#password").keyup(function(event) {
	            if (event.keyCode == 13) { doLogin(); }
	        });
	        $("#username").keyup(function(event) {
	            if (event.keyCode == 13) { doLogin(); }
	        });
	    });


	    /* Setup the search box */
	    $("#search_textbox").focus(function() { $(this).css({ 'background-color': '#fff' }); });
	    $("#search_textbox").blur(function() {
	        if ($(this).val() == "")
	            $(this).css({ 'background-color': 'transparent' });
	    });
	    $("#search_textbox").keydown(function(event) {
	        if (event.keyCode == 13) {
	            window.location = '?Content=Search+' + $("#search_textbox").val();
	        }
	    });
	    $("#search_button").bind('click', function() {
	        window.location = '?Content=Search+' + $("#search_textbox").val();
	    });


	});        // end onload

	function emailFriend() {
	    $.post('scripts/admin/email/sendemail.aspx', {
	        'ef_yname': $("#ef_yname").val(),
	        'ef_yemail': $("#ef_yemail").val(),
	        'ef_fname': $("#ef_fname").val(),
	        'ef_femail': $("#ef_femail").val(),
	        'email': $("#email").val()
	    }, function(data) { if (data == "sent") {alert("Your message has been sent."); }  });
        $("#emailafriendpop").remove();
    }
	
	function emailSignup() {
	    $.post('scripts/admin/email/signup_sendemail.aspx', {
	        'ef_yname': $("#ef_yname").val(),
	        'ef_yemail': $("#ef_yemail").val(),
	        'ef_fname': $("#ef_fname").val(),
	        'ef_femail': $("#ef_femail").val(),
	        'email': $("#email").val()
	    }, function(data) { if (data == "sent") { alert("You have been signed up to the newsletter.  Please check your email to confirm your subscription."); } else { alert(data); } });
        $("#emailafriendpop").remove();
    }	
	
    /* Sets up the MNL Menu with over effects and submenu visibility */
	function setupMenu() {

	    if (Instr(window.location, "Content", 1) > 0)
	        content = querySt("Content");
        if (content=="CCRC") {content="Programs/CCRC"}
        if (content == "Tidy_Towns") { content = "Programs/Tidy_Towns" }
        if (content == "LADR") { content = "Programs/LADR" }
        if (content == "Rural_Urban") { content = "Programs/Rural_Urban" }
	    if (Instr(content, "+", 1) > 0) {
	        element = Left(content.toLowerCase(), Instr(content, "+", 1));
	        content = element;
	    }

	    if (Instr(content, "/", 1) > 0) {
	        subelement = Right(content.toLowerCase(), (content.length - Instr(content, "/", 1) -1));
	        element = Left(content.toLowerCase(), Instr(content, "/", 1));
	        
	    } else {
	        element = content.toLowerCase();
	    }
	    if (element == "") element = "home";


	    if ($("div[title='" + element + "']").html() != null) {
	        try {
	            $("#submenustyle").html("<div id='" + element + "-content'>" + $("div[title='" + element + "']").html() + "</div>");
	            $("#submenustyle a:last").css({ 'border': 'none' });
	        } catch (err) { }

	        $("#" + element + " a").css({ 'background': '#9bcfd4', 'color': '#ffffff' });
	        $("#submenustyle a:last").css({ 'border': 'none' });

	        $("#" + element + " a").unbind("mouseover");
	    }


	    $("#maintab div").each(function() {
	        if ($(this).attr('id') != element) {
	            $("#" + $(this).attr('id') + " a").hover(function() {

	            try { $(this).animate({ 'color': '#008c99' }, 80); } catch (err) { }
	            }, function() {
	                try { $(this).animate({ 'color': '#88c2c9' }, 80); } catch (err) { }
	            });
	        }
	    });

	    $("#" + subelement).css({ 'color': '#ffffff' });
	}


	/* onchanged function for the MNL Programs Select Box */
	function selectProgram(loc) {
	    window.location = loc;
	}
	
	
	/*   put link like cursor behavor on all elements with class passed as argument  */
	function setHover(div) {
		$(div).hover(function (){$(div).css({cursor:"pointer"});}, 
      	function (){ $(div).css({cursor:"default"});});
	}
	 
	/* login script */
	function doLogin() {
	    $("#loginmsg").html("Login: In Progress...");
	    $.post('scripts/processlogin.aspx', {
	        u: $("#username").val(),
	        p: hex_sha256(hex_sha256($("#password").val()))
	    }, function(msg) {
	        $("#loginmsg").html("Login: " + msg);
	        if (Left(msg,11) == "Successfull") {
	            hideLogin();
	            setTimeout('rload()', 1000);
	        }
	    });
	}

	/* asp right function */
	function Left(str, n) {
	    if (n <= 0)
	        return "";
	    else if (n > String(str).length)
	        return str;
	    else
	        return String(str).substring(0, n);
	}

	/* asp right function */
	function Right(str, n) {
	    if (n <= 0)
	        return "";
	    else if (n > String(str).length)
	        return str;
	    else {
	        var iLen = String(str).length;
	        return String(str).substring(iLen, iLen - n);
	    }
	}

    /* asp instr function */
	function Instr(str, substr, start) {
	    var oStr = new String(str);
	    return oStr.indexOf(substr, start);
	}

	/* hides login form  */
	function hideLogin(){ $("#voice-login").fadeOut(300);	}
	
	/* logs user out of the cms  */
	function doLogout(){
		$.post('scripts/logout.aspx',{},function(msg){window.location.reload(true);});
	}
	
	/* reloads current page  */
	function rload() { window.location.reload(true); };
	
	/* encodes a string  */
	function encodeRE(s) { return s.replace(/[.*+?^${}()|[\]\/\\]/g, '\\$0'); }

    /* Gets the Query String by name */
	function querySt(ji) {
	    try {
	        hu = window.location.search.substring(1);
	        gy = hu.split("&");
	        for (i = 0; i < gy.length; i++) {
	            ft = gy[i].split("=");
	            if (ft[0] == ji) {
	                return ft[1].replace('%2f','/');
	            }
	        }
	    } catch (err) { }
	}

	//Member Registration Function
	function regMemberEvent(eid, uid) {
	    $.post('scripts/sections/events/memberreg.aspx', { 'eid': eid, 'uid': uid }, function(data) { window.alert("Thank you for your registration."); window.location.reload(); });
	}
	function unregMemberEvent(eid, uid) {
	    $.post('scripts/sections/events/memberunreg.aspx', { 'eid': eid, 'uid': uid }, function(data) { window.alert("You have been unregistered."); window.location.reload(); });
	}
	function regNonMemberEvent(extname, extemail, extemail1, extaddres, extphone, id) {
	    $.post('scripts/sections/events/nonmemberreg.aspx', { 'extname': extname, 'extemail': extemail, 'extemail1': extemail1, 'extaddres': extaddres, 'extphone': extphone, 'id': id },
	    function(data) {
	        if (data == "success") {
	            window.alert("Thank you for your registration."); 
	            window.location.reload();
	        } else {
	            window.alert(data);
	        }
	    });
	}



