/*
 * JavaScript functions used by all pages
 */
var ResizeQueued = false;
var myLogReader = null;
/*
var myLogReader = new YAHOO.widget.LogReader();
*/
if (myLogReader != null)
    myLogReader.hide();

function writeSessionCookie (cookieName, cookieValue) {
  if (testSessionCookie()) {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
    return true;
  }
  else return false;
}

function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}

function testSessionCookie () {
  document.cookie ="testSessionCookie=Enabled";
  if (getCookieValue ("testSessionCookie")=="Enabled")
    return true
  else
    return false;
}

function TimedResize(timeoutMS)
{
    timeoutMS = typeof(timeoutMS) != 'undefined' ? timeoutMS : 1000;
    if (! ResizeQueued)
    {
        ResizeQueued = true;
        window.setTimeout("window.location.href = window.location.href;", timeoutMS);
    }
    return;
}

function AvailWidth(nReservedPixels)
{
    var x;
    if (self.innerHeight) // all except Explorer
        x = self.innerWidth - 22;
    else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
        x = document.documentElement.clientWidth;
    else if (document.body) // other Explorers
        x = document.body.clientWidth;
    return (Math.max((x-nReservedPixels), 1));
}

function LogMsg(text)
{
    if (myLogReader != null)
        myLogReader.show();
    YAHOO.log(text);
    return;
}
/*
 * MENU HIGHLIGHTER
 *
 * Original version by M. Smarandoiu & A. Niculescu: www.MediaDivision.com
 */
function extractPageName(hrefString)
{
    var arr = hrefString.split('.'); /* Get [optional-www, domain-name, com|net|obj, html|php] */

    arr = arr[arr.length-2].split('/'); /* element length-2 should be the domain name plus the forward slash */
    return arr[arr.length-1].toLowerCase();
}

function setActiveMenu(arr, crtPage, em_class)
{
    for (var i=0; i < arr.length; i++)
        if (extractPageName(arr[i].href) == crtPage)
        {
            arr[i].className = em_class;
            arr[i].parentNode.className = em_class;
        }

    return;
}

function setPage(Id, em_class)
{
    var Id = (null == Id) ? "nav" : Id;
    var em_class = (null == em_class) ? "selected" : em_class;

    if (myLogReader != null)
         myLogReader.show();

    if (document.location.href)
        hrefString = document.location.href;
    else
        hrefString = document.location;

    if (document.getElementById(Id)!=null)
        setActiveMenu(document.getElementById(Id).getElementsByTagName("a"), extractPageName(hrefString), em_class);
    return;
}

/*
 * Url Functions
 */
function GoURL(loc, target)
{
    var lowtarget = (null == target) ? "_self" : target.toLowerCase();
    var lowloc = loc.toLowerCase();

    currentloc = location.href;
    if (currentloc.search(lowloc) < 0)
    {
        return;
    }

    if (lowtarget=="_self")
        location.href=lowloc;
    else if (lowtarget=="_top")
        top.location=lowloc;
    else if (lowtarget=="_blank")
        window.open(lowloc);
    else if (lowtarget=="_parent")
        parent.location=lowloc;
    else
        parent.frames[target].lowloc;

    return;
}

function JumpToURL(menu)
{
    ref=menu.choice.options[menu.choice.selectedIndex].value;
    splitc=ref.lastIndexOf("*");
    target="";
    if (splitc != -1)
    {
        loc=ref.substring(0,splitc);
        target=ref.substring(splitc+1,1000);
    }
    else
    {
        loc=ref;
        target="_top";
    }
    if (0 != loc.length)
        GoURL(loc,target);

    return;
}

function GetUrl($UrlStr)
{
   if (IsUsingIE())
       return ('"'+"http://"+$UrlStr+'"');
   else
       return ('"'+"http://"+unescape($UrlStr)+'"');
}

/*
 * Browser version detection
 */

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure
    if (navigator.appName == "Microsoft Internet Explorer")
    {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat( RegExp.$1 );
    }
    return rv;
}

function IsUsingIE()
{
    var ver = getInternetExplorerVersion();

    if ( ver > -1 )
        if ( ver >= 6.0 )
            return 1;
        else
            return 1;
    return (0);
}

/*
 * Predefined image size selection
 */

function GetImageSizeQual(sImage, nColumns)
{
    var x;
    x = AvailWidth(0);
    if (x <= 1000)
        if (nColumns <= 1)
            return (sImage);
        else if (nColumns <= 2)
            return (sImage.replace(".jpg", "_s.jpg"));
        else
            return (sImage.replace(".jpg", "_xs.jpg"));
    else if (x <= 1200)
        if (nColumns <= 2)
            return (sImage);
        else
            return(sImage.replace(".jpg", "_xs.jpg"));
    else if (nColumns <= 2)
        return (sImage);
    else
        return (sImage.replace(".jpg", "_s.jpg"));

    return;
}

