var ss_INTERVAL;
var ss_STEPS = 25;		

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function formatStrMoney(str,dcm)
{
   var rst;
   if (dcm==0)
      rst=str +'.00';
   else
    {
      into=str.length-dcm;
      rst=str.substring(0,into)+'.'+str.substring(into,into+dcm);
    }
    return rst
}
function formatCurr(num)
{
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function FormatDate(fecha)
{
   var arrMonths;
   var arrDays;
   var m, d, y;
   var date = fecha;
   m = date.substring(4,6);
   d = date.substring(6,8);
   y = date.substring(0,4);
   var DateF = new Date(y,m-1,d);
   var mdate = DateF.getDate();
   var wdate = DateF.getDay();
   var month = DateF.getMonth();
   
   var formattedDate = "";
   
   switch(currentLanguage){
	case "EN":
		arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
		arrDays = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
		formattedDate = arrDays[wdate] + ", " +  arrMonths[month] + " " + mdate;
		break;
		
	case "ES":
		arrMonths = new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
		arrDays = new Array("Dom","Lun","Mar","Mie","Jue","Vie","Sab");
		formattedDate = arrDays[wdate] + " " + mdate + " de " +  arrMonths[month];
		break;
   }
       
   return formattedDate;
}

function FormatTime(tiempo)
{
   var hr = parseInt(tiempo/60);   
   var min = tiempo % 60;
   if (hr == 0)
      return min + " min.";   
   else {
      if (min == 0)
         return hr + " hr" ;
      else
         return hr + " hr. " + min + " min." ;
   }   
}

function FormatHour(hora)
{
   if (hora >= 1200 & hora <=2400) {
      var hr = getFormat(hora);
      if (hr.substring(0,2) >= 13) {               
         var hr2 = hr.substring(0,2) - 12;
         var min = hr.substring(3,5);
         return hr2 + ":" + min + " pm";
      }else {
         return hr + " pm";
      }      
   }else {
      var hr = getFormat(hora);
      return hr + " am";
   }
}

function getFormat(hora)
{
   if (hora.length == 3) {
      var hr = hora.substring(0,1);
      var min = hora.substring(1,3);         
   }else {
      if (hora.length == 2) {
         var hr = 12;
         var min = hora;
      }else {
         if (hora.length == 1){
            var hr = 12;
            var min = "0" + hora;
         }else {
            var hr = hora.substring(0,2);
            var min = hora.substring(2,4);
         }
      }
   }
   return hr + ":" + min
}

function alignCenter(iBrw, tblMain){
   //var iBrw=window.document.getElementById(iDiv); iBrw must be an object
   var tbl=document.getElementById(tblMain);                       
   //iBrw.style.position='Absolute';
   if(iBrw){               
    var scrollAmount = document.all ? document.body.scrollTop : window.pageYOffset;
    var heightBox = document.all ? document.body.offsetHeight : window.innerHeight;
        iBrw.style.top=(heightBox/2) + scrollAmount;
        iBrw.style.left='50%';
        //iBrw.style.marginLeft = (tbl.style.width.replace('px','')/2 * -1) + 'px';
        //iBrw.style.marginTop = (tbl.style.height.replace('px','')/2 * -1) + 'px';                       
   }
}

function smoothScroll() 
{
 	 var destinationLink = document.getElementById('divMatrix');
 // If we didn't find a destination, give up and let the browser do
 // its thing
	 if (!destinationLink) {return true;}
	 
 // Find the destination's position
 	var destx = destinationLink.offsetLeft;  
	var desty = destinationLink.offsetTop;
	var thisNode = destinationLink;
	while (thisNode.offsetParent && (thisNode.offsetParent != document.body)) 
	{
	   thisNode = thisNode.offsetParent;
	   destx += thisNode.offsetLeft;
	   desty += thisNode.offsetTop;
	}
 // Stop any current scrolling
 	//clearInterval(ss_INTERVAL);
 	cypos = ss_getCurrentYPos();
 
 	ss_stepsize = parseInt((desty-cypos)/ss_STEPS);
	ss_INTERVAL = setInterval('ss_scrollWindow('+ss_stepsize+','+desty+',"divMatrix")',10);
 
// And stop the actual click happening
 	if (window.event) 
	{
	   window.event.cancelBubble = true;
	   window.event.returnValue = false;
	}
	
} 



function ss_scrollWindow(scramount,dest,anchor) 
{
 wascypos = ss_getCurrentYPos();
 isAbove = (wascypos < dest);
 window.scrollTo(0,wascypos + scramount);
 iscypos = ss_getCurrentYPos();
 isAboveNow = (iscypos < dest);
 if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
   // if we've just scrolled past the destination, or
   // we haven't moved from the last scroll (i.e., we're at the
   // bottom of the page) then scroll exactly to the link
   window.scrollTo(0,dest);
   // cancel the repeating timer
   clearInterval(ss_INTERVAL);
   // and jump to the link directly so the URL's right
   location.hash = anchor;
 }
}

function ss_getCurrentYPos() 
{
 if (document.body && document.body.scrollTop)
   return document.body.scrollTop;
 if (document.documentElement && document.documentElement.scrollTop)
   return document.documentElement.scrollTop;
 if (window.pageYOffset)
   return window.pageYOffset;
 return 0;
} 
