// <![CDATA[

var Rules = {
	'select.select_date:change':function(element){
		the_id=element.id;
		//extract the sufix for the other date pickers.
		the_select_group=the_id.substr(the_id.indexOf("_")+1,the_id.length);
		the_day_elem=$('day_'+the_select_group);
		the_month_elem=$('month_'+the_select_group);
		the_year_elem=$('year_'+the_select_group);
		//alert(the_select_group+' '+the_day_elem+' '+the_month_elem+' '+the_year_elem);
		window.populate(the_day_elem,the_month_elem,the_year_elem);
		
	},
    'input.do_ajax_update:click':function(element,e){
		if(window.ajax_update){
    		window.ajax_update(element,e);
    	}
    },
	'input.do_confirm_form:click':function(element,e){ 
		var fRet = confirm('Are you sure you want to '+element.value); 
		//alert(fRet);
		//if the confirm is false (cancelled) then cancel the bubble
		if(! fRet){
			Event.stop(e);
		} 
	},
	'table.create_select_all':function(element){
		//search for checkbox
		var has_checkbox=false;
		var options = element.getElementsByTagName('INPUT');
		$A(options).find(function(check_element) {
				if(check_element.type=='checkbox'){
					has_checkbox=true;
					return true;
				}
			}
		);

		if(has_checkbox && window.select_all){
			//add select all and de-select all
			
			var insert_html='<label for="'+element.id+'_select_all"></label><input type="button" id="'+element.id+'_select_all" title="Select all" value="Select All" class="js_action_button"/><input type="button" id="'+element.id+'_deselect_all" title="Deselect all" value="Deselect All" class="js_action_button clear_right" />';
			new Insertion.After(element.parentNode,insert_html);
			
			$(element.id+'_select_all').observe('click', checkbox_select_all);
			$(element.id+'_deselect_all').observe('click', checkbox_deselect_all);
		}		
	},
	
	'.ajax_checkbox':function(element){
		if(window.ajax_checkbox_click){
    		window.ajax_checkbox_click(element);
    	}
	},
	'.js_get_markers:click':function(element,e){
		
		if(window.ajax_get_markers){
			window.ajax_get_markers(element,e);
		}
	},
	
	'.school_id_search':function(element){
    	if(window.create_school_id_search){
    		window.create_school_id_search(element);
    	}
    },
    '.audio_player:click':function(element,e){
    	if(window.create_audio_player){
    		window.create_audio_player(element);
    		Event.stop(e);
    	}
    },
	'a.js_new_window:click':function(element,e){
		var newWindow = window.open(element.href, 'TC_window');
		newWindow.focus();
		Event.stop(e);
	},
	
    '.do_show_hide:click':function(element){
    	window.show_element_from_click(element);
    },
    
    '.do_show_hide_element':function(element){
    	window.hide_element_check(element);
    },
	
	'.do_hide_element':function(element){
    	element.hide();
    },
    
    '.do_print:click':function(element){
    	window.print();
    },
    
    'table.click_table_row:click':function(element,e){
    	if(window.click_table_rows){
    		window.click_table_rows(e);
    	}
    },
	'table.click_table_row:mouseout':function(element,e){
		if(window.event_row_onmouseout){
    		window.event_row_onmouseout(e);
    	}
    },
	'table.click_table_row:mouseover':function(element,e){
    	if(window.event_row_onmouseover){
    		window.event_row_onmouseover(e);
    	}
    },
    'table.sortable_table':function(element){
    	//window.sortables_init(element);
		window.SortableTable.init(element);
    },
    'input.numeric_only:change, textarea.numeric_only:change':function(element){
		
		var v=element.value;
		if(isNaN(v) || /[^\d]/.test(v)){
			window.show_alert("Numbers only please.");
			element.value=v.gsub(/[^\d]/, '');
		}
    },
    'input.letters_only:keypress, textarea.letters_only:keypress':function(element,e){
    	if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		if (code > 31 && (code < 65 || code > 90) && (code < 97 || code > 122)) {
	        window.show_alert("Enter letters only.");
	        Event.stop(e);
	    }
    },
    'input.check_mobile:change':function(element){
    	if( element.value.charAt(0)==0 && element.value.charAt(1)==7){
			//input is a mobile
			if(element.value.length < 11){
				//not long enough (english only)
				window.show_alert("You must enter a mobile number that is 11 digits long");
				element.value='';
			}
		}else{
			window.show_alert("You must enter a mobile number begining with '07', or leave the field blank");
			element.value='';
		}
    },
    'input.check_uk_num:change':function(element){
		if(element.value.length < 11 || element.value.length > 11){
			//not long enough (english only)
			window.show_alert("You must enter a phone number that is 11 digits long");
			element.value='';
		}
    },
    
    'input.min_max:change':function(element){
    	if($(element.name+"_min_value") && $(element.name+"_max_value")){
			min_value=$(element.name+"_min_value").value;
			max_value=$(element.name+"_max_value").value;
			
			the_value=parseInt(element.value);
			if( the_value<min_value ){
				window.show_alert("You must enter a number greater than "+min_value);
				element.value=min_value;
			}
			if( the_value>max_value ){
				window.show_alert("You must enter a number less than "+max_value);
				element.value=max_value;
			}
		}
    },
    
    'input.email_only:change':function(element){
		alert("check email");
    	window.check_email_address2(element.value);
    },
    
    'input.count_chars:keypress, textarea.count_chars:keypress,input.count_chars, textarea.count_chars':function(element){
    	window.set_character_count(element);
    },
    'form.check_form:submit':function(element,e){
    	returned=window.checkform(element,e);
    },
	'form.validate_form':function(element){
		new Validation(element,{useTitles : true, immediate : true});
	},
	'form.validate_form input.submit_button:click':function(element){
		//set this element as the one for the working animation
		Element.addClassName(element, "js_do_submit_animation");
	},
	
    'form.main_form':function(element){
    	element.focusFirstElement();
    },
    'input:change, input':function(element){
    	//change the required to a tick when form element filled in
   		el_name=element.name;
    	el_value=element.value;
	
    	if(el_name.substr( el_name.length - 4 ) == '_req' || Element.hasClassName(element,'required')){
    		//add the filled in functionality
    		previous_array=element.previousSiblings();
    		if(previous_array.length > 0){
    			inner_array=previous_array[0].immediateDescendants();
    			if(inner_array.length > 0){
    				//alert(inner_array[0].innerhtml);
    				if(el_value == ''){
    					inner_array[0].removeClassName("tick_icon");
    				}else{
    					inner_array[0].addClassName("tick_icon");
    				}
    			}
    		}
    	}
    },
    'input.count_chars:keypress, textarea.count_chars:keypress':function(element,event){
    	if(event.keyCode!=8 && event.keyCode!=46){
    		window.set_character_max(element);
    	}
    },
	'table.final_row_total':function(element){
    	var all_rows=element.select("tr");
		all_rows[all_rows.length-1].addClassName('table_total_row');
    },
	
    'select.do_submit_onchange:change':function(element,event){
    	element.form.submit();
    	window.disable_enable_submit(element.form,true);
    }
   
  };

Event.observe(window, "load", function() { EventSelectors.start(Rules) });

function set_character_count(element){
	//get the name of the count field
	element_to_show=$(element.id+'_char_amount');
	if(element_to_show){
		element_to_show.value=element.value.length;
	}
};

function set_character_max(element){
	//get the name of the count field
	element_max=$(element.id+'_max_chars');
	if(element_max){
		if(element_max.value <= element.value.length){
			window.show_alert("You are up to your character limit");
			element.value=element.value.substr( 0,element.value.length - 1 );
		}
		
	}
};
function show_element_from_click(element){
	
	var el_id=element.id;
	element_to_show=$(el_id+'_show_hide');
	if(element_to_show){
		/*
		if(element_to_show.visible()){
			Cookie.set(element_to_show.id,'do_hidden',600);
		}else{
			Cookie.set(element_to_show.id,'do_show',600);
		}
		*/
		element_to_show.toggle();
	}
	if(element.type=='radio'){
		//get the name and check off
		the_radio_list=document.getElementsByName(element.name);
		if(the_radio_list.length > 1){
			for(i=0;i<the_radio_list.length;i++){
				element_to_show=$(the_radio_list[i].id+'_show_hide');
				element_to_show.toggle();
			}
		}
	}
	
};
function hide_element_check(element){
	//change the class to hidden if loaded as a check
	the_id=element.id;
	element_checked=$(the_id.substr( 0,the_id.length - 10 ));
	//alert(the_id.substr( 0,the_id.length - 10 ));
	if(element_checked){
		element_to_show=element;
		if(element_to_show){
			if(element_checked.type=='checkbox'){
				if(element_checked.checked){
					element_to_show.show();
				}else{
					element_to_show.hide();
				}
			}else{
				element_to_show.hide();
			}
		}
	}
};

function check_email_address(v) {
	if(v.blank() || !/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v)){
		show_alert("The email address may not be formatted correctly. Please verify.");
	}
};

function show_alert(the_text){
	//so i can remove the alert later
	alert(the_text);
};

var Cookie = {
	set: function(name,value,seconds){
		if(seconds){
			d = new Date();
			d.setTime(d.getTime() + (seconds * 1000));
			expiry = '; expires=' + d.toGMTString();
		}else
			expiry = '';
		document.cookie = name + "=" + value + expiry + "; path=/";
	},
	get: function(name){
		nameEQ = name + "=";
		ca = document.cookie.split(';');
		for(i = 0; i < ca.length; i++){
			c = ca[i];
			while(c.charAt(0) == ' ')
				c = c.substring(1,c.length);
			if(c.indexOf(nameEQ) == 0)
				return c.substring(nameEQ.length,c.length);
		}
		return null
	},
	unset: function(name){
		Cookie.set(name,'',-1);
	}
};
function cancel_bubble_event(event) {
	//cancel the bubble but not the default action
	if (event.stopPropagation){
		event.stopPropagation(); 
    }else{                   
	  event.cancelBubble = true;
    }
}
function fireEvent(element,event){
    if (document.createEventObject){
        // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else{
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}
//string whitespace from start and end of a string
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }