// JavaScript Document
function validateChangepass(theForm) {
var reason = "";

  reason += validateEmpty(theForm.oldpass,'Nhập mật khẩu hiện tại');
  reason += validatePassword(theForm.pass);
  reason += rePass(theForm.pass,theForm.repass);
      
  if (reason != "") {
    alert("Xuất hiện lỗi sau:\n" + reason);
    return false;
  }

}


function validateFormOnSubmit_dtd(theForm) {
var reason = "";

  reason += validateEmpty(theForm.vitri,'Nhập Vị trí');
  reason += validateEmpty(theForm.chucvu,'Chọn chức vụ');
  reason += validateEmpty(theForm.loaihinhcv,'Chọn Loại hình công việc');
  reason += validateEmpty(theForm.soluongtuyen,'Nhập Số lượng cần tuyển');
  reason += validateEmpty(theForm.mucluong,'Nhập mức lương');
  reason += validateEmpty(theForm.namkinhnghiem,'Chọn số năm kinh nghiệm');
  reason += validateEmpty(theForm.trinhdo,'Chọn Trình độ');
  reason += validateEmpty(theForm.hosogom,'Hồ sơ gồm');
  reason += hanhs(theForm);
  reason += validateEmpty(theForm.val,'Nhập mã bảo mật');

if (reason != "") {
    alert("Xuất hiện lỗi sau:\n" + reason);
    return false;
  }

}



function validateFormOnSubmit_tv(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validatePassword(theForm.pass);
  reason += rePass(theForm.pass,theForm.repass);
  reason += validateEmpty(theForm.fullname,'Nhập Họ và tên');
  reason += brithday(theForm);
  reason += validateEmpty(theForm.address,'Nhập Địa chỉ');
  reason += validateEmpty(theForm.thanhpho,'Chọn Tỉnh/Thành phố');
  reason += validateEmpty(theForm.sdt,'Nhập Số điện thoại liên hệ');
  reason += validateEmpty(theForm.val,'Nhập mã bảo mật');
  reason += dieukhoan(theForm.chkAcceptRule);
      
  if (reason != "") {
    alert("Xuất hiện lỗi sau:\n" + reason);
    return false;
  }

}


function validateFormOnSubmit_td(theForm) {
var reason = "";

  reason += validateEmail(theForm.email);
  reason += validatePassword(theForm.pass);
  reason += rePass(theForm.pass,theForm.repass);
  reason += validateEmpty(theForm.company_name,'Nhập Tên công ty');
  reason += validateEmpty(theForm.com_address,'Nhập Địa chỉ công ty');
  reason += validateEmpty(theForm.thanhpho,'Chọn Tỉnh/Thành phố');
  reason += validateEmpty(theForm.fullname,'Nhập Tên người liên hệ');
  reason += validateEmpty(theForm.address_user,'Nhập Địa chỉ liên hệ');
  reason += validateEmpty(theForm.sdt,'Nhập Số điện thoại liên hệ');
  reason += validateEmpty(theForm.val,'Nhập mã bảo mật');
      
  if (reason != "") {
    alert("Xuất hiện lỗi sau:\n" + reason);
    return false;
  }

}

function dieukhoan(thForm) {
    var error = "";
 
    if (thForm.value == '') {
        thForm.style.background = 'Yellow'; 
        error = "Đọc và đồng ý thỏa thuận sử dụng.\n";
    } else {
        thForm.style.background = 'White';
    }
    return error;  
}

function hanhs(theForm) {
    var error = "";
 
    if (theForm.hanhs_ngay.value.length == 0) {
        theForm.hanhs_ngay.style.background = 'Yellow'; 
        error = "Bạn chưa chọn hạn nộp hồ sơ.\n";
    } else {
        theForm.hanhs_ngay.style.background = 'White';
    }
    if (theForm.hanhs_thang.value.length == 0) {
        theForm.hanhs_thang.style.background = 'Yellow'; 
        error = "Bạn chưa chọn hạn nộp hồ sơ.\n";
    } else {
         theForm.hanhs_thang.style.background = 'White';
    }
    if (theForm.hanhs_nam.value.length == 0) {
        theForm.hanhs_nam.style.background = 'Yellow'; 
        error = "Bạn chưa chọn hạn nộp hồ sơ.\n";
    } else {
        theForm.hanhs_nam.style.background = 'White';
    }
    return error;  
}



function brithday(theForm) {
    var error = "";
 
    if (theForm.sn_ngay.value.length == 0) {
        theForm.sn_ngay.style.background = 'Yellow'; 
        error = "Bạn chưa chọn ngày sinh.\n";
    } else {
        rp.style.background = 'White';
    }
    if (theForm.sn_thang.value.length == 0) {
        theForm.sn_thang.style.background = 'Yellow'; 
        error = "Bạn chưa chọn ngày sinh.\n";
    } else {
        rp.style.background = 'White';
    }
    if (theForm.sn_nam.value.length == 0) {
        theForm.sn_nam.style.background = 'Yellow'; 
        error = "Bạn chưa chọn ngày sinh.\n";
    } else {
        rp.style.background = 'White';
    }
    return error;  
}



function rePass(ep,rp) {
    var error = "";
 
    if (ep.value != rp.value) {
        rp.style.background = 'Yellow'; 
        error = "Xác nhận mật khẩu không chính xác.\n";
    } else {
        rp.style.background = 'White';
    }
    return error;  
}

function validateEmpty_Select(fld,alr) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
       // error = "The required field has not been filled in.\n"
		error = "You didn't select a "+ alr +".\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateEmpty(fld,alr) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
       // error = "The required field has not been filled in.\n"
		error =  alr +".\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } 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 = 'Yellow';
        error = "Bạn không nhập Mật khẩu.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "Mật khẩu phải lớn hơn 6 ký tự và nhỏ hơn 15 ký tự. \n";
        fld.style.background = 'Yellow';
/*    } else if (illegalChars.test(fld.value)) {
        error = "Mật khẩu có ký tự không hợp lệ.\n";
        fld.style.background = 'Yellow';
*/    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "Mật khẩu phải có ít nhất một chữ số.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}  
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 = 'Yellow';
        error = "Chưa nhập Email.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Nhập Email không chính xác.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Email có ký tự không hợp lệ.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}

