$(document).ready(function(){
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	var hide = true;
	
	function IsNumeric(strString){
		var strValidChars = "0123456789.-";
		var strChar;
		var blnResult = true;
		
		if (strString.length == 0)
			return false;
		
		for (i = 0; i < strString.length && blnResult == true; i++){
			strChar = strString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1){
				blnResult = false;
			}
		}
		return blnResult;
	}

	
	$("#clicksubmit").click(function(){
		if($('form').attr("id") == 'calculus')
		{
			if($('#calculate_bend_type').val() == '4')
			{
				if($('#calculate_details').val() == '')
				{
					if (language == 'ro')
						$('.notification_text').text('Completati detaliile comenzii');
					else
						$('.notification_text').text('Fill in the details');
						
					$('#notification').slideDown('slow');
					setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
					return false;
				}
			}
			else
			{
				if($('#calculate_length').val() == '')
				{
					if (language == 'ro')
						$('.notification_text').text('Completati lungimea');
					else
						$('.notification_text').text('Fill in the length');
						
					$('#notification').slideDown('slow');
					setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
					return false;
				}
				if($('#calculate_height').val() == '')
				{
					if (language == 'ro')
						$('.notification_text').text('Completati inaltimea');
					else
						$('.notification_text').text('Fill in the height');
						
					$('#notification').slideDown('slow');
					setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
					return false;
				}
			}
		
			if($('#calculate_qty').val() == '')
			{
				if (language == 'ro')
					$('.notification_text').text('Introduceti numarul de bucati');
				else
					$('.notification_text').text('Fill in the number of items');
					
				$('#notification').slideDown('slow');
				setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
				return false;
			}
			if(!IsNumeric($('#calculate_qty').val()))
			{
				if (language == 'ro')
					$('.notification_text').text('Introduceti corect numarul de bucati');
				else
					$('.notification_text').text('Fill in corectly the number of items');
					
				$('#notification').slideDown('slow');
				setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
				return false;
			}
			if($('#calculate_profile_brand').val() == '')
			{
				if (language == 'ro')
					$('.notification_text').text('Alegeti marca profilului');
				else
					$('.notification_text').text('Choose the profile brand');
					
				$('#notification').slideDown('slow');
				setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
				return false;
			}
			if($('#calculate_nbr_chambers').val() == '')
			{
				if (language == 'ro')
					$('.notification_text').text('Alegeti numarul de camere');
				else
					$('.notification_text').text('Choose the bumber of chambers');
				$('#notification').slideDown('slow');
				setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
				return false;
			}
			
		}
		$("form").submit();
		return false;
	});

	$('#bend1').click(function(){
		var clicked = $(this);
		$(clicked).addClass('selected');
		$('#bend2, #bend3, #bend4').removeClass('selected');
		
		$('#bend_img').attr({src: "images/bendings/bend1.gif"});
		$('#calculate_bend_type').val("1");
		
		$('#order_details_box').hide();
		$('#dimensions_box').show();
		$('#results_box').hide();
		$('#small_details_box').hide();
		$('#qty_box').hide();
		$('#profiles_box').hide();
		
		$('#model_picture_box div').show();
		return false;
	});

	$('#bend2').click(function(){
		var clicked = $(this);
		$(clicked).addClass('selected');
		$('#bend1, #bend3, #bend4').removeClass('selected');
		
		$('#bend_img').attr({src: "images/bendings/bend2.gif"});
		$('#calculate_bend_type').val("2");
		
		$('#order_details_box').hide();
		$('#dimensions_box').show();
		$('#results_box').hide();
		$('#small_details_box').hide();
		$('#qty_box').hide();
		$('#profiles_box').hide();
		
		$('#model_picture_box div').show();
		return false;
	});

	$('#bend3').click(function(){
		var clicked = $(this);
		$(clicked).addClass('selected');
		$('#bend1, #bend2, #bend4').removeClass('selected');
		
		$('#bend_img').attr({src: "images/bendings/bend3.gif"});
		$('#calculate_bend_type').val("3");
		
		$('#order_details_box').hide();
		$('#dimensions_box').show();
		$('#results_box').hide();
		$('#small_details_box').hide();
		$('#qty_box').hide();
		$('#profiles_box').hide();
		
		$('#model_picture_box div').show();
		return false;
	});

	$('#bend4').click(function(){
		var clicked = $(this);
		$(clicked).addClass('selected');
		$('#bend1, #bend2, #bend3').removeClass('selected');
		
		$('#bend_img').attr({src: "images/bendings/bend4.gif"});
		$('#calculate_bend_type').val("4");
		
		$('#order_details_box').show();
		$('#dimensions_box').hide();
		$('#results_box').hide();
		$('#small_details_box').hide();
		$('#qty_box').show();
		$('#profiles_box').show();
		
		$('#model_picture_box div').hide();
		return false;
	});
		
	/////////////////////////////////////////////////
		
	if ($('.notification_text').text() != ''){
		$('#notification').slideDown('slow');
		setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
		return false;
	}

	/////////////////////////////////////////////////

	/* var textarea_focus = false;
	$('textarea').focus(function(){
		textarea_focus = true;
	}).blur(function(){
		textarea_focus = false;
	});  */
	
	$("form").keypress(function (e){
		//if (((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) && (textarea_focus == false)){
		
		if (((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) && ($('form').attr("id") != 'calculus')){
			$("form").submit(); 
			return false;
		}
		else{
			return true;  
		}
	});
	
	///////////////////////////////////////////////////
		
	function roundNumber(rnum, rlength) {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		return newnumber;
	}
		
	$("#calculate").click(function(){
		hide = false;
		
		$('#results_box').hide('fast');
		$('#qty_box').hide('fast');
		$('#small_details_box').hide('fast');
		$('#profiles_box').hide('fast');
		
		var L = $('#calculate_length').val();
		var H = $('#calculate_height').val();
		
		if((L == '') || (H == '')){
			
			if (language == 'ro')
				$('.notification_text').text('Completati toate valorile');
			else
				$('.notification_text').text('Fill in both values');
				
			$('#notification').slideDown('slow');
			setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
			return false;
		}
		
		if(!IsNumeric(L))
		{
			if (language == 'ro')
				$('.notification_text').text('Introduceti corect inaltimea');
			else
				$('.notification_text').text('Fill in the height corectly');
				
			$('#notification').slideDown('slow');
			setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
			return false;
		}

		if(!IsNumeric(H))
		{
			if (language == 'ro')
				$('.notification_text').text('Introduceti corect lungimea');
			else
				$('.notification_text').text('Fill in the length corectly');
				
			$('#notification').slideDown('slow');
			setTimeout(function(){ $('#notification').slideUp('slow');}, 4000);
			return false;
		}
		
		// jumatate de cerc
		if($('#calculate_bend_type').val() == '1')
		{
			var R = (H / 2) + (Math.pow(L, 2) / (8 * H));
			R = roundNumber(R, 2);
			$('#show_radius').text(R + " mm");
			$('#calculate_radius').val(R);
			
			var Ld = Math.sqrt(Math.pow(L, 2) + (5.33 * Math.pow(H, 2))) + 600;
			Ld = roundNumber(Ld, 2);
			$('#show_cut_length').text(Ld + " mm");
			$('#calculate_cut_length').val(Ld);
		}
		
		// jumatate de cerc cu parte dreapta
		if($('#calculate_bend_type').val() == '2')
		{
			var R = L / 2;
			R = roundNumber(R, 2);
			$('#show_radius').text(R + " mm");
			$('#calculate_radius').val(R);
			
			var right_side = 2 * (H - R);
			
			var Ld = (R * 3.14159265) + right_side + 200;
			Ld = roundNumber(Ld, 2);
			$('#show_cut_length').text(Ld + " mm");
			$('#calculate_cut_length').val(Ld);
		}
		
		// arc (segment) de cerc
		if($('#calculate_bend_type').val() == '3')
		{
			var R = (H / 2) + (Math.pow(L, 2) / (8 * H));
			R = roundNumber(R, 2);
			$('#show_radius').text(R + " mm");
			$('#calculate_radius').val(R);
			
			var Ld = Math.sqrt(Math.pow(L, 2) + (5.33 * Math.pow(H, 2))) + 600;
			Ld = roundNumber(Ld, 2);
			$('#show_cut_length').text(Ld + " mm");
			$('#calculate_cut_length').val(Ld);
		}
		
		if(hide == false){
			$('#calculate_length').keydown(function(){
				hide = true;

				$('#results_box').fadeOut();
				$('#profiles_box').fadeOut();
				$('#qty_box').fadeOut();
				$('#small_details_box').fadeOut();
			});
			$('#calculate_height').keydown(function(){
				hide = true;

				$('#results_box').fadeOut();
				$('#profiles_box').fadeOut();
				$('#qty_box').fadeOut();
				$('#small_details_box').fadeOut();
			});
		}
		
		$('#results_box').fadeIn('slow');
		$('#small_details_box').fadeIn('slow');
		$('#qty_box').fadeIn('slow');
		$('#profiles_box').fadeIn('slow');
		return false;
	});
})
