function filterElement(elem)
{
    return document.getElementById(elem);
}
//------------------------------------------------------------------------------
function IsAlphabet(Digit) 
{
    return /^[a-zA-Z]$/.test(Digit);
}
//------------------------------------------------
// Function Name : Trim 
// Actions : Remove left&right space.
//------------------------------------------------
function Trim(temp) 
{
    if(temp=='')
        return temp;
        
    temp = temp + '';
    
    return RTrim(LTrim(temp));
}
//------------------------------------------------
// Function Name : LTrim 
// Actions : Remove left string.
//------------------------------------------------
function LTrim(temp) 
{    
    if(temp=='')
        return temp;
    
    return temp.replace(/^\s+/, '');
}

//------------------------------------------------
// Function Name : RTrim 
// Actions : Remove right space.
//------------------------------------------------
function RTrim(temp) 
{
    if(temp=='')
        return temp;
    return temp.replace(/\s+$/, '');
}
//
//------------------------------------------------
// Function Name : FormatCurrency
// Actions : Return Currency Formatted
//------------------------------------------------
function FormatCurrency(num,currencyCode,isReplace,justFormat)
{    
    if (num == null)
        return "";
    var num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    var sign = (num == (num = Math.abs(num)));    
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    {
        switch(Trim(currencyCode.toLowerCase()))
        {
            case 'en-us':
            {
                num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
                break;
            }
            case 'vi-vn':
            {                
                num = num.substring(0,num.length-(4*i+3))+ '.' + num.substring(num.length-(4*i+3));
                break;
            }
        }
    }    
    var res='0';
    switch(Trim(currencyCode.toLowerCase()))
    {
        case 'en-us':
        {   
            if(justFormat!=null && justFormat==true)
            {
                if(isReplace==false)
                    res=(((sign)?'':'-') + num + '.' + cents);
                else
                    res=(((sign)?'':'-') + num);
            }
            else
            {
                if(isReplace==false)
                    res=(((sign)?'':'-') + '$' + num + '.' + cents);
                else
                    res=(((sign)?'':'-') + '$' + num);
            }
            break;
        }
        case 'vi-vn':
        {           
            if(justFormat!=null && justFormat==true)
            { 
                if(isReplace==false)
                    res=(((sign)?'':'-') + num + ',' + cents);
                else
                    res=(((sign)?'':'-') + num);
            }
            else
            {
                if(isReplace==false)
                    res=(((sign)?'':'-') + num + ',' + cents + '<u>đ</u>');
                else
                    res=(((sign)?'':'-') + num + '<u>đ</u>');
            }
            break;
        }
    }
    return res;
}
//
function Money_CheckCorrect(controlCheckCorrectID)
{
  
	var input=Trim(filterElement(controlCheckCorrectID).value.toString());
    
    if(input!=''&& !IsAlphabet(input))
    {
        var priceFor=Trim(Money2Long(input));                
        if(FormatCurrency(priceFor,'vi-vn',true,true)=="0")
        {
           filterElement(controlCheckCorrectID).value='0';
        }
        else
        {
            filterElement(controlCheckCorrectID).value=FormatCurrency(priceFor,'vi-vn',true,true);
        }        
    }
    else
    {        
        filterElement(controlCheckCorrectID).value=input.substring(0,input.length-1);
    }
} 

function Money_OnBlurChange(controlCheckCorrectID)
{
    var input=Trim(filterElement(controlCheckCorrectID).value.toString());    
    var price=Trim(Money2Long(input));
    if(price==""|| parseInt(price)<=0)
    {
       filterElement(controlCheckCorrectID).value='0';
    }    
}

function Money2Long(input)
{
    //input = Trim(input);
    return parseInt(input.replace(/\./ig,""));
}

function Money_OnBlurChangedMin(minControlCheckCorrectID,maxControlCheckCorrectID)
{
    var inputMin=Trim(filterElement(minControlCheckCorrectID).value.toString());
    var inputMax=Trim(filterElement(maxControlCheckCorrectID).value.toString());
    var minPrice="";
    var arrMinPrice=inputMin.split(".");
    if(arrMinPrice.length>1)
    {
        for(var iMin=0;iMin<arrMinPrice.length;iMin++)
        {
            minPrice+=arrMinPrice[iMin];
        }
    }
    else
    {
        minPrice=Money2Long(inputMin);
    }
    minPrice=Trim(minPrice);
    var maxPrice="";
    var arrMaxPrice=inputMax.split(".");
    if(arrMaxPrice.length>1)
    {
        for(var iMax=0;iMax<arrMaxPrice.length;iMax++)
        {
            maxPrice+=arrMaxPrice[iMax];
        }
    }
    else
    {
        maxPrice=Money2Long(inputMax);
    }
    maxPrice=Trim(maxPrice);
    if(minPrice==''||parseInt(minPrice)<1000)
    {
        filterElement(minControlCheckCorrectID).value='1.000';
    }
}

function Money_OnBlurChangedMax(minControlCheckCorrectID,maxControlCheckCorrectID)
{
    var inputMin=Trim(filterElement(minControlCheckCorrectID).value.toString());
    var inputMax=Trim(filterElement(maxControlCheckCorrectID).value.toString());
    var minPrice="";
    var arrMinPrice=inputMin.split(".");
    if(arrMinPrice.length>1)
    {
        for(var iMin=0;iMin<arrMinPrice.length;iMin++)
        {
            minPrice += arrMinPrice[iMin];
        }
    }
    else
    {
        minPrice=Money2Long(inputMin);
    } 
    minPrice=Trim(minPrice);
    var maxPrice="";
    var arrMaxPrice=inputMax.split(".");
    if(arrMaxPrice.length>1)
    {
        for(var iMax=0;iMax<arrMaxPrice.length;iMax++)
        {
            maxPrice+=arrMaxPrice[iMax];
        }
    }
    else
    {
        maxPrice=Money2Long(inputMax);
    }
    maxPrice=Trim(maxPrice);
    if(maxPrice==''||parseInt(maxPrice)<1000)
    {
        filterElement(maxControlCheckCorrectID).value='1.000';
    }
} 


function confirmDelete(url){
	if(confirm('Bạn chắc chắn muốn xóa?')){
		window.location=url;
	}else{
		return false;
	}
}
/*
function upSanPham(url){
		alert("Sản phẩm đã được up lên đầu.");
		window.location=url;
}*/

function upSanPham(url,countup){
	
		if(countup >=0)
		{
			alert("Đã up thành công. Bạn còn "+ countup +" lượt up trong ngày hôm nay.");
			window.location=url;
		}
		else
		{
			alert("Bạn đã hết lượt up. Đăng kí gian hàng đảm bảo để được 500 lượt up mỗi ngày. Liên hệ ngay: 38.36.36.06 - 0977.910.999");
		}

		
}





function upSanPhamNhanh(url){
	
		
			alert("Đã up thành công.");
			window.location=url;


		
}

function getId(name){
	var a = document.getElementById(name);
	return a;
}


function checkLogin(){
	if(getId('txtUsername').value == ""){
		getId('txtUsername').focus();
		return false;
	}
	
	if(getId('txtPassword').value == ""){
		getId('txtPassword').focus();
		return false;
	}
}
function IsMatchingCode(str){
    var re = /^[0-9-A-z]*$/; 
    return myRegExp.test(str)
}


function checkRegister(){
	 var re = /^[0-9-A-z]*$/;
	 
	//Check username
	if(getId('txtUsername').value == ""){
		alert('Tên đăng nhập không được để trống !');
		getId('txtUsername').focus();
		return false;
	}
	
	if(!re.test(getId('txtUsername').value))
	{
		alert('Tên đăng nhập chỉ có thể là chữ hay số. Không được kí tự đặc biệt!');
		getId('txtUsername').focus();
		return false;	
	}

	if(getId('txtUsername').value.length < 6 || getId('txtUsername').value.length > 20 ){
		alert('Tên đăng nhập phải lớn hơn 6 kí tự và nhỏ hơn 20 kí tự!');
		getId('txtUsername').focus();
		return false;
	}
	
	//
	if(getId('txtPassword').value == ""){
		alert('Mật khẩu không được để trống !');
		getId('txtPassword').focus();
		return false;	
	}
	
	if(getId('txtPassword').value.length < 6){
		alert('Mật khẩu phải lớn hơn 6 kí tự !');
		getId('txtPassword').focus();
		return false;
	}
	
	if(getId('txtPasswordConfirm').value != getId('txtPassword').value){
		alert('Mật khẩu xác thực không khớp !');
		getId('txtPasswordConfirm').value = "";
		getId('txtPasswordConfirm').focus();
		return false;
	}
	//
	var tmail=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(getId('txtEmail').value == ""){
		alert('Email không được để trống!');
		getId('txtEmail').focus();
		return false;	
	}
	
	if(tmail.test(getId('txtEmail').value) == false){
		alert('E-mail nhập không đúng định dạng !');
		getId('txtEmail').value = "";
		getId('txtEmail').focus();
		return false;
	}
	
		
	//
	if(getId('verify_code').value == ""){
		alert('Mã xác thực không được để trống!');
		getId('verify_code').focus();
		return false;	
	}
	
	if(getId('txtRule').checked == false){
		alert('Bạn phải đồng ý với thỏa thuận trước khi đăng kí !');
		return false;
	}

}

function checkSuataikhoan(){
	 var re = /^[0-9-A-z]*$/;
	 
	//Check username
	if(getId('txtShopName').value == ""){
		alert('Tên shop không được để trống !');
		getId('txtShopName').focus();
		return false;
	}
	
	if(getId('txtTinhThanh').value == '-1'){
		alert('Hãy chọn tỉnh thành !');
		getId('txtTinhThanh').focus();
		return false;
	}
	
	//
		
	if(getId('dtcodinh').value == "" && getId('dtdidong').value == ""){
		alert('Phải có 1 số điện thoại để liên lạc !');
		getId('dtcodinh').focus();
		return false;	
	}
	
	
	

}

function funDangBanChecker(){
	
	if(getId('t_tieude').value == ''){
		alert('Tiêu đề không được để trống !');
		getId('t_tieude').focus();
		return false;
	}
	
	
	if(getId('t_loaisanpham').value == ''){
		alert('Hãy chọn loại sản phẩm !');
		getId('t_loaisanpham').focus();
		return false;
	}
	
	
	if(getId('txtTinhThanh').value == '-1'){
		alert('Hãy chọn tỉnh thành !');
		getId('txtTinhThanh').focus();
		return false;
	}
	
	
	if(tinyMCE.get('elm2').getContent() == ''){
		alert('Vui lòng nhập chi tiết sản phẩm !');
		getId('elm2').focus();
		return false;
	}
	
	
		
	if(getId('t_giaban').value == ''){
		getId('t_giaban').value = 0;
	}

	
}



function funRaoVatChecker(){
	if(getId('rv_tieude').value == ''){
		alert('Vui lòng nhập tiêu đề !');
		getId('rv_tieude').focus();
		return false;
	}
	
	if(getId('rv_loairaovat').value == ''){
		alert('Hãy chọn loại rao vặt !');
		getId('rv_loairaovat').focus();
		return false;
	}
	
	if(getId('rv_thoigiandang').value == ''){
		alert('Vui lòng nhập thời gian đăng !');
		getId('rv_thoigiandang').focus();
		return false;
	}
	
	if(isNaN(getId('rv_thoigiandang').value)){
		alert('Thời gian đăng phải là số nguyên !');
		getId('rv_thoigiandang').focus();
		return false;
	}
	
	if(getId('txtQuocGia').value == '-1'){
		alert('Hãy chọn quốc gia !');
		getId('txtQuocGia').focus();
		return false;
	}
	
	if(getId('txtTinhThanh').value == '-1'){
		alert('Hãy chọn tỉnh thành !');
		getId('txtTinhThanh').focus();
		return false;
	}
	
	if(tinyMCE.get('elm2').getContent() == ''){
		alert('Vui lòng nhập chi tiết rao vặt !');
		getId('elm2').focus();
		return false;
	}
	
	getId('rv_thoigiandang').value = Math.abs(getId('rv_thoigiandang').value);
}

function funTuyenDungChecker(){
	if(getId('td_tieude').value == ''){
		alert('Vui lòng nhập tiêu đề !');
		getId('td_tieude').focus();
		return false;
	}
	
		
	if(getId('td_loaituyendung').value == ''){
		alert('Hãy chọn loại tuyển dụng !');
		getId('td_loaituyendung').focus();
		return false;
	}
	
	if(getId('td_thoigiandang').value == ''){
		alert('Vui lòng nhập thời gian đăng !');
		getId('td_thoigiandang').focus();
		return false;
	}
	
	if(isNaN(getId('td_thoigiandang').value)){
		alert('Thời gian đăng phải là số nguyên !');
		getId('td_thoigiandang').focus();
		return false;
	}
	
	
	
	if(isNaN(getId('td_mucluong_from').value)){
		alert('Mức lương phải là số nguyên !');
		getId('td_mucluong_from').focus();
		return false;
	}
	
	if(isNaN(getId('td_mucluong_to').value)){
		alert('Mức lương phải là số nguyên !');
		getId('td_mucluong_to').focus();
		return false;
	}
	
	if(getId('td_sonamkinhnghiem').value == ''){
		alert('Vui lòng nhập số năm kinh nghiệm !');
		getId('td_sonamkinhnghiem').focus();
		return false;
	}
	
	if(isNaN(getId('td_sonamkinhnghiem').value)){
		alert('Số năm kinh nghiệm !');
		getId('td_sonamkinhnghiem').focus();
		return false;
	}
	
	if(getId('txtQuocGia').value == ''){
		alert('Vui lòng chọn quốc gia !');
		getId('txtQuocGia').focus();
		return false;
	}
	
	
	if(getId('txtTinhThanh').value == ''){
		alert('Vui lòng chọn tỉnh thành !');
		getId('txtTinhThanh').focus();
		return false;
	}
	
	if(getId('td_chitiet').value == ''){
		alert('Vui lòng nhập chi tiết !');
		getId('td_chitiet').focus();
		return false;
	}

	if(getId('td_mucluong_from').value == ''){
		getId('td_mucluong_from').value = 0;
	}
	
	if(getId('td_mucluong_to').value == ''){
		getId('td_mucluong_to').value = 0;
	}
	getId('td_thoigiandang').value = Math.abs(getId('td_thoigiandang').value);
	getId('td_mucluong_from').value = Math.abs(getId('td_mucluong_from').value);
	getId('td_mucluong_to').value = Math.abs(getId('td_mucluong_to').value);
	getId('td_sonamkinhnghiem').value = Math.abs(getId('td_sonamkinhnghiem').value);
}
