$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".reset_button").click(function(){
	$('.error').hide();
	$("input#name").select().focus();
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
		
	  	var name = $("input#name").val();
		if (name == "") {
      		$("label#name_error").show();
      		$("input#name").focus();
      		return false;
    	}
		
		var address = $("input#address").val();
		if (address == "") {
      		$("label#address_error").show();
      		$("input#address").focus();
      		return false;
    	}
		
		var city = $("input#city").val();
		if (city == "") {
      		$("label#city_error").show();
      		$("input#city").focus();
      		return false;
    	}
		
		var state = $("input#state").val();
		if (state == "") {
      		$("label#state_error").show();
      		$("input#state").focus();
      		return false;
    	}
		
		var zip = $("input#zip").val();
		if (zip == "") {
      		$("label#zip_error").show();
      		$("input#zip").focus();
      		return false;
    	}
		
		var dob = $("input#dob").val();
		if (dob == "") {
      		$("label#dob_error").show();
      		$("input#dob").focus();
      		return false;
    	}
		
		var email = $("input#email").val();
		if (email == "") {
      		$("label#email_error").show();
      		$("input#email").focus();
      		return false;
    	} else {
			reEmail = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
			if (!reEmail.test(email)){
				$("label#email_error").show();
      			$("input#email").focus();
      			return false;
			}
		}
			
		var homephone = $("input#homephone").val();
		if (homephone == "") {
      		$("label#homephone_error").show();
      		$("input#homephone").focus();
      		return false;
    	}
		
		var cellphone = $("input#cellphone").val();
		//rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
			//if (!rePhoneNumber.test(cellphone)){
				//$("label#cellphone_error").show();
      			//$("input#cellphone").focus();
      			//return false;
			//}
		//}
		
		var parents = $("input#parents").val();
		if (parents == "") {
      		$("label#parents_error").show();
      		$("input#parents").focus();
      		return false;
    	}
		
		var parents_email = $("input#parents_email").val();
		if (parents_email == "") {
      		$("label#parents_email_error").show();
      		$("input#parents_email").focus();
      		return false;
    	} else {
			reEmail = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
			if (!reEmail.test(parents_email)){
				$("label#parents_email_error").show();
      			$("input#parents_email").focus();
      			return false;
			}
		}
		
		var instrument = $("input#instrument").val();
		if (instrument == "") {
      		$("label#instrument_error").show();
      		$("input#instrument").focus();
      		return false;
    	}
		
		var years_studied = $("input#years_studied").val();
		if (years_studied == "") {
      		$("label#years_studied_error").show();
      		$("input#years_studied").focus();
      		return false;
    	}
		
		var pieces_studied = $("textarea#pieces_studied").val();
		if (pieces_studied == "") {
      		$("label#pieces_studied_error").show();
      		$("textarea#pieces_studied").focus();
      		return false;
    	}
		
		var orchestra = $("textarea#orchestra").val();
		if (orchestra == "") {
      		$("textarea#orchestra_error").show();
      		$("input#orchestra").focus();
      		return false;
    	}
		
		var pyears_studied = $("input#pyears_studied").val();
		if (pyears_studied == "") {
      		$("label#pyears_studied_error").show();
      		$("input#pyears_studied").focus();
      		return false;
    	}
		
		var teacher = $("input#teacher").val();
		if (teacher == "") {
      		$("label#teacher_error").show();
      		$("input#teacher").focus();
      		return false;
    	}
		
		var teacher_email = $("input#teacher_email").val();
		if (teacher_email == "") {
      		$("label#teacher_email_error").show();
      		$("input#teacher_email").focus();
      		return false;
    	} else {
			reEmail = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
			if (!reEmail.test(teacher_email)){
				$("label#teacher_email_error").show();
      			$("input#teacher_email").focus();
      			return false;
			}
		}
		
		var teacher_phone = $("input#teacher_phone").val();
		if (teacher_phone == "") {
      		$("label#teacher_phone_error").show();
      		$("input#teacher_phone").focus();
      		return false;
    	} //else {
			//rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
			//if (!rePhoneNumber.test(teacher_phone)){
				//$("label#teacher_phone_error").show();
      			//$("input#teacher_phone").focus();
      			//return false;
			//}
		//}
		
		var solo = $("input[@name='solo']:checked").val();
		
		var location = $("input#location").val();
		
		var dataString = 'name='+ name + '&address=' + address + '&city=' + city + '&state=' + state + '&zip=' + zip + '&dob=' + dob + '&email=' + email + '&homephone=' + homephone + '&cellphone=' + cellphone + '&parents=' + parents + '&parents_email=' + parents_email + '&instrument=' + instrument + '&years_studied=' + years_studied + '&pieces_studied=' + pieces_studied + '&orchestra=' + orchestra + '&pyears_studied=' + pyears_studied + '&teacher=' + teacher + '&teacher_email=' + teacher_email + '&teacher_phone=' + teacher_phone + '&solo=' + solo + '&location=' + location;
		
		//alert ('location = ' + location);//return false;
		
		$.ajax({
      type: "POST",
      url: "bin/process.php",
      data: dataString,
      success: function() {
        $('#app_form').html("<div id='message'></div>");
        $('#message').html("<h2>Application Submitted!</h2>")
        .append("<p>We will be in touch soon. If you do not hear from the CREDO office within 3 business days, please contact us at office@credochambermusic.org</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='images/check.png' />");
        });
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});