// form validation function //
function validate(form) {
  var firstname = form.firstname.value;
  var lastname = form.lastname.value;
  var email = form.email.value;
  var address = form.address.value;
  var city = form.city.value;
  var country = form.country.value;
  //var stateprov = form.stateprov.value;
  var zip = form.zip.value;
  var phone = form.phone.value;
  
  var billfname = form.billfname.value;
  var billlname = form.billlname.value;
  var billemail = form.billemail.value;
  var billaddress = form.billaddress.value;
  var billcity = form.billcity.value;
  var billcountry = form.billcountry.value;
  //var billstateprov = form.billstateprov.value;
  var billzip = form.billzip.value;
  var billphone = form.billphone.value;
  var payment = $RF('donateForm', 'payment_type');
  var billcard = form.billcard.value;
  
  var otheramount = form.other_amount.value;
  var donAmount = $RF('donateForm', 'Ac1'); 
  var Ac1 = form.Ac1.value;
  var monthlyplus = form.monthlyplus.value;
  
  var monthlyplusamount = form.monthlyplusamount.value;
  
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var messageRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  var phoneRegex = /^(?:\([2-9]\d{2}\)\ ?|[2-9]\d{2}(?:\-?|\ ?))[2-9]\d{2}[- ]?\d{4}$/;

  if(donAmount == null) {
    inlineMsg('theParent','You must select a donation option.(Monthly or One-Time)',6);
    return false;
  } 
  if(donAmount == "monthly_other" && otheramount == "") {
    inlineMsg('monthly6','You must enter other amount (ie 100.00).',6);
    return false;
  } 
  if($('monthlyplus').checked == true && monthlyplusamount == ""){
    inlineMsg('monthlyplusamount','You must enter one time add on amount (ie 100.00).',6);
    return false;
  }
  
  //Personal fields
  if(firstname == "") {
    inlineMsg('firstname','You must enter your First Name.',6);
    return false;
  }
  if(lastname == "") {
    inlineMsg('lastname','You must enter your First Name.',6);
    return false;
  }
  if(!firstname.match(nameRegex)) {
    inlineMsg('firstname','You have entered an invalid First Name.',6);
    return false;
  }
   if(!lastname.match(nameRegex)) {
    inlineMsg('lastname','You have entered an invalid Last Name.',6);
    return false;
  }
  if(email == "") {
    inlineMsg('email','<strong>Error</strong><br />You must enter your Email.',6);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','<strong>Error</strong><br />You have entered an invalid Email.',6);
    return false;
  }
  if(address == "") {
    inlineMsg('address','<strong>Error</strong><br />You must enter an Address.',6);
    return false;
  }
  if(city == "") {
    inlineMsg('city','You must enter your City.',6);
    return false;
  }
   if(stateprov == "--" || stateprov == "") {
    inlineMsg('stateprov','You must select a State/Province.',6);
    return false;
  }
  
  if(!city.match(nameRegex)) {
    inlineMsg('city','You have entered an invalid City.',6);
    return false;
  }
   if(country == "") {
    inlineMsg('country','You must enter you Country.',6);
    return false;
  }
  
  if(zip == "") {
    inlineMsg('zip','You must enter a Zip/Postal Code.',6);
    return false;
  }
   if(phone == "") {
    inlineMsg('phone','You must enter a Phone Number.',6);
    return false;
  }
  
 //Billing Fields
   if(billfname == "") {
    inlineMsg('billfname','You must enter your First Name.',6);
    return false;
  }
  if(billlname == "") {
    inlineMsg('billlname','You must enter your First Name.',6);
    return false;
  }
  if(!billfname.match(nameRegex)) {
    inlineMsg('billfname','You have entered an invalid First Name.',6);
    return false;
  }
   if(!billlname.match(nameRegex)) {
    inlineMsg('billlname','You have entered an invalid Last Name.',6);
    return false;
  }
  /*if(billemail == "") {
    inlineMsg('billemail','<strong>Error</strong><br />You must enter your Email.',6);
    return false;
  }
  if(!billemail.match(emailRegex)) {
    inlineMsg('billemail','<strong>Error</strong><br />You have entered an invalid Email.',6);
    return false;
  }
 if(billaddress == "") {
    inlineMsg('billaddress','<strong>Error</strong><br />You must enter an Address.',6);
    return false;
  }
  if(billcity == "") {
    inlineMsg('billcity','You must enter you City.',6);
    return false;
  }
  if(!billcity.match(nameRegex)) {
    inlineMsg('billcity','You have entered an invalid City.',6);
    return false;
  }
   if(billcountry == "") {
    inlineMsg('billcountry','You must enter you Country.',6);
    return false;
  }*/
 if((payment=="VIS" || payment=="MC") && billcard==""){
   inlineMsg('billcard','You must enter a valid credit card number.',6);
    return false;
 
 }
 
 
  return true;
}

function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}

// 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);
  //alert("td: "+targetdiv +" t:"+target)
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
   window.scroll(0,topposition -40);
  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"; 
}