var err_title_alert = "VUI LÒNG KIỂM TRA LẠI THÔNG TIN !";
var err_illegal = "không hợp lệ";
var err_notvalid = "không có giá trị";
var err_notnumber = "phải có giá trị là số";

var errormsg="";
function clearError()//goi ham` nay truoc
{
   errormsg="";
}
function checkEmail(email, name){
	var at1      =  email.indexOf("@");
	var at2      =  email.lastIndexOf("@");
	var dot     =  email.indexOf(".");
	var space   =  email.indexOf(" ");
	if((at1!=-1)&&(at1!=0)&&(at2==at1)&&(dot!=-1)&&(dot<email.length-1)&&(space==-1)){
		return true;
	}else{
		errormsg=errormsg+"- `"+name+"` "+err_illegal+" .\n";
		return false;
	}
}
function checkEmail_noalert(email){
	var at1      =  email.indexOf("@");
	var at2      =  email.lastIndexOf("@");
	var dot     =  email.indexOf(".");
	var space   =  email.indexOf(" ");
	if((at1!=-1)&&(at1!=0)&&(at2==at1)&&(dot!=-1)&&(dot<email.length-1)&&(space==-1)){
		return true;
	}else{
		return false;
	}
}
function checkDate(dd,mm,yyyy,name)
{
   result=true;
   if(!isNaN(mm)&&(mm!="")&&(parseInt(mm)<12)){
   		result=false
   }
   if(!isNaN(dd)&&(dd!="")&&(parseInt(dd)<31)){
   		result=false
   }
   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+"` "+err_illegal+".\n"	
   return result;
}
function checkInterval(dstart,mstart,ystart,names,dend,mend,yend,namee)
{
   var re=true;
   re=checkDate(dstart,mstart,ystart,names)&&checkDate(dend,mend,yend,namee);   
   if(re)
   {
      var yyyy=yend-ystart;
      if(yyyy>0)
	 re=true;
      else if(yyyy==0)
      {
         var mm=mend-mstart;
         if(mm>0)
            re=true;
         else if(mm==0)
         {
            var dd=dend-dstart;
	    if(dd>=0)
	       re=true;
            else
               re=false; 		 
         }
         else
	    re=false; 
      }
      else
         re=false; 	
   }
   if(re==false)
      errormsg=errormsg+"- Interval time is illegal.\n";
   
   return re; 
}
function checkTextField(str,name)
{
	if(str=="")
		errormsg=errormsg+"- `"+name+"` "+err_notvalid+".\n";
}

function checkRadio(val,name)
{
	var legal=false;
	for(var i=0;i<val.length;i++)
	{
		if(val[i].checked==true)
		{
			legal=true;
		}
	}
	if(legal==false)
		errormsg=errormsg+"- `"+name+"` "+err_notvalid+".\n";
}
function checkChoice(val,name)
{
	if(val<0)
		errormsg=errormsg+"- `"+name+"` "+err_notvalid+".\n"; 
}
function checkIsNum(val,name)
{
	if((val!="")&&(!isNaN(val)))
		return true;
	else
	{	
		errormsg=errormsg+"- `"+name+"` "+err_notnumber+" . \n";
	}

}
function checkAll()
{
	if(errormsg!="")
	{
		alert(err_title_alert+"\n\n"+errormsg);
		return false;
	}
	else
		return true;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  url=window.open(theURL,winName,features);
  url.focus();
}

//dependent, directories, fullscreen, location, menubar, resizable, scrollbars, status, toolbar, top=200, left=400, width=200, height=200, screenX=400, screenY=200
function OpenNewWindow(theURL, myname, w, h, features) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+''+features
	win = window.open(theURL, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
function selCheckbox(n, fldName){
  if (!fldName) {
     fldName = 'cb';
  }
	var f = document.adminForm;
	var c = f.toggle.checked;
	var n2 = 0;
	for (i=0; i < n; i++) {
		cb = eval( 'f.' + fldName + '' + i );
		if (cb) {
			cb.checked = c;
			n2++;
		}
	}
	if (c) {
		document.adminForm.boxchecked.value = n2;
	} else {
		document.adminForm.boxchecked.value = 0;
	}
}
function isChecked(isitchecked){
	if (isitchecked == true){
		document.adminForm.boxchecked.value++;
	}
	else {
		document.adminForm.boxchecked.value--;
	}
}
function submitbutton(pressbutton) {
	submitform(pressbutton);
}

/**
* Submit the admin form
*/
function submitform(pressbutton){
	document.adminForm.option.value=pressbutton;
	try {
		document.adminForm.onsubmit();
		}
	catch(e){}
	document.adminForm.submit();
}

function checkLen(Target, Remain, num) {	
	StrLen = Target.value.length;
	if (StrLen > num){
		Target.value = Target.value.substring(0,num);
		charsLeft = 0;
	} else {
		charsLeft = num - StrLen;
	}
	Remain.value=StrLen;
}

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

if (document.layers) { // Netscape
  document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
  document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
  document.onmousemove = captureMousePosition;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

function captureMousePosition(e) {

  if (document.layers) { //Netscape
    xMousePos = e.pageX;
    yMousePos = e.pageY;
    xMousePosMax = window.innerWidth+window.pageXOffset;
    yMousePosMax = window.innerHeight+window.pageYOffset;
  } else if (document.all) { //IE
    xMousePos = window.event.x+document.body.scrollLeft;
    yMousePos = window.event.y+document.body.scrollTop;
    xMousePosMax = document.body.clientWidth+document.body.scrollLeft ;
    yMousePosMax = document.body.clientHeight+document.body.scrollTop ;
  } else if (document.getElementById) {
  
    // Netscape 6 behaves the same as Netscape 4 in this regard
    xMousePos = e.pageX;
    yMousePos = e.pageY;
    xMousePosMax = window.innerWidth+window.pageXOffset;
    yMousePosMax = window.innerHeight+window.pageYOffset;
  }
  
  //window.status = "xMousePos=" + xMousePos + ", yMousePos=" + yMousePos + ", xMousePosMax=" + xMousePosMax + ", yMousePosMax=" + yMousePosMax;

}

function expandCell(el, expid){
  el.style.cursor="pointer";
  el.onmouseout=function() {
    el.style.cursor="default";
  }
  el.onclick=function() {
    if(document.getElementById(expid)){
      flag = getCookie(expid);
      if(parseInt(flag)==1){
        document.getElementById(expid).className = 'hidden';
        writeCookie(expid, 0);
      }else{
        document.getElementById(expid).className = 'show';
        writeCookie(expid, 1);
      }
    }
  }
}

function getCookie(name){
  if(document.cookie){
    cookieArray=document.cookie.split(";");
    cookieArray2=new Array();
    for(i in cookieArray){
      cookieArray2[cookieArray[i].split("=")[0].replace(/ /g,"")]=cookieArray[i].split("=")[1].replace(/ /g,"");
    }
    return cookieArray2[name];
  }
  return 0;
}

function writeCookie(name, value){ // Runs through the menu and puts the "states" of each nested list into an array, the array is then joined together and assigned to a cookie.
  strCookie = name+"="+value;
  document.cookie=strCookie+";expires="+new Date(new Date().getTime() + 365*24*60*60*1000).toGMTString();
}
