﻿function checkEmptyTxt(strText,strName,object)
{
    if (strText=="")
    {
        alert (strName+ " không được rỗng!");
        object.focus(); 
        return false; 
    }
    return true;
} 
function checkEmptyTxt2(strText,strAlert,object)
{
    if (strText=="")
    {
        alert (strAlert);
        object.focus(); 
        return false; 
    }
    return true;
}   
function checkEmptyTxt_Multilingual(strText,strName_VN, strName_EN, idLang,object)
{
    var tb=' không được rỗng!';
    var tb_en=' must be fill!';
    if (strText=="")
    {
        alert (""+idLang=="1"?strName_VN + tb:strName_EN + tb_en);
        object.focus(); 
        return false; 
    }
    return true;
}   

function checkEmptyCbo_Multilingual(selectValue,strName_VN, strName_EN,idLang,object)
{
    var tb=' không được rỗng!';
    var tb_en=' must be fill!';
    if (selectValue=="-1")
    {
        alert (""+idLang=="1"?strName_VN + tb:strName_EN + tb_en);
        object.focus(); 
        return false; 
    } 
    return true;
}   
function checkEmptyCbo(selectValue,strName,object)
{
    if (selectValue=="-1")
    {
        alert ("Chưa chọn " + strName + "!")
        object.focus(); 
        return false; 
    } 
    return true;
}   

function CheckInt(strText,strName,object)
{
    if (strText!="" && !IsNumeric(strText))
    {
        alert (strName + " phải là số nguyên!");
        object.focus();
        return false;
    }       
    return true;    
}
function CheckInt_Multilingual(strText,strName_VN, strName_EN, idLang,object)
{
    var tb=' phải là số nguyên!';
    var tb_en=' must be integer!';
    if (strText!="" && !IsNumeric(strText))
    {
        alert (""+idLang=="1"?strName_VN + tb:strName_EN + tb_en);
        object.focus(); 
        return false; 
    }
    return true;
}   
  
function CheckFloat(strText,strName,object)
{
    if (strText!="" && parseFloat(strText).toString()!=strText)
    {
        alert (strName + " phải là số!");
        object.focus();
        return false;
    }       
    return true;    
}

function CheckEmail(strEmail,txtEmail)
{
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(strEmail!="" && !pattern.test(strEmail))
    {        
        alert ("Địa chỉ Email nhập không đúng định dạng!")
        txtEmail.focus();  
        return false;    
    }
    return true; 
}

function CheckURL(strUrl,txtUrl)
{
	var valid = new RegExp(); 
    valid.compile("^(http|https):\/\/[[A-Za-z0-9]+([\-\.]{1}[[A-Za-z0-9]+)*\.[A-Za-z0-9-_%&\?\/.=]{1,800}?$");
	if(strUrl!="")
	{
		if(valid.test(strUrl))
		{
			return true;
		}
		else
		{
		    alert ("Liên kết website nhập không đúng định dạng(http://www.abc.com)!")
			txtUrl.focus();  
			return false;
		}
	}
	else
		return true;
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;
function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
    s=stripCharsInBag(strPhone,validWorldPhoneChars);
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function checkPhone(Phone)
{
	if (checkInternationalPhone(Phone.value)==false){
		alert("Số điện thoại nhập không đúng định dạng!")
		Phone.focus()
		return false
	}
	return true
 }
        
function checkWordLength(strText,strName,object)
{
    var strTexts = strText.split(" ");                   
    for (var i=0;i<strTexts.length;i++)
    {           
        if (strTexts[i].length>15)
        {        
            alert (strName+" không đuợc chứa các từ quá 15 ký tự!")
            object.focus(); 
            return false; 
        }
   }   
   return true;
}   
      
function CheckDate(strDate,strName,object)
{
    if (!isDate(strDate))
    {
       alert (strName + " nhập không đúng định dạng !")
       object.focus(); 
       return false; 
    } 
    return true;    
}
function CheckDate_Multilingual(strDate,strName_VN, strName_EN, idLang,object)
{
    var tb=' nhập không đúng định dạng!';
    var tb_en=' is invalid!';
    if (!isDate(strDate))
    {
        alert (""+idLang=="1"?strName_VN + tb:strName_EN + tb_en);
        object.focus(); 
        return false; 
    }
    return true;
}   

function CheckMoreThanToday(strDate,strName,object)
{
        var today = new Date();
        var strDateParts = strDate.split("/");
        var d=new Date();
        d.setDate(strDateParts[0]);
        d.setMonth(strDateParts[1]-1); // January = 0
        d.setFullYear(strDateParts[2]); 
        if(d <= today)
        {
            alert(strName + " phải lớn hơn ngày hiện tại!");
            object.focus();
            return false;
        }
        return true;
}
function CheckMoreThanToday_Multilingual(strDate,strName_VN, strName_EN, idLang,object)
{
    var tb=' phải lớn hơn ngày hiện tại!';
    var tb_en=' must be larger than today!';
        var today = new Date();
        var strDateParts = strDate.split("/");
        var d=new Date();
        d.setDate(strDateParts[0]);
        d.setMonth(strDateParts[1]-1); // January = 0
        d.setFullYear(strDateParts[2]); 
        if(d <= today)
        {
        alert (""+idLang=="1"?strName_VN + tb:strName_EN + tb_en);
            object.focus();
            return false;
        }
        return true;
}   

function check2Date(strFromDate,strToDate,strName,object)
{
     var EffectiveDateParts = strFromDate.split("/");
     var ExpiryDateParts = strToDate.split("/");  
     if (((Number(ExpiryDateParts[2]) < Number(EffectiveDateParts[2])) ||
            (Number(ExpiryDateParts[2]) == Number(EffectiveDateParts[2]) &&
            ( (Number(ExpiryDateParts[1]) < Number(EffectiveDateParts[1])) ||
            ( (Number(ExpiryDateParts[1]) == Number(EffectiveDateParts[1])) &&
            (Number(ExpiryDateParts[0]) < Number(EffectiveDateParts[0])) )))))
     {
        alert (strName);
        object.focus();
        return false;       
     }
     return true;
}
            
/*Hàm cắt khoảng trống 2 bên của xâu ký tự*/
function trimStr(strText) 
{ 
    // Cat ben trai
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // Cat ben phai
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
   return strText;
}

/*Hàm cắt số 0 bên trái xâu ký tự*/
function trimLeftNum(strText) 
{     
    while (strText.substring(0,1) == '0') 
        strText = strText.substring(1, strText.length);
   return strText;
}
/* Hàm kiểm tra địa chỉ mail có hợp lệ? */
function isEmail(text)
{
    var pattern = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var regex = new RegExp( pattern );
    return regex.test( text );
}
/*Hàm kiểm tra xâu ký tự có phải kiểu ngày hay không*/
function isDate(s)
{
    var sDay, sMonth, sYear, nMonth, nDay, nYear, nSep1, nSep2;
    if(s == "")
        return true;
    nSep1 = s.indexOf("/");
    if(nSep1 < 0)
        return false;
    nSep2 = s.lastIndexOf("/");
    if(nSep2 < 0)
        return false;
    if(nSep1 == nSep2)
        return false;

    sDay = s.substring(0, nSep1);
    sMonth = s.substring(nSep1 + 1, nSep2);
    sYear = s.substring(nSep2 + 1);
    
    if(sYear.length != 4 || sMonth.length> 2 || sDay.length> 2)
        return false;
    if(!sMonth.length || !sDay.length || !sYear.length)
        return false;

    if(isNaN(sMonth) || isNaN(sDay) || isNaN(sYear))
        return false;
    if(sDay.indexOf(".")>= 0 || sMonth.indexOf(".")>= 0 || sYear.indexOf(".")>= 0)
        return false;
    nMonth = parseInt(sMonth,10);
    nDay = parseInt(sDay,10);
    nYear = parseInt(sYear,10);
    if(nMonth <= 0 || nDay <= 0 || nYear <= 0)
        return false;
    if(nMonth> 12)
        return false;
    if(nMonth == 1 || nMonth == 3 || nMonth == 5 || nMonth == 7 || nMonth == 8 || nMonth==10 || nMonth == 12)
    {
        if(nDay> 31)
            return false;
    }
    if(nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11)
    {
        if(nDay> 30)
            return false;
    }
    if(nMonth==2)
    {
        if((nYear % 4 == 0 && nYear % 100 != 0) ||(nYear % 400 == 0))
        {
            if(nDay> 29)
	            return false;
        }
        else
        {
            if(nDay> 28)
	            return false;
        }
    }
    return true;
}

// kiem tra phai so khong
function IsNumeric(strString)   
{
    var strValidChars = "0123456789";
    var strChar;
    var blnResult = true;
    if (strString.length <= 0) return false;
    for (i = 0; i < strString.length && blnResult == true; i++)
    {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
        {
            blnResult = false;
        }
    }
    return blnResult;
}

// kiểm tra có ký tự đặc biệt trong str
function IsCharSpecial(strString)   
{
    var strValidChars = "!@#$%&*()_-+|/-*+<>\|";
    var strChar;
    var blnResult = false;
    if (strString.length <= 0) return false;
    for (i = 0; i < strValidChars.length && blnResult == false; i++)
    {
        strChar = strValidChars.charAt(i);
        if (strString.indexOf(strChar) != -1)
         {
            blnResult = true;
         }
    }
    return blnResult;
}

