
function refresh(){
	document.forms[0].method.value='refresh'; 
	document.forms[0].submit();
}

function refreshProposal(method){
	document.forms[0].method.value=method; 
	document.forms[0].submit();
}

function performAction(action) {
	if(action == 'delete') {
		var agree=confirm('Delete this record, are you sure?');
		if (agree) {
			document.forms[0].method.value = action;
			document.forms[0].submit();
		}
	} 
	else if(action == 'saveAsOfficial') {
		var statusId = document.forms[0].status.value;
		var officialId = document.forms[0].officialId.value;
		var unofficialId = document.forms[0].unofficialId.value;
		var agree;
		if(statusId == officialId)
			agree=confirm('Set this place name status to Official, are you sure?');
		else if(statusId == unofficialId)
			agree=confirm('Set this place name status to Unofficial, are you sure?');
			
		if (agree) {
			document.forms[0].method.value = action;
			document.forms[0].submit();
		}
	}
	else {
		document.forms[0].method.value = action;
		document.forms[0].submit();
	}
}

function performActionParam(action, dataGridRecordId) {
	if(action == 'deleteDesc' || action == 'deleteLga') {
		var agree=confirm('Delete this record, are you sure?');
		if (agree) {
			document.forms[0].method.value = action;
			document.forms[0].dataGridRecordId.value = dataGridRecordId;
			document.forms[0].submit();
			}
	}
	else if(action == 'saveDesc')
	{
		//var desc = document.forms[0]['featureDescription.description'].value;
		var desc = document.forms[0].description.value;
		
		if(trim(desc) == "")
		{
			document.forms[0].method.value = "edit";
			document.forms[0].submit();
		}
		else
		{
			document.forms[0].method.value = action;
			document.forms[0].dataGridRecordId.value = dataGridRecordId;
			document.forms[0].submit();
		}
	}
	else { 
		document.forms[0].method.value = action;
		document.forms[0].dataGridRecordId.value = dataGridRecordId;
		document.forms[0].submit();
	}
}

function collapseRows(collapse, tableId) 
	{
		var rows = document.getElementById(tableId).rows;
				
		for(ii = 0; ii < rows.length; ii++) 
		{
			if(rows[ii].className == "collapse") 
			{
				if(collapse == 'true' || collapse == true) 
				{
					if(document.all && document.compatMode && document.compatMode == "CSS1Compat" && !window.opera)
					{
						rows[ii].style.display = "block";
						// Mozilla prior to 1.8a2, Opera 7.x and MSIE 5+
					} 
					else if(document.getElementById && document.getElementById(tableId).rows) 
					{
						rows[ii].style.display = "table-row";
					}		
				} 
				else 
				{
						rows[ii].style.display = "none";
				}
			}
		}		
	}
		
//function enableNotifyAuthority(event, locked) {
//	
//	if(!locked)
//		{
//		var conformsElement = document.forms[0]['proposal.conforms'];
//		var publishedDateElement = document.forms[0]['proposal.gazettePublished'];
//		var notifyAuthorityBtn = document.getElementById('notifyAuthorityButton'); 
//		
//		if(conformsElement != null && publishedDateElement != null) {
//			if(conformsElement[0].checked && publishedDateElement.value.length > 0)
//				{
//				notifyAuthorityBtn.disabled = false;
//				} else notifyAuthorityBtn.disabled = true;		
//			}	
//		}	
//	}
		
function notifyAuthority(stateSignificant)
	{
		if(stateSignificant == undefined)
		{
			performAction('notifyAuthority');
		} 
		else if(stateSignificant)
		{
			alert('This proposal is state significant - you cannot notify the Naming Authority.');
		}
		else 
		{
		performAction('notifyAuthority');
		}
	}
		
function enableButton(buttonElement, controlElement) {
	if(controlElement != null) {
		if(controlElement.checked == true)
			{
			buttonElement.disabled = false;
			} else buttonElement.disabled = true;		
		}	
	}


function lockForm(lock)	{	
	if(lock)
		{
		var form = document.forms[0];
				
		for(i = 0; i<form.length; i++)
			{
				if(i > 1)
					form[i].disabled=true;				
			} 
		}	
}

function load(page)
{
	var link;
	var comparator
	var escape = false;

/*This basically identifies a unique query string parameter for the link that we need on the
  page.  As far as we can tell this stays constant.  For now it is all we have to go on to be
  able to uniquely identify the link we want*/
  
	if(page == "municipality")
		comparator = "d-49653-e=2";
	else if(page == "placeName")
		comparator = "d-49682-e=2";
	
	for(var i=0; i<document.links.length; i++)
	{
		link = document.links[i];
		array=link.href.split("&");
		
		for(var j=0; j<array.length; j++)
		{
			if(array[j] == comparator)
				escape=true;
		}
		
		if(escape == true)
			break;
	}

	link.href = "javascript:verifyTermsAccepted('"+link.href+"')"
/*	
	var link = document.links[document.links.length-1];
	array = link.href.split("&");
	link.href = "javascript:verifyTermsAccepted('"+link.href+"')"
*/
}

function hideOrDisplayDownloadButton()
{
	var checkbox = document.forms[0].acceptTerms;
	var element = document.getElementById("exportButton");
	
	if(checkbox.checked == false)
	{
		element.style.display = "none";
	}
	else
	{
		element.style.display = "block";	
	}
}

function showPopUp(url)
{
	var leftPixels = (screen.width-900)/2;
	var topPixels = (screen.height-700)/2;
	var options = "height=700,width=900,resizable=1,screenX=" + leftPixels + ",screenY=" + topPixels;
	
	window.open(url, "popup", options);
}

function setMapURL(url)
{
	var checkbox = null;
	var count = 1;
	var boxChecked = false;
		
	for(i=0; i < document.forms[0].elements.length; i++){
		if(document.forms[0].elements[i].name == "mapId"){
			checkbox = document.forms[0].elements[i];
			if (checkbox.checked == true) {
				url += checkbox.value + ",";
				boxChecked = true;
			}
		}
	}
	url = url.substr(0, (url.length-1));
	if(boxChecked == true)
		window.open(url, "", "");
	else
		alert("You must select at least one Place to view on a map");
}

function setRoadMapURL(url)
{
	var checkbox = null;
	var count = 1;
	var boxChecked = false;
	
	
	for(i=0; i < document.forms[0].elements.length; i++){
		if(document.forms[0].elements[i].name == "mapId"){
			checkbox = document.forms[0].elements[i];
			if (checkbox.checked == true) {
				url += checkbox.value + ",";
				boxChecked = true;
			}
		}
	}
	url = url.substr(0, (url.length-1));
	if(boxChecked == true)
		window.open(url, "", "");
	else
		alert("You must select at least one Road to view on a map");
}

function setDescription()
{
	document.forms[0].description.value = document.getElementById("description").value;
}

function clearAllFromList(name)
{
	var i = 0;
	var element = eval("document.forms[0]." + name);
	
	while(i < element.length)
	{
		element.options[i].selected = false;
		i++;
	}
}

function rejectProposalConfirm()
{
	if(confirm("Are you sure that you want reject this proposal?  It cannot be re-opened once rejected."))
			return true;
	else return false;
}
		
function enableOrDisableByRadioBtn(radioBtn, controlId)
{
	var controlElement = document.getElementById(controlId);
	if(radioBtn != null && controlElement != null) {
		if(radioBtn[0].checked)
		{
			controlElement.style.display = "none";
		}
		else
		{
			controlElement.style.display = "table-cell";
		}
	}	
}

function setParameters(radioName, listName)
{
	var i = 0;
	var radioElement = eval("document.forms[0]." + radioName);
	var listElement = eval("document.forms[0]." + listName);
	
	if(radioElement[1].checked)
	{
		listElement.style.display = "none";
		
		while(i < listElement.length)
		{
			listElement.options[i].selected = true;
			i++;
		}
		
		if(listName == "selectedMunicipalities")
			document.forms[0].clearMunicipalities.style.display = "none";
		if(listName == "selectedFeatures")
			document.forms[0].clearFeatures.style.display = "none";
	}
	else
	{
		clearAllFromList(listName);
		listElement.style.display = "block";
		if(listName == "selectedMunicipalities")
			document.forms[0].clearMunicipalities.style.display = "block";
		if(listName == "selectedFeatures")
			document.forms[0].clearFeatures.style.display = "block";
	}
	
	if(document.forms[0].municipalitySelection[1].checked && document.forms[0].featureSelection[1].checked)
		document.forms[0].search.value = "all";
	else
		document.forms[0].search.value = "";
}

// return the desired height of the map dialogue - 70% of the window height
function getMapFrameHeight()
{
	return 70 * (getWindowHeight() / 100);
}

// return the desired width of the map dialogue - 85% of the window width
function getMapFrameWidth()
{
	return 85 * (getWindowWidth() / 100);
}

/* Provide the inner width of the current browser window */
function getWindowWidth() 
{
    var windowWidth = 0;
    if (typeof(window.innerWidth) == 'number') 
    {
        windowWidth = window.innerWidth;
    }
    else 
    {
        if (document.documentElement && document.documentElement.clientWidth) 
        {
            windowWidth = document.documentElement.clientWidth;
        }
        else 
        {
            if (document.body && document.body.clientWidth) 
            {
                windowWidth = document.body.clientWidth;
            }
        }
    }
    return windowWidth;
}

/* Provide the inner height of the current browser window */
function getWindowHeight() 
{
    var windowHeight = 0;
    if (typeof(window.innerHeight) == 'number') 
    {
        windowHeight = window.innerHeight;
    }
    else 
    {
        if (document.documentElement && document.documentElement.clientHeight) 
        {
            windowHeight = document.documentElement.clientHeight;
        }
        else 
        {
            if (document.body && document.body.clientHeight) 
            {
                windowHeight = document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


//Code for dhtml menus
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
			
			if(document.getElementById("menu_iframe")) {
          		var iframe = document.getElementById("menu_iframe");

          		var submenu = this.getElementsByTagName("UL");
          	if(submenu.length) {
           		iframe.style.top = submenu[0].offsetTop + this.offsetTop;
            	iframe.style.left = submenu[0].offsetLeft + this.offsetLeft;
            	iframe.style.width = submenu[0].scrollWidth;
            	iframe.style.height = submenu[0].clientHeight;
            	iframe.style.display = "inline";
          	}
        }
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			if(document.getElementById("menu_iframe")) {
          		var iframe = document.getElementById("menu_iframe");
          		iframe.style.display = "none";
        	}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);


//Fix for menubar button hovers in IE.
menuButtonHover = function() {
	var menuBar = document.getElementById("menu-bar");
	if(menuBar != null) {
		var sfEls = menuBar.getElementsByTagName("TD");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" menuButtonHover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" menuButtonHover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", menuButtonHover);
