var Functions='opener.parent.contents.myOrderList';
      Header='  <table border="0" height="5" width="100">                                          ' +
             '   <tr><td></td></tr>                                                                ' +
             '  </table>                                                                           ' +
             '  <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>                          ' +
             '   <TR>                                                                              ' +
             '    <TD class=infoBoxHeading height=14>                                              ' +
             '     <IMG height=14 alt="" src="corner_left.bmp" width=11 border=0>                  ' +
             '    </TD>                                                                            ' +
             '    <TD class=infoBoxHeading width="100%" align="center" height=14>Съдържание на количката Ви</TD>  ' +
             '    <TD class=infoBoxHeading height=14>                                              ' +
             '     <IMG height=14 alt="" src="corner_right_left.bmp" width=11 border=0>            ' +
             '    </TD>                                                                            ' +
             '   </TR>                                                                             ' +
             '  </TABLE>                                                                           ' +
             '  <table border="0" height="1" cellSpacing=0 cellPadding=0 width="100%" bgcolor="#B6B7CB">      ' +
             '   <tr><td height="1"> </td></tr> 							      ' +
             '  </table>										      ' +
             '  <table cellSpacing=0 cellPadding=0 border=0 bgcolor="#BBC3D3" width="100%" height="4" >       ' +
             '   <TR>                                                   				      ' +
             '    <TD width="13%" height=4></TD>				                              ' +
             '    <TD width="62%" height=4><font size="2" color="#FFFFFF"><b>&nbsp;Стока</b></font></TD>      ' +
             '    <TD width="5%"  height=4 nowrap><font size="2" color="#FFFFFF"><b>К-во</b></font></TD>      ' +
             '    <TD width="20%" height=4 align=center><font size="2" color="#FFFFFF"><b>Цена</b></font></TD>' +
             '   </TR>       										      ' +
             '  </table>    										      ' ;
      Footer='  <table border="0" cellspacing="0" cellpadding="0" bgcolor="#F9FEFF" bordercolorlight="#B6B7CB" bordercolordark="#98B2CC" width="100%" bordercolor="#EFF3F6" style="border-left-width:0; border-top-width:0; border-bottom-width:0">' +
             '   <TR>' +
             '    <td id="LeftBtn" onMouseOver="Focus(this,true)" onMouseOut="Focus(this,false)" onClick="'+Functions+'.removeChecked()" height="20" width="50%" align="center" bgcolor="#EFF3F6" style="border-left-style:solid; border-left-width:1; border-right-style:solid; border-right-width:0; border-top-style:none; border-top-width:medium; border-bottom-style:solid; border-bottom-width:1" bordercolor="#BBC3D3" bordercolorlight="#B6B7CB" bordercolordark="#B6B7CB" >' +
             '      <font size="2" color="#FF0000">' +
             '       Изтрий маркираните стоки' +
             '      </font>' +
             '    </td>' +
             '    <td id="RightBtn" onMouseOver="Focus(this,true)" onMouseOut="Focus(this,false)" OnClick="'+Functions+'.checkForm();" height="20" width="50%" align="center" bgcolor="#EFF3F6" style="border-left-style:solid; border-left-width:1; border-right-style:solid; border-right-width:1; border-top-style:none; border-top-width:medium; border-bottom-style:solid; border-bottom-width:1" bordercolor="#BBC3D3" bordercolorlight="#B6B7CB" bordercolordark="#B6B7CB" >' +
             '      <font size="2" color="#555555">' +
             '        Потвърди поръчката' +
             '      </font>' +
             '    </td>' +
             '   </tr>' +
             '  </table>' +
             '  <table border="0" height="5" width="100">' +
             '   <tr><td></td></tr>' +
             '  </table>' ;
var Seller  = 'satelit@mbox.contact.bg';
try { var Buyer=parent.document.all['customer'].src; }
catch (e) { var Buyer= ''; }
finally { }
var Valuta = '';
function InitArray() {
  this.count = 0;
  return(this);
}
function Add(STOKA,PRICE,LOCATION,KOLICH) {
var Exist=false;
  for(i=1;i<=myOrderList.items.count;i++){
    if(myOrderList.items[i].Location==LOCATION){
      myOrderList.items[i].Quantity++;
      Exist=true;
      break;
    }
  }
  if(!Exist){
    myOrderList.items.count++;
    myOrderList.items[myOrderList.items.count] = new item(STOKA, PRICE, LOCATION, KOLICH);
  }
  ShowOrder();
}
function UpdateQty(NUM,QTY){
  myOrderList.items[NUM].Quantity=Number(QTY);
  win.RightBtn.innerHTML='<font size="2" color="#555555">' +
                         ' <b> Преизчисли поръчката </b>' +
                         '</font>' ;
  win.RightBtn.onclick=ShowOrder;
}
function item(STOKA, PRICE, LOCATION, KOLICH) {
  this.Stoka    = STOKA;
  this.Price    = PRICE;
  this.Location = LOCATION;
  this.Quantity = 1;
  this.InStock  = ExtractReal(KOLICH);
  this.Checked  = false;
  return(this);
}
function orderList() {          // Export functions
  this.items         = new InitArray();
  this.addItem       = Add;
  this.updateQty     = UpdateQty;
  this.checkItem     = ToggleCheck;
  this.removeChecked = RemoveChecked;
  this.isChecked     = IsChecked;
  this.checkAll      = CheckAll;
  this.clear         = ClearArray;
  this.printOrder    = PrintOrder;
  this.showOrder     = ShowOrder;
  this.checkForm     = CheckForm;
}
function Remove(ID){
  for(i=ID;i<=myOrderList.items.count;i++){
    myOrderList.items[i]=myOrderList.items[i+1];
  }
  myOrderList.items.count--;
}
function RemoveChecked(){
var i=myOrderList.items.count;
  while(i>=1){
    if(IsChecked(i)) Remove(i);
    i--;
  }
  ShowOrder();
}
function IsChecked(NUM){
  return myOrderList.items[NUM].Checked;
}
function ToggleCheck(OBJ){
  ID=OBJ.id.substr(2,OBJ.id.length);
  Chk='ch'+ID;  ChkObj=win.document.all[Chk];
  ChkObj.checked=!ChkObj.checked;
  myOrderList.items[ID].Checked=ChkObj.checked;
}
function CheckAll(){
  Chk='chALL';  ChkALLObj=win.document.all[Chk];
  for(i=1;i<=myOrderList.items.count;i++){
    Chk='ch'+i;  ChkObj=win.document.all[Chk];
    myOrderList.items[i].Checked=ChkALLObj.checked;
    ChkObj.checked=ChkALLObj.checked;
  }
}
function MakeHTMLRow(NUM){
  Checked='';
  if(myOrderList.items[NUM].Checked) Checked='checked';
  CheckBox='<input onClick="checked=!checked" id="ch'+NUM+'" type="checkbox" '+Checked+'>';
  result=
    ' <table id="tr'+NUM+'" OnClick="'+Functions+'.checkItem(this)" cellSpacing=0 cellPadding=0 border=1 width=100% bgcolor=#EFF3F6 style="border-collapse: collapse; border-left-width: 0; border-right-width: 0; border-top-width: 0" bordercolor="#BBC3D3">'+
    '  <TR onMouseOver="Focus(this,true)" onMouseOut="Focus(this,false)">'+
    '   <TD width=13% align="left"  style="border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1">'+CheckBox+'<img border="0" src="stoka.bmp" width="16" height="16" align="absmiddle" hspace="0"></img></TD>'+
    '   <TD width=62% align="left"  style="border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1"><font size="2" color="#555555">'+myOrderList.items[NUM].Stoka+'</TD>'+
    '   <TD width=5%  align="right" style="border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1"><font size="2" color="#555555"><input onKeyUp="'+Functions+'.updateQty('+NUM+',this.value)" type="text" value="'+myOrderList.items[NUM].Quantity+'" size=1 maxLength=4></TD>'+
    '   <TD width=20% id="pr'+NUM+'" align="right" style="border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1"><font size="2" color="#555555">'+Trunc(ExtractPrice(myOrderList.items[NUM].Price)*myOrderList.items[NUM].Quantity)+' '+Valuta+'</TD>'+
    '  </TR>'+
    ' </table>';
  return result;
}
function Trunc(REAL){
var result=REAL;
  REAL=REAL+'';
  if(REAL.indexOf('.')>-1){
    result=REAL.substring(0,(REAL.indexOf('.')+3));
  };
  return result;
}
function MakeTOTALRow(PRICE){
  CheckBox='<input id="chALL" onClick="'+Functions+'.checkAll()" type="checkbox">';
  result=
    ' <table cellSpacing=0 cellPadding=0 border=1 width=100% bgcolor=#F9FEFF style="border-collapse: collapse; border-left-width: 0; border-right-width: 0; border-top-width: 0" bordercolor="#BBC3D3">'+
    '  <TR>'+
    '   <TD width=13% align="left"  style="border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1; cursor:hand" onClick="chALL.click()">'+CheckBox+'<font face="Arial" size="2" color="#555555">всички</font></TD>'+
    '   <TD width=67% align="right" style="border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1" ><font size="2" color="#555555">Общо:</font></TD>'+
    '   <TD width=20% align="right" style="border-left-style: none; border-left-width: medium; border-right-style: none; border-right-width: medium; border-top-style: none; border-top-width: medium; border-bottom-style: solid; border-bottom-width: 1"><font size="2" color="#555555">&nbsp;<b>'+Trunc(PRICE)+''+Valuta+'</b></font></TD>'+
    '  </TR>'+
    ' </table>';
  return result;
}
function AddKolich(NUM){
var tmp='';
  tmp=myOrderList.items[NUM].Location;
  tmp=tmp.substr(0,tmp.length-1);
  return tmp+'»'+myOrderList.items[NUM].Quantity+']';
}
function PrintOrder(){
  Order='';
  for(i=1;i<=myOrderList.items.count;i++){
    Order=Order+AddKolich(i,myOrderList.items[i].Location,myOrderList.items[i].Quantity)+'\r\n';
  }
  return '{'+Buyer+'}\r\n'+Order;
}
function isNumber(CHAR){
  for(k=0;k<=9;k++){
    if(CHAR==k&&CHAR!=' '){
      return true
    }
  }
}
function ExtractReal(REAL){
  var result='';
  var i=0;
  var n='';
  for(i=0;i<=REAL.length;i++){
    n=REAL.charAt(i);
    if(isNumber(n)||n=='.') {
      if(result.indexOf('.')>=0&&n=='.') break;
      result=result+n;
    }
  }
  return Number(result);
}
function ExtractPrice(PRICE){
  Valuta=''
  var result='';
  var i=0;
  var n='';
  for(i=0;i<=PRICE.length;i++){
    n=PRICE.charAt(i);
    if(isNumber(n)||n=='.') {
      if(result.indexOf('.')>=0&&n=='.') break;
      result=result+n;
    }else{
      Valuta=Valuta+n
    }
  }
  return Number(result);
}
function CheckForm(){
var QtyTotal=0;
var OutOfStock='';
var msg1='За следните стоки няма достатъчна наличност:\r';
var msg2='\rЗабележка:\r   Стоките извън наличност могат да се доставят със закъснение!\r'+
         '\r           Желаете ли все пак да изпратите поръчката?';
        
  for(i=1;i<=myOrderList.items.count;i++){
    QtyTotal=QtyTotal+myOrderList.items[i].Quantity;
    if(myOrderList.items[i].Quantity>myOrderList.items[i].InStock){
      OutOfStock=OutOfStock+'   "'+myOrderList.items[i].Stoka+'" - поръчани: '+myOrderList.items[i].Quantity+' в наличност: '+myOrderList.items[i].InStock+'\r';
    };
  }
  if(QtyTotal<=0){
    win.alert('Сборът на количеството на всички стоки трябва да е по-голямо от 0!');
    win.focus();
    return;
  };
  if(OutOfStock.length>0){
    if(!win.confirm(msg1+OutOfStock+msg2)){
      return
    };
    win.focus();
  };
  win.PostForm.submit();
}
function MakeForm(SELLER,BUYER,MESSAGE){
  result='<form name="PostForm" method="post" action="mail.php">'+
           '<input type="hidden" name="Seller"  value="'+SELLER +'">'+
           '<input type="hidden" name="Buyer"   value="'+BUYER  +'">'+
           '<input type="hidden" name="Message" value="'+MESSAGE+'">'+
         '</form>';
  return result;
}
function ShowOrder(){
  var i,TotalSum=0;
  var Order=Header;
  for(i=1;i<=myOrderList.items.count;i++){
    Order=Order+MakeHTMLRow(i);
    TotalSum=TotalSum+Number(Trunc(ExtractPrice(myOrderList.items[i].Price)*myOrderList.items[i].Quantity));
  };
  if(myOrderList.items.count>0){
    Order=Order+MakeTOTALRow(TotalSum)+Footer;
    Order=Order+MakeForm(Seller,Buyer,PrintOrder());
  }else{
    win.close();
    return;
  }
  try{win.document}
  catch(e){win=null}
  finally{};
  if(win==null){
    win=window.open('', 'Win2k', 'height=500,width=400,top=100,left=100,scrollbars=yes');
  }
  win.document.open();
  win.document.write('<html>'+
                     ' <head>'+
                     '  <title>Количка</title>');
  win.document.write('  <script language="JavaScript">'+
                     '   function Focus(Cell,Over){'+
                     '     Cell.bgColor="#EFF3F6";'+
                     '     if(Over){Cell.bgColor="#F9FEFF"};'+
                     '     Cell.style.cursor="hand"'+
                     '   }'+
                     '  </script>'+
                     '  <link rel="stylesheet" href="stylesheet.css" type="text/css">'+
                     '  <META http-equiv="Content-Type" content="text/html; charset=windows-1251">'+
                     ' </head>'+
                     ' <body topMargin="4" bgcolor="#F9FEFF" OnSelectStart="return false">'+
                     Order+
                     ' </body>'+
                     '</html>');
  win.focus();
}
function ClearArray() {
  this.items.count = 0;
}
var win;
var myOrderList = new orderList();
