
function _chop(str){
    if(str==null||typeof str!='string')return null;
    if(str=="")return "";
    return str.substring(0,str.length-1);
}

function setInnerHTML(div_id,value)
{
    var dstDiv=document.getElementById(div_id);
    dstDiv.innerHTML=value;
}

function openPopup(url,name,height,width)
{
    var newwindow=window.open(url,name,'height='+height+',width='+width);
    if(window.focus){newwindow.focus()}
    return false;
}

function openDiv(elName){
    var theElemenet=document.getElementById(elName);
    if(theElemenet){
        theElemenet.style.display="block";
    }
}

function closeDiv(elName){
    var theElemenet=document.getElementById(elName);
    if(theElemenet){
        theElemenet.style.display="none";
    }
}

function showInline(elName){
    var theElemenet=document.getElementById(elName);
    if(theElemenet){
        theElemenet.style.display="inline";
    }
}

function hideInline(elName){
    var theElemenet=document.getElementById(elName);
    if(theElemenet){
        theElemenet.style.display="none";
    }
}


function setVisible(divName,onOrOff){
    var tempDiv=document.getElementById(divName);
    if(!tempDiv){
        return;
    }
    if(onOrOff){
        tempDiv.style.visibility="visible";
    }
    else{
        tempDiv.style.visibility="hidden";
    }
}




function validateURL(inputField){
    if(inputField.value.indexOf("http://")==0){
        return false;
    }
    else {
        inputField.value="http://"+inputField.value;
        return true;
    }
}

function getDivHeight(div){
    if(div.clientHeight){
        return div.clientHeight;
    }
    else if(div.offsetHeight){
        return div.offsetHeight;
    }
}

function readCookie(name,fallback){
    var nameEQ = name+"=";
    var ca=document.cookie.split(';');
    for(var i=0;i<ca.length;i++){
        var c = ca[i];
        while(c.charAt(0)==' ') c=c.substring(1,c.length);
        if(c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);
    }
    if(fallback){
        return fallback;
    }
    else {
        return null;
    }
}

function createCookie(name,value,days){
    var cookie="";
    var domain=cookie_domain;
    var path="/";
    cookie+=name+"="+value+";";
    cookie+="domain=."+domain+";";
    cookie+="path="+path+";";
    if(days){
        var date=new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        cookie+="expires="+date.toGMTString()+";";
    }
    document.cookie=cookie;
}

function eraseCookie(name){
    createCookie(name,"",-1);
}


function getXmlHttpRequest()
{
    var httpRequest=null;
    try
    {
        httpRequest=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            httpRequest=null;
        }
    }

    if(!httpRequest&&typeof XMLHttpRequest!="undefined")
    {
        httpRequest=new XMLHttpRequest();
    }
    return httpRequest;
}

// performs a server request and assigns a callback function
function processServerRequest(serverScript, callbackFunction)
{
  var xmlHttp = getXmlHttpRequest();
  
  // continue only if xmlHttp isn't void
  if (xmlHttp)
  {
    // try to connect to the server
    try
    {
      // initiate server request
      xmlHttp.open("GET", serverScript, true);
      xmlHttp.onreadystatechange = callbackFunction;
      xmlHttp.send(null);
      return 1;
    }
    // display an error in case of failure
    catch (e)
    {
      return 0;
    }
  }
}


function hex1(i) {
    if (i >= 0 && i < 10)
        return i;
    else if (i == 10)
        return "a";
    else if (i == 11)
        return "b";
    else if (i == 12)
        return "c";
    else if (i == 13)
        return "d";
    else if (i == 14)
        return "e";
    else if (i == 15)
        return "f";
    else return "0"
}

function hex(i) {
    if(i<0)
        return "00";
    else if(i>255)
        return "ff";
    else
    return "" + hex1(Math.floor(i/16)) + hex1(i%16);
}

function setbgColor(r,g,b,elt){
    var hr=hex(r);var hg=hex(g);var hb=hex(b);
    document.getElementById(elt).style.backgroundColor="#"+hr+hg+hb;
}

function setForeColor(r,g,b,elt){
    var hr=hex(r);var hg=hex(g);var hb=hex(b);
    document.getElementById(elt).style.color="#"+hr+hg+hb;
//    alert("color: " + "#"+hr+hg+hb);
}

function fadeBack(sr,sg,sb,er,eg,eb,step,elt){
    for(var i=0;i<=step;i++){
        setbgColor(
         Math.floor(sr*((step-i)/step)+er*(i/step)),
         Math.floor(sg*((step-i)/step)+eg*(i/step)),
         Math.floor(sb*((step-i)/step)+eb*(i/step)), elt);
    }
}

function fadeFore(sr,sg,sb,er,eg,eb,step,elt){
    for(var i=0;i<=step;i++){
        setForeColor(
         Math.floor(sr*((step-i)/step)+er*(i/step)),
         Math.floor(sg*((step-i)/step)+eg*(i/step)),
         Math.floor(sb*((step-i)/step)+eb*(i/step)), elt);
    }
}

function fadeTest(elt, shade) {
//    alert("elt: " + elt + " shade: " + shade);
//    document.getElementById(elt).innerHTML = shade;
    setForeColor(shade,shade,shade,elt);
    shade++;
    if (shade < 256) setTimeout("fadeTest('" + elt + "', " + shade + ")", 100);
}

function fadeIn(elt){
    fadeBack(255,255,255,102,204,0,825,elt);
}

function fadeOut(elt){
    setTimeout("fadeBack(102,204,0,255,255,255,2025," + elt + ")",800)
}

// Starts with Black text, fades linearly to white, returns linearly to black, about 8 second cycles.
// Call it with shade = 0 and dir = 'out'
function fadeForeOutIn(elt, shade, dir) {
    setForeColor(shade,shade,shade,elt);
    if (dir == 'out') {
        shade++;
        if (shade < 256) 
            setTimeout("fadeForeOutIn('" + elt + "', " + shade + ", 'out')", 10);
        else
            setTimeout("fadeForeOutIn('" + elt + "', 255, 'in')", 10);
    }
    else {
        shade--;
        if (shade >= 0) 
            setTimeout("fadeForeOutIn('" + elt + "', " + shade + ", 'in')", 10);
        else
            setTimeout("fadeForeOutIn('" + elt + "', 0, 'out')", 10);
    }
}

// Starts with Black text for 57 seconds, fade to white in < 2, return to black in < 2, cycles again.
// Call it with shade = 0 and dir = 'none'
function fadeForeOutIn2(elt, shade, dir) {
    setForeColor(shade,shade,shade,elt);
//    document.getElementById(elt).innerHTML = dir;
    if (dir == 'none') {
            setTimeout("fadeForeOutIn2('" + elt + "', " + shade + ", 'out')", 57000);
    }
    else {
        if (dir == 'out') {
            shade += 8;
            if (shade < 256) 
                setTimeout("fadeForeOutIn2('" + elt + "', " + shade + ", 'out')", 20);
            else
                setTimeout("fadeForeOutIn2('" + elt + "', 255, 'in')", 20);
        }
        else {
            shade -= 8;
            if (shade >= 0) 
                setTimeout("fadeForeOutIn2('" + elt + "', " + shade + ", 'in')", 20);
            else
                setTimeout("fadeForeOutIn2('" + elt + "', 0, 'none')", 20);
        }
    }
}

// Same as 2 but adds a random amount to the number each cycle.
// Call it with shade = 0 and dir = 'none'
function fadeForeOutIn3(elt, shade, dir, mean) {
    setForeColor(shade,shade,shade,elt);
    if (dir == 'none') {
            setTimeout("fadeForeOutIn3('" + elt + "', " + shade + ", 'out', " + mean + ")", 57000);
    }
    else {
        if (dir == 'out') {
            shade += 8;
            if (shade < 256) 
                setTimeout("fadeForeOutIn3('" + elt + "', " + shade + ", 'out', " + mean + ")", 20);
            else {
                // We're about to fade in.  Increase the number by a small random amount.
                if (mean > 0) IncreaseNumberInElement(elt, mean);
                setTimeout("fadeForeOutIn3('" + elt + "', 255, 'in', " + mean + ")", 20);
            }
        }
        else {
            shade -= 8;
            if (shade >= 0) 
                setTimeout("fadeForeOutIn3('" + elt + "', " + shade + ", 'in', " + mean + ")", 20);
            else
                setTimeout("fadeForeOutIn3('" + elt + "', 0, 'none', " + mean + ")", 20);
        }
    }
}

function IncreaseNumberInElement(elt, mean) {
    var inStr;      // the innerHTML of the span we're working on
    var space;      // index to first space in inStr
    var num;        // number part of inStr
    var inc;        // amount to increase number
    var txt;        // text part of inStr
    
    inStr = document.getElementById(elt).innerHTML;
    space = inStr.indexOf(' ');
    num = inStr.substring(0, space);
    txt = inStr.substring(space, inStr.length);
    num = num.replace(/,/g, '');      // get rid of commas in number
    num = parseFloat(num);            // convert string to number
    
    inc = (Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1);  // sum 3 rnd #s between -1 and 1
    inc = Math.floor(((10 + inc) * mean) / 10); // standard deviation is 1/10 of mean
    
    num += inc;   // add the increase
    num = addCommas(num);             // convert number to string with commas added
    document.getElementById(elt).innerHTML = num + txt;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function stripNonNumber(val){
    return val.replace(/[^\d]/g,'');
}

