/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function toggle_blind( id ) {
	
	if ( $( id ).visible() ) {
		Effect.BlindDown( id );
		
	} else {
		Effect.BlindDown( id );
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function erase_input_default( form, defaultValue, elemID ) {
	
	if (elemID) {
		// The element's ID has been supplied. This is being used for the search
		// input in the global nav. We're going to turn this copy from grey to black...
		var elem = $(elemID);
		
		elem.setStyle({color:"#000000"});
	}
	
	if (form.value == defaultValue) {
		form.value = '';
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function checkform(ref)
{
	var errorActivated = false;
	
	// Test if DOM is available and there is an element called required
	if(!document.getElementById || !document.createTextNode){return;}
	if(!document.getElementById('required')){return;}

	// Define error messages and split the required fields
	var errorID='errormsg';
	var errorClass='_error';
	var errorMsg='';
	var reqfields=document.getElementById('required').value.split(',');
	
	// Clean up old mess
	
	$('form_error_msg').hide();
	
	for(var i=0;i<reqfields.length;i++)
	{
		var f=document.getElementById(reqfields[i]);
		if(!f){continue;}
		
		var curClassName = f.className;
		var index = curClassName.indexOf(errorClass);
		
		if (index != -1) {
			f.className = curClassName.substr(0,index);
		}
	}
		
	// loop over required fields
	for(var i=0;i<reqfields.length;i++)
	{
		
		// check if required field is there
		var f=document.getElementById(reqfields[i]);
		if(!f){continue;}
		
		// test if the required field has an error, 
		// according to its type
		switch(f.type.toLowerCase())
		{
			case 'text':
			
				// email is a special field and needs checking
				if(f.id.indexOf('email') != -1 && !cf_isEmailAddr(f.value)){cf_add_error(f, 'Email address is invalid')}
				
				if(f.value=='' && f.id!='email'){cf_add_error(f)}							
				
										
			break;
			case 'textarea':
				if(f.value==''){cf_add_error(f)}							
			break;
			
			case 'password':
				if(f.value==''){cf_add_error(f)}
				
				// password must be four characters
				if(f.value.length < 4){cf_add_error(f, 'Password must be at least 4 characters')}
				
			break;
			
			case 'checkbox':
				if(!f.checked){cf_add_error(f)}							
			break;
			case 'select-one':
				if(!f.selectedIndex && f.selectedIndex==0){cf_add_error(f)}							
			break;
		}// end switch
	}// end for
	
	
	return !errorActivated;
	
	
	function cf_isEmailAddr(str) 
	{
	    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
	}
	
	function cf_add_error(o,msg)
	{
		errorMsg += msg;
		
		if (o.className.indexOf(errorClass) == -1) o.className= o.className + errorClass;
		
		// Check if there is no error message
		if(!errorActivated)
		{
			// create errormessage and insert before submit button
			errorActivated = true;
			Element.update('form_error_msg', errorMsg);
			
			$('form_error_msg').show();
			new Effect.Highlight( $('form_error_msg') );
			
			if (msg != '' && msg != undefined) {
				//alert(msg);
			}
				
		} 
	}
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength) {
		this.relatedElement.className = 'toomuch';
	} else {
		this.relatedElement.className = '';
	}
	this.relatedElement.firstChild.nodeValue = currentLength;
	
	// not innerHTML
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
