//http://www.webcheatsheet.com/javascript/form_validation.php
var error_color ="#eb8f8f";

function validateContactUsSubmit(theForm) {
var reason = "";
  
  reason += validateEmpty(theForm.name," nume ");
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validateEmpty(theForm.message," mesaj ");
 
  if (reason != "") {
    alert("Anumite campuri trebuie corectare:\n" + reason);
    return false;
  }

  return true;
}

function validateOrderSubmit(theForm) {
var reason = "";
  if(theForm.same_address[0].checked)  return true;
  else if(theForm.same_address[1].checked){
	  reason += validateEmpty(theForm.first_name," prenumele ");
	  reason += validateEmail(theForm.email);
	  reason += validateEmpty(theForm.last_name," numele ");
	  reason += validateEmpty(theForm.address," adresa ");
	  reason += validateEmpty(theForm.city," orasul ");
	  reason += validateEmpty(theForm.state," judetul ");
	  reason += validateEmpty(theForm.zipcode," cod postal ");
	  reason += validatePhone(theForm.phone);
	  //reason += validateEmpty(theForm.message," message ");
	 
	  if (reason != "") {
		alert("Anumite campuri trebuie corectare:\n" + reason);
		return false;
	  }
  }

  return true;
}

function validateUpdateMemberFormOnSubmit(theForm) {
var reason = "";
  
  reason += validateEmpty(theForm.first_name," prenumele ");
  reason += validateEmpty(theForm.last_name," numele ");
  reason += validateEmpty(theForm.address," adresa ");
  reason += validateEmpty(theForm.city," orasul ");
  reason += validateEmpty(theForm.state," judetul ");
  reason += validateEmpty(theForm.zipcode," cod postal ");
  reason += validatePhone(theForm.phone);
 
  if (reason != "") {
    alert("Anumite campuri trebuie corectare:\n" + reason);
    return false;
  }

  return true;
}

function validateMemberFormOnSubmit(theForm) {
var reason = "";
  
  reason += validateEmail(theForm.email);
  reason += validateConfirmEmail(theForm.confirm_email);
  reason += validateMatchEmails(theForm.email, theForm.confirm_email);
  
  reason += validatePassword(theForm.password); 
  reason += validateConfirmPassword(theForm.confirm_password);
  reason += validateMatchPasswords(theForm.password , theForm.confirm_password );
 
   reason += validateEmpty(theForm.first_name," prenumele ");
  reason += validateEmpty(theForm.last_name," numele ");
  reason += validateEmpty(theForm.address," adresa ");
  reason += validateEmpty(theForm.city," orasul ");
  reason += validateEmpty(theForm.state," judetul ");
  reason += validateEmpty(theForm.zipcode," cod postal ");
  reason += validatePhone(theForm.phone);
  
  if (reason != "") {
    alert("Anumite campuri trebuie corectare:\n" + reason);
    return false;
  }

  return true;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = error_color;
        error = "Nu ati introdus o adresa de email.\n";
		fld.focus();
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = error_color;
        error = "Va rog introduce-ti o adresa  de email corecta.\n";
		fld.focus();
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = error_color;
        error = "Va rog introduce-ti o adresa  de email corecta.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateMatchEmails(fld1, fld2) {
    var error="";
   //alert('validateEmails');
    if (fld1.value != fld2.value) {
        fld1.style.background = error_color;
		fld2.style.background = error_color;
        error = "Adresa de email si cea de confirmare nu sunt la fel.\n";
		fld1.focus();
		fld2.focus();
    } 
    return error;
}



function validateConfirmEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = error_color;
        error = "Nu ati introdus o adresa de email de confirmare.\n";
		fld.focus();
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = error_color;
        error = "Va rog introduce-ti o adresa de email de confirmare corecta.\n";
		fld.focus();
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = error_color;
        error = "Va rog introduce-ti o adresa de email de confirmare corecta.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
	    error = "Nu ati introdus un numar de telefon.\n";
        //error = "You didn't enter a phone number.\n";
        fld.style.background = error_color;
		fld.focus();
    } else if (isNaN(parseInt(stripped))) {
		error = "Numar de telefon contine caractere incorecte.\n";
        //error = "The phone number contains illegal characters.\n";
        fld.style.background = error_color;
		fld.focus();
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = error_color;
		fld.focus();
        error = "Nu ati introdus o parola.\n";
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function validateConfirmPassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = error_color;
		fld.focus();
        error = "Nu ati introdus o parola de confirmare.\n";
    } else {
        fld.style.background = 'White';
    }
   return error;
}

function validateMatchPasswords(fld1, fld2) {
    var error="";
   //alert('validateEmails');
    if (fld1.value != fld2.value) {
        fld1.style.background = error_color;
		fld2.style.background = error_color;
        error = "Parola si parola de confirmare nu sunt la fel. \n";
		fld1.focus();
		fld2.focus();
    } 
    return error;
}

function validateEmpty(fld, name) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = error_color;
        error = "Va rugam introduce-ti "+name+".\n"
		fld.focus();
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateChecked(fld) {
    var error="";

    if (fld.checked == false ) {
        fld.style.background = error_color;
        error = "Please accept the disclaimer.\n";
		fld.focus();
		
    } 
    return error;
}