﻿function checkChangePass() {
	if(trim(document.frmChangePass.txtOldPass.value) == "") {
		alert("Vui lòng nhập mật khẩu cũ!")
		document.frmChangePass.txtOldPass.focus();
		return false;
	}
	else if(trim(document.frmChangePass.txtNewPass.value) == "") {
		alert("Vui lòng nhập mật khẩu mới!")
		document.frmChangePass.txtNewPass.focus();
		return false;
	}
	else if(trim(document.frmChangePass.txtConfirmNewPass.value) == "") {
		alert("Vui lòng xác nhận mật khẩu mới!")
		document.frmChangePass.txtConfirmNewPass.focus();
		return false;
	}
	else if(document.frmChangePass.txtConfirmNewPass.value != document.frmChangePass.txtNewPass.value) {
		alert("Xác nhận mật khẩu mới chưa đúng. Vui lòng nhập lại!")
		document.frmChangePass.txtConfirmNewPass.focus();
		return false;
	}
	else {
		return true;
	}
}

function logout() {
	if(confirm("Bạn có chắc muốn thoát không?")) {
		location.href = "logout.php";
	}
}

// Kiem tra khoang trang va cat khoang trang
function trim (inputString) {
	if (typeof inputString != "string") {
		return inputString;
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	// Kiem tra khoang trang o dau chuoi
	while (ch == " ") {
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	// Kiem tra khoang trang o cuoi chuoi
	while (ch == " ") {
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	// Tim kiem nhieu khoang trang trong chuoi
	while (retValue.indexOf("  ") != -1) { 
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
	}
	// Tra ve chuoi duoc cat khoang trang
	return retValue;
} // Ends the "trim" function

function isEmail(email) {
  var validFormatRegExp = /^\w(\.?\w)*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;
  var isValid = validFormatRegExp.test(email);
  return isValid;
}

function numberOnly(evt) {
    evt = (evt) ? evt : ((window.event) ? event : null);
    if (evt) {
       var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
       if (elem) {
           var charCode = (evt.charCode) ? evt.charCode : 
               ((evt.which) ? evt.which : evt.keyCode); 
           if ((charCode < 32 ) || (charCode > 47 && charCode < 58)) {
               return true;
           } else {
               return false;
           }
       }
    }
}

function checkChangeEmail() {
	if(trim(document.frmEmail.txtEmail.value) == "") {
		alert("Vui lòng nhập email!")
		document.frmEmail.txtEmail.focus();
		return false;
	}
	else if(!isEmail(document.frmEmail.txtEmail.value)) {
		alert("Email không đúng định dạng. Vui lòng nhập lại!")
		document.frmEmail.txtEmail.focus();
		return false;
	}
	else {
		return true;
	}
}

function saveDownload(id) {
	if(trim(document.frmDownload.txtTitle.value) == "") {
		alert("Vui lòng nhập tên bảng báo giá!")
		document.frmDownload.txtTitle.focus();
		return false;
	}
	else {
		if(id == null) {
			document.frmDownload.hdAction.value = "save";
		}
		else {
			document.frmDownload.hdAction.value = "save";
			document.frmDownload.hdValue.value = id;
		}
		document.frmDownload.submit();
	}
}

function updateDownload(id) {
	if(id == null) {
		document.frmDownload.hdDisplay.value = "update";
	}
	else {
		document.frmDownload.hdDisplay.value = "update";
		document.frmDownload.hdValue.value = id;
	}
	document.frmDownload.submit();
}

function deleteDownload(id) {
	if (confirm("Bạn có chắc muốn xóa?")) {
		document.frmDownload.hdAction.value = "delete";
		document.frmDownload.hdValue.value = id;
		document.frmDownload.submit();
	}
}

function saveDriver(id) {
	if(trim(document.frmDriver.txtTitle.value) == "") {
		alert("Vui lòng nhập tên driver!")
		document.frmDriver.txtTitle.focus();
		return false;
	}
	else {
		if(id == null) {
			document.frmDriver.hdAction.value = "save";
		}
		else {
			document.frmDriver.hdAction.value = "save";
			document.frmDriver.hdValue.value = id;
		}
		document.frmDriver.submit();
	}
}

function updateDriver(id) {
	if(id == null) {
		document.frmDriver.hdDisplay.value = "update";
	}
	else {
		document.frmDriver.hdDisplay.value = "update";
		document.frmDriver.hdValue.value = id;
	}
	document.frmDriver.submit();
}

function deleteDriver(id) {
	if (confirm("Bạn có chắc muốn xóa?")) {
		document.frmDriver.hdAction.value = "delete";
		document.frmDriver.hdValue.value = id;
		document.frmDriver.submit();
	}
}

function saveGuide(id) {
	if(trim(document.frmGuide.txtTitle.value) == "") {
		alert("Vui lòng nhập tên tập tin!")
		document.frmGuide.txtTitle.focus();
		return false;
	}
	else {
		if(id == null) {
			document.frmGuide.hdAction.value = "save";
		}
		else {
			document.frmGuide.hdAction.value = "save";
			document.frmGuide.hdValue.value = id;
		}
		document.frmGuide.submit();
	}
}

function updateGuide(id) {
	if(id == null) {
		document.frmGuide.hdDisplay.value = "update";
	}
	else {
		document.frmGuide.hdDisplay.value = "update";
		document.frmGuide.hdValue.value = id;
	}
	document.frmGuide.submit();
}

function deleteGuide(id) {
	if (confirm("Bạn có chắc muốn xóa?")) {
		document.frmGuide.hdAction.value = "delete";
		document.frmGuide.hdValue.value = id;
		document.frmGuide.submit();
	}
}

function saveProCat(id) {
	if(trim(document.frmProCat.txtName.value) == "") {
		alert("Vui lòng nhập tên nhóm sản phẩm!")
		document.frmProCat.txtName.focus();
		return false;
	}
	else {
		if(id == null) {
			document.frmProCat.hdAction.value = "save";
		}
		else {
			document.frmProCat.hdAction.value = "save";
			document.frmProCat.hdValue.value = id;
		}
		document.frmProCat.submit();
	}
}

function updateProCat(id) {
	if(id == null) {
		document.frmProCat.hdDisplay.value = "update";
	}
	else {
		document.frmProCat.hdDisplay.value = "update";
		document.frmProCat.hdValue.value = id;
	}
	document.frmProCat.submit();
}

function deleteProCat(id) {
	if (confirm("Thực hiện xóa nhóm sản phẩm sẽ xóa tất cả nhóm sản phẩm con và sản phẩm thuộc nhóm sản phẩm này.\nBạn có chắc muốn xóa?")) {
		document.frmProCat.hdAction.value = "delete";
		document.frmProCat.hdValue.value = id;
		document.frmProCat.submit();
	}
}

function saveSubCat(id) {
	if(trim(document.frmSubCat.txtName.value) == "") {
		alert("Vui lòng nhập tên nhóm sản phẩm con!")
		document.frmSubCat.txtName.focus();
		return false;
	}
	else {
		if(id == null) {
			document.frmSubCat.hdAction.value = "save";
		}
		else {
			document.frmSubCat.hdAction.value = "save";
			document.frmSubCat.hdValue.value = id;
		}
		document.frmSubCat.submit();
	}
}

function updateSubCat(id) {
	if(id == null) {
		document.frmSubCat.hdDisplay.value = "update";
	}
	else {
		document.frmSubCat.hdDisplay.value = "update";
		document.frmSubCat.hdValue.value = id;
	}
	document.frmSubCat.submit();
}

function deleteSubCat(id) {
	if (confirm("Thực hiện xóa nhóm sản phẩm sẽ xóa tất cả sản phẩm thuộc nhóm sản phẩm này.\nBạn có chắc muốn xóa?")) {
		document.frmSubCat.hdAction.value = "delete";
		document.frmSubCat.hdValue.value = id;
		document.frmSubCat.submit();
	}
}

function saveAdv(id) {
	if(trim(document.frmAdvertising.txtTitle.value) == "") {
		alert("Vui lòng nhập tên quảng cáo!")
		document.frmAdvertising.txtTitle.focus();
		return false;
	}
	else {
		if(id == null) {
			document.frmAdvertising.hdAction.value = "save";
		}
		else {
			document.frmAdvertising.hdAction.value = "save";
			document.frmAdvertising.hdValue.value = id;
		}
		document.frmAdvertising.submit();
	}
}

function updateAdv(id) {
	if(id == null) {
		document.frmAdvertising.hdDisplay.value = "update";
	}
	else {
		document.frmAdvertising.hdDisplay.value = "update";
		document.frmAdvertising.hdValue.value = id;
	}
	document.frmAdvertising.submit();
}

function deleteAdv(id) {
	if(confirm("Bạn có chắc muốn xóa?")) {
		document.frmAdvertising.hdAction.value = "delete";
		document.frmAdvertising.hdValue.value = id;
		document.frmAdvertising.submit();
	}
}

function savePromo(id) {
	if(trim(document.frmpromotion.txtTitle.value) == "") {
		alert("Vui lòng nhập tên Khuyến mãi!")
		document.frmpromotion.txtTitle.focus();
		return false;
	}
	else {
		if(id == null) {
			document.frmpromotion.hdAction.value = "save";
		}
		else {
			document.frmpromotion.hdAction.value = "save";
			document.frmpromotion.hdValue.value = id;
		}
		document.frmpromotion.submit();
	}
}


function updatePromo(id) {
	if(id == null) {
		document.frmpromotion.hdDisplay.value = "update";
	}
	else {
		document.frmpromotion.hdDisplay.value = "update";
		document.frmpromotion.hdValue.value = id;
	}
	document.frmpromotion.submit();
}

function deletePromo(id) {
	if(confirm("Bạn có chắc muốn xóa?")) {
		document.frmpromotion.hdAction.value = "delete";
		document.frmpromotion.hdValue.value = id;
		document.frmpromotion.submit();
	}
}


function saveProduct(id) {
	if(trim(document.frmProduct.txtName.value) == "") {
		alert("Vui lòng nhập tên sản phẩm!")
		document.frmProduct.txtName.focus();
		return false;
	}
	if(document.frmProduct.cboProCat.value == 0) {
		alert("Vui lòng chọn nhóm sản phẩm!")
		document.frmProduct.cboProCat.focus();
		return false;
	}
	else if(trim(document.frmProduct.txtPrice.value) == "") {
		alert("Vui lòng nhập giá sản phẩm!")
		document.frmProduct.txtPrice.focus();
		return false;
	}
	else if(document.frmProduct.txtPrice.value < 0 || isNaN(document.frmProduct.txtPrice.value)) {
		alert("Giá sản phẩm không chính xác. Vui lòng nhập lại!")
		document.frmProduct.txtPrice.focus();
		return false;
	}
	else {
		if(id == null) {
			document.frmProduct.hdAction.value = "save";
		}
		else {
			document.frmProduct.hdAction.value = "save";
			document.frmProduct.hdValue.value = id;
		}
		document.frmProduct.submit();
	}
}

function updateProduct(id) {
	if(id == null) {
		document.frmProduct.hdDisplay.value = "update";
	}
	else {
		document.frmProduct.hdDisplay.value = "update";
		document.frmProduct.hdValue.value = id;
	}
	document.frmProduct.submit();
}

function deleteProduct(id) {
	if (confirm("Bạn có chắc muốn xóa?")) {
		document.frmProduct.hdAction.value = "delete";
		document.frmProduct.hdValue.value = id;
		document.frmProduct.submit();
	}
}

function saveSupport(id) {
	if(trim(document.frmSupport.txtSupportName.value) == "") {
		alert("Vui lòng nhập tên liên hệ!")
		document.frmSupport.txtSupportName.focus();
		return false;
	}
	else if(trim(document.frmSupport.txtName.value) == "") {
		alert("Vui lòng nhập họ và tên!")
		document.frmSupport.txtName.focus();
		return false;
	}
	else if(trim(document.frmSupport.txtPhone.value) == "") {
		alert("Vui lòng nhập điện thoại!")
		document.frmSupport.txtPhone.focus();
		return false;
	}
	else if(document.frmSupport.txtPhone.value < 0 || isNaN(document.frmSupport.txtPhone.value)) {
		alert("Số điện thoại không chính xác. Vui lòng nhập lại!")
		document.frmSupport.txtPhone.focus();
		return false;
	}
	else if(trim(document.frmSupport.txtYahooID.value) == "") {
		alert("Vui lòng nhập Yahoo ID!")
		document.frmSupport.txtYahooID.focus();
		return false;
	}
	else {
		if(id == null) {
			document.frmSupport.hdAction.value = "save";
		}
		else {
			document.frmSupport.hdAction.value = "save";
			document.frmSupport.hdValue.value = id;
		}
		document.frmSupport.submit();
	}
}

function updateSupport(id) {
	if(id == null) {
		document.frmSupport.hdDisplay.value = "update";
	}
	else {
		document.frmSupport.hdDisplay.value = "update";
		document.frmSupport.hdValue.value = id;
	}
	document.frmSupport.submit();
}

function deleteSupport(id) {
	if (confirm("Bạn có chắc muốn xóa?")) {
		document.frmSupport.hdAction.value = "delete";
		document.frmSupport.hdValue.value = id;
		document.frmSupport.submit();
	}
}

function checkContact() {
	if(trim(document.frmContact.txtFullName.value) == "") {
		alert("Vui lòng nhập họ tên!")
		document.frmContact.txtFullName.focus();
		return false;
	}
	else if(trim(document.frmContact.txtAddress.value) == "") {
		alert("Vui lòng nhập địa chỉ!")
		document.frmContact.txtAddress.focus();
		return false;
	}
	else if(trim(document.frmContact.txtPhone.value) == "") {
		alert("Vui lòng nhập điện thoại!")
		document.frmContact.txtPhone.focus();
		return false;
	}
	else if(document.frmContact.txtPhone.value < 0 || isNaN(document.frmContact.txtPhone.value)) {
		alert("Số điện thoại không chính xác. Vui lòng nhập lại!")
		document.frmContact.txtPhone.focus();
		return false;
	}
	else if(trim(document.frmContact.txtEmail.value) == "") {
		alert("Vui lòng nhập email!")
		document.frmContact.txtEmail.focus();
		return false;
	}
	else if(!isEmail(document.frmContact.txtEmail.value)) {
		alert("Email không đúng định dạng. Vui lòng nhập lại!")
		document.frmContact.txtEmail.focus();
		return false;
	}
	else if(trim(document.frmContact.txtTitle.value) == "") {
		alert("Vui lòng nhập tiêu đề!")
		document.frmContact.txtTitle.focus();
		return false;
	}
	else if(trim(document.frmContact.txtContent.value) == "") {
		alert("Vui lòng nhập nội dung!")
		document.frmContact.txtContent.focus();
		return false;
	}
	else {
		return true;
	}
}

function saveIntro() {
	document.frmIntro.hdAction.value = "save";
	document.frmIntro.submit();
}

function expand(subId, id) {
	document.getElementById(subId).style.display = "block";
	document.getElementById(id).href = "javascript:dispand('"+subId+"','"+id+"');";
}

function dispand(subId, id) {
	document.getElementById(subId).style.display = "none";
	document.getElementById(id).href = "javascript:expand('"+subId+"','"+id+"');";
}

function savenews(id) {
	if(trim(document.frmnews.txtTitle.value) == "") {
		alert("Vui lòng nhập tên tập tin!")
		document.frmnews.txtTitle.focus();
		return false;
	}
	else {
		if(id == null) {
			document.frmnews.hdAction.value = "save";
		}
		else {
			document.frmnews.hdAction.value = "save";
			document.frmnews.hdValue.value = id;
		}
		document.frmnews.submit();
	}
}

function updatenews(id) {
	if(id == null) {
		document.frmnews.hdDisplay.value = "update";
	}
	else {
		document.frmnews.hdDisplay.value = "update";
		document.frmnews.hdValue.value = id;
	}
	document.frmnews.submit();
}

function deletenews(id) {
	if (confirm("Bạn có chắc muốn xóa?")) {
		document.frmnews.hdAction.value = "delete";
		document.frmnews.hdValue.value = id;
		document.frmnews.submit();
	}
}
