// JavaScript Document

//window.onload = init;
var formIndex = 1;
function init() {
	var dObj = document.forms;
	var num = dObj.length;
	var isField = false;
	var fr = 1;
	for(var i=0; i < num; i++) {
		var fObj = dObj[i];
		if (fObj.tagName == 'FORM' && fr == formIndex) {
			for (var j=0; j < fObj.length; j++) {
				if (fObj[j].type == 'text' || fObj[j].type == 'textarea' || fObj[j].type == 'password') {
					fObj[j].focus();
					isField = true;
					break;
				}
			}
		}
		if (isField) {
			break;
		}
		fr++;
	}
}

function go(url) {

	var theFrm = document.form;
	for (var i = 0; i < theFrm.length; i++){
		var e = theFrm.elements[i];

		if (e.type.toLowerCase() == "button" || e.type.toLowerCase() == "reset" || e.type.toLowerCase() == "submit") {
			e.disabled = true;
		}
	}

	document.form.action = url;
	if(arguments[1] != "" && arguments[1] != null && document.form.cat != null && document.form.cat != 'undefined'){
		document.form.cat.value = arguments[1];
	}
	if(arguments[2] != "" && arguments[2] != null && document.form.mode != null && document.form.mode != 'undefined'){
		document.form.mode.value = arguments[2];
	}
	document.form.submit();
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;
	if(!d) d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document;
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) x=d.all[n];
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n);
	return x;
}

function htmlspecialchars (obj_str) {
	var ENT_NOQUOTES = arguments[1];
	var ENT_QUOTES = arguments[2];

	var str = String(obj_str);
	str = str.replace(/&/gi, '&amp;');
	//	if (!ENT_NOQUOTES) {
	str = str.replace(/"/gi, '&quot;');
//	}

//	if (ENT_QUOTES) {
		str = str.replace(/\'/gi, '&#039;');
//	}
	str = str.replace(/</gi, '&lt;');
	str = str.replace(/>/gi, '&gt;');
	return str;
}

function htmlentities (obj_str) {
	var str = String(obj_str);
	str = str.replace(/&amp;/gi, '&');

	str = str.replace(/&quot;/gi, '"');

	str = str.replace(/&#039;/gi, '\'');

	str = str.replace(/&lt;/gi, '<');
	str = str.replace(/&gt;/gi, '>');
	return str;
}

function openSearch(url) {
	popWin(url, '690', '600', '', 1, '');
}

function popWin(url, width, height, windowName, scrollbar, content){
	var x = (screen.width) ? (screen.width-width) / 2 : 100;
	var y = (screen.height) ? (screen.height-height) / 2 : 100;

	var features = "width=" + width + "px, height=" + height + "px, top=" + y + "px, left=" + x + "px";
	features += (scrollbar) ? ", scrollbars=yes" : "";
	features += ", resizable=yes";
	if (!windowName){
		windowName = "untitled";
	}
	var popWindow = window.open(url, windowName, features);

	if (popWin.arguments.length == 6 && content != ""){
		popWindow.document.write(content);
	}
	if (document.layers){
		window.moveTo(x, y);
	}
	popWindow.focus();
}

function returnName(userId, fullName) {
	//ope = GlobalWindowImpl::GetOpener();
	window.opener.document.frmReport.full_name.value = fullName;
	//window.opener.document.getElementById('full_name').value = fullName;
	window.opener.document.frmReport.user_id.value = userId;

	window.close();
	return false;
}

//Tools
function popUpContent(url) {
	popUpWin = window.open(url,'','height=400,width=750, location=no, scrollbars=yes, menubars=no,toolbars=no,resizable=yes');
	if (!popUpWin.opener)
	popUpWin.opener = self;
}

function popUpChungkhoan(url) {
	popUpWin = window.open(url,'','height=800,width=900, location=no, scrollbars=yes, menubars=no,toolbars=no,resizable=yes');
	if (!popUpWin.opener)
	popUpWin.opener = self;
}

function popUpFilms(url) {
	popUpWin = window.open(url,'','height=650,width=900, location=no, scrollbars=yes, menubars=no,toolbars=no,resizable=yes');
	if (!popUpWin.opener)
	popUpWin.opener = self;
}

function popUpTV(url) {
	popUpWin = window.open(url,'','height=600,width=550, location=no, scrollbars=no, menubars=no,toolbars=no,resizable=yes');
	if (!popUpWin.opener)
	popUpWin.opener = self;
}

function popUpFootball(url) {
	popUpWin = window.open(url,'','height=600, width=650, location=no, scrollbars=yes, menubars=no, toolbars=no, resizable=yes');
	if (!popUpWin.opener)
	popUpWin.opener = self;
}

function popUpWeb(url) {
	popUpWin = window.open(url,'','height=600, width=800, location=no, scrollbars=yes, menubars=no, toolbars=no, resizable=yes');
	if (!popUpWin.opener)
	popUpWin.opener = self;
}

function popUpFunny(url) {
	popUpWin = window.open(url,'','height=600, width=800, location=no, scrollbars=yes, menubars=no, toolbars=no, resizable=yes');
	if (!popUpWin.opener)
	popUpWin.opener = self;
}

function popUpKqxs(url) {
	popUpWin = window.open(url,'','height=340,width=450, location=no, scrollbars=no, menubars=no,toolbars=no,resizable=yes');
	if (!popUpWin.opener)
	popUpWin.opener = self;
}

function popUpWeather(url) {
	popUpWin = window.open(url,'','height=450,width=500, location=no, scrollbars=yes, menubars=no,toolbars=no,resizable=yes');
	if (!popUpWin.opener)
	popUpWin.opener = self;
}

function popUpContent1(url) {
	popUpWin = window.open(url,'','height=300,width=500, location=no, scrollbars=yes, menubars=no,toolbars=no,resizable=no');
	if (!popUpWin.opener)
	popUpWin.opener = self;

}

function CheckDel()
{if (confirm('Bạn chắc chắn muốn xoá không?')){return true;}else{return false;}}
//Upload Image
function OpenUploadFile(ctl,folder,prefix,width,height)
{
	var strUrl="uploadfile.php?ctl="+ctl+"&folder="+folder+"&pre="+prefix+"&width="+width+"&height="+height;
	window.status="Open";
	if(width<300) width=350;
	if(height<300) height=250;
	width+=50;height+=50;
	window.open(strUrl,"View","width="+width+",height="+height+",toolbar=no,scrollbars=yes");
}

function CheckAction()
{if (confirm('Bạn chắc chắn muốn kích hoạt không?')){return true;}else{return false;}}

function CheckUnAction()
{if (confirm('Bạn chắc chắn muốn bỏ kích hoạt không?')){return true;}else{return false;}}

function makeObject(){
	try {
		req = new XMLHttpRequest(); /* e.g. Firefox */
	} catch(err1) {
		try {
			req = new ActiveXObject('Msxml2.XMLHTTP'); /* some versions IE */
		} catch (err2) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP"); /* some versions IE */
			} catch (err3) {
				req = false;
			}
		}
	}
	return req;
}
function LTrim(value) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim(value) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim(value) {
	return LTrim(RTrim(value));
}
function requestGET(url, query, req) {
	myRand=parseInt(Math.random()*99999999);
	req.open("GET",url+'?rand='+myRand+'&'+query,true);
	req.send(null);
}

function requestPOST(url, query, req) {
	req.open("POST", url,true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//req.setRequestHeader("Content-length", query.length);
	//req.setRequestHeader("Connection", "close");
	req.send(query);
}

function doCallback(callback,item) {
	//eval(callback + '(item)');
	el=document.getElementById(callback);
	el.innerHTML=(item);
}

function LoadAjaxPage(url,query,callback)
{
	doAjaxPage(url,query,callback,'get','0');
}

function PostAjaxPage(url,query,callback)
{
	doAjaxPage(url,query,callback,'post','0');
}

function doAjaxPage(url,query,callback,reqtype,getxml) {
	// create the XMLHTTPRequest object instance
	var myreq = makeObject();
	myreq.onreadystatechange = function() {
		el=document.getElementById(callback);
		el.innerHTML='<img src="images/loading.gif" width=16 border=0>';
		if(myreq.readyState == 4) {
			if(myreq.status == 200) {
				var item = myreq.responseText;
				if(getxml==1) {
					item = myreq.responseXML;
				}
				doCallback(callback, item);
			}
		}
	}
	if(reqtype=='post') {requestPOST(url,query,myreq);} else {requestGET(url,query,myreq);
	}
}

function ajaxSelect2Select(selectbox1,selectbox2,url,parm,valueCheck,selDefText,selDefValue)
{

	var selbox1=document.getElementById(selectbox1);
	var sel=selbox1.options[selbox1.selectedIndex].value;
	if(sel!=valueCheck)
	{
		var x = makeObject();
		url=url+'&'+parm+'='+sel;
		removeAllOptions(selectbox2);
		addOption(selectbox2,'','Đang tải...');
		var selbox2=document.getElementById(selectbox2);
		selbox2.disabled=true;
		if(x){x.onreadystatechange=function() {
			if(x.readyState==4&&x.status==200){
				var listsel=x.responseText;
				var arr = new Array();
				arr = listsel.split(",");
				removeAllOptions(selectbox2);
				addOption(selectbox2,selDefValue,selDefText);
				for (var i=0; i < arr.length-1;i=i+2)
				{
					addOption(selectbox2,arr[i],arr[i+1]);
				}
				selbox2.disabled=false;
			}
		}
		}

		req.open("GET",url,true);
		req.send(null);
	}
}

function addOption(selectbox,value,text)
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	var selbox=document.getElementById(selectbox);
	selbox.options.add(optn);
}

function removeAllOptions(selectbox)
{
	var selbox=document.getElementById(selectbox);
	for(i=selbox.options.length-1;i>=0;i--){selbox.remove(i);}
}

function ViewForm(_formFrom,prefix)
{
	//alert(prefix);
	var els = document.getElementById(_formFrom).elements;
	var text='';
	for(i = 0; i < els.length; i++)
	{
		ctlto=prefix+els[i].name;
		if(els[i].type== "text")
		{
			document.getElementById(ctlto).innerHTML=els[i].value;
		}
		if(els[i].type== "textarea")
		{
			if ( typeof( FCKeditorAPI ) != 'undefined' )
			{
				var oEditor = FCKeditorAPI.GetInstance(els[i].name) ;
				text=oEditor.GetXHTML();
			}
			else text=els[i].value;
			document.getElementById(ctlto).innerHTML=text;
		}
		//alert(els[i].type+"|"+els[i].value);
		if(els[i].type=="select-one")
		{
			text='';
			if(els[i].value!="") text=els[i].options[els[i].selectedIndex].text
			document.getElementById(ctlto).innerHTML=text;
		}
		if(els[i].type=="checkbox")
		{
			temp='';
			if(els[i].checked)temp='<img src="images/home/actived.gif">'; else temp='<img src="images/home/inactived.gif">';
			document.getElementById(ctlto).innerHTML=temp;
		}
		if(els[i].type=="hidden")
		{
			if(els[i].name.indexOf('image')>-1)
			{
				document.getElementById(ctlto).innerHTML=getImage(els[i].name);
			}else
			{
				if(document.getElementById(ctlto))document.getElementById(ctlto).innerHTML=els[i].value;
			}
		}
	}
}

function getImage(ctl){

	imgSrc=document.getElementById(ctl).value;
	var width=120;
	var height=90;
	var newImg = new Image();
	newImg.src = imgSrc;
	var fHeight = newImg.height;
	var fWidth = newImg.width;
	if(width != "" && height != "") {
		rWidth = fWidth / width;
		rHeight = fHeight / height;
		rWidth > rHeight ? ratioImg = rWidth : ratioImg = rHeight;
		if(ratioImg > 1){
			fWidth = fWidth / ratioImg;
			fHeight = fHeight / ratioImg;
		}
	}else {
		if(width == "")
		if(fHeight > height) {
			fHeight = height;
			fWidth = fWidth * fHeight / fHeight;
		}
		if(height == "")
		if(fWidth > width) {
			fWidth = width;
			fHeight = fHeight * fWidth / fWidth;
		}
	}
	
	img='<img src="'+imgSrc+'" width='+fWidth+' height='+fHeight+'/>';

	return img;
	
}

function ChangeClass(obj,className) {
	obj.className=className;
}

function CheckField(_form, listfiled)
{
	var els = document.getElementById(_form).elements;
	var rt = 0;
	arrTmp=listfiled.split(',');
	for(i=0;i<arrTmp.length;i++)
	{
		ctl=document.getElementById(arrTmp[i]);
		if(ctl.type=="select-one") {style="select";styleErr="select_error";}
		if((ctl.type=="text")||(ctl.type=="textarea")) {style="text";styleErr="textbox_error";}
		if(ctl.value=="")
		{
			ctl.className = styleErr;
			rt++;
		}else ctl.className = style;
	}
	if(rt > 0)
	{
		alert("Bạn phải nhập đầy đủ thông tin !");
		return false;
	}
	
	return true;
}

function ChangeCostType(IdSelect1,IdSelect2,IdCost,IdDate,IdTotal)
{
	var value=document.getElementById(IdSelect1).value;
	var typesellid=document.getElementById('typesell_id').value;
    if(document.getElementById) var x=makeObject();
    if(x){x.onreadystatechange=function() {
        if(x.readyState==4&&x.status==200){
            	var text=trim(x.responseText);
				if(text!="")
				{
					var arr = new Array();
					arr = text.split(",");
					document.getElementById('show_'+IdCost).innerHTML=arr[1];
					document.getElementById(IdCost).value=arr[1];
					document.getElementById('price_id').value=arr[0];
				}
				else
				{
					document.getElementById('show_'+IdCost).innerHTML=0;
					document.getElementById(IdCost).value=0;
					document.getElementById('price_id').value=0;
				}
				ChangePostDate(IdSelect1,IdSelect2,IdCost,IdDate,IdTotal);
            }
        }
    x.open("GET",'?module=ajax&cat=price&sub=costype&costtypeid='+value+'&typesellid='+typesellid,true);x.send(null);
    }
}

function ChangePostDate(IdSelect1,IdSelect2,IdCost,IdDate,IdTotal)
{
	var value=document.getElementById(IdSelect2).value;
	var priceid=document.getElementById('price_id').value;
	//alert(value+'|'+priceid);
    if(document.getElementById) var x=makeObject();
    if(x){x.onreadystatechange=function() {
        if(x.readyState==4&&x.status==200){
            	var text=trim(x.responseText);
				//alert(text);
				if(text!="")
				{
					document.getElementById('show_'+IdDate).innerHTML=text;
					document.getElementById(IdDate).value=text;
				}
				else
				{
					document.getElementById('show_'+IdDate).innerHTML=0;
					document.getElementById(IdDate).value=0;
				}
				SumCost(IdCost,IdDate,IdTotal);
            }
        }
    x.open("GET",'?module=ajax&cat=price&sub=postdate&postdateid='+value+'&priceid='+priceid,true);
	x.send(null);
    }
}

function SumCost(IdCost,IdDate,IdTotal)
{
	var vCost=document.getElementById(IdCost).value;
	var vDate=document.getElementById(IdDate).value;
	document.getElementById('show_'+IdTotal).innerHTML=parseInt(vCost)+parseInt(vDate);
	document.getElementById(IdTotal).value=parseInt(vCost)+parseInt(vDate);
}