﻿/*-----------------------------------*/
/*-  Kiem tra email --*/
/*-----------------------------------*/
function isEmail(s)
{
  if (s=="") return false;
  if(s.indexOf(" ")>0) return false;
  var i = 1;
  var sLength = s.length;
  if (s.indexOf(".")==sLength) return false;
  if (s.indexOf(".")<=0) return false;
  if (s.indexOf("@")!=s.lastIndexOf("@")) return false;

  while ((i < sLength) && (s.charAt(i) != "@"))
  { i++
  }

  if ((i >= sLength) || (s.charAt(i) != "@")) return false;
  else i += 2;

  while ((i < sLength) && (s.charAt(i) != "."))
  { i++
  }

  if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
   var str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghikjlmnopqrstuvwxyz-@._";
   for(var j=0;j<s.length;j++)
	if(str.indexOf(s.charAt(j))==-1)
		return false;
   return true;
}
/*-----------------------------------*/
/*-      kiem tra ten truy cap     --*/
/*-----------------------------------*/
function validTel(s){
	var str="0123456789)(- .+";
	if(s.length>=20||s.length<=5) return false;
	for(var i=0;i<s.length;i++){
		if(str.indexOf(s.charAt(i))==-1)	return false;
	}
	return true;

}
/*-----------------------------------*/
/*-      kiem tra ten truy cap     --*/
/*-----------------------------------*/
function isUsername(s)
{
  if (s=="") return false;
  if(s.indexOf(" ")>0) return false;
  var i = 1;
  var sLength = s.length;
   var str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghikjlmnopqrstuvwxyz._";
   for(var j=0;j<s.length;j++)
	if(str.indexOf(s.charAt(j))==-1)
		return false;
   return true;
}
/*-----------------------------------*/
/*-  Lay toa do x, y cua scrollbar --*/
/*-----------------------------------*/
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
/*-----------------------------------*/
/*-  Lay toa do x, y cua scrollbar --*/
/*-----------------------------------*/
function getElementById(id){
    if (!document.getElementById) {
        if (document.all) {
            document.getElementById = function (id) {
                return document.all[id];
            }
        }
        else {
            document.getElementById = function (id) {
            return document.layers[id]; }
        }
    }
}
/*-----------------------------------*/
/*-------                   ---------*/
/*-----------------------------------*/
function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
}
function IsNumeric(s){
    var str="0123456789";
	for(var i=0;i<s.length;i++){
		if(str.indexOf(s.charAt(i))==-1)return false;
	}
	return true;
}

/*-----------------------------------*/
/*-------     BUTTON NEXT   ---------*/
/*-----------------------------------*/
function NEXTCHOOSE( bienDem, nextto, idHidden, urlSend,arrayRequest,arrayValue,divLoading,divShow){
  var getValue=document.getElementById(idHidden);
  var i=getValue.value;
  i=parseInt(i);
  bienDem=parseInt(bienDem);
  nextto=parseInt(nextto);

  if(bienDem%nextto==0){
    if(i==(bienDem - nextto)){i=0;}
    else {i=i+nextto;}
  }else{
    if(i==(bienDem-(bienDem%nextto))){i=0;}
    else {i=i+nextto;}
  }
  document.getElementById(idHidden).value=i;
  var newRequest = [arrayRequest,'step','limit'];
  var newValue = [arrayValue,i,nextto];
  GETREQUEST(urlSend,newRequest,newValue,divLoading,divShow);
}
/*-----------------------------------*/
/*-------   BUTTON PREVIEW  ---------*/
/*-----------------------------------*/
function PREVIEWCHOOSE( bienDem, previewto, idHidden, urlSend,arrayRequest,arrayValue,divLoading,divShow){
    var getValue=document.getElementById(idHidden);
    var i=getValue.value;
    i=parseInt(i);
    bienDem=parseInt(bienDem);
    previewto=parseInt(previewto);
    if(i<=0){
      if(bienDem%previewto==0)i=bienDem - previewto;
      else i=bienDem-(bienDem%previewto);
    }
    else { i=i - previewto;}
    document.getElementById(idHidden).value=i;
    var newRequest = [arrayRequest,'step','limit'];
    var newValue = [arrayValue,i,previewto];
    GETREQUEST(urlSend,newRequest,newValue,divLoading,divShow);
}
/*-----------------------------------*/
/*-------                   ---------*/
/*-----------------------------------*/
function GETREQUEST(urlSend,arrayRequest,arrayValue,divLoading,divShow){
//ham truyen GETREQUEST voi cac parameter :
//urlSend: duong dan den thu muc chua file php xu ly
//arrayRequest: la doi tuong $_GET[''] trong file urlSend
//arrayValue: la gia tri cua $_GET['']
//divLoading: id cua div chua noi dung loading, waiting...
//divShow: id cua div trinh bay ket qua tra ve tu urlSend
    var request='';
    if(is_array(arrayValue) && is_array(arrayRequest)){
        for(var x in arrayValue){
            if(isNaN(arrayValue[x]) && (arrayValue[x].substr(0,1)=='#' || arrayValue[x].substr(0,1)=='.')){
                arrayValue[x] = arrayValue[x].substr(1,arrayValue[x].length);
                request += arrayRequest[x]+'='+document.getElementById(arrayValue[x]).value;
            }
            else
                request += arrayRequest[x]+'='+arrayValue[x]+'&';
        }
        request = request.substr(0,(request.length)-1);
    }else{
        if(isNaN(arrayValue) && (arrayValue.substr(0,1)=='#' || arrayValue.substr(0,1)=='.')){
            arrayValue = arrayValue.substr(1,arrayValue.length);
            request += arrayRequest+'='+document.getElementById(arrayValue).value;
        }
        else
            request += arrayRequest+'='+arrayValue;
    }
    $.ajax({
        url:urlSend,
        data:request,
        datatype:'html',
        type:'GET',
        beforeSend:function(){
            if(divLoading != '')
                if(is_array(divLoading)){
                    for(var x in divLoading){
                        $(divLoading[x]).fadeIn(500);
                    }
                }else
                    $(divLoading).fadeIn(500);
            if(is_array(divShow)){
                for(var x in divShow){
                    $(divShow[x]).fadeOut({queue:false,duration:50});
                }
            }else{
                $(divShow).fadeOut({queue:false,duration:50});
            }
        },
        success:function(data){
            if(is_array(divShow)){
                data = data.split('<!OO!>');
                for(var x in data){
                    $(divShow[x]).html(data[x]);
                    $(divShow[x]).fadeIn(500);
                }
            }else{
                $(divShow).html(data);
                $(divShow).fadeIn(1000);
            }
            if(divLoading != ''){
                if(is_array(divLoading)){
                    for(var x in divLoading){
                        $(divLoading[x]).fadeOut({queue:false,duration:50});
                    }
                }else
                    $(divLoading).fadeOut({queue:false,duration:50});
            }
        }
    });
}
/*-----------------------------------*/
/*-- CHANG SIZE AND RESTORE IMAGE ---*/
/*-----------------------------------*/
function changeSize(divImage, divText, changeWidth, changeHeight){
    if(divText!=''){
        $(divText).slideDown('slow');
    }
    divImage = document.getElementById(divImage);
    divImage.style.width=changeWidth;
    divImage.style.height=changeHeight;
}
function restoreSize(divImage, divText, changeWidth, changeHeight){
    if(divText!=''){
        document.getElementById(divText).style.display='none';
    }
    divImage = document.getElementById(divImage);
    divImage.style.width=changeWidth;
    divImage.style.height=changeHeight;
}
/*-----------------------------------*/
/*--   POST DU LIEU BANG AJAX     ---*/
/*-----------------------------------*/
function POSTREQUEST(urlSend,arrayRequest,divLoading,divShow){
    var request='';
    if(is_array(arrayRequest)){
        for(var x in arrayRequest){
            request += arrayRequest[x]+'='+document.getElementById(arrayRequest[x]).value+'&';
        }
        request = request.substr(0,(request.length)-1);
    }else{
        request += arrayRequest+'='+document.getElementById(arrayRequest).value;
    }
    $.ajax({
        url:urlSend,
        data:request,
        type:'POST',
        beforeSend:function(){
            if(divLoading != '')
                $(divLoading).fadeIn({queue:false,duration:500});
            if(divShow != ''){
                if(is_array(divShow)){
                    for(var x in divShow){
                        $(divShow[x]).fadeOut({queue:false,duration:50});
                    }
                }else{
                    $(divShow).fadeOut({queue:false,duration:50});
                }
            }
        },
        success:function(data){
            if(divShow != ''){
                if(is_array(divShow)){
                    data = data.split('<!OO!>');
                    for(var x in data){
                        if(data[x]=='success'){
                            $(divShow).hide();
                        }
                        else{
                          $(divShow[x]).html(data[x]);
                          $(divShow[x]).fadeIn({queue:false,duration:500});
                        }
                    }
                }else{
                    data = data.split('<!OO!>');
                    for(var x in data){
                        if(data[x]=='success'){
                            $(divShow).hide();
                        }
                        else{
                            $(divShow).html(data[x]);
                            $(divShow).fadeIn({queue:false,duration:500});
                        }
                    }
                }
            }
            if(divLoading != '')
                $(divLoading).fadeOut({queue:true,duration:50});
        }
    });
}
/*----------------------------------------*/
/*-- RESET TAT CA CAC FIELD TRONG FORM ---*/
/*----------------------------------------*/
function resetFieldInForm(arrayRequest){
    if(is_array(arrayRequest)){
        for(var x in arrayRequest){
            document.getElementById(arrayRequest[x]).value='';
        }
    }else{
        document.getElementById(arrayRequest).value='';
    }
}
function SHOWHIDE(divShowHide, arrayDiv){
      if ($(divShowHide).is(":hidden")) {
        $(divShowHide).slideDown({queue:false,duration:100});
      } else {
        $(divShowHide).hide({queue:false,duration:100});
      }
      if(arrayDiv != ''){
        if(is_array(arrayDiv)){
            for(var x in arrayDiv)
                $(arrayDiv[x]).hide({queue:false,duration:100});
        }else{
            $(arrayDiv).hide({queue:false,duration:100});
        }
      }
}
function FADEOUT(div){
    $(div).fadeOut({queue:false,duration:100});
}
//-----------------------------------------
//-------------             ---------------
//-----------------------------------------
//var checkshow=true;
function CHECKSHOWHIDE(div,classNormal, classSpecial, arrayDiv)
{
    var checkshow = document.getElementById(div).className;
     if(checkshow == classNormal)
     {
       $("#"+div).removeClass(classNormal);
       $("#"+div).addClass(classSpecial);

        if(arrayDiv!=''){
            if(is_array(arrayDiv)){
                for(var x in arrayDiv){
                    $("#"+arrayDiv[x]).removeClass(classSpecial);
                    $("#"+arrayDiv[x]).addClass(classNormal);
                }
            }else{
                $("#"+arrayDiv).removeClass(classSpecial);
                $("#"+arrayDiv).addClass(classNormal);
            }
         }

     //  checkshow=false;
     }else
     {
       $("#"+div).removeClass(classSpecial);
       $("#"+div).addClass(classNormal);
     //  checkshow=true;
     }
}
//-----------------------------------------
//-------     COPY TO CLIPBOARD     -------
//-----------------------------------------
function CopyToClipboard(text2copy) {
   if(window.clipboardData) {
        window.clipboardData.setData("Text",text2copy);
   }
   else {
       var flashcopier = 'flashcopier';
       if(!document.getElementById(flashcopier)) {
           var divholder = document.createElement('div');
           divholder.id = flashcopier;
           document.body.appendChild(divholder);
       }
       document.getElementById(flashcopier).innerHTML = '';
       var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
       document.getElementById(flashcopier).innerHTML = divinfo;
   }
   alert("Đã copy vào clipboard. Bạn chỉ cần bấm Ctrl+V.");
}
/*-------------------------------------------------*/
/*--   POST DU LIEU BANG AJAX HOAN CHINH HON    ---*/
/*-------------------------------------------------*/
function POSTFUNCTION(urlSend , arrayRequest , arrayValue , divLoading , divShow){
    var request='';
    if(is_array(arrayRequest) && is_array(arrayValue)){
        for(var x in arrayRequest){
            if(IsNumeric(arrayValue[x]))
                request += arrayRequest[x]+'='+arrayValue[x]+'&';
            else
                request += arrayRequest[x]+'='+document.getElementById(arrayValue[x]).value+'&';
        }
        request = request.substr(0,(request.length)-1);
    }else{
        if(IsNumeric(arrayValue))
            request += arrayRequest+'='+arrayValue;
        else
            request += arrayRequest+'='+document.getElementById(arrayRequest).value;
    }
    $.ajax({
        url:urlSend,
        data:request,
        type:'POST',
        beforeSend:function(){
            if(divLoading != '')
                $(divLoading).fadeIn({queue:false,duration:500});
            if(divShow != ''){
                if(is_array(divShow)){
                    for(var x in divShow){
                        $(divShow[x]).fadeOut({queue:false,duration:50});
                    }
                }else{
                    $(divShow).fadeOut({queue:false,duration:50});
                }
            }
        },
        success:function(data){
            if(divShow != ''){
                if(is_array(divShow)){
                    data = data.split('<!OO!>');
                    for(var x in data){
                        if(data[x]=='success'){
                            $(divShow).hide();
                        }
                        else{
                          $(divShow[x]).html(data[x]);
                          $(divShow[x]).fadeIn({queue:false,duration:500});
                        }
                    }
                }else{
                    data = data.split('<!OO!>');
                    for(var x in data){
                        if(data[x]=='success'){
                            $(divShow).hide();
                        }
                        else{
                            $(divShow).html(data[x]);
                            $(divShow).fadeIn({queue:false,duration:500});
                        }
                    }
                }
            }
            if(divLoading != '')
                $(divLoading).fadeOut({queue:true,duration:50});
        }
    });
}
function addClass(id, oldClass, newClass){
    document.getElementById(id).className=newClass;
}
function removeClass(id, oldClass, newClass){
    document.getElementById(id).className=oldClass;
}
/**********************************/
function popupForm(value, sendURL, arrayRequest, arrayValue, divLoading, divShow){
    var id = document.getElementById(value).getAttribute("id");
    var obj = document.getElementById(value);
    var rong = screen.availWidth;
    var cao = screen.availHeight;
    var scroll = getScrollXY();
    var x = scroll[0];
    var y = scroll[1];

    var divRong = document.getElementById(value).style.width;
    var divCao = document.getElementById(value).style.height;
    divRong = parseInt(divRong);
    divCao = parseInt(divCao);

    obj.style.left = (parseInt(rong)/2) - (parseInt(divRong/2)) + 'px';
    obj.style.top = (parseInt(cao)/2) + (parseInt(y)) - (parseInt(divCao)) + 'px';
    //$('#'+value).fadeIn(1000);
    GETREQUEST(sendURL, arrayRequest, arrayValue, divLoading, divShow);
}
/***********************************/
function formCenter(value){
    var id = document.getElementById(value).getAttribute("id");
    var obj = document.getElementById(value);
    var rong = screen.availWidth;
    var cao = screen.availHeight;
    var scroll = getScrollXY();
    var x = scroll[0];
    var y = scroll[1];

    var divRong = document.getElementById(value).style.width;
    var divCao = document.getElementById(value).style.height;
    divRong = parseInt(divRong);
    divCao = parseInt(divCao);
    obj.style.left = (parseInt(rong)/2) - (parseInt(divRong/2)) + 'px';
    if(y==0)
        obj.style.top = (parseInt(cao)- parseInt(divCao))/3 - parseInt(40) + 'px';
    else
        obj.style.top = (parseInt(cao)/2) + (parseInt(y)) - (parseInt(divCao)) + 'px';
}
/***********************************/
function closePopUpForm(value){
    $('#'+value).fadeOut(1000);
}
/***********************************/
function modelessDialogShow(url,width,height){
if (document.all&&window.print) //if ie5
eval('window.showModelessDialog(url,window,"dialogWidth:'+width+'px;dialogHeight:'+height+'px;edge:Raised;center:1;help:0;resizable:1;")');
else
eval('window.open(url,"a","width='+width+'px,height='+height+'px,resizable=1,scrollbars=1,copyhistory=yes")')
}
/***********************************/
function getValueSelect(id, hidden){
	var len = document.getElementById(id).length;
	
	var selected = document.getElementById(id);
	var hiddenField = document.getElementById(hidden);
	
	var str = '';
	var temp = '';
	if(selected.checked==true){
		str += hiddenField.value + ',' + selected.value;
		hiddenField.value = str;
		alert(str);
	}else{
		var arrayValue = hiddenField.value;
		arrayValue = arrayValue.split(',');
		for(i = 0; i < arrayValue.length; i++){
			if(arrayValue[i] != selected.value) {
				str += arrayValue[i] + ',';
			}
			else {
				str += str + '';			
			}
		}
		alert(str);
	}
}
/************************************/
function checkAll(){
	var len = document.frm.chk.length;
	alert(len);
}

/*
****************************************************/
function getstring(){
    var str="";
    var alen=document.frm.chk.length;

    if (alen>1){
    	for(var i=0;i<alen;i++)
    		if(document.frm.chk[i].checked==true) str+=document.frm.chk[i].value+",";
    }else{
    	if(document.frm.chk.checked==true) str=document.frm.chk.value;
    }
    document.frm.arrayid.value=str;
}
/*
****************************************************/
function docheck(){
    var alen=document.frm.chk.length;

    if(document.frm.chkall.checked == true){
        if (alen>1){
        	for(var i=0;i<alen;i++)
        		document.frm.chk[i].checked=true;
        }else{
        		document.frm.chk.checked=true;
        }
    }
    else{
        if (alen>1){
        	for(var i=0;i<alen;i++)
        		document.frm.chk[i].checked=false;
        }else{
        		document.frm.chk.checked=false;
        }
    }
    getstring();
}
/*
****************************************************/
function docheckone(){
    var alen=document.frm.chk.length;
    var isChecked=true;
    if (alen>1){
    	for(var i=0;i<alen;i++)
    		if(document.frm.chk[i].checked==false)
    			isChecked=false;
    }else{
    	if(document.frm.chk.checked==false)
    		isChecked=false;
    }
    document.frm.chkall.checked=isChecked;
    getstring();
}
