
function form_check() {

	var check_alert = false;


		//  name (exists)

			if ($F('tbName') == "") {
				failed_Test('tbName');
				check_alert = true;
			} else {
				reset_Test('tbName');
			}

		//  contact details (exists)



			if (($F('tbTel') == "") && ($F('tbEmail') == "") ) {
				failed_Test('tbTel');
                failed_Test('tbEmail');
				check_alert = true;
			} else {
				reset_Test('tbTel');
                if ($F('tbEmail') == "") {
    				reset_Test('tbEmail');
    			}
			}

		//  valid email format

			if (($F('tbEmail') != "")&& ( !isValidEmail($F('tbEmail'))) ) {
				failed_Test('tbEmail','Please check your email address, it has an error');
				check_alert = true;
			} else {
				reset_Test('tbEmail');
			}


		//  message (exists)

			if ($F('tbMsg') == "") {
				failed_Test('tbMsg');
				check_alert = true;
			} else {
				reset_Test('tbMsg');
			}

		if (check_alert === false) {
			$('EnquiryForm').submit();
			reset_Test('SendButton');
		} else {
			failed_Test('SendButton','You haven`t given us enough information, please try again..');
		}


}

function form_reset() {
		// Reset all

			reset_Test('tbName');
			reset_Test('tbTel');
            reset_Test('tbEmail');
            reset_Test('tbMsg');
            reset_Test('SendButton');
}

function reset_Test(id) {

	// Clear error message.

	if (Element.visible(id+'_warning')) {
		new Effect.BlindUp(id+'_warning', { afterFinish: function (x) {$(id+'_warning').innerHTML = "";} });
	}

}
function failed_Test(id,message) {

	var message

	if (message == undefined && $(id).title != undefined) {
		message = $(id).title;
	} else if (message == undefined && $('id').title == undefined) {
		message = "Please complete this information.";
	}

	// Applies error and applies error message.

	$(id+'_warning').innerHTML = message;
	if (Element.visible(id+'_warning') === false) {
		new Effect.BlindDown(id+'_warning');
	} else {
		new Effect.Highlight(id+'_warning', {duration:5.0});
	}

}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
