//function linkMouseOver() { }
//function linkMouseOut(){ }

function flashDiv(targetDivId, color1, color2, counter)
{
	if (counter == null)
	{
		counter = 0;
	}
	
	counter++;
	
	if (counter < 10)
	{
		if (document.getElementById(targetDivId).style.backgroundColor == color1)
		{
			document.getElementById(targetDivId).style.background = color2;
		}
		else
		{
			document.getElementById(targetDivId).style.background = color1;
		}
		setTimeout("flashDiv('" + targetDivId + "','" + color1 + "','" + color2 + "'," + counter + ")", 100);
	}
	else
	{
		setTimeout("document.getElementById('" + targetDivId + "').style.background = '" + color1 + "'", 100);
				
	}
}

function ajaxFunction()
{
	var xmlHttp = null;

	try {    // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}	catch (e)	{    // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
			}
		}
	}
	return xmlHttp;
}

function scrollToBottom(targetDiv)
{
	if (targetDiv)
	{
		targetDiv.scrollTop = targetDiv.scrollHeight;	
	}
}

function openChatWindow()
{
	var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	var windowHeight = 510;
	var windowWidth = 550;
	var toolbar = 0;
	var resizeable = 1;
	
	if (isChrome)
	{
		windowHeight += 35;
	}
	
	
	childWindow = window.open('/Chat.action','LiveChat', 'left=20,top=20,width=' + windowWidth + ',height=' + windowHeight + ',toolbar=' + toolbar + ',resizable=' + resizeable);
}

function hideTipPopups()
{
	var url = '/foundation/HideTipPopups_HideTipPopups.action?&tm=' + (new Date()).getTime();

	var xmlHttp = ajaxFunction();

	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			try
			{
				var formFieldContainer = JSON.parse(xmlHttp.responseText);

				if(formFieldContainer.jsonStatus == 'OK')
				{
					window.location.reload(true);
				}

			}catch(err){
				alert('AJAX Error: ' + err.name);
			}
		}
	}

	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}

function replaceContnetAJAX(divName, url)
{
	var xmlHttp = ajaxFunction();

	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			var divObject = document.getElementById(divName);

			divObject.innerHTML=xmlHttp.responseText;
		}
	}

	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}

function replaceTxt(obj, text, equalsText)
{
	if(equalsText == obj.value)
	{
		obj.value = text;
	}
}

function clearSelectOptions(optionList)
{
	 for (x = optionList.length; x >= 0; x--)
	 {
			optionList[x] = null;
	 }
}


function addSelectOption(optionList, optionValue, optionText)
{
	 optionList[optionList.length] = new Option(optionText, optionValue);
}

function findPos(obj) 
{
	var point = new Object();
	
	point.x = 0;
	point.y = 0;
	
	if (obj.offsetParent)
	{
		do 
		{
			point.x += obj.offsetLeft;
			point.y += obj.offsetTop;
		}while (obj = obj.offsetParent);
	}
	return point;
}

function openModalPopup(id)
{
	if (document.getElementById(id))
	{
		disableBackground();
		showDiv(id);
	}
}

function closeModalPopup(id)
{
	enableBackground();
	hideDiv(id);
	
}

function disableBackground()
{
	if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) )
	{
			var pageWidth = document.body.scrollWidth+'px';
			var pageHeight = document.body.scrollHeight;
	}
	else if( document.body.offsetWidth )
	{
		var pageWidth = document.body.offsetWidth+'px';
		var pageHeight = document.body.offsetHeight;
	}
	
	//Make sure full height in FF
	if (pageHeight && window.innerHeight && window.innerHeight > pageHeight)
	{
		pageHeight = window.innerHeight;
	}
	else if (!pageHeight && window.innerHeight && window.innerHeight > 0) 
	{
		pageHeight = window.innerHeight;
	}
	//Make sure full height in IE
	if (pageHeight && document.documentElement.clientHeight && document.documentElement.clientHeight > pageHeight)
	{
		pageHeight = window.innerHeight;
	}
	else if (!pageHeight && document.documentElement.clientHeight && document.documentElement.clientHeight > 0) 
	{
		pageHeight = window.innerHeight;
	}
	
	if (pageHeight)
	{
		pageHeight += 'px';
	}
	
	if (!pageHeight || !pageWidth)
	{
		var pageWidth='100%';
		var pageHeight='100%';
	}

	document.getElementById('greyPopupBG').style.height = pageHeight;
	document.getElementById('greyPopupBG').style.width = pageWidth;
	document.getElementById('greyPopupBG').style.display='block';
}

function enableBackground()
{
	document.getElementById('greyPopupBG').style.display='none';
}

function disableBackground1550()
{
	if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) )
	{
			var pageWidth = document.body.scrollWidth+'px';
			var pageHeight = document.body.scrollHeight+'px';
	}
	else if( document.body.offsetWidth )
	{
		var pageWidth = document.body.offsetWidth+'px';
		var pageHeight = document.body.offsetHeight+'px';
	}
	else
	{
		var pageWidth='100%';
		var pageHeight='100%';
	}

	document.getElementById('greyPopupBG_1550').style.height = pageHeight;
	document.getElementById('greyPopupBG_1550').style.width = pageWidth;
	document.getElementById('greyPopupBG_1550').style.display='block';
}

function enableBackground1550()
{
	document.getElementById('greyPopupBG_1550').style.display='none';
}

function toggleSection(link, div) 
{
	var linkOne = '[-]';
	var linkTwo = '[+]';
	if (div && link)
	{
		if (link.innerHTML == linkOne) 
		{
			link.innerHTML = linkTwo;
		}
		else
		{
			link.innerHTML = linkOne;
		}
		var divObj = document.getElementById(div);
		
		if(divObj.style.display == 'none')
		{
			divObj.style.display = '';
			divObj.style.height = 0;
		}else{
			divObj.style.display = 'none';
		}
	}
}

function toggleDivCloseText(link, divName)
{
	var linkOne = link;
	var linkTwo = '<strong>Close</strong>';
	if (divName && link)
	{
		if (link == linkOne) 
		{
			link.innerHTML = linkTwo;
		}
		else
		{
			link.innerHTML = linkOne;
		}
		
		var divObj = document.getElementById(divName);
		if(divObj.style.display == 'none')
		{
			divObj.style.display = 'block';
		}else{
			divObj.style.display = 'none';
		}
	}
}

function toggleDiv(divName)
{
	var divObj = document.getElementById(divName);
	
	if(divObj.style.display == 'none')
	{
		divObj.style.display = 'block';
	}else{
		divObj.style.display = 'none';
	}
}

function hideDiv(divName)
{
	var divObj = document.getElementById(divName);

	if(divObj != null)
	{
		divObj.style.display = 'none';
	}
}

function showDiv(divName)
{
	var divObj = document.getElementById(divName);

	if(divObj != null)
	{
		divObj.style.display = 'block';
	}
}

function toggleSpan(divName)
{
	var divObj = document.getElementById(divName);
	
	if(divObj.style.display == 'none')
	{
		divObj.style.display = 'inline';
	}else{
		divObj.style.display = 'none';
	}
}

function popupHelp(helpId)
{	
	var popupDiv = document.getElementById('popoverListDiv');
	if(popupDiv != null)
	{
		replaceContnetAJAX('popoverListDiv', '/PopupHelp.action?helpId=' + helpId + '&js=true&time=' + (new Date().getTime()));

		var yOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
		var pageWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;

		popupDiv.style.top = (yOffset + 50) + 'px';
		popupDiv.style.left = ((pageWidth / 2) - 300) + 'px';
		popupDiv.style.border = '';
		
		popupDiv.style.display = 'block';
		
		disableBackground();		
	}
}

function closeHelp()
{	
	var popupDiv = document.getElementById('popoverListDiv');
	if(popupDiv != null)
	{
		var yOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
		var pageWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;

		popupDiv.style.top = (yOffset + 50) + 'px';
		popupDiv.style.left = ((pageWidth / 2) - 300) + 'px';
		popupDiv.style.border = '';
		
		popupDiv.innerHTML = '';
		popupDiv.style.display = 'none';
		
		enableBackground();		
	}
}

function popupWorkflowHelp(workflowId)
{	
	var popupDiv = document.getElementById('popoverListDiv');
	if(popupDiv != null)
	{
		if(workflowId == 'all-either_agent')
		{
			replaceContnetAJAX('popoverListDiv', '/help/all-either_agent.jsp');
		}
		else if(workflowId == 'buyer-buyers_agent')
		{
			replaceContnetAJAX('popoverListDiv', '/help/buyer-buyers_agent.jsp');
		}
		else if(workflowId == 'seller-sellers_agent')
		{
			replaceContnetAJAX('popoverListDiv', '/help/seller-sellers_agent.jsp');
		}
		else if(workflowId == 'agent-eithers_agent')
		{
			replaceContnetAJAX('popoverListDiv', '/help/agent-eithers_agent.jsp');
		}
		var yOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
		var pageWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;

		popupDiv.style.top = (yOffset + 50) + 'px';
		popupDiv.style.left = ((pageWidth / 2) - 500) + 'px';
		popupDiv.style.border = '';
		
		popupDiv.style.display = 'block';
		
		disableBackground();
	}
}

function closeWorkflowHelp()
{	
	var popupDiv = document.getElementById('popoverListDiv');
	if(popupDiv != null)
	{
		var yOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
		var pageWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;

		popupDiv.style.top = (yOffset + 50) + 'px';
		popupDiv.style.left = ((pageWidth / 2) - 300) + 'px';
		popupDiv.style.border = '';
		
		popupDiv.innerHTML = '';
		popupDiv.style.display = 'none';
		
		enableBackground();		
	}
}

function dialogBoxShow(divId)
{
	var popupDiv = document.getElementById(divId);
	if(popupDiv != null && document.getElementById('popoverListDiv'))
	{
		var afterDiv = document.getElementById('popoverListDiv');
		afterDiv.innerHTML = popupDiv.innerHTML;
		showDiv(afterDiv.id);
		
		disableBackground();
		
		var yOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
		var pageWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;


		afterDiv.style.top = (yOffset + 50) + 'px';
		afterDiv.style.left = ((pageWidth / 2) - 500) + 'px';
		afterDiv.style.border = '';
		
		afterDiv.style.display = 'block';		
	}
}


function dialogBoxClose(divId)
{	
	var popupDiv = document.getElementById('popoverListDiv');
	if(popupDiv != null)
	{
		var yOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
		var pageWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;

		popupDiv.style.top = (yOffset + 50) + 'px';
		popupDiv.style.left = ((pageWidth / 2) - 300) + 'px';
		popupDiv.style.border = '';
		
		//popupDiv.innerHTML = '';
		popupDiv.style.display = 'none';
		
		enableBackground();		
	}
}

function popupCreateLoopExplain(loopDivId)
{	
	var popupDiv = document.getElementById(loopDivId);
	if(popupDiv != null)
	{
		showDiv(loopDivId);
		
		disableBackground();
		
		var yOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
		var pageWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;


		popupDiv.style.top = (yOffset + 50) + 'px';
		popupDiv.style.left = ((pageWidth / 2) - 500) + 'px';
		popupDiv.style.border = '';
		
		popupDiv.style.display = 'block';		
	}
}

function closePopupLoopExplain(loopDivId)
{	
	var popupDiv = document.getElementById(loopDivId);
	if(popupDiv != null)
	{
		var yOffset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
		var pageWidth = window.innerWidth ? window.innerWidth : document.body.clientWidth;

		popupDiv.style.top = (yOffset + 50) + 'px';
		popupDiv.style.left = ((pageWidth / 2) - 300) + 'px';
		popupDiv.style.border = '';
		
		//popupDiv.innerHTML = '';
		popupDiv.style.display = 'none';
		
		enableBackground();		
	}
}

function textLineValidation(control, charsPerLine, maxLines)
{
	if(countLines(control, charsPerLine) > maxLines)
	{
		if(control.style.backgroundColor.toUpperCase() != '#EFD8E5' && control.style.backgroundColor.toUpperCase() != 'RGB(239, 216, 229)')
		{
			alert('There may be more text in this field than will print on the physical form.');
			
		}
		control.style.backgroundColor = '#EFD8E5';
	}else{
		control.style.backgroundColor = 'transparent';
	}
}

function countLines(control, charsPerLine)
{
	var text = control.value;
	var lineCount = 0;

	if(text != null)
	{
		text = trim(text);

		var lineArray = text.split("\n");		

		for(var x = 0 ; x < lineArray.length ; x++)
		{
			var line = trim(lineArray[x]);
			lineCount += line.length / charsPerLine;
		}
	}
	return lineCount;
}

function trim(text)
{
	return text.replace(/^\s+|\s+$/g,"");
}


function checkAllInForm(form, checked, name, idBeginsWith) 
{
	if (checked != true && checked != false && checked != 'invert')
	{
		checked = true;
	}
	
	if (document.getElementById(form))
	{
		form = document.getElementById(form);
	}
	if (form && form.elements)
	{
		for (i=0; i<form.elements.length; i++)
		{
			if (form.elements[i].type == "checkbox" && 
					(!name || form.elements[i].name == name) && 
					(!idBeginsWith || form.elements[i].id.substr(0, form.elements[i].id.indexOf('_')) == idBeginsWith))
			{
				if (!form.elements[i].disabled)
				{
					if (checked == 'invert') 
					{
						form.elements[i].checked = !form.elements[i].checked;
					}
					else
					{
						form.elements[i].checked = checked;
					}
				}
			}
		}
	}
}


function addSelectClearInvertToTbody(tbodyElementId) 
{
	if (tbodyElementId && document.getElementById(tbodyElementId)) 
	{
		var tbody = document.getElementById(tbodyElementId);
		var newRow = document.createElement('selectOptions');
		
		string = '<tr style="background: rgb(10,100,200); color: white;">' +
				'<td colspan="2" class="txtSm">' +
					'<div id="javascriptCheckAllLink" class="dspN">' +
						'<a href="#" onclick="checkAllInForm(\'allNotesList\'); return false;" style="color:white;">Select All</a>' +
						'-' +
						'<a href="#" onclick="checkAllInForm(\'allNotesList\', false); return false;" style="color:white;">Clear All</a>' +
						'-' +
						'<a href="#" onclick="checkAllInForm(\'allNotesList\', \'invert\'); return false;" style="color:white;">Invert</a>' +
					'</div>' +
				'</td>' +
				'<td></td>' +
			'</tr>' + tbody.innerHTML; 
		
	}
}

function checkAllInTable(table, checked)
{
	if (checked != true && checked != false)
	{
		checked = true;
	}
	
	if (document.getElementById(table))
	{
		table = document.getElementById(table);
	}
	
	if (table && table.rows)
	{
		for (i=0; i<table.rows.length; i++)
		{
			currentRow = table.rows[i];
			if (currentRow.cells)
			{
				for (x=0; x<currentRow.cells.length; x++)
				{
					currentCell = currentRow.cells[x];
					for (y=0; y<currentCell.childNodes.length; y++)
					{
						currentNode = currentCell.childNodes[y];
						if (currentNode.checked == true || currentNode.checked == false)
						{
							currentNode.checked = checked;
						}
					}
				}
			}
		}
	}
}



//Popup menus

allPopupMenus        = new Array();



function popupMenu(menuWidth, menuText, divID)
{
	if (!divID || divID == '') 
	{
		divID = "popUpMenu" + Math.floor(Math.random()*1000+1);
	}
	
	if (!menuText)		    { menuText          = "<span class=\"bold\" style=\"text-decoration: underline;\">More Options</span>"; }
	if (!menuWidth)			{ menuWidth         = "200px";  }
	menuBackground    = "#375A80";
	menuOpenLinkClass = "noline"; 
	menuOpenLinkStyle = "";       

	
	outerMostDivAdditionalParams = 'class="fltR" id="javaScriptButton" style="margin-left: 50px;"';
	//alert(divID);
	if (divID) 
	{
		this.linksAsArray = new Array();
		this.divID = divID;
		
		this.preHTML = '';
		this.preHTML += ("<div " + outerMostDivAdditionalParams + ">");
		this.preHTML += (	"<div class=\"posR\">");
		this.preHTML += (		"<div class=\"posA dspN\" " + 
								    " style=\"bottom:0; right:0; background-color:" + menuBackground + "; width:" + menuWidth + "\" " +
								    " onmouseout=\"popupMouseOut(this, " + allPopupMenus.length + ");\" id='" + divID + "'\"/>");
		this.preHTML += (			"<div onmouseover=\"openMenu(" + allPopupMenus.length + ");\">");
		
		this.postHTML = '';
		this.postHTML += (			"</div>");
		this.postHTML += (		"</div>");
		this.postHTML += (	"</div>");
		this.postHTML += (	"<a href=\"#\" onclick=\"openMenu(" + allPopupMenus.length + ", '" + divID + "'); return false;\" class='" + menuOpenLinkClass + "'>" + menuText + "</a>");
		this.postHTML += ("</div>");
		
		this.addLink=popupAddLink;
		this.write=writePopupMenu;
		this.open=popupMenuOpen;
		this.close=popupClose;
		this.addItem=popupAddItem;
		
		
		allPopupMenus[allPopupMenus.length] = this;
	}
}

function openMenu(menuID, setTimeoutElement) 
{
	if (allPopupMenus[menuID]) 
	{
		menuToOpen = allPopupMenus[menuID];
		menuToOpen.open();
		if (setTimeoutElement) 
		{
			menuToOpen.timer = setTimeout("popupMouseOut(document.getElementById('" + setTimeoutElement + "'), " + menuID + ");", 4000);
		}
		else 
		{
			clearTimeout(menuToOpen.timer);
		}
	}
}

function popupMenuOpen() 
{
	if (document.getElementById(this.divID)) 
	{
		div = document.getElementById(this.divID);
		div.style.display='block';
	}
}

function popupClose()
{
	if (document.getElementById(this.divID))
	{
		document.getElementById(this.divID).style.display = 'none';
	}
}

function popupMouseOut(element, menuID) 
{
	if (allPopupMenus[menuID] && element.id == allPopupMenus[menuID].divID) 
	{
			allPopupMenus[menuID].close();
			return true;
	}
}

function popupAddItem(text, onclick) 
{
	this.linksAsArray[this.linksAsArray.length] = "<div " + "style='color:#375A80; background-color:#D8E5EF; padding: 5px; margin: 2px;'" + 
												  " onmouseover=\"popupMenuOpen('" + this.divID + "');\">" + 
												  "<a href='#' onclick=\"" + onclick + "; document.getElementById('" + this.divID + "').style.display = 'none';\" class='noline blueTextLink'>" +
												  text + 
												  "</a></div>";
}

function popupAddLink(text, url, css) 
{
	this.linksAsArray[this.linksAsArray.length] = "<div " + "style='color:#375A80; background-color:#D8E5EF; padding: 5px; margin: 2px;'" + 
												  " onmouseover=\"popupMenuOpen('" + this.divID + "');\">" + 
												  "<a href='" + url + "' onclick=\"document.getElementById('" + this.divID + "').style.display = 'none';\" class='noline blueTextLink'>" +
												  text + 
												  "</a></div>";
}

function writePopupMenu()
{
		document.write(this.preHTML);
		for(i=0;i<this.linksAsArray.length;i++)
		{
			document.write(this.linksAsArray[i]);
		}
		document.write(this.postHTML);
}

//End popup menus


function checkEnter(e)
{
	if (e && e.keyCode) 
	{
		charCode = e.which;
	}
	else if (e)
	{
		charCode = e.keyCode;
	}
	
	
	if (!charCode)
	{
		if (event && event.keyCode)
		{
			charCode = event.keyCode;
		}
		else if (e && e.keyCode) 
		{
			charCode = e.which;
		}
		else if (e)
		{
			charCode = e.keyCode;
		}
	}

	if (charCode && charCode == 13)
	{
		return false;
	}
	return true;
}
