// JavaScript Document
//alert("Loaded Successfully");
_autoLoad();

function _autoLoad()
{
	var submits = document.getElementById('submit');
	submits.onclick = verify;
	var submitBox = submits.parentNode;
	var t = submitBox.removeChild(submits);
	var errorMsgBox = document.createElement('div');
	errorMsgBox.setAttribute("id","errorMsg");
	submitBox.appendChild(errorMsgBox);
	submitBox.appendChild(t);
}

function getLabel(name,labels)
{
	for (var i=0;i<labels.length;i++)
		{if (labels[i].htmlFor == name){return labels[i];}}
}
function getLabelContents(name,labels)
{
	var label = getLabel(name,labels);
	return label.innerHTML;
}

function returnError(field,label)
{
	errorCount++;
	label.style.color = '#FF0000';
	field.style.borderColor = '#FF0000';
	return true;
}
function noError(field,label)
{
	label.style.color = '#000000';
	field.style.borderColor = '#CCCCCC';
	field.style.borderStyle = 'solid';
	return true;
}

function verify()
{
	testableBoxes = false;
	errorCount = 0;
	k = -1;
	boxesName = '';
	testBoxes = new Array;
	testBoxes[0] = '';
	boxChecked = new Object;
	
	errorCount = 0;
	docForm = document.getElementById('demoRequest');
	if (!docForm) docForm = document.getElementById('emailForm');
	var inputs = docForm.getElementsByTagName("input");
	var labels = docForm.getElementsByTagName("label");
//	alert("Labels.length = "+labels.length);
	for (var i=0;i<inputs.length;i++)
	{
		if (inputs[i].parentNode.className == 'required')			//If it's not required, we don't need to worry about it
		{
			//alert(errorCount+" errors thus far");
			var name = inputs[i].name;
			var check = inputs[i].className;
			var label = getLabel(name,labels);
			check = check.slice(check.search('check-')+6);
			//alert (name+" is required and checked by "+check);
			if (!isBlank(inputs[i]))								//No sense running tests on blank input
			{
				//alert(check+" check");
				switch(check)
				{
					case 'alpha':	alphaTest(inputs[i],label);break;
					case 'email':	emailTest(inputs[i],label);break;
					case 'phone':	phoneTest(inputs[i],label);break;
					case 'boxes': 	testableBoxes = true;
									boxName = inputs[i].name;
									if (boxName !== testBoxes[k])
									{
										k++;
										testBoxes[k] = boxName;
									}
									break;
				}
			}
			else returnError(inputs[i],label);
		}
	}
	if (testableBoxes)
	{
		//alert('here');
		for(var j=0;j<testBoxes.length;j++)
		{
			var checkboxes = docForm[testBoxes[j]];
			if (!boxesTest(checkboxes))
			{
				errorCount++;
				for(var k=0;k<checkboxes.length;k++)
					{checkboxes[k].parentNode.style.color = '#ff0000';}
			}
			else
			{
				for(var k=0;k<checkboxes.length;k++)
					{checkboxes[k].parentNode.style.color = '#000000';}
			}
		}
	}
	if (errorCount > 0)
	{
		//alert(errorCount+" errors");
		if (errorCount > 1) var problems = 'problems';
		else problems = 'problem';
		document.getElementById('errorMsg').innerHTML = 'Please fix the '+errorCount+' '+problems+' above (in red).';
		return false;
	}
	else return true;
}

function isBlank (field,label)
{
	if (field.value == ""){return true;}
	else return false;
}


function phoneTest(field,label)
{
	var stripped = field.value.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {returnError(field,label);}
	else 
	{
		if (!(stripped.length >= 10)) {returnError(field,label);}
		else noError(field,label);
	}
}
function boxesTest(checkboxes)
{
	var checked = false;
	for(var i=0;i<checkboxes.length;i++)
	{
		if (checkboxes[i].checked) return true;
	}
	return false;
}
function emailTest(field,label)
{
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(field.value))) 
	{ 
//	   alert("Please enter a valid email address.");
	   returnError(field,label);
	   return false;
	}
	else
	{
		noError(field,label);
		return true;
	}
}

function alphaTest(field,label)
{
	var illegalChars = /\W/;
	
	var name = field.name;
	
	var value = field.value;
	if (illegalChars.test(value))
	{
		returnError(field,label);
		var labelname = label.innerHTML;
		alert("You've entered an invalid value for "+labelname+".\nThis field should be letters and numbers only. \n Please try again.");
		//return false;
	}
	else noError(field,label);
}