/**************************************************
* kamilo.js
* General purpose routines 
***************************************************/

String.prototype.trim = function(){ return this.replace(/(^\s*)|(\s*$)/g, "");}

function trim(text){ return (text ? text.replace(/(^\s+)|(\s+$)/g, "") : "");}	

function shortDateToDate(sd)
{
	var a = sd.split("/");
	return new Date(a[2], a[1] - 1, a[0]);
}

function enableAll(form)
{
	for (var i = 0; i < form.elements.length; i++) form.elements.item(i).disabled = false;
}

function nmbe_keydown(oSrc, event)
{
	if (isNav) 
	{ 
		if (event.which == 13) event.which == 9;
	}
	else
	{
		if (event.keyCode == 13) event.keyCode = 9;
	}	 
}

function nmbe_keypress(oSrc, event)
{
	var validKeys = "32,44,48,49,50,51,52,53,54,55,56,57";
	var key = isNav ? event.which : event.keyCode; 
	if(validKeys.indexOf(key) == -1)
	{
		event.returnValue = false;
		return false;
	}
}
function dateToShort(oDate, bTime)
{
	var text = padl(oDate.getDate(), "0", 2) + "/" + padl(oDate.getMonth() + 1, "0", 2) + "/" + oDate.getFullYear();
	if (bTime) text += "  " + padl(oDate.getHours(), "0", 2) + ":" + padl(oDate.getMinutes(), "0", 2);// + ":" + padl(oDate.getSeconds(), "0", 2);
	return text;
}	

function padl(s, ch, length)
{
	s = s.toString();
  for (var i = s.length; i < length; i++)
    s = ch + s;
  return s;
}

function distinctArray(a)
{
  var b = [];
  a = a.sort(numberCompare);
  var j;
  for (var i = 0; i < a.length; i++)
  {
    if (a[i] != j) b[b.length] = a[i];
    j = a[i];
  }
  return b;
} 

function numberCompare(a, b){	return a - b;	}

function getParentOfTag(element, tag)
{
	element = element.parentNode;
	while (element)
	{
		if (element.tagName == tag) return element;
		element = element.parentNode;
	}
}

function tenToTheNth(n)
{
  if (n == 0)
    return 1;
  else
    return 10 * tenToTheNth(n-1);
}

function doSelectedRecs(form, action)
{
	if (form.tagName != "FORM") form = form.form;
	var valid = false;
	var cbs = form.elements["__tbcb"];
	if (!cbs) return;
	//alert(typeof(form.__tbcb));
	if (cbs.id)
	{
		if (cbs.checked) valid = true;
	}
	else
	{
		for(var i = 0; i < cbs.length; i++)
		{
			if (cbs[i].checked)
			{
				valid = true;
				break;
			}
		}
	}
	with (form.validator.validationSummary)
	{
		if (!valid) 
		{
			clear();
			add(0, "No ha sido marcado ningun campo a borrar");
			show();
		}	
		else
		{
			form.requested_proc.value = action;
			form.submit();
		}
	}	
}

function removeSelectedRecs(form){doSelectedRecs(form, "remtik");}
function activateSelectedTickets(form){doSelectedRecs(form, "activate");}

function formatNumber(number, bstrFormat)
{
  var afterDecimal = false;
  var afterDecimalPrecision = 0;
	//number = parseFloat(number);
	//if (bstrFormat == 2) bstrFormat = "#,##0.00";
	//if (bstrFormat == 0) bstrFormat = "#,###";

  // parse the format string, building a template determining precision required for
  //  rounding, whether there is a thousands separator, and performing scaling.
  var thousandsSeparator = false;
  var afterDecimalMinPrecision = 0;
  var afterDecimalMaxPrecision = 0;
  var beforeDecimalMinPrecision = 0;
  var template = "";
  for (var i=0; i<bstrFormat.length; i++)
  {
    switch (bstrFormat.charAt(i)) {
    case "0":
      if (afterDecimal)
      {
        afterDecimalPrecision++;
        afterDecimalMinPrecision = afterDecimalPrecision;
        afterDecimalMaxPrecision = afterDecimalPrecision;
      }
      else
      {
        beforeDecimalMinPrecision++;
      }
      template += "0";
      break;
    case "#":
      if (afterDecimal)
      {
        afterDecimalPrecision++;
        afterDecimalMaxPrecision = afterDecimalPrecision;
      }
      else
      {
        if (beforeDecimalMinPrecision > 0)
          beforeDecimalMinPrecision++;
      }
      template += "#";
      break;
    case ".":
      afterDecimal = true;
      template += ".";
      break;
    case ",":
      if (afterDecimal)
        template += ",";
      else
      {
        if (bstrFormat.charAt(i+1) == "0" || bstrFormat.charAt(i+1) == "#")
          thousandsSeparator = true;
        else
          number /= 1000;
      }
      break;
    case "%":
      number *= 100;
      template += "%";
      break;          
   case '"':
      i++;
      while (bstrFormat.charAt(i) != '"' && i < bstrFormat.length)
        template += '"' + bstrFormat.charAt(i++);
      break;
    default:
      template += bstrFormat.charAt(i);
    }
  }

  // Round the number to the correct number of digits and convert it to to a string;
  number = (Math.round(number*tenToTheNth(afterDecimalMaxPrecision)))/tenToTheNth(afterDecimalMaxPrecision);
  var numberString = number + "";
  if (number < 1)
    numberString = numberString.substring(1);

  // Where is the decimal point?
  quotedTemplate = template.replace(/"\./g,'""');
  var decimalPosition = quotedTemplate.indexOf(".");
  if (decimalPosition < 0)
    decimalPosition = template.length;

  // Fill in the template starting at the decimal point and working left
  var j = numberString.indexOf(".");
  if (j < 0)
    j = numberString.length;
  var insertionPoint = 0;
  var thousands = 0;
  for (var i=decimalPosition; i >=0; i--)
  {
    if (template.charAt(i-1) != '"')
    {
      if ((template.charAt(i) == "0" || template.charAt(i) == "#") && j > 0)
      {
        template = template.substring(0,i) + numberString.charAt(--j) + template.substring(i+1);
        if (thousandsSeparator && j > 0)
        {
          thousands++;
          if (thousands > 2) {
            thousands = 0;
            template = template.substring(0,i) + "," + template.substring(i);
          }
        }
        insertionPoint = i;
      }
      if (template.charAt(i) == "#" && j <= 0)
        template = template.substring(0,i) + template.substring(i+1);
    }
  }
      
  // Insert remaining left digits, with thousands separators as necessary
  if (j > 0)
  {
    for (j; j>0; j--)
    {
      template = template.substring(0,insertionPoint) + numberString.substring(j-1,j) + template.substring(insertionPoint);
      if (thousandsSeparator)
      {
        if (thousands > 2) {
          thousands = 0;
          template = template.substring(0,insertionPoint+1) + "," + template.substring(insertionPoint+1);
        }
        thousands++;
      }
    }
  }
      

  // Where is the decimal point now?
  quotedTemplate = template.replace(/"\./g,'""');
  var decimalPosition = quotedTemplate.indexOf(".");
  if (decimalPosition < 0)
    decimalPosition = template.length;

  // Fill in the template starting at the decimal point and working right
  var j = numberString.indexOf(".");
  if (j < 0)
    j = numberString.length;
      
  for (var i=decimalPosition + 1; i < template.length; i++)
  {
    if (template.charAt(i) == "0" && j < (numberString.length -1))
    {
      j++
      template = template.substring(0,i) + numberString.charAt(j) + template.substring(i+1);
    }
    if (template.charAt(i) == "#")
    {
      if (j < numberString.length)
      {
        j++
        template = template.substring(0,i) + numberString.charAt(j) + template.substring(i+1);
      }
      else
      {
        template = template.substring(0,i) + template.substring(i+1);
        i--;
      }
    }
    if (template.charAt(i) == '"')
      i++;
  }

  template = template.replace(/"/g,"");
  return template;      
}

function viewScanedImage(image)
{
	var newWin=window.open("http://www.loteria103.com/tickets/" + image + ".jpg",
		"image_viewer","width=300,height=330,location=no,status=0,resizable=yes,scrollbars=yes,menubar=no,toolbar=no");
	newWin.focus();		
}

// Example:
// onMouseOver="toolTip('tool tip text here')";
// onMouseOut="toolTip()";
// -or-
// onMouseOver="toolTip('more good stuff', '#FFFF00', 'orange')";
// onMouseOut="toolTip()"; 
/*
MOVE this to the <body>:

*/
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
offsetX = 0;
offsetY = 20;
var toolTipSTYLE="";
function initToolTips()
{
  if(ns4||ns6||ie4)
  {
    if(ns4) toolTipSTYLE = document.toolTipLayer;
    else if(ns6) toolTipSTYLE = document.getElementById("toolTipLayer").style;
    else if(ie4) toolTipSTYLE = document.all.toolTipLayer.style;
    if(ns4) document.captureEvents(Event.MOUSEMOVE);
    else
    {
      toolTipSTYLE.visibility = "visible";
      toolTipSTYLE.display = "none";
    }
    document.onmousemove = moveToMouseLoc;
  }
}
function toolTip(msg, fg, bg)
{
  if(toolTip.arguments.length < 1) // hide
  {
    if(ns4) toolTipSTYLE.visibility = "hidden";
    else toolTipSTYLE.display = "none";
  }
  else // show
  {
    if(!fg) fg = "#000000";
    if(!bg) bg = "#FFFFCC";
    var content =msg;
   //'<table border="1" cellspacing="0" cellpadding="0" bgcolor="' + bg + 
  //  '"><tr><td align="center" valign="top"><font face="sans-serif" color="' + fg +
   // '" size="-2">&nbsp\;' + msg +
   // '&nbsp\;</font></td></tr></table>';
    if(ns4)
    {
      toolTipSTYLE.document.write(content);
      toolTipSTYLE.document.close();
      toolTipSTYLE.visibility = "visible";
    }
    if(ns6)
    {
      document.getElementById("toolTipLayer").innerHTML = content;
      toolTipSTYLE.display='block'
    }
    if(ie4)
    {
      document.all("toolTipLayer").innerHTML=content;
      toolTipSTYLE.display='block'
    }
  }
}
function moveToMouseLoc(e)
{
  if(ns4||ns6)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else
  {
    x = event.x + document.body.scrollLeft;
    y = event.y + document.body.scrollTop;
  }
  if(x>500)x=x-300
  toolTipSTYLE.left = x + offsetX;
  toolTipSTYLE.top = y + offsetY;
  return true;
}
function swapImage(id,imag){
document.getElementById(id).src=imag
}
function swapImgRestore(id,imag){
document.getElementById(id).src=imag
}
