var was_SbrPopUp_Init = 0;
var SbrPopUp = {
	init: function(id) { 
		this.last = false;
		this.trigger = false;

		if (id && (form = document.getElementById(id))) {
			this.s = form.getElementsByTagName('select');
		} else {
			this.s = document.getElementsByTagName('select');
		}
    	this.sCount = this.s.length;
		for (var i=0; i<this.sCount; i++) {
		  if (!this.s[i].multiple) {
  			if (!this.s[i].id) {
  				this.s[i].id = 'select_'+i;
  			}
			this.run(this.s[i], i);
 		  }
		}
	},

	run: function(obj, counter) {
		// building faux select structure
		var fauxSel = document.createElement('dl');
		fauxSel.className = obj.className ? obj.className + ' sbrWrap' : 'sbrWrap';
		obj.className = 'selReplaced';

		fauxSel.onclick = function() {	
			var selectElement = this.nextSibling;
			if (this.childNodes[1].className == 'sbrOptsCol') {
				selectElement.onfocus();
				SbrPopUp.last.className = 'sbrOptsCol';
				SbrPopUp.last = this.childNodes[1];
				SbrPopUp.trigger = false;
				document.onclick = function() { if (SbrPopUp.trigger) SbrPopUp.last.className = 'sbrOptsCol'; SbrPopUp.trigger = true; }
				this.childNodes[1].className = 'sbrOptsExp';
			} else {
				SbrPopUp.lastObjId = false;
				document.onclick = function() {	}
				this.childNodes[1].className = 'sbrOptsCol';
			}
			if (this.childNodes[0].className == '') {
				this.nextSibling.focus();
			}
		}

		var curOpt = document.createElement('dt');
		curOpt.className = '';
		fauxSel.appendChild(curOpt);
		var optsWrap = document.createElement('dd');
		optsWrap.className = 'sbrOptsCol';
		fauxSel.appendChild(optsWrap);
		var optsList = document.createElement('ul');
		optsWrap.appendChild(optsList);
		optsList.style.zIndex = fauxSel.style.zIndex = 1000-counter;

		var opts = obj.options;
		var selectedOpt = (!obj.selectedIndex) ? 0 : obj.selectedIndex;
		for (var i=0; i<opts.length; i++) {
			var li = document.createElement('li');
			var txt = opts[i].text ? document.createTextNode(opts[i].text) : document.createTextNode('blank option');
			li.appendChild(txt);
			li.title = opts[i].title;
			li.selIndex = i;
			li.selectID = obj.id;
			li.onclick = function() {
				SbrPopUp.selectMe(this);
			}
			if (i == selectedOpt) {
				li.className = 'sbrSelected';
				curOpt.appendChild(txt.cloneNode(false));
			}
			if (window.attachEvent) {
				li.onmouseover = function() {
					this.className += ' hover';
				}
				li.onmouseout = function() {
					this.className = this.className.replace(new RegExp(" hover\\b"), '');
				}
			}
			optsList.appendChild(li);
		}
		obj.oldonfocus = obj.onfocus;
		obj.onfocus = function() {
			if (this.oldonfocus) this.oldonfocus();
			this.previousSibling.childNodes[0].className = 'sbrFocused';
		}
		obj.oldonblur = obj.onblur;
		obj.onblur = function() {
			if (this.oldonblur) this.oldonblur();
			this.previousSibling.childNodes[0].className = '';
		}
		obj.oldonchange = obj.onchange;
		obj.onchange = function() {
			if(obj.previous == undefined) {
				obj.previous = obj.value;
			} else {
				if(obj.previous == obj.value) {
					return;
				} else {
					obj.previous = obj.value;
				}
			}
			//alert("Change");
			if (this.oldonchange) this.oldonchange();
			var idx = this.selectedIndex;
			var ul = this.previousSibling.childNodes[1].childNodes[0];
			SbrPopUp.selectMe(ul.childNodes[idx],1);
		}
		obj.onkeypress = obj.onchange;

		obj.parentNode.insertBefore(fauxSel,obj);
	},

	selectMe: function(liObj) {
		var selectElement = liObj.parentNode.parentNode.parentNode.nextSibling;
		var lis = liObj.parentNode.getElementsByTagName('li');
		for (var i=0; i<lis.length; i++) {
			if (lis[i] != liObj) {
				lis[i].className = '';
				lis[i].onclick = function() {
					SbrPopUp.selectMe(this);
				}
			} else {
				this.setVal(liObj.selectID, liObj.selIndex);
				liObj.className = 'sbrSelected';
				var curOpt = liObj.parentNode.parentNode.parentNode.childNodes[0];
				curOpt.removeChild(curOpt.childNodes[0]);
				curOpt.appendChild(liObj.childNodes[0].cloneNode(false));
				if(arguments[1] != 1){
					selectElement.onchange();
				}
			}
		}
	},

	setVal: function(objID, val) {
		var obj = document.getElementById(objID);
		obj.selectedIndex = val;
	}
}


var browserName=navigator.appName;  

if (browserName=="Microsoft Internet Explorer") 
{ 
  var agt=navigator.userAgent.toLowerCase();
  var iePos  = agt.indexOf('msie');
  var browserVer = parseFloat(agt.substring(iePos+5,agt.indexOf(';',iePos)));

  if(browserVer < 7)   window.isIE6=true;
}
else 
  window.isIE6=false; 



function getPageSize() {
	
 var xScroll, yScroll;

if (window.innerHeight && window.scrollMaxY) {	
	xScroll = window.innerWidth + window.scrollMaxX;
	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
	if(document.documentElement.clientWidth){
		windowWidth = document.documentElement.clientWidth; 
	} else {
		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 = xScroll;		
} else {
	pageWidth = windowWidth;
}

return [pageWidth,pageHeight];
}

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 getWindowSize()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return [w,h];
}


function showSimple(id)
{
	var arrayPageSize = this.getPageSize();
	$('popup_overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
	
	$('popup_overlay').show();
	$(id).show();
	
	if(getScrollXY()[1] > 0)
	{
	 var myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	  }
		$(id).style.top = getScrollXY()[1] + myHeight/2 + 'px';
		$(id).style.marginLeft = '-'+$(id+'_holder').getWidth()/2 + 'px';
		$(id).style.marginTop = '-'+$(id+'_holder').getHeight()/2 + 'px';
	}
	else
	{
		$(id).style.top = "50%";
		$(id).style.marginLeft = '-'+$(id+'_holder').getWidth()/2 + 'px';
		$(id).style.marginTop = '-'+$(id+'_holder').getHeight()/2 + 'px';
	}
}

function moveNodeContent(from, to) {
	$A($(from).childNodes).each(function(elt) {
		if (elt.nodeType != 3) {
			$(from).removeChild(elt); $(to).appendChild(elt);
		}
	})
}
 
function popupShow(elemId,href,cost,isKeywords, yesId)
{		
	if(href)
	{
		if(typeof(yesId) == 'undefined'){
			$('yes').href = href;
			$('yes_tetx').href = href;
		}
		else
		$(yesId).href = href;
	}
	if(cost) $('warning_cost').update(cost);
	if(cost) $('warning_cost_f').update(cost);
	
	var arrayPageSize = this.getPageSize();
	$('popup_overlay').setStyle({ width: arrayPageSize[0] + 'px', height: arrayPageSize[1] + 'px' });
	
	if($(elemId+'_holder') && $(elemId+'_holder').innerHTML != "")
	{
		$(elemId).update($(elemId+'_holder').innerHTML);
		$('popup_window').update("");
	}
	
	$('popup_window').appendChild(Builder.node('div',{id:elemId+'_holder'}));
	
	//$(elemId+'_holder').update($(elemId).innerHTML);
	//$(elemId).update("");
	moveNodeContent($(elemId), $(elemId+'_holder'));
	
	if(window.isIE6)$('popup_window').style.top = getScrollXY()[1] + document.documentElement.clientHeight/2 + "px";
	else $('popup_window').style.top = "50%";
	$('popup_window').style.marginLeft = '-'+$('popup_window').getWidth()/2 + 'px';
	$('popup_window').style.marginTop = '-'+$('popup_window').getHeight()/2 + 'px';
	$('popup_window').style.position= 'fixed';
	
	if($('popup_window').getHeight() > getWindowSize()[1])
	{
		$('popup_window').style.top = getScrollXY()[1]+"px";
		$('popup_window').style.marginTop = '0px';
		$('popup_window').style.position= 'absolute';
	}
	
	$('popup_overlay').show();
	$('popup_window').show();
	
	$('popup_window').style.zIndex = 10001;
	
	if(window.isIE6)
	{
		$$('select').each(function(sel){
			if(typeof(sel.up('.popup_wnd')) == 'undefined') sel.style.visibility = 'hidden';
		});
		// assign other position for IE6 only
		$('popup_window').style.position= 'absolute';	
		$('popup_window').style.marginLeft = '-'+$('popup_window').getWidth()/2 + 'px';
		$('popup_window').style.marginTop = '-'+$('popup_window').getHeight()/2 + 'px';
	}
	
	/*if (was_SbrPopUp_Init == 0) {
		SbrPopUp.init('js_reportForm');
		was_SbrPopUp_Init = 1;
	} else {
		document.getElementById('js_reportForm').getElementsByTagName('dl')[0].remove();
		SbrPopUp.init('js_reportForm');
	}*/
}


function popupCloseSimple(elemId)
{
	$('popup_overlay').hide();
	$(elemId).hide();	
}

function popupClose(elemId)
{	
if($(elemId+'_holder') && $(elemId+'_holder').innerHTML != "")
{
	//$(elemId).update($(elemId+'_holder').innerHTML);
	moveNodeContent($(elemId+'_holder'), $(elemId));
	$('popup_window').update("");
}

$('popup_overlay').hide();
$('popup_window').hide();

if(window.isIE6)
{
	$$('select').each(function(sel){
	   if(typeof(sel.up('.popup_wnd')) == 'undefined') sel.style.visibility = 'visible';
	});
}

}

function showTab(tabID, obj)
{
$$('#tab_nav a').each(function(it){
	it.className = '';
});

$$('.tabCont').each(function(it){
	it.style.display = 'none';
});

$(obj).className = 'sel';
$(tabID).style.display = 'block';
}


Event.observe(window, 'load', function () {
/*		new Insertion.Bottom(document.body,'<div id="popup_overlay"></div>');
new Insertion.Bottom(document.body,'<div id="popup_window"></div>');*/
document.body.appendChild(Builder.node('div',{id:'popup_overlay'}));
document.body.appendChild(Builder.node('div',{id:'popup_window', className:'popup_wnd'}));

$('popup_overlay').setStyle({'position': 'absolute', 'top': '0', 'left': '0', 'z-index': '10000', 'width': '100%', 'height': '500px', 'background': '#000', 'display':'none', 'opacity':'0.8'});

$('popup_window').setStyle({'position':'fixed', 'top':'50%', 'left':'50%', 'z-index':'100001', 'display':'none'});

});
