
// ========================================================================================
// Toon boom Affiliates: Javascipt functions
// ========================================================================================
function request_sendContactForm (form_id) {
  $('sendForm').setAttribute('disabled','true');
	new Ajax.Request('send.php?mode=sndF&form=' + form_id, {
  	method:'post',
  	parameters: $(form_id).serialize(true),
  	onSuccess: function(transport){
  		alert("Your request has been sent. Thank you.");
    	$('sendForm').removeAttribute('disabled');
  	},
  	onFailure: function() {  	}
  });
}

function validateTheseFields() {
  if ($('contactSubject')) {
    $('contactSubject').removeClassName('errState');
    if (isEmpty($F('contactSubject'))) { errMsg("Please enter a Subject", "contactSubject"); return false; }
  }

  if ($('contactName')) {
    $('contactName').removeClassName('errState');
    if (isEmpty($F('contactName'))) { errMsg("Please enter your name", "contactName"); return false; }
  }

  if ($('email')) {
    $('email').removeClassName('errState');
    if ( isEmpty($F('email')) || !isEmail($F('email')) ) { errMsg("Please enter a valid Email Address", "email"); return false; }
  }

  if ($('comments')) {
    $('comments').removeClassName('errState');
    if (isEmpty($F('comments'))) { errMsg("Please enter your comments", "comments"); return false; }
  }

  return true;
}

function errMsg (theMessage, formField) {
  alert(theMessage);
  $(formField).addClassName('errState');
  setTimeout("$('" + formField + "').focus();",1);
}

function isEmpty(myString) {
  if (myString == "" || myString == null) return true;
  else return false;
}

function isEmail(email) {
  // check email address structure
	if (email.indexOf("@")  == -1 || email.indexOf(".")  == -1 ||          // these 2 must be in address
	    email.indexOf(" ")  >=  0 || email.indexOf("\\") >=  0 ||          // these must not be in address
	    email.indexOf("..") >=  0 || email.indexOf(".@") >=  0 ||
	    email.indexOf("@.") >=  0 ) return false;

  var parts = email.split("@");             // create array from email address: local_part@domain.qualifier(s)
  if (parts.length > 2) return false;       // should only be one @

  var localPart = parts[0];                 // local_part
  var domainPart = parts[1];                // domain.qualifier
  if (localPart.length == 0 || domainPart.length == 0) return false;

  var domain = domainPart.split(".");       // split domain part
  if (domain.length < 2) return false;

  var domainName = domain[0];
  var qualifier = domain[domain.length-1];  // get the last qualifier
  if (domainName.length == 0 || qualifier.length == 0) return false;

  return true;
}
