function popup(URL, title, width, height)
{
	window.open(URL, "_new", "width="+width+",height="+height+"toolbar=0,scrollbars=yes,location=0,statusbar=0,menubar=0,resizable=yes");
}

//two functions for dynamically creating tables (kids, school list, etc)
function makeHeader(values)
{
	var header = document.createElement("tr");
	
	for(i = 0; i < values.length; i++)
	{
		var th = document.createElement("th");
		th.innerHTML = values[i];
		header.appendChild(th);
	}
	
	return(header);
}
function makeRow(id, hidden, values)
{
	var tr = document.createElement("tr");
	tr.setAttribute("id", id);
	
	if(values != null)
	{
		for(j = 0; j < values.length; j++)
		{
			var td = document.createElement("td");
			td.innerHTML = values[j];
			tr.appendChild(td);
		}
	}
	
	if(hidden) tr.style.display = 'none';
	
	return(tr);
}

//for header search options
function changeHeaderSearchOption(index)
{
	options = new Array(2);
	options[0] = document.getElementById('kidageSearch');
	options[1] = document.getElementById('nameSearch');	

	for(i = 0; i < options.length; i++)
		options[i].style.display = "none";
				
	switch(parseInt(index))
	{
		case 0:
			options[0].style.display = "inline";
			options[0].selectedIndex = 0;
			break;
		case 1:
			options[1].style.display = "inline";
			options[1].value = "Type Name Here";
			options[1].name = "name";
			options[0].selectedIndex = 0;
			break;		
		case 2:
			options[1].style.display = "inline";
			options[1].value = "Type Screenname";
			options[1].name = "name";
			options[0].selectedIndex = 0;
			break;		
		case 3:
			options[1].style.display = "inline";
			options[1].value = "Type Zip Here";
			options[1].name = "zip_code";
			options[0].selectedIndex = 0;
			break;					
		case 4:			
			clearSearchFields(options);
			document.location = '/search/search_moms.php?reset=1';
			return;
		default:
			break;
	}
}
function clearSearchFields(fields)
{
	if(fields != null)
	{
		for(i = 0; i < fields.length; i++)
		{
			if(fields[i].nodeType == 'select' || fields[i].nodeType == 'SELECT')
				fields[i].fields[fields[i].selectedIndex].value = "";
			else
				fields[i].value == "";
		}
	}
}

//some random string functions
function trim(value)
{ 
	return(value.replace(/^\s*|\s*$/g,""));
}
String.prototype.replaceAll = function(strTarget, strSubString)
{
	var strText = this;
	var intIndexOfMatch = strText.indexOf(strTarget);
	 
	// Keep looping while an instance of the target string still exists in the string.
	while(intIndexOfMatch != -1)
	{
		// Relace out the current instance.
		strText = strText.replace(strTarget, strSubString)
		 
		// Get the index of any next matching substring.
		intIndexOfMatch = strText.indexOf(strTarget);
	}
	 
	// Return the updated string with ALL the target strings
	// replaced out with the new substring.
	return(strText);
}

//random Array functions
Array.prototype.contains = function(element)
{
	array = this;
	for (var i = 0; i < array.length; i++) 
	{
		if (array[i] == element) {
			return(true);
		}
	}
	
	return(false);
}

//unescapes html escaped strings (&amp; -> &, etc)
function htmldecode(str) {
    str = str.replaceAll("&gt;", ">").replaceAll("&lt;", "<").replaceAll("&quot;", "\"").replaceAll("&amp;", "&");
    return(str);
}

//a generic tab panel switcher function
//tabList = an array of tab element IDs
//paneList = an array of pane element IDs
//index = index of tab the user clicked on (1-based)
function setTab(tabList, paneList, index)
{	
	if(tabList != null && paneList != null && index != null && tabList.length == paneList.length)
	{
		var tabs = new Array(tabList.length);
		var panes = new Array(paneList.length);		
		
		for(i = 0; i < tabs.length; i++)
		{
			tabs[i] = document.getElementById(tabList[i]);
			panes[i] = document.getElementById(paneList[i]);
			
			if(i != index-1)
			{
				tabs[i].className = "";
				panes[i].style.display = "none";
			}	
			else
			{
				tabs[i].className = "active";
				panes[i].style.display = "block";			
			}			
		}
	}
}

//set focus to the first tinymce element on the page
function setFocus()
{	
	if(tinyMCE != null)
	{
		tinyMCE.execCommand('mceFocus', false, 'mce_editor_0');
	}
}

//for friend chooser
function selectAllRecipients()
{
	List = document.getElementById('chooser_recipients');
	
	if(List != null)
	{
		if (List.length && List.options[0].value == 'temp') return;
		for (i=0;i<List.length;i++)
		{
			List.options[i].selected = true;
		}
		List.name='recipients[]';
	}
}

function copyToList(from,to,remove,add)
{
	fromList = document.getElementById(from);
	toList = document.getElementById(to);

	if(fromList != null && toList != null)
	{
		for (i=0;i<fromList.options.length;i++)
		{
			var current = fromList.options[i];
			if (current.selected)
			{
				txt = current.text;
				val = current.value;
				alreadyThere = false;
				for(j=0;j<toList.options.length;j++)
				{
					if(toList.options[j].value == val)
						alreadyThere = true;
				}
				if(!alreadyThere && add)
				{
					toList.options[toList.length] = new Option(txt,val);
				}
				if(remove) 
				{
					fromList.options[i] = null;
					i--;
				}
				current.selected = false;
			}
		}
	}
}

//Toggle TinyMCE editor
function toggleEditor(id,linkObj) {
    if (tinyMCE.getInstanceById(id) == null) {
        linkObj.innerHTML = "hide editor";
        tinyMCE.execCommand('mceAddControl', false, id);
    }
    else {
        linkObj.innerHTML = "show editor";
        tinyMCE.execCommand('mceRemoveControl', false, id);
    }
}

//for submit buttons - replace with "please wait..." and disable
var submitted = false;
function pleaseWait(id) {
    if(!submitted) {        
        var button = document.getElementById(id);
        var txt = "Please Wait..."
        
        if(button.nodeName.toLowerCase() == "input") {    //if button (<input type=button...)
            button.value = txt;
            button.disabled = true;
        }
        else {                                            //if link (<a href...><span>click here</span></a>
            button.innerHTML = txt;
            button.parentNode.onclick.onclick = "function(){return false;}";
        }
        submitted = true;
        return(true);
    }
    else {
        return(false);
    }
}

function getBrowserInfo()
{
	var str = "";
	var element = document.getElementById("info");
	
	str += "\\nCode name of the browser: " + navigator.appCodeName;
	str += "\\nMinor version of the browser: " + navigator.appMinorVersion;	
	str += "\\nName of the browser: " + navigator.appName;
	str += "\\nPlatform and version of the browser: " + navigator.appVersion; 
	str += "\\nCurrent browser language: " + navigator.browserLanguage;
	str += "\\nCookies are enabled in the browser: " + navigator.cookieEnabled;
	str += "\\nCPU class of the browser's system: " + navigator.cpuClass;
	str += "\\nSystem is in offline mode: " + navigator.onLine;
	str += "\\nOperating system platform: " + navigator.platform;
	str += "\\nDefault language used by the OS: " + navigator.systemLanguage;
	str += "\\nUser-agent header sent by the client to the server: " + navigator.userAgent;
	str += "\\nOS' natural language setting: " + navigator.userLanguage;
	
	str += "\\nPlugins: \\n\\n"
	for(i = 0; i < navigator.plugins.length; i++)
	{
		str += navigator.plugins[i].name + " - " + navigator.plugins[i].filename + "\\n";
	}
	
	element.value = str;
}

function characterCounter(textElementId, countElementId, start)
{
    var textElement = document.getElementById(textElementId);
    var countElement = document.getElementById(countElementId);
    
    if(textElement != null && countElement != null) {
        if(start == null) {
            countElement.innerHTML = textElement.value.length;
        } else {
            var value = start - parseInt(textElement.value.length);
            if(value <= 0) {
                textElement.value = textElement.value.substring(0, start);
                value = 0;
            }
            countElement.innerHTML = value;
        }
    }
}