$(function(){

		
		$('#back2form').click(function(){
			$('#cover2_container').css({
				display: 'none'
			});
			$('#request').css({
				position: 'relative',
				top: '0px'
			});
			$('#stamp_para').css({
				display: 'block'
			});
		});
		
		
		$('#submit_form').click(function(){
			isRealyReadyForSubmit = true;

			document.getElementById('submit').click();
			
		});
		
		var isRealyReadyForSubmit = false;
		
		xForm1.checkForSubmit = function(e) {   
			if (this.isReadyForSubmit() && check_field_length()) {
				if(isRealyReadyForSubmit || $('#cover2_container').length == 0 ){
					return true;
				} else {
					show_cover();
					return false;
				}
			} else {
				this.getFirstErrorWidget().focus();
				this.addClass(FormWidget.CLASS_NAME_SUBMITTED);
				return false;
			}
		}
		
		xForm1.isReadyForSubmit = function(){
		
			var aWidgets = this.aWidgets;
			
			for (var sId in aWidgets) {
			
				if (!aWidgets.hasOwnProperty(sId) ||
				aWidgets[sId] == this) {
					continue;
				}
				
				if (!aWidgets[sId].isReadyForSubmit(this.bCheckForValid)) {
					return false;
				}
				
			}
			
			return true;
			
		}
		


		xForm1.getFirstErrorWidget = function() {
			var oReturn;
			
			for(var sId in this.aWidgets) {
				if(!this.aWidgets.hasOwnProperty(sId)) {
					continue;
				}
				oWidget = this.aWidgets[sId];
				if(oWidget.isEnabled() &&
					(oWidget.bRequired ||
						(this.bCheckForValid && !oWidget.bValid)
						)
					) {
					
					
					if(sId == 'birthdate'){
						oReturn = document.getElementById('birthdateInput');
					} else {
						return oWidget;
					}
					
				}
			}
			
			

		}
		
		
		check_field_length = function(){
			/* ????? ????????? ???????? ? ?????? ?? ?????? ????? */
			if( $('#phone_mobile').val() != '' && !(/^[8][1-9]\d{9}$/).test($('#phone_mobile').val()) ){
				return false;
			}
			if(  $('#phone_home').val() != '' && !(/^[8][1-9]\d{9}$/).test($('#phone_home').val()) ){
				return false;
			}
			
			if( $('#postal_code').val() != '' && !(/^[1-9]\d{5}$/).test($('#postal_code').val())  ){
				return false;
			}
			if( $('#email').val() != '' && !(/^[a-zA-Z0-9][a-zA-Z0-9\.\-\_\~]*\@[a-zA-Z0-9][a-zA-Z0-9\.\-\_]*\.[a-zA-Z]{2,4}$/).test($('#email').val())  ){
				return false;
			}
			
			return true;
		}
		

		
		show_cover = function(){
			window.scrollTo(0, 0);
			$('#cover2_container').css({
				display: 'block'
			});
			$('#stamp_para').css({
				display: 'none'
			});
			$('#request').css({
				position: 'absolute',
				top: '-3000px'
			});
			
			if ($('#postal_code').val() != '') {
				$('#cover_index').css({
					display: 'inline'
				});
				$('#cover_index .value').html($('#postal_code').val())
			} else {
				$('#cover_index').css({
					display: 'none'
				});
			}
			
			$('#cover_region').html($('#place').val());
			
			if ($('#area').val() != '') {
				$('#cover_area').css({
					display: 'inline'
				});
				$('#cover_area .value').html($('#area').val())
			} else {
				$('#cover_area').css({
					display: 'none'
				});
			}
			
			if ($('#city').val() != '') {
				$('#cover_city').css({
					display: 'inline'
				});
				$('#cover_city .value').html($('#city').val())
			} else {
				$('#cover_city').css({
					display: 'none'
				});
			}
			
			$('#cover_street .value').html($('#street').val());
			$('#cover_house .value').html($('#building').val());
			
			if ($('#corpus').val()) {
				$('#cover_corpus').css({
					display: 'inline'
				});
				$('#cover_corpus .value').html($('#corpus').val());
			} else {
				$('#cover_corpus').css({
					display: 'none'
				});
			}
			if ($('#stroenie').val()) {
				$('#cover_stroenie').css({
					display: 'inline'
				});
				$('#cover_stroenie .value').html($('#stroenie').val());
			} else {
				$('#cover_stroenie').css({
					display: 'none'
				});
			}
			
			if ($('#flat').val()) {
				$('#cover_flat').css({
					display: 'inline'
				});
				$('#cover_flat .value').html($('#flat').val());
			} else {
				$('#cover_flat').css({
					display: 'none'
				});
			}
			if ($('#dormitory_i0').get(0).checked) {
				$('#cover_dormitory').css({
					display: 'inline'
				});
			} else {
				$('#cover_dormitory').css({
					display: 'none'
				});
			}
			
		}
		
});