// form validation function //
function validate(form) {

  var digitRegex = /^\d+$/;
  
  var textRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  
  var phoneRegex = /^\(\d{3}\) ?\d{3}( |-)?\d{4}|^\d{3}( |-)?\d{3}( |-)?\d{4}$/;
  
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  
  var pswdRegex = /^.*(?=.{8,})(?=.*\d)(?=.*[a-zA-Z]).*$/;
  
  var multiEmailRegex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*$/;

//  var pswdRegex = /^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#%]).*$/;

var error = 0;
  
//DONATION VALIDATION

try{
  var firstname = form.rvar_first_name.value;
  
  if(firstname == "") {
    inlineMsg('rvar_first_name','You must enter your first name.',2);
	error = 1;
    return false;
  } 
}
catch(err)
{
}

try{
  var lastname = form.rvar_last_name.value;
  
  if(lastname == "") {
    inlineMsg('rvar_last_name','You must enter your last name.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var address = form.rvar_address1.value;
  
  if(address == "") {
    inlineMsg('rvar_address1','You must enter your address.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var city = form.rvar_city.value;

  if(city == "") {
    inlineMsg('rvar_city','You must enter your city.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}


try{
  var provStat = form.rvar_province_state.value;

  if(provStat == "") {
    inlineMsg('rvar_province_state','You must enter a Province/State.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}


try{
  var postalZip = form.rvar_postal_zip.value;

  if(postalZip == "") {
    inlineMsg('rvar_postal_zip','You must enter a Postal/Zip Code.',2);
	error = 1;
    return false;
  } 
}
catch(err)
{
}

try{
  var country = form.rvar_country.value;

  if(country == "") {
    inlineMsg('rvar_country','You must enter your country.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}


try{
  var phone = form.rvar_phone.value;

  if(phone == "") {
    inlineMsg('rvar_phone','You must enter a phone number.',2);
	error = 1;
    return false;
  } 
  if(!phone.match(phoneRegex)) {
    inlineMsg('rvar_phone','<strong>Error</strong><br />You have entered an invalid phone number.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var email = form.rvar_email.value;

  if(email == "") {
    inlineMsg('rvar_email','You must enter your email.',2);
	error = 1;
    return false;
  }

  if(!email.match(emailRegex)) {
    inlineMsg('rvar_email','<strong>Error</strong><br />You have entered an invalid email.',2);
	error = 1;
    return false;
  }  
}
catch(err)
{
}

try{
  var emailComp = form.rvar_emailComp.value;

  if(emailComp == "") {
    inlineMsg('rvar_emailComp','<strong>Error</strong><br />You must confirm the email.',2);
	error = 1;
    return false;
  }

  if((!email.match(emailComp)) || (!emailComp.match(emailRegex))) {
    inlineMsg('rvar_emailComp','<strong>Error</strong><br />Your email address does not match.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var amount = form.rvar_donation_amount.value;

  if(amount == "" || amount == 0) {
    inlineMsg('rvar_donation_amount','You must donate at least $10',2);
	error = 1;
    return false;
  }
  if(!amount.match(digitRegex)) {
    inlineMsg('rvar_donation_amount','<strong>Error</strong><br />You must enter only digits<br>[.][,][$] are not allowed.',2);
	error = 1;
    return false;
  }  
}
catch(err)
{
}


//SITE VALIDATION
	
try{
  var firstname = form.firstName.value;
  
  if(firstname == "") {
    inlineMsg('firstName','You must enter your first name.',2);
	error = 1;
    return false;
  } 
}
catch(err)
{
}

try{
  var lastname = form.lastName.value;
  
  if(lastname == "") {
    inlineMsg('lastName','You must enter your last name.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var address = form.address.value;
  
  if(address == "") {
    inlineMsg('address','You must enter your address.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var city = form.city.value;

  if(city == "") {
    inlineMsg('city','You must enter your city.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}


try{
  var provStat = form.provStat.value;

  if(provStat == "") {
    inlineMsg('provStat','You must enter a Province/State.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}


try{
  var postalZip = form.postalZip.value;

  if(postalZip == "") {
    inlineMsg('postalZip','You must enter a Postal/Zip Code.',2);
	error = 1;
    return false;
  } 
}
catch(err)
{
}

try{
  var country = form.country.value;

  if(country == "") {
    inlineMsg('country','You must enter your country.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}


try{
  var phone = form.phone.value;

  if(phone == "") {
    inlineMsg('phone','You must enter a phone number.',2);
	error = 1;
    return false;
  } 
  if(!phone.match(phoneRegex)) {
    inlineMsg('phone','<strong>Error</strong><br />You have entered an invalid phone number.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var email = form.email.value;

  if(email == "") {
    inlineMsg('email','You must enter your email.',2);
	error = 1;
    return false;
  }

  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
	error = 1;
    return false;
  }  
}
catch(err)
{
}

try{
  var emailComp = form.emailComp.value;

  if(emailComp == "") {
    inlineMsg('emailComp','<strong>Error</strong><br />You must confirm the email.',2);
	error = 1;
    return false;
  }

  if((!email.match(emailComp)) || (!emailComp.match(emailRegex))) {
    inlineMsg('emailComp','<strong>Error</strong><br />Your email address does not match.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var amount = form.dAmmount.value;

  if(amount == "" || amount < '10') {
    inlineMsg('dAmmount','You must donate at least $10.00',2);
	error = 1;
    return false;
  }
  if(!amount.match(digitRegex)) {
    inlineMsg('dAmmount','<strong>Error</strong><br />The amount must be in numbers.',2);
	error = 1;
    return false;
  }  
}
catch(err)
{
}

try{
  var username = form.username.value;

  if(username == "") {
    inlineMsg('username','You must enter username.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var pswd = form.password.value;

  if(pswd == "") {
    inlineMsg('password','You must enter a password.',2);
	error = 1;
    return false;
  }

  if(!pswd.match(pswdRegex)) {
    inlineMsg('password','<strong>Error</strong><br />You password is not strong enough.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var pswdComp = form.passwordComp.value;

  if(pswdComp == "") {
    inlineMsg('passwordComp','You must confirm the password.',2);
	error = 1;
    return false;
  }
  if((!pswdComp.match(pswdRegex)) || (!pswd.match(pswdComp))) {
    inlineMsg('passwordComp','<strong>Error</strong><br />You password does not match.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var eventName = form.eventName.value;
  
  if(eventName == "") {
    inlineMsg('eventName','You must enter your Event Name.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var description = form.description.value;
  
  if(description == "") {
    inlineMsg('description','You must enter a description for your Event.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var location = form.location.value;
  
  if(location == "") {
    inlineMsg('location','You must specify your event location.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}

try{
  var target = form.target.value;
  
  if(target == "") {
    inlineMsg('target','You must set a Goal for your event.',2);
	error = 1;
    return false;
  }
  
  if(!target.match(digitRegex)) {
    inlineMsg('target','<strong>Error</strong><br />You must enter only digits<br>[.][,][$] are not allowed.',2);
	error = 1;
    return false;
  }  
}
catch(err)
{
}

try{
  var multiEmail = form.sendto.value;
  
  if(multiEmail == "") {
    inlineMsg('sendto','You must enter a valid email address.',2);
	error = 1;
    return false;
  }
  
  if(!multiEmail.match(multiEmailRegex)) {
    inlineMsg('sendto','<strong>Error</strong><br />You must enter valid email address(es) sepreated by a comma [,]',2);
	error = 1;
    return false;
  }  
}
catch(err)
{
}

/*
try{
  var imgRadio = form.imgRadio.checked;
  
  if(imgRadio != "checked") {
    inlineMsg('imgcheck','An Image must be selected for your event.',2);
	error = 1;
    return false;
  }
}
catch(err)
{
}*/
//Validation If Statements
/*  if(firstname == "") {
    inlineMsg('firstname','You must enter your first name.',2);
	error = 1;
    return false;
  }
  if(!firstname.match(textRegex)) {
    inlineMsg('firstname','<strong>Error</strong><br />You have entered an invalid first name.',2);
	error = 1;
    return false;
  }
  if(lastname == "") {
    inlineMsg('lastname','You must enter your last name.',2);
	error = 1;
    return false;
  }
  if(!lastname.match(textRegex)) {
    inlineMsg('lastname','<strong>Error</strong><br />You have entered an invalid lastname.',2);
	error = 1;
    return false;
  }
  if(city == "") {
    inlineMsg('city','You must enter your city.',2);
	error = 1;
    return false;
  }
  if(!city.match(textRegex)) {
    inlineMsg('city','<strong>Error</strong><br />You have entered an invalid city.',2);
	error = 1;
    return false;
  }
  if(provStat == "") {
    inlineMsg('provStat','You must enter a Province/State.',2);
	error = 1;
    return false;
  }
  if(postalZip == "") {
    inlineMsg('postalZip','You must enter a Postal/Zip Code.',2);
	error = 1;
    return false;
  } 
  if(country == "") {
    inlineMsg('country','You must enter your country.',2);
	error = 1;
    return false;
  }
  if(!country.match(textRegex)) {
    inlineMsg('country','<strong>Error</strong><br />You have entered an invalid country.',2);
	error = 1;
    return false;
  }
  if(phone == "") {
    inlineMsg('phone','You must enter a phone number.',2);
	error = 1;
    return false;
  } 
  if(!phone.match(phoneRegex)) {
    inlineMsg('phone','<strong>Error</strong><br />You have entered an invalid phone number.',2);
	error = 1;
    return false;
  }
  if(email == "") {
    inlineMsg('email','You must enter your email.',2);
	error = 1;
    return false;
  }
  if(emailComp == "") {
    inlineMsg('emailComp','<strong>Error</strong><br />You must confirm the email.',2);
	error = 1;
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid email.',2);
	error = 1;
    return false;
  }
  if((!email.match(emailComp)) || (!emailComp.match(emailRegex))) {
    inlineMsg('emailComp','<strong>Error</strong><br />Your email address does not match.',2);
	error = 1;
    return false;
  }
  if(username == "") {
    inlineMsg('username','You must enter username.',2);
	error = 1;
    return false;
  }
  if(pswd == "") {
    inlineMsg('password','You must enter a password.',2);
	error = 1;
    return false;
  }
  if(!pswd.match(pswdRegex)) {
    inlineMsg('password','<strong>Error</strong><br />You password is not strong enough.',2);
	error = 1;
    return false;
  }
  if(pswdComp == "") {
    inlineMsg('passwordComp','You must confirm the password.',2);
	error = 1;
    return false;
  }
  if((!pswdComp.match(pswdRegex)) || (!pswd.match(pswdComp))) {
    inlineMsg('passwordComp','<strong>Error</strong><br />You password does not match.',2);
	error = 1;
    return false;
  }*/
  if(error == 1)
    return false;
  else
    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"; 
}