$(document).ready(function() {
	var checkSquareFlag = 0;//для индикации выбора цвета. Если цвет выбран, то необходимо проверить наличие площади, шаг4.
	$("td.menu div.header").click(function() {
		$(this).next("ul").slideToggle("slow");
		$(this).toggleClass("minus");
		return false;
	});
	$("ul.menu").find("li.plus").children("a").click(function() {
		$(this).next("ul").slideToggle("fast");
		return false;
	});
	
	$("div.choose_pantone div").click(function() {
		if (this.style.backgroundImage == "" || this.style.backgroundImage == "none") {
			$(this).parent().children("div").css({backgroundImage:"none"});
			$(this).css({backgroundImage:"url(/images/pantone_pointer.gif)"});
			var divArray = new Array();
			for (var i = 0; i < this.parentNode.childNodes.length; i++) {
				if (this.parentNode.childNodes[i].tagName == "DIV") {
					divArray.push(this.parentNode.childNodes[i]);
				}
			}
			for (var i = 0; i < divArray.length; i++) {
				if (divArray[i] == this) {
					var thisPantoneNumber = i;
				}
			}
			$(this).parents(".pantone").children(".text").html(pantonesArray[thisPantoneNumber]);
			$(this).parents(".pantone").children("input").attr({value:$(this).attr("class")});
			$(this).parents("td").children("div.own_pantone").find("input").attr({checked:""});
			$(this).parents("td").children("div.fill").css({visibility:"visible"});
			checkSquareFlag = 1;
		}
		else {
			$(this).parent().children("div").css({backgroundImage:"none"});
			$(this).css({backgroundImage:"none"});
			$(this).parents(".pantone").children(".text").html("");
			$(this).parents(".pantone").children("input").attr({value:""});
			$(this).parents("td").children("div.fill").css({visibility:"hidden"}).find("input").attr({value:""});
			checkSquareFlag = 0;
		}
	});
	$("input[name*='other_pantone']").click(function() {
		if($(this).attr("checked") == "checked" || $(this).attr("checked") == true) {
			var choosePantoneDiv = $(this).parents("td").find("div.choose_pantone");
			$(choosePantoneDiv).children("div").css({backgroundImage:"none"});
			$(choosePantoneDiv).parent().children(".text").html("");
			$(choosePantoneDiv).parent().children("input").attr({value:""});
			$(this).parents("td").children("div.fill").css({visibility:"visible"});
			checkSquareFlag = 1;
		}
		else {
			$(this).parents("td").children("div.fill").css({visibility:"hidden"}).find("input").attr({value:""});
			checkSquareFlag = 0;
		}
	});
	$("#input_image_step4").click(function() {
		if(checkSquareFlag == 1) {
			var flag = 0;
			$("div.step4").find("input[name*='square']").each(function() {
				if($(this).attr("value") == "") {
					if($(this).parents("td").find("input[type='hidden']").attr("value") != "") {
						$(this).parents("div.form_field").children("h5").addClass("attention");
						flag = 1;
					}
					else {
						var checkBoxElement = $(this).parents("td").find("input[type='checkbox']");
						if($(checkBoxElement).attr("checked") == "checked" || $(checkBoxElement).attr("checked") == true) {
							$(checkBoxElement).parents("div.form_field").children("h5").addClass("attention");
							flag = 1;
						}
					}
				}
				else {
					$(this).parents("div.form_field").children("h5").removeClass("attention");
				}
			});
			if (flag == 1) {return false;
			}
		}
	});
	
	$("div.t").click(function() {
		$("table.order_variant tr td.act").removeClass("act");
		$(this).parent("td").addClass("act");
		$("input[name='variantID']").attr({value:$(this).parent("td").attr("id")});
	});
	$("#box_length").keyup(function() {
		checkNumberField(this, 4);
		$("#l_w_h li.length span").text($(this).attr("value"));
	});
	$("#box_width").keyup(function() {
		checkNumberField(this, 4);
		$("#l_w_h li.width span").text($(this).attr("value"));
	});
	$("#box_height").keyup(function() {
		checkNumberField(this, 4);
		$("#l_w_h li.height span").text($(this).attr("value"));
	});
	$("#cover_h").keyup(function() {
		checkNumberField(this, 4);
		$(this).parents("td").children("div.img").children("div.sign").children("span").text($(this).attr("value"));
	});
	$("#valve_size").keyup(function() {
		checkNumberField(this, 4);
		$(this).parents("td").children("div.img").children("div.sign").children("span").text($(this).attr("value"));
	});
	$("#wall_h").keyup(function() {
		checkNumberField(this, 4);
		$(this).parents("td").children("div.img").children("div.sign").children("span").text($(this).attr("value"));
	});
	//change event
	$("#box_length").change(function() {
		checkNumberField(this, 4);
		$("#l_w_h li.length span").text($(this).attr("value"));
	});
	$("#box_width").change(function() {
		checkNumberField(this, 4);
		$("#l_w_h li.width span").text($(this).attr("value"));
	});
	$("#box_height").change(function() {
		checkNumberField(this, 4);
		$("#l_w_h li.height span").text($(this).attr("value"));
	});
	$("#cover_h").change(function() {
		checkNumberField(this, 4);
		$(this).parents("td").children("div.img").children("div.sign").children("span").text($(this).attr("value"));
	});
	$("#valve_size").change(function() {
		checkNumberField(this, 4);
		$(this).parents("td").children("div.img").children("div.sign").children("span").text($(this).attr("value"));
	});
	$("#wall_h").change(function() {
		checkNumberField(this, 4);
		$(this).parents("td").children("div.img").children("div.sign").children("span").text($(this).attr("value"));
	});
	$("#countProduct").change(function() {
		checkNumberField(this, 10);
		$("#quantity span").text($(this).attr("value"));
	});
	
	
	$("#number_of").keyup(function() {
		checkNumberField(this, 10);
	});
	$("input[name*='square']").keyup(function() {
		checkNumberField(this, 10);
	});
	
	if (document.forms["order_box"]) {
		step3Form();
		var form = document.forms["order_box"];
		$(":input[type='text']").each(function() {
			$(this).bind("keyup", function() {
				step3Form();
			});
			$(this).bind("change", function() {
				step3Form();
			});
		});
	}
});

function step3Form() {
	var flag = 1;
	$(":input[class='text']").each(function() {
		if($(this).attr("value") == "") {
			flag = 0;
		}
	});
	if (flag == 0) {
		$("#input_image_step3").css({display:"none"});
	}
	else {
		$("#input_image_step3").css({display:"inline"});
	}
}

function q3_onclick()
{
//document.getElementById('q2').style.visibility='visible';
document.getElementById('q2').style.display='';
}

function sixTransform(number) {
	var digits = new Array;
	for (var i = 0; i < number.length; i++) {
		switch (number.slice(i, i+1)) {
			case "a": var digit = 10; break;
			case "b": var digit = 11; break;
			case "c": var digit = 12; break;
			case "d": var digit = 13; break;
			case "e": var digit = 14; break;
			case "f": var digit = 15; break;
			default:  var digit = number.slice(i, i+1);
		}
		digits.push(digit);
	}
	digits.reverse();
	var summ = 0;
	for (var i = 0; i < digits.length; i++) {
		summ += digits[i]*(Math.pow(16,i));
	}
	return summ;
}
function checkNumberField (input, number) {//проверяем чтобы в поле вводились только цифры
	var inputValue = input.value;
	if (inputValue != "") {
		var numberRegex = /([\d/])$/i;
		if(!inputValue.match(numberRegex) || inputValue.length > number){
			input.value = inputValue.slice(0,inputValue.length-1);
			checkNumberField(input);
		}
	}
}