//////////////////////////////////////////////////////////////////////////////
// FILE:    bud.js
// PURPOSE: Budget functions
//////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////
// INTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////////////////////
function I2(e, s) // Translator english / spanish
  { if(PdbLan=="english") { return e; } else { return s; } }


//////////////////////////////////////////////////////////////////////////////
// CONTROL
//////////////////////////////////////////////////////////////////////////////
var PdbArr = new Array; // Works as array (access by numbers)
var PdbLst = new Array; // Works as asociative list (access by strings)

var PdbImgDef = -1; // Remembers the last object with image and with price
var PdbPriDef = -1; // for use as default when an invalid picture is found.

var PdbLan = "english"; // Language english / spanish
var PdbFrm = "image";   // Forms image / budget

//////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
   function PdbObj(picNum, // Picture number (text)
                   picTit, // Picture title (text)
                   picDte, // Picture end date (text)
                   picJpg, // Picture image (url)
                   picTec, // Picture technique (text)
                   priBas) // Price base (real)

// PURPOSE: Create a Picture database object.
//////////////////////////////////////////////////////////////////////////////
{
  this.picNum = picNum; // Picture number (text)
  this.picTit = picTit; // Picture title (text)
  this.picDte = picDte; // Picture end date plus serial number (text)
  this.picJpg = picJpg; // Picture image (url)
  this.picTec = picTec; // Paper technique, paper+background+foreground (text)
  this.priBas = priBas; // Price base (real)
}


//////////////////////////////////////////////////////////////////////////////
   function PdbPut(picNum, // Picture number (text)
                   picTit, // Picture title (text)
                   picDte, // Picture end date (text)
                   picJpg, // Picture image (url)
                   picTec, // Picture technique (text)
                   priBas) // Price base (real)

// PURPOSE: Create a Picture database object.
//////////////////////////////////////////////////////////////////////////////
{
  var picPos = PdbArr.length; // Position for this new entry
  PdbArr[picPos] = picNum; 
  PdbLst[picNum] = new PdbObj(picNum,picTit,picDte,picJpg,picTec,priBas);
  
  if(picJpg.indexOf(".jpg") > -1) { PdbImgDef=picPos; } // There is an image
  if(priBas > 0)                  { PdbPriDef=picPos; } // There is a price
}


//////////////////////////////////////////////////////////////////////////////
   function PdbChkPicNum(picTst) // picNum to test

// PURPOSE: Returns a valid picNum, if the input picNum is not valid then
//          returns a valid default one.
//////////////////////////////////////////////////////////////////////////////
{
  var picNum = "";
  if(PdbLst[picTst]) { picNum = picTst;           } // If is not undefined
  else               { return(PdbArr[PdbPriDef]); } // If is undefined
  var pdbObj = PdbLst[picNum];

  var priOly = false; // Assume all pictures
  if(PdbFrm=="bdg") { priOly = true; } // Only pictures with price

  if((! priOly && pdbObj.picJpg.indexOf(".jpg") > -1) || // images and there is an image
     (  priOly && pdbObj.priBas > 0))                    // prices and there is a price
                     { return(picNum); }
  else               { return(PdbArr[PdbPriDef]); }      // with image and price always
}


//////////////////////////////////////////////////////////////////////////////
   function PdbValidUntil(numDay)

// PURPOSE: Returns a text date for valid until yyyy/mm/dd.
//          The day is Today plus numDay days.
//////////////////////////////////////////////////////////////////////////////
{
  var nowDte = new Date();
  var untDte = new Date(nowDte.getTime() +(numDay * 86400000));
  
  var untTxt = "since "+DteAnsiFormat(nowDte)+" to "+DteAnsiFormat(untDte);
  
  return(untTxt);
}


//////////////////////////////////////////////////////////////////////////////
   function PdbUpdate(frmObj, // Form object
                      imgUpd) // If true then update image jpeg

// PURPOSE: Update a form with a picture information, the information about
//          prices only when is a budget.
//////////////////////////////////////////////////////////////////////////////
{
  var picNum = frmObj.picNum.value;
  var pdbObj = PdbLst[picNum];

  frmObj.picDte.value = pdbObj.picDte;
  frmObj.picTec.value = pdbObj.picTec;

  if(PdbFrm=="bdg")
  {
    BoCSelectChange(frmObj); // Change the price
    frmObj.valDte.value = PdbValidUntil(90);
  }                      
  if(imgUpd) { PicRndUpdate(picNum); }
}


//////////////////////////////////////////////////////////////////////////////
// FIELDS
//////////////////////////////////////////////////////////////////////////////
var PdbRowIni = "<tr valign=middle><td bgcolor='#606060' width=65><h2>";
var PdbRowMid = "</h2></td><td bgcolor='#606060' align=right>";
var PdbRowEnd = "</td></tr>";


//////////////////////////////////////////////////////////////////////////////
   function PdbFrmHeader()

// PURPOSE: Write the form header with links to all forms
//////////////////////////////////////////////////////////////////////////////
{
  // bdg, img, lnk, msg
  
  var msgTit = I2("feedback","mensajes");
  var imgTit = I2("images for free","imágenes gratis");
  var bdgTit = I2("budgets","precios");
  var lnkTit = I2("art links","enlaces de arte");

  var msgLnk = "<a href='inffeebck.htm'>";
  var imgLnk = "<a href='inffeeimg.htm'>";
  var bdgLnk = "<a href='inffeebdg.htm'>";
  var lnkLnk = "<a href='inffeelnk.htm'>";

  var mnuSep = " | "; // "&nbsp;&nbsp;|&nbsp;&nbsp;";
  var mnuBld = ""; // "<b>";
  var mnuDlb = ""; // "</b>";
  var mnuHtm = ""
  switch(PdbFrm)
  {
    case "msg":
      mnuHtm = mnuBld+msgTit+mnuDlb+mnuSep+
               imgLnk+imgTit+"</a>"+mnuSep+
               bdgLnk+bdgTit+"</a>"+mnuSep+
               lnkLnk+lnkTit+"</a>";
    break;

    case "img":
      mnuHtm = msgLnk+msgTit+"</a>"+mnuSep+
               mnuBld+imgTit+mnuDlb+mnuSep+
               bdgLnk+bdgTit+"</a>"+mnuSep+
               lnkLnk+lnkTit+"</a>";
    break;

    case "bdg":
      mnuHtm = msgLnk+msgTit+"</a>"+mnuSep+
               imgLnk+imgTit+"</a>"+mnuSep+
               mnuBld+bdgTit+mnuDlb+mnuSep+
               lnkLnk+lnkTit+"</a>";
    break;

    case "lnk":
      mnuHtm = msgLnk+msgTit+"</a>"+mnuSep+
               imgLnk+imgTit+"</a>"+mnuSep+
               bdgLnk+bdgTit+"</a>"+mnuSep+
               mnuBld+lnkTit+mnuDlb;
    break;
  }
  heaHtm = PdbRowIni + "" + PdbRowMid + mnuHtm + PdbRowEnd;
  document.write(TxtReplace(heaHtm,"#606060","#e0e0e0"));
}


//////////////////////////////////////////////////////////////////////////////
   function PdbFldHidden(valTxt, // Init value
                         inpNam) // Input name

// PURPOSE: Write an initial control file
//////////////////////////////////////////////////////////////////////////////
{
  document.write(
    "<input type='hidden' name='"+inpNam+"' "+"value='"+valTxt+"'>");
}


//////////////////////////////////////////////////////////////////////////////
   function PdbFldInput(lblEnd, // English label
                        lblSpa, // Spanish label
                        inpNam, // Input name
                        inpCla) // Input class

// PURPOSE: Write a simple input field
//////////////////////////////////////////////////////////////////////////////
{
  document.write(
    PdbRowIni+I2(lblEnd,lblSpa)+PdbRowMid+
    "<input class='"+inpCla+"' type='text' name='"+inpNam+"' maxlength=60>"+
    PdbRowEnd);
}


//////////////////////////////////////////////////////////////////////////////
   function PdbFldArea(lblEnd, // English label
                       lblSpa, // Spanish label
                       inpNam, // Input name
                       inpCla, // Input class
                       numRow) // Rows

// PURPOSE: Write a text area
//////////////////////////////////////////////////////////////////////////////
{
  document.write(
    PdbRowIni+I2(lblEnd,lblSpa)+PdbRowMid+
    "<textarea class='"+inpCla+"' name='"+inpNam+"' cols=37 rows="+numRow+
    " maxlength=512></textarea>"+
    PdbRowEnd);
}


//////////////////////////////////////////////////////////////////////////////
   function PdbFldSubmit(lblEnd, // English label
                         lblSpa, // Spanish label
                         butEnd, // English button
                         butSpa, // Spanish button
                         heiPix) // Precision height = numRow + heiPix

// PURPOSE: Write a simple submit button
//////////////////////////////////////////////////////////////////////////////
{
  document.write(
    PdbRowIni+I2(lblEnd,lblSpa)+PdbRowMid+
    "<div id='BoCTrcDiv'></div>"+
    "<img src='../../common/gif/block.gif' width=1 height="+heiPix+">"+
    "<input class='button' type='submit' name='btnSbm' value='"+
    I2(butEnd,butSpa)+"'>"+
    PdbRowEnd);
}

//////////////////////////////////////////////////////////////////////////////
   function PdbFldPictures(lblEnd, // English label
                           lblSpa, // Spanish label
                           priOly, // If true paintings with price
                           picSel) // The selected picture number (text)

// PURPOSE: Write select option for all pictures in the database
//////////////////////////////////////////////////////////////////////////////
{
  document.write(
    PdbRowIni+I2(lblEnd,lblSpa)+PdbRowMid+
    "<select name='picNum' size=1 "+
    "onChange='PdbUpdate(document.FrmFeeBck,true);'>");

  var selTxt = " ";
  for(var arrPos=0; arrPos<PdbArr.length; arrPos++)
  {
    var picNum = PdbArr[arrPos];
    var pdbObj = PdbLst[picNum];
    if((! priOly && pdbObj.picJpg.indexOf(".jpg") > -1) || // images and there is an image
       (  priOly && pdbObj.priBas > 0))                    // prices and there is a price
    {
      var picTit = pdbObj.picTit;
      if(picNum!=picSel) { selTxt = " "; }
      else               { selTxt = " selected "; }
      var optTxt = "<option"+selTxt+"value='"+picNum+"'>"+
                    picNum+" | "+picTit+"</option>";
      document.write(optTxt);
    }
  }
  document.write("</select>"+PdbRowEnd);
}


//////////////////////////////////////////////////////////////////////////////
   function PdbFldSize(lblEnd, // English label
                       lblSpa) // Spanish label

// PURPOSE: Write select option for sizes
//////////////////////////////////////////////////////////////////////////////
{
  var fstTxt = I2("select A4 or A3", "seleccionar A4 o A3");
  document.write(
    PdbRowIni+I2(lblEnd,lblSpa)+PdbRowMid+
    "<select name='picSiz' size=1>"+
    "<option value='no'>"+fstTxt+"</option>"+
    "<option value='A4'>Din A4, Jpeg, 300 dpi</option>"+
    "<option value='A3'>Din A3, Jpeg, 300 dpi</option>"+
    "</select>"+PdbRowEnd);
}


//////////////////////////////////////////////////////////////////////////////
   function PdbFldCurrency(lblEnd, // English label
                           lblSpa) // Spanish label

// PURPOSE: Write select option for currencies
//////////////////////////////////////////////////////////////////////////////
{
  document.write(PdbRowIni+I2(lblEnd,lblSpa)+PdbRowMid);
  BoCSelectWrite("FrmFeeBck","EUR");
  document.write(PdbRowEnd);
}



