jQuery(document).ready(function() {
	var IC = {
		valid: true,
		
		checkEmail: function(str) {
			var at="@";
			var dot=".";
			var lat=str.indexOf(at);
			var lstr=str.length;
			var ldot=str.indexOf(dot);
			if (str.indexOf(at) == -1 || str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr || str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr || str.indexOf(at,(lat+1)) != -1 || str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot || str.indexOf(dot,(lat+2)) == -1 || str.indexOf(" ")!=-1) {
				return false;
			}
			return true;
		},
		
		checkTel: function(tel) {
			if (isNaN(IC.removeSpaces(tel)) == true) {
				return false;
			}
			tel = IC.removeSpaces(tel);
			if (tel.length <= 10) {
				return false;
			}			
			return true;
		},
		
		checkPostcode: function(postcode) {
			if (postcode.length <= 5 || postcode.length >= 9) {
				return false;
			}
			return true;
		},
		
		removeSpaces: function(string) {
			return string.split(' ').join('');
		}
	}
	
	jQuery('form#formEnquiry').submit(function() {
		IC.valid = true;
		
		if (jQuery('form#formEnquiry textarea#message').val()=='') {
			jQuery('form#formEnquiry textarea#message').css('background','#FFDFBF').focus();
			IC.valid = false;
		} else {
			jQuery('form#formEnquiry textarea#message').css('background','#D1DBBD');
		}
		
		if (jQuery('form#formEnquiry input#tel').val()!='' && IC.checkTel(jQuery('form#formEnquiry input#tel').val()) == false) {
			jQuery('form#formEnquiry input#tel').css('background','#FFDFBF').focus();
			IC.valid = false;
		} else {
			jQuery('form#formEnquiry input#tel').css('background','#D1DBBD');
		}
		
		if (jQuery('form#formEnquiry input#email').val()=='' || IC.checkEmail(jQuery('form#formEnquiry input#email').val()) == false) {
			jQuery('form#formEnquiry input#email').css('background','#FFDFBF').focus();
			IC.valid = false;
		} else {
			jQuery('form#formEnquiry input#email').css('background','#D1DBBD');
		}
		
		if (jQuery('form#formEnquiry input#name').val()=='') {
			jQuery('form#formEnquiry input#name').css('background','#FFDFBF').focus();
			IC.valid = false;
		} else {
			jQuery('form#formEnquiry input#name').css('background','#D1DBBD');
		}
		
		if (IC.valid == false) {
			return false;
		}
		return true;
	});
	
	jQuery('form#formCallback').submit(function() {
		IC.valid = true;
		
		if (IC.checkTel(jQuery('form#formCallback input#tel').val()) == false) {
			jQuery('form#formCallback input#tel').css('background','#FFDFBF').focus();
			IC.valid = false;
		} else {
			jQuery('form#formCallback input#tel').css('background','#D1DBBD');
		}
		
		if (jQuery('form#formCallback input#name').val()=='') {
			jQuery('form#formCallback input#name').css('background','#FFDFBF').focus();
			IC.valid = false;
		} else {
			jQuery('form#formCallback input#name').css('background','#D1DBBD');
		}
		
		if (IC.valid == false) {
			return false;
		}
		return true;
	});
});
