var main_links;
var main_sub_menuus;
var active_sub_menu;
var active_sub_menu_height;
var next_active_sub_menu;
var active_menu_link;
var height_animator = new Fx;
var page_content_height;
var in_ie_hell=false;
var tags_need_resizing = false;
var collection_item_links, expo_gal_links, EL_bg_image, sim_item_links, document_body;

window.addEvent('domready', function() {

	activate_mmka_menu();
	if($$('a.long_text_toggle').length>0) { toggle_long_text(); }
	document_body =$(document.body);

	if(document_body.id=='home') { 
		if(document_body.hasClass('no_animation')==false) { start_home_animation(); }
		toggle_blocks($$('a.toggle_more_info'), $$('.content_holder.more_holder'));
	}
	// EXPO GALLERY
	if($('expo_gallery')) { expo_gal(); }
	// TAGGED BG IMAGE
	
	if(Browser.Engine.trident==true) { 
		in_ie_hell=true; 
		reset_hr_elements();
	}
	if($('page_menu')!=null) {
		init_sub_menu_toggler();
	}
	// OTHER TOGGLING BUSINESS
	if($('agenda_expo_archive')!=null) {
		toggle_archive_years();
		toggle_blocks($$('a.toggle_more_info'), $$('#agenda_expo_upcoming', '#agenda_expo_archive'));
	}
	if($('reset_col_form')!=null) { init_col_form_resetting(); }
	// CLOSE ACTIVITY
	if($('close_activity')!=null) { set_activity_closer(); }
	
	// COLLECTION TIPS
	if($$('a.col_item').length>0) { 
		collection_item_links = $$('a.col_item');
		init_col_tips(collection_item_links); 
		init_collection_focus_links();
	}
	// SIM TIPS
	if($$('a.sim_item').length>0) { 
		sim_item_links = $$('a.sim_item');
		init_col_tips(sim_item_links); 
		init_sim_focus_links();
	}
	// EXPO GALLERY
	if($$('a.expo_gal_link').length>0) { 
		expo_gal_links = $$('a.expo_gal_link');
		init_expo_gal_links();
	}
	// PRESS LOGINS
	if($$('a.press_login').length>0) { 
		check_login_links();
	}
	// FULL SIZE BG
 	moosizer = new mooSizer({ bgElement:'page_bg_image' });
	EL_bg_image = $('page_bg_image');

	if($$('.tagged_area').length>0) { 
		tags_need_resizing=true;
		init_tagged_images(); 
	}
	// THANK YOU MAM?
	if($('msg_modal')!=null) {
		launch_modal('msg_modal');
	}
	max_length_textareas();
	init_search_validation();
	// IPAD & IPHONE FIXES
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
		// ALTER FIXED POSITIONED DIVS
		iFixes();
	}
	//transfer_fs_image_to_body();
	init_simple_croppper();
	// ONTKETEND TIPS
	if($$('a.ontketend_overview_item').length>0) { 
		init_col_tips($$('a.ontketend_overview_item')); 
	}
	check_gmaps();
	check_add_ontketend_comment();
	if($('ontketend_overview_map')!=null) {
		init_map();
	}
});


////////////////////////////////////////////////////////
//////////////// SITE MENU /////////////////////////////
////////////////////////////////////////////////////////

function activate_mmka_menu() {
	main_links = $$('#MMKA_navigation a.main_a');
	main_sub_menuus = $$('#MMKA_navigation div.MMKA_sub_menu');
	
	main_links.each(function(main_link, index) {
		main_link.store('his_index', index);
		main_link.store('has_sub_menu', true);
		main_link.store('is_active_page', false);
		if(main_link.hasClass('no_sub_menu')) { main_link.store('has_sub_menu', false); }
		if(main_link.hasClass('active')) { main_link.store('is_active_page', true); }
		main_link.addEvent('click', function() {
			if((active_menu_link) && (this.href==active_menu_link)) {
				next_active_sub_menu='';
				if((active_sub_menu.getStyle('margin-left')=='auto') || (active_sub_menu.getStyle('margin-left')=='0px')) {
					hide_sub_menu(active_sub_menu);
					if(this.retrieve('is_active_page')==false) {
						return true;
					}
				} else {
					show_sub_menu(this.retrieve('his_index'));
				}
			} else {
				show_sub_menu(this.retrieve('his_index'));
			}
			if(this.retrieve('has_sub_menu')==true) {
				return false;
			}
		});	
	});
}

function show_sub_menu(i) {
	active_menu_link=main_links[i];
	check_outside_menu_clicking('on');
	// CHECK OF ER ACTIVE MENU IS, ZOJA DIE EERST HIDEN, DAARNA 
	if(active_sub_menu) {
		next_active_sub_menu = main_sub_menuus[i];
		hide_sub_menu(active_sub_menu);
	} else {
		var end_height = main_sub_menuus[i].getStyle('height');
		main_sub_menuus[i].setStyle('height', 0);
		next_active_sub_menu='';
		slide_out_sub_menu(main_sub_menuus[i], 0, end_height);
	}
	active_sub_menu=main_sub_menuus[i];
}

function slide_out_sub_menu(element, start, finish) {
	toggle_active_links();
	element.setStyle('margin-left', 'auto');
	active_sub_menu_height = finish;
	height_animator = new Fx.Tween(element, { property: 'height', duration: 300 }).start(start, finish);
}

function hide_sub_menu(element) {
	var start=element.getStyle('height');
	var finish = 0;
	height_animator = new Fx.Tween(element, { property: 'height', duration: 150 } ).start(start, finish).chain(
	// zet ingeklapte menu terug naar links en op volledige hoogte, en kijk of er een volgende moet worden uitgeklapt
	function() {  
		active_menu_link.className='main_a';
		// active_menu_link='';
		element.setStyle('margin-left', -9999);
		element.setStyle('height', active_sub_menu_height);
		check_next_fade();
	}
	);
}
function check_next_fade() {
	if(next_active_sub_menu) {
		var end_height = next_active_sub_menu.getStyle('height');
		next_active_sub_menu.setStyle('height', 0);
		slide_out_sub_menu(next_active_sub_menu, 0, end_height);		
		next_active_sub_menu='';
	} else {
		check_outside_menu_clicking('off');
	}
}

function check_outside_menu_clicking(on_off) {
	if(on_off=='on') {
		$(document.body).addEvent('click', outside_click_close_sub_menu);
	} else {
		$(document.body).removeEvent('click', outside_click_close_sub_menu);
	}
}

function outside_click_close_sub_menu(e) { 
	if((active_sub_menu) && !$(e.target).getParent('.main_menu')) { 
		hide_sub_menu(active_sub_menu);
	} 
}

function toggle_active_links() {
	for(var i=0;i<main_links.length; i++) {
		if((active_menu_link) && (main_links[i]==active_menu_link)) {
			main_links[i].className='main_a active_sub_menu';	
		} else {
			main_links[i].className='main_a';	
		}		
	}
}
////////////////////////////////////////////////////////////
////////////// TOGGLE SUBMENU'S //////////////////////////////////////////////
////////////////////////////////////////////////////////////
var subs_to_toggle = new Array();
var sub_togglers;

function init_sub_menu_toggler() {
	sub_togglers = $$('a.toggle_sub_menu');	
	sub_togglers.each(function(sub_toggler, index) {
		subs_to_toggle[index] = $(sub_toggler.rel);
		if(sub_toggler.id!='active_toggled_menu') {
			$(sub_toggler.rel).setStyle('display', 'none');
		}
		sub_toggler.addEvent('click', function() {
			toggle_sub_menu(sub_toggler);
			return false;
		});
	});
}
function toggle_sub_menu(sub_toggler) {
	sub_id = sub_toggler.rel;
	subs_to_toggle.each(function(sub_to_toggle, index) {
		if(sub_to_toggle.id==sub_id) {
			if((sub_to_toggle.style.display=='block') || (sub_to_toggle.style.display=='')) {
				sub_to_toggle.setStyle('display', 'none');
				sub_togglers[index].removeClass('opened');
			} else {
				sub_to_toggle.setStyle('display', 'block');
				sub_togglers[index].addClass('opened');
			}
		} else {
			sub_to_toggle.setStyle('display', 'none');
			sub_togglers[index].removeClass('opened');
		}
	});
}
////////////////////////////////////////////////////////////
////////////// TOGGLE ARCHIVE YEARS //////////////////////////////////////////////
////////////////////////////////////////////////////////////
function toggle_archive_years() {

	// ZORG DAT PAGINA GOED MEE SCROLLED
	var archive_holder = $('agenda_expo_archive');
	var archived_expo_list = $('archived_expo_list');
	var archive_holder_init_size = 24;
	
	var year_togglers = $$('a.view_archive_year');
	year_togglers.each(function(year_toggler, index) {
		var year_to_toggle = year_toggler.id;
		year_to_toggle = year_to_toggle.replace('archive_', '');
		var year_listings_class = 'archive_year_'+year_to_toggle;
		var his_year_listings = $$('li.'+year_listings_class);
		his_year_listings.each(function(year_list, index) { year_list.setStyle('display', 'none'); });
		year_toggler.his_listings = his_year_listings;
		
		year_toggler.addEvent('click', function() {

			if(year_toggler.his_listings[0].style.display=='none') {
				year_toggler.his_listings.each(function(year_list, index) { year_list.setStyle('display', 'block'); });
				year_toggler.addClass('year_opened');
			} else {
				year_toggler.his_listings.each(function(year_list, index) { year_list.setStyle('display', 'none'); });
				year_toggler.removeClass('year_opened');
			}
			var arch_holder_size = archived_expo_list.getSize();
			archive_holder.setStyle('height', arch_holder_size.y+archive_holder_init_size);
		    return false;
		});
		// id#archive_2010
		// li.archive_year_2010
		//year_toggler.
	});
}
////////////////////////////////////////////////////////////
////////////// TOGGLE BLOKKEN //////////////////////////////////////////////
////////////////////////////////////////////////////////////

function toggle_blocks(toggle_triggers, els_to_toggle) {
	var toggle_height = new Fx;
	
	//alert('num toggle_triggers = '+toggle_triggers.length+' num els_to_toggle = '+els_to_toggle.length);
	// OG HEIGHT PAKKEN
	var els_to_toggle_height = new Array();
	for(var i=0; i<els_to_toggle.length; i++) {
		toggle_triggers[i].his_index = i;
		toggle_triggers[i].title='Klik om te bekijken';
		var holder_height = Math.ceil(els_to_toggle[i].getStyle('height').toInt());
		els_to_toggle_height[i] = holder_height;
	}
	// ALLEMAAL INKLAPPEN
		els_to_toggle.setStyles({
			height: '20px',
			overflow: 'hidden'
		});
		var start_height = 20;
	// TOGGLE HEIGHT FUNCTIE
		toggle_triggers.addEvent('click', function(){

			var his_holder = els_to_toggle[this.his_index];
			var his_holder_og_height = els_to_toggle_height[this.his_index];
			var cur_height = his_holder.getStyle('height').toInt();
			his_holder.set('tween', {duration: 200});

			toggle_height = new Fx.Tween(his_holder, {property: 'height', duration: 200});

			if(cur_height==start_height) {
				this.className='toggle_more_info opened';
				this.title='Klik om te sluiten';
				toggle_height.start(els_to_toggle_height[this.his_index]).chain(function() { his_holder.setStyle('overflow', 'visible'); });
			} else {
				this.className='toggle_more_info';
				this.title='Klik om te bekijken';
				his_holder.setStyle('overflow', 'hidden');
				toggle_height.start(start_height).chain(function() { });;
			}
		    return false;
		});
}


////////////////////////////////////////////////////////
//////////////// LONG TEXT TOGGLING ////////////////////
////////////////////////////////////////////////////////

function toggle_long_text() {
	var long_text_collapsed_h = '300px';
	var long_text_togglers = $$('a.long_text_toggle');
	for(var i=0; i<long_text_togglers.length; i++) {
		var text_to_toggle_id = long_text_togglers[i].rel;
		if($(text_to_toggle_id)) {
			var text_to_toggle = $(text_to_toggle_id);
			long_text_togglers[i].rel_div = text_to_toggle;
			long_text_togglers[i].rel_div_og_h = text_to_toggle.getStyle('height');
			text_to_toggle.setStyles({
				height: long_text_collapsed_h,
				overflow: 'hidden'
			});
			long_text_togglers[i].onclick=function() {
				var toggle_link = this;
				var div_to_shorten = this.rel_div;
				if(div_to_shorten.style.height==long_text_collapsed_h) {
					height_animator = new Fx.Tween
					(div_to_shorten, { property: 'height', duration: 300 })
					.start(long_text_collapsed_h, this.rel_div_og_h)
					.chain( 
						function () { 
							toggle_link.innerHTML = '&laquo; lees minder'; 
							div_to_shorten.setStyle('padding-bottom', '35px');
						} 
					);
				} else {
					height_animator = new Fx.Tween
					(div_to_shorten, { property: 'height', duration: 300 })
					.start(this.rel_div_og_h, long_text_collapsed_h)
					.chain( 
						function () { 
							toggle_link.innerHTML = '&raquo; lees meer'; 
							div_to_shorten.setStyle('padding-bottom', '0px');
						} 
					);
				}
				return false;
			}			
		}
	}
}

////////////////////////////////////////////////////////
//////////////// CLOSE ACTIVITY /////////////////////
////////////////////////////////////////////////////////
function set_activity_closer() {
	var close_button = $('close_activity');
	var activity_to_close = $('activity_focus');
	var divs_to_show = $$('div.hidden_under_activity');

	close_button.addEvent('click', function() {
		activity_to_close.setStyle('display', 'none');
		divs_to_show.each(function(div_to_show, index) { div_to_show.removeClass('hidden_under_activity'); });
		return false;
	});
}

////////////////////////////////////////////////////////
//////////////// COLLECTION BIZZLE /////////////////////
////////////////////////////////////////////////////////

function init_col_form_resetting() {
	var reset_col_form_but = $('reset_col_form');
	var collection_filter_form = document.forms['mmka_collection_filter'];
	reset_col_form_but.addEvent('click', function() {
		collection_filter_form.col_material.value='';
		collection_filter_form.col_subject.value='';
		collection_filter_form.col_artist.value='';
		collection_filter_form.col_period_start.value='';
		collection_filter_form.col_period_end.value='';
		return false;
	});
}
function init_col_tips(link_collection) {
	//var col_info_tips = new Tips('a.col_item');
	var col_info_tips = new Tips(link_collection,{
		className: 'tip_col_info',
		fixed: false,
		hideDelay: 50,
		showDelay: 50
	});
}
////////////////////////////////////////////////////////
//////////////// HELPING FUNCTIONS /////////////////////
////////////////////////////////////////////////////////

// PARENT TARGET CHECKERSSSSS
function has_parent_tag_and_id(theTarget, theTag, theId) {

	is_correct_parent = false;
	
	$(theTarget).getParents().each(function(el) {
	
	    if(el.get('tag') == theTag) {
        	if(el.getProperty('id') == theId) {
            	is_correct_parent = true;
	        }
    	}	
	});
	return( is_correct_parent );
}

function has_parent_tag_and_class(theTarget, theTag, theClass) {

	is_correct_parent = false;
	
	$(theTarget).getParents().each(function(el) {
	
	    if(el.get('tag') == theTag) {
	        if(el.hasClass(theClass)) {
	            returnme = true;
	        }
    	}
	
	});
	return( is_correct_parent );
}


////////////////////////////////////////////////////////
/////////// RESET HR TO DIV FOR FUCKING IE /////////////
////////////////////////////////////////////////////////
var hrs_content_bottom, hrs_visable ;
function reset_hr_elements() {
	// .content_bottom
	// .is_visable
	hrs_content_bottom = $$('hr.content_bottom');
	hrs_visable = $$('hr.is_visable');

	var div_content_bottom = new Element('div', {'class' : 'content_bottom'});
	var div_is_visable = new Element('div', {'class' : 'is_visable'});
	
	// REPLACE CONTENT_BOTTOM HR'S
	hrs_content_bottom.each(function(this_hr, i){
		//div_content_bottom.inject(this_hr, 'before');
		var new_div_content_bottom = div_content_bottom.clone().inject(this_hr,'before');
		this_hr.dispose();
	});
	// REPLACE IS_VISABLE HR'S
	hrs_visable.each(function(this_hr, i){
		var new_div_is_visable = div_is_visable.clone().inject(this_hr,'before');
		//div_is_visable.inject(this_hr, 'before');
		this_hr.dispose();
	});
}
////////////////////////////////////////////////////////
/////////// iPhone & iPad FAILS OPVANGEN... /////////////
////////////////////////////////////////////////////////
var footer_div, footer_top;
function iFixes() {
	///*
	// alert('hello iPad/iPhone user');
	footer_div = $('footer');
	// BG IMAGE HEIGHT
	var bg_image_dim = EL_bg_image.getSize();
	var bg_image_h = bg_image_dim.y;
	transfer_fs_image_to_body(EL_bg_image);
	// PAGE CONTENT HEIGHT
	var EL_page_content = $('page_content');
	var EL_page_content_dims = EL_page_content.getSize();
	var page_content_h = EL_page_content_dims.y;
	
	var footer_def_height = bg_image_h;
	if(page_content_h>bg_image_h) {
		footer_def_height = page_content_h;
	}
	
	// FOOTER HEIGHT
	var footer_dim = footer_div.getSize();
	var footer_height = footer_dim.y;
	// alert('iPAD TESTING: content H = '+page_content_h+' bg_image_h = '+bg_image_h)
	footer_top = footer_def_height - footer_height;
	iFix_footer();
	// alert('page_content H: '+page_content_dim.y);
	window.addEvent('scroll', function() {
		iFix_footer();
		// footer_div.setStyles('top': page_content_dim.y, 'bottom': 'auto');
	});
	//*/
}
function iFix_footer() {
	footer_div.setStyles({top: footer_top});
}
function transfer_fs_image_to_body() {
	var bg_image_src = $('bg_image').src;
	EL_bg_image.setStyle('display', 'none');
	var set_bg_image_str = 'url('+bg_image_src+')';
	//alert('set_bg_image_str = '+set_bg_image_str);
	$(document.body).setStyle('background-image', set_bg_image_str);
//	$(document.body).setStyle('background-size', 'cover');
}
////////////////////////////////////////////////////////
/////////// INSERT JS OUTPUTTER DIVSKIE /////////////
////////////////////////////////////////////////////////

function insert_output_div(parent_div) {
	var new_output_div = new Element('div', { id: 'js_output'});
	var insert_output_after = $(parent_div);
	// PAK ZN LEFT POS, ZODAT HIJ ALS LAATSTE ERBIJ STAAT
	new_output_div.inject(insert_output_after, 'top');
	js_output_ta=$('js_output');
	debug=true;
}
function get_clock_time() {
	var current_time = new Date();
	var hours = current_time.getHours();
	var minutes = current_time.getMinutes();
	if (minutes < 10){
		minutes = "0" + minutes;
	}
	var clock_time =  hours+':'+minutes;
	return clock_time;
}

////////////////////////////////////////////////////////
/////////// SIMPLE CROPPER /////////////
////////////////////////////////////////////////////////
function init_simple_croppper() {
	if($('ontketend_crop_holder')!=null) {
		var crop_holder = $('ontketend_crop_holder');
		var crop_dragger = $('ontketend_cropper');
		var cropper_move = new Drag.Move(crop_dragger, {container: crop_holder });
		if($('submit_simple_crop')!=null) {
			var submit_crop_pos=$('submit_simple_crop');
			var crop_form = submit_crop_pos.getParent('form');
			crop_form.addEvent('submit', function() {
				var drag_pos = crop_dragger.getPosition(crop_holder);
				crop_form.x.value = drag_pos.x;
				crop_form.y.value = drag_pos.y;
				// alert(drag_pos.x+' - '+drag_pos.y);
				// return false;
			});
		}
	}
}

///////////////////////////////////////////////////////////////
////////////////// GOOGLE MAP //////////////////////////////////
///////////////////////////////////////////////////////////////

var directionDisplay, directionsService;
var update_coords = false;
function check_gmaps() {
	if($$('div.google_map').length>0) {
		
		// directionsService = new google.maps.DirectionsService();
		// directionsDisplay = new google.maps.DirectionsRenderer();
				
		var google_maps = $$('div.google_map');
		google_maps.each(function(gmap, index) {
			set_google_map($(gmap.id), gmap.title);
		});
		if($('update_coords_trigger')!=null) {
			update_coords = $('update_coords_trigger').value;
		}
	}
}

function set_google_map(map_element, map_location) {
	// alert('map_element:' +map_element+' map_location: '+map_location);
	var geocoder;
	var map;
	
	var default_lat = 52.224979;
	var default_long = 5.622372;
	var infowindow;
	
	function initialize() {
		geocoder = new google.maps.Geocoder();
		var latlng = new google.maps.LatLng(default_lat, default_long);
		var myOptions = {
			zoom: 6,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		map = new google.maps.Map(map_element, myOptions);
	    // directionsDisplay.setMap(map);
		// directionsDisplay.setPanel(document.getElementById('location_route'));
    }
	initialize();

	geocoder.geocode( { 'address': map_location}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			// map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker({
					map: map, 
					position: results[0].geometry.location
			});
			map.setCenter(marker.position);
			// UPDATE COORDINATIONS VAN ONTKETEND ITEM VOOR VOLGENDE KEER
			if(update_coords!=false) {
				var coords_to_string = marker.position.toString();//.join('-') 
				var load_file = '_inc/_content_readers/load_ajax.inc.php?part=update_ontketend_location&id='+update_coords+'&coords='+coords_to_string;
				//alert(load_file);	=> // coords= (51.9851034, 5.898729600000024)
				sendRequest(load_file, handle_coord_update);
			}
			/*
			var info_window_header = '<strong>&#039;t Raboes</strong><br />';
			var infowindow = new google.maps.InfoWindow({
				content: info_window_header+map_location
			});
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map,marker);
			});				
			*/
		} else {
			alert("Geocode was not successful for the following reason: " + status);
		}
	});
	/*
	$('submit_get_directions').addEvent('click', function() {
		calculate_route();
		return false;
	});
	function calculate_route() {
		var start = document.getElementById("from_address_field").value;
		var end = map_location;
		var request = {
			origin:start, 
			destination:end,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};
		directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(response);
			}
		});
	}
	*/
}

////////////////////////////////////////////////////////
/////////// ADD ONTKETEND COMMENT /////////////
////////////////////////////////////////////////////////
function check_add_ontketend_comment() {
	if($('toggle_add_comment')!=null) {
		var EL_toggle_add_comment=$('toggle_add_comment');
		var EL_comment_form = $('hidden_comment_adder');
		var EL_hide_add_comment = $('hide_add_comment');
		EL_toggle_add_comment.addEvent('click', function() {
			var cur_add_display = EL_comment_form.getStyle('display');
			if(cur_add_display=='none') {
				EL_comment_form.setStyle('display', 'block');
			} else {
				EL_comment_form.setStyle('display', 'none');
			}
			return false;
		});
		EL_hide_add_comment.addEvent('click', function() {
			EL_comment_form.setStyle('display', 'none');
			return false;
		});
	}
}

////////////////////////////////////////////////////////
/////////// VALIDATE BUSINESS /////////////
////////////////////////////////////////////////////////

function max_length_textareas(){
    var objs = document.getElementsByTagName('textarea');
    var oi = 0; //oi is object index
    var thisObj;

    for (oi=0;oi<objs.length;oi++) {
        var this_name = objs[oi].id;
        thisObj = objs[oi];
        // note that maxlength is case sensitve
        if (thisObj.getAttribute('maxlength')){
            // CHECK OF ER EEN COUNTER SPAN IS
            var update_counter;
            var counter_span_id=thisObj.name+'_counter';
            if(document.getElementById(counter_span_id)) {
            	update_counter=true; 
            	objs[oi].his_counter = counter_span_id;           	
            } else {
            	update_counter=false;
            	counter_span_id='';
            }
            objs[oi].onkeyup = function() {
                force_max_length(this, update_counter);
            }
        }
    }
}

function force_max_length(the_textarea_to_force, update_counter) {
	var counter_id = the_textarea_to_force.his_counter;
    var max_length = parseInt(the_textarea_to_force.getAttribute('maxlength'));
    var cur_length = the_textarea_to_force.value.length;
    if(cur_length > max_length) {
        the_textarea_to_force.value = the_textarea_to_force.value.substring(0, max_length);
    }
    if(update_counter==true) {
    	var len_difference = max_length - cur_length;
    	document.getElementById(counter_id).innerHTML=len_difference;
    }
}
////////////////////////////////////////////////////////
/////////// VALIDATE BUSINESS /////////////
////////////////////////////////////////////////////////

function init_search_validation() {
	var search_form = document.forms.MMKA_search;
	var search_term_input = search_form.search_term;
	search_form.onsubmit=function() {
		if(!default_check(search_term_input)) { return false; }
		if(search_term_input.value.length<3) { 
			alert('Please enter a longer word'); 
			search_term_input.focus();
			return false; 
		}
		return true;
	}
}

function validate_press(theform) {
	if(!default_check(theform.press_user)) { return false; }
	if(!default_check(theform.press_pass)) { return false; }
	return true;
}
function validate_nb_subscribe(theform) {
	if(!default_check(theform.YMP0)) { return false; }
	if(echeck(theform.YMP0.value)==false) { 
		theform.YMP0.focus();
		return false; 
	}
	return true;
}

function validate_add_comment(theform) {
	if(!default_check(theform.poster_name)) { return false; }
	if(!default_check(theform.poster_email)) { return false; }
	if(!echeck(theform.poster_email.value)) { 
		theform.poster_email.focus();
		return false; 
	}
	if(!default_check(theform.msg_subject)) { return false; }
	if(!default_check(theform.comment_add)) { return false; }
	if(!default_check(theform.wut)) { return false; }
	return true;
}

function validate_new_ontk_item_1(theform) {
	if(!default_check(theform.ontk_user_name)) { return false; }
	if(!default_check(theform.ontk_user_mail)) { return false; }
	if(!echeck(theform.ontk_user_mail.value)) { 
		theform.ontk_user_mail.focus();
		return false; 
	}
	if(!default_check(theform.user_city)) { return false; }
	if(!validate_dropdown(theform.user_country)) { 
		alert('Please select a country');
		return false; 
	}
	
	if(!default_check(theform.file1)) { return false; }
	return true;
}
function validate_new_ontk_item_2(theform) {
	if(!default_check(theform.ontk_maker)) { return false; }
	if(!default_check(theform.ontk_material)) { return false; }
	if(!default_check(theform.ontk_item_story)) { return false; }
	return true;
}
function validate_ontketend_crop(theform) {
	var step_2_form = document.forms['step_2'];
	var save_first_notice = $('ontk_save_first');
	var step_2_saved_val = step_2_form.step_2_saved.value;
	if(
		(step_2_form.ontk_maker!='')
		||
		(step_2_form.ontk_material!='')
		||
		(step_2_form.ontk_item_story!='')
	) {
		if(step_2_saved_val=='no') {
			save_first_notice.setStyle('display', 'inline');
			return false;
		}
	}
}

var default_border_color = '#CCCCCC';
function default_check(input) {
	if(input!=undefined) {
		if ((input.value==null)||(input.value=="")){
    		input.style.borderColor="#ff0000";
			input.focus()
			return false;
		} else {
	    	input.style.borderColor=default_border_color;
    	    return true;
		}
	} else {
		return true;
	}
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Please fill out a valid email address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please fill out a valid email address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please fill out a valid email address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please fill out a valid email address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please fill out a valid email address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please fill out a valid email address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please fill out a valid email address")
		    return false
		 }

    return true;
}

// VALIDATE DROPDOWN SELECTS
function validate_dropdown(dropdown) {
	if (dropdown.selectedIndex==0) {
		return false;
	} else {
		return true;
	}
}

var op_i=0;
var output_div_available;
var output_div;
function output(string) {
	if(output_div_available==undefined) {
		if($('js_output')!=null) {
			output_div_available=true;
			output_div = $('js_output');
		} else {
			output_div_available=false;		
		}
	}
	if(output_div_available==true) {
	//$('output').appendText("<br />"+string);	
		var cur_content = output_div.innerHTML;
		output_div.innerHTML = cur_content+'<br />'+string;
	}
	return false;
}

