window.onload = initall;

function initall()
{
	if(document.getElementById('ca'))
	{
	document.getElementById('ca').onsubmit = validateformca;
	}
	if(document.getElementById('hw'))
	{
	document.getElementById('hw').onsubmit = submithw;
	}

	if(document.getElementById('em'))
	{
	document.getElementById('em').onsubmit = changeem;
	}

}


function fixForm(formEl, ms)
{
var errorMS = formEl.id +"err";

document.getElementById(errorMS).style.color = "black";

document.getElementById(errorMS).style.backgroundColor = "#D2AE4E";
document.getElementById(formEl.id).focus();
document.getElementById(errorMS).innerHTML = ms;

}





function resetBack(formEl)
{
var errorMS = formEl.id +"err";
document.getElementById(errorMS).style.color = "#d6af50";
document.getElementById(errorMS).style.borderStyle = "none";

document.getElementById(errorMS).style.backgroundColor = "#220E0F";
}




function validateformca()
{

resetBack(document.getElementById('fnameca'));
resetBack(document.getElementById('lnameca'));
resetBack(document.getElementById('emailca'));
resetBack(document.getElementById('passwordca'));
resetBack(document.getElementById('password2ca'));

re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/

	if(document.getElementById('fnameca').value == '')
	{
	fixForm(document.getElementById('fnameca'), "First Name: must be filled out");
	return false;
	}

	if(document.getElementById('lnameca').value == '')
	{
	fixForm(document.getElementById('lnameca'), "Last Name: must be filled out");
	return false;
	}

	if(!re.test(document.getElementById('emailca').value))
	{
	fixForm(document.getElementById('emailca'), "Email: Improper format");
	return false;
	}

	
	if(document.getElementById('passwordca').value == '')
	{
	fixForm(document.getElementById('passwordca'), "Password: can not be left blank");
	return false;
	}

	
	if(document.getElementById('passwordca').value != document.getElementById('password2ca').value)
	{
	fixForm(document.getElementById('password2ca'), "Retype password: Does not match first password");
	return false;
	}
return true;
}




function submithw()
{





regurl = /http:\/\/power\.arc\.losrios\.edu\/~[a-zA-Z-0-9]+\/\w+\//
regfilename = /[a-zA-Z]+\d+\.[a-zA-Z]+/

	if(document.getElementById('assignmenturlhw'))
	{

resetBack(document.getElementById('assignmenturlhw'));
		if(!regurl.test(document.getElementById('assignmenturlhw').value))
		{
		var mycom = "Check your URL! The address should start with this pattern: http://power.arc.losrios.edu/~username/classname/  <br />followed by the file name such as index.html or myassignment.php";
		fixForm(document.getElementById('assignmenturlhw'), mycom);
		return false;
		}
	}
	
	if(document.getElementById('uploadfilehw'))
        {
 resetBack(document.getElementById('uploadfilehw'));
                if(!regfilename.test(document.getElementById('uploadfilehw').value))
                {
var message = 'Please check your file name. It should consist of your lastname followed by the'; 
message = message + ' chapter or assignment number with 1 dot followed by the file extension';
message = message + ' such as: \n\t\ leeverch4.doc \n\t or leeverch10.rtf \n\t or leeverinformalidea2.doc';
message = message + ' \n There should be no spaces or strange characters';
message = message + ' like the # sign in the filename.';
               
              fixForm(document.getElementById('uploadfilehw'), message); 
		 return false;
                }
	}

	if(document.getElementById('scommenthw'))
	{
resetBack(document.getElementById('scommenthw'));
		if(document.getElementById('scommenthw').value == '')
		{
		fixForm(document.getElementById('scommenthw'), "Student comments are required for this assignment -- please read the instructions.");
		return false;
		}
	}
	
return true;
}


function changeem()
{
re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/
 resetBack(document.getElementById('emailnew'));	
	if(!re.test(document.getElementById('emailnew').value))
	{
	fixForm(document.getElementById('emailnew'), 'Edit Email: Check the format!'); 
	
	return false;
	}
	
return true;

}
