var currNav = ''; // Set current nav id
jQuery.noConflict();

// Set top and sometimes left nav
function setNav(topNav, sideNav)
{
	currNav = topNav;
	if (document.getElementById(topNav)) {
		document.getElementById(topNav).className = 'current';
	}
	
	if (sideNav != undefined)
	{
		if (document.getElementById(sideNav)) {
			document.getElementById(sideNav).className = 'current';
		}
	}
}

function toggle(target)
{
	realTarget = document.getElementById(target);
	
	if (realTarget.style.display == 'none')
	{
		realTarget.style.display = 'block';
	} else {
		realTarget.style.display = 'none';
	}
}

/*** Form checking functions ***/
function checkField (val, field)
{
    var error = "";
    if (val == "")
    {
      error = "You must enter a value in the " + field + " field.\n";
    }
    return error;
}
function checkEmail(val, field)
{
    var error = "";
    if ((val == "" || val.length < 3) ||
        (val.indexOf("@") == "-1") ||
        (val.indexOf(".") == "-1"))
    {
        error = "Please enter a valid " + field + " address.\n";
    }

    return error;
}

jQuery(document).ready(function()
{
	jQuery("#search_name").focus(function(){
		if(jQuery(this).attr('value') == "My Search")
			jQuery(this).attr('value',"");
	});
	
	jQuery("#search_name").blur(function(){
		if(jQuery(this).attr('value') == "")
			jQuery(this).attr('value',"My Search");
	});
	
	jQuery("#innercirlce_profile_submit").click(function(){
		var url	= jQuery(this).attr('url');

		if (url == undefined) return false;

		hideStatus('profile_messages');
		hideStatus('password_messages');

		// Serialize our form and submit it via ajax
		var mrForm = jQuery('#profileForm');
		jQuery.ajax({
			data: mrForm.serialize(),
			url: url,
			type: 'POST',
			timeout: 2000,
			error: function() {
				showStatus("ERROR: Could not save (bad form?).",'profile_messages');
			},
			success: function(r) {
				if (r == '' || r == '0')
					showStatus("ERROR: Could not save (no item?).",'profile_messages');
				else
					showStatus(r,'profile_messages');
			}
		}); // ajax
		return false;
   	});
	
	jQuery("#innercirlce_password_submit").click(function(){
		var url	= jQuery(this).attr('url');

		if (url == undefined) return false;

		hideStatus('profile_messages');
		hideStatus('password_messages');

		// Serialize our form and submit it via ajax
		var mrForm = jQuery('#passwordForm');
		jQuery.ajax({
			data: mrForm.serialize(),
			url: url,
			type: 'POST',
			timeout: 2000,
			error: function() {
				showStatus("ERROR: Could not save (bad form?).",'password_messages');
			},
			success: function(r) {
				if (r == '' || r == '0')
					showStatus("ERROR: Could not save (no item?).",'password_messages');
				else
					showStatus(r,'password_messages');
			}
		}); // ajax
		return false;
   	});

});

function showStatus(msg,id)
{
	jQuery("#"+id).html(msg);
	jQuery("#"+id).slideDown("fast");
}

function hideStatus(id)
{
	jQuery("#"+id).slideUp("fast");
}

function saveprop(url,prop_id)
{	
	jQuery.post(url, {},
		function(data){
			if(data >= 0)
			{
				jQuery("#innercircleprops_count").html(data);
				document.getElementById("save_"+prop_id).style.display   = "none";
				document.getElementById("remove_"+prop_id).style.display = "block";
			}
		}
	);
}

function removeprop(url,prop_id)
{	
	jQuery.post(url, {},
		function(data){
			if(data >= 0)
			{
				jQuery("#innercircleprops_count").html(data);	
				document.getElementById("save_"+prop_id).style.display   = "block";
				document.getElementById("remove_"+prop_id).style.display = "none";
			}
		}
	);
}

function savesearch(url)
{
	var title 	 = jQuery("#search_name").attr('value');
	var criteria = jQuery("#search_criteria").attr('value');
	
	jQuery.post(url, {title : title , criteria : criteria},
		function(data){
			if(data >= 0)
			{
				jQuery("#innercirclesearches_count").html(data);	
				jQuery("#save_search_form").slideUp("slow");
				document.getElementById("save_search").style.display   = "none";
				document.getElementById("remove_search").style.display = "block";
			}
		}
	);
}

function removesearch(url,criteria)
{
	jQuery.post(url, {criteria : criteria},
		function(data){
			if(data >= 0)
			{
				jQuery("#innercirclesearches_count").html(data);
				document.getElementById("save_search").style.display   = "block";
				document.getElementById("remove_search").style.display = "none";
			}
		}
	);
}

function switch_forms(show,hide,header)
{
	jQuery("#"+show).show();
	jQuery("#"+hide).hide();
	jQuery("#direction h2").html(header);
}

function generate_new_password(url,email)
{
	var result = "";
	
	if((result = echeck(email)) != "")
	{
		alert(result);
		return false;
	}

	jQuery.post(url, {email : email},
		function(data){
			if(data)
			{
				jQuery("#forgot_password_message").html(data);
				jQuery("#forgot_password_message").show();
				jQuery(".error_box .errors").hide();
				switch_forms('mysite_login_form','forgotten_password_form','members login here');
			}
		}
	);
}

function switchNews(url,id,place_holder)
{
	jQuery("ul li a.news").removeClass('current');
	jQuery("#"+id).addClass('current');
	jQuery("#news_items").html("<center><img class='spinner' src='"+place_holder+"'></center>");

	jQuery.post(url, {link_name : id},
		function(data){
			if(data)
			{
				jQuery("#news_items").html(data);
			}
		}
	);
}

/*
* http://www.smartwebby.com/DHTML/email_validation.asp
*/
function echeck(str)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1)
		return "Invalid E-mail Address";

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return "Invalid E-mail Address";

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return "Invalid E-mail Address";

	if (str.indexOf(at,(lat+1))!=-1)
		return "Invalid E-mail Address";

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return "Invalid E-mail Address";

	if (str.indexOf(dot,(lat+2))==-1)
		return "Invalid E-mail Address";

	if (str.indexOf(" ")!=-1)
       	return "Invalid E-mail Address";

	return "";					
}