<!--

// adds a field to a form - cross browser friendly
function addField (form, fieldType, fieldName, fieldValue)
		{
		var input = document.createElement('INPUT');

		if (document.getElementById)
			{
			if (document.all)
				{

				// what follows should work
				// with NN6 but doesn't in M14
				input.type = fieldType;
				input.name = fieldName;
				input.value = fieldValue;
				input.id = fieldName;
				}
			else if (document.getElementById)
				{ // so here is the
				  // NN6 workaround
				  input.setAttribute('type', fieldType);
				  input.setAttribute('name', fieldName);
				  input.setAttribute('id', fieldName);
				  input.setAttribute('value', fieldValue);
				}

				form.appendChild(input);
			}
		}

YAHOO.util.Dom.getElementsByAttribute = function getElementsByAttribute(atr, val, tag, root) {
		var method = function(el) { 
			var re = new RegExp('(?:^|\\s+)' + val + '(?:\\s+|$)');
			if ( el.getAttribute(atr) && re.test(el.getAttribute(atr)) ) {
				return true;
			}
			return false;
		};
		return this.getElementsBy(method, tag, root);
	};

/*---------------------------------------------------------------*/
/* Validate user login details                                   */
/*---------------------------------------------------------------*/
function validate_login(formObj){
	
	/*validate user_name*/	
	if (isEmpty(formObj.user_name.value)){
		alert("Enter your username"); 
		formObj.user_name.focus();
		return false;
	}
	
	if (!isAlphanumeric(stripBlanks(formObj.user_name.value))){
		alert("This filed must only contain alphanumeric characters"); 
		formObj.user_name.focus();
		return false;	
	}
	
	/* validate password*/
	if (isEmpty(formObj.user_password.value)){
		alert("Enter your password"); 
		formObj.user_password.focus();
		return false;
	}	
	
	if (!isAlphanumeric(stripBlanks(formObj.user_password.value))){
		alert("This filed must only contain alphanumeric characters"); 
		formObj.user_password.focus();
		return false;	
	}
	
	
	
	/*all checks ok so return true*/
	return true;				
}
/*---------------------------------------------------------------*/
/* single checkbox clicked                              */
/*---------------------------------------------------------------*/
function item_checked(checkbox){
			
	var first_underscore_position = checkbox.id.indexOf('_');

	var checkbox_type = checkbox.id.substring(0,first_underscore_position);
	
}

function set_region_checkbox(country_region_id){			
		
	var region_checkboxes = YAHOO.util.Dom.getElementsByClassName('region_checkbox', 'input');
	
	for(i=0;i<region_checkboxes.length;i++){
		
		var first_underscore =  6;
						
		var region_id = region_checkboxes[i].id.substring(first_underscore+1);
						
		if(region_id == country_region_id){
			region_checkboxes[i].disabled= false;
			region_checkboxes[i].checked = true;			
		}
	}
	
}

function unset_region_checkbox(country_region_id){			
	
	
	// if ALL the checkboxes for the region are now unchecked, also uncheck the region checkbox	
	
	// get array of the country checkboxers applicable
	var checkboxes = YAHOO.util.Dom.getElementsByAttribute('fk_region_id',country_region_id,'input','country_checkbox_div');
			
	// loop thorugh the country checkboxes for the region
	var at_least_one_country_checked = false;
	
	for(i=0;i<checkboxes.length;i++){
				
		if(checkboxes[i].checked == true){
			at_least_one_country_checked = true;
			break;			
		}						
	}	
	
	if(at_least_one_country_checked==false){
		// uncheck the region
		document.getElementById('region_' + country_region_id).checked = false;
	}	
	
}

function all_age_range_checked(checkbox){
								
	if(checkbox.checked == false){								
		var checkboxes = YAHOO.util.Dom.getElementsByClassName('age_range_checkbox','input');
	
		for(i=0;i<checkboxes.length;i++){										
			checkboxes[i].checked= false;						
		}
		
	}else{
		
		var checkboxes = YAHOO.util.Dom.getElementsByClassName('age_range_checkbox','input');
		
		for(i=0;i<checkboxes.length;i++){										
			checkboxes[i].checked= true;						
		}	
	}
					
}

function all_issue_checked(checkbox){
								
	if(checkbox.checked == false){								
		var checkboxes = YAHOO.util.Dom.getElementsByClassName('issue_checkbox','input');
	
		for(i=0;i<checkboxes.length;i++){										
			checkboxes[i].checked= false;						
		}
		
	}else{
		
		var checkboxes = YAHOO.util.Dom.getElementsByClassName('issue_checkbox','input');
		
		for(i=0;i<checkboxes.length;i++){										
			checkboxes[i].checked= true;						
		}	
	}
					
}


function all_country_checked(checkbox,region_id){
	
							
	if(checkbox.checked == false){								
		unset_all_country_checkboxes(region_id);
	}else{				
		set_all_country_checkboxes(region_id);
	}
					
}

function unset_all_country_checkboxes(region_id){
		
	var checkboxes = YAHOO.util.Dom.getElementsByAttribute('fk_region_id',region_id,'input','country_checkbox_div');
	
	for(i=0;i<checkboxes.length;i++){										
		checkboxes[i].checked= false;						
	}	
}

function set_all_country_checkboxes(region_id){
	
	var checkboxes = YAHOO.util.Dom.getElementsByAttribute('fk_region_id',region_id,'input','country_checkbox_div');
		
	for(i=0;i<checkboxes.length;i++){										
		checkboxes[i].checked= true;						
	}	
}

function maximize_minimize(fieldset){
						
	var last_underscore_position = fieldset.id.lastIndexOf('_');
	
	var table_name = fieldset.id.substring(0,last_underscore_position);
		
	var target_div = table_name +'_checkboxes';
	
	var obj = document.getElementById(target_div);
	
	if(obj.style.display == "none"){			
		obj.style.display = "";
	}else{											
		obj.style.display = "none";		
	}	 				 		
	
}

function upload_finished(){
	
	var holder = parent.document.getElementById('upload_status_display');
	
	holder.innerHTML = "hello";
			
}


function form_focus(input){
	document.getElementById(input).focus();
	
}

function validate_tag(formObj){
		
	if (isBlank(formObj.tag_name.value)){
		alert("Cannot be left blank"); 
		formObj.tag_name.focus();
		return false;	
	}	
	if (!isAlphanumeric(stripBlanks(formObj.tag_name.value))){
		alert("This filed must only contain alphanumeric characters"); 
		formObj.tag_name.focus();
		return false;	
	}				
	
	/*all checks ok so return true*/
	return true;
	
}

function textLimit(field, maxlen) {	
	//if (field.value.length > maxlen + 1)
	//	alert('The description has exceeded the maxiumum number of characters and has been truncated!');
	if (field.value.length > maxlen)
		field.value = field.value.substring(0, maxlen);
}

function validate_media_tags(formObj){
	
	// validate media title
	if (isBlank(formObj.media_title.value)){
		alert("Cannot be left blank"); 
		formObj.media_title.focus();
		return false;	
	}	
	if (!isAlphanumeric(stripBlanks(formObj.media_title.value))){
		alert("This filed must only contain alphanumeric characters"); 
		formObj.media_title.focus();
		return false;	
	}
	
	// validate media description
	if (isBlank(formObj.media_description.value)){
		alert("Cannot be left blank"); 
		formObj.media_description.focus();
		return false;	
	}	
	
	
	// check at least one tag has been applied!
	
	var number_of_elements = formObj.elements.length;
	var checked_element_count = 0;
	
	for(i=0;i<number_of_elements;i++){
		if(formObj.elements[i].type == 'checkbox'){
			
			if(formObj.elements[i].checked == true){
				checked_element_count++;				
				break;
			}
		}
	}
	
	if(checked_element_count == 0){
		alert('You must apply at least one tag to the Media item!');
		return false;
	}
	
	/* validate country file, issue file and lesson resource tagging */
	
	//alert(document.getElementById('media_sub_type_id'));
	
	if (document.getElementById('media_sub_type_id')){
		var $D = YAHOO.util.Dom;
		var media_sub_type_id = document.getElementById('media_sub_type_id').value;
				
		// get list of media sub type elements
		var media_sub_type_list = $D.getElementsByClassName('media_sub_type_hidden','input');
		var media_sub_type_name = '';
		
	
		
		for(i=0;i<media_sub_type_list.length;i++){
		
			if(media_sub_type_id == media_sub_type_list[i].value){
				
				var media_sub_type_name = media_sub_type_list[i].id;	
				//alert(media_sub_type_name);
					
			} // end IF
						
			switch (media_sub_type_name){
			
					case 'COUNTRY_FILE_MEDIA_SUB_TYPE':
						// get all country checkboxes and make sure only one is tagged
						var checkboxes = $D.getElementsByClassName('country_checkbox','input');
			
						// loop thorugh the country checkboxes for the region
						var check_count = 0;
	
						for(i=0;i<checkboxes.length;i++){
				
							if(checkboxes[i].checked == true){
								check_count++;										
							}						
						}	
	
						//alert(check_count);
						
						if(check_count < 1){
							alert('One country must be checked for a Country File');
							return false;
							break;
						}
						if(check_count > 1){
							alert('Only one country can be checked for a Country File');
							return false;
							break;
						}
						
						break;
				
					case 'ISSUE_FILE_MEDIA_SUB_TYPE' :
						// get all issue checkboxes and make sure only one is tagged						
						var checkboxes = $D.getElementsByClassName('issue_checkbox','input');
			
						// loop thorugh the country checkboxes for the region
						var check_count = 0;
	
						//alert(check_count);
						for(i=0;i<checkboxes.length;i++){
				
							if(checkboxes[i].checked == true){
								check_count++;										
							}						
						}	
	
						if(check_count < 1){
							alert('One Issue must be checked for an Issue File');
							return false;
						}
						if(check_count > 1){
							alert('Only one Issue can be checked for an Issue File');
							return false;
						}
																
						break;
				
					case 'LESSON_RESOURCE_MEDIA_SUB_TYPE' :
						// get all project checkboxes and make sure only one is tagged
						var checkboxes = $D.getElementsByClassName('project_checkbox','input');
			
						// loop thorugh the country checkboxes for the region
						var check_count = 0;
						//alert(check_count);
						for(i=0;i<checkboxes.length;i++){
				
							if(checkboxes[i].checked == true){
								check_count++;										
							}						
						}	
	
						if(check_count < 1){
							alert('One Project must be checked for a Project File');
							return false;
						}
						if(check_count > 1){
							alert('Only one country can be checked for a Project File');
							return false;
						}
		
						break;	
					
						
				} // end SWITCH
			
		} // end FOR				
	} // end IF
	
	
		
}

function validate_weblink(formObj){
					
	if (isEmpty(formObj.weblink.value)){
		alert("Enter a weblink"); 
		formObj.weblink.focus();
		return false;
	}	
	
	if (formObj.weblink.value=='http://'){
		alert("Enter a weblink"); 
		formObj.weblink.focus();
		return false;
	}
	
	return true;
}

function test_weblink(){
	
	var formObj = document.weblink_form;
	
	if(validate_weblink(formObj)){
	
		var link =	formObj.weblink.value;
		
		window.open(link);
		
		
	}
		
	
}

/*	
media_sub_type_selected()
@ 	returns 		nothing but submits the form supplied
@	description		interogates the media_sub_type select in the media tag form and hides page elements according to sub type selected
								
*/

function media_sub_type_selected(){
	
	var $D = YAHOO.util.Dom;
	
	var media_sub_type_id = document.getElementById('media_sub_type_id').value;
		
	// get list of media sub type elements
	var media_sub_type_list = $D.getElementsByClassName('media_sub_type_hidden','input');
	var media_sub_type_name = '';
	
	for(i=0;i<media_sub_type_list.length;i++){
	
		if(media_sub_type_id == media_sub_type_list[i].value){
			
			var media_sub_type_name = media_sub_type_list[i].id;	
									
		} // end IF
					
		switch (media_sub_type_name){
		
				case 'COUNTRY_FILE_MEDIA_SUB_TYPE':
					
					show_all_fieldsets();
					
					
					/* BELOW HIDES SOME TAG CATEGORIES
					var region_fieldset = document.getElementById('region_fieldset');																
					region_fieldset.style.display = "none";
	
					var issue_fieldset = document.getElementById('issue_fieldset');																
					issue_fieldset.style.display = "none";
		
					var project_fieldset = document.getElementById('project_fieldset');																
					project_fieldset.style.display = "none";
	
					var age_range_fieldset = document.getElementById('age_range_fieldset');																
					age_range_fieldset.style.display = "none";*/						 											
					
					break;
			
				case 'ISSUE_FILE_MEDIA_SUB_TYPE' :
					
					show_all_fieldsets();
					
					/* BELOW HIDES SOME TAG CATEGORIES
					var region_fieldset = document.getElementById('region_fieldset');																
					region_fieldset.style.display = "none";
	
					var country_fieldset = document.getElementById('country_fieldset');																
					country_fieldset.style.display = "none";
							
					var project_fieldset = document.getElementById('project_fieldset');																
					project_fieldset.style.display = "none";
	
					var age_range_fieldset = document.getElementById('age_range_fieldset');																
					age_range_fieldset.style.display = "none";*/
															
					break;
			
				case 'LESSON_RESOURCE_MEDIA_SUB_TYPE' :
					
					show_all_fieldsets();
				
					/* BELOW HIDES SOME TAG CATEGORIES
					var region_fieldset = document.getElementById('region_fieldset');																
					region_fieldset.style.display = "none";
	
					var country_fieldset = document.getElementById('country_fieldset');																
					country_fieldset.style.display = "none";
	
					var issue_fieldset = document.getElementById('issue_fieldset');																
					issue_fieldset.style.display = "none";
							
					var age_range_fieldset = document.getElementById('age_range_fieldset');																
					age_range_fieldset.style.display = "none";*/
	
					break;	
					
				default : 
				
					show_all_fieldsets();
					
					break;
					
			} // end SWITCH
		
	} // end FOR				
	
}


function show_all_fieldsets(){
	
	var region_fieldset = document.getElementById('region_fieldset');																
	region_fieldset.style.display = "";
	
	var country_fieldset = document.getElementById('country_fieldset');																
	country_fieldset.style.display = "";
	
	var issue_fieldset = document.getElementById('issue_fieldset');																
	issue_fieldset.style.display = "";
	
	var project_fieldset = document.getElementById('project_fieldset');																
	project_fieldset.style.display = "";
	
	var age_range_fieldset = document.getElementById('age_range_fieldset');																
	age_range_fieldset.style.display = "";
		
}

function display_time_limit_expired(){
	
	if(alert('dfdfdfd')){
		
		window.location.href='admin';	
	}

	
}

/*-------------------------
     Search Interface
----------------------------

update_country_id()
@	parameter 	source html element (region drop down)
@	parameter		target html element id (country drop down)
@ 	returns 		nothing
@	description	recieves a reference to the region drop down - the selected region id is sent to a process page, run in an iframe.
				This queries the db for the countries in the region and rebuilds the country drop down 
				
*/

function update_country_id(source , target_id)
	{
	// establish values
	var source_value = source.value;
	var source_frame = document.getElementById('resource_source_frame');
	
	// do redirect
	source_frame.src = '';
	source_frame.src ='/process/update_country_id/' + source_value + '/' + target_id;
	}
	
/*	
format_form_query_string()
@	parameter 	source html element (form)
@ 	returns 		nothing but submits the form supplied
@	description	recieves a reference to a form and makes up a code ignitor type URl -
				
				ie  age_range_id/1/region_id/1 instead of age_range_id=1&region_id=1 etc

*/

function format_form_query_string(page_form){
		
	// get information about the form and it's elements
	var target_form = page_form.id;

	var target_form_url = page_form.action;

	var target_form_elements = page_form.elements;
	
	var number_of_target_form_elements = page_form.elements.length;

	// loop through these elements and construct url
	for(var i = 0 ; i < number_of_target_form_elements ; i++){
		
		// filter out elements that are unwanted - ie buttons but add the rest in a key / value pairs
		switch(target_form_elements[i].type){
			case 'submit':
			
			break;
			default :
			
				target_form_url += target_form_elements[i].id + "/" + target_form_elements[i].value + "/";
				
			break;
		}	
	}

	// now we have our URL - remove the last slash and do a redirect as if the form has been submitted
	
	// the loction.href redirect method is used to maintain the history so the back buttion can be used
	target_form_url = target_form_url.substring(0,(target_form_url.length-1));
	
	window.location.href =target_form_url;
		
	return false;
}

/*-------------------------
     Download link
----------------------------

count_media()

@ 	returns 		nothing
@	description	hijacks various hyperlinks on the system and makes them do bad things
				media_download_link : redirects an iframe to download counter - which then does the redirecting to the media itself
				media_view_link : redirects an iframe to view counter - which then makes a pop up to the media itself
				weblink_media_view_link : redirects a pop up  to view counter - and then to the website itself
				media_delete_link : not really anything to do with counters but it fits in easily here and if you are able to look past that 
							       detail so are we.
				media_description : not really anything to do with counters but it fits in easily here and if you are able to look past that 
							       detail so are we - makes sure the maxlength of media_description text areas can be maintained
						
*/
YAHOO.count_media = function() {

	// shortcuts
	var $E = YAHOO.util.Event;
	var $D = YAHOO.util.Dom;
	var $ = $D.get;

	return {
		init: function(){
			

			// get an array of download links
			var media_download_link_list = $D.getElementsByClassName('media_download_link','a');

			// loop through and pass into click event listener
			for(var i = 0; i< media_download_link_list.length;i++){

				$E.on(media_download_link_list[i].id, 'click',this.download_link_counter);

			}	
			
			// get an array of view links
			var media_view_link_list = $D.getElementsByClassName('media_view_link','a');

			// loop through and pass into click event listener
			for(var i = 0; i< media_view_link_list.length;i++){

				$E.on(media_view_link_list[i].id, 'click',this.view_link_counter);

			}
			
			// get an array of weblink view links
			var weblink_media_view_link_list = $D.getElementsByClassName('weblink_media_view_link','a');
			
			// loop through and pass into click event listener
			for(var i = 0; i< weblink_media_view_link_list.length;i++){

				
				$E.on(weblink_media_view_link_list[i].id, 'click',this.weblink_view_link_counter);

			}
			
			// get an array of weblink view links
			var media_delete_link_list = $D.getElementsByClassName('media_delete_link','a');
			
			// loop through and pass into click event listener
			for(var i = 0; i< media_delete_link_list.length;i++){

				$E.on(media_delete_link_list[i].id, 'click',this.media_delete_link_message);

			}
			
			// get media_description
			var media_description_textarea = document.getElementById('media_description','a');
			
			if(media_description_textarea){
				$E.on(media_description_textarea.id, 'keydown',this.media_description_textarea_count);
			}
		},
			
		// define DOWNLOAD click event listener
		download_link_counter: function(e){
			
			// kill normal clicking activities -  all the way to the top
			$E.stopEvent(e);

			if(document.getElementById('media_source_frame')){
				
				document.getElementById('media_source_frame').src = "";
			
				document.getElementById('media_source_frame').src = "/resource_center/download_counter/" + this.id.substring(5,this.id.length);
				
			}
		},
		
		// define VIEW click event listener
		view_link_counter: function(e){
			
			// kill normal clicking activities -  all the way to the top
			$E.stopEvent(e);

			if(document.getElementById('media_source_frame')){
				
				// increment counter
				document.getElementById('media_source_frame').src = "";
			
				document.getElementById('media_source_frame').src = "/resource_center/view_counter/" + this.id.substring(5,this.id.length);
				
			}
		},
		
		// define weblink VIEW click event listener
		weblink_view_link_counter: function(e){
			
			// kill normal clicking activities -  all the way to the top
			$E.stopEvent(e);

			// open window
			if(!window.open('/resource_center/view_weblink/' + this.id)){
				alert('It is possible that your browser is blocking pop ups and this has prevented you from viewing this site.Please allow pop ups on this site and try again.');
			} 
			
		},
		
		// define DELETE media click event listener
		
		media_delete_link_message: function(e){
			
			// kill normal clicking activities -  all the way to the top
			$E.stopEvent(e);

			if(confirm('Are you sure you would like to delete this item from the system?')){

				location.href = this.href;
			}
			
		},
		
		// define description text area count
		media_description_textarea_count: function(e){
			
			// if the value length is equal or greater than the maxlength attribute then kill event
			var current_description = this.value;
			
			if(Number(this.value.length)>Number(this.getAttribute('maxlength'))){

				this.value = current_description.substring(0,140);
				// kill normal clicking activities -  all the way to the top

				$E.stopEvent(e);
				
			}
		}
	};
}();

// intialise count_download object on page load - run it within the scope of its parent
YAHOO.util.Event.on(window, 'load', YAHOO.count_media.init, YAHOO.count_media, true );


/*-------------------------
     Statistics
----------------------------

filter_statistic_result()

@ 	returns 		false
@	description	reloads the stats page based on a user selecting a number of records from the drop down
				
*/

function filter_statistic_result(){
	
	// establish url
	var category = "default";
	
	if(document.getElementById('category')){
		
		category = document.getElementById('category').value;
	}
	
	var search_result = -1;
	
	if(document.getElementById('search_result')){
		
		search_result = document.getElementById('search_result').value;
	}

	if(category != "default"){
		
		location.replace("/admin/statistics/category/" + category + "/number_of_results/" + search_result);
	}
	
	return false;
}

/*
filter_download_result()

@ 	returns 		false
@	description	reloads the stats page based on a user selecting a number of records from the drop down
				
*/

function filter_download_result(){
	
	// establish url
	
	var search_result = -1;
	
	if(document.getElementById('search_result')){
		
		search_result = document.getElementById('search_result').value;
	}

	// and go
	
	location.replace("/admin/statistics/category/downloads/number_of_results/" + search_result);
	
	return false;
}
//-->