/************** START GLOBAL VARIABLE ON ALL PROJECT **************************/

/* type of images can upload to server */
var LIST_TYPE_IMG_UPLOAD=new Array('GIF','JPG','SWF','BMP');

/************** END GLOBAL VARIABLE ON ALL PROJECT ****************************/

/************** START MAIN FUNCTION *******************************************/

/* check type of brwowser */
function check_browser(){
	var AGT = navigator.userAgent.toLowerCase();
	return ((AGT.indexOf("msie") != -1) && (AGT.indexOf("opera") == -1));
}//--end function--

function change_parent(list_change,m_level){
	for(j=m_level;j<list_change.length;j++){
		change_child(list_change[j][0].value,list_change[j][1],list_change[j][2],list_change[j][3]);
	}
}//--end function--

function change_child(m_id_parent,obj_src,obj_des,id_selected){
	empty_select(obj_des);
	var count_item=0;
	for(var i=0;i<obj_src.size;i++){
	  if(obj_src[i].id_parent==m_id_parent){
	  	count_item++;
	    add_select(obj_des,obj_src[i].id,obj_src[i].value);
	  }
	}//--end for--
	if(count_item==0){
		add_select(obj_des,DEFAULT[0],DEFAULT[1]);
	}else{//have data
		set_selected(obj_des,id_selected);
	}
}//--end function--

/* end main function */

/* start main object */

function NHD_DATA_CHILD(m_id,m_id_parent,m_value){
  this.id=m_id;
  this.id_parent=m_id_parent;
  this.value=m_value;
}//--end object data */

/* object list hole all data */
function NHD_OBJECT_LIST(){
  this.size=0;
  this.add=_add;
  function _add(m_obj){
  	this[this.size]=m_obj;
    this.size++;
  }
}//--end object list--

/* end main object */

/* start function for select box */

function empty_select(m_obj){
  for (var m=m_obj.options.length-1;m>=0;m--){
  	m_obj.options[m]=null;
  }
}//--end function--

function add_select(m_obj,m_key,m_value){
	m_obj.options[m_obj.length]=new Option(m_value,m_key);
}//--end function--

function set_selected(obj_des,m_id){
	for(var i=0;i<obj_des.length;i++){
	  if(obj_des.options[i].value==m_id){
	  	obj_des.options[i].selected=true;
	    break;
	  }
	}
}//--end function--

/* end function for select box */
/**************For Admin*******************************************************/
var MIN_PASSWORD_CHARACTER=3;
var MAX_PASSWORD_CHARACTER=15;

function check_user(m_name,m_msg){
	var m_value=document.all[m_name].value;
	if(!check_blank(m_value)){
		window.alert(m_msg);
		return false;
	}else{//validate blank
		if(!check_normal_char(m_value) || (m_value.length<MIN_PASSWORD_CHARACTER || m_value.length>MAX_PASSWORD_CHARACTER) ){ //Khong hop le
			window.alert(m_msg);
			return false;
    }else{//validate
			return true;
		}
	}
}//---Output: true(valid) | false(invalid)--------------------------------------
function check_password(m_name,m_msg){
	var m_value=document.all[m_name].value;
	if(!check_blank(m_value)){
		window.alert(m_msg);
		return false;
	}else{//validate blank
		if(!check_normal_char(m_value) || (m_value.length<MIN_PASSWORD_CHARACTER || m_value.length>MAX_PASSWORD_CHARACTER) ){ //Khong hop le
			window.alert(m_msg);
			return false;
    }else{//validate
			return true;
		}
	}
}//---Output: true(valid) | false(invalid)--------------------------------------
function check_blank(m_string){
  for(var i=0;i<m_string.length;i++){
    if(m_string.charCodeAt(i)!=32) return true;//<>blank
  } return false;
}//Output: true(Not All blank) | false(All blank)
function check_text(m_name,m_msg){	var m_value=document.all[m_name].value;
	if(!check_blank(m_value)){
		window.alert(m_msg);
		return false;
	}else{//validate
		return true;
	}
}//--Output: true(Valid) | false(Invalid)---------------------------------------
function check_email(m_name,m_msg){
	var m_value=document.all[m_name].value;
	if(!check_blank(m_value) || !valid_email(m_value) ){
		window.alert(m_msg);
		return false;
	}else{//validate
		return true;
	}
}//--Output: true(Valid) | false(Invalid)---------------------------------------
function check_number(m_name,m_msg){
	var m_value=document.all[m_name].value;
	if(isNaN(m_value) || !check_blank(m_value)){
  	window.alert(m_msg);
  	return false;
	}else{
  	return true;
	}
}//--Output True(Number)--False(Not number)-------------------------------------
function valid_email(stringIn){
	if (stringIn.indexOf("@") < 1){
		return false;
	}else{
	  st = stringIn.substr(stringIn.indexOf("@"));
	  if(st.indexOf(".") == -1 || st.indexOf(".")==1){
	    return false;
	  }else{
	    if(!check_normal_char(stringIn)){
	      return false;
	    }else{
	    	return true;
	    }
	  }
	}
}//-----------------------------------------------------------------------------
function check_normal_char(stringIn){
  for (var 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)){//Hop le
    }else return false;
  }//End for
  return true;
}//-----------------------------------------------------------------------------
function check_confirm(obj_root,obj_check,Msg){
	if(document.all[obj_root].value!=document.all[obj_check].value){
		window.alert(Msg);
		return false;
	}else{
  	return true;
	}
}//-----------------------------------------------------------------------------
function change_row(m_obj,m_color){
	m_obj.style.backgroundColor=m_color;
}
/**************New Here**************************/
function get_child_of_key(mkey,mArrData,mObjChild){
	empty_select(mObjChild);
	for(var i=0;i<mArrData[mkey].length;i++){
		mObjChild.options[i]=new Option(mArrData[mkey][i],mArrData[mkey][i]);
	}
}//-----------------------------------------------------------------------------
function check_extension(m_file){
	if(m_file.indexOf(".",0)>0){
		return true;
	}	return false;
}//-----------------------------------------------------------------------------
function close_boxsearch(){
	document.all['mnusearch'].style.display='none';
}//-----------------------------------------------------------------------------
var SCREEN_WIDTH=window.screen.width;
function show_boxsearch(){
	var search_pid;
	search_pid=document.all['mnusearch'];
	//search_pid.style.left=200;//window.event.x;
	search_pid.style.left=(SCREEN_WIDTH/2)-150;
	search_pid.style.top=40;//window.event.y;
	search_pid.style.display='block';
	document.all['valuesearch'].focus();
}//-----------------------------------------------------------------------------
var f_menu=-1;
document.onmousemove=move_boxsearch
function start_search_on(){f_menu=-f_menu;}
function move_boxsearch(){
	if(f_menu>0){
		var search_pid=document.all['mnusearch'];
	  search_pid.style.posLeft=window.event.clientX + document.body.scrollLeft - (search_pid.offsetWidth/2) ;
	  search_pid.style.posTop=window.event.clientY + document.body.scrollTop -10 ;
	}
}//-----------------------------------------------------------------------------
function CheckUserSelected(){
	if(document.getElementsByName('de_checked[]')){
		var pid_ch=document.getElementsByName('de_checked[]');
		for(i=0;i<pid_ch.length;i++){
			if(pid_ch[i].checked==1) return true;
		}
	}
	return false;
}//-----------------------------------------------------------------------------
function CheckUserSelected2(){
  if(document.getElementsByName('de_checked[]')){
    var list_id=new String();list_id='0';
    var pid_ch=document.getElementsByName('de_checked[]');
    for(i=0;i<pid_ch.length;i++){
      if(pid_ch[i].checked==1){//checked
        list_id+=','+pid_ch[i].value;
      }
    }//end for
    return (list_id=='0')?false:list_id;
  }
  return false;
}//-----------------------------------------------------------------------------
function SendIdChecked(m_form){
  if(CheckUserSelected()){
    var value=window.confirm('Are you sure Remove checked ?');
    if(value){
    	window.document.forms[m_form].submit();
  	}
  }else{
  	window.alert("Please check Item");
    return false;
  }
}//-----------------------------------------------------------------------------
function user_checked_all(mParent){
	var pid=document.getElementsByName('de_checked[]');
  for(i=0;i<pid.length;i++){pid[i].checked=parseInt(mParent.id)}
  mParent.id=(mParent.id==0)?1:0;
}//-----------------------------------------------------------------------------

function check_user_checked(m_obj){
  for(var i=0;i<m_obj.length;i++){
  	if(m_obj[i].checked==true) return true;
  } return false;
}//---input: object name--|--output: true(alest checked) or false(not checked)--

function CheckInList(mObj,mValue){//Return : true->Exit;false->Does not exit
	var tmp=document.getElementById(mObj);
	for(var i=0;i<tmp.length;i++)
		if(tmp.options[i].value==mValue) return true;
	return false;
}//-----------------------------------------------------------------------------
function Remove_A_Value(mObj){
  var i=0;
  var tmp=document.getElementById(mObj);
  for(i=tmp.length-1;i>=0;i--){
     if(tmp.options[i].selected==true) tmp.remove(i);
  }//End For
}//-----------------------------------------------------------------------------
function Add_A_Value(mID,mKey,mValue){
	var tmp=document.getElementById(mID);
  tmp.options[tmp.length]=new Option(mValue,mKey);
}//-----------------------------------------------------------------------------
function convert_path_img(m_path){
	var tmp=m_path.replace("\\","/");
	return "file://"+tmp;
}//-----------------------------------------------------------------------------
/* change select images */
function change_select_images(m_root,img_name){
	if(m_root.value!=""){
	  var tmp,m_type;
	  if(tmp=document.all[img_name]){
	    if(m_type=check_upload_type(m_root.value)){
	      if(m_type=='SWF'){
	      	window.alert('You can not preview file Flash(*.swf)');
	      }else{
	      	document.images[img_name].src=convert_path_img(m_root.value);
	      }
	    }else{
	    	window.alert('You can not upload file with this type');
	    }
	  }else{
	  	//document.images[img_name].src=convert_path_img(m_root.value);
	  }
	}//end if
}//-----------------------------------------------------------------------------
function check_upload_type(m_path){
	var tmp=new String(),m_type;
	tmp=m_path;
	tmp=tmp.toUpperCase();
	for(var i=0;i<LIST_TYPE_IMG_UPLOAD.length;i++){
	  m_type=tmp.indexOf(LIST_TYPE_IMG_UPLOAD[i],0);
	  if(m_type!==-1) return LIST_TYPE_IMG_UPLOAD[i];
	}
	return false;
}//-----------------------------------------------------------------------------
/******For move select box option**********************************************/
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
}
// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
	}

// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			if (i != 0 && !obj.options[i-1].selected) {
				swapOptions(obj,i,i-1);
				obj.options[i-1].selected = true;
			}
		}
	}
}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown(obj) {
	if (!hasOptions(obj)) { return; }
	for (i=obj.options.length-1; i>=0; i--) {
		if (obj.options[i].selected) {
			if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
				swapOptions(obj,i,i+1);
				obj.options[i+1].selected = true;
			}
		}
	}
}
/******End For move select box option******************************************/
//========Add new Here==========================================================
function ObjectData(){
  this.add=function(mKey,mValue){
    this[mKey]=mValue;
  }
  this.remove=function(mKey){
    delete this[mKey];
  }
  this.get=function(mKey){
    return this[mKey];
  }
  this.update=function(mid_tab,m_name){
    window.cstruct.refresh_selectbox(mid_tab,m_name);
  }
  this.update_list_saved=function(){
    window.cnews.update_list('my_art','&ms=in');
  }
}//====Create Object For Hold Data: Interactive by Ajax=========================
function get_object(obj_name){
  var tmp_obj=new Object();
  tmp_obj=document.getElementById(obj_name);
  if(tmp_obj){//IE
    return tmp_obj;
  }else{
    tmp_obj=obj_name;
  }
  return tmp_obj;
}//====Input:Object name | Output:Object========================================
function get_object_name(obj_name){
  var tmp_obj=new Object();
  tmp_obj=document.getElementsByName(obj_name);
  if(tmp_obj){//IE
    return tmp_obj;
  }else{
    tmp_obj=obj_name;
  }
  return tmp_obj;
}//====Input:Object name | Output:Object========================================
function select_button(obj,m_msg){
  obj.className='defaultbuttonover';
  my_box.txt_msg.value=m_msg;
}
function out_button(obj){
  obj.className='defaultbutton';
  my_box.txt_msg.value='';
}//======================For button of struct save==============================
function convert_string(m_string){
  var tmp=new String();tmp=m_string;
  var pos=tmp.indexOf('::',0);
  if(pos==-1){//Not found
    pos=tmp.indexOf('+',0);
    return tmp.substring(pos+2,tmp.length);
  }
  return tmp.substring(pos+3,tmp.length);
}//====Convert String in selectbox==============================================
function empty_function(txt){
  alert(txt);
}//Does not anything

function change_row(m_id,m_color){
	m_obj=document.getElementById(m_id);
	if(m_obj){
		m_obj.style.backgroundColor=m_color;
	}
}//--change background of first column------------------------------------------
function change_row2(m_obj,m_color){
  m_obj.style.backgroundColor=m_color;
}//--change background of row---------------------------------------------------

function ChangePoll_Vote(mID){
 var pid=document.getElementById("id_sel_answer");
   pid.value=mID;
}//--for poll & vote--
function OpenPoll_Vote(mIDQuestion,mIDAnswer,m_page){
 if(mIDAnswer==""){
   alert('Khong co cau tra loi');
 }else{
   var mURL="&my_data="+mIDQuestion+"[-PLY-]"+mIDAnswer;
   var myPoll=window.open(m_page+"?n=dlg_poll&special=1"+mURL,'Poll_Vote','width=550,height=300,resizable=1');
       myPoll.focus();
 }
}//--open dialog result poll--

function view_thuvien_anh(m_path_img,m_properties){	var myPic;
	if(m_properties){
		myPic=window.open(m_path_img,'VIEW_PICTURE',m_properties);
	}else{		myPic=window.open(m_path_img,'VIEW_PICTURE',"width=300,height=300,resizable=1");	}
	myPic.focus();}

function openNewImage(m_src){	window.open(m_src,'OPEN_IMG');}

function user_insert_images(m_id_txt,m_id_img,m_properties){	var m_pro="width=550,height=500,resizable=1";	if(m_properties){		m_pro=m_properties;	}
	var usr_img=window.open("post_images/select_image.php?my_data="+m_id_txt+'&char='+m_id_img,"POST_IMAGES",m_pro);
			usr_img.focus();}//--end function--

function no_use_images(m_id){	document.getElementById('id_'+m_id).value='';
	document.getElementById('idimg_'+m_id).src='images/logo.gif';}

function open_winmusic(mURL){	var m_pro="width=320,height=520,resizable=1";	var winMusic=window.open(mURL,"HearMusic",m_pro);
			winMusic.focus();}//--end function--

function open_winmusic_tv(mURL,mLyric){
	var m_pro="width=320,height=320,resizable=1";
	window.open(mURL,"HearMusic",m_pro);
			/*winMusic.focus();*/
	var winLyric=window.open(mLyric,"ShowLyric");
			winLyric.focus();

}//--end function--