var do_alert=false;
var strMessage = '';

// use add_event.js via class="check_form"
function checkform(the_form,the_event) { 
//initial vars
show_process=false;
f=the_form;
strMessage = 'Please correct the following:\n\n';
var objarray; 
var objTemp; 
var strName = '';
var elem_length = f.elements.length;
//disable submit and show working
window.disable_enable_submit(the_form,true);
//now begins the loop which takes all the form elements and tests them
is_error=false;

//remove all form error nodes
set_form_error_message_off();

for ( var i = 0; i < elem_length; i++ ) {
	//strMessage += i+'\n'
	
	objarray= f.elements;
	objTemp = objarray[i];
	
	if(objarray[i].type){
			
		strName = objTemp.name;
		strID = objTemp.id;
		str_type=objTemp.type;
		
		objlength = f.elements[strName].length;
		
		//see if this form element is in a hidden layer
		//if so ignore it as it isn't relavant
		if(! test_parent_node_recurse(objTemp)){
			if ( strName.substr( strName.length - 4 ) == '_req' ) {
				
				// first see if it's required
				//if required, parse the name
				strName = strName.replace( /data_/, '' );
				strName = strName.replace( /_req/, '' );
				str_no_space = strName.replace( /_/g, ' ' );
				strName = strName.toLowerCase();
				if ( objTemp.value == '') {
					
					//if field is empty 
					//add the hidden field message and set elem_length to --1
					set_form_error_message_text(objTemp);
					is_error=true;
					elem_length-=1;
				
				} else if ( objTemp.type == 'radio' ) {
					//this is a radio button
					//so check to see if checked
					//weird radio thing, in that a radio group of 1 has no length.
					
					if(! objlength){
						//do object length so this object is it
						
						if ( objTemp.checked == false) {
							radio_checked=false;
						}else{
							radio_checked=true;
						}
						
					}else{
						for ( var j = 0; j <= objlength; j++ ) {
							checkbox_obj = eval("f."+objTemp.name+"["+j+"]");
							radio_checked=false;
							if ( checkbox_obj ) {
								if ( checkbox_obj.checked ) {
									radio_checked=true;
									break;
									//found check ok so break
								}
							}
							
						}
					}
					//no check so set the message 
					
					if(radio_checked==false){
						
						set_form_error_message_text(objTemp);
						is_error=true;
					}
					elem_length-=1;
					
				} else if ( objTemp.type == 'checkbox' ) {
					//this is a radio button
					//so check to see if checked
					//checkbox_obj = eval("document.forms[0].product_on_req[1]");
					
					//do alert for checked
					for ( var j = 0; j < objlength; j++ ) {
						checkbox_obj = eval("f."+objTemp.name+"["+j+"]");
						checked=false;
						if ( checkbox_obj.checked ) {
							checked=true;
							break;
							//found check ok so break
						}
					}
					//no check so set the message 
					//alert("checkbox obj_length = "+ objlength+" checked ="+checked);
					if(checked==false){
						set_form_error_message_text(objTemp);
						is_error=true;
					}
					elem_length-=1;
				}
			}
		}
	}
	}

	if ( is_error ) {
		//If true alert user via CSS or if off via alert
		f_do_alert( strMessage );
		//enable submit and disable show working
		window.disable_enable_submit(the_form,false);
		Event.stop(the_event);
	} else {
		
		try{
			if(show_process && window.set_processing_animation){
		    	set_processing_animation(true);
		    }
		}catch(anerror){
			if(window.set_processing_animation){
		    	set_processing_animation(true);
		    }
		}
		
		return true;
		//Event.stop(the_event);	
	}
};

function test_parent_node_recurse(current_element){
	the_parent=current_element.parentNode;
	
	for(i=1;i<100;i++){
		current_style=the_parent.style;
		
		//alert(getElementStyle(the_parent,'visibility','visibility'));
		
		if( getElementStyle(the_parent,'visibility','visibility') =='hidden'){
			
			return true;
		}else{
			if(the_parent.nodeName == 'BODY' ){
				//return false;
				//alert('found BODY');
				return false;
			}else{
				//alert('the parent node is '+the_parent.nodeName+' the visiblity is '+the_parent.style.visibility);
				the_parent=the_parent.parentNode;
			}
		}
	}	
};

function getElementStyle(current_elem, IEStyleProp, CSSStyleProp) {
    var elem = current_elem;
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
};

function set_form_error_message_text(div_element) {
	
	if(do_alert==true){
		
		alert(div_element.alt);
		strMessage += div_element.alt+'\n';
	}else{
		var newPara = document.createElement("div");
		newPara.className="form_error";
		
		if(div_element.alt){
			var newText = document.createTextNode(div_element.alt);
		}else{
			var newText = document.createTextNode(div_element.title);
		}
		newPara.appendChild(newText);
		the_parent=div_element.parentNode;
	
		if(the_parent){
			//old
			//the_parent.appendChild(newPara);
			the_parent.insertBefore(newPara,div_element.nextSibling);
		}
	
	}
};

function modify_form_error_message_text(div_id,email_error) {
	
	if(do_alert==true){
		
		alert(div_element.alt);
		strMessage += email_error+'\n';
	}else{
		var newPara = document.createElement("div");
		newPara.className="form_error";
		var newText = document.createTextNode(email_error);
		newPara.appendChild(newText);
		the_parent=div_element.parentNode;
		
		if(the_parent){
			the_parent.appendChild(newPara);
		}
	
	}
};

function set_form_error_message_off() {
	//remove all form error nodes
	class_list=document.getElementsByClassName('form_error');
	class_list_len=class_list.length;
	//remove em
	for (i=0; i<class_list_len; i++){
		if(class_list[i].parentNode){
			class_list[i].parentNode.removeChild(class_list[i]);
		}
	}
	
};
function f_do_alert(the_message) {
	if(do_alert==true){
		show_alert(the_message);
	}else{
		show_alert("You have errors on the form which have been highlighted, please correct.");
	}
};

function disable_enable_submit(the_form,b_enabled) {
		
		var submit_array=Form.getInputs(the_form, 'submit');
		
		//default to the last element
		var last_element=submit_array.length;
		var the_element=submit_array[last_element-1];
		//or get the element with the class name 'js_do_submit_animation'
		
		if($$('input.js_do_submit_animation')[0]){
			the_element=$$('input.js_do_submit_animation')[0];
		}
		if(the_element){
			if(b_enabled){
				
				the_element.hide();
				
				//alert("adding working link");
				var insert_html='<strong class="working_link" id="js_working_link">working...</strong>';
				new Insertion.After(the_element,insert_html);
				
			}else{	
				if($('js_working_link')){
					$('js_working_link').remove('js_working_link');
					//remove the class name too
					Element.removeClassName(the_element, "js_do_submit_animation");
				}
				the_element.show();
			}
		}
}