//age check
function twoDigits(dig){
	var str = dig.toString();
	var digit = (str.length == 2) ? str : '0'+str;
	return digit;
}


function realMonth(mm){
	var realmonth = (mm < 12) ? mm + 1 : mm = 1;
	return realmonth;
}
			
// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber(str){
	var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
	return re.test(str);
}
	
// returns true if the string only contains characters A-Z, a-z or 0-9 or . or #
function isAddress(str){
		var re = /[^a-zA-Z0-9\#\.]/g
		if (re.test(str)) return true;
		return false;
}
	
// returns true if the string is 5 digits
function isZip(str){
		var re = /\d{5,}/;
		if(re.test(str)) return true;
		return false;
}
	
// returns true if the string only contains characters A-Z or a-z
function isAlpha(str){
		var re = /[^a-zA-Z]/g
		if (re.test(str)) return true;
		return false;
}

// returns true if string follows proper name conventions A-Z or - or ' or space 
function isName(str){
	var re = /^[a-zA-Z-'\s\.,]*$/
	if (re.test(str)) return true;
	return false;
}
	
// returns true if the string only contains characters A-Z or a-z or 0-9
function isAlphaNumeric(str){
		var re = /[^a-zA-Z0-9-'\s]/g
		if (re.test(str)) return false;
		return true;
}

function isNumeric(str){
		var re = /^\d+$/
		if (re.test(str)) return true;
		return false;
}

function isEmpty(str){
		if(str.length == 0 || str == null){
			return true;
		}else{
			return false;
		}
}
	
function isEmail(str){
	if(str == '') return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}
	
function stripWhitespace(str, replacement){
	if (replacement == null) replacement = '';
	var result = str;
	var re = /\s/g
	if(str.search(re) != -1){
		result = str.replace(re, replacement);
	}
	return result;
}

//function for showing inline error bubble
function hasErrors(fieldID, err){
	if(err){
		document.getElementById(fieldID).style.visibility = 'visible';
	}else{
		document.getElementById(fieldID).style.visibility = 'hidden';
	}
}

function clearError(id){
	$("#"+id).removeClass('err');
}

function validateNewsCommentForm(){
	
	var errors=0;
	var cname = $("#comment_name").val();
	var cemail = $("#comment_email").val();
	var ccomm = $("#comment_comment").val();
		
	if(isEmpty(cname) || !isName(cname) || cname == 'name'){
	$('#comment_name').addClass('err');
	errors++;
	}else{
	$('#comment_name').removeClass('err');
	}
	
	
	if(isEmpty(cemail) || !isEmail(cemail)){
	$('#comment_email').addClass('err');
	errors++;
	}else{
	$('#comment_email').removeClass('err');
	}
	
	if(isEmpty(ccomm) || ccomm == 'comment'){
	$("#comment_comment").addClass('err');
	errors++;
	}else{
	$("#comment_comment").removeClass('err');	
	}
	
	if(errors > 0){ return false; }
	
	return addComment();
}

function validateContactForm(){
	
	var errors=0;
	var fname = $("#contact_firstname").val();
	var lname = $("#contact_lastname").val();
	var add1 = $("#contact_address").val();
	var add2 = $("#contact_address2").val();
	var city = $("#contact_city").val();
	var state = $("#contact_state").val();
	var phon = $("#contact_telephone").val();
	var zip = $("#contact_zipcode").val();
	var subj = $("#contact_subject").val();
	var email = $("#contact_email").val();
	var mess = $("#contact_message").val();
		
	if(isEmpty(subj) || subj == 'subject*'){
	$('#contact_subject').addClass('err');
	errors++;
	}else{
	$('#contact_subject').removeClass('err');
	}
		
	if(isEmpty(email) || !isEmail(email)){
	$('#contact_email').addClass('err');
	errors++;
	}else{
	$('#contact_email').removeClass('err');
	}
	
	if(isEmpty(mess) || mess == 'message*'){
	$("#contact_message").addClass('err');
	errors++;
	}else{
	$("#contact_message").removeClass('err');	
	}
	
	if(errors > 0){ return false; }
	
	return sendContact();
}

function validateMailingList(){
	
	var errors=0;
	var mlmail = $("#ml_email").val();
		
	if(isEmpty(mlmail) || !isEmail(mlmail)){
	$('#ml_email').addClass('err');
	errors++;
	}else{
	$('#ml_email').removeClass('err');
	}
	
	if(errors > 0){ return false; }
	
	return addToMailingList();
}