﻿	
	$(function() {
		$.datepicker.setDefaults( $.datepicker.regional[ "ru" ] );
		$( "#kogdabox" ).combobox2();
		$( "#otkudabox" ).combobox();
		$( "#prodbox" ).combobox();
		$( "#vzrosbox" ).combobox();
		$( "#deteybox" ).combobox();
		$( "#stoimbox" ).combobox();
		$( "#istochnik_zayavki" ).combobox();
		$( "#istochnik_zayavki2" ).combobox();
		$( "#menedger" ).combobox();
		$( "#status_zayavki" ).combobox();
		$( "#ch_kogdabox").combobox2();
		$( "#ch_otkudabox" ).combobox();
		$( "#ch_prodbox" ).combobox();
		$( "#ch_vzrosbox" ).combobox();
		$( "#ch_deteybox" ).combobox();
		$( "#ch_stoimbox" ).combobox();
		$( "#ch_istochnik_zayavki" ).combobox();
		$( "#ch_istochnik_zayavki2" ).combobox();
		$( "#ch_menedger" ).combobox();
		$( "#ch_status_zayavki" ).combobox();
		$( "#f_date" ).combobox();
		$( "#f_meneger" ).combobox();
		$("#create_new").button();
		$("#f_button").button();
		//$('button').button();
		//$("tr input").attr("size","26");
		//$("#KudaDialogRadio").buttonsetv();
		$("#radio").buttonsetv();
		$("#ch_radio").buttonsetv();
		$("tr[class='frm_small'] input").attr("class","frm ui-widget ui-widget-content ui-corner-left frm_smallinput");
		$("tr[class='frm_small2'] input").attr("class","frm ui-widget ui-widget-content ui-corner-left frm_smallinput2");
		$("tr[class='frm_small3'] input").attr("size","25");
		$("tr[class='frm_normal'] input").attr("size","31");
		$("[type='call_date']").datepicker( {dateFormat: 'dd.mm'});
		
		//$("#kogdabox IMG").attr("style","{width:20px;height:20px;margin-left:7px; margin-bottom:-7px}");
		//$("tr[class='normal'] textarea").attr("size","125");
		$( "#KudaDialog" ).dialog({
			autoOpen: false,
			width:100,
			height:500,
			show: "highlight",
			hide: "highlight",
			//position: [100,50],
			buttons: {
				Ok: function() {     
					var allVals = [];
					$('input[name="countries"]:checked').each(function() {
						allVals.push($(this).val());
					});
					$( this ).dialog( "close" );
					$("[custom='globus']").val(allVals);
					$("[custom='globus']").attr('custom','');
					}
				}
		});
		
		
		$("[name='globusIMG']").click(function(e) {
			$(this).prev().attr('custom','globus');
			var offset = $(this).offset();
			$( "#KudaDialog" ).dialog( {position:[offset.left + 25,offset.top]});
			$( "#KudaDialog" ).dialog("open");
		});
		var x1= Math.floor(Math.random()*8+1)
		var x2= Math.floor(Math.random()*8+1)
		var res=x1+x2;
		$("<b>"+x1+"+"+x2+"=</b>").appendTo('#labelProtect');
		$("#submitbutton").click(function(){
			if ($("#protect").val()==res) {
				if ($("#submit").length==0) {
					//$("#form").submit();
					var  parameters=$("#form").serialize();
					//$("#form").resetForm();
					x1= Math.floor(Math.random()*8+1)
					x2= Math.floor(Math.random()*8+1)
					res=x1+x2;
					//$("<b>"+x1+"+"+x2+"=</b>").append('#labelProtect');
					$('#labelProtect').html("Защита от роботов: <b>"+x1+"+"+x2+"=</b>");
					$('#protect').val('');//$('#form').refresh();
					//alert(parameters);
                   	//$('#form').refresh();			
				$.ajax({  
					type:"POST",
					url: "http://www.sorvemsia.ru/forma/DB_index.php",
					data:""+parameters+"",
					async: false,
					success: function(Data){
						if (Data.length>0) {
							alert(Data);
						}
						else {alert('Ошибка передачи данных.'+ Data)}
						//$("#waiting").hide();
					}
				}); 
				
				}
			}
			else {
				$("#submit").remove();
				if ($("#submitbutton").length==0) {
				$("<input type='button' id='submitbutton' value='Отправить'  class='frm ui-widget-content ui-corner-all'>").insertAfter($("#for_submit"));
				}
				if ($("#protect").val()!=res) {
					$("#protect").val('');
					alert("Неверная сумма чисел.");
				}
			}
		});	
		
		var array = [ " Бали", " Индонезия", " Болгария", " Бразилия", " Вьетнам", " Гоа", " Индия", " Греция", " Доминикана", " Египет", 
						" Израиль", " Испания", " Италия", " Канары", " Кипр", " Китай", " Куба", " Мальдивы", " Мальта", " Марокко", " Мексика", 
						" ОАЭ", " США", " Тайланд", " Тунис", " Турция", " Франция", " Германия", " Хорватия", " Черногория", "Чехия", "Шри-Ланка",
						" Швейцария", " Хургада", " Шарм-эль-шейх"];
		$("#kuda").autocomplete({
			source: function( request, response ) {
				var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
				response( $.grep( array, function( value ) {
					value = value.label || value.value || value;
					return matcher.test( value );
				}) );
			}
		});
		$("#ch_kuda").autocomplete({
			source: function( request, response ) {
				var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
				response( $.grep( array, function( value ) {
					value = value.label || value.value || value;
					return matcher.test( value );
				}) );
			}
		});
		$("#f_kuda").autocomplete({
			source: function( request, response ) {
				var matcher = new RegExp( $.ui.autocomplete.escapeRegex( request.term ), "i" );
				response( $.grep( array, function( value ) {
					value = value.label || value.value || value;
					return matcher.test( value );
				}) );
			}
		});
		$("#kuda").click(function(){
			$(this).autocomplete( "search" , " " );
		});
		$("#ch_kuda").click(function(){
			$(this).autocomplete( "search" , " " );
		});		
		$("#f_kuda").click(function(){
			$(this).autocomplete( "search" , " " );
		});	
	});
