//setup for validate script////////////////////////////////////////////////////////

//set id of master element containing the form
var master_element = "frm1";

//set field names for validation///////////////////////////////////////////////////

//set email field for validation, leave blank if none
var email_field = "email";


//set reqiured fields, add fields to below array.  put "ALL" in the array to make everything required.  make sure field names match what is on the form, and seperate fields with a comma

var y
req = new Array("ALL")

//set style colors for field interaction.

var active_field = "#FFFFF0";
var field_error = "#F3D0D0";
var done_field = "";

///////////////////////////////////////////////////////////////////////////////////
function init() {

f_table = ge(master_element);

//get all ids of form elements
inps = f_table.getElementsByTagName('input');
inps_t = f_table.getElementsByTagName('textarea');
inps_s = f_table.getElementsByTagName('select');


//start array for ids
var x
fields = new Array()


//sort input tags and put them in array
var c = 0;

for (var i = 0; i < inps.length; i++) {

//get input type and ignore some
atr = inps[i].getAttribute("type");

if(atr == "hidden" || atr == "checkbox" || atr == "reset")
{

}
else if(atr == "submit")
{
atrn = inps[i].getAttribute("id");
inps[i].id = atrn;

submit_button = inps[i].id;

}
else
{
//if not ignored, get name and make it the ID

atrn = inps[i].getAttribute("name");
inps[i].id = atrn;

//put ids in array
fields[c] = inps[i].id

c++;

} //end if

} //end for


//add textarea tags to array

for (var i = 0; i < inps_t.length; i++) {

atrn = inps_t[i].getAttribute("name");
inps_t[i].id = atrn;

//put ids in array
fields[c] = inps_t[i].id

c++;

} //end for


//add select tags to array

for (var i = 0; i < inps_s.length; i++) {

atrn = inps_s[i].getAttribute("name");
inps_s[i].id = atrn;

//put ids in array
fields[c] = inps_s[i].id

c++;

} //end for



//add events to form elements and set values to null
for (x in fields)
{

ge(fields[x]).onfocus = function () { this.style.backgroundColor = active_field; }
ge(fields[x]).onblur = function () { form_off(this); }
//ge(fields[x]).value = "";

//alert(x + " = " + fields[x]);
}


//disable submit button onload to wait for validation to enable
ge(submit_button).setAttribute('disabled','disabled');

} //end function
window.onload = init; 




//begin validation code

function form_off(el) {


ID = el.id;


if (ID == email_field)
{

eml = ge(ID).value;

//email validation/////
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(eml)){

	}
	else
	{
	eml = "";

	}
///////////


}

else
{

eml = ge(ID).value;

}

if(eml == "")
{

	for (y in req)
	{
	rq = req[y];

	if(rq == ID || req.length == 1)
	{

	ge(ID).style.backgroundColor = field_error;
	break;
	}
	else
	{
	ge(ID).style.backgroundColor = done_field;
	}

	}// end for

}
else
{
ge(ID).style.backgroundColor = done_field;
ge(submit_button).removeAttribute('disabled','disabled');
}



if(req.length == 1)
{
check_whole()
}
else
{
check_required()
}


} //end function




//check over all fields to see if they are filled in or not
function check_whole()
{

//use fields array defined earlier
for (x in fields)
{


//if email address isn't valid, disable submit
fd = ge(fields[x]).id;

if (fd == email_field)
{
eml = ge(fd).value;

//email validation/////
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(eml)){
	}
	else
	{
	ge(submit_button).setAttribute('disabled','disabled');
	}
///////////
}


fv = ge(fields[x]).value;

if(fv == "")
{
ge(submit_button).setAttribute('disabled','disabled');
}

} //end for


} //end function












//check over required fields to see if they are filled in or not
function check_required()
{

//use req array defined earlier
for (y in req)
{


//if email address isn't valid, disable submit
fd = ge(req[y]).id;

if (fd == email_field)
{
eml = ge(fd).value;

//email validation/////
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(eml)){
	}
	else
	{
	ge(submit_button).setAttribute('disabled','disabled');
	}
///////////
}


fv = ge(req[y]).value;

if(fv == "")
{
ge(submit_button).setAttribute('disabled','disabled');
}

} //end for
} //end function
