 var height;

 $(document).ready(function(){
 $(".phones").mask("(999) 999-9999");	
 $("a.gallery").colorbox();
 $("a[rel='gallery']").colorbox();
 validateFields('form_name', '/_ajax/online.php', 'output_mail', 'overlay2');

 $('#code_MOSH230_2').change(function(){
    if(!$(this).attr('value').length)
       $('#code_MOSH400_2').removeAttr('disabled');
    else
       $('#code_MOSH400_2').attr('disabled', 'disabled');
 });
 
 $('img.prevView').hover(function(){
   var $obj = $(this);
   var $divObj = $(this).parents('div:first');
   
   var $wrapper = $('<div/>').css({
     'position': 'absolute',
     'margin-left': '90px',
     'margin-top': '0px',
     'z-index': '200'         
   }).addClass('popUp');
   
   $wrapper.append('<img src="' + $obj.parents('a:first').attr('href') + '" style="margin-left: 15px">');
   $divObj.prepend($wrapper);
 },
 function (){
   
   $('div.popUp').remove();
 });
 
 $('a.requestLink').click(function (){
   
    var $curLink = $(this);
    $('#basic-modal-content').modal({
      onOpen: function (dialog) {
        dialog.overlay.fadeIn('slow', function () {
          dialog.container.slideDown('slow', function () {
            
           $.ajax({
             type: "POST",
             url: "/_ajax/request.php",
             data: "name=John",
             success: function(msg){
               dialog.data.html(msg);
               $mosh=$curLink.parents('tr').find('a:first').attr('rel');               
               if($mosh.length)
                $('#form_request input[name="product"]').attr('value', $curLink.parents('tr').find('a:first').text()+'/ '+$mosh);
               else
                $('#form_request input[name="product"]').attr('value', $curLink.parents('tr').find('a:first').text());
               validateFields('form_request', '/_ajax/request.php', 'output', 'overlay');
               $("#form_request input[name='phone']").mask("(999) 999-9999");       
             }     
           });
            
            dialog.data.fadeIn('slow');          
            //$('.numeric').numeric();
            
            height=$('#simplemodal-container').css('height').substr(0,3)-0;
          });
        });
      }
   });
        
   return false;
 });

 $('#code_MOSH400_2').change(function(){
    if(!$(this).attr('value').length)
       $('#code_MOSH230_2').removeAttr('disabled');
    else
       $('#code_MOSH230_2').attr('disabled', 'disabled');
 });
 
 $('.vkladka').live('click', function(){
    var text = $('.current').find('span').html();
    var text2 = $(this).html();

    $('.current').html('<a href="#" class="vkladka">'+text+'</a>');
    $('.current').removeClass('current');
    $('.wrappers').addClass('hidden');
    $(this).parent().addClass('current');
    $('#p'+$(this).parent().attr('id')).removeClass('hidden');
    $('.current').html('<span>'+text2+'</span>');

    return false;
 });

 $('.preview_picture').click(function(){
	new_link='/image.php?main='+$(this).attr('rel');
	new_link=$(this).attr('rel');
    $('#main_picture').attr('src', $(this).attr('rel'));

     return false;
 });

 $('.order').click(function (e) {
    $('#basic-modal-content-order').modal({
        onOpen: function (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.slideDown('slow', function () {
			dialog.data.fadeIn('slow');

                        $('.numeric').numeric();
                        validateFields('form_price_modal', '/_ajax/price.php', 'output_modal', 'overlay');
			height=$('#simplemodal-container').css('height').substr(0,3)-0;
		});
	});

    }});
    return false;
 });

 $('.questions').click(function (e) {
    $('#basic-modal-content-questions').modal({
        onOpen: function (dialog) {
  dialog.overlay.fadeIn('slow', function () {
    dialog.container.slideDown('slow', function () {
      dialog.data.fadeIn('slow');

                        $('.numeric').numeric();
                        validateFields('form_questions', '/_ajax/questions.php', 'output_modal', 'overlay');
      height=$('#simplemodal-container').css('height').substr(0,3)-0;
    });
  });

    }});
    return false;
 });

 $('.arenda').click(function (e) {
    $('#basic-modal-content-arenda').modal({
        onOpen: function (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.slideDown('slow', function () {
			dialog.data.fadeIn('slow');

                        $('.numeric').numeric();
		        validateFields('form_arendaDGU_modal', '/_ajax/arendaDGU.php', 'output_modal', 'overlay');
			height=$('#simplemodal-container').css('height').substr(0,3)-0;
		});
	});
    }});
    return false;
 });


 $('.teh').click(function (e) {
    $('#basic-modal-content-teh').modal({
        onOpen: function (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.slideDown('slow', function () {
			dialog.data.fadeIn('slow');

                        $('.numeric').numeric();
		        validateFields('form_tehDGU_modal', '/_ajax/tehDGU.php', 'output_modal', 'overlay');
			height=$('#simplemodal-container').css('height').substr(0,3)-0;
		});
	});
    }});
    return false;
 });

  $('.numeric').numeric();
  validateFields('form_arendaDGU', '/_ajax/arendaDGU.php', 'output', 'overlay');
  validateFields('form_tehDGU', '/_ajax/tehDGU.php', 'output', 'overlay');
  validateFields('form_order', '/_ajax/order.php', 'output', 'overlay');
  validateFields('form_contact', '/_ajax/contact.php', 'output', 'overlay');

 });

var submit_back_url = false;

function validateFields(form_name, url, target, classBlock) {

  $.validator.addMethod('validName', function (value) {
      var result = true;
      var iChars = "!@#$%^&*()+=[]\\\';,./{}|\":<>?";
      for (var i = 0; i < value.length; i++) {
          if (iChars.indexOf(value.charAt(i)) != -1) {
              return false;
          }
      }
      return result;
  }, '');

  var options = { 
    target: "#" + form_name + " #"+target,
    url: url,
    timeout: 25000, 
    beforeSubmit: function() {
	$('.'+classBlock).removeClass('hidden');
    },
    success: function() {
	if(submit_back_url) location.href=submit_back_url;
        $('#'+form_name+' #aj_image').load('/_ajax/captcha.php',{},function(){
           $('.'+classBlock).addClass('hidden');
           $('#captcha_word').attr('value','');
        });
	$('#simplemodal-container').css('height', height + 30 + 'px');
    }
  };

  $("#"+form_name).validate({
     submitHandler: function(form) {
  	    $(form).ajaxSubmit(options);
    },  
    focusInvalid: false,
    focusCleanup: true,
    rules: {
      product: {
        required: true
      },
      org: {
         required: true,
         validName: true,
         minlength: 4,
         maxlength: 50
      },
      name: {
         required: true,
         validName: true,
         minlength: 4,
         maxlength: 50
      },
      message: {
         required: true,
         minlength: 4,
         maxlength: 500
      },
      address: {
         required: true,
         minlength: 4,
         maxlength: 25
      },
      email: {
         required: true,
         email: true
      },             
      srok: {
         required: true
      },        
      moshnost: {
         required: true
      },      
      type: {
         required: true
      },          
      captcha_word: {
         required: true
      },        
      phone: {
         required: true
      }      
    },
    messages: {
      product: {
        required: "Введите наименование товара"
      },
      message: {
        required: "Введите ваше сообщение",
        minlength: "Минимум 4 символа ",
        maxlength: "Максимум 500 символов "
      },
      name: {
        required: "Введите контактное лицо",
        validName: "Символы !@#$%^&*()+=[]\\\';,./{}|\":<>? запрещены.",
        minlength: "Минимум 4 символа ",
        maxlength: "Максимум 50 символов "
      },
      srok: {
        required: "Введите срок аренды"
      },
      moshnost: {
        required: "Мощность, кВт"
      },
      type: {
        required: "Тип оборудования"
      },
      org: {
        required: "Введите название организации",
        validName: "Символы !@#$%^&*()+=[]\\\';,./{}|\":<>? запрещены.",
        minlength: "Минимум 4 символа ",
        maxlength: "Максимум 50 символов "
      },
      address: {
        required: "Введите адрес объекта",
        validName: "Символы !@#$%^&*()+=[]\\\';,./{}|\":<>? запрещены.",
        minlength: "Минимум 4 символа ",
        maxlength: "Максимум 25 символов "
      },
      captcha_word: {
        required: "Введите текст с картинки"
      },
      phone: {
        required: "Введите контактный телефон"
      },
      email: {
        required: "Введите адрес ящика",
        email: "Введите корректный адрес"
      }
    },
    errorPlacement: function(error, element) {
        $('#'+target).html('');        
        '<br>'+error.appendTo( element.parent().parent().parent().parent().parent().find("label[for='" + element.attr("name") + "']").find("span") );
    }

  });


}
