schedule("window", initForm);


function hideElement(theElement){  
   theElement.className="hidden";
   return true; 
}

function showElement(theElement){    
   theElement.className=""; 
   return true;
}


function initForm()
{
  if (!document.getElementById) return false;
  if (!document.getElementById("submitForm")) return false;
  var theForm = document.getElementById("submitForm");
  theForm.onsubmit = checkForm;
    
}




function checkForm()
{
        
        var fieldsets = this.getElementsByTagName("FIELDSET");
        var labels = this.getElementsByTagName("label");
	var error = false;
	var customError = false;
	var first = null;
        
        //for radio buttons            
        
        for (var i= 0; i< fieldsets.length; i++)
	{ 
          
          if (fieldsets[i].className == "required"){
             var errorText = null; 
             var input = fieldsets[i].getElementsByTagName("input")[0];               
             var labelText =  fieldsets[i].getElementsByTagName("legend")[0];
                  
             if(input.getAttribute("type") == "radio" && !checkRadioGroup(this,input.name)){               
                errorText = "This field is required";                 
             }  
             
             if (errorText != null){                
                error = true; 
                writeCorrection(input,errorText);           
                var fields = this.elements[input.name]; 
                for (var j=0; j<fields.length;j++){                   
                  fields[j].onclick = checkValidity;
                }  
             }else{
                writeCorrection(input);
             }       
          }  
              
        } 



	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 = "This field is required";
                                 if (input.nodeName.toLowerCase() == "select"){
                                    errorText = "This field is required";
                                 }    
                                   
                           }else if (input.nodeName.toLowerCase() == "input" && input.getAttribute("type") =="checkbox"){                                                   
                                 if (!input.checked){
                                        errorText = "This field is required";   
                                     }                           
 

                           }else if (labels[i].className.classExists("requiredEmail") && input.value !=""){                                  
                                  if (!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;
                                             default:                                           
	                                               input.onchange = checkValidity;
                                                       break;
					}//end switch
				}
				else
				{
					writeCorrection(labels[i]);
				}
			}
		}
	}
        
                
        if(window.customValidate) {
           
           customError = !customValidate(); 
        }
	
        if (error || customError)
	{          
	    if(first) first.focus();
            return false;
	}       
        
        
        return true;
        
      
};
	


function writeCorrection(label, text, correct)
{
        
        if (label.nodeName=="INPUT"){
              label = label.parentNode; 
        }
  
	var spans = label.getElementsByTagName("span");
	var input = label.getElementsByTagName("select")[0] || label.getElementsByTagName("input")[0] || label.getElementsByTagName("textarea")[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", "/dirv/vero/vero.nsf/attachmentsbytitle/icon_tick.gif/$file/icon_tick.gif");
			image.setAttribute("alt", "Correct");
			correctionText.className = correctionText.className.removeClass("warning");
		}
		else
		{
			image.setAttribute("src", "/dirv/vero/vero.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()
{
        if (this.getAttribute("type") == "radio"){            
            var label = this.parentNode.parentNode.getElementsByTagName("label")[0];                          
        }else{    
            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;
}




