jQuery(document).ready(function(){
    $('#contactForm').submit(function(){


  function Box(){
      this.error = function(el){$(el + 'Error').slideDown('slow');};
      this.hide = function(el){$(el).slideUp('slow');};
      this.sucess = function(){$('#sucess').slideDown('slow');}
      this.fail = function(){$('#error').slideDown('slow')}
    }

  function Validate(){
        this.errors = 0;
        this.Trim = function(str){
            return str.replace(/^\s+|\s+$/g,"");
        }
        this.email = function(str){
            return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str));
        }
		this.number = function(str){
            return (/^\+?[0-9][-]*[\s]*/.test(str));
        }
        this.isEmpty = function(str){
            return this.Trim(str).length == 0;
        }
		this.notEmpty = function(str){
            return this.Trim(str).length > 0;
        }
		this.validzip = function(str){
            return this.Trim(str).length > 5;
        }
		this.validzip1 = function(str){
            return this.Trim(str).length < 5;
        }
		this.validtel = function(str){
            return this.Trim(str).length > 20;
        }
		this.validtel1 = function(str){
            return this.Trim(str).length < 10;
        }
    }

  function Element(){
      
      this.name = $('#name').val();
	  this.lastName = $('#lastName').val();
	  this.add1 = $('#add1').val();
	  this.city = $('#city').val();
	  this.state = $('#state').val();
	  this.zip = $('#zip').val();
      this.email = $('#email').val();
	  /*this.cemail = $('#cemail').val();*/
	  this.tel = $('#tel').val();
      /* clear form on sucess */
      this.clean = function(){
          $('#name').val('');
          $('#lastName').val('');
		  $('#add1').val('');
		  $('#city').val('');
		  $('#state').val('');
		  $('#zip').val('');
          $('#email').val('');
		  $('#tel').val('');
		 /* $('#cemail').val('');*/
      }
  }

  var Validate = new Validate();
  var Box = new Box();
  var Element = new Element;

  Box.hide('.warning,.error,.sucess');
     var errors = 0;
     var action = $(this).attr('action');

         if(!Validate.email(Element.email)){
         Box.error('#email')
         errors++

     }
	 
	/* if(Element.email!=Element.cemail){
         Box.error('#cemail')
         errors++

     }*/
	 
        if(Validate.isEmpty(Element.name)){
            Box.error('#name');
            errors++
        } 
        if(Validate.isEmpty(Element.lastName)){
         Box.error('#lastName');
            errors++
        }
		
		if(Validate.isEmpty(Element.add1)){
         Box.error('#add1');
            errors++
        }
		
		if(Validate.isEmpty(Element.city)){
         Box.error('#city');
            errors++
        }
		
		if(Validate.isEmpty(Element.state)){
         Box.error('#state');
            errors++
        }
		
		if(Validate.isEmpty(Element.zip)){
         Box.error('#zip');
            errors++
        }
		if(!Validate.number(Element.zip)){
         Box.error('#zip');
         errors++

     }
		
		if(Validate.validzip(Element.zip) || Validate.validzip1(Element.zip)){
         Box.error('#zip');
            errors++
        }
		
		
	 if((Validate.notEmpty(Element.tel)) && (Validate.validtel(Element.tel) || Validate.validtel1(Element.tel))){
		 
         Box.error('#tel');
         errors++


     }
		
        if(!errors){
            $('#ajax_loader').ajaxStart(function(){
                $(this).show();
                $('#send').hide();
            })
            $('#ajax_loader').ajaxStop(function(){
                $(this).hide();
                $('#send').show();
            })

        var data = {
            email: Element.email,
            name: Element.name,
            lastName: Element.lastName,
            add1: Element.add1,
			city: Element.city,
			state: Element.state,
			zip: Element.zip
        };

        var $ajaxCallBack = function(data){
            if(data != ''){
				
               // Box.sucess();
               // Element.clean();
				//alert("haha");
                }
            else 
                {
					//Box.fail(); alert("nana");
				}
				
        }
        $.post(action, data, $ajaxCallBack);
		return true;
        }
            return false;
    })
})
