﻿var bToggle = false;

function copySelectedOptions(from,to)
{
	to.options.length = 0;
	var j = 0;
	for(i=0;i<from.options.length;i++)
	{
		if(from.options[i].selected == true)	
		{	
			var temp = from.options[i].value.split("~");
			to.options[j] = new Option(temp[1],temp[0]);
			j++;
		}
	}
}
function copySelectedToTextBox(from,to)
{
	for(i=0;i<from.options.length;i++)
	{
		if(from.options[i].selected == true)	
		{	
			var temp = from.options[i].value.split("~");
			to.value = temp[1];
		}
	}
}

function replaceSubstring(inputString, fromString, toString) 
{
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") 
   {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) 
   { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) 
      {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } 
   else 
   { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") 
      {
         for (var i=0; i < midStrings.length; i++) 
         {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) 
            {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) 
      {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) 
      {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function
function LTrim(sString)
{
	var temp = sString;
	while(temp.substring(0,1) == ' ')
	{
		temp = temp.substring(1, temp.len);
	}
	return temp;
}
function toggle(sName, bDisplay)
{
	if(bDisplay)
	{
		document.getElementById(sName).style.display = '';
	}
	else
	{
		document.getElementById(sName).style.display = 'none';
	}
}
function isNum(n)
{
	var valid = "0123456789";
	//var ok = "yes";
	var temp;
	for (var i = 0; i < n.length; i++) 
	{
		temp = "" + n.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") return false;
	}
	return true;
}
function isISSN(sISSN)
{
	if(sISSN.length != 9)return false;
	if(sISSN.charAt(4) != "-")return false;
	var temp=sISSN.split("-");
	if(!isNum(temp[0]))return false;
	if(!isNum(temp[1]))return false;
	return true;
}
function isEmail1(str)
{
	var filter=/^.+@.+\..{2,3}$/

	if (filter.test(str))
	{
		return true;
	}
	else 
	{
		return false;
	}
}
function isEmail2(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
	{
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1)
	{
		return false;
	}
	if (str.indexOf(" ")!=-1)
	{
		return false;
	}
	return true;			
}
function isDate(sString)
{
	//check for dd/mm/yyyy
	var d1=sString.split("/");
	var _dd=parseFloat(d1[1]);
	var _mm=parseFloat(d1[0]);
	var _yy=parseFloat(d1[2]);
	if(d1.length != 3 || d1[0] == "" || d1[1] == "" || d1[2] == "" || !isNum(d1[0]) || !isNum(d1[1]) || !isNum(d1[2]) || _dd > 31 || _mm > 12 || _yy < 1900 || _dd <= 0 || _mm <= 0 || _yy > 9999)
	{
		return false;
	}
	return true;
}
function fIsEmpty(sName, sMessage)
{
	var oElement = window.document.getElementById(sName);
	if (oElement.value == "")
	{
		return sMessage;
	}
	else
	{
		return "";
	}
}
function fIsNotNum(sName, sMessage)
{
	var oElement = window.document.getElementById(sName);
	if(oElement.value == "") return "";
	if (!isNum(oElement.value))
	{
		return sMessage;
	}
	else
	{
		return "";
	}
}
//kiem tra so CMTND
function isSoCMTND(scmtnd)
{
	if(scmtnd.length != 9)return false;
	if(!isNum(scmtnd))return false;
	return true;
}
function fIsSoCMTND(sName, sMessage)
{
	var oElement = window.document.getElementById(sName);
	if(oElement.value == "") return "";
	if (isSoCMTND(oElement.value))
	{
		return "";
	}
	else
	{
		return sMessage;
	}
}
//KIEM TRA TEXTBOX KHAC RONG CON DROPDOWNLIST =0 THI THONG BAO LOI
function fIschecktextdrpnotempty(sText1,sdrp1,sValue,sMessage)
{
	var oText1 = window.document.getElementById(sText1);
	var oList = window.document.getElementById(sdrp1);
	if(oText1.value == "")return "";
	if(oText1.value != "")
	{
		if(oList.options[oList.selectedIndex].value == sValue)
		{
			return sMessage;
		}
		else
		{
			return "";
		}
		
	}
	else
	{
		return "";
	}
}
//so sanh 2 text box A # NULL, B = NULL thong bao loi
function fIschecktextboxnotempty(sText1, sText2,sMessage)
{
	var oText1 = window.document.getElementById(sText1);
	var oText2 = window.document.getElementById(sText2);
	if(oText1.value == "")return "";
	if(oText1.value != "")
	{
		if(oText2.value == "")
		{
			return sMessage;
		}
		else
		{
			return "";
		}
		
	}
	else
	{
		return "";
	}
}

function fIsNotEmptyLonHon(sName, sMessage)
{
	var oElement = window.document.getElementById(sName);
	if(oElement.value == "") return "";
	if(oElement.length != 9)return false;
	
}

function fIsNotEmail(sName, sMessage)
{
	var oElement = window.document.getElementById(sName);
	if(oElement.value == "") return "";
	if (!isEmail2(oElement.value))
	{
		return sMessage;
	}
	else
	{
		return "";
	}
}
function fIsNotInRange(iRange1, iRange2, sName, sMessage)
{
	var oElement = window.document.getElementById(sName);
	if(oElement.value == "") return "";
	if (!isNum(oElement.value))
	{
		return sMessage;
	}
	var iCheck = parseInt(oElement.value);
	if (iCheck < iRange1 || iCheck > iRange2)
	{
		return sMessage;
	}
	else
	{
		return "";
	}
}
function fIsNotNumOrChar(sChar, sName, sMessage)
{
	var oElement = window.document.getElementById(sName);
	if(oElement.value == "") return "";
	if (isNum(oElement.value) || (oElement.value == sChar))
	{
		return "";
	}
	else
	{
		return sMessage;
	}
}
function fIsChooseActorOk(sName1, sName2, sName3, sName4, sName5, sName6, sName7, sName8, sName9, sName10, sMessage)
{
	var oElement1 = window.document.getElementById(sName1);
	var oElement2 = window.document.getElementById(sName2);
	var oElement3 = window.document.getElementById(sName3);
	var oElement4 = window.document.getElementById(sName4);
	var oElement5 = window.document.getElementById(sName5);
	var oElement6 = window.document.getElementById(sName6);
	var oElement7 = window.document.getElementById(sName7);
	var oElement8 = window.document.getElementById(sName8);
	var oElement9 = window.document.getElementById(sName9);
	var oElement10 = window.document.getElementById(sName10);
	var a = "";
	for(i = 1;i<=10;i++)
	{
		for(j=i+1;j<=10;j++)
		{
			eval("if(oElement" + i + ".value != ''){if (oElement" + i + ".value == oElement" + j + ".value){ a = '" + sMessage + "';}}");
		}
	}
	return a;
}
function fIsChooseActorOk1(sName1, sName2, sMessage)
{
	var oElement1 = window.document.getElementById(sName1);
	var oElement2 = window.document.getElementById(sName2);
	var a = "";
	for(i = 1;i<=2;i++)
	{
		for(j=i+1;j<=2;j++)
		{
			eval("if(oElement" + i + ".value != ''){if (oElement" + i + ".value == oElement" + j + ".value){ a = '" + sMessage + "';}}");
		}
	}
	return a;
}
function isInString(sString, sInString) 
{
  if (sString == "") return true;
  for (i=0; i<sString.length; i++) 
  {
    if (sInString.indexOf(sString.charAt(i),0) == -1) return false;
  }
  return true;
}
function fIsValidString(sString, sName, sMessage)
{
	var oElement = window.document.getElementById(sName);
	if(oElement.value == "") return "";
	if (isInString(oElement.value, sString))
	{
		return "";
	}
	else
	{
		return sMessage;
	}
}
function fIsISSN(sName, sMessage)
{
	var oElement = window.document.getElementById(sName);
	if(oElement.value == "") return "";
	if (isISSN(oElement.value))
	{
		return "";
	}
	else
	{
		return sMessage;
	}
}
function fIsDate(sName, sMessage)
{
	var oElement = window.document.getElementById(sName);
	if(oElement.value == "") return "";
	if (isDate(oElement.value))
	{
		return "";
	}
	else
	{
		return sMessage;
	}
}
function fToggle(bChecked)
{
	bToggle = bChecked;
	toggle("hiderow0000",bChecked);
	toggle("hiderow2006",bChecked);
	toggle("hiderow2007",bChecked);
	toggle("hiderow2008",bChecked);
	toggle("hiderow2009",bChecked);
	toggle("hiderow2010",bChecked);
	toggle("hiderow2011",bChecked);
	toggle("hiderow2012",bChecked);
	toggle("hiderow2013",bChecked);
	toggle("hiderow2014",bChecked);
	toggle("hiderow2015",bChecked);
	toggle("hiderow2016",bChecked);
}
function fShowCategory(sList, sDesc)
{
	var oList = window.document.getElementById(sList);
	var oDesc = window.document.getElementById(sDesc);
	var sText = oList.options[oList.selectedIndex].text;
	oDesc.value = sText
}
function fShowCategory1(sList, sDesc1, sDesc2)
{
	var oList = window.document.getElementById(sList);
	var oDesc1 = window.document.getElementById(sDesc1);
	var oDesc2 = window.document.getElementById(sDesc2);
	var sText = oList.options[oList.selectedIndex].value;
	var temp = sText.split("/")
	oDesc2.value = temp[1];
	oDesc1.value = temp[2];
	//oDesc1.value = sText
}
function fSelectValue(sList, sValue, sMessage)
{
	var oList = window.document.getElementById(sList);
	if(oList.options[oList.selectedIndex].value == sValue)
	{
		return sMessage;
		oList.focus();
	}
	else
	{
		return "";
	}
}
function fSelectValueIsEqual(sList1, sList2, sMessage)
{
	var oList1 = window.document.getElementById(sList1);
	var oList2 = window.document.getElementById(sList2);
	if(oList1.selectedIndex == 0)return ""; 
	if(oList2.selectedIndex == oList1.selectedIndex)
	{
		return sMessage;
	}
	else
	{
		return "";
	}
}
//so sanh 2 textbox co bang nhau khong
function fIsNotEqualtextbox(sText1, sText2, sMessage)
{
	var oText1 = window.document.getElementById(sText1);
	var oText2 = window.document.getElementById(sText2);
	if(oText1.value == "")return "";
	if(oText2.value == "")return "";  
	if(oText2.value != oText1.value)
	{
		return sMessage;
	}
	else
	{
		return "";
	}
}
//This function use for get select value from list box to list box
function fInsert(el,ins) { 
    if (el.setSelectionRange){ 
        el.value = el.value.substring(0,el.selectionStart) + ins + el.value.substring(el.selectionStart,el.selectionEnd) + el.value.substring(el.selectionEnd,el.value.length); 
    } 
    else if (document.selection && document.selection.createRange) { 
        el.focus(); 
        var range = document.selection.createRange(); 
        range.text = ins + range.text; 
    } 
} 
function fGetValueFromListBox(sListFrom, sListTo)
{
	var oListFrom = window.document.getElementById(sListFrom);
	var oListTo = window.document.getElementById(sListTo);
	copySelectedOptions(oListFrom, oListTo);
}
function fGetValueFromListBoxToTextbox(sListFrom, sListTo)
{
	var oListFrom = window.document.getElementById(sListFrom);
	var oListTo = window.document.getElementById(sListTo);
	copySelectedToTextBox(oListFrom, oListTo);
}
function fInsertVariable(sVariable, sSubject, sBody, sWhichText)
{
	var oVariable = window.document.getElementById(sVariable);
	var oWhichText = window.document.getElementById(sWhichText);
	var oText;
	if (oWhichText.value == "1")
	{
		oText = window.document.getElementById(sSubject);
	}
	else
	{
		oText = window.document.getElementById(sBody);
	}
	fInsert(oText, oVariable.options[oVariable.selectedIndex].text);
}

function addOption(selectObject,optionText,optionValue) 
{
	if (selectObject != null)
	{
		var optionObject = new Option(optionText,optionValue);
		var optionRank = selectObject.options.length;
		selectObject.options[optionRank]=optionObject;
    }
}
function deleteOption(selectObject,optionRank) 
{
	if(selectObject.options.length != 0) 
    { 
		selectObject.options[optionRank] = null; 
	}
}
function fMoveToOtherListbox(sFrom, sTo)
{
	var oFrom = window.document.getElementById(sFrom);
	var oTo = window.document.getElementById(sTo);
	addOption(oTo,oFrom.options[oFrom.selectedIndex].text,oFrom.options[oFrom.selectedIndex].value);
	deleteOption(oFrom,oFrom.selectedIndex);
}

//scroll table for div
function ScrollArrange(me, ScrollTag)
	{
		ScrollTag.scrollLeft=me.scrollLeft;
	}
//hien thi ngay thang vn date
function writeTime(s)
{
	var mydate=new Date(s)
	
	var year = mydate.getYear()
	if (year < 1000)
		year += 1900
	var month = mydate.getMonth() + 1
	if (month < 10)
		month = "0" + month
	var day = mydate.getDate()
	if (day < 10)
		day = "0" + day

	var dayw = mydate.getDay()
	
	var hour = mydate.getHours()
	if (hour < 10)
		hour = "0" + hour
	
	var minute=mydate.getMinutes()
	if (minute < 10)
		minute = "0" + minute
	var ap = "AM";
    if (hour   > 11) { ap = "PM";}

	var dayarray=new Array("Ch&#7911; Nh&#7853;t","Th&#7913; Hai","Th&#7913; Ba","Th&#7913; T&#432;","Th&#7913; N&#259;m","Th&#7913; S&#225;u","Th&#7913; B&#7843;y")
	document.write(dayarray[dayw]+", "+day+"/"+month+"/"+year+",&nbsp;"+hour+":"+minute+" "+ap+"")
}

//english date
function writeTime_en(s)
{
	var mydate=new Date(s)
	
	var year = mydate.getYear()
	if (year < 1000)
		year += 1900
	var month = mydate.getMonth() + 1
	if (month < 10)
		month = "0" + month
	var day = mydate.getDate()
	if (day < 10)
		day = "0" + day

	var dayw = mydate.getDay()
	
	var hour = mydate.getHours()
	if (hour < 10)
		hour = "0" + hour
	
	var minute=mydate.getMinutes()
	if (minute < 10)
		minute = "0" + minute
	var ap = "AM";
    if (hour   > 11) { ap = "PM";}

	var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
	document.write(dayarray[dayw]+", "+month+"/"+day+"/"+year+",&nbsp;"+hour+":"+minute+" "+ap+"")
}

// POPUP
var undefined;
var winHandle = null;
function popWin(URL, width, height, target, option ) {
  if( width  == undefined || width  == "" ) width  = 420;
  if( height == undefined || height == "" ) height = 410;
  if( target == undefined || target == "" ) target = "_POP_CTNH";
  
  if( option == undefined || option == "" ) {
      option = ",scrollbars=0,resizable=0,status=1";
  }
  else if(option == 1)
  {
      option = ",scrollbars=1,resizable=1,status=1";    
  }
  else if(option == 2)
  {
      option = ",scrollbars=0,resizable=1,status=1";    
  }

  var w_avail = screen.availWidth;
  var h_avail = screen.availHeight;
  var top     = Math.round ( (h_avail - height) * .5);
  var left    = Math.round ( (w_avail - width ) * .5);
  
  var winOptions = "toolbar=0,location=0,directories=0,menubar=0,copyhistory=0,left=" 
      + left + ",top=" + top + ",width=" + width + ",height=" + height + option;
if (winHandle != null) {
      winHandle.close();
  }
  winHandle = window.open(URL, target,  winOptions);
  
 var popUpsBlocked;
 if(winHandle)
    popUpsBlocked = false;
 else
	popUpsBlocked = true;
 if(popUpsBlocked)
		alert('Trinh duyet cua ban dang chan Pop-Up.\n Ban phai mo tinh nang nay de co the vao duoc he thong!');
		
winHandle.focus();
}

function getPrint(print_area)
		{	
			//Creating new page
			var pp = window.open();
			//Adding HTML opening tag with <HEAD> … </HEAD> portion 
			pp.document.writeln('<HTML><HEAD><title>Print Preview</title><LINK href=Styles.css  type="text/css" rel="stylesheet">')
			pp.document.writeln('<LINK href=../images/PrintStyle.css  type="text/css" rel="stylesheet" media="print"><base target="_self"></HEAD>')
			//Adding Body Tag
			pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0">');
			//Adding form Tag
			pp.document.writeln('<form  method="post">');
			//Creating two buttons Print and Close within a table
			pp.document.writeln('<TABLE width=100% ><TR><TD bgcolor=#d3d3d3></TD></TR><TR><TD align=center bgcolor=#d3d3d3><INPUT style="WIDTH: 60px" ID="PRINT" type="button" value="In" onclick="javascript:location.reload(true);window.print();"><INPUT ID="CLOSE" type="button" value="Hủy" onclick="window.close();"></TD></TR><TR><TD align=center>');
			//Writing print area of the calling page
			pp.document.writeln(document.getElementById(print_area).innerHTML);
			pp.document.writeln('</TD></TR></TABLE>');
			//Ending Tag of </form>, </body> and </HTML>
			pp.document.writeln('</form></body></HTML>');			
			
		}		
function winpopup(urlx,param,twidth,theight)
{
	var strurl= urlx + '?param=' + param;
	var tposx= (screen.width- twidth)/2
	var tposy= (screen.height- theight)/2;

	var newWin=window.open(strurl,"moi","toolbar=no,width="+ twidth+",height="+ theight+ ",directories=no,status=no,scrollbars=yes,resizable=no, menubar=no")
	newWin.moveTo(tposx,tposy);
	newWin.focus();
}
function Print()
{	
	w=open(location.href.concat('&print=y'), '_blank', '');
	return false;	
}
