// --------- NOTES ---------------------------
function Notes(propkey, user, counter, owner_ids) {
	this._propkey = propkey;
	this._user = user;
	this._counter = counter;
	this._owner_ids = owner_ids;
	
	this.addNote = function(note, type, button) {
		note = note.replace(/^(\s*)([\W\w]*)(\b\s*$)/, '$2');
		if (note=="") {
			alert("Please write a note.");
			return;
		}

		button.disabled = '1';
		button.value = 'Adding Note...';

		note = note.replace(/\n/g, "<br>");

		// parse links
		note = note.replace(/\b(www(\.[\w-]+){2,}(\/[\w-]+)*(\.[\w-]+)*)/g, "<a href='http://$1' target='_blank'>$1</a>");
		note = note.replace(/http:\/\/\<a href='([^']+)'/g, "<a href='$1'");
		// parse emails
		note = note.replace(/\b([\w-\.]+@[\w-]+(\.[\w-]+){1,})/g, "<a href='mailto:$1'>$1</a>");

		psrk.ajaxLoader("/mason/Accounts/My/Notes/notes_add.html?note="+escape(note)+"&type="+type+"&propkey="+this._propkey+"&owner_ids="+this._owner_ids+"&counter="+this._counter, "notesDiv"+this._counter);
	}
	
	this.deleteNote = function(id) {
		if (!window.confirm("Are you sure you want to delete this note?"))
			return;
		psrk.ajaxLoader("/mason/Accounts/My/Notes/notes_delete.html?note_id="+id+"&propkey="+this._propkey+"&owner_ids="+this._owner_ids+"&counter="+this._counter, "notesDiv"+this._counter);
	}
	
}

// --------- LISTS ---------------------------
var counter_handlePropertyToList = 0;

function handlePropertyToList(propkey, id, status, loadingDiv) {
	var w = document.getElementById(loadingDiv);
	w.style.display = "block";
	if (w){
		w.innerHTML = "Updating the lists ";
		if (counter_handlePropertyToList == 0)
			setTimeout("working('"+loadingDiv+"')", 200);
	}
	updateStats("list_click", propkey);
		
	if (status)	var file = "add_prop.html";
	else		var file = "delete_prop.html";

	var service = new AjaxService();
	service.setDelegate(workingDone);
	service._forwardParams = "loadingDiv="+loadingDiv;
	service.setURL("/mason/Accounts/My/Lists/"+file+"?propkey="+propkey+"&id="+id);
	service.callService();

	counter_handlePropertyToList++;
}

function working(loadingDiv) {
	if (counter_handlePropertyToList>0){
		setTimeout("working('"+loadingDiv+"')", 200);
	}else{
		return;
	}

	var w = document.getElementById(loadingDiv);
	if (w.innerHTML.length<28)
		w.innerHTML += ". ";
	else
		w.innerHTML = "Updating the lists ";
}

function workingDone(r, forwardParams) {
	var w = document.getElementById(forwardParams['loadingDiv']);
	if (!r.match(/OK/)){
		w.innerHTML = "Error updating the lists";
		counter_handlePropertyToList = 0;
		return;
	}
	counter_handlePropertyToList--;
	if (counter_handlePropertyToList == 0) {
		w.innerHTML = "The lists were updated";
	}
}

// --------- REPORT SECTIONS ---------------------------
function processReportSections(r, forwardParams){
	var JSON = eval("(" + r + ")");
	var result = "<div class='report_sections'><ul>";
	for (var i=0;i<JSON.content.length;i++) {
		var name = JSON.content[i].name;
		var section = JSON.content[i].section;
		if (name && name!='' && section && section!='')
			result += "<li><a href='javascript:void(0)' onClick=\"popupshow(this, '/mason/Reports2/ajax_sections.html?propkey=" + JSON.propkey + "&ajax_section=" + section + "', 'type=ajaxLoader', 'delegate=processReportSection', 'call_event=click', 'title="+escape(name)+"', 'align=center', 'valign=middle', 'stayopen', 'width=660', 'loc="+document.searchform.shortLocale.value+"')\">"+ name +"</a></li>";
	}
	result += "</ul></div>";
	return(result);
}

function processReportSection(r){
	var r = r;
	try {
		r = r.replace(/\<h4\>/g, "<h4 style='border-bottom: 1px solid #999; margin: 1em 0 0 0'>");
		r = r.replace(/\<div class="toggle"\>/g, "<div style='display:none'>");
		r = r.replace(/class="t"/g, "style='display:none'");
		r = r.replace(/class="section"/g, "class='section' style='background:none; border:none; margin-top: 8px'");
		r = r.replace(/\<a[^\>]+\>\<img src="\/img\/question\.gif"\>\<\/a\>/g, "");

		//handle maps	
		var draw = r.match(/(drawMap\([^\)]+\))/g, "''");
		if (draw) {
			for (var i=0;i<draw.length;i++) {
				eval("var map = "+draw[i]);
				r = r.replace(/\<script\>[^d]+document\.write\(drawMap[^\<]+\<\/script\>/, map);
			}
		}

		//handle maxwidgets requests
		var pattern = /\<script\>(HTTP.request[^\<]+)\<\/script\>/;
		var maxReq = r.match(pattern);
		if (maxReq) {
			eval(maxReq[1]);
			r = r.replace(pattern, "");
		}

		//handle other js scripts
		var pattern = /\<script\>([^\<]+)\<\/script\>/;
		var otherReq = r.match(pattern);
		if (otherReq){
			eval(otherReq[1]);
			r = r.replace(pattern, "");
		}

		//extract propertyAddress if available
		var propertyAddress = "";
		var pattern = /\<input type=hidden name=propertyAddress value="([^"]+)"\>/;
		var propertyAddress_array = r.match(pattern);
		if (propertyAddress_array){
			propertyAddress = propertyAddress_array[1];
		}

		if (r == "") throw("empty");
	} catch (e) {
		r = (e == "empty") ? "No content found." : "Error getting report section.<br>["+e+"]";
		r = "<center>" + r + "</center>";
	}

	return("<div class='reportmain' style='margin:0'>" + r + "</div>");
}

function processLists(r){
	include_css('/includes/myLists.css', 'myLists');
	var r = r;
	r = r.replace(/class="section navlinks"/, "");
	r = r.replace(/class="t"/, "class='hide'");
	return(r);
}

function processListsProperties(r){
	include_css('/includes/myLists.css', 'myLists');
	return(r);
}

