﻿
function strTrim(s) {
	while (s.substring(0, 1) == ' ') {
		s = s.substring(1, s.length);
	}
	while (s.substring(s.length - 1, s.length) == ' ') {
		s = s.substring(0, s.length - 1);
	}
	return s;
}

function doHMTBlur(component) {
	
	var hmtVal = component.value;
	if (strTrim(hmtVal) == '') {
		component.value = "Type your own answer";

		return false;
	
	}
	
	return true;

}



function doHMTFocus(component) {
	var hmtVal = component.value;
	if (hmtVal == "Type your own answer") {
		component.value = "";
	}
	document.getElementById('hmt3').checked = true;
	return true;
}

function doHTMclick(component) {
	document.getElementById('hmt3').checked = true;
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
	if (str.indexOf(" ")!=-1){
		return false
	}
	return true					
}

function isValidZipCode(value) {
	var re = /^\d{5}([\-]\d{4})?$/;
	return (re.test(value));
}

function validate_form() {
	if($('#name').val() == '') {
		alert("Please enter your name.");
		return false;
	}
	if($('#email').val() == '') {
		alert("Please enter your email.");
		return false;
	}
	if($('#zip').val() == '') {
		alert("Please enter your zip code.");
		return false;
	}
	if(echeck($('#email').val()) == false) {
		alert("Please enter a valid email address.");
		return false;
	}
	if(isValidZipCode($('#zip').val()) == false) {
		alert("Please enter a valid zip code.");
		return false;
	}
	if($('#children').val() == '0') {
		alert("Please select your number of children.");
		return false;
	}
	return true;
}

function validate_form2() {
	if($('#firstname').val() == '') {
		alert("Please enter your first name.");
		return false;
	}
	if($('#lastname').val() == '') {
		alert("Please enter your last name.");
		return false;
	}
	if($('#email').val() == '') {
		alert("Please enter your email.");
		return false;
	}
	if(echeck($('#email').val()) == false) {
		alert("Please enter a valid email address.");
		return false;
	}
	if($('#address').val() == '') {
		alert("Please enter your address.");
		return false;
	}
	if($('#city').val() == '') {
		alert("Please enter your city.");
		return false;
	}
	if($('#state').val() == '') {
		alert("Please select your state.");
		return false;
	}
	if($('#phone').val() == '') {
		alert("Please enter your phone number.");
		return false;
	}
	if($('#whyrecipe').val() == '') {
		alert("Please tell us a little bit about yourself and why you think your recipe should win.");
		return false;
	}
	if($('#title').val() == '') {
		alert("Please enter your recipe name/title.");
		return false;
	}
	if($('#instructions').val() == '') {
		alert("Please enter your recipe instructions.");
		return false;
	}
	if(!$('#agree').is(':checked')) {
		alert("You must read and agree to the Official Rules*.");
		return false;
	}
	return true;
}

$(document).ready(function(){
	$("#tableaddmore").live('click', function() {
		//var c = $("#tablerecipe tr").length;
		$("#tableaddmore").remove();
		$("#tablerecipe").append('<tr><td><input type="text" name="measure[]" size="15" /></td><td><input type="text" name="ingredient[]" size="40" /> <a href="#" id="tableaddmore" onclick="return false;">Add More</a></td></tr>'); 
		return false;
	});
});

function validate_form3() {
	if($('#name').val() == '') {
		alert("Please enter your name.");
		return false;
	}
	if($('#email').val() == '') {
		alert("Please enter your email.");
		return false;
	}
	if(echeck($('#email').val()) == false) {
		alert("Please enter a valid email address.");
		return false;
	}
	if($('#state').val() == '') {
		alert("Please select your state.");
		return false;
	}
	if($('#phone').val() == '') {
		alert("Please enter your phone number.");
		return false;
	}
	return true;
}
