// ORDER FORM

$(function(){
	
	var subm, file_names; //provjera popunjenih polja
	var br_stranica, br_dana, cijena; //izračun cijene
 	
	//Upload datoteke
	$("#upload_submit").click(function(){return false;});

	$("#submit_form").click(function(){
		
		subm = true;
		
		$("form input.required").each(function(){ 

			if($(this).val()=='')
			{
				$(this).css("background-color", "#f26c4f");
				subm = false;
			}

		})
		
		if(subm == false)
		{
			$(".message_error").html("Alle Felder mit (*) sind erforderlich!");
			return false;
		}
		else
		{
			$(".message_error").html("");
		}
		
	})
	
	$("form input.required").blur(function(){
		
		if($(this).val()!="")
		{
			$(this).css("background-color", "#fff");
		}
		else
		{
			$(this).css("background-color", "#f26c4f");
		}
		
	
	})
	
	
	var name1, email1, company1;
	
	$("#upload_button_id").click(function(){
	
		name1 = $("#name").attr('value');
		email1 = $("#email").val();
	
	})
	
	// You must create it only after the DOM is ready for manipulations
	// Use $(document).ready for jquery
	// document.observe("dom:loaded" for prototype
	//new AjaxUpload('upload_button_id', {action: 'upload.php'});
	
	new AjaxUpload('#upload_button_id', {
	  // Location of the server-side upload script
	  action: 'scripts/upload_de.php',
	  // File upload name
	  name: 'upload',
	  // Additional data to send
	  data: {
		name : name1,
		email : email1,
		company : $("#company").val(),
		address : $("#address").val(),
		phone : $("#phone").val(),
		nr_pages : $("#nr_pages").val(),
		delivery : $("#delivery").val(),
		comments : $("#comments").html(),
		price : $("#price").html()
		//example_key2 : 'example_value2'
	  },
	  // Submit file after selection
	  autoSubmit: true,
	  // Fired after the file is selected
	  // Useful when autoSubmit is disabled
	  // You can return false to cancel upload
	  // @param file basename of uploaded file
	  // @param extension of that file
	  onChange: function(file, extension){},
	  // Fired before the file is uploaded
	  // You can return false to cancel upload
	  // @param file basename of uploaded file
	  // @param extension of that file
	  onSubmit: function(file, extension) {
		  
		$(".file_upload").css("display", "none");
		$("#loading").css("display", "block");
		  
	  },
	  // Fired when file upload is completed
	  // @param file basename of uploaded file
	  // @param response server response
	  onComplete: function(file, response) { 
	  	
		if(response=='error')
		{
			alert("Fehler! Maximale Dateigrosse betragt 36 MB!");
		}
		else
		{
			$("#uploaded_files").css("display", "block");
			$("#uploaded_files").append(file+'<br />');
			
			file_names = $("#files").val();
			
			$("#files").val(file_names + file+'<br />');
			
			$(".message_error").html("");
			
		}
		$("#loading").css("display", "none");
		$(".file_upload").css("display", "block");
		
	  }
	  
	});
	
	
	
	var cijene=new Array();
	cijene[1]=289;
	cijene[2]=249;
	cijene[3]=219;
	cijene[4]=179;
	cijene[5]=159;
	cijene[6]=144;
	cijene[7]=129;
	
	
	$("#price").css("display", "block");
	
	function izracun_cijene()
	{
		
		br_stranica = $("#nr_pages").val();
		br_dana = $("#delivery option:selected").val();
		
		if (br_stranica == 1)
		{
			cijena = cijene[br_dana];
		}
		else
		{
			cijena = (cijene[br_dana]);
			cijena = cijena + (((br_stranica-1) * (cijene[br_dana] * 0.7)));
		}
		$("#price strong").html(cijena.toFixed(0));
		
		//console.log(br_dana);
		//console.log(br_stranica);
		
	}
	
	$("#delivery").change(function(){ 
								 
		izracun_cijene(); 
	
	});
	
	$("#nr_pages").change(function(){ 
								 
		izracun_cijene(); 
	
	});
	
	
	


})
