
function FormatNumber(str){
    var strTemp = GetNumber(str);
    if(strTemp.length <= 3)
        return strTemp;
    strResult = "";
    for(var i =0; i< strTemp.length; i++)
        strTemp = strTemp.replace(",", "");
    strTemp = strTemp.replace(".", "");

    for(var i = strTemp.length; i>=0; i--)
    {
        if(strResult.length >0 && (strTemp.length - i -1) % 3 == 0)
            strResult = "," + strResult;
        strResult = strTemp.substring(i, i + 1) + strResult;
    }
    return strResult;
}
function GetNumber(str)
{
    for(var i = 0; i < str.length; i++)
    {
        var temp = str.substring(i, i + 1);
        if(!(temp == "," || temp == "." || (temp >= 0 && temp <=9)))
        {
            alert("Giá chỉ được nhập vào là số từ 0-9");
            return str.substring(0, i);
        }
        if(temp == " ")
            return str.substring(0, i);
    }
    return str;
}

function IsNumberInt(str)
{
    for(var i = 0; i < str.length; i++)
    {
        var temp = str.substring(i, i + 1);
        if(!(temp == "," || temp == "." || (temp >= 0 && temp <=9)))
        {
            alert("Giá chỉ được nhập vào là số từ 0-9");
            return str.substring(0, i);
        }
        if(temp == " " || temp == ",")
            return str.substring(0, i);
    }
    return str;
}

function Format_Price(strTemp,currency,unit)
{
    strTemp        = strTemp.replace(/,/g, "");
    var priceTy    = parseInt(strTemp/1000000000,0)
    var priceTrieu = parseInt((strTemp % 1000000000)/1000000,0)
    var priceNgan  = parseInt(((strTemp % 1000000000))%1000000/1000,0)
    var priceDong  = parseInt(((strTemp % 1000000000))%1000000%1000,0)
    var strTextPrice = ""

    if(priceTy > 0 && parseInt(strTemp,0) > 900000000)
        strTextPrice = strTextPrice  + "<b>" + priceTy + "</b>Tỷ "
    if(priceTrieu > 0)
        strTextPrice = strTextPrice  + "<b>" + priceTrieu + "</b>Tr "
    if(priceNgan > 0)
        strTextPrice = strTextPrice  + "<b>" + priceNgan + "</b>Ngàn "
    if(priceDong > 0)
        strTextPrice = strTextPrice  + "<b>" + priceDong + "</b>"


    if(priceTy > 0 || priceTrieu > 0 || priceNgan > 0 || priceDong > 0)
        strTextPrice = strTextPrice  + " <span class='update_date'>"+ currency +"<span>"

    if(unit == "0")
    {
        strTextPrice = strTextPrice + "/Tổng diện tích";
    }else if(unit == "1")
    {
        strTextPrice = strTextPrice + "/m2";
    }else if(unit == "2")
    {
        strTextPrice = strTextPrice + "/Tháng";
    }else if(unit == "3")
    {
        strTextPrice = strTextPrice + "/Năm";
    }
    // = strTextPrice

    return strTextPrice;
}



function imageFilter(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 == 13 )  {
                sl=document.getElementById('ListImage');
                var no = sl.options.length;
                for(i=0;i<no;i++){
                    if(elem.value==''){
                        sl.options[i].style.display='';
                        continue;
                    }
                
                    if(sl.options[i].text.indexOf(elem.value)>=0){

                    }else{
                        sl.options[i].style.display='none';
                    }
                }
            }
        }
    }
}


//Ham chi cho nhap so vao textbox
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 > 44 && charCode < 47) ||
                (charCode > 47 && charCode < 58)) {
                return true;
            } else {
                return false;
            }
        }
    }
}

function number_format(obj){
    var seperator=',';
    var tmp=obj.value.replace(',','');
    for (i=0;i<3;i++){
        tmp=tmp.replace(',','');
    }
    var counta=0;
    var resulta='';
    if(tmp!=''&&tmp.length<12){
        for(ii=tmp.length-1;ii>=0;ii--)
        {
            if(counta>2 ){
                resulta+=seperator;
                counta=0;
            }
            //alert(tmp[ii]);
            resulta+=tmp.charAt(ii);
            counta++;
        }
        tmp='';
		
        for(jj=resulta.length-1;jj>=0;jj--)
        {
            tmp+=resulta.charAt(jj);
        }
        obj.value=tmp;
    }
}

//<!--
// SimpleFix by Wrapbit
function format_number(dec,fix) {
    fixValue = parseFloat(Math.pow(10,fix));
    retValue = parseInt(Math.round(dec * fixValue)) / fixValue;
    return retValue;
}
function calculateIt() {
    form = document.form1;
    if(isNaN(form.dec.value) || isNaN(form.fix.value)) {
        alert("Input is empty or not a number");
        form.dec.select();
        return;
    }
    form.result.value = format_number(form.dec.value,form.fix.value);
}
//-->


//Chon tat ca cac checkbox
function checka(){
    form=document.adminForm;
    for (var i = 0; i < form.elements.length; i++) {
        if ( form.all.checked==true ){
            form.elements[i].checked = true;
        }
        else{
            form.elements[i].checked = false;
        }
    }
}

//An - Hien 1 doi tuong
function hide_show_item(item_name){	
    var obj = document.getElementById(item_name).style;
    if(obj.display=="none"){
        obj.display="block";
    }else{
        obj.display="none";
    }
}

function lockMenu(){
    $('menu').style.display="none";
}

function unlockMenu(){	
    $('menu').style.display="inline";
}

function ajaxSubmit(div,link,pressbutton,form) {	
	
    link=link.replace(/\//g,'-');
	
    switch(pressbutton){
		
        case 'pause_one':
            if(confirm('Bạn thật sự muốn ngưng?')){
                Nav.go(div,link,form);
            }
            break;
			
        case 'stop_one':
            if(confirm('Bạn thật sự muốn hủy?')){
                Nav.go(div,link,form);
            }
            break;
		
        case 'deleteone':
            if(confirm('Bạn thật sự muốn xóa?')){
                Nav.go(div,link,form);
            }
            break;
        case 'delete':
            for (var i = 0; i < document.adminForm.elements.length; i++) {
                if ( document.adminForm.elements[i].checked == true && document.adminForm.elements[i].id=='cb'){
                    break;
                }
            }
            if(i==document.adminForm.elements.length){
                alert('Dữ liệu chưa được chọn!');
                break;
            }
            if(confirm('Bạn thật sự muốn xóa?')){
                Nav.go(div,link,form);
            }
            break;
			
        case 'move':
            for (var i = 0; i < document.adminForm.elements.length; i++) {
                if ( document.adminForm.elements[i].checked == true && document.adminForm.elements[i].id=='cb'){
                    break;
                }
            }
            if(i==document.adminForm.elements.length){
                alert('Dữ liệu chưa được chọn!');
                break;
            }
            if(confirm('Bạn muốn phân quyền quản lý cho user đang chọn?')){
                Nav.go(div,link,form);
            }
            break;
		
        case 'pause_all':
            for (var i = 0; i < document.adminForm.elements.length; i++) {
                if ( document.adminForm.elements[i].checked == true && document.adminForm.elements[i].id=='cb'){
                    break;
                }
            }
            if(i==document.adminForm.elements.length){
                alert('Dữ liệu chưa được chọn!');
                break;
            }
            if(confirm('Bạn thật sự muốn ngừng?')){
                Nav.go(div,link,form);
            }
            break;
			
        case 'stop_all':
            for (var i = 0; i < document.adminForm.elements.length; i++) {
                if ( document.adminForm.elements[i].checked == true && document.adminForm.elements[i].id=='cb'){
                    break;
                }
            }
            if(i==document.adminForm.elements.length){
                alert('Dữ liệu chưa được chọn!');
                break;
            }
            if(confirm('Bạn thật sự muốn hủy?')){
                Nav.go(div,link,form);
            }
            break;
			
        case 'change_status':
            if(confirm('Bạn muốn chuyển trạng thái cho đơn hàng này?')){
                Nav.go(div,link,form);
            }
            break;
			
        case 'change_type':
            if(confirm('Đơn hàng này sẽ trở thành đơn hàng thanh toán đặc biệt?')){
                Nav.go(div,link,form);
            }
            break;
			
        case 'change_contract_status':
            if(confirm('Bạn muốn chuyển trạng thái cho hợp đồng này?')){
                Nav.go(div,link,form);
            }
            break;
						
        case 'published':
            for (var i = 0; i < document.adminForm.elements.length; i++) {
                if ( document.adminForm.elements[i].checked == true && document.adminForm.elements[i].id=='cb'){
                    break;
                }
            }
            if(i==document.adminForm.elements.length){
                alert('Dữ liệu chưa được chọn !');
                break;
            }
            Nav.go(div,link,form);
            break;
        default:
            Nav.go(div,link,form);
            ;
    }
    return false;
}

function ajax(container,action,form){
    $('loading').style.display='inline';
    width='50%';
    height='50%'//screen.height;
    $('imgcover').style.zIndex=200;
    $('imgcover').style.marginLeft="42%";
    $('imgcover').style.marginTop=200;

    if(form){
        form=$(form);
        form.action=action;
			
        form.send({
            update: $(container),
            evalScripts:true,
            onComplete: function() {
                $('loading').style.display='none';
                var JTooltips = new Tips($$('.hasTip'), {
                    maxTitleChars: 50,
                    fixed: false
                });
            }
        });
    }else{
        new Ajax(action, {
            method:'post',
            update: $(container),
            asynchronous:true,
            evalScripts:true,
            onComplete: function(){
                $('loading').style.display='none';
                var JTooltips = new Tips($$('.hasTip'), {
                    maxTitleChars: 50,
                    fixed: false
                });
            }
        }).request();
	
    }
}

function simple_ajax(div,url){
    var fn = function() {
        var fx = new Fx.Style(div, 'background-color', {
            duration: 800,
            transition: Fx.Transitions.Quad.easeOut
        }).start('#fb9393', '#f8f8f8');
    }

    new Ajax(url, {
        method: 'get',
        evalScripts:true,
        update: $(div),
        onComplete: fn
    }).request();
}


function MM_preloadImages() { //v3.0
    var d=document;
    if(d.images){
        if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
        for(i=0; i<a.length; i++)
            if (a[i].indexOf("#")!=0){
                d.MM_p[j]=new Image;
                d.MM_p[j++].src=a[i];
            }
    }
}

function empty(str){
    var temp="";
    var len=0;
    temp=str;
    while(len!=temp.length){
        len=temp.length;
        temp=temp.replace(" ","");
        temp=temp.replace("\r","");
        temp=temp.replace("\n","");
    }
    if(temp=="")return true;
    return false;
}
function floatingFn(Show,Element){
    if(Show){
        document.getElementById(Element).style.display='block';
        document.getElementById(Element).style.zindex=100;
    }else{
        document.getElementById(Element).style.display='none';
        document.getElementById(Element).style.zindex=1;
    }
}
