//----------------------
var pWin;
function popWin(url, attr) {
  if (!pWin || pWin.closed) {
    pWin = window.open(url,"pWin", attr);
        if (!pWin.opener) {
          pWin.opener = window;
        }
  } else {
    pWin.location = url;
  }
  pWin.moveTo(0,0);
  pWin.focus();
  window.onunload = function(){pWin.close();}
  return true;
}
//----------------------
function enlarge(url) {
  popWin(url, "resizable=1,width=440,height=440");
}
//----------------------
function enlarge2(url,x,y) {
  popWin(url, 'resizable=1,width=' + x + ',height=' + y);
}
//----------------------
function showhide(id){
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
} else {
obj.style.display = "none";
}
}
}
//----------------------
function showhidecheckbox(id,checkboxid){
if (document.getElementById){
obj = document.getElementById(id);
objcb = document.getElementById(checkboxid);
if (objcb.checked == true){
obj.style.display = "";
} else {
obj.style.display = "none";
}
}
}
//----------------------
function showid(id){
if (document.getElementById){
obj = document.getElementById(id);
obj.style.display = "";
}
}
//----------------------
function hideid(id){
if (document.getElementById){
obj = document.getElementById(id);
obj.style.display = "none";
}
}
//----------------------
function showhidedropdown(form, oldid){
var index=form.selectedIndex
if (form.options[index].value != "0") {
id=form.options[index].value;
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
} else {
obj.style.display = "none";
}
if (oldid != "0") {
obj = document.getElementById(oldid);
obj.style.display = "none";
}
}
oldid = id;
return oldid;
}
}
//----------------------
function showhidedropdown2(form, oldid){
var index=form.selectedIndex
if (form.options[index].value != "0") {
id=form.options[index].value + "b";
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
} else {
obj.style.display = "none";
}
if (oldid != "0") {
obj = document.getElementById(oldid);
obj.style.display = "none";
}
}
oldid = id;
}
return id;
}
//----------------------
function highlight(id){
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.backgroundColor == '#FCE8A3'){
obj.style.backgroundColor = '#FFC155';
} else {
obj.style.backgroundColor = '#FCE8A3';
}
}
}
//----------------------
function highlight1(id){
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.backgroundColor == 'lightgray'){
obj.style.backgroundColor = 'lightyellow';
} else {
obj.style.backgroundColor = 'lightgray';
}
}
}
//----------------------
function highlightcheckbox(id,checkboxid,hilight,lolight){
if (document.getElementById){
obj = document.getElementById(id);
objcb = document.getElementById(checkboxid);
if (objcb.checked == true){
obj.style.backgroundColor = hilight;
} else {
obj.style.backgroundColor = lolight;
}
}
}
//----------------------
function textbox0to5(id){
var ReturnValue = 0
if (document.getElementById){
obj = document.getElementById(id);
if (obj.value > 5 || obj.value < 0){
obj.value = 0;
obj.style.backgroundColor = '#FF0000'
alert('Gia tri chi nam trong khoang tu 0 den 5');
} else {
ReturnValue = obj.value;
}
}
  var checkOK = "0123456789-,";
  var checkStr = obj.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch != ",")
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Chi nhap cac so trong khoang tu 0 den 5.");
    obj.focus();
    obj.value = 0;
    obj.style.backgroundColor = '#FF0000';
    ReturnValue =0;
  }
if (ReturnValue !=0) {obj.style.backgroundColor = '#00FFFF';}
return ReturnValue;
}
//----------------------
function textboxXtoY(id,x,y){
var ReturnValue = -1;
var obj;
if (document.getElementById){
obj = document.getElementById(id);
obj.style.backgroundColor = '#00FFFF';
if (obj.value > y || obj.value < x){
obj.value = x;
obj.style.backgroundColor = '#FF0000'
alert('Gia tri chi nam trong khoang tu ' + x + ' den ' + y);
} else {
ReturnValue = obj.value;
}
}

if (ReturnValue == obj.value){
  var checkOK = "0123456789";
  var checkStr = obj.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch != ",")
      allNum += ch;
  }
  if (!allValid)
  {
    alert('Gia tri chi nam trong khoang tu ' + x + 'den ' + y);
    obj.focus();
    obj.value = x;
    obj.style.backgroundColor = '#FF0000';
    ReturnValue =-1;
  }
  }
if (ReturnValue !=-1) {obj.style.backgroundColor = '#00FFFF';}
return ReturnValue;
}
//----------------------
function floatSS(iX,iY,id){
var L=dD.getElementById?dD.getElementById(id):dD.all?dD.all[id]:dD.layers[id];		
this[id+'O']=L;if(dD.layers)L.style=L;L.nX=L.iX=iX;L.nY=L.iY=iY;
L.P=function(x,y){this.style.left=x+px;this.style.top=y+px;};L.Fss=function(){var pX, pY;	
pX=(this.iX >=0)?0:nn?innerWidth:nn&&dH.clientWidth?dH.clientWidth:dB.clientWidth;	
pY=nn?pageYOffset:nn&&dH.scrollTop?dH.scrollTop:dB.scrollTop;	
if(this.iY<0)pY+=nn?innerHeight:nn&&dH.clientHeight?dH.clientHeight:dB.clientHeight;	
this.nX+=.1*(pX+this.iX-this.nX);this.nY+=.1*(pY+this.iY-this.nY);this.P(this.nX,this.nY);
setTimeout(this.id+'O.Fss()',33);};return L;}
//---------------------------------------
function numbertomoney(id)
{
  ReturnValue = "0";
if (document.getElementById){
  obj = document.getElementById(id);
  obj.value = obj.value.replace(/,/g,"")
  ReturnValue = obj.value;
  
  var checkOK = "0123456789-,.";
  var checkStr = obj.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  var stlen = checkStr.length;
  // check valid first
  for (i = 0;  i < stlen;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch != ",")
      allNum += ch;
    //alert(14.5 / 1);
    //if (((stlen-i-1)%3 ==0)&&(stlen-i-1>0))
    // allNum += ",";
  }
  if (!allValid)
  {
    alert("Sai du lieu. Gia tien chi duoc phep la con so");
    obj.focus();
    obj.value = 0;
    obj.style.backgroundColor = '#FF0000';
    ReturnValue ="0";
  }
  //translate from a string number to money
  if (ReturnValue !="0")
  {  
    var v = parseFloat(allNum);
    var rest = v % 1; //-- Phan thap phan, sau dau cham giu nguyen
    var stint = parseFloat(allNum) - rest;
    var s = String(stint)
    stlen = s.length;
    var r = String(rest)
    r = r.slice(2,4)
    if (rest ==0){r=''}
    allNum = "";
    for (i = 0;  i < stlen;  i++)
    { 
      ch = s.charAt(i);
      allNum += ch;
      if (((stlen-i-1)%3 ==0)&&(stlen-i-1>0))
        allNum += ",";
    }
    if (rest>0){allNum += "." + r;}
    ReturnValue =allNum;
    obj.value = allNum;
  }
}
if (ReturnValue !="0") {obj.style.backgroundColor = '#00FFFF';}
return ReturnValue;
}
//------------------------------------------
function pricecompare(id, idorigin)
{
if (document.getElementById){
obj = document.getElementById(id);
objorigin = document.getElementById(idorigin);
 var newvalue = parseFloat(obj.value.replace(/,/g,""));
 var originvalue = parseFloat(objorigin.value);
// alert(newvalue);
// alert(newvalue / originvalue);
 var t = newvalue / originvalue;
 if (t > 1.1 || t < 0.9)
   {obj.style.backgroundColor = '#00FF00';}
   else
   {obj.style.backgroundColor = '#00FFFF';};
 if (t==1) {obj.style.backgroundColor = '#FFFFFF';}
}
return true;
}
//------------------------------------------
function toggleDiv(id,flagit) {
var tX = tempX;
var tY = tempY;
  if (tY + 200 > winH + document.body.scrollTop) {tY = tY - 150;}
  if (tX + 250 > winW + document.body.scrollLeft) tX = tX - 250;
if (flagit=="1"){
if (document.layers) {document.layers[''+id+''].visibility = "show";
  document.layers[''+id+''].top = tY;document.layers[''+id+''].left = tX;}
else if (document.all) {document.all[''+id+''].style.visibility = "visible";
  document.all[''+id+''].style.top = tY;document.all[''+id+''].style.left = tX;}
else if (document.getElementById) {document.getElementById(''+id+'').style.visibility = "visible";
  document.getElementById(''+id+'').style.top = tY;document.getElementById(''+id+'').style.left = tX;}
}
else
if (flagit=="0"){
if (document.layers) document.layers[''+id+''].visibility = "hide"
else if (document.all) document.all[''+id+''].style.visibility = "hidden"
else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
}
}
//------------------------------------------
function toggleDivPos(id,flagit,w,h) {
var tX = tempX;
var tY = tempY;
  if (tY + h + 50 > winH + document.body.scrollTop) {tY = tY - h - 50;}
  if (tX + w + 50 > winW + document.body.scrollLeft) tX = tX - w - 50;
if (flagit=="1"){
if (document.layers) {document.layers[''+id+''].visibility = "show";
  document.layers[''+id+''].top = tY;document.layers[''+id+''].left = tX;}
else if (document.all) {document.all[''+id+''].style.visibility = "visible";
  document.all[''+id+''].style.top = tY;document.all[''+id+''].style.left = tX;}
else if (document.getElementById) {document.getElementById(''+id+'').style.visibility = "visible";
  document.getElementById(''+id+'').style.top = tY;document.getElementById(''+id+'').style.left = tX;}
}
else
if (flagit=="0"){
if (document.layers) document.layers[''+id+''].visibility = "hide"
else if (document.all) document.all[''+id+''].style.visibility = "hidden"
else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
}
}
//-----------------------------------------------
var winW = 630, winH = 460;
//winW=document.all?document.body.clientWidth:window.innerWidth;
//winH=document.all?document.body.clientHeight:window.innerHeight; 
//-----------------------------------------------
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s
//-----------------------------------------------
function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  //obj = document.getElementById('T1');
  //obj.value = tempY //+ '--' + tempY
  //document.Show.MouseY.value = tempY
  tempY += 20
  winW=document.all?document.body.clientWidth:window.innerWidth;
  winH=document.all?document.body.clientHeight:window.innerHeight; 
  //if (tempX + 250 > document.body.clientWidth) tempX = document.body.clientWidth - 250;
  //if (tempY + 100 > document.body.clientHeight) tempY = document.body.clientHeight - 100;
  //if (tempY + 200 > winH + document.body.scrollTop) {tempY = tempY - 150;}
  //if (tempX + 250 > winW + document.body.scrollLeft) tempX = tempX - 250;
  return true
}
//-----------------------------------------------
function inputVND(idprice,idcheck){
if (document.getElementById){
objp = document.getElementById(idprice);
objc = document.getElementById(idcheck);
var v = objp.value;
v = v.replace(/,/g,"");
if (v>5000) {objc.checked = true} else {objc.checked = false}
//objp.value=200;
}
return true
}
//-----------------------------------------------
function inputFixedVND(idprice,idcheck){
if (document.getElementById){
objp = document.getElementById(idprice);
objc = document.getElementById(idcheck);
var v = objp.value;
v = v.replace(/,/g,"");
if (v<2000) {objc.checked = false}
}
return true
}
//-----------------------------------------------
function changeimg(idimg, img_src,w,h){
if (document.getElementById){
objp = document.getElementById(idimg);
   {
   objp.src = img_src;
   objp.width = w;
   objp.height = h;
   }
}
}
//-----------------------------------------------
