/** The functions in this document are to accompany ajaxform.php **/
/******************************************/
/* DIGITAL SURGEONS AJAX CONTACT FORM     */
/* See comment in php file for more info. */
/******************************************/

function isEmpty(strng, fieldname) {
  if (fieldname=="") {
	  fieldname="Name"
  }
  if (strng == "Name" || strng == "Email" || strng == "Your Website Address") {
    strng = "";
  }
  var error = "";
  if (strng.length == 0) {
     error = "- " + fieldname + " is a required field.\n";
  }
  return error;	  
}

function checkEmail (strng) {
  var error="";
  if (strng == "") {
    error = "- Email is a required field.\n";
  }
  var emailFilter=/^.+@.+\..{2,3}$/;
  if (!(emailFilter.test(strng))) { 
    error = "- Please enter a valid email address.\n";
  } else {
    //test email for illegal characters
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
    if (strng.match(illegalChars)) {
      error = "- The email address contains illegal characters.\n";
    }
  }
  return error;    
}

function checkPhone (strng) {
  var error = "";
  if (strng == "") {
    error = "- Phone is a required field.\n";
  }
  var stripped = strng.replace(/[\(\)\.\+\/\-\ ]/g, ''); //strip out acceptable non-numeric characters
  if (isNaN(parseInt(stripped))) {
    error = "- The phone number contains illegal characters.\n";
  }
  if (!(stripped.length == 10)) {
    error = "- Please make sure the phone number is 10 digits.\n";
  } 
  return error;
}


function checkForm() {
    var why = "";
    why += isEmpty(document.getElementById('name').value, "Name");     		 // Is name blank?
    why += checkEmail(document.getElementById('email').value); 				 // Is email blank or invalid?
    why += isEmpty(document.getElementById('url').value, "Website Address"); // Is name blank?
	why += checkPhone(document.getElementById('phone').value); 				 // Is phone blank or invalid?
    if (why != "") {  // If there's an error,
       alert("Your submission has the following errors:\n\n"+why);    // Tell the user whats wrong,
       return false;  // And halt submission.
    }
return true;          // Otherwise allow submission to proceed.
}

function removeText(input, type) {
	if (input.value == "Name" && type == "name") {
		input.value = "";
	} else if (input.value == "Email" && type == "email") {
		input.value = "";
	} else if (input.value == "Your Website Address" && type == "url") {
		input.value = "http://";
	} else if (input.value == "Phone Number" && type == "phone") {
		input.value = "";
	}
}

function addText(input, type) {
	if (input.value == "" && type == "name") {
		input.value = "Name";
	} else if (input.value == "" && type == "email") {
		input.value = "Email";
	} else if ((input.value == "" || input.value == "http://") && type == "url") {
		input.value = "Your Website Address";
	} else if (input.value == "" && type == "phone") {
		input.value = "Phone Number";
	}
}

function hideErrorDiv() {
	document.getElementById('errortext').style.display = "none";
	document.getElementById('errortext').style.visibility = "hidden";
	document.getElementById('errortextouter').style.display = "none";
	document.getElementById('errortextouter').style.visibility = "hidden";
	document.getElementById('errortextinner').style.display = "none";
	document.getElementById('errortextinner').style.visibility = "hidden";
	document.getElementById('errorimg').style.display = "none";
	document.getElementById('errorimg').style.visibility = "hidden";
}