//make sure you put the settings code for this script in the top of your html to make this work.

var d = document;

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

//shortcut functions//////////////////////////////////
function dce(obj) {
 return d.createElement(obj);
}

function ge(objID) {
 return d.getElementById(objID);
}

function so_getText(obj) {
 if(obj.textContent) return obj.textContent;
 if (obj.nodeType == 3) return obj.data;
 var txt = new Array(), i=0;
 while(obj.childNodes[i]) {
  txt[txt.length] = so_getText(obj.childNodes[i]);
  i++;
 }
    return txt.join("");
}
//////////////////////////////////////////////////////