var fBrw=(navigator.userAgent.indexOf('MSIE')!= -1 && navigator.userAgent.indexOf('Windows')!= -1);

function refreshParent() {
  window.opener.location.href = window.opener.location.href;
  if (window.opener.progressWindow)
 {
    window.opener.progressWindow.close()
  }
  window.close();
}

function popUp( location, w, h , screenX, screenY, menubar , scrollbars, resizable,status) 
{
	if( w == null ) { w = 300; }
	if( h == null ) { h = 200; }
	if(screenX == null) screenX = (screen.width-w)/2;
	if(screenY == null) screenY = (screen.height-h)/2;
	if( menubar == null) menubar = "0";
	if(scrollbars == null) scrollbars="0";
	if(resizable == null) resizable="0";
	if( status == null ) { status = "1"; }
	
	var toolbar = "'menubar="+menubar+",scrollbars="+scrollbars+", resizable="+resizable+",screenX="+screenX+",screenY="+screenY+", top="+screenY+", left="+screenX+",width="+w+",height="+h+",status="+status+"'";
	var editorWin = window.open(location,'editWin', toolbar);
	editorWin.focus(); 
}

function openImage(vLink, vHeight, vWidth)
{
	//var sLink = (typeof(vLink.href) == 'undefined') ? vLink : vLink.href;
	var sLink = vLink;
	
	if (sLink == '')
	{
		return false;
	}

	winDef = 'status=no,resizable=no,scrollbars=no,toolbar=no,location=no,fullscreen=no,titlebar=yes,height='.concat(vHeight).concat(',').concat('width=').concat(vWidth).concat(',');
	winDef = winDef.concat('top=').concat((screen.height - vHeight)/2).concat(',');
	winDef = winDef.concat('left=').concat((screen.width - vWidth)/2);
	newwin = open('', '_blank', winDef);

	newwin.document.writeln('<html>');
	newwin.document.writeln('<head>');
	newwin.document.writeln('<title>Xem anh</title>');   
	newwin.document.writeln('</head>');
	
	if (vHeight == null || vWidth == null){	// resize the window to match the picture
		if (navigator.appName == "Microsoft Internet Explorer") 
			// in IE resizeTo give the outer of the window
			newwin.document.writeln('<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" onLoad="window.moveTo((screen.width-document.images[0].width)/2,(screen.height-document.images[0].height)/3); window.resizeTo(document.images[0].width+20,document.images[0].height+ 20)">');
		else
			newwin.document.writeln('<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" onLoad="window.moveTo((screen.width-document.images[0].width)/2,(screen.height-document.images[0].height)/3); window.resizeTo(document.images[0].width,document.images[0].height)">');
	}
	else 
		newwin.document.writeln('<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">');	

	newwin.document.writeln('<a href="" onClick="window.close(); return false;"><img src="', sLink, '" alt="', (fBrw) ? '&#272;&#243;ng l&#7841;i' : 'Dong lai', '" border=0></a>');
	newwin.document.writeln('</body>');
	newwin.document.writeln('</html>');

	if (vHeight == null || vWidth == null){
		newwin.document.close();
		newwin.focus();  // place the window in front
	}
	
	if (typeof(vLink.href) != 'undefined')
	{
		return false;
	}
}

function openFile(item,pageid,how){
	window.open(pageid+'&opt=brpage&item='+item+'&how='+how,'_blank');
}
function saveFile(item,pageid,how){
	window.open(pageid+'&opt=brpage&item='+item+'&act=save&how='+how,'_self');
}
function viewFile(item,pageid,how){
	window.open(pageid+'&opt=brpage&item='+item+'&act=view&how='+how,'_blank');
}

function expandMe(id) {
	var obj = document.getElementById(id);
	if(obj!=null){
		var color = null;
		var act = null;
		var cls = null;
		var title = 'Mo xuong';
		
		var box = document.getElementById(id+'_box');
		if(box!=null) cls = box.className;

		//if(cls==null) color = 'red';//'white';
		//else color = 'white';
		color = 'red';

		if(obj.className=='off'){
			obj.className = 'on';
			title = 'Thu lai';
			
			act = 'dropto';
		}
		else{
			obj.className = 'off';
			
			act = 'arrowto';
		}
		obj.data = true;
		
		var img = document.getElementById(id+'_img');
		if(img!=null){
			img.src = '../images/'+act+'_'+color+'.gif';
			img.title = title;
		}		
	}
}

function closeMe() {
	opener.location.reload();
	self.close();
}

function checkDelete(msg) {
 var value = confirm(msg);
	if (value == true) {
		return true;
	} else {
		return false;
	}
}

function selectedVals(name){
	var val=null;
	var aSelects=document.getElementById(name);
	var nRows = aSelects.length;
	for (var i = 0; i < nRows; i++) {
		if(aSelects[i].selected){
			if(val==null) val = aSelects[i].value;
			else val = val + ',' + aSelects[i].value;
		}
	}
	return val;
}

function checkValsByName(name,sep){
	var val=null;
	var aSelects=document.getElementsByName(name);
	var nRows = aSelects.length;
	for (var i = 0; i < nRows; i++) {
		if(aSelects[i].checked){
			if(val==null) val = aSelects[i].value;
			else val = val + sep + aSelects[i].value;
		}
	}
	return val;
}
function textValsByName(name,sep){
	var val=null;
	var aSelects=document.getElementsByName(name);
	var nRows = aSelects.length;
	for (var i = 0; i < nRows; i++) {
		if(aSelects[i].value!=null&&aSelects[i].value!=''){
			if(val==null) val = aSelects[i].value;
			else val = val + sep + aSelects[i].value;
		}
	}
	return val;
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

/*Cookie functions*************************************/
function GetCookie (name) { 
	var arg = name + "="; 
	var alen = arg.length; 
	var clen = document.cookie.length; 
	var i = 0; 
	while (i < clen) { 
		var j = i + alen; 
		if (document.cookie.substring(i, j) == arg) 
			return getCookieVal (j); 
		i = document.cookie.indexOf(" ", i) + 1; 
		if (i == 0) break; 
	} 
	return null;
}

function SetCookie (name, value) { 
	var argv = SetCookie.arguments; 
	var argc = SetCookie.arguments.length; 
	var expires = (argc > 2) ? argv[2] : null; 
	var path = (argc > 3) ? argv[3] : null; 
	var domain = (argc > 4) ? argv[4] : null; 
	var secure = (argc > 5) ? argv[5] : false; 
	document.cookie = name + "=" + escape (value) + 
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
	((path == null) ? "" : ("; path=" + path)) + 
	((domain == null) ? "" : ("; domain=" + domain)) + 
	((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) { 
	var exp = new Date(); 
	exp.setTime (exp.getTime() - 1); 
	var cval = GetCookie (name); 
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function setToTime(expire) {
	var exp = new Date(); 
	exp.setTime(exp.getTime() + expire);
	return exp;
}

function preview_image(src_item,view_item){
	var src=document.all.item(src_item).value;
	document.images[view_item].src = src;
}

function CharReplace(iStr)
{
	var	r1=/%26/g;
	var	r2=/%20/g;
	var	r3=/%22/g;

	iStr	=iStr.replace(r1, '&');
	iStr	=iStr.replace(r2, ' ');
	iStr	=iStr.replace(r3, '"');

	return iStr;
}

function GetPostVariable(vName, vDef)
{
	var	str=location.href;
	var	pos=str.indexOf('?'.concat(vName).concat('='));

	if (pos==-1)
	{
		pos=str.indexOf('&'.concat(vName).concat('='));
		if (pos==-1) return vDef;
	}
	
	str=str.substring(pos + vName.length + 2);
	pos=str.indexOf('&');

	if (pos==-1)
	{
		pos=str.length;
	}	

	if (pos > 0)
	{
		str=str.substring(0, pos);
	}

	return (typeof(vDef)=='number') ? parseInt(str) : CharReplace(str);
}

function UnicodeSet(iStr)
{
	for (i=0, oStr=''; i < iStr.length; i++)
	{
		switch ((j=iStr.charCodeAt(i)))
		{
		case 34:
			oStr=oStr.concat('&quot;');
			break;
		case 38:
			oStr=oStr.concat('&amp;');
			break;
		case 39:
			oStr = oStr.concat('&#39;');
			break;
		case 60:
			oStr = oStr.concat('&lt;');
			break;
		case 62:
			oStr = oStr.concat('&gt;');
			break;
		default:
			if (j < 32 || j > 127 || j==34 || j==39)
			{
				oStr=oStr.concat('&#').concat(j).concat(';');
			}
			else
			{
				oStr=oStr.concat(iStr.charAt(i)); 
			}
			break;
		}
	}
	
	return oStr;
}

function UnicodeGet(iStr)
{
	for (i=0, oStr=''; i < iStr.length; )
	{
		if (iStr.charCodeAt(i)==38)
		{
			if (iStr.charCodeAt(i + 1)==35)
			{
				p=iStr.indexOf(';', i  + 2);
				if (p!=-1)
				{
					if (p - i <= 7)
					{
						if (isFinite(iStr.substr(i + 2, p - i - 2)))
						{
							oStr = oStr.concat(String.fromCharCode(iStr.substr(i + 2, p - i - 2)));
							i = p + 1;
							continue;
						}
					}
				}
			}
			else
			{
				p=iStr.indexOf(';', i  + 1);
				if (p!=-1)
				{
					switch (iStr.substr(i + 1, p - i - 1))
					{
					case 'amp':
						oStr = oStr.concat('&');
						i = p + 1;
						break;
					case 'quot':
						oStr = oStr.concat('"');
						i = p + 1;
						break;
					case 'lt':
						oStr = oStr.concat('<');
						i = p + 1;
						break;
					case 'gt':
						oStr = oStr.concat('>');
						i = p + 1;
						break;
					}
				}
			}
		}
	
	
		oStr=oStr.concat(iStr.charAt(i));
		i++;
	}
	
	return oStr;
}

function SearchMe(s, a)
{
	while (s.length > 0 && s.charAt(0) <= ' ')
	{
		s = s.substr(1);
	}

	while ((i=s.length) > 0 && s.charAt(i - 1) <= ' ')
	{
		s = s.substr(0, i - 1);
	}

	if (s=='')
	{
		document.Search.TSearch.value = s;
		return false;
	}
	
	//f = GetPostVariable('r', RelatedFolder);
	//s = escape(UnicodeSet(s));
	r = 'index.php?option=search&'.concat('&s=').concat(s);

	if (location.pathname.toLowerCase()=='/search/')
	{
		location.replace(r);
	}
	else
	{
		location.href=r;
	}
	return false;
}

function SearchOnFocus(field)
{
	if(field.value=='Search')
	{
		field.value = '';
	}
}

function SearchOnBlur(field)
{
	if(field.value=='')
	{
		field.value='Search';
	}
}

function ShowSearch()
{
	if ((s=GetPostVariable('s', ''))!='')
	{
		s = unescape(s);
	}

	s=UnicodeGet(s);

	document.writeln('<table cellspacing=0 cellpadding=0 border=0 align=left>');
	document.writeln('<form method="POST" name="Search" onSubmit="return SearchMe(document.Search.TSearch.value, 1)">');
	document.writeln('<tr>');
	document.writeln('<td><div><input type="text" name="TSearch" value="Search" class=SearchBox onfocus="SearchOnFocus(this)" onblur="SearchOnBlur(this)"></div></td>');
	document.writeln('<td class=BreakLine width=2></td>');
	document.writeln('<td><a href="javascript:SearchMe(document.Search.TSearch.value, 1)"><img src="images/Go.gif" border=0></a></td>');
	document.writeln('</tr>');
	document.writeln('</form>');
	document.writeln('</table>');

	if (s!='')
	{
		document.Search.TSearch.value = s;
	}
}


function LTrim(iStr)
{
	while (iStr.charCodeAt(0) <= 32)
	{
		iStr=iStr.substr(1);
	}
	return iStr;
}

function RTrim(iStr)
{
	while (iStr.charCodeAt(iStr.length - 1) <= 32)
	{
		iStr=iStr.substr(0, iStr.length - 1);
	}
	return iStr;
}

function Trim(iStr)
{
	while (iStr.charCodeAt(0) <= 32)
	{
		iStr=iStr.substr(1);
	}

	while (iStr.charCodeAt(iStr.length - 1) <= 32)
	{
		iStr=iStr.substr(0, iStr.length - 1);
	}

	return iStr;
}

//check form
var errormsg="";
function checkEmailAddress(Email)
{
	Email = Trim(Email);

	while (Email != '')
	{
		c = Email.charAt(0);	
		if (c==' ' || c=='<' || c==39 || c==':' || c=='.')
		{
			Email = Email.substr(1);
		}
		else
		{
			break;
		}
	}

	i = Email.indexOf('>');
	if (i==-1)
	{
		while (Email != '')
		{
			c = Email.charAt(Email.length - 1);
			if (c==' ' || c==39 || c=='.')
			{
				Email = Email.substr(0, Email.length - 1);
			}
			else
			{
				break;
			}
		}
	}
	else
	{
		Email = Email.substr(0, i);
	}

	if (Email.length > 96)
		return '';

	i = Email.lastIndexOf('@');
	j = Email.lastIndexOf('.');
	if (i < j)
		i = j;

	switch (Email.length - i - 1)
	{
	case 2:
		break;
	case 3:
		switch (Email.substr(i))
		{
		case '.com':
		case '.net':
		case '.org':
		case '.edu':
		case '.mil':
		case '.gov':
		case '.biz':
		case '.pro':
		case '.int':
		case '.vn':
		case '.us':
		case '.au':
			break;
		default:
			return '';
		}
		break;
	default:
		switch (Email.substr(i))
		{
		case '.name':
		case '.info':
			break;
		default:
			return '';
		}
		break;
	}

	Email = Email.toLowerCase();

	if (Email == '')
		return '';

	if (Email.indexOf(' ') != -1)
		return '';

	if (Email.indexOf('..') != -1)
		return '';

	if (Email.indexOf('.@') != -1)
		return '';

	if (Email.indexOf('@.') != -1)
		return '';

	if (Email.indexOf(':') != -1)
		return '';

	for (i=0; i < Email.length; i++)
	{
		c = Email.charAt(i);

		if (c >= '0' && c <= '9')
			continue;
		
		if (c >= 'a' && c <= 'z')
			continue;
		
		if ('`~!#$%^&*-_+=?/\\|@.'.indexOf(c) != -1)
			continue;

		return '';
	}

	if ((i=Email.indexOf('@'))==-1)
		return '';

	if (Email.substr(i + 1).indexOf('@')!=-1)
		return '';

	if (Email.charAt(0)=='.' || Email.charAt(Email.length - 1)=='.')
		return '';

	return Email;
}

function checkDate(dd,mm,yyyy,name)
{
   result=true;
   if(!isNaN(yyyy)&&(yyyy!="")&&(yyyy<10000))	
   {
      if( (mm=="02") && ((dd=="30")||(dd=="31")) )
         result=false;
      if( (mm=="02") && (dd=="29") && (yyyy%4>0) )
         result=false;
      if( ((mm=="04")||(mm=="06")||(mm=="09")||(mm=="11")) && (dd=="31") )
         result=false;
   }
   else
   {
      result=false;
   }
   if(result==false)
      errormsg=errormsg+"- "+name+" khong dung dinh dang.\n"	
   return result;
}

function checkTextField(str,name)
{
	if(str=="")
	{
		errormsg=errormsg+"- "+name+" chua nhap thong tin.\n";
		return false;
	}
	else
		return true;
}

function checkPassword(p,namep,rp,namerp)
{
	if(p!=="")
	{
		if(p==rp)
	 		return true;
	 	else
		{			
			errormsg=errormsg+"- "+namep+" khong giong voi "+namerp+".\n";			
			return false;
		}	 	
	}
	else
	{	
		errormsg=errormsg+"- "+namep+" chua co thong tin.\n";
		return false;
	}
	
}

function checkIsIntNum(val,name)
{
	if((val!="")&&(!isNaN(val)) && val.indexOf(".")<0)
		return true;
	else
	{	
		errormsg=errormsg+"- "+name+" khong phai la so nguyen.\n";
		return false;
	}
}

function checkChoice(val,name)
{
	if(val=="")
		errormsg=errormsg+"- "+name+" phai chon mot.\n"; 
}

function checkIsNum(val,name)
{
	if((val!="")&&(!isNaN(val)))
		return true;
	else
	{	
		errormsg=errormsg+"- "+name+" khong phai la mot so.\n";
	}

}

function setCheckboxes(the_form, the_box ,do_check)
{
	var elts   = 	document.forms[the_form].elements[the_box];				
	var elts_cnt  =  elts.length;
	if (elts_cnt)
	{
		for (var i = 0; i < elts_cnt; i++)
			elts[i].checked = do_check;
	} 
	else
	  	elts.checked        = do_check;
	return true;
} 

function checkCheckboxes(the_form, the_box )
{
	var elts   = 	document.forms[the_form].elements[the_box];				
	var elts_cnt  =  elts.length;
	var a = false;
	for(i=0;i<elts_cnt;i++)
	{
		if(elts[i].checked==true)
		{
			a=true;
			break;
		}
	}	
	return a;
}

function checkAll()
{
	if(errormsg!="")
	{
		alert("---------------------------------------\nXIN KIEM TRA LAI THONG TIN !    \n---------------------------------------\n"+errormsg);
		errormsg="";
		return false;
	}
	else
		return true;
	errormsg="";
}
