$(document).ready(function() {
  $('input[name=senderName]').focus();
  $('input[name=cancelEmail]').click(function() {
    history.back(-1);
  });
  submitAction();
	
	refreshCaptcha();
});

function submitAction() {
  // Prevent the enter key from being used.
  $(document.forms[0]).keypress(function(e) {
    if (e.keyCode == 13) {
      return false;
    }
  });
  $(document.forms.contactForm).submit(function() {
    if (!validateForm()) {
      return false;
    }
  });
}

function refreshCaptcha() {
	$("#refresh_captcha").click(function() {
		$("#captcha")[0].src = 'experimental/securimage/securimage_show.php?sid='+Math.random();
		return false;
	});
}

function validateForm() {
  var error = false;
  var email = $('input[name=senderEmail]').val();
  if (email == '') {
    alert('Your e-mail address cannot be blank.');
    error = true;
  } else if (!isValidEmail(email)) {
    alert('The e-mail address "'+email+'" is invalid.');
    error = true;
  }
  return (error) ? false : true;
}

function isValidEmail(email) {
  if (typeof email == 'undefined') return false;
  if (email.match(/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/) == null) {
    return false;
  } else {
    return true;
  }
}

function refreshCAPTCHA(link) {
  var url = link.href;
  $(link).load(url);
  return false;
}


