// Contact form javascript
var ContactForm = {};
ContactForm = {
  settings: {

    thankyou_url: 'thankyou.html' 		// @NOTE: Provide the path to your "thank you" html file here.
    
    
    
  },
  cancel: function() {
    if (Lightbox.visible()) {
      Lightbox.close();
    } else {
      window.history.back();
    }
    return false;
  },
  submit: function(valid) {
    if (valid) {
      
      if (Lightbox.visible()) {
        //ContactForm.post_to_server();
        //ContactForm.load_thankyou();
        return true;
      } else {
        return true;
      }
      
    } else {
      return false;
    }
  },
  show_error_message: function() {
    $('#contact-form p.buttons span#form-error').show();
  },
  show_email_error_message: function() {
    $('#contact-form p.buttons span#email-error').show();
  },
  hide_email_error_message: function() {
	$('#contact-form p.buttons span#email-error').hide();
  },
  load_thankyou: function() {
    Lightbox.pull(ContactForm.settings.thankyou_url, { height: 190, width: 420 })
  },
  post_to_server: function() {
    $.post($('form#contact-form').attr('action'), $('form#contact-form').serializeArray());
  },
  setup: function() {
    
   // var cancel_button_html = "<a href=\"#\" onclick=\"return ContactForm.cancel()\" class=\"button close\"><span>&nbsp;</span></a>";
    var error_html = "<span class=\"error-message\" id=\"form-error\" style=\"display:none\">Please fill out required fields.</span><br>";
	var error_email = "<span class=\"error-message\" id=\"email-error\" style=\"display:none\">Please check email format.</span>"

    $('#contact-form p.buttons').prepend(error_email)
    $('#contact-form p.buttons').prepend(error_html)
	

    $('#contact-form p.input input, #contact-form p.input textarea').focus(function() {
      $(this).removeClass('error');
    });

    $('#contact-form').submit(function() {
      var valid = true;

      $('#contact-form p.input input, #contact-form p.input textarea, #contact-form p.input select').each(function() {
        $this = $(this);
        if ($this.val() == '' && $this.attr('rel') == 'required' && $this.attr('id') != 'email-address') {
          valid = false;
          $this.addClass('error');
		  $this.prev("label").addClass('error');
        } else if ($this.attr('id') == 'E-Mail') {
			if (isEmail($this.val()) == false) {
				valid = false;
				$this.addClass('error');
				$this.prev("label").addClass('error');
				ContactForm.show_email_error_message();
			} else {
				ContactForm.hide_email_error_message();
				$this.removeClass('error');
				$this.prev("label").removeClass('error');
			}
		} else {
			$this.removeClass('error');
			$this.prev("label").removeClass('error');
		}
		if (valid == false) {
			ContactForm.show_error_message();
		}
      });
      
      return valid; //ContactForm.submit(valid);
    });
    
  },
  setup_thankyou: function() {
    $('#ok-button').click(function() {
      if (Lightbox.visible()) {
        Lightbox.close();
        return false;
      } else {
        return true;
      } // end of if
    }); // end of $
  } // end of setup_thankyou
};

function isEmail(sEmail) {
    if (sEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		if(sEmail.search(/^(\s*)$/) < 1) {
			if ((sEmail.length)-(sEmail.lastIndexOf('.')) >= 3) {
		        return true;
			} else {
				return false;
			}
		}
	} else {
        return false;
	}
}

function isPhone(sPhone) {
	if (sPhone.length < 10) return false;

		var sTrm = '';
		var i = 0;

		while (i < sPhone.length) {
			if (isNaN(Number(sPhone.substr(i,1))) == false && sPhone.substr(i,1) != " ") {
				sTrm += sPhone.substr(i,1);
			}
			i++;
		}

		if (sTrm.length < 10) { 
			return false;
		} 
		return true;
	
}
