var xmlHttp=null;
var url="";

function _O(oId){return document.getElementById(oId);};

function AJAX(ajax){
    if(ajax.showProgress)
        _O(ajax.spanId).innerHTML=ajax.onProgress;
    xmlHttp=Get_xmlhttp();
    url=url_prefix+ajax.url+"&method="+ajax.method;
    xmlHttp.open(ajax.type,url,ajax.async);
    if(ajax.type=="POST")xmlHttp.send(ajax.txt);else xmlHttp.send(null);
    xmlHttp.onreadystatechange=function(){
        if(xmlHttp.readyState==4){
            if(ajax.cb){
                eval(ajax.cb);
                return;
            }
            if(ajax.message!=""&&xmlHttp.responseText=="true"){
                _O(ajax.spanId).innerHTML=ajax.message;
            }
            if(ajax.message!=""&&xmlHttp.responseText=="false"){
                _O(ajax.spanId).innerHTML=ajax.falseMessage;
            }
            if(ajax.printAsIs){
                _O(ajax.spanId).innerHTML=xmlHttp.responseText;
            }
            if(ajax.addScript){
                var jfi=document.createElement('script');
                jfi.setAttribute("type","text/javascript");
                jfi.setAttribute("id",ajax.tagId);
                document.getElementsByTagName("head")[0].appendChild(jfi);
                if(document.getElementById(ajax.tagId))
                    document.getElementById(ajax.tagId).innerHTML=xmlHttp.responseText;
            }
            if(ajax.RemoveHtml){
                _O(ajax.spanId).innerHTML='';
            }
            if(ajax.addRowId){
                _O(ajax.addRowId).innerHTML+=xmlHttp.responseText;
            }
            if(ajax.hideId&&xmlHttp.responseText!=""){
                _O(ajax.hideId).style.visibility='hidden';
            }
            if(xmlHttp.responseText=="true"&&ajax.redirect){
                window.location.href=ajax.redirectUrl;
            }
        }
    };
};

function Get_xmlhttp(){
    if (window.XMLHttpRequest){
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    else{
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
};


var TimeToFade = 500.0;
function fade(eid){
    var element = _O(eid);
    if(element == null)
        return;

    if(element.FadeState == null){
        if(element.style.opacity == null || element.style.opacity == '' || element.style.opacity == '1'){
            element.FadeState = 2;
        }
        else{
            element.FadeState = -2;
        }
    }

    if(element.FadeState == 1 || element.FadeState == -1){
        element.FadeState = element.FadeState == 1 ? -1 : 1;
        element.FadeTimeLeft = TimeToFade - element.FadeTimeLeft;
    }
    else{
        element.FadeState = element.FadeState == 2 ? -1 : 1;
        element.FadeTimeLeft = TimeToFade;
        setTimeout("animateFade(" + new Date().getTime() + ",'" + eid + "')", 33);
    }
};

function animateFade(lastTick, eid){
    var curTick = new Date().getTime();
    var elapsedTicks = curTick - lastTick;

    var element = _O(eid);

    if(element.FadeTimeLeft <= elapsedTicks){
        element.style.opacity = element.FadeState == 1 ? '1' : '0';
        element.style.filter = 'alpha(opacity = ' + (element.FadeState == 1 ? '100' : '0') + ')';
        element.FadeState = element.FadeState == 1 ? 2 : -2;
        return;
    }

    element.FadeTimeLeft -= elapsedTicks;
    var newOpVal = element.FadeTimeLeft/TimeToFade;
    if(element.FadeState == 1)
        newOpVal = 1 - newOpVal;

    element.style.opacity = newOpVal;
    element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';
    setTimeout("animateFade(" + curTick + ",'" + eid + "')", 33);
};

function ObjectPosition(obj){
    var curleft = 0;
      var curtop = 0;
      if (obj.offsetParent) {
            do {
                  curleft += obj.offsetLeft;
                  curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
      }
      return [curleft,curtop];
};

function ShowImage(divId,divInner,w,h,src){
    grayOut(true, {'zindex':'50', 'bgcolor':'black', 'opacity':'95'});
    LeftPosition=(screen.width)?(screen.width-w)/2:0;
    TopPosition=(screen.height)?(screen.height-h)/2:0;
    _O(divId).style.left=LeftPosition+"px";
    _O(divId).style.top=TopPosition+"px";
//    _O(divId).style.width=w+"px";
//    _O(divId).style.height=h+"px";
    _O(divId).style.visibility='visible';
    _O(divId).style.zIndex=100;
    _O(divInner).innerHTML="<iframe src='"+src+"'></iframe>";
};

function OpenNewLayer(divId,w,h){
    grayOut(true);
    LeftPosition=(screen.width)?(screen.width-w)/2:0;
    TopPosition=(screen.height)?(screen.height-h)/2:0;
    _O(divId).style.left=LeftPosition+"px";
    _O(divId).style.top=TopPosition+"px";
    _O(divId).style.width=w+"px";
    _O(divId).style.height=h+"px";
    _O(divId).style.visibility='visible';
    _O(divId).style.zIndex=100;
};

function grayOut(vis, options){
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 140;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=_O('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=_O('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
};

function getScrollXY(){
    var scrOfX = 0, scrOfY = 0;
    if(typeof(window.pageYOffset)=='number'){
        //Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    }
    else if(document.body && (document.body.scrollLeft || document.body.scrollTop )){
        //DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    }
    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }
    return[scrOfX,scrOfY];
};

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
};

function setTop(element,t){
    element = $(element);
    element.style.top = t +'px';
};

function setLeft(element,t){
    element = $(element);
    element.style.left = t +'px';
};
