//auto value fields/////////////////////////
function nameBlur(obj_id, t_string){
	var obj;
	obj = window.document.getElementById(obj_id);
	if(obj.value ==""){
		obj.value = t_string;	
	}
}
function nameFocus(obj_id, t_string){
	var obj;
	obj = window.document.getElementById(obj_id);
	if(obj.value == t_string){
		obj.value = "";	
	}
}
//form validation/////////////////////////////////////////////////////////////////
function validate_email($email) {
	//Validates a correctly formatted email address
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test($email)) {
    	// failed validation do what you want here 
		return false;
    }else{
		return true;	
	}
}
function checkBlank(passed_field,passed_defualt){
	//passed_defualt represents any default description value the field has 
	var obj;
	obj = window.document.getElementById(passed_field);
	if(obj.value == passed_defualt || obj.value ==""){
		return false;	
	}else{
		return true;	
	}
}
function checkEmail(passed_field){
	var email_ok;	
	var obj;
	obj = window.document.getElementById(passed_field);
	email_ok = validate_email(obj.value);
	return email_ok;
}
function checkSubmit_contact(){	
	var first_name_ok;	
	var email_ok;	
	var hear_about_ok;
	
	first_name_ok = checkBlank("realname");
	email_ok = checkEmail("email");
	
	hear_about = window.document.getElementById("source").value;
	if(hear_about ==""){
		hear_about_ok = false;
	}else{
		hear_about_ok = true;
	}
	
	if(first_name_ok && email_ok && hear_about_ok){		
		//set up the cookies
		var first_name_val;
		var last_name_val;
		var company_val;
		var email_val;
		var telephone_val;
		var address_val;
		var comments_val;
		var hear_about_val;
		var newsletter_val;
		var source_other_val;
		//set up values for check boxes
		for(i=1; i<=13; i++) { 			
			var check_value;
			check_value = $('#checkbox_' + i).attr("checked");			
			if(check_value == true){
				$.cookie('checkbox_' + i, "checked");
			}
		}
		//set up selected drop for 'How did you hear about us:'
		
				
		first_name_val = window.document.getElementById("realname").value;		
		$.cookie('first_name', first_name_val);	
		
		last_name_val = window.document.getElementById("last_name").value;		
		$.cookie('last_name', last_name_val);	
		
		company_val = window.document.getElementById("company").value;		
		$.cookie('company', company_val);
		
		email_val = window.document.getElementById("email").value;		
		$.cookie('email', email_val);
		
		telephone_val = window.document.getElementById("telephone").value;		
		$.cookie('telephone', telephone_val);
		
		address_val = window.document.getElementById("address").value;		
		$.cookie('address', address_val);
		
		comments_val = window.document.getElementById("comments").value;		
		$.cookie('comments', comments_val);
		
		hear_about_val = window.document.getElementById("source").value;		
		$.cookie('hear_about', hear_about_val);
		
		newsletter_val = window.document.getElementById("newsletter").value;		
		$.cookie('newsletter', newsletter_val);
		
		source_other_val = window.document.getElementById("source_other").value;		
		$.cookie('source_other', source_other_val);
		
		//let the form post itself
		return true;
	}else{		
		var first_name_msg;		
		var email_msg;
		var hear_about_msg;
		var main_error;		
		
		first_name_msg = window.document.getElementById("firstname_contact_msg");
		email_msg = window.document.getElementById("email_contact_msg");
		hear_about_msg = window.document.getElementById("hear_about_contact_msg");	
		main_error = window.document.getElementById("main_error");	
		
		
		var no_of_problems;
		no_of_problems = 0;
		if(!first_name_ok){					
			first_name_msg.style.display = "block";
			no_of_problems ++;
		}else{
			first_name_msg.style.display = "none";
		}
		
		if(!email_ok){					
			email_msg.style.display = "block";	
			no_of_problems ++;
		}else{
			email_msg.style.display = "none";
		}
		
		if(!hear_about_ok){					
			hear_about_msg.style.display = "block";
			no_of_problems ++;
		}else{
			hear_about_msg.style.display = "none";
		}
		main_error.innerHTML = '&nbsp;&nbsp;There are <span class="nunmber_of_problems">' + no_of_problems +'</span> fileds which need to be completed. Please review the form.';
		main_error.style.display = "block";
		return false;
	}
}

function check_cookie_set(cookie){
	if(cookie == null){
		return "";
	}else{
		return cookie;	
	}
}
//function to populate forms with cookie data
$(document).ready(function() {
	
	//retreive the cookies			
	$("#realname").attr("value", check_cookie_set($.cookie('first_name')));
	$("#last_name").attr("value", check_cookie_set($.cookie('last_name')));
	$("#company").attr("value", check_cookie_set($.cookie('company')));
	$("#email").attr("value", check_cookie_set($.cookie('email')));
	$("#telephone").attr("value", check_cookie_set($.cookie('telephone')));
	$("#address").attr("value", check_cookie_set($.cookie('address')));
	$("#comments").attr("value", check_cookie_set($.cookie('comments')));
	$("#source_other").attr("value", check_cookie_set($.cookie('source_other')));
	//$("#comments").attr("value", check_cookie_set($.cookie('first_name')));
	
	for(i=1; i<=13; i++) { 		
		if($.cookie('checkbox_' + i) == "checked"){
			$('#checkbox_' + i).attr("checked", "true");	
		}
	}
	//set 'How did you hear about us: ' dropdown value
	$("#source option").each(function(i){    	
		if( $(this).val() == $.cookie('hear_about')){
			$(this).attr("selected", "selected");	
		}
    });
	//set newsletter dropdown
	$("#newsletter option").each(function(i){    	
		if( $(this).val() == $.cookie('newsletter')){
			$(this).attr("selected", "selected");	
		}
    });

});