// form validation function //
var request = false;
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = false;
			}
		}
	}
	if (!request)
		alert("Error initializing XMLHttpRequest!");
var captchaval;
function validate_captcha(){
	var recaptcha_challenge_field = document.getElementById("recaptcha_challenge_field").value;
	var recaptcha_response_field = document.getElementById("recaptcha_response_field").value;	
	var url = "validatecaptcha.php";
	request.open("POST", url, false);
	request.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	request.onreadystatechange = updatePage;
	request.send('recaptcha_challenge_field=' + recaptcha_challenge_field +'&'+ 'recaptcha_response_field='+recaptcha_response_field);
	Recaptcha.reload();
	
}
function updatePage() {
	if (request.readyState == 4) {
		if (request.status == 200) {
			var response = request.responseXML;
			var resp = response.getElementsByTagName('response');
			captchaval=resp[0].firstChild.nodeValue;
		}
	}
}
	
function validate_ptc(form) {
  var f_name = form.f_name.value;
  var l_name = form.l_name.value;
  var email = form.email.value;
  var phone = form.phone.value;
  
 
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var phoneRegex = /^[\d]{3}[\s-]?[\d]{3}[\s-]?[\d]{4}$/;///^(?:\(\d{3}\)|\d{3})?[\s-]?[\d]{3}[\s-]?[\d]{4}$/
  var zipRegex = /^\d{5}(-\d{4})?$/;
  if(f_name == "") {
    inlineMsg('f_name','You must enter your first name.',2);
    return false;
  }
  if(!f_name.match(nameRegex)) {
    inlineMsg('f_name','You have entered an invalid first name.',2);
    return false;
  }
   if(l_name == "") {
    inlineMsg('l_name','You must enter your last name.',2);
    return false;
  }
  if(!l_name.match(nameRegex)) {
    inlineMsg('l_name','You have entered an invalid last name.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  if(phone == "") {
	    inlineMsg('phone','You must enter your Contact Phone.',2);
	    return false;
	  }
	  if(!phone.match(phoneRegex)) {
	    inlineMsg('phone','You have entered an invalid Contact Phone.',2);
	    return false;
	  }
 
  return true;
}

function validate_contact_form(form) {
	
	  var f_name = form.f_name.value;
	  var l_name = form.l_name.value;
	  var email = form.email.value;
	  var phone = form.phone.value;
	  var issue = form.issue.value;
	 
	  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	  var phoneRegex = /^(?:\(\d{3}\)|\d{3})?[\s-]?[\d]{3}[\s-]?[\d]{4}$/;
	  var zipRegex = /^\d{5}(-\d{4})?$/;
	if(f_name == "") {
	    inlineMsg('f_name','You must enter your first name.',2);
	    return false;
	  }
	  if(!f_name.match(nameRegex)) {
	    inlineMsg('f_name','You have entered an invalid first name.',2);
	    return false;
	  }
	   if(l_name == "") {
	    inlineMsg('l_name','You must enter your last name.',2);
	    return false;
	  }
	  if(!l_name.match(nameRegex)) {
	    inlineMsg('l_name','You have entered an invalid last name.',2);
	    return false;
	  }
	  if(email == "") {
	    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
	    return false;
	  }
	  if(!email.match(emailRegex)) {
	    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
	    return false;
	  }
	  if(phone == "") {
		    inlineMsg('phone','You must enter your Contact Phone.',2);
		    return false;
		  }
		  if(!phone.match(phoneRegex)) {
		    inlineMsg('phone','You have entered an invalid Contact Phone.',2);
		    return false;
		  }
		  
		  if(issue == "") {
			    inlineMsg('issue','You must enter message.',2);
			    return false;
			  }
		  if ((issue.search(/http/i)>=0) || (issue.search(/https/i)>=0) || (issue.search(/www/i)>=0) || (issue.search(/href/i)>=0)){inlineMsg('issue','<strong>Error</strong><br />You should not enter weblinks!',2);return false;}  
	 validate_captcha();	  
	 if(captchaval == "notvalid") {
		    inlineMsg('recaptcha_response_field','Wrong captcha.',2);
		    return false;
		  }
	  return true;
	}

function validate_rm_sp(form) {
  var f_name = form.f_name.value;
  var l_name = form.l_name.value;
  var email = form.email.value;
  var phone = form.phone.value;
 
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var phoneRegex = /^(?:\(\d{3}\)|\d{3})?[\s-]?[\d]{3}[\s-]?[\d]{4}$/;
  var zipRegex = /^\d{5}(-\d{4})?$/;
  if(f_name == "") {
    inlineMsg('f_name','You must enter your first name.',2);
    return false;
  }
  if(!f_name.match(nameRegex)) {
    inlineMsg('f_name','You have entered an invalid first name.',2);
    return false;
  }
   if(l_name == "") {
    inlineMsg('l_name','You must enter your last name.',2);
    return false;
  }
  if(!l_name.match(nameRegex)) {
    inlineMsg('l_name','You have entered an invalid last name.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  if(phone == "") {
	    inlineMsg('phone','You must enter your Contact Phone.',2);
	    return false;
	  }
	  if(!phone.match(phoneRegex)) {
	    inlineMsg('phone','You have entered an invalid Contact Phone.',2);
	    return false;
	  }
 
 
  return true;
}

function validate_webinar(form) {
  var f_name = form.f_name.value;
  var l_name = form.l_name.value;
  var email = form.email.value;
  var phone = form.phone.value;
  var day = form.day.value;
  var state = form.state.value;
  var zip = form.zip.value;
 
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var phoneRegex = /^(?:\(\d{3}\)|\d{3})?[\s-]?[\d]{3}[\s-]?[\d]{4}$/;
  var zipRegex = /^\d{5}(-\d{4})?$/;
  if(f_name == "") {
    inlineMsg('f_name','You must enter your first name.',2);
    return false;
  }
  if(!f_name.match(nameRegex)) {
    inlineMsg('f_name','You have entered an invalid first name.',2);
    return false;
  }
   if(l_name == "") {
    inlineMsg('l_name','You must enter your last name.',2);
    return false;
  }
  if(!l_name.match(nameRegex)) {
    inlineMsg('l_name','You have entered an invalid last name.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  if(phone == "") {
	    inlineMsg('phone','You must enter your Contact Phone.',2);
	    return false;
	  }
	  if(!phone.match(phoneRegex)) {
	    inlineMsg('phone','You have entered an invalid Contact Phone.',2);
	    return false;
	  }
  if(day == "") {
    inlineMsg('day','<strong>Error</strong><br />You must select your day.',2);
    return false;
  }
  if(state == "") {
    inlineMsg('state','<strong>Error</strong><br />You must select your State.',2);
    return false;
  }
   if(zip == "") {
    inlineMsg('zip','<strong>Error</strong><br />You must enter your Zip Code.',2);
    return false;
  }
  if(!zip.match(zipRegex)) {
    inlineMsg('zip','<strong>Error</strong><br />You have entered an invalid Zip Code.',2);
    return false;
  }
 
  return true;
}

function validate_app(form) {
	  var f_name = form.f_name.value;
	  var l_name = form.l_name.value;
	  var st_address = form.st_address.value;
	  var city = form.city.value;
	  var state = form.state.value;
	  var zip = form.zip.value;
	  var email = form.email.value;
	  var h_phone = form.h_phone.value; 

	  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	  var phoneRegex = /^(?:\(\d{3}\)|\d{3})?[\s-]?[\d]{3}[\s-]?[\d]{4}$/;
	  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	  var zipRegex = /^\d{5}(-\d{4})?$/;
	  if(f_name == "") {
	    inlineMsg('f_name','You must enter your first name.',2);
	    return false;
	  }
	  if(!f_name.match(nameRegex)) {
	    inlineMsg('f_name','You have entered an invalid first name.',2);
	    return false;
	  }
	   if(l_name == "") {
	    inlineMsg('l_name','You must enter your last name.',2);
	    return false;
	  }
	  if(!l_name.match(nameRegex)) {
	    inlineMsg('l_name','You have entered an invalid last name.',2);
	    return false;
	  }
	  if(st_address == "") {
		    inlineMsg('st_address','<strong>Error</strong><br />You must enter your street address.',2);
		    return false;
	  }
	  if(city == "") {
		    inlineMsg('city','<strong>Error</strong><br />You must enter your city.',2);
		    return false;
	  }
	  if(state == "") {
	    inlineMsg('state','<strong>Error</strong><br />You must select your State.',2);
	    return false;
	  }
	   if(zip == "") {
	    inlineMsg('zip','<strong>Error</strong><br />You must enter your Zip Code.',2);
	    return false;
	  }
	  if(!zip.match(zipRegex)) {
	    inlineMsg('zip','<strong>Error</strong><br />You have entered an invalid Zip Code.',2);
	    return false;
	  }
	  if(email == "") {
		    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
		    return false;
		  }
	  if(!email.match(emailRegex)) {
		    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
		    return false;
	  }
	  if(h_phone == "") {
		    inlineMsg('h_phone','You must enter your home phone number.',2);
		    return false;
		  }
		  if(!h_phone.match(phoneRegex)) {
		    inlineMsg('h_phone','You have entered an invalid phone number.',2);
		    return false;
		  }
		validate_captcha();	  
		if(captchaval == "notvalid") {
			inlineMsg('recaptcha_response_field','Wrong captcha.',2);
			return false;
		}
	 
	  return true;
	}

function validate_testimonial(form) {
	  var email = form.email.value;
	  
	 
	  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	  if(email == "") {
	    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
	    return false;
	  }
	  if(!email.match(emailRegex)) {
	    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
	    return false;
	  }
	 
	  return true;
	}
	
function validate_employment_form(form) {
	 
  var f_name = form.f_name.value;
  var l_name = form.l_name.value;
  var email = form.email.value;
  var phone = form.phone.value;
  var address = form.address.value;
  var city = form.city.value;
  var state = form.state.value;
  var zip = form.zip.value;
  var most_recent_employer = form.most_recent_employer.value;
  var mre_from_date = form.mre_from_date.value;
  var mre_to_date = form.mre_to_date.value;
  var mre_address = form.mre_address.value;
  var mre_city = form.mre_city.value;
  var mre_state = form.mre_state.value;
  var mre_zip = form.mre_zip.value;
  var mre_supervisor = form.mre_supervisor.value;
  var mre_supervisor_ph = form.mre_supervisor_ph.value;
  var mre_duties = form.mre_duties.value;
  var prev_employer = form.prev_employer.value;
  var prevemp_from_date = form.prevemp_from_date.value;
  var prevemp_to_date = form.prevemp_to_date.value;
  var prevemp_address = form.prevemp_address.value;
  var prevemp_city = form.prevemp_city.value;
  var prevemp_state = form.prevemp_state.value;
  var prevemp_zip = form.prevemp_zip.value;
  var prevemp_supervisor = form.prevemp_supervisor.value;
  var prevemp_supervisor_ph = form.prevemp_supervisor_ph.value;
  var prevemp_duties = form.prevemp_duties.value;
  var education_history = form.education_history.value;
  var qualifications = form.qualifications.value;
	 
	  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	  var phoneRegex = /^(?:\(\d{3}\)|\d{3})?[\s-]?[\d]{3}[\s-]?[\d]{4}$/;
	  var zipRegex = /^\d{5}(-\d{4})?$/;
/*	
  if(f_name == "") {
    inlineMsg('f_name','You must enter your first name.',2);
    return false;
  }
  if(!f_name.match(nameRegex)) {
    inlineMsg('f_name','You have entered an invalid first name.',2);
    return false;
  }
   if(l_name == "") {
    inlineMsg('l_name','You must enter your last name.',2);
    return false;
  }
  if(!l_name.match(nameRegex)) {
    inlineMsg('l_name','You have entered an invalid last name.',2);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Error</strong><br />You must enter your email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
    return false;
  }
  if(phone == "") {
	    inlineMsg('phone','You must enter your Contact Phone.',2);
	    return false;
	  }
	  if(!phone.match(phoneRegex)) {
	    inlineMsg('phone','You have entered an invalid Contact Phone.',2);
	    return false;
	  }
  if(address == "") {
    inlineMsg('address','You must enter your address.',2);
    return false;
  }
  if(city == "") {
    inlineMsg('city','You must enter your city.',2);
    return false;
  }
  if(state == "") {
    inlineMsg('state','You must select your state.',2);
    return false;
  }  
  if(zip == "") {
    inlineMsg('zip','You must enter your zip code.',2);
    return false;
  }
  if(!zip.match(zipRegex)) {
    inlineMsg('zip','<strong>Error</strong><br />You have entered an invalid zip code.',2);
    return false;
  }  
  if(most_recent_employer == "") {
    inlineMsg('most_recent_employer','This is required field.',2);
    return false;
  }
  if(mre_from_date == "") {
    inlineMsg('mre_from_date','This is required field.',2);
    return false;
  }
  if(mre_to_date == "") {
    inlineMsg('mre_to_date','This is required field.',2);
    return false;
  }
  if(mre_address == "") {
    inlineMsg('mre_address','This is required field.',2);
    return false;
  }
  if(mre_city == "") {
    inlineMsg('mre_city','This is required field.',2);
    return false;
  }
  if(mre_state == "") {
    inlineMsg('mre_state','This is required field.',2);
    return false;
  }  
  if(mre_zip == "") {
    inlineMsg('mre_zip','This is required field.',2);
    return false;
  }
  if(!mre_zip.match(zipRegex)) {
    inlineMsg('mre_zip','<strong>Error</strong><br />You have entered an invalid zip code.',2);
    return false;
  }
  if(mre_supervisor == "") {
    inlineMsg('mre_supervisor','This is required field.',2);
    return false;
  }  
  if(mre_supervisor_ph == "") {
    inlineMsg('mre_supervisor_ph','This is required field.',2);
    return false;
  } 
  if(!mre_supervisor_ph.match(phoneRegex)) {
    inlineMsg('mre_supervisor_ph','<strong>Error</strong><br />You have entered an invalid phone number.',2);
    return false;
  }
  if(mre_duties == "") {
    inlineMsg('mre_duties','This is required field.',2);
    return false;
  } 
   if(prev_employer == "") {
    inlineMsg('prev_employer','This is required field.',2);
    return false;
  }
  if(prevemp_from_date == "") {
    inlineMsg('prevemp_from_date','This is required field.',2);
    return false;
  }
  if(prevemp_to_date == "") {
    inlineMsg('prevemp_to_date','This is required field.',2);
    return false;
  }
  if(prevemp_address == "") {
    inlineMsg('prevemp_address','This is required field.',2);
    return false;
  }
  if(prevemp_city == "") {
    inlineMsg('prevemp_city','This is required field.',2);
    return false;
  }
  if(prevemp_state == "") {
    inlineMsg('prevemp_state','This is required field.',2);
    return false;
  }  
  if(prevemp_zip == "") {
    inlineMsg('prevemp_zip','This is required field.',2);
    return false;
  }
  if(!prevemp_zip.match(zipRegex)) {
    inlineMsg('prevemp_zip','<strong>Error</strong><br />You have entered an invalid zip code.',2);
    return false;
  }
   if(prevemp_supervisor == "") {
    inlineMsg('prevemp_supervisor','This is required field.',2);
    return false;
  }  
  if(prevemp_supervisor_ph == "") {
    inlineMsg('prevemp_supervisor_ph','This is required field.',2);
    return false;
  } 
  if(!prevemp_supervisor_ph.match(phoneRegex)) {
    inlineMsg('prevemp_supervisor_ph','<strong>Error</strong><br />You have entered an invalid phone number.',2);
    return false;
  }
  if(prevemp_duties == "") {
    inlineMsg('prevemp_duties','This is required field.',2);
    return false;
  }
  if(education_history == "") {
    inlineMsg('education_history','This is required field.',2);
    return false;
  }*/
  if(qualifications == "") {
    inlineMsg('qualifications','This is required field.',2);
    return false;
  }  
	validate_captcha();	  
	if(captchaval == "notvalid") {
		inlineMsg('recaptcha_response_field','Wrong captcha.',2);
		return false;
	}	 
	  return true;
	}	
	
	

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //


 function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}

 var checkflag = false;
function check_agreement() {
 var myelement = document.getElementById("agreement");
 if (checkflag == false)  {
  
    document.getElementById('send').disabled = false;
   checkflag = true;
 
 }
 else {
  document.getElementById('send').disabled = true;
   checkflag = false;
 }
}
$(document).ready(function() {$('textarea[maxlength]').keyup(function(){
//get the limit from maxlength attribute
var limit = parseInt($(this).attr('maxlength'));
//get the current text inside the textarea
var text = $(this).val();
//count the number of characters in the text
var chars = text.length;
var charsleft=limit-chars;
if (charsleft < 1){charsleft='No';}
$(".remaining").html((charsleft) + " Letters Remaining.");
//check if there are more characters then allowed
if(chars > limit){//and if there are use substr to get the text before the limit
var new_text = text.substr(0, limit);//and change the current text with the new text
$(this).val(new_text);}
});
});
