var errorMessage  = "Sorry an error has occured, please try again or contact the site's adimistrator";

Event.observe(window, 'load', function() { placeNavigation() });

/**
* places the navigation arrow at the current page location
*
*/
function placeNavigation() {
	var navElements = new Array();
	
	//removes the arrow pointer from it's previous location
	try {
	    
	    var current = $('navigation').getElementsByClassName('current');
	        if( current.length > 0) {
	        	current[0].removeClassName('current');
	        } 
	        
	        if( $('urHere').value !== 'home' && $('urHere').value !== '' ){
	        	var here = 'nav_' + ($('urHere').value);
	        	$(here).parentNode.className = 'navelement current';			 
	        }
	        else {
	        	$('nav_home').parentNode.className = 'navelement current';
	        } 
	        
	    }    
	catch (err) {
	 
	}    
}

/**
* check the request contact form on /request
*
*/
function checkContactForm() {
 	
 	var errors = new Array();
	var services = $('rcf_services').getValue();   
	var formElements = $('requestContactForm').getElements();
  
	formElements.each(function(element) {
		element.style.background = "#fff";	
	} );
	
	// check if a service is selected  
	if( services.length <= 0) {
		errors.push('rcf_services');
	} 

	if ( services.indexOf('404') >= 0 ) {
	 
		if( $('rcf_note').value == null || $('rcf_note').value == "" || $('rcf_note').value == ' '){
			
			errors.push('rcf_note');
		} 	  
	}

  	// chick if contact name is provided
	if( $('rcf_contactName').value == null || $('rcf_contactName').value == "" || $('rcf_contactName').value == " ") {
		errors.push('rcf_contactName');	
	} else {
		if(!isValidPersonName( $('rcf_contactName'), false, false)) {
			errors.push('rcf_contactName');
		}
	}

	//check if the emali is valid 	
  	if( !($('rcf_email').value == null || $('rcf_email').value == "" || $('rcf_email').value == " ") ||
		!($('rcf_phone').value == null || $('rcf_phone').value == "" || $('rcf_phone').value == " ") ) {
		
		if( !($('rcf_email').value == null || $('rcf_email').value == "" || $('rcf_email').value == " ")) {
			
			if( !isValidEmail($('rcf_email'), false, false) ) {
				errors.push('rcf_email');
			}		
		}
	//check if the phone number is valid 
		else {
			if( !isValidPhone($('rcf_phone'), false, false) ){
				errors.push('rcf_phone');
			}	
		}	  
	} 
	else {
	  	errors.push('rcf_phone');
		errors.push('rcf_email');
	}
	////////////////////////////////////////////////////////////////////
	
	if( errors.length > 0) {
		// highlite invalid elemnts
		errors.each(function(element) {
			$(element).style.background = "#ffff66";
			$(element).parentNode.className="wrong_td";
		} );
		
		errors = new Array();
	  	return false;
  	}
  	errors = new Array();
  	 
  	return true;
}

/**
* checks if a textarea content is not over the maxlength allowed
* @param objext textbox 	
*/
function ismaxlength(textbox) {
	
	maxlength = textbox.getAttribute("maxLength");
	if( textbox.value.length >= maxlength ) {
		p = textbox.value.truncate(maxlength, '...');
		textbox.value = p;
	} 
	return ;
}	


/**
* checks if a givven name is a valid person name
*	
* @param object field, 
* @param bool wrong (put arrow on parent's background)
* @param bool paint ( paint field in yellow)	 
*/
function isValidPersonName (field, wrong, paint) {
	
	var pattern = new RegExp("^[a-zA-Z\-?\\s?]+$");
	
	if( !pattern.test( field.value )) {
		wrong ? field.parentNode.className="wrong_td" : {};
		paint ? field.style.background = "#ffff66" : {} ;
		
		return false;
	}
	
	paint ? field.style.background = "#fff" : {} ;
	wrong ? field.parentNode.removeClassName('wrong_td') : {};

	return true;
}

/**
* checks if a givven name is a valid e-mail address
*	
* @param object field, 
* @param bool wrong (put arrow on parent's background)
* @param bool paint ( paint field in yellow)	 
*/
function isValidEmail (field, wrong, paint) {
	
	var pattern = new RegExp("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$");
	
	if( !pattern.test( field.value )) {
		wrong ? field.parentNode.className="wrong_td" : {};
		paint ? field.style.background = "#ffff66" : {} ;
		
		return false;
	}
	
	paint ? field.style.background = "#fff" : {} ;
	wrong ? field.parentNode.removeClassName('wrong_td') : {};
	
	return true;
}

/**
* checks if a givven name is a valid phone number (no digit length specified)
*	
* @param object field, 
* @param bool wrong (put arrow on parent's background)
* @param bool paint ( paint field in yellow)	 
*/
function isValidPhone (field, wrong, paint) {
	if( field.value == ""){ return false;}
	var pattern = new RegExp("^[0-9\\s\\-]+$");
	
	if( !pattern.test( $('rcf_phone').value )) {
		wrong ? field.parentNode.className="wrong_td" : {};
		paint ? field.style.background = "#ffff66" : {} ;
		
		return false;
	}

	paint ? field.style.background = "#fff" : {} ;
	wrong ? field.parentNode.removeClassName('wrong_td') : {};
	
	return true;
}

