// Graphisoft R&D
// written by Lehel László - llaszlo@graphisoft.hu
// 2002-02-26 v0.1
/************************************************
*               Setting - START                 *
************************************************/

//document.oncontextmenu = fnContextMenu;

function fnContextMenu(){event.returnValue = false;}

var sParamSeparator='__space__';


/************************************************
*               Setting - END                   *
************************************************/

/************************************************
*                 Utils -  START                *
************************************************/
//function setSetterGetter (sObjectName, sFunction)
//function GetIFrameHTMLTag(oIFrame){
//function GetObjectTop(oTag, oParentFrame)
//function GetObjectLeft(oTag, oParentFrame)
//function GetObjectMostTopOffsetParent(oTag)
//function GetHTMLTagByName(oParent, sName)
//function FindInFrame(oFrame, sSearchElementName)
//function FindFrame(oStartFrame, sFrameName)
//function DateToXMLDate(aDate)
//function StringToDate(aString)
//function doChangeURLValue(sURL, sParamName, sNewValue)
//function doGetURLParamValue(sURL, sParamName)
//function DelphiColorToJSColor(iColor) 
//function JSColorToDelphiColor(sColor) 
//function getRandom(iNrFrom, iNrTo)   
//function SetStyleButton(sName, bDisabled)
//function SetStyleCombo(sName, bDisabled)
//function SetStyleInputBox(sName, bDisabled)

function setSetterGetter (sObjectName, sFunction){
  if (document.all){
    document.getElementById(sObjectName).onpropertychange = function(){eval(sFunction);};
  } else {
    eval("document.getElementById('" + sObjectName + "').value setter = function (sText) {" +
            "this.setAttribute('value', sText);" +
            sFunction + ";" +
    "};" +
    "document.getElementById('" + sObjectName + "').value getter = function () {" +
            "return this.getAttribute('value');" +
    "};");
  }
}

function GetIFrameHTMLTag(oIFrame){
  var oRes = null;  
  if(oIFrame){
    var sName= oIFrame.name;
    var oParent = oIFrame.parent;
    if(sName != "" && oParent)
      oRes = oParent.document.getElementById(sName); 
  }
  return oRes;   
}

function GetTop(oTag, oShowTag){
  return GetObjectTop(oTag)// + GetFrameTopDelta(oTag, oShowTag);
}

function GetLeft(oTag, oShowTag){
  return GetObjectLeft(oTag)// + GetFrameLeftDelta(oTag, oShowTag);
}

function GetObjectTop(oTag){
  if(!oTag) return 0;
  var iPlus = 0;
  if(oTag.offsetParent)
	if(oTag.offsetParent.tagName == "BODY")
	  if(oTag.style.position == "absolute")
	    iPlus = GetObjectTop(oTag.offsetParent);
	  else
	    iPlus = 0;
	else
	  iPlus = GetObjectTop(oTag.offsetParent);
  return oTag.offsetTop - oTag.scrollTop + iPlus;
}

function GetObjectLeft(oTag){
  if(!oTag) return 0;
  var iPlus = 0; 
  if(oTag.offsetParent)
	if(oTag.offsetParent.tagName == "BODY")
	  if(oTag.style.position == "absolute")
	    iPlus = GetObjectLeft(oTag.offsetParent);
	  else
	    iPlus = 0;
	else
	  iPlus = GetObjectLeft(oTag.offsetParent);
  return oTag.offsetLeft - oTag.scrollLeft + iPlus;
}

function GetFrameTopDelta(oTag, oShowTag){
  if(!oTag || !oShowTag || oTag.document.parentWindow == oShowTag.document.parentWindow) return 0;
  var oTemp = oTag.document.parentWindow;
  var iPlus = 0;
  while(oTemp && oTemp != oShowTag.document.parentWindow){
    var oIFrame = GetIFrameHTMLTag(oTemp);
    if(oIFrame) 
      iPlus += oIFrame.offsetTop - oIFrame.scrollTop; 
    oTemp = oTemp.parent;
  }
  return iPlus;
}

function GetFrameLeftDelta(oTag, oShowTag){
  if(!oTag || !oShowTag || oTag.document.parentWindow == oShowTag.document.parentWindow) return 0;
  var oTemp = oTag.document.parentWindow;
  var iPlus = 0;
  while(oTemp && oTemp != oShowTag.document.parentWindow){
    var oIFrame = GetIFrameHTMLTag(oTemp);
    if(oIFrame) 
      iPlus += oIFrame.offsetLeft - oIFrame.scrollLeft;
    oTemp = oTemp.parent;
  }
  return iPlus;
}

function GetObjectMostTopOffsetParent(oTag){
  if(oTag.offsetParent)
	if(oTag.offsetParent.tagName == "BODY")
	  if(oTag.style.position == "absolute")
	    return oTag;
	  else
	    return oTag.offsetParent;
	else
	  return GetObjectMostTopOffsetParent(oTag.offsetParent);
  return oTag;
}

function GetHTMLTagByName(oParent, sName){
  if(oParent)
    return(oParent.document.getElementById(sName));
  else
    return(document.getElementById(sName));	
}

function FindInFrame(oFrame, sSearchElementName){
  if(GetHTMLTagByName(oFrame, sSearchElementName) != null)
    return GetHTMLTagByName(oFrame, sSearchElementName);
  else
    for(i = 0; i < oFrame.frames.length; i++){
      oTemp = FindInFrame(oFrame.frames[i], sSearchElementName);
      if(oTemp != null)
        return oTemp;
    }
  return null;
}

function FindFrame(oStartFrame, sFrameName){
  var p = oStartFrame, i;
  while(p != null)
    if(p.name == sFrameName)
     return p;
    else{
      for(i = 0; i < p.frames.length ;i++)
        try {
             if(p.frames[i].name == sFrameName)
              return p.frames[i];
            }
        catch (e) {}
        finally {}
      if(p != p.parent)
        p = p.parent;
      else
        p = null;
    }
  return p;
}

function DateToXMLDate(aDate){
 var y, m, d, s="";
 y=aDate.getFullYear(); m=aDate.getMonth()+1; d=aDate.getDate();
 s=y.toString();
 if(m<10) s=s+'0'; s=s+m;
 if(d<10) s=s+'0'; s=s+d;
 return s;
}

function StringToDate(aString){
  var iY=1900, iM=1, iD=1, iH=0, iMin=0, iS=0;
  var aElements = sDateDisplayFormat.split(sDateSeparator);
  var aValues = aString.split(sDateSeparator);
  if(aElements.length == aValues.length){
    for(i=0; i< aElements.length; i++){
      if(isNaN(aValues[i])) return(null);
      if     (aElements[i]=="yy"  ) iY = ((parseInt(aValues[i],10) < 51)? 2000 : 1900) + parseInt(aValues[i], 10);
      else if(aElements[i]=="yyyy") iY =   parseInt(aValues[i],10);
      else if(aElements[i]=="m" || aElements[i]=="mm") iM = parseInt(aValues[i],10);
      else if(aElements[i]=="d" || aElements[i]=="dd") iD = parseInt(aValues[i],10);
    }
    if(! (isNaN(iY)||isNaN(iM)||isNaN(iD) || iM<1 || iM>12 || iD<1 || iD>32) )
      return( new Date(iY, iM-1, iD));
  }
  return(null);
}

function doChangeURLValue(sURL, sParamName, sNewValue){
  if(sURL.indexOf("?" + sParamName + "=") > -1)
    sParamName="?" + sParamName + "=";
  else if (sURL.indexOf("&" + sParamName + "=") > -1)
    sParamName="&" + sParamName + "=";
  else if(sNewValue != "")
    return(sURL + "&" + sParamName + "=" + sNewValue);
  else
    return(sURL);

  var s1, s2, p=sURL.indexOf(sParamName), l=sParamName.length;
  s1=sURL.substring(0, p);
  s2=sURL.substring(p+l, sURL.length);
  p=s2.indexOf("&");
  if(p>-1)
    s2=s2.substring(p, s2.length);
  else
    s2='';
  return(s1 + sParamName + sNewValue + s2);
}

function doGetURLParamValue(sURL, sParamName, sSep){
  if(!sSep) sSep = "&";
  if(sURL.indexOf("?" + sParamName + "=") > -1)
    sParamName = "?" + sParamName + "=";
  else if (sURL.indexOf(sSep + sParamName + "=") > -1)
    sParamName = sSep + sParamName + "=";
  else if (sURL.indexOf(sParamName + "=") == 0)
    sParamName = sParamName + "=";
  else return("");

  var s, p=sURL.indexOf(sParamName), l=sParamName.length;
  s = sURL.substring(p+l, sURL.length);
  p = s.indexOf(sSep);
  if(p > -1)
    s = s.substring(0, p);
  return(s);
}

function IntDiv(a, b){
  return Math.floor(a / b); 
}

function Power(n, m){
  if(!n || !m || isNaN(n) || isNaN(m))
    return null;
  var res = 1;
  for(var i=0; i<m; i++)
    res *= n;
  return res;    
}

function Hexa(i){
  var k;
  var Result = '';
  k = 16;
  do
    k *= 16;
  while(IntDiv(i, k) != 0);

  do {
    k = IntDiv(k, 16);
    var n = IntDiv(i, k);
    if(n < 10) Result += n;
    else
     switch(n){
      case 10 : Result += 'A'; break;
      case 11 : Result += 'B'; break;
      case 12 : Result += 'C'; break;
      case 13 : Result += 'D'; break;
      case 14 : Result += 'E'; break;
      case 15 : Result += 'F'; break;
     };
    i = i % k; 
  }
  while(k != 1);
  return Result;
}

function RGB(iR, iG, iB){
  return '#' + Hexa(iR) + Hexa(iG) + Hexa(iB);
}

function DelphiColorToJSColor(iColor){
  var sBGR = Hexa(iColor);
  var n = sBGR.length;   
  for(var i=n; i<6; i++)
    sBGR = "0" + sBGR;    
  return '#' + sBGR.substring(4,6) + sBGR.substring(2,4) + sBGR.substring(0,2);
}

function JSColorToDelphiColor(sRGB){
  if(!sRGB) return "";
  if(sRGB && sRGB.charAt(0) == '#')
    sRGB = sRGB.substring(1, sRGB.length);
  var sBGR = sRGB.substring(4,6) + sRGB.substring(2,4) + sRGB.substring(0,2);
  return parseInt(sBGR, 16);  
}

function getRandom(iNrFrom, iNrTo){   
  return parseInt(iNrFrom, 10) + Math.round(Math.random()*(iNrTo - iNrFrom)); 
}

function SetStyleInputBox(sName, bDisabled){
  var oTemp = document.getElementById(sName);
  if (oTemp) {
    oTemp.readOnly = bDisabled;
    oTemp.className =  (bDisabled ? "FMTextBoxDisabled" : "FMTextBox");
  }
}

function SetStyleCombo(sName, bDisabled){
  var oTemp = document.getElementById("str" + sName);
  if (!oTemp)
    oTemp = document.getElementById(sName);
  if (oTemp) {
    oTemp.disabled = bDisabled;
    oTemp.className =  (bDisabled ? "FMComboDisabled" : "FMCombo");
    oTemp = document.getElementById("btn" + sName);
    if (oTemp)
      oTemp.disabled = bDisabled;
  }
}

function SetStyleButton(sName, bDisabled){
  var oTemp = document.getElementById(sName);
  if (oTemp) {
    oTemp.disabled = bDisabled;
  }
}

/************************************************
*                 Utils -  END                  *
************************************************/

