$(document).ready(function() {

	$('input[title!=""]').hint();
	$("#mailing").validate({
		rules: {
			name	: "required",
			email	: {	
						required: true,
						email: true
					},
			phone	: "required",
			captcha	: {	
						required: true,
						remote: "http://www.executiveconnections.eu/assets/templates/captcha/process.php"
					}
		},
		messages: {
					name	: "Missing",
					email	: "Missing",
					phone	: "Missing",
					captcha	: "Missing"
				}
	});

	function hide_optional() {
		$("#id-country-other").hide();
		$("#id-town-other").hide();
		$("#jc1-opt").hide();
		$("#jc2-opt").hide();
		$("#jc3-opt").hide();
		$("#jc4-opt").hide();
		$("#jc5-opt").hide();
		$("#jc6-opt").hide();
		$("#jc7-opt").hide();
		$("#jc8-opt").hide();
		$("#jc9-opt").hide();
		$("#jc10-opt").hide();
		$("#jc11-opt").hide();
		$("#jc12-opt").hide();
		$("#jc13-opt").hide();
		$("#jc14-opt").hide();
	}

	$("input[id='jc_1']").click( function() {
		if (  $("input[id='jc_1']:checked").val() != undefined  ) {
  			$("#jc1-opt").toggle();
  		} else {
			$(this).next().next().children(':checkbox').attr("checked", false);
  			$("#jc1-opt").toggle();
  		}
	});

	$("input[id='jc_2']").click( function() {
		if (  $("input[id='jc_2']:checked").val() != undefined  ) {
  			$("#jc2-opt").toggle();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$("#jc2-opt").toggle();
  		}
	});

	$("input[id='jc_3']").click( function() {
		if (  $("input[id='jc_3']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_4']").click( function() {
		if (  $("input[id='jc_4']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_5']").click( function() {
		if (  $("input[id='jc_5']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_6']").click( function() {
		if (  $("input[id='jc_6']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_7']").click( function() {
		if (  $("input[id='jc_7']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_8']").click( function() {
		if (  $("input[id='jc_8']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_9']").click( function() {
		if (  $("input[id='jc_9']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_10']").click( function() {
		if (  $("input[id='jc_10']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_11']").click( function() {
		if (  $("input[id='jc_11']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_12']").click( function() {
		if (  $("input[id='jc_12']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_13']").click( function() {
		if (  $("input[id='jc_13']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});

	$("input[id='jc_14']").click( function() {
		if (  $("input[id='jc_14']:checked").val() != undefined  ) {
  			$(this).next().next().show();
  		} else {
  			$(this).next().next().children(':checkbox').attr("checked", false);
  			$(this).next().next().hide();
  		}
	});


	$("select[id='country']").change( function() {
  			var country = $(this).val()

  			if ( country != 'Cyprus') {
  				$("select[id='town']").val('Other');
  				$("#id-town-other").show();
  			} else if ( country == 'Cyprus') {
  				$("input[id='town_other']").val('');
  				$("#id-town-other").hide();
  				
  			}

  			if (country == 'Other') {
				$("#id-country-other").show();
				$("#id-town-other").show();
  			} else {
  				$("input[id='country_other']").val('');
  				$("#id-country-other").hide();
  			}
	});


	$("select[id='town']").change( function() {
  			var town = $(this).val()
  			if (town == 'Other') {
				$("#id-town-other").show();
  			} else {
  				$("input[id='town_other']").val('');
  				$("#id-town-other").hide();
  			}
	});


	hide_optional();
	$("#reg-form").validate({
		rules: {
			"FormValue_Fields[CustomField99]"	: "required",		// title
			"FormValue_Fields[CustomField69]"	: "required",		// name
			"FormValue_Fields[CustomField70]"	: "required",		// surname
			"FormValue_Fields[EmailAddress]"	: {
													required: true,
													email: true
			},
			"FormValue_Fields[CustomField98]"	: "required",		// country
			"FormValue_Fields[CustomField101]"	: "required",		// town
			"FormValue_Fields[CustomField100]"	: {					// country-other
													required: function(element) {
														return $("#country").val() == 'Other';
													}
			},
			"FormValue_Fields[CustomField102]"	: {					// town-other
													required: function(element) {
														return $("#town").val() == 'Other';
													}
			}
		},
		messages: {
			"FormValue_Fields[CustomField99]"	: "required",		// title
			"FormValue_Fields[CustomField69]"	: "required",		// name
			"FormValue_Fields[CustomField70]"	: "required",		// surname
			"FormValue_Fields[EmailAddress]"	: "required",		// email address
			"FormValue_Fields[CustomField98]"	: "required",		// country
			"FormValue_Fields[CustomField100]"	: "required",		// country-other
			"FormValue_Fields[CustomField101]"	: "required",		// town
			"FormValue_Fields[CustomField102]"	: "required"		// town-other
		},
		submitHandler: function(form) {
			form.submit();
		}
	});

 });
