
// ******************************************************************************************************************************
// Global Routines
// ******************************************************************************************************************************

var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var isVersion = (navigator.appVersion);

function objFind(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=objFind(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function formSubmit(sFrm) {
  oFrm = objFind(sFrm);
  if (oFrm) oFrm.submit();
}

function utilsRandom(iMin,iMax) {
  iMin = (iMin) ? iMin : 1000;
  iMax = (iMax) ? iMax : 9999;
  return Math.floor(Math.random()*iMax) + iMin;
}

// ******************************************************************************************************************************
// Div Position
// ******************************************************************************************************************************

function posObjSet(oObj,e,sObj,iXAdd,iYAdd) {
  if (sObj) var oObj = objFind(sObj);
  if (oObj) {
    if (!iXAdd) iXAdd = 0;
    if (!iYAdd) iYAdd = 0;
    if (!(document.all)) {
      iX = e.layerX + iXAdd;
      iY = e.layerY + iYAdd;
    } else {
      iX = e.x + iXAdd;
      iY = e.y + iYAdd;
    }
    posObjSetDirect(oObj,iX,iY);
  }
}

function posObjSetCenter(oObj,iWid,iHei,iXAdd,iYAdd) {
  aRet = windowSizeGet();
  if (!iWid)  iWid = 0;
  if (!iHei)  iHei = 0;
  if (!iXAdd) iXAdd = 0;
  if (!iYAdd) iYAdd = 0;
  iX   = (aRet[0]/2) - ((iWid > 0) ? (iWid/2) : 0);
  iY   = (aRet[1]/2) - ((iHei > 0) ? (iHei/2) : 0);
  if (oObj) posObjSetDirect(oObj,iX+iXAdd,iY+iYAdd);
}

function posObjSetDirect(oObj,iX,iY) {
  if (!(document.all)) {
    oObj.style.left = iX + 'px';
    oObj.style.top  = iY + 'px';
  } else {
    oObj.style.pixelLeft = iX;
    oObj.style.pixelTop  = iY;
  }
}

// ******************************************************************************************************************************
// Window Position
// ******************************************************************************************************************************

function windowSizeGet() {
  var iWid = 0;
  var iHei = 0;

  if( typeof( window.innerWidth ) == 'number' ) {
    iWid = window.innerWidth;
    iHei = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    iWid = document.documentElement.clientWidth;
    iHei = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    iWid = document.body.clientWidth;
    iHei = document.body.clientHeight;
  }
  return [iWid,iHei];
}

var windowState = (function() {
  var readScroll = {scrollLeft:0,scrollTop:0};
  var readSize = {clientWidth:0,clientHeight:0};
  var readScrollX = 'scrollLeft';
  var readScrollY = 'scrollTop';
  var readWidth = 'clientWidth';
  var readHeight = 'clientHeight';

  function otherWindowTest(obj) {
    if ((document.compatMode) && (document.compatMode == 'CSS1Compat') && (document.documentElement)) {
      return document.documentElement;
    } else if(document.body) {
      return document.body;
    } else {
      return obj;
    }
  };

  if ((typeof this.innerHeight == 'number') && (typeof this.innerWidth == 'number')) {
    readSize = this;
    readWidth = 'innerWidth';
    readHeight = 'innerHeight';
  } else {
    readSize = otherWindowTest(readSize);
  }

  if ((typeof this.pageYOffset == 'number') && (typeof this.pageXOffset == 'number')) {
    readScroll = this;
    readScrollY = 'pageYOffset';
    readScrollX = 'pageXOffset';
  } else {
    readScroll = otherWindowTest(readScroll);
  }

  return {
    getScrollX:function() {
      return (readScroll[readScrollX]||0);
    },
    getScrollY:function() {
      return (readScroll[readScrollY]||0);
    },
    getWidth:function() {
      return (readSize[readWidth]||0);
    },
    getHeight:function() {
      return (readSize[readHeight]||0);
    }
  };
})();

// ******************************************************************************************************************************
// Div Drag
// ******************************************************************************************************************************

var sDragName = "";
var oDragDiv  = null;
var bDragDown = false;
var bDragOver = false;
var iDragPosX = 0;
var iDragPosY = 0;

function dragObjMouseUp() {
  if (oDragDiv) oDragDiv.style.zIndex = 100;
  oDragDiv  = null;
  bDragDown = false;
  bDragOver = false;
  iDragPosX = 0;
  iDragPosY = 0;
}

function dragObjMouseDown(e) {
  bDragDown = true;
  if (bDragOver) {
    oDragDiv = objFind(sDragName);
    oDragDiv.style.zIndex = 101;
    if (!(document.all)) {
      iDragPosX = e.layerX;
      iDragPosY = e.layerY;
      return false;
    } else {
      iDragPosX = event.offsetX;
      iDragPosY = event.offsetY;
    }
  }
}

function dragObjMouseMove(e) {
  if (!bDragDown) return false;

  if (oDragDiv && bDragOver) {
    oDragDiv.style.zIndex = 101;
    window.status=sDragName;
    if (!(document.all)) {
      oDragDiv.style.top = (e.pageY-iDragPosY) + 'px';
      oDragDiv.style.left = (e.pageX-iDragPosX) + 'px';
      return false;
    } else {
      oDragDiv.style.pixelLeft = event.clientX-iDragPosX + document.body.scrollLeft;
      oDragDiv.style.pixelTop = event.clientY-iDragPosY + document.body.scrollTop;
      return false;
    }
  }
}

function dragObjStart(sObj) {
  sDragName = sObj;
  oDragDiv  = objFind(sDragName);
  oDragDiv.style.display = "block";
  if (!(document.all)) document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
  document.onmousedown = dragObjMouseDown;
  document.onmousemove = dragObjMouseMove;
  document.onmouseup = dragObjMouseUp;
}

function dragObjStop(sDragName) {
  if (sDragName != "") {
    oDragDiv = objFind(sDragName);
    if (oDragDiv) {
      oDragDiv.style.zIndex = 100;
      oDragDiv.style.display = "none";
    }
  }
  sDragName = "";
  dragObjMouseUp();
}

// ******************************************************************************************************************************
// Input Routines
// ******************************************************************************************************************************

function inputFormatAmount(oObj,sFor) {
  var sVal = oObj.value;
  var sRet = sVal;
  var sSta = '';
  var sEnd = '';

  if (sFor == "PER") {
    sVal = sVal.replace(',',".");
    if (inStrBln(sVal,'.') == false) sVal += '.00';
    if (sVal.substr(0,1) == ".") sVal = "0"+sVal;
    aVal = sVal.split('.');
    for (i=0; i<(aVal.length-1); i++) sSta += (''+aVal[i]);
    sEnd = aVal[aVal.length-1];
    sRet = sSta+'.'+sEnd;
  } else {
    sVal = sVal.replace('.',",");
    if (inStrBln(sVal,',') == false) sVal += ',00';
    if (sVal.substr(0,1) == ",") sVal = "0"+sVal;
    aVal = sVal.split(',');
    for (i=0; i<(aVal.length-1); i++) sSta += (''+aVal[i]);
    sEnd = aVal[aVal.length-1];
    sRet = sSta+','+sEnd;
  }

  oObj.value = sRet;
}

// ******************************************************************************************************************************
// Selectbox Routines
// ******************************************************************************************************************************

function selectShow(sObj,bShow) {
  oInp = objFind(sObj+'-value');

  if (bShow) {
    for (i=1; i<20; i++) {
      oItm = objFind(sObj+'-'+i);
      if (oItm) {
        oItm.className = (oItm.innerHTML != oInp.value) ? 'select option' : 'select option-sel';
      } else {
        break;
      }
    }
  }

  oBox = objFind(sObj+'-box');
  oBox.className = (bShow) ? 'select box' : 'select box hide';
}

function selectSet(sObj,oItm,sFunction) {
  oInp = objFind(sObj+'-value');
  oCap = objFind(sObj+'-caption');
  oInp.value = oItm.innerHTML;
  oCap.innerHTML = oItm.innerHTML;
  selectShow(sObj, false);
  if (sFunction) eval(sFunction);
}


function selectShow(sName,bShow) {
  oObj = objFind(sName + '-select');
  oInp = objFind(sName + '-val');
  oCap = objFind(sName + '-cap');

  if (bShow) {
    for (i=0; i<oObj.options.length; i++) {
      oObj.options[i].selected = (oObj.options[i].value == oInp.value);
    }
  }
  oObj.className = (bShow) ? "show" : "hide";
  oCap.className = (bShow) ? "hide" : "show";
}

function selectPick(sName,oObj,sFunction) {
  selectShow(sName,false);

  oCap = objFind(sName + '-cap');
  oInp = objFind(sName + '-val');
  sSel = oObj.options[oObj.selectedIndex].value;

  if (sSel != oInp.value) {
    oCap.innerHTML = sSel;
    oInp.value = sSel;
    if (sFunction) eval(sFunction);
  }
}

// ******************************************************************************************************************************
// Row Routines
// ******************************************************************************************************************************

var iRowSelected = 0;

function mainRowSelected(iRow,sFunc) {
  if (iRowSelected > 0) styleRowState(objFind('row-'+iRowSelected),3);
  iRowSelected = iRow;
  if (iRowSelected > 0) styleRowState(objFind('row-'+iRowSelected),2);
  eval(sFunc);
}

function mainRowGet() {
  var iRow = (iRowSelected < 1) ? 1 : iRowSelected;
  var oRow = objFind('row-'+iRow);
  var aRet = new Array();

  if (oRow) {
    var aEle = oRow.getElementsByTagName('td')
    var aRet = new Array();
    var iRet = 0;

    for (i=0; i<aEle.length; i++) {
      iRet++;
      aRet[iRet] = (iRowSelected > 0) ? aEle[i].innerHTML : "";
    }
  }

  return aRet;
}

// ******************************************************************************************************************************
// Scroll Routines
// ******************************************************************************************************************************

var iScrollSteps = 25;

function scrollInit() {
  var aLinks = document.getElementsByTagName('a');
  for (var i=0;i<aLinks.length;i++) {
    var oLnk = aLinks[i];
    if ((oLnk.href && oLnk.href.indexOf('#') != -1) && ( (oLnk.pathname == location.pathname) || ('/'+oLnk.pathname == location.pathname) ) && (oLnk.search == location.search)) {
      scrollAddEvent(oLnk,'click',scrollScroll);
    }
  }
}

function scrollScroll(e) {
  if (window.event) {
    target = window.event.srcElement;
  } else if (e) {
    target = e.target;
  } else return;

  if (target.nodeName.toLowerCase() != 'a') target = target.parentNode;
  if (target.nodeName.toLowerCase() != 'a') return;

  anchor = target.hash.substr(1);
  var aLinks = document.getElementsByTagName('a');
  var oLink  = null;
  for (var i=0;i<aLinks.length;i++) {
    var oLnk = aLinks[i];
    if (oLnk.name && (oLnk.name == anchor)) {
      oLink = oLnk;
      break;
    }
  }
  if (!oLink) oLink = document.getElementById(anchor);
  if (!oLink) return true;

  var iDestX = oLink.offsetLeft;
  var iDestY = oLink.offsetTop;
  var oNode = oLink;
  while (oNode.offsetParent && (oNode.offsetParent != document.body)) {
    oNode   = oNode.offsetParent;
    iDestX += oNode.offsetLeft;
    iDestY += oNode.offsetTop;
  }

  clearInterval(iScrollInterval);

  var iPos  = scrollCurrentYPos();
  var iStep = parseInt((iDestY-iPos)/iScrollSteps);
  iScrollInterval = setInterval('scrollWindow('+iStep+','+iDestY+',"'+anchor+'")',10);

  if (window.event) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (e && e.preventDefault && e.stopPropagation) {
    e.preventDefault();
    e.stopPropagation();
  }
}

function scrollWindow(iAmount,iDest,anchor) {
  var iCur        = scrollCurrentYPos();
  var bIsAbove    = (iCur < iDest);
  window.scrollTo(0,iCur + iAmount);
  var iCurNow     = scrollCurrentYPos();
  var bIsAboveNow = (iCurNow < iDest);
  if ((bIsAbove != bIsAboveNow) || (iCur == iCurNow)) {
    window.scrollTo(0,iDest);
    clearInterval(iScrollInterval);
    location.hash = anchor;
  }
}

function scrollCurrentYPos() {
  if (document.body && document.body.scrollTop) return document.body.scrollTop;
  if (document.documentElement && document.documentElement.scrollTop) return document.documentElement.scrollTop;
  if (window.pageYOffset) return window.pageYOffset;
  return 0;
}

function scrollAddEvent(oEle, sType, sFunction, bCapture) {
  if (oEle.addEventListener){
    oEle.addEventListener(sType, sFunction, bCapture);
    return true;
  } else if (oEle.attachEvent){
    return oEle.attachEvent("on"+sType, sFunction);
  } else {
    // alert("Handler could not be removed");
  }
}

// ******************************************************************************************************************************
// Status Routines
// ******************************************************************************************************************************

function mainStatus(sObj,sTxt) {
  oObj = objFind(sObj);
  if (oObj) oObj.innerHTML = sTxt;
}

// ******************************************************************************************************************************
// File Routines
// ******************************************************************************************************************************

function fileAction(sAct,sFile) {
  if (sAct == 'download') {
    if (sFile) sRowFile = sFile;
    if (sRowFile != "") windowOpenCentered("/download.php?file="+sRowFile,"win_download");
  } else {
    var oAct = objFind('act-file');
    oAct.value = sAct;
  }
}

// ******************************************************************************************************************************
// Date Routines
// ******************************************************************************************************************************

function date_stamp(iAdd) {
  if (!iAdd) iAdd = 0;
  dDat = new Date();
  return ((dDat.getTime()-dDat.getMilliseconds())/1000)+iAdd;
}

function date_format(sFor,iUnix) {
  if (!sFor)  sFor = 'HH:II DD-MM-YYYY';
  if (!iUnix) iUnix = date_stamp(0);
  if(iUnix<10000000000) iUnix *= 1000;

  var sRet = '';
  try {
    var dDat = new Date();
    dDat.setTime(iUnix);

    sRet = sFor;
    sRet = sRet.replace(/HH/,formatNumber(dDat.getHours(),'00'));
    sRet = sRet.replace(/H/,dDat.getHours(),'0');
    sRet = sRet.replace(/II/,formatNumber(dDat.getMinutes(),'00'));
    sRet = sRet.replace(/I/,dDat.getMinutes(),'0');
    sRet = sRet.replace(/SS/,formatNumber(dDat.getSeconds(),'00'));
    sRet = sRet.replace(/S/,dDat.getSeconds(),'0');
    sRet = sRet.replace(/DD/,formatNumber(dDat.getDate(),'00'));
    sRet = sRet.replace(/D/,dDat.getDate()+1,'0');
    sRet = sRet.replace(/MM/,formatNumber(dDat.getMonth()+1,'00'));
    sRet = sRet.replace(/M/,dDat.getMonth(),'0');
    sRet = sRet.replace(/YYYY/,dDat.getFullYear());
    sRet = sRet.replace(/YY/,dDat.getFullYear().substr(2));
  } catch(e) {}

  return sRet;
}

// ******************************************************************************************************************************
// Cookie Routines
// ******************************************************************************************************************************

function cookieCreate(sName,sValue,iHours) {
  if (!iHours) iHours=24;
  var dDate = new Date();
  dDate.setTime(dDate.getTime()+(iHours*60*60*1000));
  var sExpires = "; expires="+dDate.toGMTString();
  document.cookie = sName+"="+sValue+sExpires+"; path=/";
}

function cookieRead(sName) {
  var sNameEQ = sName + "=";
  var aCookie = document.cookie.split(';');
  for(var i=0;i < aCookie.length;i++) {
    var sCookie = aCookie[i];
    while (sCookie.charAt(0)==' ') sCookie = sCookie.substring(1,sCookie.length);
    if (sCookie.indexOf(sNameEQ) == 0) return sCookie.substring(sNameEQ.length,sCookie.length);
  }
  return null;
}

function cookieErase(sName) {
  cookieCreate(sName,"",-1);
}

// ******************************************************************************************************************************
// Window Routines
// ******************************************************************************************************************************

function windowOpenCentered(sUrl,sNam,iWid,iHei) {
   var sNam = (sNam) ? sNam : "_remote";
   if (iWid) {
     var iLef = Math.ceil( (window.screen.width  - iWid) / 2 );
     var iTop = Math.ceil( (window.screen.height - iHei) / 2 );

     var sSpc ='toolbar=no,location=no,directories=no,menubar=no,'
         sSpc+='scrollbars=yes,resizable=yes,'
         sSpc+='width='+iWid+',height='+iHei;
     if (isIE) {
       sSpc +=',left='+iLef+',top='+iTop;
     } else {
       sSpc+=',screenX='+iLef+',screenY='+iTop;
     }
     return window.open(sUrl,sNam,sSpc);
   } else {
     return window.open(sUrl,sNam);
   }
}

// ********************************************************************************************************************
// Global String Routines
// ********************************************************************************************************************

function trim(sText) {
  return (typeof sText == 'string' && sText != '' && isNaN(sText)) ? sText.replace( /\s+$/g, "" ) : sText;
}

function inStr(sText,sNeedle) {
  var iRet = -1;
  if (sText) iRet = sText.indexOf(sNeedle);
  return iRet;
}

function inStrBln(sText,sNeedle) {
  return (inStr(sText,sNeedle) > -1);
}

if (typeof String.prototype.replaceAll === 'undefined') {
  String.prototype.replaceAll = function (sTarget,sSubString) {
    var sText = this;
    var iIndexOfMatch = sText.indexOf(sTarget);
    while (iIndexOfMatch != -1) {
      sText = sText.replace(sTarget,sSubString);
      iIndexOfMatch = sText.indexOf(sTarget);
    }
    return sText;
  }
}

function encode(sText) {
  var sUtf = "";

  sText = sText.replace(/\r\n/g,"\n");

  for (i=0; i<sText.length; i++) {
    var iChr = sText.charCodeAt(i);

    if (iChr < 128) {
      sUtf += String.fromCharCode(iChr);
    } else if((iChr > 127) && (iChr < 2048)) {
      sUtf += String.fromCharCode((iChr >> 6) | 192);
      sUtf += String.fromCharCode((iChr & 63) | 128);
    } else {
      sUtrf += String.fromCharCode((iChr >> 12) | 224);
      sUtrf += String.fromCharCode(((iChr >> 6) & 63) | 128);
      sUtrf += String.fromCharCode((iChr & 63) | 128);
    }
  }

  return escape(sUtf);
}

function decode(sText) {
  if (sText != "") {
    var sUtf = "";
    var iChr = c1 = c2 = 0;
    var i = 0;

    sText = unescape(sText);

    while (i < sText.length ) {
      iChr = sText.charCodeAt(i);

      if (iChr < 128) {
        sUtf += String.fromCharCode(iChr);
        i++;
      } else if((iChr > 191) && (iChr < 224)) {
        c2 = sText.charCodeAt(i+1);
        sUtf += String.fromCharCode(((iChr & 31) << 6) | (c2 & 63));
        i += 2;
      } else {
        c2 = sUtrf.charCodeAt(i+1);
        c3 = sUtrf.charCodeAt(i+2);
        sUtf += String.fromCharCode(((iChr & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
    }

    return sUtf;
  } else {
    return "";
  }
}

function decodeToArray(sText) {
  var aRet = new Array();

  if (inStrBln(sText,'=')) {
    if (inStrBln(sText,'&')) {
      var aText = sText.split('&');
      for (i=0; i<aText.length; i++) {
        var aItm = aText[i].split('=');
        aRet[""+aItm[0]] = decode(aItm[1]);
      }
    } else {
      var aItm = sText.split('=');
      aRet["'"+aItm[0]+"'"] = decode(aItm[1]);
    }
  }

  return aRet;
}

function formatNumber(iNum,sFormat) {
  if (!sFormat) sFormat='00'
  iNum = parseInt(iNum);
  sRet = sFormat+(iNum);
  return (iNum.length > sFormat.length) ? iNum : sRet.substr(sRet.length-sFormat.length);
}

// ******************************************************************************************************************************
// Init Routines
// ******************************************************************************************************************************

function init() {
  if (typeof partnersShow == 'function') partnersShow();
}

// scrollAddEvent(window,"load",scrollInit);
