function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	return x1 + x2;
}


;(function ($) {

	$(document).ready(function() {
		
		$("#obl1").autocomplete("/requestcard/json", {
			width: 260,
			selectFirst: false,
			cacheLength:0,
			extraParams: {obl: 0}
		}).result(function(event, data, formatted) {
			if (data){
				$("#obl1_hidden").val(data[1]);
				$("#area1_hidden").val(data[2]);
				$("#city1_hidden").val(data[3]);
				$("#punkt1_hidden").val(data[4]);
				$("#street1_hidden").val(data[5]);
			}
		});
		
		$("#obl2").autocomplete("/requestcard/json", {
			width: 260,
			selectFirst: false,
			cacheLength:0,
			extraParams: {obl: 0}
		}).result(function(event, data, formatted) {
			if (data){
				$("#obl2_hidden").val(data[1]);
				$("#area2_hidden").val(data[2]);
				$("#city2_hidden").val(data[3]);
				$("#punkt2_hidden").val(data[4]);
				$("#street2_hidden").val(data[5]);
			}
		});
		
		
		$("#area1").autocomplete("/requestcard/json", {
			width: 260,
			selectFirst: false,
			cacheLength:0,
			extraParams: {obl: $("#obl1_hidden").val(), area: 0}
		}).result(function(event, data, formatted) {
			if (data){
				$("#obl1_hidden").val(data[1]);
				$("#area1_hidden").val(data[2]);
				$("#city1_hidden").val(data[3]);
				$("#punkt1_hidden").val(data[4]);
				$("#street1_hidden").val(data[5]);
			}
		});
		
		$("#area2").autocomplete("/requestcard/json", {
			width: 260,
			selectFirst: false,
			cacheLength:0,
			extraParams: {obl: function(){return $("#obl2_hidden").val();}, area: 0}
		}).result(function(event, data, formatted) {
			if (data){
				$("#obl2_hidden").val(data[1]);
				$("#area2_hidden").val(data[2]);
				$("#city2_hidden").val(data[3]);
				$("#punkt2_hidden").val(data[4]);
				$("#street2_hidden").val(data[5]);
			}
		});

		
		$("#city1").autocomplete("/requestcard/json", {
			width: 260,
			selectFirst: false,
			cacheLength:0,
			extraParams: {obl: function(){return $("#obl1_hidden").val();}, area: function(){return $("#area1_hidden").val();}, city: 0}
		}).result(function(event, data, formatted) {
			if (data){
				$("#obl1_hidden").val(data[1]);
				$("#area1_hidden").val(data[2]);
				$("#city1_hidden").val(data[3]);
				$("#punkt1_hidden").val(data[4]);
				$("#street1_hidden").val(data[5]);
			}
		});
		
		$("#city2").autocomplete("/requestcard/json", {
			width: 260,
			selectFirst: false,
			cacheLength:0,
			extraParams: {obl: function(){return $("#obl2_hidden").val();}, area: function(){return $("#area2_hidden").val();}, city: 0}
		}).result(function(event, data, formatted) {
			if (data){
				$("#obl2_hidden").val(data[1]);
				$("#area2_hidden").val(data[2]);
				$("#city2_hidden").val(data[3]);
				$("#punkt2_hidden").val(data[4]);
				$("#street2_hidden").val(data[5]);
			}
		});
		
		$("#street1").autocomplete("/requestcard/json", {
			width: 260,
			selectFirst: false,
			cacheLength:0,
			extraParams: {index: function(){return $("#address1_index").val();}, obl: function(){return $("#obl1_hidden").val();}, area: function(){return $("#area1_hidden").val();}, city: function(){return $("#city1_hidden").val();}, punkt: function(){return $("#punkt1_hidden").val();}, street: 0}
		}).result(function(event, data, formatted) {
			if (data){
				$("#obl1_hidden").val(data[1]);
				$("#area1_hidden").val(data[2]);
				$("#city1_hidden").val(data[3]);
				$("#punkt1_hidden").val(data[4]);
				$("#street1_hidden").val(data[5]);
			}
		});
		
		$("#street2").autocomplete("/requestcard/json", {
			width: 260,
			selectFirst: false,
			cacheLength:0,
			extraParams: {index: function(){return $("#address2_index").val();}, obl: function(){return $("#obl2_hidden").val();}, area: function(){return $("#area2_hidden").val();}, city: function(){return $("#city2_hidden").val();}, punkt: function(){return $("#punkt2_hidden").val();}, street: 0},
			focus: function() {
				return false;
			}
		}).result(function(event, data, formatted) {
			if (data){
				$("#obl2_hidden").val(data[1]);
				$("#area2_hidden").val(data[2]);
				$("#city2_hidden").val(data[3]);
				$("#punkt2_hidden").val(data[4]);
				$("#street2_hidden").val(data[5]);
			}
		});
	});


})(jQuery);


$dr(document).ready(function() {
	
	$dr('.mask_int').focus(function(){
		$dr(this).select();
	}).keypress(function(event){
		var reg = /[0-9]/;
		if (event.which != 8 && event.which != 0 && !reg.test(String.fromCharCode(event.which)))
			return false;
	});
	
	$dr(".mask_date").mask("99.99.9999",{placeholder:"_"});
	/*$dr(".mask_int").mask("9?9999999999999999999",{placeholder:" "});*/
	$dr(".mask_phone_mobile").mask("+7 (999) 999-99-99",{placeholder:"_"});
	$dr(".mask_phone").mask("+7 (9999) 99-99-99",{placeholder:"_"});
	$dr(".mask_passport").mask("99 99 999999",{placeholder:"_"});
	$dr(".mask_passport2").mask("999-999",{placeholder:"_"});
	$dr(".mask_dohod").mask("9?9999999999999999999",{placeholder:" "});
	
	$dr("#dohodOsn, #dohodSovm, #dohodPens, #dohodOther").keypress(function(){
		var total = 0;
		total += isNaN (parseInt ( $dr("#dohodOsn").val() )) ? 0 : parseInt ( $dr("#dohodOsn").val() );
		total += isNaN (parseInt ( $dr("#dohodSovm").val() )) ? 0 : parseInt ( $dr("#dohodSovm").val() );
		total += isNaN (parseInt ( $dr("#dohodPens").val() )) ? 0 : parseInt ( $dr("#dohodPens").val() );
		total += isNaN (parseInt ( $dr("#dohodOther").val() )) ? 0 : parseInt ( $dr("#dohodOther").val() );
		$dr("#dohodTotal").val(total);
	});
	
	$dr("#rashodNalog, #rashodAlim, #rashodCred, #rashodOther").keypress(function(){
		var total = 0;
		total += isNaN (parseInt ( $dr("#rashodNalog").val() )) ? 0 : parseInt ( $dr("#rashodNalog").val() );
		total += isNaN (parseInt ( $dr("#rashodAlim").val() )) ? 0 : parseInt ( $dr("#rashodAlim").val() );
		total += isNaN (parseInt ( $dr("#rashodCred").val() )) ? 0 : parseInt ( $dr("#rashodCred").val() );
		total += isNaN (parseInt ( $dr("#rashodOther").val() )) ? 0 : parseInt ( $dr("#rashodOther").val() );
		$dr("#rashodTotal").val(total);
	});
	
	
	$dr(".mask_index").mask("999999", {completed:function(){
		if ( $dr(this).attr('name') == 'address1[index]' ) {
			$dr('#byIndex1').css('display', 'block');
			clearAddressInfo ('1');
			
			$dr.ajax({
				url: "/requestcard/json",
				global: false,
				type: "get",
				data: ({byIndex : $dr(this).val()}),
				dataType: "json",
				success: function(json){
					$dr("#obl1").val(json.obl);
					$dr("#area1").val(json.area);
					$dr("#city1").val(json.city);
					$dr("#obl1_hidden").val(json.obl_id);
					$dr("#area1_hidden").val(json.area_id);
					$dr("#city1_hidden").val(json.city_id);
					$dr("#punkt1_hidden").val(json.punkt_id);
				}
			});
		}
		if ( $dr(this).attr('name') == 'address2[index]' ) {
			$dr('#byIndex2').css('display', 'block');
			clearAddressInfo ('2');
			
			$dr.ajax({
				url: "/requestcard/json",
				global: false,
				type: "get",
				data: ({byIndex : $dr(this).val()}),
				dataType: "json",
				success: function(json){
					$dr("#obl2").val(json.obl);
					$dr("#area2").val(json.area);
					$dr("#city2").val(json.city);
					$dr("#obl2_hidden").val(json.obl_id);
					$dr("#area2_hidden").val(json.area_id);
					$dr("#city2_hidden").val(json.city_id);
					$dr("#punkt2_hidden").val(json.punkt_id);
				}
			});
		}
	}},{placeholder:" "});
		
		
		
	$dr("#bralCredit").change(function(){
		if ($dr(this).is(':checked')) {
			$dr("#creditInfo").css('display', 'block');
		} else {
			$dr("#creditInfo").css('display', 'none');
		}
	});
	$dr("#estCredit").change(function(){
		if ($dr(this).is(':checked')) {
			$dr("#creditOstatok").css('display', 'block');
		} else {
			$dr("#creditOstatok").css('display', 'none');
		}
	});
		
	
	
	$dr("#semPol").change(function(){
		if ($dr(this).val() == 2) {
			$dr("#suprugInfo").css('display', 'block');
		} else {
			$dr("#suprugInfo").css('display', 'none');
		}
	});
	
	
	
	$dr("#step2_supr_address_1").change(function() {
		if ($dr(this).is(':checked')) {
			$dr("input[name='step2[supr_address]']").val('');
			$dr("input[name='step2[supr_address]']").attr('disabled', true);
			$dr("input[name='step2[supr_address]']").addClass('text_disabled');
		} else {
			$dr("input[name='step2[supr_address]']").attr('disabled', false);
			$dr("input[name='step2[supr_address]']").removeClass('text_disabled');
		}
	});
	
	$dr("#socStatus").change(function(){
		switch ( $dr(this).val() ) {
			case '1':
			case '2':
				$dr("#workPlaceInfo").css('display', 'block');
				$dr("#workPlaceBoss").css('display', 'block');
				break;
			case '3':
			case '6':
			case '7':
				$dr("#workPlaceInfo").css('display', 'none');
				$dr("#workPlaceBoss").css('display', 'none');
				break;
			case '4':
			case '5':
				$dr("#workPlaceInfo").css('display', 'none');
				$dr("#workPlaceBoss").css('display', 'block');
				break;
		}
	});
	
	
	$dr('a.submit_next').click(function(elem){
		elem.preventDefault();
		$dr(this).closest('form').find('input').removeAttr("disabled");
		$dr(this).closest('form').find('select').removeAttr("disabled");
		$dr(this).closest('form').submit();
	});
	
	
	
	$dr(".check_f1").focus(function() {
		$dr(this).parent(".form-value").removeClass('checked_ok');
		$dr(this).parent().parent(".form-value").removeClass('checked_ok');
	});
	$dr(".check_f1").blur(function() {
		// любое поле, проверка по длине
		if ($dr(this).val().length > 0) {
			$dr(this).parent(".form-value").addClass('checked_ok');
			$dr(this).parent().parent(".form-value").addClass('checked_ok');
		}
	});
	
	
	
	
	
	
	$dr(document).click(function(elem){
		if ( $dr('#agreement-box').length && $dr('#agreement-box').css('display') == 'block' &&
		     $dr(elem.target).closest('.agreement-text').length == 0 && $dr(elem.target).closest('a.agreement-a').length == 0) {
			$dr('#agreement-box').css('display', 'none');
		}
	});
	$dr('a.agreement-a').click(function(elem){
		elem.preventDefault();
		var elemtmp = $dr(this).next('#agreement-box');
		elemtmp.css({
			'display': 'block'
		});
		elemtmp.children().css({
			'margin-top': ( (window.innerHeight -elemtmp.children().height() )/2) + 'px'
		});
	});
	
	
	
});
	/*
	var countCreds = 0;
	$dr(document).ready(function(){
		countCreds = 
	});*/

	function addCred() {
		var newId = $dr('.cred-name-form .cred-field-form').length;
		var str1 = '<div class="cred-field-form" id="cred-name-'+newId+'"><input type="text" name="step4[cred_cred_deist_bank]['+newId+']" value="" class="text" /></div>';
		var str2 = '<div class="cred-field-form" id="cred-val-'+newId+'"><input type="text" name="step4[cred_cred_ost]['+newId+']" value="" class="text mask_dohod" /> руб.</div>';
		$dr("#cred-name-" + (newId-1)).after(str1);
		$dr("#cred-val-" + (newId-1)).after(str2);
		countCreds++;
		$dr('#cred-name-'+newId).children('.text').focus();
	}


	function sobsSelector(box) {
		if ($dr("#sobs_check_"+box).is(':checked')) {
			$dr("#sobs_box_"+box).css('display', 'block');
		} else {
			$dr("#sobs_box_"+box).css('display', 'none');
		}
	}


	function clearAddressInfo (box) {
		$dr("input[name='address" + box + "[region]']").val('');
		$dr("#obl" + box + "_hidden").val('0');
		$dr("input[name='address" + box + "[area]']").val('');
		$dr("#area" + box + "_hidden").val('0');
		$dr("input[name='address" + box + "[city]']").val('');
		$dr("#city" + box + "_hidden").val('0');
		$dr("#punkt" + box + "_hidden").val('0');
		$dr("input[name='address" + box + "[street]']").val('');
		$dr("input[name='address" + box + "[house]']").val('');
		$dr("input[name='address" + box + "[corp]']").val('');
		$dr("input[name='address" + box + "[apart]']").val('');
		$dr("input[name='address" + box + "[stroenie]']").val('');
	}
	function doNotKnowIndex (box) {
		$dr("input[name='address" + box + "[index]']").val('');
		clearAddressInfo (box);
		if ($dr("input[name='address" + box + "[doNotKnowIndex]']").is(':checked')) {
			$dr("#byIndex" + box + "").css('display', 'block');
			$dr("input[name='address" + box + "[index]']").attr('disabled', true);
			$dr("input[name='address" + box + "[region]']").attr('disabled', false);
			$dr("input[name='address" + box + "[area]']").attr('disabled', false);
			$dr("input[name='address" + box + "[city]']").attr('disabled', false);
		} else {
			$dr("#byIndex" + box + "").css('display', 'none');
			$dr("input[name='address" + box + "[index]']").attr('disabled', false);
			$dr("input[name='address" + box + "[region]']").attr('disabled', true);
			$dr("input[name='address" + box + "[area]']").attr('disabled', true);
			$dr("input[name='address" + box + "[city]']").attr('disabled', true);
		}
	}
	function adress2As1() {
		if ($dr("input[name='address2[as1]']").is(':checked')) {
			$dr("#adress2Box").css('display', 'none');
		} else {
			$dr("#adress2Box").css('display', 'block');
		}
	}





$dr(document).ready(function(){
	
		$dr('.request-begun').each(function(){
			var begun = $dr(this).find('.begun');
			var begunSel = begun.find('.begun-sel');
			var bul = begunSel.children('div');
			
			var inputId = begun.children('.inputId').val();
			var begunField = $dr('#' + inputId);
			
			var begunStart = begun.children('.min').val();
			var begunEnd = begun.children('.max').val();
			var begunStep = begun.children('.step').val();
			var begunCountSteps = (begunEnd - begunStart) / begunStep;
			
			var begunWidthMin = 0;
			var begunWidthMax = begun.width()-bul.width();
			var begunWidthStart = 0;
			var begunWidthStep = (begunWidthMax - begunWidthMin) / begunCountSteps;
			
			var bulStart = 0;
			var bulEnd = 0;
			var inAction = 0;
			
			var setBegunVal = function(){
				var wallPercent = 0.75; // стенка
				
				if (begunEnd > 1000000) {
					if ( begunSel.width() / begunWidthMax < wallPercent ) {  // 0 .. 0.75
						var begunValTmp = Number(begunStart) + Number(Math.floor( 1000000 *  begunSel.width() / begunWidthMax / wallPercent ));
					} else if ( begunSel.width() / begunWidthMax >= wallPercent   &&   begunSel.width() / begunWidthMax <= (wallPercent + 0.02) ) { // 0.75
						var begunValTmp = 1000000;
					} else { // 0.75 ...
						var begunValTmp = 1000000 + Number(Math.floor( (begunEnd-1000000) *  (begunSel.width() / begunWidthMax - wallPercent) / ( 1 - wallPercent ) ));
					}
	
					begunField.val( Math.floor(begunValTmp) );
					begunField.change();
					
				} else {
					begunField.val( Number(begunStart) + Number(Math.floor((begunSel.width()-begunWidthMin)  / begunWidthStep) * begunStep)  );
					begunField.change();
				}

			};
			
			var setBegunPos = function(elem){
				if (!inAction) {
					var val = Number ( elem.val() );
					if (isNaN (val) || val < begunStart) {
						val = begunStart;
					} else if (val > begunEnd) {
						val = begunEnd;
					}
					
					if (begunEnd > 1000000) {
						var wallPercent = 0.75; // стенка
						if (val <= 1000000) {
							var begunPosTmp = ((val-begunStart) / 1000000) * (begunWidthMax * wallPercent);
						} else {
							var begunPosTmp = begunWidthMax * (wallPercent  +  (1 - wallPercent) * ((val-1000000) / (begunEnd-1000000)));
						}
						begunField.val( val );
						begunSel.width(   Math.floor( begunPosTmp ) );
					} else {
						begunField.val( val );
						begunSel.width(   Math.floor( ((val-begunStart) / begunStep) * begunWidthStep )   );
					}
				}
			}
			
			setBegunPos( begunField );
			
			
			begunField.change(function(){
				setBegunPos( $dr(this) );
			});
		
			bul.mousedown(function(event){
				bulStart = event.pageX;
				begunWidthStart = begunSel.width();
				inAction = 1;
			});
			
			$dr(document).mousemove(function(event){
				if (inAction) {
					bulEnd= event.pageX;
					
					if (begunWidthStart + bulEnd - bulStart <= begunWidthMin) {
						begunSel.width(begunWidthMin);
					} else if (begunWidthStart + bulEnd - bulStart >= begunWidthMax) {
						begunSel.width( begunWidthMax );
					} else {
						begunSel.width( begunWidthStart + bulEnd - bulStart );
					}
					setBegunVal();
				}
			});
			
			begun.click(function(event){
				inAction = true;
				bulStart = begun.offset().left;
				bulEnd= event.pageX;
				if (bulEnd - bulStart <= begunWidthMin) {
					begunSel.width(begunWidthMin);
				} else if (bulEnd - bulStart >= begunWidthMax) {
					begunSel.width( begunWidthMax );
				} else {
					begunSel.width( bulEnd - bulStart -8 );
				}
				setBegunVal();
				inAction = false;
			}).find('.begun-sel > div').click(function(event){
				event.stopPropagation();
			});
			
			bul.mouseup(function(event){
				inAction = false;
			});
			$dr(document).mouseup(function(event){
				if (inAction) {
					inAction = false;
				}
			});
			
		});















	$dr('.sale-catalog-images').each(function(){
		var SCIcontainer = $dr(this);
		var SCIimage = SCIcontainer.children('a').children('img');
		var SCIimages = SCIcontainer.children('ul').children('li');
		
		SCIimage.parent().click(function(elem){
			elem.preventDefault();
		});
		
		if (SCIimages.length) {
			SCIimages.each(function(){
				$dr(this).children('a').click(function(elem){
					elem.preventDefault();
					SCIimage.attr('src', $dr(this).attr('href') );
					
					SCIimages.removeClass('current');
					$dr(this).parent().addClass('current');
				});
			});
		}
	});
});