/********* PROPERTY MANAGEMENT JS ************/
var isNN = ( navigator.appName.indexOf( "Netscape" ) != -1 );

function autoTab( input,len, e ) {
	var keyCode	= ( isNN ) ? e.which : e.keyCode;
	var filter	= ( isNN ) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if( input.value.length >= len && !containsElement( filter, keyCode )) {
	input.value = input.value.slice( 0, len );
	input.form[( getIndex( input ) + 1 ) % input.form.length].focus();
	}
	return true;
}

function containsElement( arr, ele ) {
	var found = false, index = 0;
	while( !found && index < arr.length )
	if( arr[index] == ele ) {
		found = true;
	} else {
		index++;
	}
	return found;
}

function getIndex( input ) {
	var index = -1, i = 0, found = false;
	while ( i < input.form.length && index == -1 )
	if ( input.form[i] == input ) {
		index = i;
	} else {
		i++;
	}
	return index;
}
/*********************************************/



function krgDialogTop(title) {

	var html = '<div id="dialog">' + "\n";
	html += '	<h1>' + title + '</h1>' + "\n";
	html += '	<div id="dialogInner">' + "\n";

	return html;
}

function krgDialogBottom() {

	var html = '	</div>' + "\n";
	html += '</div>' + "\n";

	return html;
}


function frmAddArea(id, name, abbrev) {

	if(!id) id = false;
	if(!name) name = '';
	if(!abbrev) abbrev = '';
	var title = (id) ? 'Edit Area' : 'Add Area';
	var action = (id) ? '/area/edit.dT' : '/area/add.dT';

	var html = '';
	html += krgDialogTop(title);
	html += '	<form action="' + action + '" method="post" onsubmit="return dynamItPublish(this);">' + "\n";
	if(id) html += '	<input type="hidden" name="id" value="' + id + '" />' + "\n";
	html += '	<div id="dialogPlate">' + "\n";
	html += '	<table border="0">';
	html += '	<tr>' + "\n";
	html += '		<td>Name:</td>' + "\n"
	html += '		<td><input type="text" name="name" value="' + name + '" /></td>' + "\n"
	html += '	</tr>' + "\n"
	html += '	<tr>' + "\n"
	html += '		<td>Abbrev.</td>' + "\n"
	html += '		<td><input type="text" name="abbrev" value="' + abbrev + '" /></td>' + "\n"
	html += '	</tr>' + "\n"
	html += '	</table>' + "\n"
	html += '	</div>' + "\n"
	html += '	<div class="center">' + "\n"
	html += '		<input type="submit" value="Save." /> &nbsp; &nbsp;' + "\n"
	html += '		<input type="button" value="Cancel." onclick="dynamItCloseDialog();" /></div>' + "\n"
	html += '	</form>' + "\n"
	html += krgDialogBottom();

	dynamItOpenDialog(html, 400, 'auto');

}

function deleteArea(id) {
	dynamItSendOnConfirm('/area/delete.dT?id=' + id, 'Are you sure you wish to delete this area?');
}



function frmAddType(id, name) {

	if(!id) id = false;
	if(!name) name = '';
	var title = (id) ? 'Edit Type' : 'Add Type';
	var action = (id) ? '/type/edit.dT' : '/type/add.dT';

	var html = '';
	html += krgDialogTop(title);
	html += '	<form action="' + action + '" method="post" onsubmit="return dynamItPublish(this);">' + "\n";
	if(id) html += '	<input type="hidden" name="id" value="' + id + '" />' + "\n";
	html += '	<div id="dialogPlate">' + "\n";
	html += '	<table border="0">';
	html += '	<tr>' + "\n";
	html += '		<td>Name:</td>' + "\n"
	html += '		<td><input type="text" name="name" value="' + name + '" /></td>' + "\n"
	html += '	</tr>' + "\n"
	html += '	</table>' + "\n"
	html += '	</div>' + "\n"
	html += '	<div class="center">' + "\n"
	html += '		<input type="submit" value="Save." /> &nbsp; &nbsp;' + "\n"
	html += '		<input type="button" value="Cancel." onclick="dynamItCloseDialog();" /></div>' + "\n"
	html += '	</form>' + "\n"
	html += krgDialogBottom();

	dynamItOpenDialog(html, 400, 'auto');

}


function deleteType(id) {
	dynamItSendOnConfirm('/type/delete.dT?id=' + id, 'Are you sure you wish to delete this type?');
}

function frmAddStatus(id, name) {

	if(!id) id = false;
	if(!name) name = '';
	var title = (id) ? 'Edit Status' : 'Add Status';
	var action = (id) ? '/status/edit.dT' : '/status/add.dT';

	var html = '';
	html += krgDialogTop(title);
	html += '	<form action="' + action + '" method="post" onsubmit="return dynamItPublish(this);">' + "\n";
	if(id) html += '	<input type="hidden" name="id" value="' + id + '" />' + "\n";
	html += '	<div id="dialogPlate">' + "\n";
	html += '	<table border="0">';
	html += '	<tr>' + "\n";
	html += '		<td>Name:</td>' + "\n"
	html += '		<td><input type="text" name="name" value="' + name + '" /></td>' + "\n"
	html += '	</tr>' + "\n"
	html += '	</table>' + "\n"
	html += '	</div>' + "\n"
	html += '	<div class="center">' + "\n"
	html += '		<input type="submit" value="Save." /> &nbsp; &nbsp;' + "\n"
	html += '		<input type="button" value="Cancel." onclick="dynamItCloseDialog();" /></div>' + "\n"
	html += '	</form>' + "\n"
	html += krgDialogBottom();

	dynamItOpenDialog(html, 400, 'auto');

}
function deleteStatus(id) {
	dynamItSendOnConfirm('/status/delete.dT?id=' + id, 'Are you sure you wish to delete this status?');
}


function propertyEditFiles(pid) {
	dynamItLoad('/property/editfiles.dT?id=' + pid, null, 400, 'auto', null);
}

function propertyEditPeople(pid) {
	dynamItLoad('/property/editpeople.dT?id=' + pid, null, 400, 'auto', function() {
		$('#dynamItPopUpX').hide();
	});
}

function propertyEditBasic(pid) {
	dynamItLoad('/property/editbasic.dT?id=' + pid, null, 400, 'auto', null);
}

function propertyEditDetails(pid) {
	dynamItLoad('/property/editdetails.dT?id=' + pid, null, 400, 'auto', null);
}

function propertyAddShowing(pid) {
	dynamItLoad('/showing/add.dT?property=' + pid, null, 400, 'auto', null);
}

function propertyEditShowing(sid) {
	dynamItLoad('/showing/edit.dT?id=' + sid, null, 400, 'auto', null);
}

function propertyDeleteShowing(sid) {
	dynamItSendOnConfirm('/showing/delete.dT?id=' + sid, 'Are you sure you wish to permanently remove this showing?');
}

function propertyAddMailing(pid) {
	dynamItLoad('/mailing/add.dT?property=' + pid, null, 400, 'auto', null);
}

function propertyEditMailing(mid) {
	dynamItLoad('/mailing/edit.dT?id=' + mid, null, 400, 'auto', null);
}

function propertyDeleteMailing(mid) {
	dynamItSendOnConfirm('/mailing/delete.dT?id=' + mid, 'Are you sure you wish to permanently remove this mailing?');
}


function propertyAddNote(pid) {
	dynamItLoad('/notes/add.dT?property=' + pid, null, 400, 'auto', null);
}

function propertyEditNote(nid) {
	dynamItLoad('/notes/edit.dT?id=' + nid, null, 400, 'auto', null);
}

function propertyDeleteNote(nid) {
	dynamItSendOnConfirm('/notes/delete.dT?id=' + nid, 'Are you sure you wish to permanently remove this note?');
}

function propertyAddPhoto(pid) {
	dynamItLoad('/gallery/add.dT?property=' + pid + '&count=' + photoCount, null, 400, 'auto', null);
}
function propertyEditPhoto(pid) {
	dynamItLoad('/gallery/edit.dT?id=' + pid, null, 400, 'auto', null);
}
function propertyDeletePhoto(pid) {
	dynamItSendOnConfirm('/gallery/delete.dT?id=' + pid, 'Are you sure you wish to permanently remove this photo?');
}

	function propertyDrawPhoto(id, path) {
		var cl = (photoCount % 2 == 0) ? '-even' : '';
		html = '<div class="cb-gallery' + cl + '" id="photo_' + id + '">';
		html += '	<div class="editBtn"><a href="javascript:propertyDeletePhoto(' + id + ');" class="iconS ico-greenx"></a></div>';
		html += '	<div class="editBtn" style="right: 24px;"><a href="javascript:propertyEditPhoto(' + id + ');" class="iconS ico-edit"></a></div>';
		html += '	<img src="' + path + '" alt="" /></div>';
		$(html).appendTo("#propertyGallery");
		photoCount++;
	}

	function propertyRefreshPhoto(path) {
		$("#propertyImage img").attr('src', path);
	}
	function propertyRefreshFiles(html) {
		$("#propertyLinks").html(html);
	}


function propertyAddAgent(pid, aid) {
	var exists = $('#agent_' + aid).length;
	if(exists == 0) {
		dynamItSend('/property/addagent.dT?property=' + pid + '&agent=' + aid);
	}
}
function propertyRemoveAgent(pid, aid) {
	dynamItSend('/property/remagent.dT?property=' + pid + '&agent=' + aid)
}
function propertyUpdateOwner(pid, oid) {
	$("#updateMessage-owner").hide();
	dynamItSend('/property/updateowner.dT?property=' + pid + '&owner=' + oid);
}
function propertyAddContact(pid, cid) {
	var exists = $('#contact_' + cid).length;
	if(exists == 0) {
		dynamItSend('/property/addcontact.dT?property=' + pid + '&contact=' + cid);
	}
}
function propertyRemoveContact(pid, cid) {
	dynamItSend('/property/remcontact.dT?property=' + pid + '&contact=' + cid)
}


function addUser() {
	dynamItLoad('/users/addfromdialog.dT', null, 400, 'auto', null);
}

function clearFilters() {
	var f = dynamItElem('filterForm');
	f.agent.selectedIndex = 0;
	f.area.selectedIndex = 0;
	f.owner.selectedIndex = 0;
	f.status.selectedIndex = 0;
	f.type.selectedIndex = 0;
	dynamItPublish(f);
}

function checkVisibile(on) {
	$("tr.propertyRow:visible input.propertyCheck").each(function() {
		var e = $(this).get(0);
		e.checked = on;
	});
}

function selectTemplate(id) {
	$("#template_" + selectedTemplate).css('borderColor', '#FFE57F');
	$("#template_"+ id).css('borderColor', '#cc0000');
	selectedTemplate = id;
}

function sendStep2(){
	location.href = '/reports/generate.dT?template=' + selectedTemplate;
}


function viewGalleryItem(pid, i) {
	dynamItLoad('/property/photo.dT?property=' + pid + '&i=' + i, null, 'auto', 'auto');
}

function deleteProperty(id) {
	dynamItLinkOnConfirm('/property/delete.dT?pid=' + id, 'Are you sure you wish to permanently delete this property?<br /><small>You will NOT be able to undo this action</small>.');
}

function deleteUser(id) {
	dynamItSendOnConfirm('/users/delete.dT?user=' + id, 'Are you sure you wish to permanently delete this user?<br /><small>You will NOT be able to undo this action</small>.');
}


function calcRates(type, price) {
	var sqft = parseFloat( $('#available_sqft').val() );
	var acres = parseFloat( $('#available_acres').val() );

	if(sqft > 0) $('#' + type + '_price_sqft').val( (price/sqft).toFixed(2) );
	if(acres > 0) $('#' + type + '_price_acre').val( (price/acres).toFixed(2) );

}