//AJAX
///nhap du lieu
function LSH(url,id, parameters) {
	loadstatustext = '<center>Loading...<br><img src=images/loading.gif></center>';
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = function()
   {
      // document.getElementById('loading').innerHTML = 'loading...';
	   el = document.getElementById(id);
       el.innerHTML = loadstatustext;
       if (http_request.readyState == 4) {
       //alert(http_request.responseText);
       result = http_request.responseText;
	   // document.getElementById('loading').innerHTML = '';
       document.getElementById(id).innerHTML = result;
       }
    }

http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
return true;
}
////////
function ajaxPost(pagepost,fields,idview){
var srchStr="_LSH_";
var offset = 0;
var dau=0;
poststr="DATA=";
var i=0;
offset = fields.indexOf(srchStr, offset);

  while(offset!=-1){
    var subcon=fields.substring(dau,offset);
//    alert(subcon);
    poststr+= encodeURIComponent(document.getElementById(subcon).value)+"L_S_H";
    dau=offset+5;
    offset=dau;
    offset = fields.indexOf(srchStr, offset);
    i=1;
  }

  offset = fields.lastIndexOf(srchStr);
  if(i==1)  subcon=fields.slice(offset+5);
  else subcon=fields;
  //alert(subcon);
  poststr+= encodeURIComponent(document.getElementById(subcon).value);
LSH(pagepost,idview,poststr);
}
//end nhap du lieu
////truyen lien ket

function ajaxLoad(url,id)
{
	loadstatustext = '<center>Loading...<br><img src=images/loading.gif></center>';
   if (document.getElementById) {
       var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
       }
       if (x){
            x.onreadystatechange = function()
            {
                   el = document.getElementById(id);
                   el.innerHTML = loadstatustext;
             if (x.readyState == 4 && x.status == 200){
                   el.innerHTML = x.responseText;
               }
             }
           x.open("GET", url, true);
           x.send(null);
           }
}

//	var url = window.location.href;
//	if(url.indexOf('#')!=-1) c_url = url.split('#');
//	id = c_url?c_url[1]:'#';
	//ajaxLoad('noidung.php?id='+id,'hienthi');
//======end truyen lien ket
//end AJAX

function postbinhchon(Num_binhchon){
	var str_fields="";
	for(i=1;i<=Num_binhchon;i++){ 
		if(i==1) str_fields="binhchon"+i+"_LSH_id_binhchon"+i;
		else str_fields+="_LSH_binhchon"+i+"_LSH_id_binhchon"+i;
	}
	str_fields+="_LSH_code";
//	alert(str_fields);
	ajaxPost('user/dbbinhchon.php',str_fields,'hienthi');
}
function SendBinhchon(Num)
{
/*	var flag=0;
for(var i=1;i<=Num;i++){
	var id="binhchon"+i;
	var v=getObjectById(id).value;
	v=parseInt(v);
	alert(v);
	if(v!="")
	{
		flag=1;break;
	}
	//if(v<0){ alert("Bạn đã nhập số < 0");return false;}
	//if(v>10){ alert("Bạn đã nhập số > 10");return false;}
}
if(flag==0){ 
alert("Bạn chưa bình chọn mục nào! Vui lòng nhập ít nhất một mục để bình chọn");
return false;
}*/
     //  frm_binhchon.submit();
	 postbinhchon(Num);
}
function checktxtsearch() {
	var txt = getObjectById('search').value;
	if (txt == "" || txt=="Nhập từ khóa cần tìm") {
		alert("Bạn hãy nhập vào từ khóa cần tìm");
		return false;
	}
}

function postsearch(){
	ajaxPost('user/dbsearch.php','search', 'contentcolumn')
}
function ClearText(IdClear)

{
	var mCheck=getObjectById(IdClear);
	var TextClear=mCheck.value;
	if(TextClear!="") mCheck.value="";
}
function getObjectById( id ) {
	var obj = null;
	if( document.getElementById ){
		obj = document.getElementById( id );
	}
	else if( document.all )
		obj = document.all[id];
	else
		obj = document.layer[id];
	return obj;
}
function getImgSize(root,id){
	//////////////
	var newImg = new Image();
	newImg.src = document.getElementById("hinh").value;
	var h = newImg.height;
	var w = newImg.width;

	var slide=getObjectById("fields[slidetin]");
	var trangchu=getObjectById("fields[newstrangchu]");
//alert(h);alert(w);
if(h!=0 && w!=0){
	if(slide.value==1){ // 5 tin slide
	
		if(h<300 && w<400){
		 alert("Kích thu?c hình ?nh nh? hon quy dinh (400px X 300px). Vui lòng ch?n hình khác");
		}
	}
	else if(trangchu.value==1){ // tin trang chu
		if(h<180 && w<240){
		 alert("Kích thu?c hình ?nh nh? hon quy dinh (240px X 180px). Vui lòng ch?n hình khác");
		}
	}
	else{ //5 tin dau
		if(h<70 && w<93){
		 alert("Kích thu?c hình ?nh nh? hon quy dinh (93px X 70px). Vui lòng ch?n hình khác");
		}
	}
}
}
function OnMouse(ctrl)
{
        ctrl.style.borderColor ='#000000';
        ctrl.style.backgroundColor = '#B5BED6';
}
function OutMouse(ctrl)
{
        ctrl.style.borderColor ='#D6D3CE';
        ctrl.style.backgroundColor = '#D6D3CE';
}
function ChangeImg(Root,Img)
{
        if(Root.value!="")
        {
                var tmp=document.getElementById(Img);
                tmp.src=Root.value;
        }
}
function RowOver(Rowid)
{
        var tmp=eval("document.getElementById('"+Rowid+"')");
        tmp.className="st2";
}
function RowOut(Rowid)
{
        var tmp=eval("document.getElementById('"+Rowid+"')");
        tmp.className="st1";
}
function ChangeColorRow(Rowid)
{
        var tmp=eval("document.getElementById('"+Rowid+"')");
        if(tmp.className=="st1")
        {
                tmp.className="st2";
        }else if(tmp.className=="st2") tmp.className="st1"
}
function ChangeColorRowName(Rowid)
{
        var tmp=eval("document.getElementByName('"+Rowid+"')");
        if(tmp.className=="st1")
        {
                tmp.className="st2";
        }else if(tmp.className=="st2") tmp.className="st1"
}
function ChangeColor(Ctr,Color)
{
        Ctr.style.backgroundColor=Color;
}
function CheckLen(Id,Name,Num)
{
        var str=frm.elements[Id].value;
        if(str.length<Num)
        {
                window.alert("Ban phai nhap "+Name+" dai hon "+Num+" ky tu");
                return true;
        }
    return false;
}
function CheckText(Name,Msg,Style)
{
        var str;
        if(frm.elements[Name].value==Style)
        {
                window.alert(Msg);
                return true;
        }
    return false;
}
function CheckText2(Name,Msg,Style,Form)
{
       // var obj=eval(Form);
		var obj=document.getElementById(Form);
        var str;
        if(obj.elements[Name].value==Style)
        {
                window.alert(Msg);
                return true;
        }
    return false;
}
function CheckUser2(Name,Msg,Form)
{
        var obj=eval(Form)
        var strIn=obj.elements[Name].value;
        if(NormalChar(strIn)==false) //Khong hop le
        {
                alert(Msg);
                return true;
        }
        return false;
}
function CheckUser(Name,Msg,Value)
{
        var strIn=frm.elements[Name].value;
        if(NormalChar(strIn)==false) //Khong hop le
        {
                alert(Msg);
                return true;
        }
        return false;
}
function CheckEmail(mName,Msg,Value)
{
        var str=frm.elements[mName].value;
        if(cEmail(str)==false) //Khong hop le
        {
                alert(Msg);
                return true;
        }
        return false;
}
function CheckEmail2(mName,Msg,Form)
{
        //var obj=eval(Form)
		var obj=document.getElementById(Form);
        var str=obj.elements[mName].value;
        if(cEmail(str)==false) //Khong hop le
        {
                alert(Msg);
                return true;
        }
        return false;
}
function CheckNumber(Name,Msg,Value)
{
        if(isNaN(frm.elements[Name].value))
        {
                alert(Msg);
                return true;
        }
        return false;
}
function CheckNumber2(Name,Msg,Form)
{
        var obj=eval(Form)
        if(isNaN(obj.elements[Name].value))
        {
                alert(Msg);
                return true;
        }
        return false;
}
function NormalChar(stringIn)
{
         retval = false
         var i;
     for (i=0;i<=stringIn.length-1;i++) {
              //cho phep nhung ky tu tu A->Z, a->z, 0->9,va cac ky tu @,_
              if (((stringIn.charCodeAt(i) >= 48)&&(stringIn.charCodeAt(i) <= 57)) || ((stringIn.charCodeAt(i) >= 64)&&(stringIn.charCodeAt(i) <= 90)) || ((stringIn.charCodeAt(i) >= 97)&&(stringIn.charCodeAt(i) <= 122)) ||(stringIn.charCodeAt(i)==95) ||(stringIn.charCodeAt(i)==46) ||(stringIn.charCodeAt(i)==45)) {
                retval = true;
                }else{
                                retval = false;
                                break;
         }
     }
         return retval;
}
function cEmail(stringIn)
{
        if (stringIn.indexOf("@") < 1){
                return false;
        }else{
                st = stringIn.substr(stringIn.indexOf("@"));
                if(st.indexOf(".") == -1 || st.indexOf(".")==1){
                        return false;
                }else{
                        if(!NormalChar(stringIn)){
                                return false;
                        }else{
                                return true;
                        }
                }
        }
}
function CheckAcc(User,Pass,Repass)
{
        if(CheckText(User,"Ban hay nhap vao UserName","")) return true;
        if(CheckLen(User,"UserName",4)) return true;
        if(CheckText(Pass,"Ban hay nhap vao Password","")) return true;
        if(CheckLen(Pass,"Password",4)) return true;
        if(frm.elements[Pass].value!=frm.elements[Repass].value)
        {
                alert("Hai password khong giong nhau");
                return true;
        }
    return false;
}
function AddName()
{
//Option(frm.txt.value,lastindex)
//Option(giatri_1,giatri_2) co hai gia tri :
//giatri_1:noi dung text trong hop select
//giatri_2:gia tri value cua noi dung text nay
var lastindex;
var i=0;
var flag=0;
//****************
        flag=0;
        lastindex=frm.selname.options.length
        for(i=0;i<lastindex;i++)
        {//Check exit of value
                if(frm.cellname.value==frm.selname.options[i].value)
                {
                        flag=1;break;
                }
        }
        if(flag==0)
        {
                frm.selname.options[lastindex]=new Option(frm.cellname.value,frm.cellname.value)
        }
        else
        {
                window.alert("Ban da chon muc nay roi")
        }
}
function RemoveName()
{
        frm.selname.remove(frm.selname.selectedIndex)
}
function CheckMail(Name,Msg,Style)
{
        var mail=frm.elements[Name].value;
        var bien=mail.indexOf("@")
        var cham=mail.indexOf(".")
                if(bien==-1||bien==0||cham==-1||cham!=mail.length-4)//Mail:Kiem tra sai
                {
                        alert(Msg);
                        return true;
                }
        return false;
}
function CheckData(myArr)
{
        var tmp=new Array();
        var cnt=0;
        for(i=0;i<myArr.length;i++)
                if(myArr[i].checked==true)
                        tmp[cnt++]=myArr[i].value;
        return tmp.toString();
}
function CheckAll(Arr,flag)
{
        for(i=0;i<Arr.length;i++) Arr[i].checked=flag;
}
var check=false;
function CheckBoxAll()
{
        if(check==false) check=true;
        else check=false;
        CheckAll(frm.CheckID,check);
}
function GetChecked()
{
        var tmp=CheckData(frm.CheckID);
           var value=window.confirm ("Are you sure delete ?")
        if(value==true)
        {
            if(tmp!="") window.location.href="id.php?f=db&s=idarr&idarr="+tmp;
        }
    return true;
}
function PostSearch()
{
        frm_search.action="id.php?f=list&se=t&fp"
        frm_search.submit();
}
function ShowSearch()
{
        window.menu_search.style.posLeft=window.event.x;
        window.menu_search.style.posTop=window.event.y;
        window.menu_search.style.visibility="visible";
}
function CloseMenu()
{
        window.menu_search.style.visibility="hidden";
}
var f_menu=-1;
//document.onmousemove=MoveMenu
//document.onclick=FlagOn
function FlagOn(){f_menu=-f_menu;}
function MoveMenu()
{
        if(f_menu>0)
        {
                window.menu_search.style.posLeft=window.event.clientX + document.body.scrollLeft - (window.menu_search.offsetWidth/2) ;
                window.menu_search.style.posTop=window.event.clientY + document.body.scrollTop -10 ;

        }
}
//--------For IFRAME VIEW----------------------------------------------
function DoBold(Str)
{
      window.frames[Str].focus();
      window.frames[Str].document.execCommand('bold', false, null);
}
function DoItalic(Str)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('italic', false, null);
}
function DoUnderLine(Str)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('underline', false, null);
}
function DoLeft(Str)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('justifyleft', false, null);
}
function DoCenter(Str)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('justifycenter', false, null);
}
function DoRight(Str)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('justifyright', false, null);
}
function DoJustify(Str)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('justifyfull', false, null);
}
function DoBulletedList(Str)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('insertunorderedlist', false, null);
}
function DoNumberedList(Str)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('insertorderedlist', false, null);
}
function DoTXTColor(Str,color)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('forecolor', false, color);
}
function DoBGcolor(Str,color)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('BackColor', false, color);
}
function OpenTableColor(Str,mType)
{
        var win=window.open("../include_basic/bang_mau.php?obj="+Str+"&type="+mType,"NHDCOLOR","toolbar=no,menubar=no,width=250,height=180");
        win.focus();
}
function DoFont(Str,fName)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('fontname', false, fName);
}
function DoFontSize(Str,fSize)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('fontsize', false, fSize);
}
function DoLink(Str)
{
        window.frames[Str].focus();
        window.frames[Str].document.execCommand('createlink');
}
function DoInsertPic(Obj,imgPath,StrPro)
{
        var range=window.frames[Obj].document.selection.createRange();
        range.pasteHTML("<img src='../upload/nhd_"+imgPath+"'"+StrPro+">");
}
function UploadFile(mName)
{
        var win=window.open("../include_basic/upload_dialog.php?name="+mName,"NHD","width=410,height=260");
        win.focus();
}
var c_flag=1
function DoRefresh()
{
    //iView.document.execCommand('Refresh', false, null);
        c_flag=-c_flag;
        if(c_flag>0)
        {
                //View Code
                viewtieude.focus();
                iText = viewtieude.document.body.innerText;
                viewtieude.document.body.innerHTML =iText;
        }
        else
        {
                //Show
                viewtieude.focus();
                iHTML = viewtieude.document.body.innerHTML;
                viewtieude.document.body.innerText = iHTML;
        }
}
//--------EndFor IFRAME VIEW--------
function ShowSelect(mID,mSelChild,mSelTed)
{
    var i=0,flag=0,SelTedChild=0,m=0;
//    window.alert(mID)
        var mchild=document.getElementById(mID).getElementsByTagName("span");
        if(typeof(mchild)!="undefined")
        {
              var sel_child=document.getElementById(mSelChild);
              for(m=0;m<mchild.length;m++) sel_child.options[m]=null;
              for(i=0;i<mchild.length;i++)
              {
                      if(mchild[i].id==mSelTed && flag==0){SelTedChild=i;flag=1}
                      sel_child.options[i]=new Option(mchild[i].innerText,mchild[i].id);
              }
              sel_child.options[SelTedChild].selected=true;
        }//End if
}
//------Function For Open Dialog Zoom Images----------
function NHDIMGZOOM(mPath,mImgLarge,mImgSmall,mWidth,mHeight)
{
        this.path=mPath;
        this.imglarge=mImgLarge;
        this.imgsmall=mImgSmall;
        this.open=OpenZoom;
        this.width=(mWidth?mWidth:500);
        this.height=(mHeight?mHeight:500);
}
//var nhdimg=new NHDIMGZOOM("largezoom2.htm","ao1.jpg","ao2.jpg");
function OpenZoom()
{
        var mytext=showModalDialog(this.path,this,'dialogHeight:'+this.height+'px;dialogWidth:'+this.width+'px;center:yes;status:no;scroll: no;help: no;');
}

