//This function writes the value of the destination field into a format that emailgen can use.

//Notes:
//to can be either the id of the field that should contain whom the email is to or a string containing the address.
//from can be either the id of the field that should contain whom the email is from or a string containing the address.
//header and footer can be left blank by placing '' in the call.
//destinationID is the id of the field that you want to contain the information (ie "*email1").


var delim= "|";

function createEmail(to, from, subject, header, footer, destinationID)
{
	var email="";

	if (to.indexOf('@')==-1)
		email=document.getElementById(to).value+delim;
	else
		email=to+delim;

	if (from.indexOf('@')==-1)
		email+=document.getElementById(from).value+delim;
	else
		email+=from+delim;

	email+=subject+delim+header+delim+footer
	document.getElementById(destinationID).value=email;
}

//Will color an element either red or white
function color(id, colorName){

		if (colorName=='red'){
			if(document.getElementById(id).style.backgroundColor='#FFCCD6'){}
		}
		else{
			if(document.getElementById(id).style.backgroundColor='#FFFFFF'){}
		}

}

//Will color an array of elements either red or white
function colorArray(idarray, colorName){
	var i;
	for (i in idarray)
	{
		color(idarray[i], colorName);
	}
}

//This function checks to see if a field is blank.  If the field is blank, it returns true.
function isEmpty(id, coloring)
{
	//emulate default function arguments
	if (arguments.length<2) coloring=true;
	if (document.getElementById(id).type=='radio' || document.getElementById(id).type=='checkbox'){
		if (document.getElementById(id).checked==false){
			if (coloring) color(id, 'red');
			return true;
		}
		else{
			if (coloring) color(id, 'white');
			return false;
		}
	}
	else{
		if (document.getElementById(id).value==""){
			if (coloring) color(id,'red');
			return true;
		}
		else{
			if (coloring) color(id, 'white');
			return false;
		}
	}
}

//This function takes an array of id's and checks to see if they are empty.  It returns a value of the number of empty fields.

function isArrayEmpty(idarray, coloring, allNone)
{
	//emulate default function arguments
	if (arguments.length<3) allNone=false;
	if (arguments.length<2) coloring=true;
	var empties=0;
	var i;
	for (i in idarray)
	{
		if (allNone){
			if (isEmpty(idarray[i], false))
				empties++;
		}
		else{
			if (isEmpty(idarray[i], coloring))
				empties++;
		}
	}

	if (allNone && coloring)
	{
		if (empties==idarray.length)	colorArray(idarray, 'red');
		else							colorArray(idarray, 'white');
	}
	return empties;

}
