
$(document).ready(function() {
	
	$('#step2 select.opensides').change(function() {
		var data = $(this).attr('id').split('_');
		var id = data[1];
		var open_side = $(this).val();
		var table = $('#window_'+id);
		var widths_table = $(table).next();
		
		var ii = 1
		$('.opensides_window_' + id + ' div').each(function () {
			if (ii <= open_side) {
				$(this).fadeIn();
			}
			if (ii > open_side) {
				$(this).fadeOut();
			}
			ii++;
		})		
		
		$('.window_'+ id + '_opensides_count').val(open_side);
		
		$(table).hide();
		$(widths_table).hide();
		
		var tds = widths_tds = value = false;
		
		$(table).children().remove();
		$(widths_table).children().remove();
		
		if (open_side == 2) {
			$(table).width(120);
			$(widths_table).width(120);
		}
		if (open_side == 3) {
			$(table).width(140);
			$(widths_table).width(140);
		}
		if (open_side == 4) {
			$(table).width(160);
			$(widths_table).width(160);
		}		

		if (open_side == 1) {
			$(table).width(100);
			tds = '<tr><td valign="top">&nbsp;</td></tr>';
		} else {
			tds = widths_tds =  '<tr>';
			for (i=0;i<open_side;i++) {
				tds = tds + '<td valign="top" style="width:' + ($(table).width()/open_side) + 'px">&nbsp;</td>';
				this_input = '<input type="text" id="win_sides_' + id + '_' + i + '" class="small" name="Window[' + id + '][OpenSide][' + i + '][Width]" value=""/>';
				widths_tds = widths_tds + '<td>' + this_input + '<br />mm</td>';
			}
			tds = tds + '</tr>';
			widths_tds = widths_tds  + '</tr>';
		}
		
		
		if (tds) {
			$(table).append(tds).fadeIn();
		}
		if (widths_tds) {
			$(widths_table).append(widths_tds).fadeIn();
		}
		
		for (i=0;i<open_side;i++) {
				this_input = '<input type="text" id="win_sides_' + id + '_' + i + '" class="small" name="Window[' + id + '][OpenSide][' + i + '][Width]" value=""/>';
				$.getJSON('/home/request/_get_windows_widths.php', {id:id, i:i}, function(data) { 
					if (data) {
						$('#win_sides_' + data.id + '_' + data.i + '' ).val(data.value);
					}
				} );
			}
		
		$('#step2 .window_openside').change();
		
	})
	
	$('#step1 div.submit').click(function () {
		var accept = true;
		if (($('#window_summ').val() == "" || $('#window_summ').val() == "0") && ($('#door_summ').val() == "" || $('#door_summ').val() == "0")) {
			$('#window_summ').parent().addClass('alert');
			$('#door_summ').parent().addClass('alert');
			accept = false;
		} 
		
		if (accept) {
			 $('#step1').submit();
		}
	})
	
	$('#step2 div.submit').click(function () { 
		var accept = true;
		
		$('div.required input').each(function () {
			if ($(this).val() == "") {
				accept = false;
				$(this).parent().addClass('alert');
			} else {
				$(this).parent().removeClass('alert');
			}
		})
		
		if (accept) {
			$('#step2').submit();
		}
	})
	
	
	$('#step2 .openside input:text').click(function () {
		return false;
	})
	
	$('#step2 .door_openside').change(function() {
		var door_id = $(this).prev().attr('name');
		var variant = $(this).val();
		table = $('#'+door_id);
		td = $(table).children().children().children('td:first');
		$(td).empty();
		
		change_openside(variant, $(td), $(td).width(), $(table).height());
		
	})
	
	function change_openside(variant, elem, width, height) {
		$(elem).empty();
		
		if (variant == 1) {
			$(elem).append('<img src="/home/images/request/left.gif" style="width:'+width+'px;height:'+height+'px" />');
		}		
		if (variant == 2) {
			$(elem).append('<img src="/home/images/request/right.gif" style="width:'+width+'px;height:'+height+'px" />');
		}		
		if (variant == 3) {
			$(elem).append('<img src="/home/images/request/top.gif" style="width:'+width+'px;height:'+height+'px" />');
		}	
		if (variant == 4) {
			$(elem).append('<img src="/home/images/request/left.gif" style="width:'+width+'px;height:'+height+'px" />');
			$(elem).append('<img src="/home/images/request/top.gif" style="width:'+width+'px;height:'+height+'px" />');
		}	
		if (variant == 5) {
			$(elem).append('<img src="/home/images/request/right.gif" style="width:'+width+'px;height:'+height+'px" />');
			$(elem).append('<img src="/home/images/request/top.gif" style="width:'+width+'px;height:'+height+'px" />');
		}
	}
	
	
	$('#step2 .window_openside').change(function() {
		
		data = $(this).attr('id').split('_');
		var id = data[1];
		var td_count = data[2];

		variant = $(this).val();
		
		td = $('#window_'+id).children().children().children().eq(td_count);
		
		change_openside(variant, td, td.width(), td.height());	
		
	})

	$('.openside').click(function () {
		var data = $(this).attr('id').split('_');
		var id = data[1];
		var open_side = data[2];
		var table = $('#window_'+id);
		var widths_table = $(table).next();
		
		var ii = 1
		$('.opensides_window_' + id + ' div').each(function () {
			if (ii == open_side) {
				$(this).removeClass('optional').removeClass('optional').addClass('required');
			} else {
				$(this).removeClass('required').removeClass('required').addClass('optional');
			}
			if (ii <= open_side) {
				$(this).children('').fadeIn();
				$(this).children('select, span').fadeIn();
			}
			if (ii > open_side) {
				$(this).children('select, span').fadeOut();
			}
			ii++;
		})		
		
		$('.window_'+ id + '_opensides_count').val(open_side);
		
		$(table).hide();
		$(widths_table).hide();
		
		var tds = widths_tds = false;
		
		$(table).children().remove();
		$(widths_table).children().remove();
		
		if (open_side == 2) {
			$(table).width(120);
			$(widths_table).width(120);
		}
		if (open_side == 3) {
			$(table).width(140);
			$(widths_table).width(140);
		}
		if (open_side == 4) {
			$(table).width(160);
			$(widths_table).width(160);
		}		

		if (open_side == 1) {
			$(table).width(100);
			tds = '<tr><td valign="top">&nbsp;</td></tr>';
		} else {
			tds = widths_tds =  '<tr>';
			for (i=0;i<open_side;i++) {
				tds = tds + '<td valign="top" style="width:' + ($(table).width()/open_side) + 'px">&nbsp;</td>';
				widths_tds = widths_tds + '<td><input type="text"  class="small" name="Window[' + id + '][OpenSide][' + i + '][Width]" /><br />mm</td>';
			}
			tds = tds + '</tr>';
			widths_tds = widths_tds  + '</tr>';
		}
		
		$('.window_openside').change();
		
		if (tds) {
			$(table).append(tds).fadeIn();
		}
		if (widths_tds) {
			$(widths_table).append(widths_tds).fadeIn();
		}


	})


	$('.panel_on').click(function () {
		var table_id = $(this).attr('name');
		table = $('#'+table_id);
		$(table).hide();
		extra = $('#'+table_id+' td').html();
		$('#'+table_id+' tr').remove();
		if ($(this).val() == 0) {
			$(table).append('<tr><td valign="top">' + extra + '&nbsp;</td></tr>');
		} else {
			$(table).append('<tr><td valign="top" style="height:100px">' + extra + '&nbsp;</td></tr><tr><td style="background:#ccc;height:40px">&nbsp;</td></tr>');
		}		
		$('#'+table_id).fadeIn();
	})


	$('#submit').click(function() {

		var accept = true;
		if ($('#contactForm_name').val() == "") {
			$('#contactForm_name').parent().addClass('alert');
			accept = false;
		} else {
				$('#contactForm_name').parent().removeClass('alert');
		}
//		if ($('#contactForm_address').val() == "") {
//			$('#contactForm_address').parent().addClass('alert');
//			accept = false;
//		} else {
//			$('#contactForm_address').parent().removeClass('alert');
//		}
		var email_check = $('#contactForm_email').val();
		if ($('#contactForm_email').val() != "" && email_check.match(new RegExp('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]{2,})+$'))) {
			$('#contactForm_email').parent().removeClass('alert');
		} else {
			$('#contactForm_email').parent().addClass('alert');
			accept = false;

		}
		var tel_check = $('#contactForm_telefon').val();
		if ($('#contactForm_telefon').val() != "" && tel_check.match(new RegExp('^[0-9\-\+\\s\(\)]{7,}$'))) {
			$('#contactForm_telefon').parent().removeClass('alert');
		} else {
			$('#contactForm_telefon').parent().addClass('alert');
			accept = false;
		}

		if (accept) {
			 $('#step3').submit();
		}
		return false;

	})
	
	$('#step2 select.opensides').change();
	$('.door_openside').change();
	
	$('.panel_on:checked').click();


});