$(document).ready(function() {
   // do stuff when DOM is ready	
	
	validateProductEntry("amount_vitaepro");
	validateProductEntry("amount_biopro");
	validateProductEntry("amount_omegapro");
	validateProductEntry("amount_multipro");
	
	
	
	$("#orderform").submit(function(){
		
		var order_error_text1 = "Skjemaet er ufullstendig";
		var order_error_text2 = "Vennligst sjekk at felter med * er riktige, at antall produkter er valgt, og at du krysset for salgs- og leveringsvilkor.";
		//Unique errormessage if no products are chosen 
		var order_error_text3 = "Vennligst velg et produkt.";
		var order_error_text_email="Ugyldig epost format";
		var noEmail=false;
		// Collect Form Values:
		var email = $("#email").val();
		
		var birth = $("#birth").val();
		
			//Variable SubmitForm is used to check if the formm has errors, must remain at all times.
		var submitForm = true;
			//Variable NoProduct is set to true when a user hasen't choosen a product
		var noProduct = false;
		try{
			var conditions = $("#conditions");
			
			$("#email").filter(function() {
				var myregexp = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/;
				var valid = this.value.match(myregexp);
				if(this.value!="" && !valid){
					noEmail=true;
					submitForm=false;
				}
			});
			
			
			if(conditions.attr("checked")){
				var name = $("#name").val();
				var address = $("#address").val();
				if(name != "" && address != ""){
					var zip = $("#zip").val();
					var city = $("#city").val();
					if(zip != "" && city != ""){
						//Getting values from the document form.
							//Remove the line with corresponding productname if that product ain't available
							var vitaepro = $("#amount_vitaepro").val();
							var omegapro = $("#amount_omegapro").val();
							var biopro = $("#amount_biopro").val();
							var multipro = $("#amount_multipro").val();
							if(vitaepro != "" || omegapro != "" || biopro != "" || multipro != "" ){
							}
							else{
								submitForm = false;
								noProduct = true;//end else
							}
							
					}
					else{
						submitForm = false;
					}
				}
				else{
					submitForm = false;
				}//end else
			}// end if(Conditions.checked)
			else{
				submitForm = false;
			}//end else
		}//end Try
		catch(err){	}//end catch
			if(!submitForm){
				var errorTextElem = $("#formErrorText");
				errorTextElem.show();
				//reset html
				errorTextElem.html("");
				errorTextElem.append("<strong>" + order_error_text1 + "</strong><br /><br/>" + order_error_text2);
				if(noEmail){
					errorTextElem.append("<br/>"+order_error_text_email);
				}
				if(noProduct){
					errorTextElem.append("<br /><strong>" + order_error_text3 + "</strong>");
				}//end if(NoProduct)
				$('html').animate({scrollTop:0}, 'fast');
				return false;
			}//end if(!submitForm)
		
	});
	

	
 });

	
 function validateProductEntry(input){
	 $("#"+input).blur(function(){
			if(this.value!=""){
				if((this.value < 1) || (this.value >6)){
					$("#p_"+input).remove();
					this.value="";
					var id = "p_"+this.id;
					$("#"+input).parent().append("<p class=\"warning\" id=\""+id+"\">Tallet må være mellom 1 og 6</p>");
				}
				else{
					$("#p_"+input).remove(); 
				}
			}
			
		});
		
 }
 
 
	 
			 
 
