// form validation function //
function validate(form) {
  var type = form.type.value;
  var make = form.make.value;
  var model = form.model.value;
  var year = form.year.value;
  var trailer = form.trailer.value;
  var vcondition = form.vcondition.value;
  var sstate = form.sstate.value;
  var scity = form.scity.value;
  var szip = form.szip.value;
  var estate = form.estate.value;
  var ecity = form.ecity.value;
  var ezip = form.ezip.value;
  var fname = form.fname.value;  
  var email = form.email.value;
  var phone = form.phone.value;
  var captcha = form.captcha.value;  
  var captchaF =  form.flag.value;  
  
  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var numRegx =  /^[0-9\-]+$/;  

 
  if(type == "") {
    inlineMsg('type','Please select your vehicle type.',3);
    return false;
  }
  if(make == "") {
    inlineMsg('make','Please complete your vehicle make.',3);
    return false;
  }
  if(model == "") {
    inlineMsg('model','Please select your vehicle model.',3);
    return false;
  }
  if(year == "") {
    inlineMsg('year','Please complete your vehicle manufacturing year.',3);
    return false;
  }
  if(!year.match(numRegx)) {
    inlineMsg('year','You have entered an invalid year.',3);
    return false;
  }  
  if(trailer == "") {
    inlineMsg('trailer','Please select trailer.',3);
    return false;
  }
  if(vcondition == "") {
    inlineMsg('vcondition','Please select condition.',3);
    return false;
  }
  if(sstate == "") {
    inlineMsg('sstate','Please select start state.',3);
    return false;
  }
  if(scity == "") {
    inlineMsg('scity','Please select start city.',3);
    return false;
  }
  if(szip == "") {
    inlineMsg('szip','Please select start zip code.',3);
    return false;
  }
  if(estate == "") {
    inlineMsg('estate','Please select  end state.',3);
    return false;
  }  
  if(ecity == "") {
    inlineMsg('ecity','Please select a end city.',3);
    return false;
  }  
  if(ezip == "") {
    inlineMsg('ezip','Please select a end zip code.',3);
    return false;
  }  
  if(email == "") {
    inlineMsg('email','Please complete your email.',3);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','You have entered an invalid email.',3);
    return false;
  }
  if(phone == "") {
    inlineMsg('phone','Please complete your phone number.',3);
    return false;
  }  
  if(phone.length<10) {
    inlineMsg('phone','You have entered an invalid phone number.',3);
    return false;
  }    
  if(!phone.match(numRegx)) {
    inlineMsg('phone','You have entered an invalid phone number.',3);
    return false;
  }
  
  if(captcha == "") {
    inlineMsg('captcha','Please answer the question.',3);
    return false;
  }
  if(captchaF == "0" || captchaF == 0 ) {
    inlineMsg('captcha','Please answer the question.',3);
    return false;
  }  
  return true;
}
// form validation function //
function validateCard(form) {
  var contact_name = form.contact_name.value;
  var contact_phone = form.contact_phone.value;
  var contact_email = form.contact_email.value;
  var name1 = form.name1.value;  
  var address1 = form.address1.value;
  var home1 = form.home1.value;
  var cell1 = form.cell1.value;
  var name2 = form.name2.value;    
  var address2 = form.address2.value;
  var home2 = form.home2.value;
  var cell2 = form.cell2.value;
  var ccname = form.ccname.value;
  var baddress = form.baddress.value;
  var bcity = form.bcity.value;  
  var bstate = form.bstate.value;    
  var bzip = form.bzip.value;
  var bphone = form.bphone.value;  
  var bemail = form.bemail.value;    
  var ccnum = form.ccnum.value;  
  var ccv = form.ccv.value;
  var expmon = form.expmon.value;
  var expyear = form.expyear.value;
  var captcha = form.captcha.value;  

  var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
  var numRegx =  /^[0-9\-]+$/;  

 
  if(contact_name == "") {
    inlineMsg('contact_name','Please complete a contact name.',3);
    return false;
  }
  if(contact_phone == "") {
    inlineMsg('contact_phone','Please complete your a contact number.',3);
    return false;
  }
  if(contact_email == "") {
    inlineMsg('contact_email','Please complete a contact email.',3);
    return false;
  }
  if(!contact_email.match(emailRegex)) {
    inlineMsg('contact_email','You have entered an invalid email.',3);
    return false;
  }  
  if(address1 == "") {
    inlineMsg('address1','Please complete your pickup address.',3);
    return false;
  }
  if(name1 == "") {
    inlineMsg('name1','Please complete pickup name.',3);
    return false;
  }  
  
  if(home1 == "" && cell1 == "") {
    inlineMsg('home1','Please complete either home or cell numbers.',3);
    return false;
  }
  if(address2 == "") {
    inlineMsg('address2','Please complete your delivery  address.',3);
    return false;
  }
  if(name2 == "") {
    inlineMsg('name2','Please complete delivery name.',3);
    return false;
  }    
  if(home2 == "" && cell2 == "") {
    inlineMsg('home2','Please complete either home or cell numbers.',3);
    return false;
  }  
  if(ccname == "") {
    inlineMsg('ccname','Please complete the name on you credit card.',3);
    return false;
  } 
  if(baddress == "") {
    inlineMsg('baddress','Please complete the address of the credit card.',3);
    return false;
  }    
  if(bcity == "") {
    inlineMsg('bcity','Please complete the address of the credit card.',3);
    return false;
  }   
  if(bstate == "") {
    inlineMsg('bstate','Please complete the address of the credit card.',3);
    return false;
  }    
  if(bzip == "") {
    inlineMsg('bzip','Please complete the address of the credit card.',3);
    return false;
  }  
  if(bphone == "") {
    inlineMsg('bphone','Please complete the billing phone number.',3);
    return false;
  }    
  if(bemail == "") {
    inlineMsg('bemail','Please complete email address.',3);
    return false;
  }   
  if(ccnum == "") {
    inlineMsg('ccnum','Please complete credit card number.',3);
    return false;
  }  
  if(!ccnum.match(numRegx)) {
    inlineMsg('ccnum','You have entered an invalid ccv number.',3);
    return false;
  }    
  if(ccv == "") {
    inlineMsg('ccv','Please complete ccv.',3);
    return false;
  }  
  if(!ccv.match(numRegx)) {
    inlineMsg('ccv','You have entered an invalid ccv number.',3);
    return false;
  }  
  if(expmon == "") {
    inlineMsg('expmon','Please select a the expiration month.',3);
    return false;
  }  
  if(expyear == "") {
    inlineMsg('expyear','Please select a the expiration year.',3);
    return false;
  }    
  if(captcha == "") {
    inlineMsg('captcha','Please answer the question.',3);
    return false;
  }
  if(captchaF == "0" || captchaF == 0 ) {
    inlineMsg('captcha','Please answer the question.',3);
    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"; 
}
