// functions.js

function placeFocus() {

	var field = document.forms[0];
	for (i = 0; i < field.length; i++) {
	if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
	
		document.forms[0].elements[i].focus();

		break;
         			}
      			}
   		}

function setStatus(theStatus)
	{
	var obj = document.getElementById(status);
	if (obj)
		{
		obj.innerHTML = theStatus;
		
		

		}
	}


function hide_form()
	{
	var showslug = document.getElementById("mainSlug");	
	var showcopy = document.getElementById("mainCopy");
	showcopy.innerHTML = "";
	showslug.style.visibility = 'hidden';
	showcopy.style.visibility = 'hidden';
	}


// functions to submit a form

var aok;

function submitform(theform, serverPage, objID, valfunc)
	{
	//alert (theform);
	var file = serverPage;
	var str = 'active_page=yes&';
	str += getformvalues(theform, valfunc);
	// if the validation is ok
	//alert (str);
	if(aok == true)
		{
		//var obj = document.getElementById(objID);
		processajax(serverPage, objID, "post", str);
		}
	}

function getformvalues(fobj, valfunc)
	{
	var str = "";
	aok = true;
	var val;	
	
	//alert ("fobj: "+fobj);
	// run through a list of all objects contained within the form
	for (var i = 0; i < fobj.elements.length; i++)
		{
		if(valfunc != '') 
			{
			if (aok == true)
				{
				val = false;
				// val = valfunc (fobj.elements[i].value.fobj.elements[i].name);
				if (val == false)
					{
					aok = false;
					}
				}
			}
		if ((fobj.elements[i].type == "radio" || fobj.elements[i].type == "checkbox") )
			{
			if (fobj.elements[i].checked == true) {
			str += fobj.elements[i].name + "="+ escape(fobj.elements[i].value) + "&";
			//alert (fobj.elements[i].name + " is checked and has value: "+escape(fobj.elements[i].value));
			}}
		else {
			
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
			}
		}
	// then return the string values.
	return str;
	}


	function trim(inputString) {
	   // Removes leading and trailing spaces from the passed string. Also removes
	   // consecutive spaces and replaces it with one space. If something besides
	   // a string is passed in (null, custom object, etc.) then return the input.
	   if (typeof inputString != "string") { return inputString; }
	   var retValue = inputString;
	   var ch = retValue.substring(0, 1);
	   while (ch == " ") { // Check for spaces at the beginning of the string
	      retValue = retValue.substring(1, retValue.length);
	      ch = retValue.substring(0, 1);
	   }
	   ch = retValue.substring(retValue.length-1, retValue.length);
	   while (ch == " ") { // Check for spaces at the end of the string
	      retValue = retValue.substring(0, retValue.length-1);
	      ch = retValue.substring(retValue.length-1, retValue.length);
	   }
	   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
	      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	   }
	   return retValue; // Return the trimmed string back to the user
	} // Ends the "trim" function
	
	//Function to validate the addtask form.
	function validatetask (thevalue, thename){
		
		var nowcont = true;
		
		if (thename == "yourname"){
			if (trim (thevalue) == ""){
				document.getElementById("themessage").innerHTML = "You must enter your name.";
				document.getElementById("newtask").yourname.focus();
				nowcont = false;
			}
		}
		if (nowcont == true){
			if (thename == "yourtask"){
				if (trim (thevalue) == ""){
					document.getElementById("themessage").innerHTML = "You must enter a task.";
					document.getElementById("newtask").yourtask.focus();
					nowcont = false;
				}
			}
		}
		
		return nowcont;
	}
	




	
