/* Common General Use Functions */
/*
function getElements(name)
function getElement(name)
function getValue(name)
function setElement(elem,value)
function setValue(name,value)
function setClass(name,value)
function setAllValues(name,value)
function setSelectOptions(name,newOptions)
function addTableRow(theTable,newName,offset)
function addTableCell(tableRow,innerStuff)
function textareaAutoSize(theElem,stepSize,minSize,maxSize)
function toggleSection(sectionName)
function testPost(theFrm)
*/

/* PHP Functions */
/*
function array_search(needle,haystack)
function explode(str_separator,str_string)
function implode(str_glue,arr_pieces)
function in_array(needle,haystack)
function join(str_glue,arr_pieces)
function str_replace(str_search,str_replace,str_subject)
function round(num_val,num_precision)
*/

/* Common Data Related Functions */
/*
function generic_insert(name,tag,extras)
function formatMETA(theString,type)
function my_trim(array)
function open_win(url)
function isValidEmail(email_address)
*/

function getElements(name)
{
	var	elemList=document.getElementsByName(name);
	if(document.all!=null && elemList.length==0)
	{
		elemList=new Array();
		for(var key in document.all)
			if(document.all[key].name==name)
				elemList[elemList.length]=document.all[key];
	}
	var idElem=document.getElementById(name);
	if(idElem!=null) elemList[elemList.length]=idElem;
	return elemList;
}

function getElement(name)
{
	var elem=document.getElementById(name);
	if(elem) return elem;
	elem=document.getElementsByName(name);
	if(elem.length>0) return elem[0];
	if(document.all!=null)
	{
		for(var key in document.all)
		{
			if(document.all[key].id==name) return document.all[key];
			if(document.all[key].name==name) return document.all[key];
		}
	}
	return null;
}

function getValue(name)
{
	var elem=getElement(name);
	if(elem==null) return null;
	if(elem.value!=null) return elem.value;
	if(elem.innerHTML!=null) return elem.innerHTML;
	return null;
}

function setElement(elem,value)
{
	if(elem.tagName=='SELECT')
		for(var i=0;i<elem.options.length;i++)
			if(elem.options[i].value==value)
			{
				elem.options[i].selected=true;
				elem.selectedIndex=i;
			}
			else
				elem.options[i].selected=false;
	if(elem.tagName=='SPAN' || elem.tagName=='DIV')
	{
		elem.innerText=value;
		elem.innerHTML=value;
	}
	if(elem.value!=null) elem.value=value;
	else if(elem.innerHTML) elem.innerHTML=value;
}

function setValue(name,value)
{
	var elem=getElement(name);
	if(elem!=null) setElement(elem,value);
}

function setClass(name,value)
{
	var elem=getElement(name);
	if(elem!=null) elem.className=value;
}

function setAllValues(name,value)
{
	var elemList=getElements(name);
	if(elemList.length>0)
		for(var i=0;i<elemList.length;i++)
			setElement(elemList[i],value);
}

function setSelectOptions(name,newOptions)
{
	var elem=getElement(name);
	if(elem==null) return;
	elem.options.length=0;

	for(var key in newOptions)
		elem.options[elem.options.length]=new Option(newOptions[key],key);
}

function addTableRow(theTable,newName,offset)
{
	if(offset==null) offset=theTable.rows.length;
	if(offset<0) offset=theTable.rows.length+offset+1;
	var newRow=theTable.insertRow(offset);
	newRow.name=newName;
	newRow.id=newName;
	return newRow;
}

function addTableCell(tableRow,innerStuff)
{
	var newCol=document.createElement('td');
	newCol.innerHTML=innerStuff;
	tableRow.appendChild(newCol);
	return newCol;
}

function textareaAutoSize(theElem,stepSize,minSize,maxSize)
{
	if(theElem==null || theElem.rows==null || theElem.cols==null || theElem.value==null) return;
	if(stepSize==null || stepSize<1) stepSize=5;

	var rowContent=theElem.value.split('\n');
	var rowCount=rowContent.length;

	for(i=0;i<rowContent.length;i++)
		rowCount+=Math.floor(rowContent[i].length/60);

	rowCount=Math.floor(rowCount/stepSize)*stepSize+stepSize;

	if(minSize!=null && minSize>rowCount) rowCount=minSize;
	if(maxSize!=null && maxSize>minSize && maxSize<rowCount) rowCount=maxSize;

	theElem.rows=rowCount;
}

function toggleSection(sectionName,force)
{
	var theSect=getElement(sectionName);
	var hideNote=getElement(sectionName+'_hideNote');
	if(theSect==null) return;
	var newDisplay=(theSect.style.display=='none');
	if(force!=null) newDisplay=force;
	if(newDisplay==true)
	{
		theSect.style.display='';
		if(hideNote!=null) hideNote.style.display='none';
	}
	else
	{
		theSect.style.display='none';
		if(hideNote!=null) hideNote.style.display='';
	}
}

function testPost(theFrm)
{
	var oldact=theFrm.action;
	var oldtrg=theFrm.target;
	theFrm.action="testpost.php";
	theFrm.target="_blank";
	theFrm.submit();
	theFrm.action=oldact;
	theFrm.target=oldtrg;
}

function array_search(needle,haystack)
{
	if(needle==null || haystack==null) return null;
	for(var i=0;i<haystack.length;i++)
		if(needle==haystack[i])
			return i;
	return null;
}

function explode(str_separator,str_string)
{
	return str_string.split(str_separator);
}

function implode(str_glue,arr_pieces)
{
	return arr_pieces.join(str_glue);
}

function in_array(needle,haystack)
{
	if(needle==null || haystack==null) return false;
	for(var i=0;i<haystack.length;i++)
		if(needle==haystack[i])
			return true;
	return false;
}

function join(str_glue,arr_pieces)
{
	return implode(str_glue,arr_pieces);
}

function str_replace(str_search,str_replace,str_subject)
{
	return implode(str_replace,explode(str_search,str_subject));
}

function round(num_val,num_precision)
{
	if(num_precision==null || num_precision<1)
		num_precision=0;
	return Number(num_val).toFixed(num_precision);
}

function generic_insert(name,tag,extras)
{
	var theElem=getElement(name);
	if(theElem==null) return;

	if(extras==null) extras='';

	if(theElem.selectionStart!=null && theElem.selectionEnd!=null)
	{
		var a=theElem.selectionStart;
		var b=theElem.selectionEnd;
		theElem.value=theElem.value.substring(0,a)+'<'+tag+extras+'>'+theElem.value.substring(a,b)+'</'+tag+'>'+theElem.value.substring(b);
		theElem.selectionStart=a+2+tag.length+extras.length;
		theElem.selectionEnd=2+tag.length+extras.length+b;
		theElem.focus();
	}
	else
	{
		var repltext=null;
		var seltext=(document.all)?document.selection.createRange():document.getSelection();
		var selit=(document.all)?document.selection.createRange().text:document.getSelection();
		if(seltext!=null && selit.length>=1)
			seltext.text='<'+tag+extras+'>'+seltext.text+'</'+tag+'>';
		else
			theElem.value+='<'+tag+extras+'>TEXT</'+tag+'>';
	}
}

function formatMETA(theString,type)
{
	theString=str_replace('"',"'",theString);
	theString=str_replace(';',"",theString);
	if(type=='keyword')
	{
		theString=str_replace(","," ",theString);
		theString=str_replace("-"," ",theString);
		var words=theString.split(' ');
		words=my_trim(words);
		var keywords='';
		for(i=0;i<50 && i<words.length;i++)
		{
			if(words[i].replace(" ","")!='')
			{
				var let=words[i].charAt(0);
				let=let.toUpperCase();
				keywords+=let+words[i].substring(1,words[i].length)+", ";
			}
		}
		return keywords;
	}
	return theString;	
}

function my_trim(array)
{
	for(i=0;i<array.length;i++)
	{
		var word=array[i]+"";
		array[i]=word.replace(" ","");
	}
	return array;
}

function open_win(url)
{
	window.open(url,'','scrollbar=yes,width=400,height=400');
}

function isValidEmail(email_address)
{
	regex=/^[A-z0-9][\w.-]*@[A-z0-9][\w\-\.]+\.[A-z0-9]{2,6}$/;
	return regex.test(email_address);
}
