var cuform = '';
var cuformord = 0;
var bgurl;

function getRandom() 
{
  var rnd;
  rnd = parseInt((Math.random())*1000000);
  if (isNaN(rnd)) rnd = 1000000;
  return rnd;
}

function dw(s) 
{
  document.write(s);
}

function check(val) 
{
  var mikExp = /[0-9]/;
  var strNew = '';
  for (var i = 0; i < val.value.length; i++) 
  {
    var lchar = val.value.charAt(i);
    if (lchar.search(mikExp) != -1)   strNew += lchar;
  }
  if (val.value != strNew)  val.value = strNew;
}

function chdate(dt,mbnull,frmt) 
{
  var digExp = /[0-9]/;
  if (dt.length < frmt.length)
  {
    if (dt.charAt(0)!="0" && dt.charAt(1)==frmt.charAt(2)) dt = '0'+dt;
    if (dt.charAt(3)!="0" && dt.charAt(2)==frmt.charAt(2) && dt.charAt(4)==frmt.charAt(5)) 
      dt = dt.substr(0,3)+"0"+dt.substring(3);
    if (dt.length==7) dt=dt.substr(0,6)+"200"+dt.substring(6);
    if (dt.length==8) dt=dt.substr(0,6)+"20"+dt.substring(6);
  }
  p0 = dt.charAt(0);
  p1 = dt.charAt(1);
  p2 = dt.charAt(2);
  p3 = dt.charAt(3);
  p4 = dt.charAt(4);
  p5 = dt.charAt(5);
  p6 = dt.charAt(6);
  p7 = dt.charAt(7);
  p8 = dt.charAt(8);
  p9 = dt.charAt(9);

  bRet = false;
  
  if (dt.length==0 && mbnull=='Y') bRet = true;
  else
  {
   if (dt.length==0 && mbnull=='N') 
   { 
     bRet = false; alert("Необходимо ввести дату !");
   }
   else
   {
    if (dt.length==frmt.length && p2==frmt.charAt(2) && p5==frmt.charAt(5))
    {
     if ((p0.search(digExp) != -1) && (p1.search(digExp) != -1) &&
         (p3.search(digExp) != -1) && (p4.search(digExp) != -1) &&
         (p6.search(digExp) != -1) && (p7.search(digExp) != -1) &&
         (p8.search(digExp) != -1) && (p9.search(digExp) != -1)) 
     {
       nday  = Number(p0+p1);
       nmon  = Number(p3+p4);      
       nyear = Number(p6+p7+p8+p9);      
       if ( nyear >= 1900 && nmon >= 1 && nmon <= 12 && nday >= 1 && nday <= 31 ) bRet = true;
     }
    }
    if (bRet==false) alert("Неверный формат даты: "+dt+". Допустимый формат даты: "+frmt);
   }
  }
  return bRet;
}


function to_char(n) 
{
  str = "" + Math.round(parseFloat(n)*100)/100 + ""
  ret = ""
  k = str.indexOf('.')
  if (k > -1)
  {
    ret = str.substring(k)
    for(i=ret.length; i<3; i++)
    ret += '0'
  }
  else
  {
    k = str.length;
    ret = '.00'
  } 
  j = 0
  for(i=k-1; i > -1; i--)
  {
     ret = str.charAt(i) + ret
     if(j++ == 2 && i > 0)
     {
       ret = ',' + ret
       j = 0
     }
  }
  return ret
}

function wopen(url,wd,ht,nm)
{
  lt = (screen.width-wd)/2-5;
  tp = (screen.height-ht)/2-50;
  if (tp<0) tp = 5;
  if (lt<0) lt = 5;
  winprm = window.open(url+"&x="+Math.round(Math.random()*10000)
                    +"&y="+Math.round(Math.random()*10000), nm,
         'width='+wd+',height='+ht+',resizable=yes,titlebar=no,scrollbars=yes,status=no,toolbar=no,'
        +'left='+lt+',top='+tp);
  winprm.focus();
}


function bagadd(urladd, urlfrom)
{
  IFRBAG.location.href = urladd + '&urlfrom=' + escape(urlfrom);
}

function bagaddcmp(urladd, urlfrom, id)
{
  IFRBAG.location.href = urladd + '&qty=' + document.getElementById(id).value + '&urlfrom=' + escape(urlfrom);
}


function showtext(wtext, wparameters, wbgcolor, wstyle, title)
{
  wintext=window.open("","wintext",wparameters);
  wintext.document.open();
  wintext.document.clear();
  wintext.document.bgColor=wbgcolor;
  wintext.document.write('<HTML><HEAD><TITLE>'+title+'</TITLE></HEAD><BODY>');
  wintext.document.write('<TABLE style="'+wstyle+'"><TR><TD>'+wtext+'</TABLE>');
  wintext.document.write('</BODY></HTML>');
  wintext.document.close();
  wintext.focus();
}


function showcomplist(url, iscompset, isprod, sessid, pgidfrom, prodgoodsid, propord, propordbs, goodsid, qty, qtymin, qtymax, propname, ord, posnopr, gdsnopr, formname)
{
  qtynew = document.forms[formname].p_qtypos.value;
  if (qtynew!=qty && goodsid!=-1) 
  {
    if (save_posqty(qtynew, ord, formname, propname, qtymin, qtymax)) document.forms[formname].submit();
  }

  if (qtynew==qty || goodsid==-1)
  {
  wurl = url+"&iscompset="+iscompset+"&goodsid="+prodgoodsid+"&propord="+propord+"&propordbs="+propordbs
            +"&sessid="+sessid+"&pgidfrom="+pgidfrom
            +"&cmpidold="+goodsid+"&qty="+qtynew+"&qtymin="+qtymin+"&qtymax="+qtymax;
  if (posnopr.length > 0) wurl = wurl+ "&posnopr="+posnopr;
  if (gdsnopr.length > 0) wurl = wurl+ "&gdsnopr="+gdsnopr;
  if (isprod=="y") wurl = wurl + "&scrolltop="+gbst();
  wcmplist = window.open(wurl+"&x="+Math.round(Math.random()*10000)+"&y="+Math.round(Math.random()*10000), "wcmplist"+propord, 
         'width=900,height=600,resizable=yes,titlebar=no,scrollbars=yes,status=no,toolbar=no,'
        +'left=20,top=20');
  wcmplist.focus(); 
  }
}


function gdscomplist(sessid, iscompset, propord, goodsid, qty, qtymin, qtymax, keytop, keylist)
// не используется, раньше вызывалась из ptlgoodsp.htf_complist
{
  strcompsetid = "compsetid";
  strgoodsid = "goodsid";

  url = top.opener.location.href;
  url = url.replace(keytop, keylist);
  url = url.replace(strcompsetid, strgoodsid);
  if (url.indexOf("#")>=0) url = url.substring(0,url.indexOf("#"));
  if (url.indexOf("seed=")>=0) url = url.substring(0,url.indexOf("seed=")-1);

  url = url+ "&iscompset="+iscompset+ "&propord="+propord+ "&sessid="+sessid+ "&cmpidold="+goodsid+ "&qty="+qty+ "&qtymin="+qtymin+ "&qtymax="+qtymax;
  window.location = url;
}

function delcomp(formname)
{
  document.forms[formname].p_qtypos.value = 0;
  document.forms[formname].submit();
}


function addtoprod(iscompset, sessid, pgidfrom, prodgoodsid, url, urlfrom, ord, ordbs, gdsidold, gdsid, qty, qmin, qmax, scrltop)
{
  formname = 'addcmp'+gdsid;
  if (qty <=0) 
  { 
    alert("Количество может быть только положительным числом");  
    document.forms[formname].qty.focus(); 
    return;
  }
  if (qty < qmin)
  {
    alert("Количество комплектующей для позиции не может быть меньше "+qmin);  
    document.forms[formname].qty.value = qmin; 
    document.forms[formname].qty.focus(); 
    return;
  }
  if (qty > qmax && qmax>0 )   
  {
    alert("Количество комплектующей для позиции не может быть больше "+qmax);  
    document.forms[formname].qty.value = qmax; 
    document.forms[formname].qty.focus(); 
    return;
  }

  IFRCOMP.location.href = url + '?iscompset='+iscompset+ '&sessid='+sessid + '&pgidfrom='+pgidfrom +
                                '&prodgoodsid='+prodgoodsid +'&qty='+qty +'&cmpid='+gdsid +
                                '&propord_base='+ordbs +'&propord='+ord +
                                '&goodsid='+gdsidold +'&urlfrom='+escape(urlfrom)+"&scrolltop="+scrltop;
}


function save_posqty(qty, ord, formname, name, qmin, qmax) 
{
  if (qty >=0 && qmin==0 && qmax==0 ) return true;  
  if (qty < qmin)
  {
    if (qmin==1)  alert("Позиция  "+ord+". "+name+"  не может быть удалена из изделия");  
    else  alert("Для позиции  "+ord+". "+name+"  количество не может быть меньше "+qmin);  
    document.forms[formname].p_qtypos.value = qmin; return false;
  }
  if (qty > qmax && qmax>0 )   
  {
    alert("Для позиции  "+ord+". "+name+"  количество не может быть больше "+qmax);  
    document.forms[formname].p_qtypos.value = qmax; return false;
  }
  if (qty >= qmin && qty <= qmax && qmax>0 ) return true;  
  document.forms[formname].p_qtypos.focus(); 
}

function change_posqty(qty, ord, formname, name, qmin, qmax) 
{
  if (qty < qmin)
  {
    if (qmin==1)  alert("Позиция  "+ord+". "+name+"  не может быть удалена из изделия");  
    else  alert("Для позиции  "+ord+". "+name+"  количество не может быть меньше "+qmin);  
    document.forms[formname].p_qtypos.value = qmin; 
  }
  if (qty > qmax && qmax>0 )   
  {
    alert("Для позиции  "+ord+". "+name+"  количество не может быть больше "+qmax);  
    document.forms[formname].p_qtypos.value = qmax; 
  }
  if (qty >=0 && qmin==0 && qmax==0 ) document.forms[formname].submit();
  else if (qty >= qmin && qty <= qmax && qmax>0 ) document.forms[formname].submit();
}


function mform(formname,ord)
{
  cuform = formname;
  cuformord = ord;
  cuqtyvalue = document.forms[cuform].p_qtypos.value;
}


function cmprecalc(urltobag)
{
  isbscnf = 0;
  if (cuformord > 0)
    { document.prodsave.p_qty_last.value = document.forms[cuform].p_qtypos.value;
      document.prodsave.p_ord_last.value = cuformord;
      if (document.forms[cuform].p_qtypos.value == cuqtyvalue) isbscnf = 1;
    }
  else isbscnf = 1;

  if (isbscnf == 0) document.prodsave.submit();
  else  IFRBAG.location.href = urltobag + '&urlfrom=' + escape(urlbagfrom);
}

function prodrecalc(urlgo)
{
  if (cuformord > 0)
  { document.prodsave.p_qty_last.value = document.forms[cuform].p_qtypos.value;
    document.prodsave.p_ord_last.value = cuformord;
  }
  document.prodsave.submit();

  if (urlgo!="")  window.open(urlgo);
}


function compposins(url) 
{
 window.close();
 top.opener.location = url+"&cmpidold=-1"+"&seed="+getRandom()+"#newpos";
}


function showimg(url,wd,ht,wnm)
{
  winimg = window.open("", wnm, 'width='+wd+',height='+ht+
             ',resizable=no,titlebar=no,scrollbars=no,status=no,toolbar=no'+
             ',left='+(screen.width-wd)/2+',top='+(screen.height-ht)/2);
  winimg.document.open();
  winimg.document.clear();
  winimg.document.write('<HTML><HEAD><TITLE>'+url+'</TITLE></HEAD>'+
           '<BODY leftMargin=0 rightMargin=0 topMargin=0 marginheight=0 marginwidth=0>'+
           '<IMG SRC="'+url+'" border=0></BODY></HTML>');
  winimg.document.close();
  winimg.focus();
}


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];}}
}

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_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_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 check_flash() 
{
  plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
  if ((!plugin) && (navigator.userAgent && navigator.userAgent.indexOf("MSIE") >= 0))
  {
    document.write('<SCRIPT LANGUAGE=VBScript\> \n');
    document.write('on error resume next \n');
    document.write('plugin=( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash")))\n');
    document.write('</SCRIPT\> \n');
  }
}

function rflash(textfl, textimg) 
{
  if (textimg) tximg = textimg; else tximg = textfl;
  if (plugin) document.write(textfl);
  else 
    if (!(navigator.appName && navigator.appName.indexOf("Netscape")>=0 && navigator.appVersion.indexOf("2.")>=0)) document.write(tximg);
}


function tvsort(srttp)
{
  rnd = getRandom();

  url = document.ptlrowdisp.urlsort.value;
  if (url.indexOf("seed=")>=0) url = url.substring(0,url.indexOf("seed=")-1);
  url = url + "&srt="+srttp;
  url = url + "&seed="+String(rnd);
  url = url.toLowerCase()
  window.location = url;
}

function fixpng(element)
{
 if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
 {
   var src;
   if (element.tagName=='IMG')
   {
     if (/\.png$/.test(element.src))
     {
       src = element.src;
       element.src = "/files/ptl/css/blank.gif";
     }
   }
   if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
  }
}


function tron(id1,id2,id3,id4,id5)
{
  document.all(id1).style.background = "#E6F3F9";
  if (id2 && document.getElementById(id2)) document.all(id2).style.background = "#E6F3F9";
  if (id3 && document.getElementById(id3)) document.all(id3).style.background = "#E6F3F9";
  if (id4 && document.getElementById(id4)) document.all(id4).style.background = "#E6F3F9";
  if (id5 && document.getElementById(id5)) document.all(id5).style.background = "#E6F3F9";
}

function trof(id1,id2,id3,id4,id5)
{
  document.all(id1).style.background = "#FFFFFF";
  if (id2 && document.getElementById(id2)) document.all(id2).style.background = "#FFFFFF";
  if (id3 && document.getElementById(id3)) document.all(id3).style.background = "#FFFFFF";
  if (id4 && document.getElementById(id4)) document.all(id4).style.background = "#FFFFFF";
  if (id5 && document.getElementById(id5)) document.all(id5).style.background = "#FFFFFF";
}

function trofbg(bgc,id1,id2,id3,id4,id5)
{
  document.all(id1).style.background = bgc;
  if (id2 && document.getElementById(id2)) document.all(id2).style.background = bgc;
  if (id3 && document.getElementById(id3)) document.all(id3).style.background = bgc;
  if (id4 && document.getElementById(id4)) document.all(id4).style.background = bgc;
  if (id5 && document.getElementById(id5)) document.all(id5).style.background = bgc;
}

function elon(el)
{
  el.style.background = "#E6F3F9";
}

function elof(el)
{
  el.style.background = "#FFFFFF";
}


function winfrmscroll()
{
  if (document.getElementById("ptlrvwsaveimg")) document.getElementById("ptlrvwsaveimg").scrollIntoView(false);
  if (document.getElementById("ptlmsgsaveimg")) document.getElementById("ptlmsgsaveimg").scrollIntoView(false);
}


function winwait(elmtp, obj)
{
  if (document.getElementById("bodytab")) document.getElementById("bodytab").style.cursor = "wait";
  if (elmtp=="img") 
  { obj.className = "imgwait"; 
    obj.disabled = true; 
  }
}

function gbst()
{
  return self.pageYOffset || 
    (document.documentElement && document.documentElement.scrollTop) || 
    (document.body && document.body.scrollTop);
}

function nvgurl(url, list)
{
 return url + '&nvg=' + list + '&wy=' + gbst();
}

