//Opens the window used for sending email to gruops of members
function OpenMessageEditor(MemberCollectionID, Type, isSMS)
{	
	window.open('/Default.aspx?p=Emailer&Type='+Type+'&amp;MemberCollectionID='+MemberCollectionID+'&amp;isSMS='+isSMS,'none','width=430,height=310');
}

//Used for tracking open and closed items on dynamic pages
function AddItemID(ItemID, ControlClientID)
{
	var field = document.getElementById(ControlClientID);
	field.value = field.value + ItemID + ",";
}

function RemoveItemID(ItemID, ControlClientID)
{
	var field = document.getElementById(ControlClientID);
	field.value = field.value.replace("," + ItemID + ",", ",");
}

function AddItemIDToControl(ItemID, Control)
{	
	Control.value = Control.value + ItemID + ",";
}

function RemoveItemIDFromControl(ItemID, Control)
{
	Control.value = Control.value.replace("," + ItemID + ",", ",");
}

//Shows personal information in a standard set of divs assuming they exist
function showDetails(id, name, address, phone, email)
{
	document.getElementById('dynName'+id).innerHTML = name;
	if (address != "")
		document.getElementById('dynAddress'+id).innerHTML = address + "<br><br>";
	else
		document.getElementById('dynAddress'+id).innerHTML = "";
	if (phone != "")
		document.getElementById('dynPhone'+id).innerHTML = phone + "<br>";
	else
		document.getElementById('dynPhone'+id).innerHTML = "";
	document.getElementById('dynEmail'+id).innerHTML = '<b>E-mail: </b><a href=\'mailto:' + email + '\'>' + email + '</a>';
}

//shows or hides the div passed assuming it exists and based on it's current state when called
function SwitchDivDisplayState(divName)
{
	divToSwitch = document.getElementById(divName);
	if (divToSwitch)
	{
		if (divToSwitch.style.display == "none")
		{
			divToSwitch.style.display = "";
		}
		else
		{
			divToSwitch.style.display = "none";
		}
	}
}

//makes any object sent light blue with a darkblue border unless 
//changBorder is passed as true in which case the border is left alone
	
function GetRule(ruleName)
{
	var styleSheet=null;
	var Rules=new Array();
	var styleCount=0;
	for (var ssIndex=0;ssIndex<document.styleSheets.length;ssIndex++)
	{
		styleSheet=document.styleSheets[ssIndex];
		for (var rIndex=0;rIndex<styleSheet.rules.length;rIndex++)
		{
			rule=styleSheet.rules[rIndex]
			if ((rule.selectorText=="."+ruleName)||(rule.selectorText==ruleName))
			{
					Rules[styleCount]=rule
					styleCount++;
			}								
		}
	}
	return Rules;
}

var origColorTracker;

function glow(sender, ignoreBorder)
{	
	rules = GetRule(sender.className);
	if (rules[0])
		origColorTracker = rules[0].style.background;

	sender.style.background = "#D4E0EA";
	if (!ignoreBorder)
	{
		sender.style.border = "1px solid #ADCFE5";
	}
}

function unglow(sender, ignoreBorder)
{
	if (origColorTracker != undefined)
	{
		sender.style.background = origColorTracker;
		origColorTracker = undefined;
	}
	else
	{
		sender.style.background = "";
	}
	if (!ignoreBorder)
	{
		sender.style.border = "1px solid white";
	}
}

//Checks to see if any scripts listed in the script exists and run them on load if they do
function RunLoadScripts()
{
	if(window.VerifySave)
		VerifySave();
	if(window.OpenGroups)
		OpenGroups();
	if(window.OpenInvites)
		OpenInvites();
	if(window.OpenMeetings)
		OpenMeetings();
	if(window.invitationAlert)
		invitationAlert();
	if(window.notify)
		notify();
	if(window.SetEditableDivPosition)
		SetEditableDivPosition();
	if(window.OnWindowLoad)
		OnWindowLoad();
	if(window.createjsDOMenu)
		createjsDOMenu();
}

function OpenWin(url)
{
	window.open(url,"meeting","width=300,height=230");
}

function LimitImgDimensions(img, width, height)
{
	if (img)
	{
		var origWidth = parseInt(img.width);
		var origHeight = parseInt(img.height);
		if (origWidth - width > origHeight - height)
		{
			img.width = width;
			var div = origWidth / width;
			var newHeight = origHeight / div;
			img.height = Math.round(newHeight);
		}
		else
		{
			img.height = height;
			var div = origHeight / height;
			var newWidth = origWidth / div;
			img.width = Math.round(newWidth);
		}
	}
}
function setTip(tip)
{
	if (window.tooltips)
	{
		tooltips.innerHTML = tip;
	}
}
function clearTip()
{
	if (window.tooltips)
	{
		tooltips.innerHTML = "&nbsp;";
	}
}
function ShowPartEditTools(id)
{
	var editTools = document.getElementById("partTools"+id);
	var part = document.getElementById("part"+id);
	
	alert('hovered over a part: ' + id);
	if (part && editTools)
	{
		editTools.style.display = '';
		editTools.style.top = part.offsetTop;
		editTools.style.left = part.offsetLeft;
	}
	else
	{
		alert('something\'s null');
	}
}

function FormatDate(d)
{
	var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

	var curr_date = d.getDate();
	var sup = "";
	if (curr_date == 1 || curr_date == 21 || curr_date ==31)
	{
		sup = "st";
	}
	else if (curr_date == 2 || curr_date == 22)
	{
		sup = "nd";
	}
	else if (curr_date == 3 || curr_date == 23)
	{
		sup = "rd";
	}
	else
	{
		sup = "th";
	}

	var curr_month = d.getMonth();
	var curr_year = d.getFullYear();

	return m_names[curr_month] + " " + curr_date + "<SUP>" + sup + "</SUP>, " + curr_year;
}