﻿function ChangeImg(sImg){document.images["ShowImg"].src=sImg;}
function isEmail(s)
{   
  if (s=="") return false;
  if(s.indexOf(" ")>0) return false;
  if(s.indexOf("@")==-1) return false;
  var i = 1;
  var sLength = s.length;
  if (s.indexOf(".")==-1) return false;
  if (s.indexOf("..")!=-1) return false;
  if (s.indexOf("@")!=s.lastIndexOf("@")) return false;
  if (s.lastIndexOf(".")==s.length-1) return false;
  var str="abcdefghikjlmnopqrstuvwxyz-@._1234567890"; 
  for(var j=0;j<s.length;j++)
	if(str.indexOf(s.charAt(j))==-1)
		return false;
   return true;
}
function GoUrl(url)
{
	window.location.href=url
}

function OpenViewImgs(form_,obj,folder)
{
	var strUrl="viewimgs.php?form="+form_+"&obj="+obj+"&fder="+folder;
	window.status="Open";
	window.open(strUrl,"View","scrollbars=yes,width=650,height=400");
}
function OpenFileManager(ctl)
{
	var folder=document.getElementById(ctl).value;
	var subpar="";
	if(folder!=""&&folder.lastIndexOf("/")!=-1)
	{
		folder=folder.substr(0,folder.lastIndexOf("/"));
		subpar="&dir="+folder;
	}
	var strUrl="filemanager.php?ctl="+ctl+subpar+"&wopen=1";
	window.status="Open";
	window.open(strUrl,"View","width=800,height=600,toolbar=yes,scrollbars=yes");
}

function ViewImage(file)
{
	var strUrl="viewimage.php?f="+file;
	window.status="Open";
	window.open(strUrl,"View","scrollbars=no,width=400,height=400");
}

function SelectChange(obj)
{if(obj.options[obj.selectedIndex].value != 0) window.location.href=obj.options[obj.selectedIndex].value;}
function CheckDel()
{if (confirm('Bạn chắc chắn muốn xoá không?')){return true;}else{return false;}}

function OpenWin(strUrl,Name,Boder){window.open(strUrl,Name,Boder)}
function changeto(obj,strClass){
	if(strClass!=""){
		obj.className = strClass;
	}
	obj.style.cursor = 'hand';
}
function ShowDatHang()
{
	if(showDatHang.className=='show')showDatHang.className='hide'; else showDatHang.className='show';
}
function ShowInfo(obj)
{
	identity=document.getElementById(obj);
	alert("a");
	switch (obj)
	{
		case "frmDatHang":
		{
			if(frmDatHang.className=='show')frmDatHang.className='hide'; else frmDatHang.className='show';
			break;
		}
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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 MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
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 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 wirteText(id,str)
{
	el=document.getElementById(id);
	el.innerHTML=str;
}
function LoadPage(id,url,page){
	if(document.getElementById) var x=makeObject();
    if(x){x.onreadystatechange=function() {
        el=document.getElementById(id); 
        el.innerHTML='<img src="images/loading.gif" width=16> Loading....';
        if(x.readyState==4&&x.status==200){
            el.innerHTML=''; 
            el=document.getElementById(id);
            el.innerHTML=x.responseText; 
            //eval(eval_str);
            }
        }
    x.open("GET",url+"&page="+page,true);x.send(null);
    }
}

function ShowDetail(id,url)
{
    if(document.getElementById) var x=makeObject();
    if(x){x.onreadystatechange=function() {
        el=document.getElementById(id); 
        el.innerHTML='<img src="images/loading.gif" width=16> Loading....';
        if(x.readyState==4&&x.status==200){
            el.innerHTML=''; 
            el=document.getElementById(id);
            el.innerHTML=x.responseText; 
            //eval(eval_str);
            }
        }
    x.open("GET",url,true);x.send(null);
    }
}

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> Loading....';
		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 ajaxLogin(id)
{
	var UserName=document.getElementById("UserName").value;
	var PassWord=document.getElementById("PassWord").value;
	query='UserName='+UserName+'&PassWord='+PassWord;
	reurl=window.location.href;
	var x = makeObject();
	if(x){x.onreadystatechange=function() {
	el=document.getElementById(id); 
	el.innerHTML='<img src="images/loading.gif" width=16> Loading....';
		if(x.readyState==4&&x.status==200){
			//el.innerHTML=''; 
			el=document.getElementById(id);
			value=trim(x.responseText); 
			if(value=='OK'){document.location.href=reurl;}else{el.innerHTML=value;}
			}
		}
	req.open("POST", 'login.php',true);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send(query);
	}
}
function ajaxLogout()
{
	reurl=window.location.href;
	var x = makeObject();
	if(x){x.onreadystatechange=function() {
		if(x.readyState==4&&x.status==200){
			value=trim(x.responseText); 
			window.location.href=reurl;
			}
		}
	req.open("GET", 'login.php?status=logout',true);
	req.send(null);
	}
}

function ajaxChangPass(id)
{
	OldPassword=encodeURI(document.getElementById('OldPassword').value);
	NewPassword=encodeURI(document.getElementById('NewPassword').value);
	query='OldPassword='+OldPassword+'&NewPassword='+NewPassword;
	PostAjaxPage('changepass.php',query,id);
	document.getElementById('NewPassword').value='';
	document.getElementById('OldPassword').value='';
	document.getElementById('ReNewPassword').value='';
}

function getFile(url,query)
{
	var x = makeObject();
	myRand=parseInt(Math.random()*99999999);
	if(x){x.onreadystatechange=function() {
		if(x.readyState==4&&x.status==200){
			window.location.href=trim(x.responseText);
			}
		}
	}
	req.open("GET",url+'?'+query+'&rand='+myRand,true);
	req.send(null);
}
var clicks=0;
function write_captcha(){
   	clicks++;
	document.getElementById('captcha_id').innerHTML = '<img src="createcaptchar.php?clicks=' + clicks + '" border="0" alt="" />';
   return false;
}
function SendContact(id)
{
	FirstName=encodeURI(document.getElementById('FirstName').value);
	LastName=encodeURI(document.getElementById('LastName').value);
	DiaChi=encodeURI(document.getElementById('DiaChi').value);
	Country=encodeURI(document.getElementById('Country').value);
	ZipCode=encodeURI(document.getElementById('ZipCode').value);
	DienThoai=encodeURI(document.getElementById('DienThoai').value);
	DiDong=encodeURI(document.getElementById('DiDong').value);
	Fax=encodeURI(document.getElementById('Fax').value);
	DiaChi=encodeURI(document.getElementById('DiaChi').value);
	Email=encodeURI(document.getElementById('Email').value);
	CongTy=encodeURI(document.getElementById('CongTy').value);
	ThongTin=encodeURI(document.getElementById('ThongTin').value);
	query='FirstName='+FirstName+'&LastName='+LastName+'&DiaChi='+DiaChi+'&Country='+Country+'&ZipCode='+ZipCode+'&DienThoai='+DienThoai+'&DiDong='+DiDong+'&Fax='+Fax+'&DiaChi='+DiaChi+'&Email='+Email+'&ThongTin='+ThongTin+'&CongTy='+CongTy;
	PostAjaxPage('sendcontact.php',query,id);
}

function InmescoSelect(ctl,url,parm,id)
{
	var sel=ctl.options[ctl.selectedIndex].value;
	LoadAjaxPage(url,parm+'='+sel,id);
}
function ajaxSelect2Select(selectbox1,selectbox2,url,parm)
{
	
	var selbox1=document.getElementById(selectbox1);
	var sel=selbox1.options[selbox1.selectedIndex].value;
	var x = makeObject();
	myRand=parseInt(Math.random()*99999999);
	if(x){x.onreadystatechange=function() {
		removeAllOptions(selectbox2);
		addOption(selectbox2,'Đang tải...','');
		var selbox2=document.getElementById(selectbox2);
		selbox2.disabled=true;
		if(x.readyState==4&&x.status==200){
			var listsel=trim(x.responseText);
			var arr = new Array();
			arr = listsel.split(",");
			removeAllOptions(selectbox2);
			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+'?rand='+myRand+'&'+parm+'='+sel,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 ShowOrder(id)
{
	chon=encodeURI(document.getElementById('chon').value);
	query='chon='+chon;
	PostAjaxPage('sendorder.php',query,id);
}
function SendOrder(id)
{
	chon=encodeURI(document.getElementById('chon').value);
	isSend=encodeURI(document.getElementById('isSend').value);
	query='chon='+chon+'&isSend='+isSend;
	PostAjaxPage('sendorder.php',query,id);
}
function ViewByDate()
{
	nDay=document.getElementById('nDay').value;
	nMonth=document.getElementById('nMonth').value;
	nYear=document.getElementById('nYear').value;
	nDate="";
	if(nDay!=""&&nMonth!=""&&nYear!="")
	{
		nDate=nDay+"/"+nMonth+"/"+nYear;
		reurl=window.location.href+"&bydate="+nDate;
		LoadAjaxPage('shownews.php',reurl,'ShowInfo');
	}else
	{
		alert("Please select full filed");
	}
	
}
function CheckBoxPermission(Id,type)
{
	if(type==0)
	{
		if(document.getElementById('Per_'+Id).checked==true)
		{
			document.getElementById('Post_'+Id).disabled="";
			document.getElementById('Edit_'+Id).disabled="";
			document.getElementById('Del_'+Id).disabled="";
		}
		else
		{
			document.getElementById('Post_'+Id).disabled="disabled";
			document.getElementById('Edit_'+Id).disabled="disabled";
			document.getElementById('Del_'+Id).disabled="disabled";
		}
	}
	if(type==1)
	{
		if(document.getElementById('Edit_'+Id).checked==false) document.getElementById('Del_'+Id).checked=false;
	}
	
}
function CheckBoxAll(_form,chkBox,type)
{
	/*
	type=0: Check All
	type=1: Uncheck All
	*/
	var els =document.getElementById(_form).elements; 
	for(i=0; i<els.length; i++)
	{ 
		if(els[i].type=="checkbox"&&els[i].name.substr(0,chkBox.length)==chkBox&&els[i].disabled=="")
		{
			if(type==0) els[i].checked=true;
			else els[i].checked=false;
		}
	}
}
function PermissionCheckAll(_form,chkBox,type)
{
	/*
	type=0: Check All
	type=1: Uncheck All
	*/
	var els =document.getElementById(_form).elements; 
	for(i=0; i<els.length; i++)
	{ 
		if(els[i].type=="checkbox"&&els[i].name.substr(0,chkBox.length)==chkBox&&els[i].disabled=="")
		{
			if(type==0) els[i].checked=true;
			else els[i].checked=false;
			//alert(els[i].name.substr(chkBox.length));
			CheckBoxPermission(els[i].name.substr(chkBox.length),0);
		}
	}
}
function EditCheckAll(_form,chkBox,type)
{
	/*
	type=0: Check All
	type=1: Uncheck All
	*/
	var els =document.getElementById(_form).elements; 
	for(i=0; i<els.length; i++)
	{ 
		if(els[i].type=="checkbox"&&els[i].name.substr(0,chkBox.length)==chkBox&&els[i].disabled=="")
		{
			if(type==0) els[i].checked=true;
			else els[i].checked=false;
			//alert(els[i].name.substr(chkBox.length));
			CheckBoxPermission(els[i].name.substr(chkBox.length),1);
		}
	}
}
function AddCart(id)
{
	document.getElementById('frmShop'+id).submit();
}
function SendFriend(type,id)
{
	if(type==1)	document.getElementById(id).style.display='';
	else		document.getElementById(id).style.display='none';
}

function flashWrite(url,w,h,vars,id,bg){
	 var flashStr=
	"<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0' width='"+w+"' height='"+h+"' id='"+id+"' align='middle'>"+
	"<param name='allowScriptAccess' value='always' />"+
	"<param name='movie' value='"+url+"' />"+
	"<param name='FlashVars' value='"+vars+"' />"+
	"<param name='wmode' value='transparent' />"+
	"<param name='menu' value='false' />"+
	"<param name='quality' value='high' />"+
	"<embed src='"+url+"' FlashVars='"+vars+"' wmode='transparent' menu='false' quality='high' width='"+w+"' height='"+h+"' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />"+
	"</object>";
	document.write(flashStr);
}

function DisplayFloatBanner(side)
{
	if( side == 0 ) // ko in gi ca
		return;
		
	if( FloatBanner.length == 0 )
		return;
		
	document.write('<table cellspacing=0 cellpadding=0 border=0 width=115 bgcolor="#FFFFFF">');
	
	if (side==1) // ben trai
		for (i=0; i<FloatBanner.length; i++)
			if(FloatBanner[i][5]=='4')
				if(FloatBanner[i][0].indexOf(".swf")>0)
				{ 
					document.write('<tr><td>');
					flashWrite(PageHost.concat(FloatBanner[i][0]),FloatBanner[i][3],FloatBanner[i][4],FloatBanner[i][1]);
					document.write('</td></tr>');
				}
				else if(FloatBanner[i][0].indexOf(".htm")>0)
				{
					document.write('<tr><td>');
					document.write('<iframe width="');document.write(FloatBanner[i][3]);document.write('" height="');document.write(FloatBanner[i][4]);document.write('" src="');document.write(FloatBanner[i][0]);document.write('" marginwidth="0" marginheight="0" scrolling="no" frameborder="0"></iframe>');
					document.write('</td></tr>');
				}
				else if(FloatBanner[i][1]=='')
					document.write('<tr><td><img src="', PageHost.concat(FloatBanner[i][0]), '" width=',FloatBanner[i][3],' border=0></td></tr>');
					else document.write('<tr><td><a href="',FloatBanner[i][1],'" target="',FloatBanner[i][2],'"><img src="', PageHost.concat(FloatBanner[i][0]), '" width=',FloatBanner[i][3],' border=0></a></td></tr>');
		
	if (side==2) // ben phai
		for (i=0; i<FloatBanner.length; i++)
			if(FloatBanner[i][5]=='5')
				if(FloatBanner[i][0].indexOf(".swf")>0) 
				{
					document.write('<tr><td>');
					flashWrite(PageHost.concat(FloatBanner[i][0]),FloatBanner[i][3],FloatBanner[i][4],FloatBanner[i][1]);
					document.write('</td></tr>');
				}
				else if(FloatBanner[i][0].indexOf(".htm")>0)
				{
					document.write('<tr><td>');
					document.write('<iframe width="');document.write(FloatBanner[i][3]);document.write('" height="');document.write(FloatBanner[i][4]);document.write('" src="');document.write(FloatBanner[i][0]);document.write('" marginwidth="0" marginheight="0" scrolling="no" frameborder="0"></iframe>');
					document.write('</td></tr>');
				}	
				else if(FloatBanner[i][1]=='')
					document.write('<tr><td><img src="', PageHost.concat(FloatBanner[i][0]), '" width=',FloatBanner[i][3],' border=0></td></tr>');
					else document.write('<tr><td><a href="',FloatBanner[i][1],'" target="',FloatBanner[i][2],'"><img src="', PageHost.concat(FloatBanner[i][0]), '" width=',FloatBanner[i][3],' border=0></a></td></tr>');
		
	document.write('</table>');
}
function ItemMinimize(ctl,Name,count) 
{
	for(i=1;i<=count;i++) 
	{ 
		var IDM=ctl+'_'+i;
		var MItem=document.getElementById(IDM); 
		if (i==Name)
		{ 
			if (MItem.className=='Hide') MItem.className='Show';
		} else 
		{ 
			MItem.className='Hide';	
		} 
	}
}

function LoadAjaxPage1(method,url,param,id,bRedirect,ulrRedirect,showLoading)
{
	if(showLoading==true) $(id).innerHTML='<img src="images/loading.gif" width=16>';
	var RedirectUrl=document.location.href;
	if(bRedirect==true&&ulrRedirect!="") RedirectUrl=ulrRedirect;
	//alert(param);
	var myAjax = new Ajax.Request(
	url, 
	{
		method: method, 
		parameters: param, 
		onComplete: function(transport)
		{
			textValue=trim(transport.responseText);
			if(textValue=="OK"&&bRedirect==true)
			{
				document.location.href=RedirectUrl;
			}else
			{
				if(textValue.substring(0,3)=="OK!"&&bRedirect==true)
				{
					alert(textValue.substring(3));
					document.location.href=RedirectUrl;
				}else
				{
					$(id).innerHTML=textValue;
				}
			}
		}
	});
}

function AjaxLoad(url,param,id)
{
	LoadAjaxPage1('get',url,param,id,false,'',true);
}
function AjaxPost(_form,url,param,id,bRedirect,ulrRedirect,showLoading)
{
	var els =$(_form).elements; 
	query='rand='+parseInt(Math.random()*99999999)+'&'+param;
	for(i=0; i<els.length; i++)
	{
		query+='&'+els[i].name+'='+encodeURI(els[i].value);
	}
	LoadAjaxPage1('post',url,query,id,bRedirect,ulrRedirect,showLoading);
}
