/******
NHD DTool CopyRight 2000-2008
******/
/***Constant Variable*******/

var MIN_PASSWORD_CHARACTER=3;
var MAX_PASSWORD_CHARACTER=50;
var PATH_SYSTEM_STYLE=get_base_path()+"dtool/dtool/css/";
var PATH_SYSTEM_IMAGES=get_base_path()+"dtool/dtool/images/";
var GLOBAL_TMP_URL='';

function go2url(m_url){	window.location.href=m_url;}
function get_object(id_object){	var m_obj=document.getElementById(id_object);
	if(m_obj){		return m_obj;	}else{ ('Id Object not found: '+id_object); }
}
function get_base_path(){	var base_path=new String(document.location.href);
	var pos=base_path.indexOf('adtool');
	if(pos==-1){
		pos=base_path.indexOf('dtool');
		if(pos==-1){pos=base_path.indexOf('avtool');}
	}
	return base_path.substr(0,pos);}
function URL2SEO(m_url){}
/*****Function Global**********************************************************/
function check_is_blank(m_string){
  for(var i=0;i<m_string.length;i++){
		if(m_string.charCodeAt(i)!=32){return false;}
  } return true;
}
function exists_blank(m_string){
  for(var i=0;i<m_string.length;i++){
		if(m_string.charCodeAt(i)==32){return true;}
  } return false;
}
function check_normal_char(mString){
	if(check_is_blank(mString)){return false;}
	var numCode=0;
	for(var i=0;i<mString.length;i++){
		numCode=mString.charCodeAt(i);
		if( numCode>="A".charCodeAt(0) && numCode<="Z".charCodeAt(0) ){}else
		if( numCode>="a".charCodeAt(0) && numCode<="z".charCodeAt(0) ){}else
		if( numCode>="0".charCodeAt(0) && numCode<="9".charCodeAt(0) ){}else
		if( numCode=="@".charCodeAt(0) || numCode=="_".charCodeAt(0) ){}else
		if( numCode==".".charCodeAt(0) || numCode=="-".charCodeAt(0) ){}else{return false;}
	}
	return true;
}
function valid_email(stringIn){
	if (stringIn.indexOf("@") < 1){
		return false;
	}else{
	  var st = stringIn.substr(stringIn.indexOf("@"));
	  if(st.indexOf(".") == -1 || st.indexOf(".")==1){
	    return false;
	  }else{
	  	return check_normal_char(stringIn);
	  }
	}
}
function check_number(m_id,m_msg){	var obj=get_object(m_id);
	if(obj){
		var m_value=obj.value;
		if(isNaN(m_value) || check_is_blank(m_value)){
	  	window.alert(m_msg);
	  	obj.focus();
	  	return false;
		} return true;
	}else{return false;}
}
/*****Function Global**********************************************************/
function invalid_text(id_obj,msg_show){	var obj=get_object(id_obj);	if(obj){		if(check_is_blank(obj.value)){
			window.alert(msg_show);
			obj.focus();
			return true;
		}else{return false;}	}else{return true;}}
function invalid_account(id_obj,msg_show){	var obj=get_object(id_obj);	if(obj){
		if( check_normal_char(obj.value) ){			return false;		}else{			window.alert(msg_show);
			obj.focus();			return true;		}
	}else{return true;}
}
function invalid_email(id_obj,msg_show){	var obj=get_object(id_obj);
	if(obj){
		if( !check_is_blank(obj.value) && valid_email(obj.value) && !exists_blank(obj.value) ){
			return false;
		}else{
			window.alert(msg_show);
			obj.focus();
			return true;
		}
	}else{return true;}
}
function DocumentLoad(func){
	var onLoad = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	}else{
		window.onload = function(){
			onLoad();
			func();
		}
	}
}
function checked_all_items(mParent){	var pid=document.getElementsByName('dtool_check_data[]');
	if(pid){		var m_type=(mParent.checked?1:0);
  	for(var i=0;i<pid.length;i++){  		pid[i].checked=m_type;
  		if(m_type){  			get_object(pid[i].value).className='row_checked';  		}else{  			get_object(pid[i].value).className='';  		}
  	}
  	return true;
  }else{window.alert('Can not found id: dtool_check_data[]');}
}
function delete_checked_items(frm_name,m_url){
	if(check_checked_items()){		if(m_url){			document.forms[frm_name].action +=m_url;
		}
		document.forms[frm_name].submit();
	}else{
		window.alert('Please check items');
	}
}
function check_checked_items(){
	var pid=document.getElementsByName('dtool_check_data[]');
	if(pid){
  	for(var i=0;i<pid.length;i++){
  		if(pid[i].checked==true){return true;}
  	}
  }else{window.alert('Can not found object name: dtool_check_data[]');}
  return false;
}
function checked_all_checkbox(mParent,list_checkbox_name){
	var pid=document.getElementsByName(list_checkbox_name);
	if(pid){
		var m_type=(mParent.checked?1:0);
  	for(var i=0;i<pid.length;i++){
  		pid[i].checked=m_type;
  	}
  	return true;
  }else{window.alert('Can not found id: '+list_checkbox_name);}
}
/*****HighLight Row Table**************/
function initRowsHighLight(rows,id_selected){	for(var j=1;j<rows.length;j++){		if(rows[j].className!='none'){
			if(rows[j].id==id_selected){
				rows[j].className="tbl_row_data_selected";
			}else{				rows[j].onmouseover=function(){					if(this.className!='row_checked'){							this.className="tbl_list_data_hover";
					}
				}
				rows[j].onmouseout=function(){					if(this.className!='row_checked'){
						this.className="none";
					}
				}
			}
		}//--End if--
	}
}
function initTableHighLight(id_table,id_selected){
	var tables = document.getElementsByTagName('table');
	for(var i=0;i<tables.length;i++){
		if(tables[i].id==id_table){	  	initRowsHighLight(tables[i].getElementsByTagName("tr"),id_selected);
	  	break;
	  }
	}
}
function checked_on_rows(mParent){	if(mParent.checked){		get_object(mParent.value).className='row_checked';	}else{		get_object(mParent.value).className='';	}}
/*****HighLight Row Table**************/
/****Show Box Search****/
function show_box_search(){	var box_search=get_object('id_box_search');
	if(box_search){		box_search.style.display='block';	}else{		window.alert('Can not show box search');	}}
function show_object(id_object,m_type){	var object=get_object(id_object);
	object.style.display=m_type;}
/****Show Box Search****/

/*****Begin: For Cookies*************/
function getCookie(c_name){
	if (document.cookie.length>0){
  	c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
	    c_start=c_start + c_name.length+1 ;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length
	    	return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
/*****End: For Cookies***************/
function show_perpages(url,obj){	url+='&perpages='+obj.value+'&review=1';	window.location.href=url;}
function get_window_size(){	var d_width=630,d_height=460;
	if(parseInt(navigator.appVersion)>3){
		if (navigator.appName=="Netscape"){
			d_width =window.innerWidth;
			d_height=window.innerHeight;
		}
		if(navigator.appName.indexOf("Microsoft")!=-1){
			d_width =document.body.offsetWidth;
			d_height=document.body.offsetHeight;
		}
	}
	return new Array(d_width,d_height);}
/*****Ajax Data commnon********************************************************/
function show_customer_data(m_url,m_id_des,m_obj){	//alert(m_url);
	if(document.getElementById(m_id_des)){		getDataReturnText(INDEX_PAGE+'?'+m_url+'&idxlang='+CURRENT_LANGUAGE,status_customer_data,m_id_des);
	}else window.alert("Can not found Object:"+m_id_des);
}
function status_customer_data(m_txt,m_id){	//alert(m_txt);
	if(obj=document.getElementById(m_id)){
		obj.innerHTML=m_txt;
	}
}
function post_customer_data(m_url,m_data,m_id_des,my_var){
	if(document.getElementById(m_id_des)){
		postDataReturnText(INDEX_PAGE+'?idxlang='+CURRENT_LANGUAGE+'&'+m_url,m_data,status_pos_customer_data,m_id_des,my_var);
	}
}
function status_pos_customer_data(m_txt,m_id,m_var){
	//alert(m_txt);
	if(obj=document.getElementById(m_id)){
		obj.innerHTML=m_txt;
	}
	if(m_var){
		window.location.href=m_var;
	}
}
/*****Ajax Data commnon********************************************************/
function check_move_child(objcate,id_delete,msg_itself,msg_subchild){
	var id_move=objcate.value;
	var list_child=get_child_select_box(objcate,id_delete);
	if(id_delete==id_move){
		window.alert(msg_itself);
		return false;
	}else{		if(check_in_array_child(list_child,id_move)){			window.alert(msg_subchild);
			return false;		}
	}
	return true;
}
function get_child_select_box(objSelect,id_item){	for(var i=0;i<objSelect.length;i++){		if(objSelect.options[i].value==id_item){			return objSelect.options[i].id;		}	}
	return false;}
function check_in_array_child(m_string,m_id){	var tmp_child=m_string.split(",");
	if(tmp_child.length>=2){		for(var i=0;i<tmp_child.length;i++){			if(tmp_child[i]==m_id){ return true; }		}	}
	return false;}
function empty_options(obj,priKey){
	for(var i=obj.length-1;i>=0;i--){		if(priKey){    	if(obj.options[i].value!=priKey){    		obj.remove(i);    	}		}else{
			obj.remove(i);
		}
	}
}
function add_options(obj,m_key,m_value){
	obj.options[obj.length]=new Option(m_value,m_key);
}
function set_selected(obj,m_value){
	for(var i=0;i<obj.length;i++){
		if(obj.options[i].value==m_value){
			obj.options[i].selected=true;
			break;
		}
	}
}
function openWindow(m_href,m_width,m_height){
	if(!m_width){	m_width=400; m_height=400; }
	var myWin=window.open(m_href,"DTOOL_V4","width="+m_width+"px,height="+m_height+"px,resizable=1,scrollbars=yes");
	//var myWin=window.open(m_href,"DTOOL_V4");
			myWin.focus();
}
/****Parent Child**********/
function D_PARENT_CHILD(){
	this.pos=new Array();
	this.data=null;
	this.size=0;
	this.init=function(max_row){
  	this.data=new Array(max_row);
  	for(var i=0;i<max_row;i++){
	  	this.data[i]=new Array();
	  }
	}
	this.addroot=function(list_object){
		this.size=list_object.length;
		for(var i=0;i<this.size;i++){
			this.pos[list_object[i].id]=i;
			this[i]={id:list_object[i].id,obj:get_object(list_object[i].id),count:0,init:list_object[i].init};
		}
	}
	this.addchild=_addchild;
	this.change_parent=_change_dparent;
}
function _addchild(id_obj,id_parent,id_child,m_value){	var pos=this.pos[id_obj];
	this.data[pos-1][this[pos].count++]={id:id_obj,idpa:id_parent,idchild:id_child,value:m_value};
}
function _change_dparent(obj){
	var pos=this.pos[obj.id]+1;
	var cpos=0,cchild=0;
	setCookie(obj.id,obj.value,3);
	if(pos<this.size){
		for(var i=pos;i<this.size;i++){
			cpos=i-1; cchild=0;
			empty_options(this[i].obj);
			if(this.data[cpos]){
				//this[i].init=getCookie(this[i].id);
				for(var j=0;j<this[i].count;j++){
					if(this[i-1].obj.value==this.data[cpos][j].idpa){
						add_options(this[i].obj,this.data[cpos][j].idchild,this.data[cpos][j].value);
						cchild++;
					}
				}
				set_selected(this[i].obj,this[i].init);
			}
			if(cchild==0){
				add_options(this[i].obj,0,'No Items');
			}
		}
	}
}
/****Parent Child**********/

/****Delete Category in File List*********************/
function confirm_delete_category(m_link,m_msg,id_item){
	var obj=document.getElementsByName('dconfig_category[]');
	var objcate=get_object('did_cate_move');
	if(obj){
		for(i=0;i<obj.length;i++){
			if(obj[i].checked==true){
				if(obj[i].value=='move_all'){
					if(check_move_child(objcate,id_item,TBL_MSG_DELETE[0],TBL_MSG_DELETE[1])){
							document.location=m_link+"&tcate="+obj[i].value+"&uvar="+objcate.value;
					}
				}else{
					var m_confirm=window.confirm(m_msg+TBL_MSG_DELETE[2]+obj[i].title);
					if(m_confirm){
						document.location=m_link+"&tcate="+obj[i].value;
					}
				}
				return true;
			}
		}
	}
	window.alert(TBL_MSG_DELETE[3]);
	return false;
}
/*****For Set Account/Groups Permission*******************/
function checkbox_modules(mParent,list_checkbox_name){
	var pid=document.getElementsByName(list_checkbox_name);
	if(pid){
		var m_type=(mParent.checked?1:0);
  	for(var i=0;i<pid.length;i++){
  		if(pid[i].disabled==false){
	  		pid[i].checked=m_type;
	  		if(pid[i].onclick){
	  			pid[i].onclick();
	  		}
	  	}
  	}
  	return true;
  }else{window.alert('Can not found id: '+list_checkbox_name);}
}
function check_parent_child(mParent,list_child_name){
	var pid=document.getElementsByName('modules[]');
	var m_type=(mParent.checked?0:1);
	var m_type_child=(mParent.checked?1:0);
	var list_child=list_child_name.split(",");
	var id_child=0;
	if(list_child.length>1 || !isNaN(list_child)){
		for(var i=0;i<pid.length;i++){
			for(var j=0;j<list_child.length;j++){
				id_child=list_child[j];
				if(pid[i].value==id_child){
					if(pid[i].id==0){
						pid[i].disabled=m_type;
						pid[i].checked=m_type_child;
					}
				}
			}
		}
	}
}
function checked_all_child(mParent,list_child_name){
	var pid=document.getElementsByName('modules_account[]');
	var m_type=(mParent.checked?1:0);
	var list_child=list_child_name.split(",");
	var id_child=0;
	if(list_child.length>1 || !isNaN(list_child)){
		for(var i=0;i<pid.length;i++){
			for(var j=0;j<list_child.length;j++){
				id_child=list_child[j];
				if(pid[i].value==id_child){
					pid[i].checked=m_type;
				}
			}
		}
	}
}
function open_dialog_file_user(m_url,m_width,m_height){
	if(!m_width){m_width=500; m_height=425;}
	var m_win=window.open(m_url,"DTOOL_SELECT_FILE","width="+m_width+"px,height="+m_height+"px,resizable=yes,scrollbars=yes");
	m_win.focus();
}
function open_dialog_file(m_url,m_width,m_height){	if(!m_width){m_width=500; m_height=425;}
	var m_win=window.open(m_url,"DTOOL_SELECT_FILE","width="+m_width+"px,height="+m_height+"px,resizable=yes,scrollbars=yes");
	m_win.focus();
}
function open_dialog_file_inbox(m_url,m_width,m_height){
	var INDEX_PAGE_INBOX="../../../../../../index.php";
	if(!m_width){m_width=500; m_height=380;}
	var m_href=INDEX_PAGE_INBOX+'?n=dialog_file&ispc=1&idtxt='+m_url;
	var m_win=window.open(m_href,"DTOOL_SELECT_FILE","width="+m_width+"px,height="+m_height+"px,resizable=yes,scrollbars=yes");
	m_win.focus();
}
function get_extension_file(m_path){
	var tmp=new String();
	tmp=m_path.toLowerCase();
	var ext=tmp.substr(tmp.length-3,tmp.length);
	return ext;
}
/*****Function For Object Select Images*****/
function change_select_images(objFile,objView){	if(objView){		var m_ext=get_extension_file(objFile.value);
		if(m_ext){	  	switch(m_ext){
	  		case 'gif': case 'jpg': case 'png':
					objView.src=BASE_URL_WEBSITE+objFile.value;
	  		break;
	  		default:
					objView.src=PATH_SYSTEM_IMAGES+'icon_'+m_ext+'.gif';
	  		break;
	  	}
	   }else{
	   	window.alert('You can not upload this file');
		}
	}
}
function CheckUseImage(objCheck,m_name){
	var objView=get_object('pic_view_'+m_name);
	var objFile=get_object('id_file_'+m_name);
	if(objCheck.checked){
		GLOBAL_TMP_URL=objFile.value;
		objFile.value='';
		objView.src=PATH_SYSTEM_IMAGES+"no_images.jpg";
	}else{
		objFile.value=GLOBAL_TMP_URL;
		change_select_images(objFile,objView);
	}
}
function CheckBrowserNetscape(){
	return (navigator.appName=="Netscape")?true:false;
}
/*****For Set Account/Groups Permission*******************/
function change_background_tab(m_pos,m_color_1,m_color_2,tabName,m_bg){
	var name_tab=(tabName)?tabName:'nhd_tab';
	var all_tab=document.getElementsByName(name_tab);
	for(var i=0;i<all_tab.length;i++){
		if(m_bg){
			all_tab[i].style.backgroundImage="url('"+m_color_1+"')";
		}else{
			all_tab[i].style.backgroundColor=m_color_1;
		}
	}//--end for--
	if(m_pos<0 || m_pos>=all_tab.length) m_pos=0;
	if(all_tab){
		if(m_bg){
			all_tab[m_pos].style.backgroundImage="url("+m_color_2+")";
		}else{
			all_tab[m_pos].style.backgroundColor=m_color_2;
		}
	}else alert("Can not found tab");
}//--end function--
