//Highlight form element- © Dynamic Drive (www.dynamicdrive.com)
//For full source code, 100's more DHTML scripts, and TOS,
//visit http://www.dynamicdrive.com

var SectionHighlightColour="#D1F1DD";
/* #75FFAC"; */
//"#CFF5D1";	
/* "yellow";*/	/* "#FFFF00" */
var SectionPreviousColour="";

var RowHighlightColour = "white";
/* "#75FFAC"; */
/* "yellow"; */
/* "#FFCC00"; */	
/* "#FFFF00" */
var RowPreviousColour=""; 
var FieldPreviousBorderColor="";
var FieldPreviousBorderStyle="";

var ns6=document.getElementById && !document.all;
var SectionPreviousObj='';
var RowPreviousObj='';
var FieldPreviousObj=null;
var eventobj;

//Regular expression to highlight only form elements
var intended=/INPUT|TEXTAREA|SELECT|OPTION/

function InjectEventsIntoForm(sForm) {
	var arFormElements = Form.getElements(sForm);	// Form.getInputs(sForm);	
	
	// for every select, input, checbox and radio button, insert an event function 
	for (var x=0; x<arFormElements.length; x++){ //loop through each element
		var ElementType = arFormElements[x].tagName.toUpperCase();
		if (ElementType == "SELECT" || 
			ElementType == "INPUT" || 
			ElementType == "CHECKBOX" || 
			ElementType == "RADIO" ||
			ElementType == "TEXTAREA") {
			
			//arFormElements[x].onkeyup=highlight;
			//arFormElements[x].onclick=highlight;
			arFormElements[x].onfocus=highlight;
			//arFormElements[x].onblur=unhighlight;
		}
	}
}

//Function to check whether element clicked is form element
function checkel(which) {
	if (which.style && intended.test(which.tagName)) {
		/*
		if (ns6)
			if (eventobj.nodeType==3)
				eventobj=eventobj.parentNode.parentNode
		*/
		return true
	} else
		return false
}

function highlightObject(eventobj){
	ColouriseSection(eventobj);
	HighlightField(eventobj);
}

//Function to highlight form element
function highlight(e){
	eventobj= (ns6) ? e.target : event.srcElement;
	ColouriseSection(eventobj);
	HighlightField(eventobj);
}

function HighlightField(eventobj) {
	try {
		if (FieldPreviousObj) {
			FieldPreviousObj.style.borderColor = FieldPreviousBorderColor;
			FieldPreviousObj.style.borderStyle = FieldPreviousBorderStyle
			FieldPreviousObj=null;
		}
	} catch(e) {}
	try {
		FieldPreviousObj = eventobj;
		FieldPreviousBorderColor = eventobj.style.borderColor
		FieldPreviousBorderStyle = eventobj.style.borderStyle
		eventobj.style.borderColor = "red";
		eventobj.style.borderStyle = "solid";
	} catch(e) {}
}

function ClearField(eventobj) {
	//alert(eventobj.value);
	if (checkel(eventobj))
		if ( eventobj.value.substr(0, 1) == "(" ) {
			eventobj.value='';
		}
}

function ColouriseSection(eventobj) {
	e = eventobj;
	p = GetDIVSectionParent(e);
	if (p) {
		if (p.style.backgroundColor != SectionHighlightColour) {
			/* Reset the colour of the previous section if we've just left one */
			if (SectionPreviousObj !="") {
				SectionPreviousObj.style.backgroundColor=SectionPreviousColour;
			}
			SectionPreviousObj=p;
			SectionPreviouscolour = p.style.backgroundColor;

			/* Now set the colour of the section we've just arrived in */
			p.style.backgroundColor = SectionHighlightColour;		
		}
	}
}

function ColouriseRow(eventobj) {
	e = eventobj;
	p = GetRowParent(e);
	if (p) {
		if (p.style.backgroundColor != RowHighlightColour) {
			/* Reset the colour of the previous row if we've just left one */
			if (RowPreviousObj !="") {
				RowPreviousObj.style.backgroundColor=RowPreviousColour;
			}
			RowPreviousObj=p;
			RowPreviouscolour = p.style.backgroundColor;

			/* Now set the colour of the row we've just arrived in */
			p.style.backgroundColor = RowHighlightColour;		
		}
	}
}


function Colourise(eventobj) {
	if (previous!=''){
		if (checkel(previous))
			previous.style.backgroundColor=previouscolor;
	}
/*
else {
		if (checkel(eventobj))
			eventobj.style.backgroundColor=highlightcolor
		previous=eventobj
		previouscolor = previous.style.backgroundColor;
	}
*/
	previous=eventobj;
	previouscolor = previous.style.backgroundColor;
	if (checkel(eventobj))
		eventobj.style.backgroundColor=highlightcolor;

}

function GetDIVSectionParent(e) {
	x = e;
	while (x != null) {
		if ( x.className == "ApplicationForm_SectionDivision" ) {
			//&& x.id.substr(0,7) == "Section"
			return x;
		}
		//x = x.parentElement;
		x = x.parentNode;
	}
	return null;
}
function GetRowParent(e) {
	x = e;
	while (x != null) {
		if ( x.tagName == "TR" ) {
			//&& x.id.substr(0,7) == "Section"
			return x;
		}
		x = x.parentElement;
	}
	return null;
}