function LBClientForm(){
	this.root = document.getElementById('lb_client_form').parentNode;
	if(this.root.tagName == 'HEAD')
		this.root = this.root.parentNode;

	this.base_url = 'http://www.listingbook.com/';
	this.url = this.base_url+'cgi-bin/controls/client_form/';
	this.uid = 'A106142';

	this.popup = new Object;
	this.popup.width = 433;
	this.popup.height = 334;
	this.popup.top = null;
	this.popup.left = null;
	this.popup.zIndex="1"; //added to fix overlay

	if(this.popup.top === null || this.popup.left === null)
		this.use_viewport_center = true;

	this.btn = new Object;
	this.btn.url = 'red.gif';
	this.btn.width = 120;
	this.btn.height = 40;

	if(!this.btn.url.match(/^http(s)?:/i))
		this.btn.url = this.url+this.btn.url;

	this.fonts = "'Lucida Sans Unicode','Lucida Grande',Verdana,Arial,Helvetica,sans-serif";
}

LBClientForm.prototype.viewport_center = function(a){
	var edge = 'Left',
			dim = 'Width';
	if(a == 'Y'){
		edge = 'Top';
		dim = 'Height';
	}

	var scroll = eval('self.page'+a+'Offset'),
			center = eval('self.inner'+dim);
	if(!scroll)
		scroll = this.doc_val('scroll'+edge);
	if(!center)
		center = this.doc_val('client'+dim);

	return Math.floor(scroll + (center - this.popup[dim.toLowerCase()]) / 2);
}

LBClientForm.prototype.doc_val = function(ref){
	if(document.body)
		return document.body[ref];
	else if(document.documentElement && document.documentElement[ref])
		return document.documentElement[ref];
	return 0;
}

LBClientForm.prototype.cr = function(elm){return document.createElement(elm)}

LBClientForm.prototype.add_row = function(tbl, fn){
	var row = tbl.insertRow(tbl.rows.length);
	for(var i = 2; i < arguments.length; i++)
	{
		var cell = row.insertCell(row.cells.length);
		if(arguments[i] != '')
			fn(cell, arguments[i], this);
	}
}

LBClientForm.prototype.label_row = function(cell, val, obj){
	cell.style.color = '#777';
	cell.style.font = 'bold 11px '+obj.fonts;
	cell.innerHTML = val;
}

LBClientForm.prototype.input_row = function(cell, val, obj){
	var input = obj.cr('input');
		input.setAttribute('name', val);
		input.setAttribute('type', 'text');
		input.style.width = '130px';
		input.style.border = '1px solid #007CC3';
		input.style.backgroundColor = '#FFFFCA';
	cell.appendChild(input);
}

LBClientForm.prototype.error = function(msg){
	var error = this.cr('div');
		error.innerHTML = '[ '+msg+' ]';
		error.style.color = '#CC0000';
	this.root.appendChild(error);
}

LBClientForm.prototype.validate_submit = function(){
	if(!this.frm)
		return false;
	if(this.frm.first_name.value=='')
		return false;
	if(this.frm.last_name.value=='')
		return false;
	if(this.frm.email.value=='')
		return false;
	if(this.frm.email.value!=this.frm.email_confirm.value)
		return false;

	return true;
}

LBClientForm.prototype.validate_setup = function(){
	if(this.uid == '')
	{
		this.error('Invalid UID');
		return false;
	}

	return true;
}

LBClientForm.prototype.go = function(){
	var that = this;
	if(!this.validate_setup())
		return;

	var main = this.cr('div');
		if(!this.use_viewport_center)
			main.style.position = 'relative';

		var main_btn = this.cr('img');
			main_btn.src = this.btn.url;
			main_btn.style.width = this.btn.width;
			main_btn.style.height = this.btn.height;
			main_btn.style.cursor = 'pointer';
		main.appendChild(main_btn);

	var wrapper = this.cr('div');
		wrapper.style.backgroundColor = '#FFFFFF';
		wrapper.style.border = '1px solid black';
		wrapper.style.width = this.popup.width+'px';
		wrapper.style.height = this.popup.height+'px';
		wrapper.style.position = 'absolute';
		
		wrapper.style.zIndex = this.popup.zIndex;	//added to fix overlay
		
		wrapper.style.display = 'none';
		main_btn.onclick = function(){
			if(that.use_viewport_center){
				that.popup.top = that.viewport_center('Y');
				that.popup.left = that.viewport_center('X');
			}
			wrapper.style.top = that.popup.top+'px';
			wrapper.style.left = that.popup.left+'px';
			wrapper.style.display='block';
		};

		var header = this.cr('img');
			header.src = this.url+'header.jpg';
		wrapper.appendChild(header);

		var instr = this.cr('div');
			instr.style.margin = '35px 12px 20px 12px';
			instr.style.font = 'bold 13px '+this.fonts;
			instr.innerHTML = 'Please complete the form below.';
		wrapper.appendChild(instr);

		var form = this.cr('form');
			form.action = this.base_url+'client_request';
			form.method = 'POST';

			var hid = this.cr('input');
				hid.setAttribute('type', 'hidden');
				hid.setAttribute('name', 'ref_uid');
				hid.value = this.uid;
			form.appendChild(hid);

			var tbl = this.cr('table');
				tbl.setAttribute('cellspacing', '0');
				tbl.setAttribute('cellpadding', '3');
				tbl.style.width = '406px';
				tbl.style.margin = '0px 12px';

				this.add_row(tbl, this.label_row, 'First Name', 'Last Name', 'Phone (Optional)');
				this.add_row(tbl, this.input_row, 'first_name', 'last_name', 'phone_num');
				this.add_row(tbl, this.label_row, 'Email Address', 'Confirm Email', '');
				this.add_row(tbl, this.input_row, 'email', 'email_confirm', '');

				var row = tbl.insertRow(tbl.rows.length);
					var cell = row.insertCell(row.cells.length);
						cell.colSpan = 3;
						cell.style.paddingTop = '20px';
						cell.style.textAlign = 'center';

						var btn = this.cr('input');
							btn.setAttribute('type', 'image');
							btn.setAttribute('src', this.url+'submit.gif');
							btn.style.width = '120px';
							btn.style.height = '38px';
							btn.onclick = function(){
								if(LBClientFormCreator.validate_submit())
									return true;
								alert('There is an error in your submission.\nPlease check your information and try again.');
								return false;
							};
						cell.appendChild(btn);
						cell.appendChild(this.cr('br'));
						var cncl = this.cr('a');
							cncl.onclick = function(){wrapper.style.display='none';return false;};
							cncl.href='#';
							cncl.style.font = '9px '+this.fonts;
							cncl.innerHTML = 'No thanks';
						cell.appendChild(cncl);
			form.appendChild(tbl);
		wrapper.appendChild(form);
	main.appendChild(wrapper);
	this.root.appendChild(main);
	this.frm = form;
}

var LBClientFormCreator = new LBClientForm();
LBClientFormCreator.go();

