function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (var i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(var j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}

 

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

function showBox()
{
	TWB.showBox();
	return true;
}

function setIt(elem, box, value)
{
	$(elem).value = value;
	$(box).innerHTML = "";
	$(box).style.display = "none";
}
 

TextWithBox = Class.create();
TextWithBox.prototype = {
  	initialize: function(textID, boxID, buttonID, label, options) {
		this.textID = textID;
		this.textField = $(textID);
		this.box = $(boxID);
		this.options = options;
		this.button = $(buttonID);
		this.label = label;
		this.showFlag = false;

		this.eventShowBox = this.showBox.bindAsEventListener(this);
		this.eventHideIt = this.hideIt.bindAsEventListener(this);
		Event.observe($(buttonID), 'click', this.eventShowBox);

		this.itemPerRow = 5;

		this.type = 'min';
		this.min_row = 4;
		this.max_row = 3;
  	},

	hideIt: function()
	{
		this.box.innerHTML = "";
		this.box.style.display = "none";
		this.showFlag = false;
	},
	
	showBox: function()
	{
		if (this.showFlag == true) {
			this.hideIt();
			return true;
		}
		this.showFlag = true;

		used_obj = this.textField;
		var value1 = null;
		var main_flag = false;
		bord = this.textField.value;
//		if (bord == 'Min Price' || bord == '0') main_flag = true;
		var num_row = this.max_row;
		var all_l = this.options.length-1;

//		this.hideIt();
		var str = '<div id="prices"><div><div id="close_span">close&nbsp;</div><div>Choose a ' + this.label + '</div></div>';
		str += '<table width="100%" style="border-collaps:collaps; padding:0; margin:0"><tr>';
		for (i = 1,k = 0; i <= num_row; i++) {
			str += '<td>';
			for (m = 1; m <= this.itemPerRow ;m++,k++) {
				if (undefined != this.options[k]) {
					str += '<a href="javascript:setIt(\'' + this.textID + '\', \'' + this.box.id + '\', \'' + this.options[k] + '\' )">';
					if ('No max' == this.options[k]) {
						str += this.options[k];
					} else {
						str += '$' + number_format(this.options[k], 0, '.', ',');
					}
					str += '</a><br>';
/*					
					if (!main_flag) {
						if  ((flag && nv_temp <= bord)||(!flag && nv_temp >= bord)) {
							str += this.options[k]+'<br>';
						} else {
							str += '<a href="javascript:TWB.setIt('+"'"+v_temp+"'"+')">'+this.options[k]+'</a><br>';	
						}
					} else {
						str += '<a href="javascript:TWB.setIt('+"'"+v_temp+"'"+')">'+this.options[k]+'</a><br>';
					}
*/						
				}
			}
			str += '</td>';
		}
		this.box.innerHTML += str + '</tr></table></div>';
		this.box.style.display = "block";

		Event.observe($('close_span'), 'click', this.eventHideIt);
		$('close_span').style.cursor = 'pointer';

		if (document.all) this.turnSelectOver('prices',this.box);
	},

	_converValue: function(val)
	{
/*		
		v_temp = v_temp.replace(/\s{2}/,'');
		nv_temp = this.options[k].replace(/\s?K/,'000');
		nv_temp = nv_temp.replace(/\s?Mil/,'000000');
*/		
		return val;
	},
	
	setIt: function(val)
	{
		used_obj.value = val;
		this.hideIt();
	},

	turnSelectOver: function(id,parId){
		this.box.innerHTML += "<iframe id='zIFrame' scrolling='no' frameborder='0' style='position:absolute; top:0px; left:0px; display:none;'></iframe> ";
		var dv = parId; //$(id);
		var ifr = $('zIFrame'); 
		ifr.className = "prices";
		ifr.style.width = dv.offsetWidth;
		ifr.style.marginTop  = dv.offsetTop;
		ifr.style.marginLeft = dv.offsetLeft;

		ifr.style.height = dv.offsetHeight;
		ifr.style.zIndex = dv.style.zIndex - 1;
		ifr.style.display = "none";
		ifr.style.display  = "block"; 
	}
};
