﻿// FireBug Console Workaraound
if (!window.console || !console.firebug){
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}
        

function purge(d,description){  
    //console.log(description);
    //console.time("purge");
    var a = d.attributes, i, l, n;    
    if (a) {        
        l = a.length;        
        for (i = 0; i < l; i += 1) {            
            n = a[i].name;            
            if (typeof d[n] === 'function') {                
                d[n] = null;            
            }        
        }    
    }    
    a = d.childNodes;    
    if (a) {        
        l = a.length;        
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i],"--> " + description);        
        }    
    }
    //console.timeEnd("purge");
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num + '.' + cents);
    //return (((sign)?'':'-') + '$' + num);
}

function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function setDropDownValue(elDropDown,value){
   for (var i = 0 ; i < elDropDown.options.length ; i++){
        if (elDropDown.options[i].value == value){
            elDropDown.selectedIndex = i
        }
    }
}

/*
$$('#mylink').each(function(link) {
  link.onclick = function(){
    window.open(this.href,'name','options');
    return false;
  };
});
*/

function floatDiv(elID, sx, sy)
{
     var ParentBottom;
	 var ElementBottom; 
	 var bottomGap;
	    
    var ns = (navigator.appName.indexOf("Netscape") != -1);
	var el= $(elID);
	var parentEl = el.ancestors()[0];
	var px = document.layers ? "" : "px";
	window[elID + "_obj"] = el;
	if(document.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};

	el.floatIt=function()
	{   
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		
		ParentBottom = Position.cumulativeOffset(parentEl)[1] + parentEl.getHeight()
		ElementBottom = Position.cumulativeOffset(el)[1] + el.getHeight()
		bottomGap = ParentBottom - ElementBottom
		//console.log("bottomGap",bottomGap);
		
		//if ((bottomGap >= 0) || ((this.cy + el.getHeight()) < ParentBottom)){
		    this.cx += (pX + this.sx - this.cx)/8;
		    this.cy += (pY + this.sy - this.cy)/8;
		    //console.log("cx: " + this.cx,"sx: " + this.sx + " pX: " + pX);
		    //console.log("cy: " + this.cy,"sy: " + this.sy + " pY: " + pY);
		    this.sP(this.cx, this.cy);
		    setTimeout(this.id + "_obj.floatIt()", 0);
		//}
		//console.log("pX" + pX,"pY: " + pY);
		//console.log("left: " + el.style.left,"top: " + el.style.top + el.getHeight());				
	}
	return el;
}

function scrollPos(){
    var ScrollTop = document.body.scrollTop;
    if (ScrollTop == 0)
    {
        if (window.pageYOffset)
            ScrollTop = window.pageYOffset;
        else
            ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
    }
    return ScrollTop;
}

function ConstrainedVerticalFloat(elID,elContainer){
    console.log("ConstrainedVerticalFloat");
    var elFloat = $(elID);
    var elContainer = $(elContainer);
    
    //Required to Make Floating Slider Work.
    Position.absolutize(elFloat);
    
    //Section Below Required to Reset Containers Absolute Height to prevent slippage above the cieling
    var listingHeight = elFloat.getHeight();
    if (elContainer.getHeight() < elFloat.getHeight()){
        elContainer.style.height =  (elFloat.getHeight() + 40) + "px;";
    }
    
    var ceiling = Position.cumulativeOffset(elContainer)[1];
    var floor = ceiling + elContainer.getHeight()
    
    
    Event.observe(window, 'scroll', function(){VerticalFloat(elFloat,elContainer,ceiling,floor)});
}

function VerticalFloat(elFloat,elContainer,ceiling,floor){
    var px = document.layers ? "" : "px";
    var offset = scrollPos();
    var floatCeiling = Position.cumulativeOffset(elFloat)[1];
    var floatFloor = floatCeiling + elFloat.getHeight();
    
    if ((floatFloor < floor) && (offset > ceiling)){
        elFloat.style.top = (offset)+px;
    }else if ((offset <  floatCeiling) && (offset > ceiling)){
        elFloat.style.top = (offset)+px;
    }else if (floatFloor > floor){
        elFloat.style.top = (floor - elFloat.getHeight())+px;
    }
    
}

function ConstrainedFloatDiv(elID,elContainer, startX, startY){
    var elContainer = $(elContainer);
    
    startX = startX + Position.cumulativeOffset(elContainer)[0];
    startY = startY + Position.cumulativeOffset(elContainer)[1];
	    
    var ns = (navigator.appName.indexOf("Netscape") != -1);
    var el= $(elID);
    var parentEl = el.ancestors()[0];
    var px = document.layers ? "" : "px";
    
    var containerBottom = Position.cumulativeOffset(elContainer)[1] + elContainer.getHeight()
    
    var maxY = containerBottom - elContainer.getHeight();
    
    window[elID + "_obj"] = el;
    if(document.layers){el.style=el};
    
    el.changeX = el.startX = startX;
    el.changeY = el.startY = startY;
    
    //console.log("changeX: " + el.changeX,"changeY: " + el.changeY);
    
    el.sP=function(x,y){
        var thisBottom = Position.cumulativeOffset(this)[1] + this.getHeight();
        var Differential = this.getHeight();
        
        var TopShift = (el.changeY - startY) - startY;
        //console.log("scrollPos",scrollPos());
        
        y = y - TopShift;
   
        //console.log("thisBottom: " + thisBottom,"containerBottom: " + containerBottom);
        //console.log("scrollPos",scrollPos());
        //console.log("y: " + y,"MaxY: " + maxY);
        
        var newY = startY;
        if ((thisBottom  <= containerBottom) && (scrollPos() > startY)){
            newY = startY + TopShift;
        }else if (thisBottom  > containerBottom){
            this.style.left=x+px;this.style.top= (thisBottom - Differential)+px;
        }
        
            //console.log("ready")
            //if (TopShift > 0){
                //console.log("go")
               // console.log("startY + TopShift",startY + TopShift);
                this.style.left=x+px;this.style.top= newY+px;
           // }
        
    };

    el.floatIt=function()
    {
        var posX, posY;
        posX = (this.startX >= 0) ? 0 : ns ? innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
        posY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
        if(this.startY < 0){
            posY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
		}
		
        this.changeX += (posX + this.startX - this.changeX)/1;
        this.changeY += (posY + this.startY - this.changeY)/1;
        
        this.sP(this.changeX, this.changeY);
        setTimeout(this.id + "_obj.floatIt('" + elContainer.id + "')", 40);   		
    }
    return el;
}


String.prototype.replaceAll = function(v1,v2){
    var temp = this;
    var i = temp.indexOf(v1);

    while(i > -1){
        temp = temp.replace(v1, v2);
        i = temp.indexOf(v1, i + v2.length + 1);
    }
    return temp;
}

function OpenWindow(url, name, width, height)
{
  if( url == null || url == "" ||url == "undefined")
  { return false; }
  else
  {
    var handle =   window.open(url, name,"scrollbars=yes,screenX=25,screenY=25,directories=0,height=" + height + ",width=" + width + ",location=no,menubar=no,resizable=yes,status=no,toolbar=no"); +
    handle.focus();
    return handle;
  }
}

function OpenWindowStandard(url)
{ OpenWindow(url, "popup", 775, 525);}

function OpenWindowFull(url)
{ OpenWindow(url, "popup", screen.width, screen.height);}

function OpenWindowHelp(url)
{ OpenWindow(url, "help", 578, 400);}


function makeRequest(url) {
	var http_request = false;
	if (window.XMLHttpRequest)
	{
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
		{
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject)
		{
			try
		        {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e)
			{
			try
		        {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e)
			{}
		}
	}
	if (!http_request)
	{
		return false;
	}
	http_request.onreadystatechange = function()
	{};
	http_request.open('GET', url, true);
	http_request.send(null);
}
