
	var regUser = 0;
	var curMenuUp = 0;
	var errorFlag = 0;
	
	jQuery(function($){
	   $("#userPhone").mask("9-999-9999999");
	});

	

	$(document).ready(function(){
        $('#sellcarForm select').each(function(table){
		    $(this).blur(function() {
		    	checkForm();
		    });
		});
		
        $('#sellcarForm input').each(function(table){
		    $(this).blur(function() {
		    	checkForm();
		    });
		});
		
      /*  $('#city').blur(function(){
			checkLastForm()
		});
		
        $('#userName').blur(function(){
			checkLastForm()
		});
		
        $('#userPhone').blur(function(){
			checkLastForm()
		});
		
        $('#userMail').blur(function(){
			checkLastForm()
		});*/
		
		
	});
	

	
	function checkRegData() {
		
		var password = $('#password').attr('value');
		var password_conf = $('#password_conf').attr('value');
		
		if (password != password_conf) {
			
			$('#pwdConf').show();
			$("#tr_pass_confirm").addClass("alert");
			return 0;
		} 
				
	}
	
	
	function submitForm(idFlag) {
		

		var regNot = $('#regTrue').attr('checked');

		if (idFlag == 1) {
			
			if (checkForm() == 0 && checkLastForm() == 0) {
				document.forms['sellcarForm'].submit();
			}
			
		} else {
	
		var mail = $('#userMail').attr('value');
	
			
		var reg = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
		if (!mail.match(reg) || mail == '') {
			
			$('#emailReg').hide();
			$('#emailWrong').show();
			$("#tr_mail").addClass("alert");
			
		} else {
			
			$('#emailWrong').hide();
			$("#tr_mail").removeClass("alert");
				
			
			if (regNot == true) {
				
				if (checkRegData() != 0) { 
					
				var userMail = $('#userMail').attr('value');
				var password = $('#password').attr('value');
				var password_conf = $('#password_conf').attr('value');

				
				$('#pwdConf').hide();
				$("#tr_pass_confirm").removeClass("alert");
				
				    $.ajax({
					    url:'/ajax/carSale.php',
				        type: 'POST',
				        dataType : "json",
				        data: 'type=7&userMail='+userMail+'&password='+password,
				        cache: false,
				        success: function(table){
							if (table['result'] == '1') {
								$('#password').show();
								$("#tr_pass").addClass("alert");
							} else {
								
								$("#tr_pass").removeClass("alert");
									
									$('#password').show();
									$("#tr_pass").removeClass("alert");
									if (checkForm() == 0 && checkLastForm() == 0) {
										document.forms['sellcarForm'].submit();
									}
								
							}
				        },
				        error: function(){
				          //  alert('ERROR');
				        }
				    });
				}
			}  else {
				
					$('#emailWrong').hide();
					$("#tr_mail").removeClass("alert");
				
	    	
				    $.ajax({
					    url:'/ajax/carSale.php',
				        type: 'GET',
				        dataType : "json",
				        data: 'type=4&mail='+mail,
				        cache: false,
				        success: function(table){
				        	
				        	if ($('#regTrue').attr('checked') != true) {
				        		
								if (table['mail'] == 'bad') {
									$('#emailReg').show();
									$("#tr_mail").addClass("alert");
								} else {
									
									$("#tr_mail").removeClass("alert");
									$('#emailReg').hide();
									
									if (checkForm() == 0 && checkLastForm() == 0) {
										document.forms['sellcarForm'].submit();
									}
									
								}
							
				        	}
							
				        },
				        error: function(){
				          //  alert('ERROR');
				        }
				    });
				}
		    }	
		}
	}
	
	
	function checkLastForm() {
		
		flag = 0;
		
		city = $('#city').attr('value');

        trId = '#tr_city';
		if (city == '') {
			flag = 1;
			$(trId).removeClass();
			$(trId).addClass('alert');
		} else {
			$(trId).removeClass();
		}
		
		city = $('#userName').attr('value');
        trId = '#tr_userName';
		if (city == '') {
			flag = 1;
			$(trId).removeClass();
			$(trId).addClass('alert');
		} else {
			$(trId).removeClass();
		}
		
		city = $('#userPhone').attr('value');
        trId = '#tr_userPhone';
		if (city == '') {
			flag = 1;
			$(trId).removeClass();
			$(trId).addClass('alert');
		} else {
			$(trId).removeClass();
		}
		

		// 
		if (regUser == 1 && $('#regTrue').attr('checked') != 'true') {
			

	    	var mail = $('#userMail').attr('value');
	    	
	    	if (mail != '') {
			    $.ajax({
				    url:'/ajax/carSale.php',
			        type: 'GET',
			        dataType : "json",
			        data: 'type=4&mail='+mail,
			        cache: false,
			        success: function(table){
			        	
			        	if ($('#regTrue').attr('checked') != true) {
			        		
							if (table['mail'] == 'bad') {
							    flag = 1;
								$('#emailReg').show();
								$("#tr_mail").addClass("alert");
							} else {
								$("#tr_mail").removeClass("alert");
								$('#emailReg').hide();
							}
							
						
			        	}
						
			        },
			        error: function(){
			          //  alert('ERROR');
			        }
			    });
		    
	    	}
	    	
		}
		
		return flag;
		
	}

	function checkForm() {
		
		var flag = 0;
        $('#sellcarForm select').each(function(a) {
        	var trId = '#tr_'+$(this).attr('id');
			if ($(this).attr('id') == 'marka') {
				if ($(this).attr('value') == '0') {
					$(trId).removeClass();
					$(trId).addClass('alert');
					if (curMenuUp == 0) {flag = 1;}
				} else {
					$(trId).removeClass();
				}
				
			} else if ($(this).attr('id') == 'model') {
				if ($(this).attr('value') == '0' && $('#modelName').attr('value') == "") {
					$(trId).removeClass();
					$(trId).addClass('alert');
					if (curMenuUp == 0) {flag = 1;}
				} else {
					$(trId).removeClass();
				}
			}	
			
		});
		
        $('#sellcarForm input').each(function(a) {
        	var trId = '#tr_'+$(this).attr('id');
			if ($(this).attr('id') == 'engine' || $(this).attr('id') == 'power') 
			{
				if ($(this).attr('value') == '') {
					$(trId).removeClass();
					$(trId).addClass('attention');
				} else {
					
					if ($(this).attr('id') == 'engine') {
						
						if (parseInt($(this).attr('value')) < 700 || parseInt($(this).attr('value')) > 9000 || isNaN(parseInt($(this).attr('value')))) {
							$(trId).removeClass();	
							$(trId).addClass('attention');
							$('#engineFp').html('<em>Должен быть не меньше 700 и не более 9000 </em> Объем двигателя:'); 
							$('#engineFp em').css('display', 'inline');
						} else {
							$('#engineFp').html('Объем двигателя:'); 
							$(trId).removeClass();	
						}
					} else {
						$(trId).removeClass();	
					}
				}
				
			} else if($(this).attr('id') == 'probeg' || $(this).attr('id') == 'price') {
				
				if ($(this).attr('value') == '') {
					if (curMenuUp == 0) {flag = 1;}
					$(trId).removeClass();
					
					if ($(this).attr('id') == 'price') {
						$(trId).addClass('alert bigInside');
					} else {
						$(trId).addClass('alert');
					}
				
				} else {
					
					$(trId).removeClass();
					if ($(this).attr('id') == 'price') {$(trId).addClass('bigInside');}
					
					if ($(this).attr('id') == 'price') { 
					 
						if (((parseInt($(this).attr('value')) < 10000 && $('#valutaType').attr('value') == 'RUB') || (parseInt($(this).attr('value')) < 300 && $('#valutaType').attr('value') == 'USD') || (parseInt($(this).attr('value')) < 300 && $('#valutaType').attr('value') == 'EURO') || isNaN(parseInt($(this).attr('value')))) || (parseInt($(this).attr('value')) > 100000) && $('#valutaType').attr('value') != 'RUB') {

							
							if ($('#valutaType').attr('value') == 'USD') {
								var valuta = '$';
								var pr = '300';
					 		} else if ($('#valutaType').attr('value') == 'EURO') {
								var valuta = '&euro;';
								var pr = '300';
							} else if ($('#valutaType').attr('value') == 'RUB') {
								var valuta = 'р.';
								var pr = '10000';
							}
							
							//
							if (parseInt($(this).attr('value')) > 100000 && $('#valutaType').attr('value') != 'RUB')
							{
								$(trId).removeClass();	
								$(trId).addClass('attention bigInside');
								$('#priceFp').html('<em style="font-size:12px;">Вы уверены что Ваш автомобиль стоит '+$(this).attr('value')+' '+valuta+' </em> Цена:'); 
								$('#priceFp em').css('display', 'inline');
								
							} else {
								$(trId).removeClass();	
								$(trId).addClass('alert bigInside');
								if (curMenuUp == 0) {flag = 1;}
								$('#priceFp').html('<em style="font-size:12px;">Цена автомобиля не должна быть меньше '+pr+' '+valuta+' </em> Цена:'); 
							}
						} else {
							$('#priceFp').html('Цена:'); 
							$(trId).removeClass();	
							$(trId).addClass('bigInside');
						}
					} else if($(this).attr('id') == 'probeg') {
						
						if (parseInt($(this).attr('value')) < 1000 || isNaN(parseInt($(this).attr('value')))) {
							if (isNaN(parseInt($(this).attr('value')))) {
								$(this).attr('value', 0);
							}
						
							
							$(trId).addClass('attention');
							$('#probegFp').html('<em>У вас точно '+$(this).attr('value')+' '+$('#probegType').attr('value')+' пробега ?</em> Пробег:'); 
							$('#probegFp em').css('display', 'inline');
						} else {
							$('#probegFp').html('Пробег:'); 
							$(trId).removeClass();	
						} 
						
					} else if($(this).attr('id') == 'emailReg') {
						

						
					} else {
						$(trId).removeClass();	
					}
					
					
				}
				
			} 
		});
		
		if (flag != 0) {
			return 1;
		} else {
			return 0;
		}
		
	}


	function disSuj() {
	    $('#suj').html('');
	}
	
	function disSujCity() {
	    $('#sujCity').html('');
	}
	
	function colorPick(e) {
		$('#carColor').attr('value', e);
		$('#colorError').html('');
		$('#cError').html('');
	}

	$(document).ready(function(){
	    $("#modelName").blur(function () {
	    	setTimeout('disSuj()', '500');
	    });
	    
	    $("#city").blur(function () {
	    	setTimeout('disSujCity()', '500');
	    });
	    
	});
	
	function stSujVal(name) {
		$("#modelName").attr('value', name);
	}
	
	

	function checkAjFields(val, time) {
		if (time) {
			setTimeout("AjFields('"+val+"')", 200);
		} else {
			AjFields(val);
		}
	}

    function AjFields(val) {

    	var dataStr = '';
    	
    	if (val == 1) {
    		dataStr = 'type=1';
    		var marka = $('#marka').attr('value');
    		var year = $('#year').attr('value');
    		dataStr+='&marka='+marka+'&year='+year;
    	} else if (val == 2) {
    		if ($('#model').attr('value') == '0') {
				$('#modelField').show();
				return;
    		} else {
    			$('#modelField').hide();
    			return;
    		}
    	} else if (val == 3) {
    		var marka = $('#marka').attr('value');
    		var field = $('#modelName').attr('value');
    		dataStr = 'type=3';
    		dataStr+='&marka='+marka+'&field='+encodeURIComponent(field)+'';
    	} 
    	
    	if (dataStr != '') {
		    $.ajax({
			    url:'/ajax/carSale.php',
		        type: 'GET',
		        dataType : "json",
		        data: dataStr,
		        cache: false,
		        success: function(table){
		        	
		        	if (val == 3) {
		        		
		        		$('#suj').html(table[0]);
		        		
		        	} else {
		        	
			        	if (jQuery.trim(table['html']) != '') {
							$('#modelContainer').html(table['html']);
			        	}
			        	
			        	if (jQuery.trim(table['logoPic']) != '') {
							$('#fbc0').css('background-image', 'url('+table['logoPic']+')');
			        	}
			        	
			        	if (jQuery.trim(table['dopfield']) != '') {
							$('#modelField').show();
			        	}
		        	
		        	}
		        	
		        },
		        error: function(){
		          //  alert('ERROR');
		        }
		    });
    	}
    }
    
    function showHide() {
    	var country = $('#country').attr('value');
    	if (country != 138) {
    		$('#tr_toll').css('display', '');
    	} else {
    		$('#tr_toll').css('display', 'none');
    	}
    	
    	if (country == 197 || country == 17 || country == 31) {
    		$('#wheelTr').css('display', '');
    	} else {
    		$('#wheelTr').css('display', 'none');
    	}
    	
    }
    
    
    function regChange(id) {
    	
    	if (id == 'regTrue') {
			
    		regUser = 0;
			$('#emailWrong').hide();
			$('#emailReg').hide();
			$("#tr_mail").removeClass("alert");
    		$('#tr_pass').show();
	  		$('#tr_pass_confirm').show();
	  		
    	} else {
    		
    		regUser = 1;

			$('#emailReg').hide();
    		$('#tr_pass').hide();
	  		$('#tr_pass_confirm').hide();

    	}
    	
    }
    
    
	function stSujCity(name) {
		$("#city").attr('value', name);
	}

	$(document).ready(function(){
		
	    $('#city').keypress(function() {
	    	var field = encodeURIComponent($('#city').attr('value'));
		    $.ajax({
			    url:'/ajax/carSale.php',
		        type: 'GET',
		        data: 'type=6&field='+field,
		        cache: false,
		        success: function(table){
		        	$('#sujCity').html(table);
		        },
		        error: function(){
		          //  alert('ERROR');
		        }
		    });
	    })
	});
	
	
	function checkColor() {
		
		if ($('#carColor').attr('value') == '') {
			return 1;
		} else {
			return 0;
		}
		
	}
	 
	function deleteCarPhoto(id, carid) {
	    $.ajax({
		    url:'/ajax/carSale.php',
	        type: 'GET',
	        data: 'type=8&id='+id+'&carid='+carid,
	        cache: false,
	        success: function(table){
	        	$('#carSellItems').html(table);
	        },
	        error: function(){
	          //  alert('ERROR');
	        }
	    });
	}