//============================================================
// $Id: myutils.js,v 1.5 2007/02/10 02:33:35 pinelanders Exp $
// Copyright (C) 2005 Jem Treadwell
//============================================================

function
obj(argId)
{   return document.getElementById(argId);
}

function
objFromName(argName) {
    var list = document.getElementsByName(argName);
    if (list.length == 0)
	return null;
    return list[0];
}

function 
trim(s)
{   // Trims leading and trailing whitespace
    var str = new String(s);
    var x = str.replace(/^\s+|\s+$/g,'');
    return x;
}

function 
trimCommas(s)
{   // Trims leading and trailing commas and spaces
    var str = new String(s);
    var x = str.replace(/^[\s,]*|[\s,]*$/g,'');
    return x;
}

function
validPhone(a_phone)
{   var str = a_phone;
    str = str.replace(/\D/g,"");
    if (str.length != 10)
    	return false;

    // The number is valid, so return a properly-formatted version:
    return str.replace(/(...)(...)(....)/,"$1-$2-$3");
}

function
validEmail(a_email)
{   var str = a_email;
    // This is not technically conformant to RFC822 or RFC2822, but
    // it should be good for any address we're likely to see:
    if (! str.match(/^[a-zA-Z0-9-_'\.]{1,64}@[a-zA-Z0-9-_\.]+\.[a-zA-Z]+$/)) 
    	return false;
    if (str.match(/\.\./))
        return false;
    return str;
}

function
validZip(a_zip)
{   var str = a_zip;
    if (! str.match(/^[0-9]{5}/)) 
    	return false;
    return str;
}

function
validDate(a_dateStr)
{   if ((! a_dateStr.match(/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}$/)) ||
		a_dateStr.match(/\/[0-9]{3}$/))
	return 0;

    var parts = a_dateStr.split("/");
    var y,m,d;
    y = parts[2];
    m = parts[0];
    d = parts[1];

    if (y.length == 2)
    {  if (y < 50) 
	   y = 2000 + Number(y);
       else
	   y = 1900 + Number(y);
    }
    //if (y < 1970)
	//return false;

    if (m < 1 || m > 12)
	return false;

    var mdays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    if (y % 4 == 0)
	mdays[1] = 29;
    if (d < 1 || d > mdays[m-1])
	return false;

    date = new Date();
    date.setFullYear(y,m-1,d);
    date.setHours(0,0,0);

    return date;
}

function 
normalCase(a_string)
{   if (a_string.length < 2)
	return true;

    if ((a_string == a_string.toUpperCase())
    ||  (a_string == a_string.toLowerCase()))
        return false;
    return true;
}

function
getWindowHeight() {
    // This code courtesy of 
    // http://www.howtocreate.co.uk (thank you!)
    var myHeight = 0;
    if (typeof(window.innerWidth) == 'number')
    {   // Non-IE
        myHeight = window.innerHeight;
    } else if (document.documentElement &&
	     document.documentElement.clientHeight)
    {   // IE6+ in 'standards-compliant mode'
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && document.body.clientHeight)
    {   // IE4-compatible
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}

function 
getObjectPosition(obj) {
    // This code courtesy of
    // http://www.quirksmode.org/js/findpos.html
    var curleft = curtop = 0;
    if (obj.offsetParent)
    {	curleft = obj.offsetLeft
	curtop = obj.offsetTop
	while (obj = obj.offsetParent) 
	{   curleft += obj.offsetLeft
	    curtop += obj.offsetTop
	}
    }
    return [curleft,curtop];
}

function 
togglePanelDisplay(argButton,argPanelId) {
    var panelObj = document.getElementById(argPanelId);
    if (panelObj.style.display == "none") {
        panelObj.style.display = "";
        argButton.value = "Hide"
    }
    else {
        panelObj.style.display = "none";
        argButton.value = "Show"
    }
}

function
togglePopup(argPopupId, argOffsetId,
	/* optional */ argOffsetTop, argOffsetLeft) {

    var offsetTop = argOffsetTop == null ? 10 : argOffsetTop;
    var offsetLeft = argOffsetLeft == null ? 10 : argOffsetLeft;

    var offsetObj = obj(argOffsetId);
    var popupObj = obj(argPopupId);

    if (popupObj.style.visibility == "visible") {
        popupObj.style.visibility = "hidden";
        popupObj.style.display = "none";
    }
    else {
	var pos = getObjectPosition(offsetObj);
	popupObj.style.top = pos[1] + offsetTop;
	popupObj.style.left = pos[0] + offsetLeft;

        popupObj.style.visibility = "visible";
        popupObj.style.display = "";
    }
}

