//Function to open pop up window

function openWin(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 x = 30;
	var y = 40;
	var features = "width=" + width + "px, height=" + height + "px, top=" + y + "px, left=" + x + "px";
		features += (scrollbar) ? ", scrollbars=yes" : "";
		features += ", resizable=no";
	if (!windowName){
		windowName = "untitled";
	}
	var popWindow = window.open(url, windowName, features);

	/*if (document.layers){
		window.moveTo(x, y);
	}*/	
	popWindow.resizeTo(width + 25, height + 30); 
	
	popWindow.focus();
}

function ShowExpand(flat){
	try{
		var menu_id=document.getElementById("menu_id");
		var o1_id=document.getElementById("hide_menu");
		var o2_id=document.getElementById("show_menu");
		
		if(flat){
			o1_id.style.display = '';
			o2_id.style.display = 'none';
			menu_id.style.display = '';
		}
		else{
			o1_id.style.display = 'none';
			o2_id.style.display = '';
			menu_id.style.display = 'none';
		}
	}
	catch(e){
		return true;
	}
}

function fun_check_all(t){
	for(var i=0; i<t.form.elements.length; i++){
		var e=t.form.chk_all.checked;		
		if(t.form.elements[i].name=="id[]"){
			t.form.elements[i].checked=e;
		}
	}
}

function refresh_username(selected_id, selected_username)
{
	//opener.document.forms['search'].user_name.value = selected_username;
	//opener.document.forms['frm_main'].user_id.value = selected_id;
	opener.document.forms['frm_main'].s_pay_person.value = selected_id;
	opener.focus();
	window.close();
}

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 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];}}
}

//--------------------------------------------moi them------------------------------------------------------
// kiem tra  textbox null
function checkNull(obj, msg)
{
	var str = obj.value;
	if((str == null) || (str.length == 0))
	{  
		alert(msg);
		obj.focus();
		obj.style.backgroundColor='#FDFEE7';
		return true;
	}
	obj.style.backgroundColor='#FFFFFF';
	return false;
}
// kiem tra textbox null
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}
function trim(str){return trimLeft(trimRight(str));}
function trimLeftAll(str) {
  if (str==null){return str;}
  for (var i=0; str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t"; i++);
  return str.substring(i,str.length);
}
function trimRightAll(str) {
  if (str==null){return str;}
  for (var i=str.length-1; str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t"; i--);
  return str.substring(0,i+1);
}

// kiem tra combobox null
function checkNullDropdownList(drp, msg){
  var s = drp.value;
  if((s == null) || (s.length == 0))
  { 
    alert(msg);
    drp.focus();
    return true;
  }
  return false;
}

//kiem tra la ky tu kieu so
function isNuber(c)
{		
	return ( ( c >= '0' ) && ( c<= '9') );
}
function isNubers(str)
{
	var isOK=true;
	for(var i=0 ; i< str.length ; i++)
	{		
		if(!isNuber(str.charAt(i)))
		{
			isOK=false;
			break;
		}
	}
	return isOK;	
}
// kiem tra dien thoai co la kieu so?
function isPhoneNumber(str) {
        var stringCharacter="1234567890-()+";
        var c = '';
        for (var i=0; i<str.length; i++)
        {
		    c = str.charAt(i);          
		    if (stringCharacter.indexOf(c,0)==-1)
		    	{return false;}
	  	}  
  return true;
}
function is_0(str)
{
        var stringCharacter="0";
        var c = '';
        var dem=0;
        for (var i=0; i<str.length; i++)
        {
		    c = str.charAt(i);          
		    if (stringCharacter.indexOf(c,0)!=-1)
		    {
				dem=dem+1;
				if(dem==str.length)
		    	{
					return false;
				}
			}
	  	}  
  return true;
}

/*function checkWeb(field)
{
	var web = field.value;
	var check  = /^([whttp:\/])|([http:\/]){3,7}\.(([a-zA-Z0-9_\.\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return check.test(web);
}*/
function checkWeb(web)
{
	if (web=="")
	{
		return false;
	}
	if(!isWebChar(web))
    	return false;
	if (web.indexOf("www")!=0  && web.indexOf("http://")!=0)
	{
		return false;
	}
	if (web.indexOf("www")==0  && web.indexOf("http://")>0)
	{
		return false;
	}
	if (web.indexOf(".")<0 ||(web.lastIndexOf(".")==web.length- 1))
	{
		return false;
	}
	return true;

}
function isWebChar(str){
  for (i=0; i<str.length; i++){
    c = str.charAt(i);          
    if("~!#$%^&*(),\'`\;<>=+\n\t \\\"".indexOf(c,0) > 0)
      return false;        
  }
  return true;
}
function isEmailChar(str){
  for (i=0; i<str.length; i++){
    c = str.charAt(i);          
    if("~!#$%^&*(),\'`:\;?<>=+\n\t/ \\\"".indexOf(c,0) > 0)
      return false;        
  }
  return true;
}

//return true if the parts of email are valid
function isValidEmail(email){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  	if (email == "" || !filter.test(email)) {		
		return false;
	}
	return true;
}
// nhay con nhay toi field chi dinh
function fieldFocus(filefocus)
{	
	filefocus.focus();
}
function reset_frm(frm)
{

		for(var i=0; i<frm.elements.length; i++)
		{	
			switch(frm.elements[i].type.toLowerCase())
			{
				case "text":
					frm.elements[i].value = "";
					break;
				case "checkbox":
					frm.elements[i].checked = false;
					break;
				case "radio":
					frm.elements[i].checked = false;
					break;
				case "select-one":
					frm.elements[i].selectedIndex = 0;
					break;
				case "textarea":
					frm.elements[i].value = "";
									
			}
		}
}
function redirect(URLStr) { location = URLStr; }

function printPage() { print(document); }

function UnicodeSet(iStr)
{
	for (i=0, oStr=''; i < iStr.length; i++)
	{
		j=iStr.charCodeAt(i);
		
		if (j < 32 || j > 127 || j==34 || j==39)
		{
			oStr=oStr.concat('&#').concat(j).concat(';');
		}
		else
		{
			oStr=oStr.concat(iStr.charAt(i)); 
		}
	}
	
	return oStr;
}

function SetUnicode(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;
						}
					}
				}
			}

		}
	
		oStr=oStr.concat(iStr.charAt(i));
		i++;
	}
	
	return oStr;
}

function create_arr(chuoi, aa) {
	st = 0;
	kk = 0;
	arr = new Array(aa + 1);
	for(ii = 0; ii < chuoi.length; ii++) {
		if(chuoi.charAt(ii) == '|') {
			arr[kk] = SetUnicode(chuoi.substring(st, ii));
			st = ii + 1;
			kk++;
		}
	}
	arr[kk] = SetUnicode(chuoi.substring(st, chuoi.length));
	return arr;
}

function trim(str){
   return str.replace(/^\s*|\s*$/g,"");
}
function CheckUnicodeCharactor(obj)
{
  var val = obj.value;
  var length = val.length;  
  var index = -1;  
  if(length == 0)
    return true;
  else
  {
    for(var i = 0 ; i < length ; i++)     
    {
      var charCode = val.charCodeAt(i);
      //(charCode > 126) && (charCode != 215) && (charCode != 216)
      if( (!BelongToUnicodecharacter(charCode)))
      {
        index++;      
        break;
      }
    }  
    if(index > -1)      
      return false;   
    return true;    
  }
}

function BelongToUnicodecharacter(charCode)
{
  if( (charCode <= 126) || (charCode == 215) || (charCode == 216) ||
    ( (charCode >= 161) && (charCode <= 191) ) )
    return true
  return false;
}
