function document_onmousemove() {
	if (window.event.button == 1) {
		pbMouseMove();
		pbSpcrMouseMove();
	}
}
function document_onmouseup() {
	pbMouseUp();
	pbSpcrMouseUp();
}
var objMovePB, curOffsetX, curOffsetY;
var pbArea = null;
function pbMouseDown() {
	//if (window.event.srcElement.tagName == "INPUT") return false;
	if (window.event.srcElement.tagName != "TH") return false;
	if (window.event.button != 1) return false;	// allow only left-click
	if (objMovePB != null) return false;
	// set a reference to the PageBlock table
	if (window.event.srcElement.tagName == "IMG") {	// handle header image
		objMovePB = window.event.srcElement.parentElement.parentElement.parentElement.parentElement;
	} else {
		objMovePB = window.event.srcElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
	}
	pbArea = objMovePB.parentElement.parentElement.parentElement.parentElement;
	
	// set the Page Block Area to get offset from Left and Top (toolbar)
	pbArea.style.position = "relative";
	
	//handle the Page Block
	objMovePB.style.position = "relative";	// set this to get pb position
	objMovePB.style.left = objMovePB.offsetLeft;
	objMovePB.style.top = objMovePB.offsetTop;
	objMovePB.style.width = objMovePB.offsetWidth;
	objMovePB.style.zIndex = 1;
	objMovePB.style.cursor = "move";
	objMovePB.style.position = "absolute";
	objMovePB.style.filter = "progid:DXImageTransform.Microsoft.Alpha( style=0,opacity=85);";

//alert(objMovePB.parentElement.outerHTML.substr(objMovePB.parentElement.outerHTML.length-600,objMovePB.parentElement.outerHTML.length));	
	// check for blank column & show message if blank
	if (objMovePB.parentElement.children.tags("TABLE").length == 1) {
//		objMovePB.parentElement.children.tags("HR").item(0).previousSibling.removeNode(true);
		objMovePB.parentElement.children.tags("HR").item(0).insertAdjacentHTML("beforeBegin",strBlankColMsg);
	}
	if (objMovePB.parentElement.children.tags("TABLE").length > 1) {
		if (objMovePB.parentElement.children.tags("TABLE")[objMovePB.parentElement.children.tags("TABLE").length - 2].id.indexOf('pb') != 0 ||
				objMovePB.parentElement.children.tags("TABLE")[objMovePB.parentElement.children.tags("TABLE").length - 2].id.length < 3) {
//			objMovePB.parentElement.children.tags("HR").item(0).insertAdjacentHTML("beforeBegin",strBlankColMsg);
		}
	}
	
	//remove the spacer (<BR>)
	if(objMovePB.nextSibling != null) {objMovePB.nextSibling.removeNode(true);}
	//handle the cursor relative to the Page Block
	curOffsetX = window.event.offsetX;
	curOffsetY = window.event.offsetY;
	// adjust for screen scrolled position
	curOffsetX = curOffsetX - document.body.scrollLeft;
	curOffsetY = curOffsetY - document.body.scrollTop;
	// adjust for any parent object offsets
	var objSrc = window.event.srcElement;
	while (objSrc != objMovePB) {
		curOffsetX = curOffsetX + objSrc.offsetLeft;
		curOffsetY = curOffsetY + objSrc.offsetTop;
		objSrc = objSrc.parentElement;
	}
	objMovePB.setCapture();
	// CreateMask Under PB
//	if (document.getElementById('pbMask') == null) {
//		var sMask = '<IFRAME id=pbMask src="about:blank" frameBorder=0 width=100 height=100 scrolling=no style="DISPLAY:none;"></IFRAME>';
//		document.body.insertAdjacentHTML('beforeEnd',sMask);
//	}
//	var oMask = document.getElementById('pbMask');
//	oMask.style.display = '';
//	oMask.style.width = objMovePB.style.width;
//	oMask.style.left = -1;
//	oMask.style.top = 3;
//	oMask.style.height = 100;
//	oMask.style.height = '100%';
//	objMovePB.style.zIndex = -1;
//	oMask.style.position = 'absolute';
//	objMovePB.rows[0].cells[0].insertAdjacentElement('afterBegin',oMask);
}
/*	Info needed for each Page Block
	PageBlock ID, Current Column, Left Position, Top Position
	Will need to compare Lefts & Tops with other PageBlocks
	in order to drop them in the right position & adjust all. */
function pbMouseUp() {
	if (objMovePB == null) return false;
	// find nearest block position
	var intInsertColumn = pbGetInsertColumn();
	var intInsertPosition = pbGetInsertPosition(intInsertColumn);
	var ret = pbInsertObject(intInsertColumn, intInsertPosition);
	// clean up
	objMovePB.removeAttribute("style")
	objMovePB.style.position = "relative";
	objMovePB.style.cursor = "";
	objMovePB.style.zIndex = 0;
	objMovePB.releaseCapture();
	objMovePB = null;
	toggleSavePageLayout();
}
function pbMouseMove() {
	if (objMovePB == null) return false;
	//if (window.event.srcElement.style.cursor=="") return false;
	objMovePB.style.left = window.event.clientX - pbArea.offsetLeft - curOffsetX;
	objMovePB.style.top = window.event.clientY - pbArea.offsetTop - curOffsetY;
}
function pbInsertObject(intColumn, intPosition) {
	// update the Layout
	//var strLayout = GetCookie('Page' + intPageID + 'Layout');
	var strLayout = GetCookie('PageLayout');
	var strPB = getObjIDandStat(objMovePB, strLayout);
	// keep the cookie string clean
	var arrLayout = strLayout.split(',');
	var arrCol = arrLayout[intColumn - 1].split('.');
	var intFixed = pbGetColObj(intColumn).children.tags("TABLE").length - (arrCol.length - 1);
	// adjust if PB was moved from this column & PB moved down
	var bBelowCurrent = false;
	if (arrLayout[intColumn - 1].indexOf('.' + strPB) >= 0) {
		var i = 0;
		while (i < arrCol.length) {
			i++;
			if (arrCol[i] == strPB) {
				if (i <= intPosition) bBelowCurrent = true;
			}
		}
	}
	strLayout = strLayout.replace('.' + strPB,'');
	var arrLayout = strLayout.split(',');
	var arrCol = arrLayout[intColumn - 1].split('.');
	if (intPosition < intFixed - 1) intPosition = intPosition + intFixed
	if (bBelowCurrent) {
		arrCol[intPosition - intFixed] = arrCol[intPosition - intFixed] + '.' + strPB;
	} else {
		arrCol[intPosition - intFixed + 1] = arrCol[intPosition - intFixed + 1] + '.' + strPB;
	}
	arrLayout[intColumn - 1] = arrCol.join('.');
	//SetCookie('Page' + intPageID + 'Layout', arrLayout.join(','));
	SetCookie('PageLayout', arrLayout.join(','));
	if (intPosition - intFixed == -1) {
		var objInsert = pbGetColObj(intColumn);	// no other PBs, use the column
		if (objInsert.children.tags("SPAN").length > 0) objInsert.children.tags("SPAN").item(0).removeNode(true);
	}
	if (intPosition == -1) {
//		objInsert.style.display = "none";	//shows any dropdowns in the Page Block
//		objInsert.style.display = "";	//shows any dropdowns in the Page Block
		var objInsert = pbGetColObj(intColumn);	// no other PBs, use the column
		objInsert.insertAdjacentHTML("afterBegin", strPBSpacer);
		objInsert.insertAdjacentElement("afterBegin", objMovePB);
		return true;
	}
	var objInsert = pbGetColObj(intColumn).children.tags("TABLE").item(intPosition)
//alert(objInsert.tagName + '\n' + objInsert.id)
	objInsert.insertAdjacentElement("afterEnd", objMovePB);
	objInsert.insertAdjacentHTML("afterEnd", strPBSpacer);
	return true;
}
function pbGetInsertPosition(intColumn) {	// -1 means Top, >= 0 mean after #
	var objColumn = pbGetColObj(intColumn);
	if (objColumn.children.tags("TABLE").length == 0) return -1; //-2;
	if (objColumn.children.tags("TABLE").length == 1 && objColumn.children.tags("TABLE").item(0).id == objMovePB.id) return -1; //-2;
	var index = objColumn.children.tags("TABLE").length - 1;
	var intPBCenter = objMovePB.style.posTop + (objMovePB.style.posHeight / 2);
	//var strLayout = GetCookie('Page' + intPageID + 'Layout');
	var strLayout = GetCookie('PageLayout');
	intStaticPBs = 0;
	while (index >= 0) {
		if (objColumn.children.tags("TABLE").item(index).id != objMovePB.id) {
			// ----- check if the pb is mandatory (not in the cookie)
//			if (strLayout.indexOf((objColumn.children.tags("TABLE").item(index).id)+'S') < 0
// 				&& strLayout.indexOf((objColumn.children.tags("TABLE").item(index).id)+'H') < 0) intStaticPBs++;
			// ----- check the pb's y with the pb in the column
			if (intPBCenter >= objColumn.children.tags("TABLE").item(index).offsetTop) return index;
		}
		index--;
	}
	return -1;
}
function pbGetInsertColumn() {
//alert(window.event.clientX);
	var intColumns = (pbArea.rows[0].cells.length + 1) / 2;
	var intColumnIndexOffset = pbArea.rows[0].cells[0].id.replace('pbCol','')-1;
	var index = intColumns;
	var intLeftColumnX, intRightColumnX;
	var intPBCenter = objMovePB.style.posLeft + (objMovePB.style.posWidth / 2);
	while (index > 0) {
		if (index == intColumns) intLeftColumnX = pbGetColObj(index + intColumnIndexOffset).offsetLeft;
		intRightColumnX = intLeftColumnX;
		intLeftColumnX = pbGetColObj(index + intColumnIndexOffset).offsetLeft;
		if (index == intColumns) {
			if (intPBCenter >= intRightColumnX) return index + intColumnIndexOffset;
		}
		if (index == 1) {
			if (intPBCenter <= intLeftColumnX) return index + intColumnIndexOffset;
		}
		// assume that we should check between 2 columns
		if (intPBCenter <= intRightColumnX && intPBCenter >= intLeftColumnX) return index + intColumnIndexOffset;
		index = index - 1;
	}
	return 0;
}
function pbGetColObj(index) {
	return eval("document.all.pbCol" + index);	//orig= return document.all("pbCol" + index);
}
// spacer function
var objMovePBSpcr = null;
function pbSpcrMouseDown(objColumn) {
	if (window.event.button != 1) return false;	// allow only left-click
	if (objMovePBSpcr != null) return false;
	// set a reference to the Spacer
	objMovePBSpcr = window.event.srcElement;
pbArea = objMovePBSpcr.parentElement.parentElement.parentElement.parentElement.parentElement;
	// set the Page Block Area to get offset from Left and Top (toolbar)
	pbArea.style.position = "relative";
	// set the reference to the column to resize
	objMovePBSpcr = objColumn;
	objMovePBSpcr.setCapture();
}
function pbSpcrMouseUp() {
	if (objMovePBSpcr == null) return false;
	//var arrLayout = GetCookie('Page' + intPageID + 'Layout').split(',');
	var arrLayout = GetCookie('PageLayout').split(',');
	//if (objMovePBSpcr.id.replace('pbCol','') == "1") {
		var intCol = 0;
		intCol = objMovePBSpcr.id.replace('pbCol','')-1;
//alert(intCol + '\n' + objMovePBSpcr.id);
	//} else {
	//	var intCol = arrLayout.length - 1;
	//}
	var arrCol = arrLayout[intCol].split('.');
	arrCol[0] = arrCol[0].substr(0,arrCol[0].indexOf(':') + 1) + objMovePBSpcr.width;
	arrLayout[intCol] = arrCol.join('.');
	//SetCookie('Page' + intPageID + 'Layout', arrLayout.join(','));
	SetCookie('PageLayout', arrLayout.join(','));
	pbArea.style.position = "";
	objMovePBSpcr.releaseCapture();
	objMovePBSpcr = null;
	toggleSavePageLayout();
}
function pbSpcrMouseMove() {
	if (objMovePBSpcr == null) return false;
	var intPercent = ((window.event.clientX - pbArea.offsetLeft) / pbArea.offsetWidth * 100);
	intPercent = Math.round(intPercent);
	//if (objMovePBSpcr.id != "pbCol1") intPercent = 100 - intPercent;
//alert(objMovePBSpcr.cellIndex + '\n' + objMovePBSpcr.parentElement.cells.length);
	if (objMovePBSpcr.cellIndex == objMovePBSpcr.parentElement.cells.length-1) intPercent = 100 - intPercent;
	if (intPercent > 45) intPercent = 45;
	if (intPercent < 15) intPercent = 15;
	objMovePBSpcr.width = intPercent + "%";
}
function togglePB(objPB) {
	// get the Page Block Title Bar Object
	objPB = objPB.parentElement.parentElement.parentElement; //.parentElement;
	var dispStatus = objPB.nextSibling.style.display;
	if (dispStatus == '') objPB.nextSibling.style.display = 'none';
	if (dispStatus == 'none') objPB.nextSibling.style.display = '';
	if (dispStatus == '') objPB.nextSibling.nextSibling.style.display = 'none';
	if (dispStatus == 'none') objPB.nextSibling.nextSibling.style.display = '';
	var pbID = objPB.parentElement.parentElement.parentElement.parentElement.id;
	//var strLayout = GetCookie('Page' + intPageID + 'Layout');
	var strLayout = GetCookie('PageLayout');
	if (strLayout == null) strLayout = '';
	//if (dispStatus == '') SetCookie('Page' + intPageID + 'Layout', strLayout.replace(pbID + 'S', pbID + 'H'));
	if (dispStatus == '') SetCookie('PageLayout', strLayout.replace(pbID + 'S', pbID + 'H'));
	//if (dispStatus == 'none') SetCookie('Page' + intPageID + 'Layout', strLayout.replace(pbID + 'H', pbID + 'S'));
	if (dispStatus == 'none') SetCookie('PageLayout', strLayout.replace(pbID + 'H', pbID + 'S'));
	toggleSavePageLayout();
	var oPBContent = eval('pbContent' + pbID.substring(2));
	if(oPBContent.innerHTML=='Loading Content...'){
		jsControl.src = document.location.href + '&hmode=js&id=' + pbID.substring(2) + '&tagProp=pbContent' + pbID.substring(2);
	}
}
function refreshPB(objPB, sURL) {
	var intPBID = objPB.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.id.substring(2);
	var contentWidth = eval('pbContent' + intPBID).offsetWidth-100;
	eval('pbContent' + intPBID).insertAdjacentHTML('afterBegin','<SPAN style="POSITION:absolute;TOP:10px;LEFT:' + contentWidth/2 + 'px;COLOR:red;BACKGROUND-COLOR:white;BORDER:1px solid black">Refreshing...</SPAN>');
	jsControl.src = document.location.href + '&hmode=js&id=' + intPBID + '&tagProp=pbContent' + intPBID + '&gwURL=' + sURL;
	//window.location.href = 'Gateway.aspx?hmode=js&id=' + intPBID + '&tagProp=pbContent' + intPBID + '&gwURL=' + sURL;
	//alert(eval('pbContent' + intPBID).innerHTML);
}
var maxPBID = '';
function maxPB(objPB) {
	var objButton = objPB;
	// get the Page Block Object
	objPB = objPB.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;
	pbArea = objPB.parentElement.parentElement.parentElement.parentElement;
	if (maxPBID == '') {
		maxPBID = objPB.id;
		if (pbArea.rows.length < 2) {
			pbArea.insertRow();
			pbArea.rows(1).insertCell();
			pbArea.rows(1).cells(0).colSpan = pbArea.rows(0).cells.length;
		}
		objButton.value = '2';
		pbArea.rows(1).cells(0).innerHTML = objPB.outerHTML.replace('pbMouseDown()','"return false;"').replace('title=','').replace('"Delete Block" style="','Delete Block" style="DISPLAY:none;');
		objButton.value = '1';
		pbArea.rows(0).style.display = 'none';
		pbArea.rows(1).style.display = '';
		window.scrollTo(0,0);
	} else {
		maxPBID = '';
		pbArea.rows(1).style.display = 'none';
		pbArea.rows(0).style.display = '';
	}
}
function delPB(objPB) {
	// check if we're maximized (Now, no need - Delete button should be hidden)
	if (maxPBID != '') {maxPBID = '';pbArea.rows(0).style.display = '';return false;}
	// get the Page Block Object
	objPB = objPB.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement;//.parentElement;
	//var strLayout = GetCookie('Page' + intPageID + 'Layout');
	var strLayout = GetCookie('PageLayout');
	//SetCookie('Page' + intPageID + 'Layout', strLayout.replace(objPB.id + 'S', objPB.id + 'D').replace(objPB.id + 'H', objPB.id + 'D'));
	//SetCookie('Page' + intPageID + 'Layout', strLayout.replace('.' + objPB.id + 'S', '').replace('.' + objPB.id + 'H', ''));
	SetCookie('PageLayout', strLayout.replace('.' + objPB.id + 'S', '').replace('.' + objPB.id + 'H', ''));
	objPB.nextSibling.removeNode(true);	//remove the spacer (<BR>)
	// check for blank column & show message if blank
	if (objPB.parentElement.children.tags("TABLE").length == 1) {
		objPB.parentElement.children.tags("HR").item(0).insertAdjacentHTML("beforeBegin",strBlankColMsg);
	}
	objPB.removeNode(true);	// remove the Page Block and children
	toggleSavePageLayout();
}
function getObjIDandStat(obj, strLayoutCookie) {
	if (strLayoutCookie.indexOf(obj.id + 'S') >= 0) return obj.id + 'S';
	if (strLayoutCookie.indexOf(obj.id + 'H') >= 0) return obj.id + 'H';
}
function toggleSavePageLayout() {
	if (document.getElementById('savePageLayout') == null) return;
	frmPortal.savePageLayout.value = "Save Layout";
	frmPortal.savePageLayout.style.color = 'red';
	frmPortal.savePageLayout.style.fontWeight = 'bold';
	frmPortal.savePageLayout.disabled = false;
}
