
// homokaasu.org helper functions. Use freely.

String.prototype.endsWith = function(sEnd) {
	return (this.substr(this.length-sEnd.length)==sEnd);
}

function LaunchMindstrings() {
  var WindowName="mindstrings";
  if(location.href.toString().toLowerCase().indexOf('homokaasu.org')<0) WindowName+="_dev";

  window.open('/sect/chat/', WindowName, 'toolbar=no,location=no,status=1,menubar=no,scrollbar s=no,directories=no,resizable=yes,left=50,top=50,width=650,height=500');
}

function Glow(t, fg, bg) {
  t.style.color=fg;
  t.style.backgroundColor=bg;
}

function FillOptionList(t, start, end, sel) {
  for(i=start;i<=end;i++) t.options[i-start]=new Option(i, i, false, false);
  t.selectedIndex=sel-start;
}

function LogoutWarning() {
  return window.confirm('Your account information has been saved into your browser to enable\nautomatical log in. Select ok to log out and remove that information.\n\nTo log out from the site without removing the account information,\njust browse to another site or close your browser.');
}

function DoGlow(GlowTarget, GlowCurrentValue, GlowTargetValue) {
  var hex=new String("0123456789abcdef");
  var t=document.getElementById(GlowTarget+"_Title");

  GlowCurrentValue+=GlowTargetValue < GlowCurrentValue ? -1 : 1;

  var c=hex.charAt(Math.floor(GlowCurrentValue/16))+hex.charAt(GlowCurrentValue%16);
  c="#"+c+c+c;

  t.style.backgroundColor=c;
  t.Current=GlowCurrentValue;

  if(GlowCurrentValue!=GlowTargetValue) {
    t.Timeout=window.setTimeout("DoGlow(\""+GlowTarget+"\", "+GlowCurrentValue+", "+GlowTargetValue+");", 10);
  } else {
    t.Timeout=null;
  }
}

function Shade2(Sender, state, remember, callback) {
  var CookieExpires=new Date(new Date().getTime()+31449600000);

  var domid=Sender.id.replace("_Header", "");

  if(domid.endsWith("_Header")) domid=domid.substring(0, domid.length-7);
  if(domid.endsWith("_Shade")) domid=domid.substring(0, domid.length-6);

  var t=document.getElementById(domid+"_Normal");
  var s=document.getElementById(domid+"_Shade");

  t.style.display=state ? "none" : "";
  s.style.display=state ? "" : "none";

  if(remember) SetCookieItem("GasTables", domid, state ? "1": "0", CookieExpires);

  if(callback!="") eval(callback+"("+state+");");
}

function Shade(domid, state, remember, callback) {
  var CookieExpires=new Date(new Date().getTime()+31449600000);

  var t=document.getElementById(domid+"_Normal");
  var s=document.getElementById(domid+"_Shade");

  t.style.display=state ? "none" : "";
  s.style.display=state ? "" : "none";

  if(remember) SetCookieItem("GasTables", domid, state ? "1": "0", CookieExpires);

  if(callback!="") eval(callback+"("+state+");");
}


function GetCookieItem(CookieName, ItemName) {
  var Crumbs=GetCookie(CookieName);
  for(var i=0;i<crumbs.length;i++) {
    var b=Crumbs[i].split("=");
    if(b[0]==ItemName) return unescape(b[1]);
  }
}

function SetCookieItem(CookieName, ItemName, Value, Expiry) {
  var Bake="";

  var c=GetCookie(CookieName);
  if(c==null) {
    Bake=ItemName+"="+escape(Value);
    SetCookie(CookieName, Bake, Expiry);
    return;
  }

  var Crumbs=c.split("&");
  var found=false;
  for(var i=0;i<Crumbs.length;i++) {
    var b=Crumbs[i].split("=");
    if(b[0]==ItemName) {
      Bake+=ItemName+"="+Value;
      found=true;
    } else {
      Bake+=Crumbs[i];
    }
    if(i<Crumbs.length-1) Bake+="&";
  }
  if(!found) Bake+="&"+ItemName+"="+escape(Value);
  SetCookie(CookieName, Bake, Expiry);
}

function GetCookie(Name) {
  var Crumbs=document.cookie.split("; ");
  for(var i=0;i<Crumbs.length;i++) {
    var b=Crumbs[i].indexOf("=");
    if(Crumbs[i].substr(0,b)==Name) return Crumbs[i].substr(b+1);
  }
  return null;
}

function SetCookie(CookieName, Value, Expiry) {
  if(Value=="") return;
  if(Value==null) return;
  if(Value==undefined) return;
  document.cookie=CookieName+"="+Value+"; expires="+Expiry.toGMTString();
}


// Internet Explorer png alpha fix derived from a hack by the valiant and revered Bob Olosa
// http://homepage.ntlworld.com/bobosola/index.htm

function fixpng() {
  for(var i=0; i<document.images.length; i++) {
    try {
      var img=document.images[i];
      var imgName=img.src.toLowerCase();
      if(imgName.substring(imgName.length-3, imgName.length)!="png") continue;

      var imgID=img.id ? "id='"+img.id+"' " : "";
      var imgClass = (img.className) ? "class='" + img.className + "' " : "";
      var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
      var imgStyle = "display:inline-block;" + img.style.cssText;
      var imgAttribs = img.attributes;
      for (var j=0; j<imgAttribs.length; j++) {
        var imgAttrib=imgAttribs[j];
        if(imgAttrib.nodeName!="align") continue;

        if(imgAttrib.nodeValue=="left") imgStyle="float:left;"+imgStyle;
        if(imgAttrib.nodeValue=="right") imgStyle="float:right;"+imgStyle;
      }
      var strNewHTML = "<span " + imgID + imgClass + imgTitle;
      strNewHTML += " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";vertical-align:middle;";
      strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader";
      strNewHTML += "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"; 
      img.outerHTML = strNewHTML;
      i--;
    } catch(e) {}
  }
}
