//////////////////////////////////////////////////////////////////////////////
// FILE:    cmm.js
// PURPOSE: Common page functions
//////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////
// CONTROL
//////////////////////////////////////////////////////////////////////////////
var urlInk = ""; // Accesos absolutos que no siguen la mecanica local


//////////////////////////////////////////////////////////////////////////////
// DATE FUNCTIONS
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
   function DteAnsiFormat(dte)

// PURPOSE: Returns a text in ansi forma yyyy/mm/dd.
//////////////////////////////////////////////////////////////////////////////
{
  var day = dte.getDate();
  var mth = dte.getMonth()+1;
  var yea = dte.getYear()+1900;
  
  var txt = yea+" / "+mth+" / "+day;
  
  return(txt);
}


//////////////////////////////////////////////////////////////////////////////
// DATA FUNCTIONS
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
   function DatRound(datNum, // Number
                     decPos) // Number of decimal places
   
// PURPOSE: Returns a number rounded to a decPos number of decimal places.
//////////////////////////////////////////////////////////////////////////////
{ return(Math.round(datNum*Math.pow(10,decPos))/Math.pow(10,decPos)); }


//////////////////////////////////////////////////////////////////////////////
// TEXT FUNCTIONS
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
   function TxtReplace(inpTxt, oldTxt, newTxt)
   
// PURPOSE: Returns a text with oldTxt change by newTxt. Does not use
//          regular expressions.
//////////////////////////////////////////////////////////////////////////////
{
  var tmpArr = inpTxt.split(oldTxt);
  return(tmpArr.join(newTxt));
}


//////////////////////////////////////////////////////////////////////////////
   function TxtBetween2Tag(inpTxt, // Text
                           tagIni, // Initial tag
                           tagEnd) // End tag

// PURPOSE: The simplest TxtBetween2Tag(), no case sensitive, mandatory and
//          no compact. .substring(i,e) stops at position e and does not
//          include the character at position e.
//////////////////////////////////////////////////////////////////////////////
{
  var txtBet = "";
  var posIni = inpTxt.indexOf(tagIni);
  if(posIni >= 0) // If tagIni found
  {
    var lenIni = tagIni.length;
    var posSub = posIni + lenIni;
    var posEnd = inpTxt.indexOf(tagEnd, posSub);
    if(posEnd >= posSub) { txtBet = inpTxt.substring(posSub,posEnd); }
  }  
  return(txtBet);
};


//////////////////////////////////////////////////////////////////////////////
// HTML FUNCTIONS
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
   function HtmInjectCode(id,     // Elemente identification
                          htmCod) // Html code
   
// PURPOSE: Returns true if can inject the code htmCod in the element id.
//////////////////////////////////////////////////////////////////////////////
{ 
  var htmEle = document.getElementById(id);
  if(!htmEle) {                            return(false); }
  else        { htmEle.innerHTML = htmCod; return(true);  }
}


//////////////////////////////////////////////////////////////////////////////
// XML FUNCTIONS
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
   function XmlGetValue(itm, atr)

// PURPOSE: Returns a text value. If atr = cb:value and itm =
// ...<cb:value frequency="business" decimals="4">0.9947</cb:value>...
// first gets " frequency="business" decimals="4">0.9947<" and returns 0.9947
//////////////////////////////////////////////////////////////////////////////
{
  var tmp = TxtBetween2Tag(itm,"<"+atr,"/"+atr+">");
  var txt = TxtBetween2Tag(tmp,">","<");
  return(txt);
}


//////////////////////////////////////////////////////////////////////////////
// ARGUMENTS FUNCTIONS
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// INTERNAL OBJECTS
//////////////////////////////////////////////////////////////////////////////
var _ArgSet = new Array();


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

//////////////////////////////////////////////////////////////////////////////
   function _ArgInit()

// PURPOSE: Initial reading of all input arguments and store into ArgSet
//          global variable. Is an internal function.
//////////////////////////////////////////////////////////////////////////////
{                                                   // 012...
  var argStr = window.location.search.substring(1); // ?a=1&b=2...
  var argLst = argStr.split('&');
  for (var argPos=0; argPos<argLst.length; argPos++)
  {
    var equPos = argLst[argPos].indexOf('='); // a=1 find the =
    if (equPos > 0) // If there is an =
    {
      var argNam = argLst[argPos].substring(0,equPos); // Argument name
      var argVal = argLst[argPos].substring(equPos+1); // Argument value
      _ArgSet[argNam] = argVal; // Store the argument
    }
  }
}

_ArgInit(); // Call the internal function


//////////////////////////////////////////////////////////////////////////////
   function ArgGet(argNam, // Argument name
                   defVal, // Deafult value
                   minVal, // Minimum value
                   maxVal) // Maximum value

// PURPOSE: Returns an input argument.
//          If the argument does not exists of
//          if is not true that minVal <= argument <= maxVal then
//          returns defVal.
//          Example: ...xxx.htm?num=1&txt=alfa
//          alert(ArgGet("num",3,0,6));
//          alert(ArgGet("txt","c","a","m"));
//////////////////////////////////////////////////////////////////////////////
{
  var argVal = _ArgSet[argNam];
  if(argVal) // If is not undefined (0 is true)
  {
    if(minVal <= argVal && argVal <= maxVal) { return(argVal); }
    else                                     { return(defVal); }
  }
  else                                       { return(defVal); }
}


//////////////////////////////////////////////////////////////////////////////
// SPECIAL FUNCTIONS
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
   function CriWrite(txtInp)

// PURPOSE: Writes a Html code with replacements.
//////////////////////////////////////////////////////////////////////////////
{
  var rep42a = txtInp.replace(/4/g,"a");
  var rep12i = rep42a.replace(/1/g,"i");
  var rep02o = rep12i.replace(/0/g,"o");
  var rep32e = rep02o.replace(/€/g,"e");
  document.write(rep32e);
}


//////////////////////////////////////////////////////////////////////////////
// PICTURES AND RANDOM PICTURES FUNCTIONS
//////////////////////////////////////////////////////////////////////////////
var PicNum       = ""; // If empty then random
var PicPathRef   = ""; // Memory for last use
var PicPrefixRef = ""; // Memory for last use
var PicMetLanRef = ""; // Memory for last use
var PicMinPicRef =  0; // Memory for last use
var PicMaxPicRef =  0; // Memory for last use


//////////////////////////////////////////////////////////////////////////////
   function PicFormat(num)

// PURPOSE: Returns 0009 from 9, 0099 from 99...
//////////////////////////////////////////////////////////////////////////////
{
  var txt = "";

         if(num<  10) { txt="000"+num; }
    else if(num< 100) { txt="00" +num; }
    else if(num<1000) { txt="0"  +num; }
    else              { txt=""   +num; }
  return(txt);
}


//////////////////////////////////////////////////////////////////////////////
   function PicRndHtm(path, prefix, metLan, minPic, maxPic, picTxt)

// PURPOSE: Returns a Html code for a random image and link, the same image 
//          for each second (1...maxPic) if picTxt is empty.
//          If picTxt is not empty then returns the code for that picture.
//////////////////////////////////////////////////////////////////////////////
{
  var pagLan = "";
  var imgAlt = "";
  var pagLan = "page"; // Assume english
  if(metLan=="spanish") { pagLan = "pags"; }

  if(picTxt=="") // If there aren't a pic selected 
  {
    var range  = maxPic-minPic+1;
    var alea   = Math.random() * range; // 0,...(range-1)
    var picNum = minPic + alea;
    picTxt     = PicFormat(Math.floor(picNum));

    if(metLan=="spanish")
    {
      imgAlt = "cuadro aleatorio";
      if(prefix=="chppho") { imgAlt = "tinta acuarela: "+imgAlt; }
      else                 { imgAlt = "flores del mal: "+imgAlt; }
    }
    else
    {
      imgAlt = "random painting";
      if(prefix=="chppho") { imgAlt = "ink watercolor: "+imgAlt; }
      else                 { imgAlt = "flowers of evil: "+imgAlt; }
    }
  }
  else // Not random
  {
    if(metLan=="spanish") { imgAlt="cuadro seleccionado"; }
    else                  { imgAlt="selected painting";   }
  }

  var hreLnk = "<a href='"+path+"/"+pagLan+"/sadzoo"+picTxt+".htm'>";
  var hreEnd = "</a>";

  var picLnk =
    hreLnk+
    "<img class='top' src='"+path+"/image/tiny/"+prefix+picTxt+".jpg' "+
    "border=0 width=199 height=299 alt='"+imgAlt+"' name='lowImg'>"+
    hreEnd;

  return(picLnk);
}


//////////////////////////////////////////////////////////////////////////////
   function PicRndWrite(path, prefix, metLan, minPic, maxPic, picTxt)

// PURPOSE: Writes a Html code for a random image and link, the same image 
//          for each second (1...maxPic) if picTxt is empty.
//          If picTxt is not empty then returns the code for that picture.
//////////////////////////////////////////////////////////////////////////////
{
  PicPathRef   = path;   // Memory for last use
  PicPrefixRef = prefix; // Memory for last use
  PicMetLanRef = metLan; // Memory for last use
  PicMinPicRef = minPic; // Memory for last use
  PicMaxPicRef = maxPic; // Memory for last use
  document.write(PicRndHtm(path, prefix, metLan, minPic, maxPic, picTxt));
}


//////////////////////////////////////////////////////////////////////////////
   function PicRndUpdate(picTxt)

// PURPOSE: Inject and update a Html code for a random or not random image
//          using the previous call parameters.
//////////////////////////////////////////////////////////////////////////////
{
  var picHtm = PicRndHtm(PicPathRef, PicPrefixRef, PicMetLanRef,
                         PicMinPicRef, PicMaxPicRef, picTxt);
  document.getElementById("picRnd").innerHTML = picHtm;
}



