// JavaScript Document
window.onload = init ;


function init() {
// set the event handler for the form 
init_contact_form() ;

do_map() ;
//init_directions_button() ;

}
	

function init_contact_form() {
	document.getElementById("contact_form").onsubmit = check_contact_form ;
	document.getElementById("load_example").onclick = get_example_addresses ;
	document.getElementById("show_directions").onclick = check_directions_input ; // in the maps.js
}
function check_directions_input() {
	var str = document.getElementById("address_from").value ;
	if(str.length>2 ){
	do_directions();
	} else {
	 alert("Please enter some text in the box.\n Example: Market Square, Nottingham, NG1,UK \n NB: Note the use of comma's, Uk at the end often helps too.");	
	}
 
}

function check_contact_form() {
	msg_f = "" ;//new Array() ;// initialise the fail string: if empty at the end the we can proceed 
	// don't bother to check email here use php for sure     // if(!is_email()) 	{msg_f += "* Your email address.\n" ;}
	if(!is_name()) 		{msg_f += "* Your name was too short. A minimum of 3 characters required.\n";} // ;
	if(!is_message()) 	{msg_f += "* Your message was too short. A minimum of 5 characters required.\n";} // ;

	
	if(msg_f.length>0) {
	//document.getElementById("msg_c").innerHTML = "The Form had the following errors:<hr />" + msg_f.replace(/\n/ig,"<br />") ; //.replace(/\n/g,'<br />');
		alert("* Form Failure: \n"+msg_f) ; 
		return false ;
	} else {
	//alert("was OK") ; 
		return true ;
	}
}

function is_name() {
var str = document.getElementById("name").value.replace(/\s/g,'') ;	
	
	if(str.length<3 ) {
		do_pass_fail('name',false)
		return false ; //"Your name was too short.\n";
	} 
		do_pass_fail('name',true)
		return true ; 
}

function is_email() {
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	str = document.getElementById('email').value;
		if(str.match(emailRegEx)){
			do_pass_fail('email',true)
			return true ; 
		} else {
			do_pass_fail('email',false)
			return false ; 
		}
}
function is_message() {
	var message = document.getElementById("message").value ;
	if(message.length<5) {
		do_pass_fail('message',false);
		return false ;
	} else {
		do_pass_fail('message',true);
		return true 
	}
}
function is_reason() {
	if(document.getElementById("reason").selectedIndex<1) {
		str_return = false ;
	} else {
		str_return = true 
	}
	do_pass_fail('reason',str_return)
	return str_return  ; // still here?
}

function do_pass_fail(element,str_return) {
	if(str_return) { // you passed true
		document.getElementById(element).style.border = "1px solid green" ; 
		document.getElementById('label_'+element).style.border = "1px solid green" ; 
		document.getElementById('label_'+element).style.color = "green"
		document.getElementById('label_'+element).style.color = "green"
	} else { // you failes false 
	
		//document.getElementById("contact_text").style.display = "none" ; 
		document.getElementById(element).style.border = "1px solid red" ; 
		document.getElementById('label_'+element).style.border = "1px solid red" ; 
		document.getElementById('label_'+element).style.color = "red"
		document.getElementById('label_'+element).style.color = "red"
		
	}
}