jQuery.noConflict();

jQuery(document).ready(function($){

        /* search */
        $("div.actual > form.search_form > a.submit").click(
			function(event){
				event.preventDefault();
				$(this).blur();
				$("form.search_form").submit();
			}
		);

        /* subscribe */
		$("div.subs > span.mails > a").click(
			function(event){
				event.preventDefault();
				$(this).blur();
                if($("div.subs > form").css('display') == 'block'){
                   $("div.subs > form").slideUp('slow');
                }else{
                   $("div.subs > form").slideDown('slow');
                }
			}
		);
        $("div.subs > form.subscribe_form > a.submit").click(
			function(event){
				event.preventDefault();
				$(this).blur();
				$("form.subscribe_form").submit();
			}
		);

        /* order form 	*/
		$("a#step2").click(
            function(event){
				event.preventDefault();
				$(this).blur();
				var msg = "Пожалуйста, заполните обязательные поля:";
				var empty_index = -1;
				$("form.order_form").find("input.mandatory").each(
					function(index){
						if($(this).attr("value") == "") {
							msg = msg + "\r\n * " + $(this).attr("alt");
							if(empty_index < 0) {
								empty_index = index;
							}
						}
					}
				);
                $("form.order_form").find("textarea.mandatory").each(
					function(index){
						if($(this).attr("value") == "") {
							msg = msg + "\r\n * " + $(this).attr("alt");
							if(empty_index < 0) {
								empty_index = index;
							}
						}
					}
				);
				if(empty_index > -1) {
					alert (msg);
					$("form.order_form").find("input.mandatory").get(empty_index).focus();
					return false;
				}
				$("form.order_form").submit();
			}
		);

        $("a#step3").click(
            function(event){
				event.preventDefault();
				$(this).blur();
				$("form.pay_form").submit();
			}
		);

		$("a#step5").click(
            function(event){
				event.preventDefault();
				$(this).blur();
				var msg = "Пожалуйста, заполните обязательные поля:";
				var empty_index = -1;
				$("form.order_form").find("input.mandatory").each(
					function(index){
						if($(this).attr("value") == "") {
							msg = msg + "\r\n * " + $(this).attr("alt");
							if(empty_index < 0) {
								empty_index = index;
							}
						}
					}
				);
                $("form.order_form").find("textarea.mandatory").each(
					function(index){
						if($(this).attr("value") == "") {
							msg = msg + "\r\n * " + $(this).attr("alt");
							if(empty_index < 0) {
								empty_index = index;
							}
						}
					}
				);
				if(empty_index > -1) {
					alert (msg);
					$("form.order_form").find("input.mandatory").get(empty_index).focus();
					return false;
				}
				$("form.order_form").submit();
			}
		);

        $("em.agree > :checkbox").click(
			function(){
                if(this.checked == false){
                  $('a#step5').css('display','none');
                }else{
                  $('a#step5').css('display','block');
                }
			}
		);

        /* comment form 	*/
		$("form.comment_form").find("a.bt").click(
            function(event){
				event.preventDefault();
				$(this).blur();
				var msg = "Пожалуйста, заполните обязательные поля:";
				var empty_index = -1;
				$("form.comment_form").find("input.mandatory").each(
					function(index){
						if($(this).attr("value") == "") {
							msg = msg + "\r\n * " + $(this).attr("alt");
							if(empty_index < 0) {
								empty_index = index;
							}
						}
					}
				);
                $("form.comment_form").find("textarea.mandatory").each(
					function(index){
						if($(this).attr("value") == "") {
							msg = msg + "\r\n * " + $(this).attr("alt");
							if(empty_index < 0) {
								empty_index = index;
							}
						}
					}
				);
				if(empty_index > -1) {
					alert (msg);
					$("form.comment_form").find("input.mandatory").get(empty_index).focus();
					return false;
				}
				$("form.comment_form").submit();
			}
		);

        /* feedback form 	*/
        $("div.top > ul.sub_nav > li.ref_link > a").click(
			function(event){
				event.preventDefault();
				$(this).blur();
                if($("div.top > form").css('display') == 'block'){
                   $("div.top > form").slideUp('slow');
                }else{
                   $("div.top > form").slideDown('slow');
                }
			}
		);
		$("form.feedback_form").find("a.bt").click(
            function(event){
				event.preventDefault();
				$(this).blur();
				var msg = "Пожалуйста, заполните обязательные поля:";
				var empty_index = -1;
				$("form.feedback_form").find("input.mandatory").each(
					function(index){
						if($(this).attr("value") == "") {
							msg = msg + "\r\n * " + $(this).attr("alt");
							if(empty_index < 0) {
								empty_index = index;
							}
						}
					}
				);
                $("form.feedback_form").find("textarea.mandatory").each(
					function(index){
						if($(this).attr("value") == "") {
							msg = msg + "\r\n * " + $(this).attr("alt");
							if(empty_index < 0) {
								empty_index = index;
							}
						}
					}
				);
				if(empty_index > -1) {
					alert (msg);
					$("form.feedback_form").find("input.mandatory").get(empty_index).focus();
					return false;
				}
				$("form.feedback_form").submit();
			}
		);

        $("a.open").click(
			function(event){
				event.preventDefault();
                var style = $(this).attr("alt");
				$(this).blur();
                $(this).fadeOut('slow');
                $("div."+style+" > div.more").slideDown('slow');
			}
		);
        $("a.close").click(
			function(event){
				event.preventDefault();
                var style = $(this).attr("alt");
				$(this).blur();
                $("div."+style+" > div.more").slideUp('fast');
                $("div."+style+" > a.open").fadeIn('slow');
			}
		);

});

function OrderAddF(id) {
    JsHttpRequest.query(
        '_back_order_add_f.php', // backend
        {
            // pass a text value
            'id': id
        },
        // Function is called when an answer arrives.
        function(result, errors) {
            // Write the answer.
            if (result) {
                document.getElementById("order_link").style.display = 'block';
                alert('Инструмент добавлен в заказ.');
            }
        },
        true  // disable caching
    );
}

function OrderFormCh(value,id) {
    JsHttpRequest.query(
        '_back_order_ch.php', // backend
        {
            // pass a text value
            'par': value,
            'id': id
        },
        // Function is called when an answer arrives.
        function(result, errors) {
            // Write the answer.
            if (result) {
                document.getElementById("shopping_cart").innerHTML = result["cart"];
                document.getElementById("total").innerHTML = result["total"];
            }
        },
        true  // disable caching
    );
}

function OrderFormUp(id) {
    JsHttpRequest.query(
        '_back_order_up.php', // backend
        {
            // pass a text value
            'id': id
        },
        // Function is called when an answer arrives.
        function(result, errors) {
            // Write the answer.
            if (result) {
                document.getElementById("shopping_cart").innerHTML = result["cart"];
                document.getElementById("total").innerHTML = result["total"];
            }
        },
        true  // disable caching
    );
}

function OrderFormDn(id) {
    JsHttpRequest.query(
        '_back_order_dn.php', // backend
        {
            // pass a text value
            'id': id
        },
        // Function is called when an answer arrives.
        function(result, errors) {
            // Write the answer.
            if (result) {
                document.getElementById("shopping_cart").innerHTML = result["cart"];
                document.getElementById("total").innerHTML = result["total"];
            }
        },
        true  // disable caching
    );
}

function OrderDel(id) {
    JsHttpRequest.query(
        '_back_order_del.php', // backend
        {
            // pass a text value
            'id': id
        },
        // Function is called when an answer arrives.
        function(result, errors) {
            // Write the answer.
            if (result) {
                document.getElementById("shopping_cart").innerHTML = result["cart"];
                document.getElementById("total").innerHTML = result["total"];
                if (result["flag"] == '2'){
                  document.getElementById("step").style.display = 'none';
                }
            }
        },
        true  // disable caching
    );
}

function Delivery(id) {
    JsHttpRequest.query(
        '_back_order_delivery.php', // backend
        {
            // pass a text value
            'id': id
        },
        // Function is called when an answer arrives.
        function(result, errors) {
            // Write the answer.
            if (result) {
                document.getElementById("delivery_p").innerHTML = result["delivery"];
                document.getElementById("ttl_price").innerHTML = result["total"];
            }
        },
        true  // disable caching
    );
}
