var partMover = null;
var isPosition = false;
var zIndex = 0;
var mainMenuTimeOut = null;
var menuItem = 'menuItem'
var currentMenuItem = null
window.document.onmousedown = Document_OnMouseDown;
window.document.onmousemove = Document_OnMouseMove;
window.document.onmouseup   = Document_OnMouseUp;
var d = document;
var currentTable = null
window.onresize = resizeWin

function deleteAccount(username)
{
	if (confirm('Are you sure you wish to delete this account?'))
	{
		location.href='/data/deleteuser.asp?u=' + username
	}
}

function deleteData(dateStr)
{
	if (confirm('Are you sure you wish to delete these records?'))
	{
		location.href='/data/deletedate.asp?date=' + dateStr
	}
}

function gotoPolicyPage(vD)
{
	document.all('gotoPolicy').all('valuationDate').value = vD
	document.all('gotoPolicy').submit()
}
function gotoValuationPage(pN)
{
	document.all('gotoPolicy').all('policyNumber').value = pN
	document.all('gotoPolicy').submit()
}
function gotoPolicyPageHistoric(vD)
{
	document.all('gotoPolicyHistoric').all('valuationDate').value = vD
	document.all('gotoPolicyHistoric').submit()
}
function gotoValuationPageHistoric(pN)
{
	document.all('gotoPolicyHistoric').all('policyNumber').value = pN
	document.all('gotoPolicyHistoric').submit()
}
function runIfEnter(btnName)
{
	if (event.keyCode == 13)document.all(btnName).click();
}


function activateTable(t) 
{
	if (currentTable != null && currentTable != t) deActivateTable(t);
	currentTable = t
	if (t.rows != null)
	{
		t.style.borderColor = '#6699CC';
		t.rows[0].style.backgroundColor='#6699CC';
		t.rows[1].style.backgroundColor='#6699CC';
		t.rows[0].all[0].className = 'itemsmlwhite';
		for (i=0;i<t.rows[0].all.length;i++)
		{
			if(t.rows[0].all[i].tagName == 'IMG')
			{
				switchArrow(t.rows[0].all[i])
			}
		}
	}
}


function deActivateTable(t)
{
	if (currentTable != null)
	{
		if(currentTable.rows != null)
		{
			currentTable.style.borderColor = '#808080';
			currentTable.rows[0].style.backgroundColor='#D4D0C8';
			currentTable.rows[1].style.backgroundColor='#808080';
			currentTable.rows[0].all[0].className = 'itemsml';
			for (i=0;i<currentTable.rows[0].all.length;i++)
			{
				if(currentTable.rows[0].all[i].tagName == 'IMG')
				{
					switchArrow(currentTable.rows[0].all[i])
				}
			}
		}
		currentTable = null
	}
}
function switchArrow(img)
{
	if (img.pos =='down')
	{
		if (img.src == 'http://www.pentney-hughes.co.uk/images/bluedown.gif')
		{
			img.src = '/images/graydown.gif'
		} else {
			img.src = '/images/bluedown.gif'
		}
	} else {
		if (img.src == 'http://www.pentney-hughes.co.uk/images/blueup.gif')
		{
			img.src = '/images/grayup.gif'
		} else {
			img.src = '/images/blueup.gif'
		}
		
	}
	

	
}

function toggleDisplay(t,lay)
{
	var visiLay = document.all(lay)
	if (visiLay!= null)
	{
		if (visiLay.style!= null)
		{
			if (visiLay.style.display == '')
			{
				t.pos = 'down';
				switchArrow(t)

				visiLay.style.display = 'none'
			} else {
				t.pos = 'up';
				switchArrow(t)
				visiLay.style.display = ''
			}
		}
	}
}
function resizeWin() 
{
	var containerW = parseInt(document.body.clientWidth)-60;
	var containerH = parseInt(document.body.clientHeight) -60;
	if (containerH < 600)
	{
		containerH = 600
	}
	if (containerW < 800)
	{
		containerW = 800 
	}
	var conta = document.all('container')
	if (conta != null)
	{
		conta.style.height = containerH
		conta.style.width = containerW  
	}
}
function changeImage(t,imagePath)
{
	t.src = imagePath
}
function toggleLayer(t,layerName)
{
	var layerItem = document.all(layerName)
	if (layerItem != null)
	{
		if (layerItem.style.display=='none')
		{
			layerItem.style.display = '';
			t.innerHTML = 'hide'
		} else {
			layerItem.style.display = 'none';
			t.innerHTML = 'show'
		}
	}
}
function swapImage(elementId,imgSrc)
{
	var elementImage = document.all('menuImage'+elementId);
	if (elementImage != null)
	{
		elementImage.src = imgSrc;
	}
}
function showMenu(elementId,color)
{
		doHideMenu();
		var element = document.all('menuItem'+elementId);
		var menuLayer = document.all('mainMenu'+elementId);
		var elementImage = document.all('menuImage'+elementId);
		clearTimeout(mainMenuTimeOut)
		if (menuLayer != null)
		{
			
			currentMenuItem
//			var itemId = element.id.substr(menuItem.length,element.id.length-menuItem.length)
//						d.all('menuImage'+itemId)
			doHideMenu()
			if (currentMenuItem != null){
				doHideMenu();
			}
			currentMenuItem = menuLayer
			menuLayer.style.left = getRealLeft(elementImage)-parseInt(menuLayer.style.width)+5 + elementImage.width-28
			menuLayer.style.top = getRealTop(elementImage)+15-30
//			d.all('mainMenu').style.backgroundColor = color;
			menuLayer.style.visibility = 'visible'
		}
}
function clearHide()
{
	clearTimeout(mainMenuTimeOut)
}
function hideMenu()
{
		mainMenuTimeOut = setTimeout('doHideMenu()',3000);
}
function doHideMenu()
{
		clearTimeout(mainMenuTimeOut)
		if (currentMenuItem != null)
		{
			currentMenuItem.style.visibility = 'hidden';
			currentMenuItem.style.left = 0;
			currentMenuItem.style.top = 0;
			currentMenuItem = null;
		}
}

function getRealLeft(imgElem) {
	xPos = 0;
	if (imgElem.id != 'image') {
		xPos = eval(imgElem).offsetLeft;
	}
	tempEl = eval(imgElem).offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}
function getRealTop(imgElem) {

	yPos = eval(imgElem).offsetTop;
	tempEl = eval(imgElem).offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}
function writeHello()
{
	return 'Hello';
}
function GetAbsoluteTop(elem)
{
	var topPosition = 0;

	while (elem)
	{
		if (elem.tagName == 'BODY')
		{
			break;
		}
		topPosition += elem.offsetTop;
		elem = elem.offsetParent;
	}
	return topPosition;
}

function GetAbsoluteLeft(elem)
{
	var leftPosition = 0;

	while (elem)
	{
		if (elem.tagName == 'BODY')
		{
			break;
		}
		leftPosition += elem.offsetLeft;
		elem = elem.offsetParent;
	}

	return leftPosition;
}


function TraverseToClassName(srcElement,ClassName)
{
	while(srcElement.className != ClassName)
	{
		srcElement = srcElement.parentElement;
		if (srcElement == null)
			return null;
	}
	return srcElement;
}

function Document_OnMouseDown()
{	
	//--Reinitialize partMover variable
	partMover = null;

	var m_prtSrc = window.event.srcElement;
	var tagName = event.srcElement.tagName.toUpperCase();
	if (tagName == "IMG" || tagName == "A" || tagName == "TD" || tagName == "SPAN")
		return;

	var m_tabElement = TraverseToClassName(m_prtSrc, "tableView");
	if (!m_tabElement)
		return;

	var m_classElement = TraverseToClassName(m_prtSrc, "tableLayer");
	if (m_classElement == null)
		return;

	var strFrame = null;
	partMover = m_classElement;
	zIndex++
	partMover.style.zIndex = zIndex
//	for (var i = 0; i < arrParts.length; i++)
//	{
//		var divPart = partMover.all.item(arrParts[i]);
//		if (divPart)
//		{
//			strFrame = divPart.id;
//			break;
//		}
//	}
	
//	m_zoneElement = TraverseToClassName(m_prtSrc, "DashZoneRight");
	
//	if ((zoneLeft == -1) || (zoneTop == -1))
//	{
//		zoneLeft = GetAbsoluteLeft(m_zoneElement);
//		zoneTop = GetAbsoluteTop(m_zoneElement);
//	}

//	frmholder =  m_zoneElement.all.item(strFrame + "Holder"); 
//	frmholder.style.height = partMover.offsetHeight;
//	frmholder.style.display = "inline";
	
	partMover.style.position = 'absolute';
	partMover.style.width = partMover.offsetWidth;
	partMover.style.pixelLeft = event.x + document.body.scrollLeft
//	alert(document.body.scrollTop)
	partMover.style.pixelTop = event.y + document.body.scrollTop;
	
	orgLeft = partMover.style.pixelLeft;
	orgTop = partMover.style.pixelTop;
	partMover.style.display = "block";

	event.cancelBubble = true;
    isPosition = true;
    partMover.setCapture();
}
function Document_OnMouseMove()
{
		if(!isPosition || !partMover)
			return;

		partMover.style.pixelLeft = event.x + document.body.scrollLeft;
		partMover.style.pixelTop = event.y + document.body.scrollTop;
		event.cancelBubble = true;
		blnMoved = true;
}
function Document_OnMouseUp()
{
	if (!partMover) return;

	isPosition = false;

	partMover.releaseCapture();
//	partMover.style.position = 'static';
	var m_prtSrc = window.event.srcElement;
//	if (!CheckXPos(m_zoneElement) || !CheckYPos(m_zoneElement) || !blnMoved)
//	{
//		trackInfoForm(("HP_WPFound_"+(blnBorder? "BorderOn" : "BorderOff")));
//		SetBorderOff();
//		return;
//	}
//
//	PartSwap();
//	saveState();
	blnMoved = false;
//	trackInfoForm(("HP_WPSwap_"+(blnBorder? "BorderOn" : "BorderOff")));

//	SetBorderOff();
	
	event.cancelBubble = true;
}
function confirmBox(str) 
{ 
	return confirm(str + '\n\nClick \'OK\' to proceed or \'Cancel\' to cancel the operation.') 
} 
