//Form Validation Scripts
//* the length name keeps popping up. really irritating, as there is not tag called length. it is just the length of the collection. grrrrr.
// for these scripts to work properly some settings have to be made on the forms:
// body needs the onload and onkeyup events to link to checkall(field) and checktext(field) respectively, each handing ofver the name of an input field that should be on the form, just to check if the form is fully loaded.
// also the form needs the validation message div with the id="ValidationMsg"
// last but not least the form should not be submitted using a submit button. Use either a "href" link or "input type=button" element, both linking to the ValidateForm('formname') script, which will then subsequently check the form (given by formname) and submit it, should it be ok.
var formcomplete;
var showrequired;

	function checkInputs(){	
		var required = false;
		var divs,inputs,element,div,i,j;	
		inputs=document.getElementsByTagName("INPUT")
		divs=document.getElementsByTagName("DIV")
		for (i = 0; i < inputs.length; i++){
			element=inputs[i].id;	
			if (element!=""){
				if (document.getElementById(element).type=="text"){ //make sure we're only checking text boxes and not buttons or check boxes or crap
					if (document.getElementById(element).value.length>0) //does the text box contain info?					
						if(document.getElementById('Required'+element)){
							document.getElementById('Required'+element).style.visibility='hidden'
							document.getElementById('Required'+element).style.display='none'
						}
					else{//otherwise show the validator asterix, should there be one of course.						
						if(document.getElementById('Required'+element)){
								document.getElementById('Required'+element).style.visibility='inherit'
								//document.getElementById('Required'+element).style.display='block'
								required=true;
						}						
					}
				}
			}
		}
		return required		
	}

	function checkTextAreas(){
		var required=false;
		var divs,inputs,element,div,i,j;		
		inputs=document.getElementsByTagName("TEXTAREA")		
		for (i = 0; i < inputs.length; i++){	
			element=inputs[i].id;
			if (element!='length' && element !=""){ 
				if (document.getElementById(element).value.length>0)
						if(document.getElementById('Required'+element)) document.getElementById('Required'+element).style.visibility='hidden'
				else{
					if(document.getElementById('Required'+element)){
						document.getElementById('Required'+element).style.visibility='inherit'
						required=true;
					}	
				}
			}
		}
		return required		
	}
	
	function checkSelects(){
	//this only works if the select contains a "please select" entry with an option value of blank ""
	var required=false;
		var divs,inputs,element,div,i,j;		
		inputs=document.getElementsByTagName("SELECT")		
		for (i = 0; i < inputs.length; i++){	
			element=inputs[i].id;	
			if (element!='length' && element !=""){ 		
				if (document.getElementById(element).options[document.getElementById(element).selectedIndex].value!="")
					if(document.getElementById('Required'+element)) document.getElementById('Required'+element).style.visibility='hidden'			
				else{
					if(document.getElementById('Required'+element)){
						document.getElementById('Required'+element).style.visibility='inherit'
						required=true;
					}
				}			
			}
		}
		return required		
	}


	function checkall(){
	//should be fired up on page load, checks all the fields on the form, also works when a user uses the back button.

				
		checkInputs()
		checkTextAreas()
		checkSelects()

		showrequired=false

		var divs,inputs,element,div,i,j;				
		divs=document.getElementsByTagName("DIV")				
		for (j = 0; j < divs.length; j++){
			div=divs[j].id;
			if (div.substr(0,8)=='Required')					
				if ((document.getElementById(div).style.visibility=='visible')||(document.getElementById(div).style.visibility==''))
					showrequired=true
			if (showrequired)
				document.getElementById('ValidationMsg').style.visibility='visible'
		}
	}

	function checktext(){
	
	//should fire up on a key stroke on the form, gives the user more direct visible feedback to his actions
		var divs,inputs,element,div,i,j;		
		divs=document.getElementsByTagName("DIV")
	
		if(event.keyCode!=9){ //don't go checking on form elements if the key pressed was a tab
			showrequired=false;	
			
			if(((event.srcElement.tagName=="INPUT")&&(event.srcElement.type=="text")) ||(event.srcElement.tagName=="TEXTAREA")){ //only go for the text input boxes and textares.
				if (event.srcElement.name!="keywords"){
					if (document.getElementById(event.srcElement.name).value.length > 0)
						if(document.getElementById('Required'+event.srcElement.name)) document.getElementById('Required'+event.srcElement.name).style.visibility='hidden'
					else
						if(document.getElementById('Required'+event.srcElement.name)) document.getElementById('Required'+event.srcElement.name).style.visibility='inherit'								
				}
			}				
			//this loop checks if there are any more asterixes on the form. if so, the form is not valid yet.	
			for (j = 0; j < divs.length; j++){
				Required=divs[j].id;
				if (Required.substr(0,8)=='Required')						
					if (document.getElementById(Required).style.visibility!='hidden')				
						showrequired=true								
			}
			//final straw, show the error message should there be one ore more Validation signals left
			if (showrequired)
				document.getElementById('ValidationMsg').style.visibility='visible'
			else
				document.getElementById('ValidationMsg').style.visibility='hidden'								
		}
	}

	function checkSelect(select){
		if (select.options[select.selectedIndex].value!="")
			document.getElementById('Required'+select.id).style.visibility='hidden'
		else
			document.getElementById('Required'+select.id).style.visibility='visible'
	}

	function ValidateForm(FormName){
		var isMac;
		isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
		if (isMac)
			document.forms[0].submit();		
		else{
			if (showrequired && !isMac)
				alert("Please fill in the required information first")
			else
				document.forms[0].submit();		
		}
	}

