function validate(form) {
	var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
	var addressRegex = /^[a-zA-Z0-9]+(([\'\,\.\- ][a-zA-Z0-9 ])?[a-zA-Z0-9]*)*$/;
	//var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
	  
	if (document.all||document.getElementById) {
		if ((form.name.value.length < 2) || (!form.name.value.match(nameRegex))) {
			inlineMsg('name','You have entered an invalid name.', 2);
			return false;
		}

		if (!check_email(form.email.value)) {
			inlineMsg('email','You have entered an invalid email address.', 2);
			return false;
		}

		/*if ((form.phone.value.length < 7) || (!isNumeric(form.phone.value))) {
			inlineMsg('phone','You have entered an invalid phone number.', 2);
			return false;
		}*/

		if ((form.address1.value.length < 2) || (!form.address1.value.match(addressRegex))) {
			inlineMsg('address1','You have entered an invalid billing address.', 2);
			return false;
		}
		
		if ((form.city.value.length < 2) || (!form.city.value.match(nameRegex))) {
			inlineMsg('city','You have entered an invalid city.', 2);
			return false;
		}

		if (form.state.value.length < 2) {
			inlineMsg('state','Please select your state.', 2);
			return false;
		}

		if (!isValidZipCode(form.zip.value)) {
			inlineMsg('zip','You must enter a valid five-digit zip code.', 2);
			return false;
		}
		
		if ((form.chargetotal.value.length < 1) || (!isNumeric(form.chargetotal.value))) {
			inlineMsg('chargetotal','You have entered an invalid donation amount.', 2);
			return false;
		}
		
		/*if (!validateCreditCard(form.cardnumber.value)) {
			inlineMsg('cardnumber','You must enter a valid credit card number.', 2);
			return false;
		}*/
		
		if ((form.cardexpmonth.value.length < 1) || (form.cardexpmonth.value.length > 2) || (!isNumeric(form.cardexpmonth.value))) {
			inlineMsg('cardexpmonth','You have entered an invalid card expiration month.', 2);
			return false;
		}
		
		if ((form.cardexpyear.value.length < 1) || (form.cardexpyear.value.length > 2) || (!isNumeric(form.cardexpyear.value))) {
			inlineMsg('cardexpyear','You have entered an invalid card expiration month.', 2);
			return false;
		}
		
		if ((form.cvmvalue.value.length < 3) || (form.cvmvalue.value.length > 3) || (!isNumeric(form.cvmvalue.value))) {
			inlineMsg('cvmvalue','You have entered an card validation code. Please click the help icon for more information on cvm.', 2);
			return false;
		}
	
		return true;
	}
}

// ===========================================================
function check_email(e) {
  ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

  for(i=0; i < e.length ;i++){
    if(ok.indexOf(e.charAt(i))<0){ 
      return (false);
    }	
  } 

  if (document.images) {
    re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!e.match(re) && e.match(re_two)) {
      return (-1);		
    } 
  }
}
// ===========================================================
function isNumeric(sText) {
   var ValidChars = "0123456789-$,./";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}
// ===========================================================
function isValidSSN(value) { 
    // I got this code from: http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256F6A0072B54C
    var re = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/; 
    if (!re.test(value)) { return false; } 
    var temp = value; 
    if (value.indexOf("-") != -1) { temp = (value.split("-")).join(""); } 
    if (value.indexOf(" ") != -1) { temp = (value.split(" ")).join(""); } 
    if (temp.substring(0, 3) == "000") { return false; } 
    if (temp.substring(3, 5) == "00") { return false; } 
    if (temp.substring(5, 9) == "0000") { return false; } 
    return true; 
}
// ===========================================================
function isValidZipCode(value) {
   // I got this code from: http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256F6B005294C2
   var re = /^\d{5}([\-]\d{4})?$/;
   return (re.test(value));
}
// ===========================================================
function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}
// ===========================================================
// Credit Card Validation Javascript
// http://javascript.about.com/library/blccard.htm
function validateCreditCard(s) {
var v = "0123456789";
var w = "";
for (var i=0; i < s.length; i++) {
x = s.charAt(i);
if (v.indexOf(x,0) != -1)
w += x;
}
var j = w.length / 2;
if (j < 6.5 || j > 8 || j == 7) return false;
var k = Math.floor(j);
var m = Math.ceil(j) - k;
var c = 0;
for (var i=0; i<k; i++) {
a = w.charAt(i*2+m) * 2;
c += a > 9 ? Math.floor(a/10 + a%10) : a;
}
for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
return (c%10 == 0);
}
// ===========================================================
function checkAtleastTwo() {
	var NewCount = 0

	if (document.getElementById('ApptDate'+1).checked) { NewCount = NewCount + 1 };
	if (document.getElementById('ApptDate'+2).checked) { NewCount = NewCount + 1 };
	if (document.getElementById('ApptDate'+3).checked) { NewCount = NewCount + 1 };
	if (document.getElementById('ApptDate'+4).checked) { NewCount = NewCount + 1 };
	if (document.getElementById('ApptDate'+5).checked) { NewCount = NewCount + 1 };
	return NewCount;
}



// =================================================================================
// 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 = "inlineValidator.gif"; 
}
