// JavaScript Document
if (self != top)
 	top.location.replace(location.href);

// get position of a element
// usage: var a = getPos(document.getElementById('some_div_id'));
// var xPosition = a.x;
// var yPosition = a.y;
function getPos(el) 
  {
  for (var lx=0, ly=0;
           el != null;
           lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
    return {x: lx,y: ly};
  }

// returns true if a object with the pId exists, false if not.
function fDivIdExists(pId)
  {
  var divIds = document.getElementsByTagName('div');
	for(i=0; i<divIds.length; i++)
	  {
	  divIdString = divIds[i].id;
	  if(divIdString == pId)
		  return true;
		}
	return false;	  
  }

function fStringExistsIn(pSearch, pText)
  {
  if(pText.indexOf(pSearch) != -1)
    return true;
  return false;
  }

function fGetInt(pValue)  // javascript's parseInt seems to act strangely sometimes.
  {
  buildNumber = '';
  for(i=0; i<pValue.length; i++)
    {
    var chr = pValue.substr(i,1);
    if(chr >= '0' && chr <= 9)
      buildNumber += chr;
    }
  return parseInt(buildNumber,10);
	} 

function fGetSelectedValue(pId)
  {
  return document.getElementById(pId).options[document.getElementById(pId).selectedIndex].value;
  }

function unixtime()
  {
  var date = new Date;
  return parseInt(date.getTime() / 1000);
  }


function fChangeImage(pName,pType)
  {
  document.images[pName].src = pType;
  }
  
function fGetQuery(name) {
  var value = "";
  if ( (i=location.search.indexOf('?'+name))==-1 && (i=location.search.indexOf('&'+name,3))==-1 )
    return '';
  else
    i += name.length+2;
  if ( (j=location.search.indexOf("&",i))!=-1 )
    value = location.search.substring(i,j);
  else
    value = location.search.substring(i,location.search.length);
  for ( i=0; i<value.length; i++ )
    if ( value.charAt(i)=='+' )
      value = value.substring(0,i)+' '+value.substring(i+1,value.length);

  return unescape(value);
}


function HideDIV(d) { document.getElementById(d).style.display = "none"; }
function DisplayDIV(d) { document.getElementById(d).style.display = "block"; }

function changeColor(id, color)
  {
  element = document.getElementById(id);
  element.style.background = color;
  }
  
var oAjaxProcessing = false;
var oAjaxQueue = new Array();
function fStoreAjaxRequest(strURL, fnPtr, params)
  {
  var queueObject = new Object();
  queueObject.strURL = strURL;
  queueObject.fnPtr = fnPtr;
  queueObject.params = params;
  oAjaxQueue[oAjaxQueue.length] = queueObject;
	}  
	
function fDebug(pMessage)
  {
  var el = document.getElementById('div_debug');
  el.innerHTML = el.innerHTML + "<br>" + pMessage + '<br><br>';
	}	
	
function fMakeXmlSafe(pText)
  {
  pText = str_replace('&','_amp_',pText);
  pText = str_replace("'",'_sq_',pText);
  pText = str_replace('"','_dq_',pText);
  pText = str_replace('=','_eq_',pText);
  pText = str_replace('<','',pText);
  pText = str_replace('>','',pText);
  return pText;
	}  	
	
function fProcessAdditionalAjaxRequests()
  {
  if(oAjaxQueue.length > 0)
    {
    var queueObject = oAjaxQueue.shift();
    xmlhttpPost(queueObject.strURL, queueObject.fnPtr, queueObject.params);
    }
  }

function xmlhttpPost(strURL, fnPtr, params) {
    if(oAjaxProcessing == true)
      {
      fStoreAjaxRequest(strURL, fnPtr, params);
      return;
      }
    oAjaxProcessing = true;
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            fnPtr(self.xmlHttpReq.responseXML);
            oAjaxProcessing = false;
            fProcessAdditionalAjaxRequests();
        }
    }
    self.xmlHttpReq.send(params);
    
}

function fGetSelectedRadioButtonValue(pId)
  {
  allInputs = document.getElementsByTagName("input");
  for (i = 0; i< allInputs.length; i++)
    {
    var input = allInputs[i];
    if(input.name == pId)
      {
      if(input.checked == true)
        return(input.value);
      }
    }
  return '';
  }

var launchWin;
function launchWindow(url, width, height, scrolling)
	{
	if(launchWin)
	  launchWin.close();
	screenX = (screen.width /2) - (width /2); // used to center.
	screenY = (screen.height /2) - (height /2); // used to center.
	  launchWin = window.open(url,"pop","toolbar=no,directories=no,menubar=no,location=yes,scrollbars=" +
          scrolling + ",resizable=yes,width=" + width + ",height=" + height + ",top=" + screenY +
          ",left=" + screenX + ",screenX=" + screenX + ",screenY=" + screenY);
	launchWin.focus();
	}

function str_replace (search, replace, subject, count) 
  {
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'

    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
  }
