var save_property_button;
var save_search_button;
var delete_property_id;
var delete_search_id;
var context = "user";
var user_id = "";

function get_update_url() {
	return 'ah_organizer_update.php';
}

function set_context(ctxt) {
	context = ctxt;
}

function set_user_id(usrid) {
	user_id = usrid;
}

function save_search(button) {
	save_search_button = button;
	var arguments = "{}";
	var postbody = 'action=add_search&arguments=' + escape(arguments);
	
	var options = {
		method:'post',
		postBody: postbody,
		onSuccess: save_search_complete,
		on401: not_authenticated,
		onFailure: save_failed
	};
	
	new Ajax.Request(get_update_url(),options);
}

function save_search_complete(t) {
	r = eval('(' + t.responseText + ')');
	alert(r.message);
	if (document.getElementById('search_counter')) {
		document.getElementById('search_counter').innerHTML = r.count;
	}
	save_search_button.style.display = "none";
}

function save_property(mls_num, p_property_class, button) {
	save_property_button = button;
	var arguments = "{'mls_num':" + mls_num + ",'p_property_class': " + p_property_class + "}";
	var postbody = 'action=add_property&arguments=' + escape(arguments);
	
	var options = {
		method:'post',
		postBody: postbody,
		onSuccess: save_property_complete,
		on401: not_authenticated,
		onFailure: save_failed
	};
	new Ajax.Request(get_update_url(),options);	
}

function save_property_complete(t) {
	r = eval('(' + t.responseText + ')');
	alert(r.message);
	if (document.getElementById('property_counter')) {
		document.getElementById('property_counter').innerHTML = r.count;
	}
	save_property_button.style.display = "none";
}

function save_failed(t) {
	alert("save failed");
}

function not_authenticated() {
	if (context == 'user') {
		window.location = "/organizer-signup.php";
	}
	else {
		window.location = "../login.php";
	}
}

function delete_search(ooa) {
	delete_search_id = ooa;
	
	if (context == 'user') {
		var arguments = "{'ooa':" + ooa + "}";
	}
	else {
		var arguments = "{'ooa':" + ooa + ",'user_id':" + user_id + "}";
	}
		
	var postbody = 'action=delete_search&arguments=' + escape(arguments);
	
	var options = {
		method:'post',
		postBody: postbody,
		onSuccess: delete_search_complete,
		on401: not_authenticated,
		onFailure: save_failed
	};
	new Ajax.Request(get_update_url(),options);
}

function delete_search_complete(t) {
	alert(t.responseText);
	document.getElementById("saved_search_id_" + delete_search_id).style.display = "none";
}

function delete_property(mls_num, property_class) {
	delete_property_id = mls_num;
	delete_property_class = property_class;
	if (context == 'user') {
		var arguments = "{'mls_num':" + mls_num + ",'property_class':" + property_class + "}";
	}
	else {
		var arguments = "{'mls_num':" + mls_num + ",'property_class':" + property_class + ",'user_id':" + user_id + "}";
	}
	var postbody = 'action=delete_property&arguments=' + escape(arguments);
	
	var options = {
		method:'post',
		postBody: postbody,
		onSuccess: delete_property_complete,
		on401: not_authenticated,
		onFailure: save_failed
	};
	new Ajax.Request(get_update_url(),options);	
}

function delete_property_complete(t) {
	alert(t.responseText);
	document.getElementById("property_id_" + delete_property_id + "__property_class_" + delete_property_class).style.display = "none";
}

function set_email_updates(ooa, checkbox) {
	if (checkbox.checked) {
		value = 1;
	}
	else {
		value = 0;
	}
	
	if (context == "user") {
		var arguments = "{'ooa':" + ooa + ",'value':" + value + "}";
	}
	else {
		var arguments = "{'ooa':" + ooa + ",'value':" + value + ",'user_id':" + user_id + "}";
	}
	
	var postbody = 'action=set_email_updates&arguments=' + escape(arguments);
	
	var options = {
		method:'post',
		postBody: postbody,
		onSuccess: set_email_updates_complete,
		on401: not_authenticated,
		onFailure: save_failed
	};
	new Ajax.Request(get_update_url(),options);	
}

function set_public_search(ooa, checkbox) {
	if (checkbox.checked) {
		value = 1;
	}
	else {
		value = 0;
	}
	
	if (context == "user") {
		var arguments = "{'ooa':" + ooa + ",'value':" + value + "}";
	}
	else {
		var arguments = "{'ooa':" + ooa + ",'value':" + value + ",'user_id':" + user_id + "}";
	}
	
	var postbody = 'action=set_public_search&arguments=' + escape(arguments);
	
	var options = {
		method:'post',
		postBody: postbody,
		onSuccess: set_public_search_complete,
		on401: not_authenticated,
		onFailure: save_failed
	};
	new Ajax.Request(get_update_url(),options);	
}

function logout() {
	var arguments = "{}";
	var postbody = 'action=logout&arguments=' + escape(arguments);
	
	var options = {
		method:'post',
		postBody: postbody,
		onSuccess: logout_complete,
		on401: not_authenticated,
		onFailure: logout_failed
	};
	new Ajax.Request(get_update_url(),options);
}

function logout_complete() {
	var sURL = unescape(window.location.pathname);
	window.location.replace( sURL );
}

function logout_failed() {
	alert("Logout Failed");
}

function set_email_updates_complete(t) {
	alert(t.responseText);	
}

function set_public_search_complete(t) {
	var res = JSON.parse(t.responseText);
	span_id = "link_public_search_" + res.ooa;
	document.getElementById(span_id).innerHTML = res.link;
}

function edit_search_name(form, value) {
	var frm_id = form.id;
	var search_id = frm_id.substr(12,frm_id.length - 26);
	var arguments = "{name:'"+ value +"', id:'"+ search_id +"'}";
	return "action=edit_search_name&arguments=" + escape(arguments);
}

function edit_fname(form, value) {
	var arguments = "{fname:'" + value + "'}";
	return "action=edit_fname&arguments=" + escape(arguments);
}

function edit_lname(form, value) {
	var arguments = "{lname:'" + value + "'}";
	return "action=edit_lname&arguments=" + escape(arguments);
}

function edit_email(form, value) {
	var arguments = "{email:'" + value + "'}";
	return "action=edit_email&arguments=" + escape(arguments);
}

function edit_phone(form, value) {
	var arguments = "{phone:'" + value + "'}";
	return "action=edit_phone&arguments=" + escape(arguments);
}

function account_edited(t) {
	var res = JSON.parse(t.responseText);
	if (res.message != "") {
		alert(res.message);
	}
	t.responseText = res.value;
}