// JavaScript Document

function Validate()	{
	

	var formErrors = true;
	var nextMove  = false;
	var curr;
	var currLength;
	$('.reqfield',document.forms[0]).each(function(){
												   //curr = $(this).attr("name");
												   //currLength = $(this).val().length;
												   //console.log("%d length: %d", curr, currLength);
		if (!$(this).val().length) {
				var errorDiv = $(this).parent().next();
				$(this).keydown(function() {
					errorDiv.css("display","none");
				});
				errorDiv.css("display","block");
				//alert($(this).next());
				formErrors = false;
		}
	});
	
	//these could be combined
	var rbs = $(".rbgroup").length;
	if ($(".rbgroup").length) {
		var rbgroup = $(".rbgroup");
		if ( ! VAlrb (rbgroup) ) {
			SetUpRBsErrorDiv (rbgroup);
		}
	}
	
	if ($(".rb1").length) {
		var rbgroup1 = $(".rb1");
		if ( ! VAlrb (rbgroup1) ) {
			SetUpRBsErrorDiv (rbgroup1);
		}
	}
	
	if ($(".rb3").length) {
		var rbgroup3 = $(".rb3");
		if ( ! VAlrb (rbgroup3) ) {
			SetUpRBsErrorDiv (rbgroup3);
		}
	}
	
	
	
	
	if ($('#ddPrice').val() == '0') {
	  $('#ddPrice').change(function() {
			$('#ddError').css("display","none");
		});
	  $('#ddError').css("display","block");
		//alert($(this).next());
		formErrors = false;
	}
	
	//var test = formErrors == false?"false":"true";
	//console.log(test);
	return formErrors;
}


function SetUpRBsErrorDiv (rbgroup) {
	//console.log("in setuprb...");
	var errorDiv = $(rbgroup.get(1)).parent().next();
	//console.log(errorDiv);
	rbgroup.change(function(){
		errorDiv.css("display","none");				
	});
	errorDiv.css("display","block");
}

function VAlrb (rbgroup) {
		var checked = false;
		rbgroup.each ( function(){					  
			if ($(this).attr('checked')) {
				//console.log("checked");
				checked = true;
			}
				
		});
		//console.log("returning checked which is ", checked);
		return checked;
}
	


function PrepareForm() {
	
   $("#contactSubmit").click(function(){
   		return (Validate());
 	});
   
 
	   
   //}
 }
 
  $(document).ready(PrepareForm);