function toggleChecked(status) {
	$("input:checkbox").each( function() {
		$(this).attr("checked",status);
	});
}
function toggleCheckedInvert() {
    $("input:checkbox").each( function() {
        if($(this).attr("checked")=='checked') {
            $(this).attr("checked",false);
        }
        else {
            $(this).attr("checked",true);;
        }
    });
}
function checkInput() {
    var n = $("input:checked").length;	
	if(n==0) {
		alert("Ban phai chon it nhat 1 ban tin.");
        isChecked=false;
        return false;
    }
	else {
		blnDel=confirm("Ban co chac chan xoa cac tin nay khong?");
		if (blnDel==true)
			isChecked=true;
		else
			isChecked=false;
	}
	return isChecked;
}

function echo(st) {
	document.write(st);
}
function refreshad(id) {
	jQuery.get("/ajax.php?cmd=up_item",{item_id:id},function(data){
        if(data=='reedit') {
            location.href='/sua-tin.html?id='+id;
        } else if(data=='success') {
            location.reload();
        }
		return false;
	});
}
function make_date_input(input_name, input_value) {
	echo('<div id="'+input_name+'_div"></div>');
	new Ext.form.DateField({
		name:input_name,
		id:input_name,
		value:input_value,
		renderTo:input_name+'_div',
		format:'d/m/Y'
	});
}
var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
var px = document.layers ? "" : "px";
function JSFX_FloatDiv(id, sx, sy)
{
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};
	el.flt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.flt()", 40);
	}
	return el;
}

function isNumeric(sText) {
	var ValidChars = "0123456789.";
	var isNumeric=true;
	var Char;
	for (i = 0; i < sText.length && isNumeric == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			isNumeric = false;
		}
	}
	return isNumeric;
}

function stringToNumber(string){
	var result = string.replace(',','');
	return parseFloat(result);
}

function check_Search() {
    strKeyword=document.frmTimKiem.keyword.value;
    sCheck=document.frmTimKiem.chkSearchIn[1].checked;
    if(sCheck) {
        document.frmTimKiem.keyword.name="q";
        document.frmTimKiem.action="http://www.google.com/custom";
        document.frmTimKiem.target="_blank";
    }
	else {
		document.frmTimKiem.keyword.name="keyword";
		document.frmTimKiem.action="";
		document.frmTimKiem.target="_top";
	}
    return;
}

function login_process() {
    if ($('#user_id').val()=='' || $('#password').val()=='') {
        $('#container').html('&#272;&#259;ng nh&#7853;p kh&#244;ng th&#224;nh c&#244;ng!');
		setTimeout(function(){$('#loginForm').html('')},2000);
    }
    else {
        $.post("/ajax.php?cmd=loginform", {
                user_id: getValueId('user_id'),
                password: getValueId('password')},
            function(msg) {
                if (msg == 'success') {
                    location.reload();
                }                
                else {
                    $('#container').html('&#272;&#259;ng nh&#7853;p kh&#244;ng th&#224;nh c&#244;ng!');
					setTimeout(function(){$('#loginForm').html('')},2000);
                }
            }
        );
    }
}

function open_loginform(){
	var string_form='';
		string_form+='<div id="container">';
		string_form+='<form name="SignInForm" id="SignInForm" method="post">';
		string_form+='<div class="form-signin">';
		string_form+='<div class="field">';
		string_form+='<p class="signin-note">Vui l&#242;ng &#273;i&#7873;n &#273;&#250;ng t&#234;n &#273;&#259;ng nh&#7853;p v&#224; m&#7853;t kh&#7849;u</p>';
		string_form+='</div>';
		string_form+='<div>';
		string_form+='<label class="first" for="user_id">T&#234;n &#273;&#259;ng nh&#7853;p</label>';
		string_form+='<input type="text" name="user_id" id="user_id" class="textbox" />';
		string_form+='</div>';
		string_form+='<div>';
		string_form+='<label class="first" for="password">M&#7853;t kh&#7849;u</label>';
		string_form+='<input type="password" name="password" id="password" class="textbox" />';
		string_form+='</div>';
		string_form+='<div>';
		string_form+='<p class="lost-pass"><a href="/quen-mat-khau.html" title="Qu&#234;n m&#7853;t kh&#7849;u">Qu&#234;n m&#7853;t kh&#7849;u?</a></p>';
		string_form+='<input id="signin_submit" name="signin_submit" value="&#272;&#259;ng nh&#7853;p" type="submit" class="remember"/>';
		string_form+='</div>';
		string_form+='</div>';
		string_form+='</form>';
		string_form+='</div>';
        
    if($('#loginForm').html()){
        $('#loginForm').html('');
    }
    else {
	    $('#loginForm').html(string_form);
    }
	$("#SignInForm").submit(function() {
        login_process();
        return false;
    });
}
function logout(){
	$.post("/ajax.php", { cmd: 'logout' },
		function(msg) {
			location.reload();
		}
	);
}
function getValueId(id, type, svalue) {
    if (document.getElementById(id)) {

        if (typeof(type) == 'undefined') {
            var type = 'value';
        }

        if (typeof(svalue) == 'undefined') {
            var svalue = '';
        }

        if (type == 'value') {
            return document.getElementById(id).value;
        }
        else if (type == 'checked') {
            return document.getElementById(id).checked;
        }
        else if (type == 'assign') {
            return document.getElementById(id).value = svalue;
        }
        else {
            return '';
        }
    }
}
function startUpload(){
    $("#f1_upload_process").show();
    $("#f1_upload_form").hide();
    return true;  
}
function del_image(name,path,img_name){
    var img_path=path+name;
    $.post("delete.php", {
        imgpath: img_path,
        imgname: name
        },
        function() {
            $('div#'+img_name).remove();
            if(!$("div#pic").html()){
                $("div#pic").removeAttr('style');
            }
        }
    );
}

function stopUpload(success,name,newname,path){
      var result = '';
      if (success == 1){
        var arr=name;
        var arr_name=arr.split('##');
        newname1="'"+newname+"'";
        path1="'"+path+"'";
        result = '<div id="divpic'+arr_name.length+'" class="divpic"><img src="'+path+newname+'"/><div><a id="link_del" href="#" title="'+newname+'" onclick="del_image('+newname1+','+path1+',\'divpic'+arr_name.length+'\'); return false;">X&#243;a</a> | <a id="link_insert" href="#" title="'+newname+'" onclick="insert_pic(\''+path+newname+'\'); return false;">Ch&#232;n &#7843;nh</a></div></div>';
        for(i=1;i<arr_name.length-1;i++)
        {
        name="'"+arr_name[i]+"'";
        result = result+'<div id="divpic'+i+'" class="divpic"><img src="'+path+arr_name[i]+'"/><div><a id="link_del" href="#" title="'+arr_name[i]+'" onclick="del_image('+name+','+path1+',\'divpic'+i+'\'); return false;">X&#243;a</a> | <a id="link_insert" href="#" title="'+arr_name[i]+'" onclick="insert_pic(\''+path+arr_name[i]+'\'); return false;">Ch&#232;n &#7843;nh</a></div></div>';
        }
      }
      else if(success == 2){
         result = '<span class="emsg">B&#7841;n ch&#7881; c&#243; th&#7875; upload 20 &#7843;nh.<\/span>';
      }
	  else {
		  result = '<span class="emsg">Xu&#7845;t hi&#7879;n l&#7895;i trong qu&#225; tr&#236;nh upload file.<br/>Ki&#7875;m tra l&#7841;i file upload.<\/span>';
	  }
      document.getElementById('f1_upload_process').style.display = 'none';
      document.getElementById('pic').innerHTML = result ;
      document.getElementById('f1_upload_form').style.display = 'block';
      document.getElementById('pic').style.height='80px';
      document.getElementById('pic').style.overflow='auto';
      document.getElementById('myfile').value='';
      return true;   
}
function insert_pic(img_url){
    var tmp_pic = new Image();
    tmp_pic.src = img_url;
	if(tmp_pic.width>500){
    	tinyMCE.execCommand('mceInsertContent', true, '<br /><img src="'+img_url+'" width="500" />');
	}
	else {
		tinyMCE.execCommand('mceInsertContent', true, '<br /><img src="'+img_url+'" />');
	}
    delete(tmp_pic);
}
