	// JavaScript Document
/* xml request
   code by minhlt
   last update: 10 Sep 2006
*/

//************* label & message*******************
var msgExistEmail = 'Email đã tồn tại';
var msgExistUsername = 'Username đã tồn tại';

//************* label & message*******************
var request;
var queryString;
var strResponse;
function httpRequestPost(frm,url, isAsynch, contentType){
    setQueryString(frm);
    httpRequest("POST",url,true, contentType);
}

function setQueryString(frm){
    queryString="";
    var numberElements =  frm.elements.length;
    for(var i = 0; i < numberElements; i++) {
		if((frm.elements[i].name == "chkItem[]") && !frm.elements[i].checked) {
			continue;	
		}
    	queryString += frm.elements[i].name+"="+ encodeURIComponent(frm.elements[i].value)+"&";
    }
	queryString = queryString.substr(0, queryString.length-1);
}

 function httpRequest(requestType, url, isAsynch, contentType) {
	 //return true;
 	if(window.XMLHttpRequest) {
		request = new XMLHttpRequest();		
	} else if(window.ActiveXObject){
		request = new ActiveXObject("Msxml2.XMLHTTP");
		//alert(request);
		if(!request) {
			request = new ActiveXObject("Microsoft.XMLHTTP");
			//alert(request);
		}
	}
	if (request.overrideMimeType) {
       //request.overrideMimeType('text/xml');   
    }
	if(request) {
		initRequest(requestType, url, isAsynch, contentType);
		return false;
	} else {
		return true;
	}
 }
var p_ = "modules/map/e_map/";
var ext = '.jpg';
 function initRequest(requestType, url, isAsynch, contentType){
 	request.open(requestType, url, isAsynch);
	request.onreadystatechange = function() {
 	if(request.readyState == 4) {
		if(request.status == 200) {
/*			if (contentType == 'xml') {	//xml request
				var xmlDoc = request.responseXML;
				//alert(request.getAllResponseHeaders());
				var block_content = xmlDoc.getElementsByTagName("BlockContainer").item(0);
				var blockId;
				for (i =0, n=block_content.childNodes.length; i < n; i++) {
					if (block_content.childNodes[i].tagName == "block") {
						var blockId = block_content.childNodes[i].getAttribute("id");
						if (block_content.childNodes[i].textContent) {
							if (blockId == 'document_title') {
								document.title = block_content.childNodes[i].firstChild.nodeValue;	
							} else {
								showResult(blockId,block_content.childNodes[i].textContent);
							}
						} else {
							if (blockId != 'document_title') {
								showResult(blockId,block_content.childNodes[i].firstChild.nodeValue);
							} else {
								document.title = block_content.childNodes[i].firstChild.nodeValue;	
							}
						}
					}		
				}
				document.getElementById("loading").style.display="none";
			} else { //plain text request
				
*/				
				//alert(request.responseText);
				if (contentType.length != 0) {
					document.getElementById(contentType).innerHTML = request.responseText;
				} else {
					strResponse = request.responseText;
				}
				
				document.getElementById("loading").style.display="none";
				//dhtmlHistory.add(url,url);
			//}
		} else {
			alert("loi");
		}	
	} else {
		//document.getElementById("loading").style.top = 10 + document.documentElement.scrollTop + 'px';
		document.getElementById("loading").style.display="";	
	}
 
 };
	if (requestType == "GET") {
		request.send(null);
	} else {
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    	request.send(queryString);
	}
 }
 
 
function showResult(blockId, content) {
	if (document.getElementById(blockId)) {
		document.getElementById(blockId).innerHTML=content;	
	}
}


function changeLang(lang_id, returnURL) {
	var indexOfStrLang = -1;
	indexOfStrLang = returnURL.indexOf('&clid',0);
	if (indexOfStrLang == -1) {
			location.href = '?clid=' + lang_id;	
	} else {
		returnURL = returnURL.substr(0,returnURL.indexOf('&clid'));
		location.href = '?' + returnURL + '&clid=' + lang_id;	
	}
}

function showhide(blockID){
	var Elem = document.getElementById(blockID);		
	var Img = document.getElementById('image' + blockID);
	 if(Elem.style.display != '') { 
	 	Elem.style.display = '';
	 	Img.src = "images/minus.gif";
	 } else {
	 	Elem.style.display = 'none';
	 	Img.src = "images/plus.gif";
	 }
}

function checkAll(currentChk) {
	var aryItems = new Array();
	aryItems = document.getElementsByName("chkItem[]");
	if (currentChk.checked) {
		for(i = 0, n = aryItems.length; i < n; i++) {
			aryItems[i].checked = true;
		}
	} else {
		for(i = 0, n = aryItems.length; i < n; i++) {
			aryItems[i].checked = false;
		}
	}
}

function showHideEditBlock(block_name, block_id) {
	//alert(block_name);
	if (document.getElementById("edit_block_title").style.display == "none") {
		/*var postX = e.clientX;
		var postY = e.clientY;
		document.getElementById("edit_block_title").style.top = postY + 'px';
		document.getElementById("edit_block_title").style.left = postX + 'px';
		*/
		var url = 'modules/blocksman/frm_edit_title.php?block_name=' + block_name + '&block_id=' + block_id;
		document.getElementById("edit_block_title").style.display = "";
		httpRequest('GET',url,true,'edit_block_title');	
	} else {
		document.getElementById("edit_block_title").style.display = "none";		
	}
	return false;
}

function onChangeCatAdmin(thisSelect) {
		var cat_id = thisSelect.options[thisSelect.selectedIndex].value;
		var url = 'modules/news/listnews.php?aj=true&cid=' + cat_id;
		httpRequest('GET',url,true,'main_content');
}
function onChangeCatAdminProducts(thisSelect) {
		var cat_id = thisSelect.options[thisSelect.selectedIndex].value;
		var url = 'modules/products/list.php?aj=true&cid=' + cat_id;
		httpRequest('GET',url,true,'main_content');
}
function onChangeCatAdminJobs(thisSelect) {
		var cat_id = thisSelect.options[thisSelect.selectedIndex].value;
		//alert(cat_id);
		var url = 'modules/jobs/listjobs.php?aj=true&cid=' + cat_id;
		httpRequest('GET',url,true,'main_content');
}
function onChangeCatSearchJobs(thisSelect) {
		var cat_id = thisSelect.options[thisSelect.selectedIndex].value;
		//alert(cat_id);
		var url = 'modules/jobs/search_jobs.php?aj=true&cid=' + cat_id;
		httpRequest('GET',url,true,'main_content');
}
function reOrderCat(thisFrm, moduleName) {
		//alert(thisFrm.name);
		var url = 'modules/categories/listcat.php?aj=true&param=reorder&mname=' + moduleName;
		httpRequestPost(thisFrm,url,true,'main_content');
}

function doActionCat(thisFrm, moduleName, catType) {
		//alert(getChecked());
		var url = 'modules/categories/listcat.php?aj=true&param=s_action&mname='+moduleName+'&cat_type='+catType;
		//alert(url);
		httpRequestPost(thisFrm,url,true,'main_content');
}
function doActionNews(frm, qstring, mySelect) {
		if (mySelect.options[mySelect.selectedIndex].value != 'moveto_cat') {
			document.listnews.action = '?module=news&action=listnews&param=s_action' + qstring;
			//alert(document.listnews.action);
			document.listnews.submit();
		} else {
			document.getElementById("moveto_cat").style.display = "";
			document.frmMoveTo.str_news_id.value = getChecked();
		}
}

function doActionJobs(frm, qstring, mySelect) {
	if (mySelect.options[mySelect.selectedIndex].value != 'moveto_cat') {
			document.listnews.action = '?module=job&action=listjobs&param=s_action' + qstring;
			//alert(document.listnews.action);
			document.listjobs.submit();
		} else {
			document.getElementById("moveto_cat").style.display = "";
			document.frmMoveTo.str_jobs_id.value = getChecked();
		}
}
function doActionBanner(thisFrm) {
		var url = 'modules/banner/listbanners.php?aj=true&param=s_action';
		httpRequestPost(thisFrm,url,true,'main_content');
}

function getChecked() {
	var aryChecked = new Array();
	var str_checked = '';
	aryChecked = document.getElementsByName("chkItem[]");
	for (i = 0, n= aryChecked.length;i < n; i++) {
		if (aryChecked[i].checked) {
				str_checked = str_checked + aryChecked[i].value + "','";
		}
	}
	return str_checked.substr(0,str_checked.length-3);
}
function doActionHomepage() {
		document.homepage.action = '?module=homepage&action=listnewshp&param=s_action';
		document.homepage.submit();
}
function doActionModules() {
	document.modules.action = '?module=modulesman&action=listmodules&param=s_action';
	document.modules.submit();
}
function doActionBlocks(thisFrm) {
	var url = 'modules/blocksman/listblocks.php?aj=true&param=s_action';
	httpRequestPost(thisFrm,url,true,'main_content');
}
function doActionPoll(thisFrm) {
	var url = 'modules/poll/listpoll.php?aj=true&param=s_action';
	httpRequestPost(thisFrm,url,true,'main_content');
}
	function reOrderBlock(thisFrm) {
		var url = 'modules/blocksman/listblocks.php?aj=true&param=reorder';
		httpRequestPost(thisFrm,url,true,'main_content');
	}
function reOrderBanner(thisFrm) {
	var url = 'modules/banner/listbanners.php?aj=true&param=reorder';
	httpRequestPost(thisFrm,url,true,'main_content');
}

function submitEditBlock() {
	//document.block.action = '?module=blocksman&action=listblocks&param=eblock';
	document.blocks.submit();
}
function submitMoveToCat() {
	document.frmMoveTo.action = '?module=news&action=listnews&param=moveto';
	document.frmMoveTo.submit();
}
function hideMoveToBlock() {
	document.getElementById("moveto_cat").style.display = "none";		
}

function changeCatTypeList(mySelect) {
		optionValue = mySelect.options[mySelect.selectedIndex].value;
		url = "modules/categories/listcat.php?aj=true&mname=menu&cat_type=" + optionValue;
		//alert(url);
		httpRequest('GET',url,true,'main_content');
	
}

function changeCatTypeAddCat(myRadio) {
		radioValue = myRadio.value;
		url = "modules/categories/addcat.php?aj=true&cat_type=" + radioValue;
		//alert(url);
		httpRequest('GET',url,true,'main_content');
	
}

function changeCatTypeAdd(myRadio) {
		var radioValue = myRadio.value;
		var module_name = document.categories.module_name.value;
		url = "modules/categories/show_category_tree.php?aj=true&cat_type=" + radioValue + "&module_name=" + module_name + '&cb_name=parent_id';
		//alert(url);
		httpRequest('GET',url,true,'change_cat_type');
	
}
function changeCatTypeAddNews(myRadio) {
		var radioValue = myRadio.value;
		var module_name = 'news';
		url = "modules/categories/show_category_tree.php?aj=true&cat_type=" + radioValue + "&module_name=" + module_name;
		//alert(url);
		httpRequest('GET',url,true,'change_cat_type');
	
}
function changeLinkType(myRadio) {
		var radioValue = myRadio.value;
		/* Reset */
		$('#selected_link_id').attr('value','');
		$('#selected_name').html('');
		/********/
		if (radioValue == 'news') {
			url = "modules/categories/show_list_news.php?aj=true&cat_type=" + radioValue;
			httpRequest('GET',url,true,'link_to');
		} else if (radioValue == 'product') {
			$('#menu_link_to').show();
			url = "modules/categories/show_list_products.php?aj=true&cat_type=" + radioValue;
			httpRequest('GET',url,true,'link_to');
		} else if (radioValue == 'news_category') {
			url = "modules/categories/show_category_tree.php?aj=true&cat_type=0&module_name=news&cb_name=cb_link_id";
			httpRequest('GET',url,true,'link_to');
		} else if (radioValue == 'products_category') {
			url = "modules/categories/show_category_tree.php?aj=true&cat_type=0&module_name=products&cb_name=cb_link_id";
			httpRequest('GET',url,true,'link_to');
		} else if (radioValue == 'url') {
			$('#menu_link_to').show();
			$('#link_to').html('<input type="text" name="url" value="" size="40" class="textbox" />');
		}
		
	
}

function onClickLink (a_obj) {
	var selected_news = document.getElementById('selected_news');
	//selected_news.innerHTML = a_obj.innerHTML;
	$('#selected_name').html(a_obj.innerHTML);
	var str_link_id = a_obj.id;
	var link_id = str_link_id.substr(7);
	$('#selected_link_id').attr('value',link_id);
	//alert('Hi');
	return false;
}

function showHidePanel(panel_id) {
	var panel = document.getElementById(panel_id);
	if (panel.style.display == '') {
		panel.style.display = 'none';
	} else {
		panel.style.display = '';
	}
}


			
function Scroll() {	
	setInterval("doScroll()",1);
}
	
function doScroll() {
	var divTop;
	if (document.documentElement && document.documentElement.scrollTop) {
		divTop = parseInt(document.documentElement.scrollTop);
	} else if (document.body) {
		divTop = parseInt(document.body.scrollTop);
	}
	var scrollDivLeft = document.getElementById("scrollDivLeft");
	var scrollDivRight = document.getElementById("scrollDivRight");
	var leftTop = parseInt(scrollDivLeft.style.top) + (divTop - parseInt(scrollDivLeft.style.top))/16;
	scrollDivLeft.style.top = leftTop + "px";
	var rightTop = parseInt(scrollDivRight.style.top) + (divTop - parseInt(scrollDivRight.style.top))/16;
	scrollDivRight.style.top = rightTop + "px";
}
		
function confirmActionGet(confirm_url) {
	if(confirm("Bạn có muốn thực hiện thao tác này không?")) {
		location.href=confirm_url;
		return false;
	} else {
		return false;
	}
}

function changeCVType(thisSelect) {
	var cv_type = thisSelect.options[thisSelect.selectedIndex].value;
	var url = '?module=jobs&action=search_cv&cv_type=' + cv_type;
	location.href=url;
}
function changeCVTypeSearch(thisSelect) {
	var cv_type = thisSelect.options[thisSelect.selectedIndex].value;
	var url = '?module=jobs&action=cv_filter&cv_type=' + cv_type;
	location.href=url;
}

function changeCvTypeCreate(cv_type) {
	//radio
	var cv_action = 'create_cv';
	if (cv_type == 'enrol') {
		cv_action = 'create_enrol_cv';
	} else if (cv_type == 'outbound') {
		cv_action = 'create_outbound_cv';
	}
	url = '?module=jobs&action=' + cv_action;
	location.href=url;
}
function changeCvTypeAdmin(cv_type) {
	//radio
	url = '?module=jobs&action=cv_manager&cv_type='+cv_type;
	location.href=url;
}

function addBannerValidate() {
	var uploadFile = document.getElementById("banner_upload").value;
	if (uploadFile.length <5) {
		alert('File anh khong hop le !');
		return false;
	} else {
		var ext = uploadFile.match(/(.{3}$)/)[1];
		if (ext.toLowerCase() != 'jpg' && ext.toLowerCase() != 'gif' && ext.toLowerCase() != 'png') {
			alert('File anh khong hop le !');
			return false;
		}
	}	
}

function replaceString(str, needle, replace_by) {
	if (str.length > 0) {
		while (str.search(needle)!=-1) {
			str = str.replace(needle, replace_by);
		}
	}
	return str;
}

/////////////////////////////////
	
	function fastURL(url, isAsync,block_to_update) {
		//alert('Hi');
		//alert(url);
		/*url = url.substring(url.indexOf('module'));
		var pattern = /module=(\w+)&action=(\w+)[&\s]/;
		var fastURL = url.replace(pattern,"modules/$1/$2.php?");
		*/
		//if (fastURL.indexOf('aj=false') !=-1) {
			//fastURL =fastURL.replace('aj=false',"aj=true");
		//} else {
			//fastURL += '&aj=true';
		//}
		if (url.indexOf('.emice') != -1) {
			var url = url.replace('.emice', '.ajax');
		} else {
			url += '&aj=true';
		}	
		//alert(url);
		httpRequest('GET', url, true, block_to_update);
		return false;
	}
	
	
	function adminFastURL(url, isAsync,block_to_update) {
		url = url.substring(url.indexOf('module'));
		var pattern = /module=(\w+)&action=(\w+)[&\s]/;
		var fastURL = url.replace(pattern,"modules/$1/$2.php?");
		if (fastURL.indexOf('aj=false') !=-1) {
			fastURL =fastURL.replace('aj=false',"aj=true");
		} else {
			fastURL += '&aj=true';
		}
		var response = $.ajax({
		  type: "GET",
		  async: false,
		  url: fastURL,
		  dataType: "html"
		  //data: queryString
		}).responseText;
		var updateBlock = document.getElementById(block_to_update);
		updateBlock.innerHTML = response;
		//httpRequest('GET', fastURL, true, block_to_update);
		return false;
	}