﻿var cp = {
	// variables
	clipboard : null,	// point to clipboard div
	content_container : null,
	content : null,
/*	*/
	clip_c : null,
	sch_type : null,
	_clip_item_ids : _clip_item_ids = {'rent': {}, 'buy': {}},
	_requests : {},
	_request_max_id : {},
	//photoPath : 'http://www.hongkonghomes.com/tmp/pro_info/',
	photoPath : 'http://images.hongkonghomes.com/listing/loadPropertyPhoto.php?',
	current_scroll_top : 0,
	shield : null,
	enableShield : false,
	
	updatePageClippedItem : false, // indicate to update the container page clip item or not

	// width of mainTable
	mainTableWidth : 789,
	
	// width of white region in clipboard
	maxWidth : 667,
	minWidth : 667,
	
	// ttl height of clipboard include outer border
	maxHeight : 660,
	minHeight : 310,
	
	// outer border size
	borderTop : 26,
	borderLeft : 26,
	borderRight : 27,
	borderBottom : 27,
	
	// spacing between browser and clipboard
	marginTop : 100,
	marginBottom : 30,
	
	// white space of clipboard
	innerBorderLeft : 26,
	innerBorderRight : 26,
	
	// size of clipboard header
	innerBorderImgTop : 33,
	innerBorderImgHeight : 24,
	innerBorderSpacing : 14,
	//innerBorderTop : this.innerBorderImgTop + this.innerBorderImgHeight + this.innerBorderSpacing, //71,
	
	// brown title bar
	innerBarHeight : 30,
	
	
	ie : (document.all && !window.opera),

	// general function -------------------------------------------------------------------------
	// set attribute
	extend : function (el, attribs) {
		for (var x in attribs) el[x] = attribs[x];
		return el;
	},
	
	// set style
	setStyles : function (el, styles) {
		for (var x in styles) {
			if (this.ie && x == 'opacity')
			{
				if (styles[x] > 0.99) el.style.removeAttribute('filter');
				else el.style.filter = 'alpha(opacity='+ (styles[x] * 100) +')';
			}
			else
			{
				el.style[x] = styles[x];
			}
		}
	},

	// get size of browser
	getPageSize : function ()
	{
		var d = document, w = window, iebody = d.compatMode && d.compatMode != 'BackCompat' ? d.documentElement : d.body;
		var b = d.body;
		
		var xScroll = (w.innerWidth && w.scrollMaxX) ? w.innerWidth + w.scrollMaxX : Math.max(b.scrollWidth, b.offsetWidth),
			yScroll = (w.innerHeight && window.scrollMaxY) ? w.innerHeight + w.scrollMaxY : Math.max(b.scrollHeight, b.offsetHeight),
			pageWidth = this.ie ? iebody.scrollWidth : (d.documentElement.clientWidth || self.innerWidth),
			pageHeight = this.ie ? Math.max(iebody.scrollHeight, iebody.clientHeight) : (d.documentElement.clientHeight || self.innerHeight);
		
		var width = this.ie ? iebody.clientWidth : (d.documentElement.clientWidth || self.innerWidth),
			height = this.ie ? iebody.clientHeight : self.innerHeight;
		
		return {
			pageWidth: Math.max(pageWidth, xScroll),				// total document width
			pageHeight: Math.max(pageHeight, yScroll),				// total document height
			width: width,											// width of view part
			height: height,											// height of view part
			scrollLeft: this.ie ? iebody.scrollLeft : pageXOffset,	// scrolled left
			scrollTop: this.ie ? iebody.scrollTop : pageYOffset		// scrolled top
		}
	},

	// function to create element
	createElement : function (tag, attribs, styles, parent, nopad)
	{
		var el = document.createElement(tag);
		if (attribs) this.extend(el, attribs);
		if (styles) this.setStyles(el, styles);
		if (parent) parent.appendChild(el);
		if (nopad) this.setStyles(el, {padding: 0, border: 'none', margin: 0});
		return el;
	},
	
	// function to add event listener
	addEventListener : function (el, event, func)
	{
		try {
			
			el.addEventListener(event, func, false);
		} catch (e) {
			// ie
			try {
				el.detachEvent('on'+ event, func);
				el.attachEvent('on'+ event, func);
			} catch (e) {
				el['on'+ event] = func;
			}
		} 
	},
	
	// generate xmlhttprequest element
	getHttpRequestObj : function ()
	{
		var xmlhttp = null;
		
		if (window.XMLHttpRequest)
		{ // Mozilla, etc.
			xmlhttp = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{ // IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		return xmlhttp;
	},
	
	setResponseListener : function (action, listener)
	{
		var new_idx = null;
		if (!this._requests[action])
		{
			new_idx = 0;
			this._requests[action] = [];
		}
		else
		{
			new_idx = this._requests[action].length;
		}
		
		if (!this._request_max_id[action])
			this._request_max_id[action] = 1;
		else
			this._request_max_id[action]++;
		
		this._requests[action][new_idx] = {};
		this._requests[action][new_idx]['id'] = this._request_max_id[action];
		this._requests[action][new_idx]['listener'] = listener;
		this._requests[action][new_idx]['http_request'] = this.getHttpRequestObj();
		this._requests[action][new_idx]['http_request'].onreadystatechange = this.responseHandler;
		
		return new_idx;
	},
	
	responseHandler : function ()
	{
		for (var action in cp._requests)
		{
			for (var i = 0; i < cp._requests[action].length; i++)
			{
				if (i < 0)
					break;
				
				var http_request = cp._requests[action][i]['http_request'];
				var listener = cp._requests[action][i]['listener'];
				var req_id = cp._requests[action][i]['id'];
				
				if (http_request.readyState == 4)
				{
					var response = null;
					var is_xml_response = false;
					if (http_request.status == 200 || http_request.status == 304)
					{
						response = http_request.responseText;
					}
					
					//checkConnection(response, is_xml_response);
					cp._requests[action].splice(i, 1);
					i--;
					
					listener(response, action, req_id);
				}
			}
		}
	},
	
	xmlhttpChange : function(response, action ,req_id)
	{
		if (response == null)
			return false;
		
		if(action == 'get_listing')
		{
			response = eval('('+response+')');
			
			if (response.error === 1 || response.error === 2 || response.error === 3)
				return false;
			
			cp.createContent(cp.content, response);
		}
	},
	
	// end of general function -------------------------------------------------------------------------
	
	
	
	
	// set the size and position of clipboard
	setPositionAndSize : function()
	{
		// clipboard is not null
		if(this.clipboard)
		{
			if(this.clipboard.style.visibility == 'visible')
			{
				var page = this.getPageSize();
				
				//alert(page.pageWidth + '\n' + page.pageHeight + '\n' +page.width + '\n' +page.height + '\n'+ page.scrollLeft + '\n'+ page.scrollTop + '\n')
				
				var height = page.height - this.borderTop - this.borderBottom - this.marginTop - this.marginBottom;
			
				//alert(height);
				if (height > this.maxHeight)
					height = this.maxHeight;
				else if (height < this.minHeight)
					height = this.minHeight;
				
				//alert(height);
				// height of white space in clipboard
				this.content_container.style.height = height + 'px';
				this.content_container.parentNode.style.height = height + 'px';
				
				// height of content div, area under brown title bar
				height = height - this.innerBorderImgTop - this.innerBorderImgHeight - this.innerBorderSpacing - this.innerBarHeight;
				this.content.style.height = height + 'px';
				
				// align center, little to right
				var p_menu = document.getElementById('p_menu');
				var offsetLeft = getOffsetfunction(p_menu, true) + p_menu.offsetWidth;
				
				offsetLeft = parseInt((this.mainTableWidth - this.clipboard.offsetWidth) / 2) + offsetLeft;
				
				this.clipboard.style.left = offsetLeft + 'px';
			}
		}
	},
	
	showLoading : function()
	{
		if(document.getElementById('loading_div'))
			document.getElementById('loading_div').style.display = 'inline';
			
		if(document.getElementById('chkSelectAll'))
		{
			document.getElementById('chkSelectAll').checked = false;
			document.getElementById('chkSelectAll').disabled = true;
		}
		
		if(this.enableShield)
		{
			this.shield.style.display = 'inline';
		}
		
		while(this.content.childNodes.length > 0)
		{
			this.content.removeChild(this.content.childNodes[0]);
		}
	},
	
	hiddenLoading : function()
	{
		if(this.enableShield)
			this.shield.style.display = 'none';
		
		if(document.getElementById('loading_div'))
			document.getElementById('loading_div').style.display = 'none';
	},

	createEmptyContent : function(container)
	{
		while(container.childNodes.length > 0)
		{
			container.removeChild(container.childNodes[0]);
		}
		
		this.content.appendChild(document.createTextNode('No Clipped Items Found'));
		
		this.endShowClipboard(1);
	},

	createContent : function(container, response)
	{
		while(container.childNodes.length > 0)
		{
			container.removeChild(container.childNodes[0]);
		}
		
		var table = this.createElement('table', {cellPadding: 0, cellSpacing: 0, border : 0}, {width : '615px'}, container, true);
		var tbody = this.createElement('tbody', null, null, table);
		
		var tr, td, img, checkbox, span, i, input;
		
		// padding top
		tr = this.createElement('tr', null, null, tbody);
		td = this.createElement('td', {height : 10}, null, tr);
			
		for(i = 0; i < response.items.length; i++)
		{
			tr = this.createElement('tr', null, null, tbody);
			// check box
			td = this.createElement('td', {rowSpan : 2}, {width : '28px'}, tr);
			checkbox = this.createElement('input'
				, {
					onclick : new Function('cp.checkSelectedAllChk(this.checked);')
					, type : 'checkbox'
					, id : 'select_chk_' + response.items[i].item_id
					, name : 'select_chk_' + response.items[i].item_id
					, value : '1'
				}
				, null, td);
			// hidden box for item_type
			input = this.createElement('input'
				, {
					type : 'hidden'
					, id : 'select_type_' + response.items[i].item_id
					, name : 'select_type_' + response.items[i].item_id
					, value : response.items[i].item_type
				}
				, null ,td);
			// hidden box for pro_id
			input = this.createElement('input'
				, {
					type : 'hidden'
					, id : 'select_pro_' + response.items[i].item_id
					, name : 'select_pro_' + response.items[i].item_id
					, value : response.items[i].pro_id
				}
				, null ,td);
			// listing img
			td = this.createElement('td', {vAlign : 'top', rowSpan : 2}, {width : '121px'}, tr);
			if(response.items[i].prop_photo != '')
			{
				img = this.createElement('img'
					, {
						//src : this.photoPath + response.items[i].pro_id + '/' + response.items[i].prop_photo
						src : this.photoPath + 'photo=' + response.items[i].rf_code + '&index=' + response.items[i].cover_photo
						, border : 0
						//, onclick : new Function('alert("click on photo ' + i + '");')
					}
					, {
						width : '112px'
						, height : '84px'
						, cursor : 'pointer'
					}, td);
			}
			// location
			td = this.createElement('td', {vAlign : 'top', className : 'schResult', rowSpan : 2}, {width : '176px'}, tr);
			td.appendChild(document.createTextNode(response.items[i].build_name_eng));
			td.appendChild(document.createElement('br'));
			if(response.items[i].build_name_eng != response.items[i].build_name_big5)
			{
				td.appendChild(document.createTextNode(response.items[i].build_name_big5));
				td.appendChild(document.createElement('br'));
			}
			td.appendChild(document.createTextNode(response.items[i].pvc_name_eng));
			if(response.items[i].sale_avail == '2')
			{
				td.appendChild(document.createElement('br'));
				span = this.createElement('span', {className : 'schResult'}, {color : 'red'}, td);
				span.appendChild(document.createTextNode('Sale with Lease'));
			}
			// price
			td = this.createElement('td', {vAlign : 'top', className : 'schResult', rowSpan : 2}, {width : '87px'}, tr);
			if(!isNaN(response.items[i].price_ask) && response.items[i].price_ask > '0')
			{
				td.appendChild(document.createTextNode(formatCurrency(response.items[i].price_ask, 3, 0)));
				td.appendChild(document.createElement('br'));

				if (response.items[i].unit_size > 0)
					td.appendChild(document.createTextNode(formatCurrency(response.items[i].price_ask / response.items[i].unit_size, 3, 0) + ' p.s.f.'));
				else
					td.appendChild(document.createTextNode('--'));
			}
			else
			{
				td.appendChild(document.createTextNode('--'));
			}
			// rental
			td = this.createElement('td', {vAlign : 'top', className : 'schResult', rowSpan : 2}, {width : '89px'}, tr);
			if(!isNaN(response.items[i].rental_ask) && response.items[i].rental_ask > '0')
			{
				td.appendChild(document.createTextNode(formatCurrency(response.items[i].rental_ask, 3, 0)));
				td.appendChild(document.createElement('br'));

				if (response.items[i].unit_size > 0)
					td.appendChild(document.createTextNode(formatCurrency(response.items[i].rental_ask / response.items[i].unit_size, 3, 0) + ' p.s.f.'));
				else
					td.appendChild(document.createTextNode('--'));
			}
			else
			{
				td.appendChild(document.createTextNode('--'));
			}
			// size
			td = this.createElement('td', {vAlign : 'top', className : 'schResult'}, {width : '83px'}, tr);
			td.appendChild(document.createTextNode(formatCurrency(response.items[i].unit_size, 3, 0)));
			// empty space next to ref no
			td = this.createElement('td', {rowSpan : 2, vAlign : 'top'}, {width : '31px'}, tr);
			
			// ref no in row 2
			tr = this.createElement('tr', null, null, tbody);
			td = this.createElement('td', {align : 'right', className : 'schResult'}, {height : '20px'}, tr);
			span = this.createElement('span', null, {backgroundColor: '#988D79', color : 'white', padding : '2px'}, td);
			span.appendChild(document.createTextNode('Ref.: ' + response.items[i].ref_no));
			
			// separator
			tr = this.createElement('tr', null, null, tbody);
			td = this.createElement('td', {colSpan : 7}
				, {
					background: 'url(/common/images/clipboard/rule.gif)'
					, width : '615px'
					, height : '21px'
				} , tr);
			td = this.createElement('td', null, null, tr);
		}
		
		this.endShowClipboard(2);
	},
	
	createContentContainer : function()
	{
		this.content_container = this.createElement('div'
			, {
				//innerHTML : 'content container'
			}
			, {
				background : 'white'
			});
		
		// header of content
		// header table
		var table = this.createElement('table', {cellPadding: 0, cellSpacing: 0, border : 0}, null, this.content_container, true);
		var tbody = this.createElement('tbody', null, null, table);
		
		// row 1
		var tr = this.createElement('tr', null, null, tbody);
		var td = this.createElement('td'
			, {
				rowSpan : 3
			}
			, {
				width : this.innerBorderLeft + 'px'
			}, tr);
		td = this.createElement('td', null
			, {
				width : (this.maxWidth - this.innerBorderLeft - this.innerBorderRight) + 'px'
			}, tr);
			
		// inner header table
		var innerTable = this.createElement('table', {cellPadding: 0, cellSpacing: 0, border : 0, width : '100%'}, null, td, true);
		var innerTbody = this.createElement('tbody', null, null, innerTable);
		// inner Row 1
		var innerTr = this.createElement('tr', null, null, innerTbody);
		var innerTd = this.createElement('td', {vAlign : 'bottom'}, {height : (this.innerBorderImgTop + this.innerBorderImgHeight) + 'px'}, innerTr);
		var img = this.createElement('img'
			, {
				src : '/common/images/clipboard/hd_clipboard.gif'
				, boder : 0
			}
			, {
				width : '94px'
				, height : '24px'
			}
			, innerTd);
		
		img = this.createElement('img'
			, {
				src : '/common/images/clipboard/clip.gif'
				, border : 0
				, width : 39
				, height : 49
			}
			, {
				position : 'relative'
				, top : '-36px'
				, left : '9px'
			}
			, innerTd);
		
		innerTd = this.createElement('td', {align : 'right', vAlign : 'bottom'}, null, innerTr);
		// share property gif
		img = this.createElement('img'
			, {
				src : '/common/images/buttons/btn_share_property_s.gif'
				, onclick : new Function('cp.emailSelected(null, "sMail", 2);')
			}
			, {
				width : '94px'
				, height : '13px'
				, boder : '0px'
				, cursor : 'pointer'
			}
			, innerTd);
			
		img = this.createElement('img'
			, {
				src : '/common/images/buttons/divider.gif'
			}
			, {
				width : '13px'
				, height : '13px'
				, boder : '0px'
			}
			, innerTd);
		// send enquiry gif
		img = this.createElement('img'
			, {
				src : '/common/images/buttons/btn_send_enquiry_s.gif'
				, onclick : new Function('cp.emailSelected(null, "enquiry", 2);')
			}
			, {
				width : '67px'
				, height : '13px'
				, boder : '0px'
				, cursor : 'pointer'
			}
			, innerTd);
			
		img = this.createElement('img'
			, {
				src : '/common/images/buttons/divider.gif'
			}
			, {
				width : '13px'
				, height : '13px'
				, boder : '0px'
			}
			, innerTd);
		// remove clipboard gif
		img = this.createElement('img'
			, {
				src : '/common/images/buttons/btn_remove_clip.gif'
				, onclick : new Function('cp.removeSelected(this);')
			}
			, {
				width : '81px'
				, height : '13px'
				, boder : '0px'
				, cursor : 'pointer'
			}
			, innerTd);
		
		// inner Row 2
		innerTr = this.createElement('tr', null, null, innerTbody);
		innerTd = this.createElement('td', {colSpan : 2}, {height : this.innerBorderSpacing + 'px'}, innerTr);
		// end of innerTable
		
		td = this.createElement('td'
			, {
				rowSpan : 2
			}
			, {
				width : this.innerBorderRight + 'px'
			}, tr);
		
		// row 2
		tr = this.createElement('tr', {height : this.innerBarHeight}, {backgroundColor : '#9A8D7D'}, tbody);
		td = this.createElement('td', null, null, tr);
		innerTable = this.createElement('table', {cellPadding: 0, cellSpacing: 0, border : 0}, null, td, true);
		innerTbody = this.createElement('tbody', null, null, innerTable);
		innerTr = this.createElement('tr', null, null, innerTbody);
		// check box
		innerTd = this.createElement('td', null, {width : '28px'}, innerTr);
		var checkbox = this.createElement('input'
			, {
				onclick : new Function('cp.selectAllChk(this.checked);')
				, type : 'checkbox'
				, id : 'chkSelectAll'
				, name : 'chkSelectAll'
				, value : '1'
				, disabled : true
			}
			, null, innerTd);
		// select all
		innerTd = this.createElement('td', null, {width : '121px'}, innerTr);
		img = this.createElement('img'
			, {
				src : '/common/images/clipboard/bar_select_all.gif'
				, border : 0
			}
			, {
				width : '47px'
				, height : '30px'
			}, innerTd);
		// location 
		innerTd = this.createElement('td', null, {width : '176px'}, innerTr);
		img = this.createElement('img'
			, {
				src : '/common/images/real_estate_services/bar_location.gif'
				, border : 0
			}
			, {
				width : '44px'
				, height : '30px'
			}, innerTd);
		// price 
		innerTd = this.createElement('td', null, {width : '87px'}, innerTr);
		img = this.createElement('img'
			, {
				src : '/common/images/real_estate_services/bar_price.gif'
				, border : 0
			}
			, {
				width : '57px'
				, height : '30px'
			}, innerTd);
		// rental 
		innerTd = this.createElement('td', null, {width : '89px'}, innerTr);
		img = this.createElement('img'
			, {
				src : '/common/images/real_estate_services/bar_rental.gif'
				, border : 0
			}
			, {
				width : '61px'
				, height : '30px'
			}, innerTd);
		// size 
		innerTd = this.createElement('td', null, {width : '114px'}, innerTr);
		img = this.createElement('img'
			, {
				src : '/common/images/real_estate_services/bar_size.gif'
				, border : 0
			}
			, {
				width : '46px'
				, height : '30px'
			}, innerTd);
		
		// row 3
		tr = this.createElement('tr', null, null, tbody);
		td = this.createElement('td', {colSpan : 2}, null, tr);
		// create loading img
		var div = this.createElement('div', {id : 'loading_div'}, {position:'relative', top : '-48px'}, td);
		img = this.createElement('img', {src : '/common/images/clipboard/loading.gif', border : 0}, {width : '16px', height : '16px'} , div);
		div.appendChild(document.createTextNode('Loading ...'));
		
		// content div
		this.content = this.createElement('div'
			, {
				//innerHTML : 'content'
			}
			, {
				width : '641px'
				, overflow : 'auto'
			}, td, true);
		
		return this.content_container;
	},
	
	createClipboard : function()
	{
		var table = this.createElement('table', {cellPadding: 0, cellSpacing: 0, border : 0}, null, null, true);
		
		var tbody = this.createElement('tbody', null, null, table);
		
		// row 1
		var tr = this.createElement('tr', null, null, tbody);
		var td = this.createElement('td', null,
			{
				width : this.borderLeft + 'px'
				, height : this.borderTop + 'px'
				, backgroundImage : 'url("/common/images/pop_up_layer/top.png")'
			}, tr);
			
		td = this.createElement('td', null
			, {
				width: this.maxWidth + 'px'
				, backgroundImage : 'url("/common/images/pop_up_layer/top.png")'
			}
			, tr);
			
		td = this.createElement('td'
			, {
				onclick : new Function('cp.doClose();')
				, title : 'click to close the clipboard'
			}
			, {
				width : this.borderRight + 'px'
				, backgroundImage : 'url("/common/images/pop_up_layer/close.png")'
				, cursor : 'pointer'
			}
			, tr);
		
		// row 2 
		tr = this.createElement('tr', null, null, tbody);
		
		td = this.createElement('td', null
			, {
				backgroundImage : 'url("/common/images/pop_up_layer/top.png")'
			}
			, tr);
			
		td = this.createElement('td', null
			, {
				//height : this.getContentHeight() + 'px'
			}, tr);
		td.appendChild(this.createContentContainer());
		
		td = this.createElement('td', null
			, {
				backgroundImage : 'url("/common/images/pop_up_layer/right.png")'
			}
			, tr);
		
		// row 3
		tr = this.createElement('tr', null, null, tbody);
		
		td = this.createElement('td'
			, {
				colSpan : 2
			}
			, {
				backgroundImage : 'url("/common/images/pop_up_layer/btm.png")',
				height : this.borderBottom + 'px'
			}
			, tr);
		
		td = this.createElement('td', null
			, {
				backgroundImage : 'url("/common/images/pop_up_layer/right_corner.png")'
			}
			, tr);

		return table;
	},

	// generate div for clipboard
	initialize : function ()
	{
		this.clipboard = this.createElement('div'
			, {
				id : 'cli'
			}
			, {
				position : 'absolute'
				, left : '0px'
				, top : this.marginTop + 'px'
				, zIndex : '10'
				, display : 'none'
				, visibility : 'hidden'
			}
			, document.body, true);
			
		if(this.enableShield)
		{
			var pageSize = this.getPageSize();
		
			this.shield = this.createElement('div', null
				, {
					position : 'absolute'
					, left : '0px'
					, top : '0px'
					, zIndex : '8'
					, display : 'none'
					, opacity : '0.4'
					, height : pageSize.pageHeight + 'px'
					, width : (pageSize.pageWidth + 25) + 'px' // handler scroll bar width
				}
				, document.body);
		}
		
		this.clipboard.appendChild(this.createClipboard());
	},
	
	showClipboard : function()
	{
		if(!this.clipboard)
		{
			this.initialize();
		}
		
		this.showLoading();
		
		this.clipboard.style.visibility = 'visible';
		this.clipboard.style.display = 'inline';
		
		// hide select input in ie
		if(this.ie)
		{
			var select = document.getElementsByTagName('select');
			for(var i = 0; i < select.length; i++)
			{
				// not to hidden some select
				if(select[i].style.visibility != 'visible')
					select[i].style.display = 'none';
			}
		}
		
		if(this.enableShield)
		{
			if(this.ie)
			{
				document.body.scroll = 'no';
			}
			else
			{
				this.current_scroll_top = document.body.scrollTop;
				document.body.scrollTop = 0;
				
				document.body.style.overflow='hidden';
			}
		}
		
		this.setPositionAndSize();
		
		document.getElementById('btn_clipboard').src = '/common/images/buttons/btn_clipboard_on.gif';
		document.getElementById('btn_clipboard').title = 'Close Clipboard';
		document.getElementById('btn_clipboard').onclick = null; //new Function('cp.doClose();');
		
		this.loadClipItems();
	},
	
	endShowClipboard : function(type)
	{
		document.getElementById('btn_clipboard').onclick = new Function('cp.doClose();');

		document.getElementById('loading_div').style.display = 'none';
		
		if(document.getElementById('chkSelectAll'))
		{
			document.getElementById('chkSelectAll').disabled = false;
		}
	},
	
	doClose : function()
	{
		// show select input in ie
		if(this.ie)
		{
			var select = document.getElementsByTagName('select');
			for(var i = 0; i < select.length; i++)
			{
				// not to inline some select
				if(select[i].style.visibility != 'visible')
					select[i].style.display = 'inline';
			}
		}
		
		this.clipboard.style.visibility = 'hidden';
		this.clipboard.style.display = 'none';
		
		document.getElementById('btn_clipboard').src = '/common/images/buttons/btn_clipboard_off.gif';
		document.getElementById('btn_clipboard').title= 'Show Clipboard';
		document.getElementById('btn_clipboard').onclick = new Function('cp.showClipboard();');
		
		if(this.enableShield)
		{
			this.shield.style.display = 'none';
			
			if(this.ie)
			{
				document.body.scroll = '';
			}
			else
			{
				document.body.scrollTop = this.current_scroll_top;
				document.body.style.overflow='';
			}
		}
	},
	
	// check box --------------------------------------------------------
	selectAllChk : function(checked)
	{
		var inputs = this.content.getElementsByTagName('input');
		
		for(var i = 0; i < inputs.length; i++)
		{
			if(inputs[i].type == 'checkbox')
			{
				inputs[i].checked = checked;
			}
		}
	},
	
	checkSelectedAllChk : function(checked)
	{
		if(!checked)
		{
			document.getElementById('chkSelectAll').checked = false;
		}
		else
		{
			var inputs = this.content.getElementsByTagName('input');
			
			for(var i = 0; i < inputs.length; i++)
			{
				if(inputs[i].type == 'checkbox')
				{
					if(!inputs[i].checked)
					{
						//document.getElementById('chkSelectAll').checked = false;
						return false;
					}
				}
			}
			
			document.getElementById('chkSelectAll').checked = true;
		}
	},
	//end of check box --------------------------------------------------------
	
	// clipboard img button function ------------------------------------------
	// fr : 1 from page, 2 from clipboard
	emailSelected : function(id, type, fr)
	{
		var criteria = new Array();
		
		if(fr == 1)
		{
			criteria.push(id);
			criteria.push(sch_type);
			criteria.push(fr);
			
			mail.showMail(criteria, type);
		}
		else if(fr == 2) // clipboard
		{
			var inputs = this.content.getElementsByTagName('input');
			var pro_ids = '';
			var item_type = '';
			
			for(var i = 0; i < inputs.length; i++)
			{
				if(inputs[i].type == 'checkbox')
				{
					if(inputs[i].checked)
					{
						if(pro_ids)
						{
							pro_ids += ',';
							item_type += ',';
						}
						
						pro_ids += document.getElementById('select_pro_' + inputs[i].id.substring(11)).value;
						item_type += document.getElementById('select_type_' + inputs[i].id.substring(11)).value;
					}
				}
			}
			
			if(pro_ids != '')
			{
				criteria.push(pro_ids);
				criteria.push(item_type);
				criteria.push(fr);
				mail.showMail(criteria, type);
			}
		}
	},
	
	removeSelected : function(button)
	{
		if(this.content)
		{
			var inputs = this.content.getElementsByTagName('input');
			var clip_item_id = '';
			
			for(var i = 0; i < inputs.length; i++)
			{
				if(inputs[i].type == 'checkbox')
				{
					if(inputs[i].checked)
					{
						if(clip_item_id)
							clip_item_id += ',';
						
						clip_item_id += inputs[i].id.substring(11);
					}
				}
			}
			
			
			if(clip_item_id != '')
			{
				this.updatePageClippedItem = true;
				
				this.showLoading();
				
				var req_id = this.clip_c.removeItems(clip_item_id, this.removeClipItemHandler);
			}
		}
	},
	// end of clipboard img button function ------------------------------------------
	
	removeClipItemHandler : function(response, req_id)
	{
		if (response == null)
			return false;

		var response = eval('('+response+')');

		if (response.result == 1)
		{
			cp.loadClipItems();
			
			// clip.gif to spacer.gif
			for(var i = 0; i < response.items.length; i++)
			{
				for(var item_id in cp._clip_item_ids[sch_type])
				{
					if(cp._clip_item_ids[sch_type][item_id] == response.items[i].clip_item_id)
					{
						if(document.getElementById('clipico_' + item_id))
							document.getElementById('clipico_' + item_id).src = '../common/images/spacer.gif';
						break;
					}
				}
			}
		}
	},
	
	clipConnFailedHandler: function()
	{
		alert('Clipboard Connection Failed. Please try again later.');
	},

	loadClipItems: function(sch_type)
	{
		this.clip_c = new ClipCore('/clipboard/clipGateway.action');
		this.clip_c.setResponseFormat('json');
		this.clip_c.setConnectionFailedHandler(this.clipConnFailedHandler);

		if (sch_type != null)
			var criteria = {'namespace':'proway.clip.property','item_type':sch_type};
		else
			var criteria = {'namespace':'proway.clip.property'};

		// handler for property_info/properyt_search_list
		if(sch_type != null)
			this.clip_c.fetchItems(criteria, showClipItemsHandler);
		else
			this.clip_c.fetchItems(criteria, this.showClipboardHandler);
	},

	// handler for clipboard
	showClipboardHandler : function (response, request_id)
	{
		if (response == null)
			return false;
		
		// not yet handle property_info jsp case
		if(cp.updatePageClippedItem)
		{
			cp.updatePageClippedItem = false;

			if (typeof showClipItemsHandler == 'function')
				showClipItemsHandler(response, request_id);
		}
		
		var response = eval('('+response+')');
		
		_clip_items = response['items'];
		
		if (_clip_items.length == 0)
		{
			cp.createEmptyContent(cp.content);
		}
		else
		{
			var new_idx = 0;
			
			var pro_ids = '';
			var item_ids = '';
			var item_type = '';
			//var prop_type = null;
			
			for (var i = 0; i < _clip_items.length; i++)
			{
				//prop_type = _clip_items[i].namespace.split('.')[1].substr(0, 3); // landscope.{commercial}.property
				cp._clip_item_ids/*[prop_type]*/[_clip_items[i].item_type][_clip_items[i].item_id] = _clip_items[i].clip_item_id;
				
				if(item_ids.length > 0)
				{
					pro_ids += ',';
					item_ids += ',';
					item_type += ',';
				}
				
				pro_ids += _clip_items[i].item_id;
				item_ids += _clip_items[i].clip_item_id;
				item_type += _clip_items[i].item_type;
			}
			
			var idx = cp.setResponseListener('get_listing', cp.xmlhttpChange);
			cp._requests['get_listing'][idx]['http_request'].open('POST', '/clipboard/showClipItem.action', true);
			cp._requests['get_listing'][idx]['http_request'].setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			cp._requests['get_listing'][idx]['http_request'].send('pro_ids=' + pro_ids + '&item_ids=' + item_ids + '&item_type=' + item_type);
		}
	}
};

cp.addEventListener(window, 'resize', function() {
	cp.setPositionAndSize();
});
