AMPERSAND = '&';

function textCounter(field, counterid, maxlimit) {
    var counter = dojo.byId(counterid);
    if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);
        counter.innerHTML = 0;
    } else {
        counter.innerHTML = maxlimit - field.value.length;
    }
}


function ajaxLoad(id, url, callback) {
    // showAjaxLoader(id);
    dojo.xhrGet({
        url: url,
        handle: function(data) {
            
            // workaround for an IE innerHTML-Bug:
            var oldElement = dojo.byId(id);
            newElement = document.createElement(oldElement.tagName);
            
            newElement.id = oldElement.id;
            newElement.innerHTML = data;
            oldElement.parentNode.replaceChild(newElement, oldElement);
            
            if (callback) {
                callback();
            }
            
            //dojo.byId(id).innerHTML = data;
            gotoAnchor();
        },
        error: function(error) {
          alert(error);
        }
    });
}


/*
 * workaround for an IE innerHTML-Bug:
 */
function setInnerHtml(id, data) {
    var oldElement = dojo.byId(id);
    newElement = document.createElement(oldElement.tagName);
    newElement.id = oldElement.id;
    newElement.innerHTML = data;
    oldElement.parentNode.replaceChild(newElement, oldElement);
}


function ajaxLoadComments(id, url) {
    showAjaxLoader(id);
    dojo.xhrGet({
        url: url,
        handle: function(data) {
            if (data.match(/<empty\/>/)) {
              var container = dojo.byId(id);
              container.parentNode.removeChild(container);
            } else {
              setInnerHtml(id, data);
            }
            
            //dojo.byId(id).innerHTML = data;

            var element = dojo.byId('numberOfComments');
            if (element) {
              var nofComments = element.value;
              if (nofComments) {
                if (nofComments != 0) {
                  var suffix = nofComments == 1 ? '' : 's';
                
                  var linkId = 'nofComment' + suffix + 'Link';
                  var link = dojo.byId(linkId);
                  link.innerHTML = nofComments + ' ' + link.innerHTML;
                  link.style.display = 'inline';
                }
              }
            }
        }
    });
}


function submitForm(formid, targetid, captchaid, callback) {
    showAjaxLoader(targetid);
    dojo.byId(formid).style.display = 'none';
    var deferred = dojo.xhrPost({
        form: dojo.byId(formid),
        headers: {'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'},
        error: function(response) {
          alert(response);
        },
        handle: function(data) {
            dojo.byId(targetid).innerHTML = data;
            if (captchaid != '') {
                // trigger captcha reload:
                var captcha = dojo.byId(captchaid);
                if (captcha) {
                    captcha.src = captcha.src + "&date=" + (new Date()).getTime();
                }
            }
            if (callback) {
                callback();
            }
        }
    });
}


function showAjaxLoader(id) {
    var ajaxLoaderImg = dojo.byId('newsAjaxLoaderImg').cloneNode(true);
    dojo.byId(id).appendChild(ajaxLoaderImg);
    ajaxLoaderImg.style.display = 'block';
}


function fixObjectBlockWidth(id) {
    var div = document.getElementById(id);
    var width = 0;
    var img = div.getElementsByTagName('img')[0];
    if (img) {
       width = img.scrollWidth;
    } else {
        var obj = div.getElementsByTagName('object')[0];
        if (obj) {
            width = obj.scrollWidth;
        }
    }
    if (width != 0) {
        div.style.width = width;
    }
}

function gotoAnchor() {
  if (window.location.href.match(/#(\w.+)/)) window.location.replace(window.location.hash);
}

//really not important (the first two should be small for Opera's sake)
PositionX = 10;
PositionY = 10;
defaultWidth  = 600;
defaultHeight = 400;

var AutoClose = true;

function getInnerSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) === 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return { w: myWidth, h: myHeight };
}

function fitToSize(innerWidth) {
  var win = window;
  numInvokes += 1;
  if (numInvokes === 1) {
    return;
  }
  
  if (!document.images.length) {
    document.images[0] = document.layers[0].images[0];
  }
  var imgSize = {
      w: document.images[0].scrollWidth,
      h: document.images[0].scrollHeight
  };
  if( !imgSize.w || win.doneAlready ) {
    return;
  }
  
  var inner = getInnerSize();
  if (inner.w === 0 /* Chrome */ || inner.w === innerWidth /* Safari */) {
    setTimeout("fitToSize(" + innerWidth + ")", 100);
    return;
  }
  win.doneAlready = true;
  win.resizeBy(imgSize.w - inner.w, imgSize.h - inner.h);
  
  /*
  // center on screen
  var scW = screen.availWidth ? screen.availWidth : screen.width;
  var scH = screen.availHeight ? screen.availHeight : screen.height;
  if (!win.opera) {
    win.moveTo(Math.round((scW - inner.w)/2), Math.round((scH-inner.h)/2));
  }
  */
}

function popupImage(imageURL,imageTitle){
  var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
  if( !imgWin ) { return true; } //popup blockers should not cause errors
  imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
    'var numInvokes = 0;\n' +
    String(getInnerSize) +
    String(fitToSize) +
    '<\/script>'+
    '<\/head><body onload="fitToSize(' + window.innerWidth + ');"'+(AutoClose?' onblur="self.close();"':'')+'>'+
    (document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
    '<img src="'+imageURL+'" alt="Loading image ..." title="" onload="fitToSize(' + window.innerWidth + ');">'+
    (document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
  imgWin.document.close();
  if( imgWin.focus ) { imgWin.focus(); }
  return false;
}
