/* ================================ +
	    Mice Meets Metropolis
 + ================================ */



/* 
** Made by: Thomas Lievestro
** Email:	thomas@studioparkers.nl
** Website:	www.studioparkers.nl
*/


/* ================================ +
              Variables
 + ================================ */

/* Custom to check Array from input with jQuery Validator*/
 $(document).ready(function(){
 		
	jQuery.validator.messages.required = "Yes";
	jQuery.validator.messages.email = "";

	validator = $("#contact-form").validate({

		invalidHandler: function(e, validator) {
		
			var errors = validator.numberOfInvalids();
			if (errors) {
			
				var message = errors //== 1
					//? 'U heeft 1 veld niet (correct) ingevuld.'
					//: 'U heeft ' + errors + ' velden niet (correct) ingevuld.';
				//$("div#error span").html(message);
				//$("div#error_container").show();	
					
			} else {
				//$("div#error_container").hide();	
			}
			

		},
		
		onkeyup: false,
		submitHandler: function() {

    var error = false;
     
    /* Extra check for reqesut for information */
     if($('[name=selectedproducts]').val() == 1) {
	      /* When a productgroup is checked, automatically check the Selected products option */
	      if ($('[name=group[0]]').attr('checked') ||
	          $('[name=group[1]]').attr('checked') ||
	          $('[name=group[2]]').attr('checked') ||
	          $('[name=group[3]]').attr('checked') ||
	          $('[name=group[4]]').attr('checked') ||
	          $('[name=group[5]]').attr('checked') ||
	          $('[name=group[6]]').attr('checked') 
	        ) 
	      { 
	        $('[name=selectedproducts]').attr('checked','true');
	      }
	
	
	     /*  if nothing is checked, give a message*/
	     if(($('[name=selectedproducts]:checked').val() != "1") && ($('[name=catalog]:checked').val()!= "1")) {
	       var error = true;
	      }
	      
	     /* When Selected product is checked, also a product group should be checked */
	      if (
	          $('[name=selectedproducts]').attr('checked') &&
	          !$('[name=group[0]]').attr('checked') &&
	          !$('[name=group[1]]').attr('checked') &&
	          !$('[name=group[2]]').attr('checked') &&
	          !$('[name=group[3]]').attr('checked') &&
	          !$('[name=group[4]]').attr('checked') &&
	          !$('[name=group[5]]').attr('checked') &&
	          !$('[name=group[6]]').attr('checked') 
	        ) 
	      { 
	        var error = true;
	      }
      }
      
      if(error) {
      alert('- Bitte wahlen Sie eine oder mehrere Produktgruppen aus\n- Please choose the product groups you are interested in.\n- Veuillez indiquer nom de votre enterprise\n');
      
      } else {
        
			//SEND it! Post ajax call
			$.post("ajax/saveForm.php", { 
			
				action: 			$('#formtype').val(), 
				firstname: 		$('#firstname').val(), 
				lastname:     $('#lastname').val(), 
				firm: 				$('#firm').val(),
				street: 			$('#street').val(), 
				city: 				$('#city').val(), 
				country: 			$('#country').val(),
				tel: 				$('#tel').val(),  
				email: 				$('#email').val(),  
				question: 			$('#question').val(), 
				optin: 		$('input:radio[name=keepmeposted]:checked').val(),
				keepmeposted:    $('input:checkbox[name=keepmeposted]:checked').val(),
				group: 				$('input:checkbox[name=group[0]]:checked').val()+","
									+$('input:checkbox[name=group[1]]:checked').val()+","
									+$('input:checkbox[name=group[2]]:checked').val()+","
									+$('input:checkbox[name=group[3]]:checked').val()+","
									+$('input:checkbox[name=group[4]]:checked').val()+","
									+$('input:checkbox[name=group[5]]:checked').val()+","
									+$('input:checkbox[name=group[6]]:checked').val()+","
									+$('input:checkbox[name=group[7]]:checked').val(),
				selectedproducts: 	$('input:checkbox[name=selectedproducts]:checked').val(),
				catalog: 			$('input:checkbox[name=catalog]:checked').val(),
				mediatype:			$('input:radio[name=mediatype]:checked').val()
		

				},
				function(data){
				
				if(data == "ok") {
					$("div#error").hide();
					$("#contact-form").hide();
					$(".return-message").show();
				} else {
					$("div#error").html("<b>E-mail error</b>, please contact Galltec-Mela: +49 7457 945321 ");
					$("div#error").show();	
				}
						
			});
      }
		},
		debug:false
	});

});

