// form validation function //

function get_radio_value()
{
for (var i=0; i < document.new_contact.type.length; i++)
   {
   if (document.new_contact.type[i].checked)
      {
      var rad_val = document.new_contact.type[i].value;
      }
   }
   
   return rad_val;
}

function validate_company(form) {
  var discount = form.discount.value;
  var discount_description = form.discount_description.value;
  
  if(discount == "") {
    inlineMsg('discount','Trebuie introdus discount.',2);
    return false;
  }
  
  if(discount_description == "") {
    inlineMsg('discount_description','Trebuie introdus descrierea discount.',2);
    return false;
  }
  
  return true;
}


function validate(form) {
  var first_name = form.first_name.value;
  var last_name = form.last_name.value;
  var address = form.address.value;
  var email = form.email.value;
  var phone = form.phone.value;
  var type = get_radio_value();
  var cnp = form.cnp.value;
  var serie = form.serie.value;
  var numar = form.numar.value;
  var pasaport = form.pasaport.value;
  var card_nr = form.card_nr.value;
  var security_code = form.security_code.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 cnpRegex=/^\d{13}$/;
  var serieRegex=/^\d{6}$/;
  var numberRegex=/^\d*$/;
  
  if(first_name == "") {
    inlineMsg('first_name','Trebuie introdus numele.',2);
    return false;
  }
  if(!first_name.match(nameRegex)) {
    inlineMsg('first_name','Ati introdus un nume incorect.',2);
    return false;
  }
  
  if(last_name == "") {
    inlineMsg('last_name','Trebuie introdus numele.',2);
    return false;
  }
  if(!last_name.match(nameRegex)) {
    inlineMsg('last_name','Ati introdus un nume incorect.',2);
    return false;
  }  
  
  if(address == "") {
    inlineMsg('address','Trebuie introdus adresa.',2);
    return false;
  }
  
  if(type=="Cetatean Roman"){
  
    if(cnp == "") {
    inlineMsg('cnp','Trebuie introdus CNP.',2);
    return false;
    }
    
    if(!cnp.match(cnpRegex)) {
    inlineMsg('cnp','Ati introdus un CNP incorect.',2);
    return false;
    }     
    
    if(serie == "") {
    inlineMsg('serie','Trebuie introdus seria.',2);
    return false;
    }
    
    if(!serie.match(nameRegex)) {
    inlineMsg('serie','Ati introdus o serie incorecta.',2);
    return false;
    }     
    
    if(numar == "") {
    inlineMsg('numar','Trebuie introdus numarul.',2);
    return false;
    }
    
    if(!numar.match(serieRegex)) {
    inlineMsg('numar','Ati introdus un numar incorect.',2);
    return false;
    }      
    
  }
  
  
  if(type=="Cetatean Strain"){
  
    if(pasaport == "") {
    inlineMsg('pasaport','Trebuie introdus seria de pasaport.',2);
    return false;
    }  
  
  }  
  
  if(phone == "") {
    inlineMsg('phone','Trebuie introdus numarul de telefon.',2);
    return false;
  }  

  if(!phone.match(numberRegex)) {
    inlineMsg('phone','Ati introdus un numar incorect.',2);
    return false;
  }   

  
  if(email == "") {
    inlineMsg('email','Trebuie introdus adresa de email.',2);
    return false;
  }
  if(!email.match(emailRegex)) {
    inlineMsg('email','Ati introdus o adresa de email invalida.',2);
    return false;
  }
  
  if(card_nr == "") {
    inlineMsg('card_nr','Trebuie introdus numarul.',2);
    return false;
  }  
  
  if(!card_nr.match(numberRegex)) {
    inlineMsg('card_nr','Ati introdus un numar incorect.',2);
    return false;
  }     
  
  if(security_code == "") {
    inlineMsg('security_code','Trebuie introdus codul de securitate.',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"; 
}
