schedule("window", initForm);


function initForm()
{
  if (!document.getElementById) return false;
  if (!document.getElementById("submitForm")) return false;
  var theForm = document.getElementById("submitForm");
  theForm.onsubmit = checkForm;
    
}
 

function hideElement(theElement){  
  theElement.className="hidden";
  return true;
}

function showElement(theElement){    
   theElement.className ="";
   return true;
}


function checkForm() 
{       
       

       if (!document.getElementsByTagName) return false;
	var labels = this.getElementsByTagName("label");
	var error = false;
	var first = null;

	for (var i = 0; i < labels.length; i++)
	{
		if (labels[i].className.classExists("required"))
		{
			var input = labels[i].getElementsByTagName("input")[0] || labels[i].getElementsByTagName("textarea")[0] || labels[i].getElementsByTagName("select")[0];
			var spans = labels[i].getElementsByTagName("span");
			var errorText = null;
			var labelText = null;

			for (var j = 0; j < spans.length; j++)
			{
				if (spans[j].className.classExists("labelText"))
				{
					var labelText = spans[j];
				}
			}

			if (input != null)
			{
			   
                           if (input.value == "" || input.value=="dd-mm-yyyy")
			   {
				errorText = "Please fill in your " + labelText.childNodes[0].nodeValue.toLowerCase().replace(/:/, "");
			   }
                           else if (input.value !="" && !input.value.validInputValue()){
                                errorText = "Your " + labelText.childNodes[0].nodeValue.toLowerCase().replace(/:/, "") + " contains invalid character";
                                                               
                           }
                           else if ( input.nodeName.toLowerCase() == "input" &&  input.getAttribute("type") == "radio"){                                                                         if (!checkRadioGroup(this,input.name)){
                                           errorText = "Please make a selection for " +  labelText.childNodes[0].nodeValue.toLowerCase().replace(/:/, "");      
                                   }
                           } 
                           else if ( input.getAttribute("checked") == false &&  input.getAttribute("type") == "checkbox"){                                                                            errorText = "You have not ticked the box that you have read and agree to the " +  labelText.childNodes[0].nodeValue.toLowerCase().replace(/:/, "");                                  }   
                           else if (labels[i].className.classExists("requiredemail") && !input.value.validEmail())
			   {
			           errorText = "Please supply a valid e-mail address";
			
                           }
                           

			if (errorText != null)
			{
				if (first == null)
					{
					  first = input;
					}

					error = true;
					writeCorrection(labels[i], errorText);
                                        switch(input.getAttribute("type")){                                                                                                                  
				              case "text" || "textarea":
                                                 	input.onkeyup = checkValidity;
                                                        break;
                                              case "checkbox":
                                                        input.onclick = checkValidity;
                                                        break; 
					      case "radio":                                                                      
                                                        //var fieldGroup = document.forms["submitform"].elements[input.name]; 
                                                        var fieldGroup = this.elements[input.name]; 
                                                        for (var j=0; j<fieldGroup.length;j++){
                                                                 fieldGroup[j].onclick = checkValidity;
                                                        }                                                                                 
                                                        break;
                                              default:                                           
					                input.onchange = checkValidity;
                                                        break;
					}//end switch
				}
				else
				{
					writeCorrection(labels[i]);
				}
			}
		}
	}
        
	if (error)
	{
		first.focus();

		return false;
	}
        
        //assign mailrecipients for incident notification

        var form =  this.getAttribute("formname").value; 
        
        if ( form == "incidentnotification"){          
           var recipients = document.getElementById("state").getAttribute("value");        
           document.getElementById("mailrecipient").setAttribute("value",recipients);
        }
          

	return true;
};


function writeCorrection(label, text, correct)
{
	var spans = label.getElementsByTagName("span");
	var input = label.getElementsByTagName("input")[0] || label.getElementsByTagName("textarea")[0] || label.getElementsByTagName("select")[0];
	var image = label.getElementsByTagName("img")[0];

	if (typeof text == "undefined")
	{
		if (image != null)
		{
			label.removeChild(image);
		}

		for (var j = 0; j < spans.length; j++)
		{
			if (spans[j].className.classExists("correctionText"))
			{
				label.removeChild(spans[j]);

				break;
			}
		}
	}
	else
	{
		if (image == null)
		{
			image = document.createElement("img");
			image.className = "correctionIcon";
			image = label.insertBefore(image, input);

			var newText = document.createElement("span");
			newText.className = "correctionText";
			label.appendChild(newText);
		}

		var spans = label.getElementsByTagName("span");

		for (var j = 0; j < spans.length; j++)
		{
			if (spans[j].className.classExists("correctionText"))
			{
				var correctionText = spans[j];

				break;
			}
		}

		if (correct == true)
		{
			image.setAttribute("src", "/dirw/workerscomp/workerscomp.nsf/attachmentsbytitle/icon_tick.gif/$file/icon_tick.gif");
			image.setAttribute("alt", "Correct");
			correctionText.className = correctionText.className.removeClass("warning");
		}
		else
		{
			image.setAttribute("src", "/dirw/workerscomp/workerscomp.nsf/attachmentsbytitle/icon_cross.gif/$file/icon_cross.gif");
			image.setAttribute("alt", "Incorrect");
			correctionText.className = correctionText.className.addClass("warning");
		}

		writeSpan(correctionText, text);
	}

	return true;
};




function writeSpan(span, text)
{
	var children = span.childNodes;

	for (var i = 0; children.length > 0;)
	{
		span.removeChild(children[i]);
	}

	var textNode = document.createTextNode(text);
	span.appendChild(textNode);

	return true;
};




function checkValidity()
{
       var label = this.parentNode;
	var spans = label.getElementsByTagName("span");
	var labelText = null;

	for (var j = 0; j < spans.length; j++)
	{
		if (spans[j].className.classExists("labelText"))
		{
			var labelText = spans[j];
		}
	}

	if (this.value == "")
	{
		writeCorrection(label, "Please fill in your " + labelText.childNodes[0].nodeValue.toLowerCase().replace(/:/, ""));
	}
	else if (label.className.classExists("requiredEmail") && !this.value.validEmail())
	{
		writeCorrection(label, "Please supply a valid e-mail address");
	}
	else
	{
                    
		writeCorrection(label, "This field is correct", true);
	}

	return true;
};

function checkRadioGroup(form,fieldobj){  
  var fieldGroup = form.elements[fieldobj];
  var checked = false;
  for (var i=0; i<fieldGroup.length;i++){
      if (fieldGroup[i].checked == true){
         checked = true;
         break;
      }
  }
  return checked;
}