/************************************************************************************************************
(C) www.dhtmlgoodies.com, June 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

var dynamicContent_ajaxObjects = new Array(); 
var jsCache = new Array();
var enableCache = true; 

function ajax_loadContent(divId,pathToFile,premsg)
{
  if(enableCache && jsCache[pathToFile]){
    document.getElementById(divId).innerHTML = jsCache[pathToFile];
    return;
  }

  if(premsg == undefined){
   premsg = 'Loading content...';
  }
  
  var ajaxIndex = dynamicContent_ajaxObjects.length;
  document.getElementById(divId).innerHTML = premsg;
  dynamicContent_ajaxObjects[ajaxIndex] = new sack();
  dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;

  dynamicContent_ajaxObjects[ajaxIndex].onCompletion = 
  function(){ ajax_showContent(divId,ajaxIndex,pathToFile); };  

  dynamicContent_ajaxObjects[ajaxIndex].runAJAX();  
  
  
}

function ajax_loadContentOpacity(divId,pathToFile,opacity)
{
  if(enableCache && jsCache[pathToFile]){
    document.getElementById(divId).innerHTML = jsCache[pathToFile];
    return;
  }

  if(opacity == undefined){
   opacity = 50;
  }

  showAjaxLoader(divId); //show loader anim in centet of target div

  var ajaxIndex = dynamicContent_ajaxObjects.length;

  showAjaxOpacity(divId,opacity);



  dynamicContent_ajaxObjects[ajaxIndex] = new sack();
  dynamicContent_ajaxObjects[ajaxIndex].requestFile = pathToFile;

  dynamicContent_ajaxObjects[ajaxIndex].onCompletion =
  function(){ ajax_showContentOpacity(divId,ajaxIndex,pathToFile); };

  dynamicContent_ajaxObjects[ajaxIndex].runAJAX();


}
 

function ajax_showContent(divId,ajaxIndex,pathToFile)
{
  document.getElementById(divId).innerHTML =
    dynamicContent_ajaxObjects[ajaxIndex].response;
  if(enableCache){
    jsCache[pathToFile] = 
    dynamicContent_ajaxObjects[ajaxIndex].response;
  }
  dynamicContent_ajaxObjects[ajaxIndex] = false;
}

function ajax_showContentOpacity(divId,ajaxIndex,pathToFile)
{
  document.getElementById(divId).innerHTML =
    dynamicContent_ajaxObjects[ajaxIndex].response;
  if(enableCache){
    jsCache[pathToFile] =
    dynamicContent_ajaxObjects[ajaxIndex].response;
  }
  dynamicContent_ajaxObjects[ajaxIndex] = false;

  closeAjaxOpacity()

  closeAjaxLoader();

}


function showAjaxOpacity(tgt,opacity){
        x = findPosX(tgt);
        y = findPosY(tgt);

        w = document.getElementById(tgt).offsetWidth;
        h = document.getElementById(tgt).offsetHeight;

        uid = 'ajaxopacity-xaetad';
        mCreateDiv(uid);
	mDivBackgroundColor(uid,'#ffffff');

	obj = document.getElementById(uid);
  	obj.style.filter = "alpha(opacity:"+opacity+")";
  	obj.style.KHTMLOpacity = opacity/100;
  	obj.style.MozOpacity = opacity/100;
  	obj.style.opacity = opacity/100;

        mDivSize(uid,w,h);
        mDivPos(uid,y,x,'absolute');
}

function closeAjaxOpacity(){
mRemoveDiv('ajaxopacity-xaetad')
}


function showAjaxLoader(tgt){
	x = findPosX(tgt);
	y = findPosY(tgt);

	w = document.getElementById(tgt).offsetWidth;
	h = document.getElementById(tgt).offsetHeight;

	//work out center point
	cw = parseInt(document.getElementById(tgt).offsetWidth) /2;
	ch = parseInt(document.getElementById(tgt).offsetHeight) /2
	cw = parseInt(cw);
	ch = parseInt(ch);

	//WORK OUT WHERE TO SHOW THE LOADER
	tx = x + cw;
	ty = y + ch;

        uid = 'ajaxanim-xaetad';
        mCreateDiv(uid);
        mDivSize(uid,32,32);
	mDivPos(uid,ty,tx,'absolute');	
	mDivInnerHTML(uid,'<img src="/gfx/circle-loader.gif"/>');
}

function closeAjaxLoader(){
        mRemoveDiv('ajaxanim-xaetad');
}




