var globalShowingProgressBox;
var globalProgressTimerID;
var globalLastSessionLoc;

function formatCurrency(val){
	var strValue = val.toString().replace(/\$|\,/g,'');
	var dblValue = parseFloat(strValue);

	var blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	var intCents = dblValue%100;
	var strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<1) {
		strCents = ""
	} else if(intCents<10){
		strCents = ".0" + strCents;
	} else {
		strCents = "." + strCents;
	}	
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + strCents);
}

function deleteRecord(which, id, nomsg){
	var ask = !nomsg;
	var cont = true;
	if(ask){
		cont = confirm('Are you sure? This cannot be undone.');
	}
	if(cont){
		var deleteElement = getTopSession().document.createElement('INPUT');
		deleteElement.type = 'hidden';
		deleteElement.name = '__delete_confirmed';
		deleteElement.value = 'YES';
		getTopSession().document.form.appendChild(deleteElement);
		return true;
	}
	return false;
}

function unusedLogin(login){
	if(login != ''){
		var lcheck = new ActiveXObject("Msxml2.DOMDocument.3.0");
		lcheck.async = false;
		lcheck.load("/forms_util/checklogin.aspx?login=" + login);
		var oklogin = false;
		var enode = lcheck.selectNodes('/LoginCheck/Exists/@answer');
		if(enode != null){
			if(enode.item(0) != null){
				if(enode.item(0).text == 'no'){
					oklogin = true;
				}
			}
		}
		if(!oklogin){
			alert('That login is already assigned. Please choose another.');
		}
		return oklogin;
	} else {
		return true;
	}
}

function popupDetails(contList, type, id, fldLabel){
	if(id && id != 'undefined'){
		if(contList == 'l'){
			if(type == 'R'){
				popupSession('/forms/residential.aspx?id=' + id);
			} else if(type == 'C'){
				popupSession('/forms/commercial.aspx?id=' + id);
			} else if(type == 'V'){
				popupSession('/forms/vacant_land.aspx?id=' + id);
			} else if(type == 'N'){
				popupSession('/forms/rental.aspx?id=' + id);
			} else {
				popupSession('/forms/outside_listing.aspx?id=' + id);
			}
		} else if(contList == 'c') {
			if(type == 'AG'){
				popupSession('/forms/in_house.aspx?id=' + id);
			} else if(type == 'OA'){
				popupSession('/forms/outside.aspx?id=' + id);
			} else if(type == 'EM'){
				popupSession('/forms/employee.aspx?id=' + id);
			} else if(type == 'PR'){
				popupSession('/forms/prospect.aspx?id=' + id);
			} else {
				popupSession('/forms/contact.aspx?id=' + id);
			}
		}
	} else {
		alert('Click the ' + fldLabel + ' field to make a selection first.');
	}
}

function displayProgressMsg(){
	if(document.all['maskDiv'] == null && document.all['progressBox'] == null){
		globalShowingProgressBox = true;
		killMSelects();

		var maskDiv = document.createElement('DIV');
		maskDiv.name = 'maskDiv';
		maskDiv.id = 'maskDiv';
		maskDiv.style.position = 'absolute';
		maskDiv.style.top = '0px';
		maskDiv.style.left = '0px';
		maskDiv.style.width = '100%';
		maskDiv.style.height = '100%';
		maskDiv.style.backgroundColor = 'transparent';
		maskDiv.style.zIndex = 10;
		document.body.insertAdjacentElement('afterBegin', maskDiv);
		
		var shadowDiv = document.createElement('DIV');
		shadowDiv.name = 'shadowDiv';
		shadowDiv.id = 'shadowDiv';
		shadowDiv.style.position = 'absolute';
		shadowDiv.style.width = '200px';
		shadowDiv.style.height = '76px';
		shadowDiv.style.left = (document.body.scrollWidth / 2 - 96) + 'px';
		shadowDiv.style.top = (document.body.scrollHeight / 2 - 34) + 'px';
		shadowDiv.style.backgroundColor = '#000000';
		shadowDiv.style.filter = 'alpha(opacity=25)';
		shadowDiv.style.zIndex = 11;
		document.body.insertAdjacentElement('afterBegin', shadowDiv);
		
		var msgBox = document.createElement('DIV');
		msgBox.name = 'progressBox';
		msgBox.id = 'progressBox';
		msgBox.className = 'progressBox';
		msgBox.style.position = 'absolute';
		msgBox.style.width = '200px';
		msgBox.style.height = '76px';
		msgBox.style.left = (document.body.scrollWidth / 2 - 100) + 'px';
		msgBox.style.top = (document.body.scrollHeight / 2 - 38) + 'px';
		msgBox.style.zIndex = 12;
		msgBox.innerHTML = "Please wait...";
		document.body.insertAdjacentElement('afterBegin', msgBox);
		
		// start the timer to remove the progress box on session location change
		globalLastSessionLoc = getTopSession().location;
		globalProgressTimerID = setInterval('checkSessionLocation();', 500);
	}
}

function hideProgressMsg(){
	if(document.all['progressBox'] != null){
		document.body.removeChild(document.all['progressBox']);
	}
	if(document.all['maskDiv'] != null){
		document.body.removeChild(document.all['maskDiv']);
	}
	if(document.all['shadowDiv'] != null){
		document.body.removeChild(document.all['shadowDiv']);
	}
	if(globalProgressTimerID){
		globalProgressTimerID = clearInterval(globalProgressTimerID);
	}
	unKillMSelects();
	globalShowingProgressBox = false;
}

function checkSessionLocation(){
	if(globalLastSessionLoc != getTopSession().location){
		hideProgressMsg();
	}
}

function progressSubmit(form){
	displayProgressMsg();
	setTimeout(function(){ form.submit(); }, 50);
}