/**************************************************

Joshua K Roberson
Extra and random small but useful functions

**************************************************/

// Popup window
// IE7 has a security setting that displays address bar even if turned off
function extras_popUp(URL, w, h, scroll) {
  day = new Date();
  id = day.getTime();
  if(w == 0 || w > screen.width)  w = screen.width;
  if(h == 0 || h > screen.height) h = screen.height;
  LeftPos = (screen.width) ? (screen.width-w)/2 : 0;
  TopPos  = (screen.height) ? (screen.height-h)/2 : 0;
  settings = 'resizable=1,toolbar=0,location=0,statusbar=0,menubar=0,width='+w+',height='+h+',top='+TopPos+',left='+LeftPos+',scrollbars='+scroll;
  eval("page" + id + " = window.open(URL, '" + id + "', '"+settings+"');");
  return false;
}

// Redirect popup opener
function extras_redirectOpener(link) {
  if(window.opener) {
    window.opener.location.href=link
    window.close()
  } else {
    window.location.href=link;
  }
  return false;
}

// Reload popup opener
function extras_reloadOpener() {
  if(window.opener) {
    window.opener.location.reload(true);
    window.close()
  } else {
    window.location.reload(true);
  }
  return false;
}

// Inject Stock Chart indo element after the 'onload' body event
// <body onload="extras_loadStockChart();"><div id="ajax_stockChart"></div>
function extras_loadStockChart() {
/*
  var headTag = document.getElementsByTagName("head").item(0);
  if(headTag != null) { // Write to element if exists
    var scriptTag = document.createElement("script");
    //scriptTag.src = 'http://studio-5.financialcontent.com/hart?Account=oilandgasinvestor&Module=stockquote3&Output=jscallback&Callback=extras_stockChartLoader'; // Graphs
    scriptTag.src = 'http://studio-5.financialcontent.com/hart?Account=oilandgasinvestor&Module=stockquote4&Output=jscallback&Callback=extras_stockChartLoader'; // No Graphs
    headTag.appendChild( scriptTag );
  }
*/
  return '';
}

function extras_stockChartLoader (HTML,JS) {
  var obj = document.getElementById('ajax_stockChart');
  if(obj != null) { // Write to element if exists
    obj.innerHTML = HTML;
    eval(JS);
  }
}

// Show longer list when "more" link clicked
function extras_toogleDisplay(id_hide, id_show) {
  var id1 = id_hide;
  var id2 = id_show;
  var div1 = null;
  var div2 = null;

  if(document.getElementById) {
    div1 = document.getElementById(id1);
    div2 = document.getElementById(id2);
  } else if(document.all) {
    div1 = document.all[id1];
    div2 = document.all[id2];
  } else if(document.layers) {
    div1 = document.layers[id1];
    div2 = document.layers[id2];
  }

  div1.style.display = 'none';
  div2.style.display = 'block';
  return false;
}

// Show element if hidden or hide element if shown when link clicked
function extras_toggleElement(id) {
  var id1 = id;
  var div1 = null;
  if(document.getElementById) {
    div1 = document.getElementById(id1);
  } else if(document.all) {
    div1 = document.all[id1];
  } else if(document.layers) {
    div1 = document.layers[id1];
  }
  if(div1.style.display == 'none') {
    div1.style.display = 'block';
  } else {
    div1.style.display = 'none';
  }
  return false;
}

// Find element id and return as an object
function extras_GetElementIdAsObject(id) {
  if(document.getElementById)
    return document.getElementById(id);
  else if(document.all)
    return document.all[id];
  else if(document.layers)
    return document.layers[id];
  else
    return null;
} // extras_GetElementIdAsObject()

function extras_SetCookie(strKey, strValue, intExpireDays) {
  if(intExpireDays > 0) {
    var dateExpire = new Date();
    dateExpire.setTime(dateExpire.getTime()+(intExpireDays*86400000));
    var strExpire = '; expires=' + dateExpire.toGMTString();
  } else {
    var strExpire = '';
  }
  document.cookie = strKey + '=' + strValue + strExpire + '; path=/';
} // extras_SetCookie()

function extras_GetCookieValue(strKey) {
  var cookieValue = document.cookie;
  if(cookieValue.length > 0) {
    var intFirst = cookieValue.indexOf(strKey + "=");
    if(intFirst != -1) {
      intFirst = intFirst + strKey.length + 1;
      var intLast = cookieValue.indexOf(";",intFirst);
      if(intLast == -1)
        intLast = cookieValue.length;
      return unescape(cookieValue.substring(intFirst, intLast));
    }
  }
  return '';
} // extras_GetCookieValue()

function extras_IsCookieEnabled(Cookie) {
  if(typeof navigator.cookieEnabled != 'undefined') { // IE4+ and NS6+
    return ( (navigator.cookieEnabled) ? true : false );
  } else {
    if(Cookie === undefined)
      Cookie = 'test';
    document.cookie = Cookie;
    return ( (document.cookie.indexOf(Cookie) != -1) ? true : false );
  }
} // extras_IsCookieEnabled()

