var knum = 0;
var locMarker = null;
var noFlash = false;
var myLytebox = null;
var ptdsTbl = null;

function initializeMaps(address, propertyID, latitude, longitude)
{	
	showAddress(address);
	
	if (GBrowserIsCompatible())
	{
		var overMap = new GMap2($("overviewMap"));
		overMap.setUIToDefault();
		overMap.setMapType(G_SATELLITE_MAP);
		
		var locMap = new GMap2($("locationMap"));
		locMap.setUIToDefault();
		locMap.addControl(new GOverviewMapControl());		
		var geocoder = new GClientGeocoder();
		
		if(latitude && longitude)
		{
			var point = new GLatLng(latitude, longitude);
			
			// change the overview map!
			var overMarker = new GMarker(point);
			overMap.setCenter(point, 16);
			overMap.addOverlay(overMarker);
			
			// change the location map!
			locMarker = new GMarker(point);
			locMap.setCenter(point, 13);
			locMap.addOverlay(locMarker);
			locMarker.openInfoWindowHtml(address);
			locMarker.bindInfoWindowHtml(address);					
		}
		else
		{
			geocoder.getLatLng 
			(
				address,
				function(point)
				{
					if(!point)
					{
						alert('Sorry, address "' + address + '" could not be found');
					}
					else
					{
						// change the overview map!
						var overMarker = new GMarker(point);
						overMap.setCenter(point, 16);
						overMap.addOverlay(overMarker);
						
						// change the location map!
						locMarker = new GMarker(point);
						locMap.setCenter(point, 13);
						locMap.addOverlay(locMarker);
						locMarker.openInfoWindowHtml(address);
						locMarker.bindInfoWindowHtml(address);					
					}
				}
			);
		}
	}	
}

function showAddress(address)
{
	$('jacketTitle').innerHTML = address;
	document.title = "Briki.com > " + address;
}

function showImage(propertyID)
{	
	new Ajax.Updater({success: 'smallSitePhoto'},'/briki/functions/brikiFunctions.php?action=showImage&propertyID='+propertyID,
	{
		method: 'get',
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		onSuccess : function(resp)
		{
			Element.hide('loader');
		},
		onComplete : function(resp)
		{
			Element.hide('loader');
		}
	});
}

function savePhotoCaption(fileID, propertyID)
{
	new Ajax.Request('/briki/functions/brikiFunctions.php?action=savePhotoCaption&propertyID='+propertyID+"&fileID="+fileID,
	{
		method: 'post',
		parameters : Form.serialize('photoForm'+fileID),
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		onSuccess : function(resp)
		{
			setTimeout("Element.show('captionSaveOk')", 500);
			setTimeout("Element.hide('captionSaveOk')", 3000);
			Element.hide('loader');
		}
	});
}

function uploaderSettings (propertyID,functionPage,pageAction,theDivToUpdate, fileTypes, fileDesc)
{
	var settings = {
		upload_url: functionPage+"&propertyID="+propertyID+"&action="+pageAction,
		// File Upload Settings
		file_size_limit : "10000",	// 10MB
		file_types : fileTypes,
		file_types_description : fileDesc,
		file_upload_limit : "101",
		file_queue_limit : 0,
	
		// Event Handler Settings (all my handlers are in the Handler.js file)
		file_dialog_start_handler : fileDialogStart,
		file_queued_handler : fileQueued,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_start_handler : uploadStart,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,
	
		// Button settings
		button_image_url : "/briki/scripts/flashUploadImage.gif",	// Relative to the SWF file
		button_placeholder_id : "flashButtonPlaceholder"+theDivToUpdate+"_"+propertyID,
		button_width: 98,
		button_height: 25,
	
		// Flash Settings
		flash_url : "/briki/scripts/swfupload.swf",	// Relative to this file (or you can use absolute paths)
		
		swfupload_element_id : "flashUI"+theDivToUpdate+"_"+propertyID,		// Setting from graceful degradation plugin
		degraded_element_id : "degradedUI"+theDivToUpdate+"_"+propertyID,	// Setting from graceful degradation plugin
	
		custom_settings : {
			progressTarget : "fsUploadProgress"+theDivToUpdate+"_"+propertyID,
			cancelButtonId : "btnCancel"+theDivToUpdate+"_"+propertyID,
			page :	functionPage,
			divToUpdate :	theDivToUpdate,
			propertyID :	propertyID
		},
		
		// Debug Settings
		debug: false
	};
	return settings;						
}

function checkFileExists (functionPage)
{
	var fe = 0;
	new Ajax.Updater({},functionPage+"&action=checkFileExists",
	{
		method : 'post',
		asynchronous : false,
		evalScripts : true,
		on400 : function(resp)
		{
			alert(resp.statusText);
		},
		onFailure : function(resp)
		{
			alert('Error ' + resp.status + ' -- ' + resp.statusText);
		},
		on200 : function (resp)
		{
			fe = resp.statusText;
		}
	});	
	return fe;
}

function refreshFilesPhotos(functionPage,theDivToUpdate,propertyID)
{
	new Ajax.Updater({success: theDivToUpdate},functionPage+"&propertyID="+propertyID+"&action=refreshFilesPhotos&type="+theDivToUpdate,
	{
		method : 'post',
		asynchronous : true,
		evalScripts : true,
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		on400 : function(resp)
		{
			alert(resp.statusText);
		},
		onFailure : function(resp)
		{
			alert('Error ' + resp.status + ' -- ' + resp.statusText);
		},
		onComplete : function(resp)
		{
			myLytebox.updateLyteboxItems();	
			Element.hide('loader');
		}
	});	
}

function hideMessage(divToHide)
{
	$(divToHide).style.display = "none";
}

function switchTab(tabID)
{
	new Ajax.Updater({success: 'content'},"/briki/functions/brikiFunctions.php?action=switchTab&tabID="+tabID,
	{
		method : 'post',
		asynchronous : true,
		evalScripts : true,
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		on400 : function(resp)
		{
			alert(resp.statusText);
		},
		onFailure : function(resp)
		{
			alert('Error ' + resp.status + ' -- ' + resp.statusText);
		},
		onComplete : function(resp)
		{	
			deactivateOthers(tabID);
			myLytebox.updateLyteboxItems();
			Element.hide('loader');
		}
	});		
}

function slide(tabID)
{
	deactivateOthers(tabID);
}

function deactivateOthers(tabID)
{
	var items = $$('#navcontainer li');
	
	for (var i = 0; i < items.length; i++)
	{
		items[i].className = '';
	}
	
	if(tabID == 'Documentation')
	{
		$('Documents').className = "act";
	}
	else if(tabID == 'Photographs')
	{
		$('Photos').className = "act";
	}
	else
	{
		$(tabID).className = "act";
	}
}

function autosave(propertyID)
{
	new Ajax.Request('/briki/functions/brikiFunctions.php?action=autosave&propertyID='+propertyID,
	{
		method: 'post',
		parameters : Form.serialize('propInfo'),
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		onSuccess : function(resp)
		{
			setTimeout("Element.show('autoSaveOk')", 500);
			setTimeout("Element.hide('autoSaveOk')", 3000);
			Element.hide('loader');
		}
	});
}

function startAutosave(ev, propertyID)
{
	if (!ev) var ev = window.event;
		knum++;
		window.setTimeout('if (' + knum + '==knum) autosave('+propertyID+');', 1000);
}

function setSearchValue()
{
	if($('searchAddr').value == 'Property or Location')
	{
		$('searchAddr').value = '';
	}
}

function showHideNewDiscussion()
{
	if($('newDiscussion').style.display == 'none')
	{
		Element.show('newDiscussion');
		$('newDiscussionLink').innerHTML = '<a href="javascript:void(0);" onclick="showHideNewDiscussion();" class="button cancelDiscussionLink">Cancel New Discussion</a>';
	}
	else
	{
		Element.hide('newDiscussion');
		$('newDiscussionLink').innerHTML = '<a href="javascript:void(0);" onclick="showHideNewDiscussion();" class="button newDiscussionLink">Start a New Discussion</a>';
	}	
}

function showHideElement(elementName)
{
	if($(elementName).style.display == 'none')
	{
		Element.show(elementName);
	}
	else
	{
		Element.hide(elementName);
	}	
}

function addDiscussion(propertyID)
{
	new Ajax.Updater ({success: "discussionBlock"},"/briki/functions/brikiFunctions.php?propertyID="+propertyID+"&action=addDiscussion", 
	{
		method : 'post',
		asynchronous : true,
		evalScripts: true,
		parameters : Form.serialize($('discussionForm')),
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		onFailure : function(resp)
		{
			alert('Error ' + resp.status + ' -- ' + resp.statusText);
		},
		onComplete : function(resp)
		{
			Element.hide('loader');
		}
	});				
}

function replyToDiscussion(propertyID, discussionID)
{
	new Ajax.Updater ({success: "discussionBlock"},"/briki/functions/brikiFunctions.php?propertyID="+propertyID+"&discussionID="+discussionID+"&action=replyToDiscussion", 
	{
		method : 'post',
		asynchronous : true,
		evalScripts: true,
		parameters : Form.serialize($('replyForm_'+discussionID)),
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		onFailure : function(resp)
		{
			alert('Error ' + resp.status + ' -- ' + resp.statusText);
		},
		onComplete : function(resp)
		{
			Element.hide('loader');
			// show the reply that was just added.
			$('children_'+discussionID).style.display = "";
			// set the reply text to null in the reply form.
			$('replyDiscussionText_'+discussionID).value == '';
		}
	});
}

function deleteFile(propertyID, fileID, filename, type, theDivToUpdate)
{
	if(confirm('You are about to delete ' + filename + '. Are you sure?'))
	{
		new Ajax.Updater ({success: theDivToUpdate},"/briki/functions/brikiFunctions.php?propertyID="+propertyID+"&fileID="+fileID+"&filename="+filename+"&type="+type+"&action=deleteFile",
		{
			method : 'post',
			asynchronous : true,
			evalScripts: true,
			onLoading : function(resp)
			{
				Element.show('loader');
			},
			onFailure : function(resp)
			{
				alert('Error ' + resp.status + ' -- ' + resp.statusText);
			},
			onComplete : function(resp)
		  {
				Element.hide('loader');
				myLytebox.updateLyteboxItems();
			}
		});
	}
}

function setDefault(locMarker, propertyID, fileID, filename)
{
	if(confirm('You are going to set ' + filename + ' as default for this Property. Are you sure?'))
	{
		new Ajax.Request ("/briki/functions/brikiFunctions.php?propertyID="+propertyID+"&fileID="+fileID+"&action=setDefault", 
		{
			method : 'post',
			asynchronous : true,
			evalScripts: true,
			onLoading : function(resp)
			{
				Element.show('loader');
			},
			onFailure : function(resp)
			{
				alert('Error ' + resp.status + ' -- ' + resp.statusText);
			},
			onSuccess : function(resp)
			{
				Element.hide('loader');
				var curFileID = resp.responseText;
				
				$('photoBackground_'+curFileID).style.backgroundColor = "";
				$('defLink_'+curFileID).style.display = "inline";
								
				//Now turn on the new current fileID divs
				$('photoBackground_'+fileID).style.backgroundColor = "#EBFFEB";
				$('defLink_'+fileID).style.display = "none";
				showImage(propertyID);
			}
		});				
	}
}

function highlightTab(hash)
{
	var items = $$('#navcontainer li');
	
	for (var i = 0; i < items.length; i++)
	{
		items[i].className = '';
	}
	
	if(hash == '#top') $('Overview').className = "act";
	else if(hash == '#locationLink') $('Location').className = "act";
	else if(hash == '#informationLink') $('Information').className = "act";
	else if(hash == '#photographsLink') $('Photos').className = "act";
	else if(hash == '#documentationLink') $('Documents').className = "act";
	else if(hash == '#discussionLink') $('Discussion').className = "act";
	else if(hash == '#buildReportLink') $('BuildReport').className = "act";
	else $('Overview').className = "act";
}

function resetCheckProperty()
{
	Element.hide('propertyErrorMessage');
	Element.hide('propertyVerifiedMessage');
	Element.hide('addPropertyButton');
	$('checkPropertyButton').disabled = false;
	$('checkPropertyButton').value = "Verify Address";
	Element.show('checkPropertyButton');
	Element.hide('extraProps');
}

function checkProperty()
{
	if((! $('addAddress1').value) || 
		 (! $('addCity').value) ||
		 (! $('addState').value) || 
		 (! $('addZip').value))
	{
		$('checkPropertyButton').disabled = false;
		$('checkPropertyButton').value = "Verify Property";
		Element.show('propertyErrorMessage');
		$('propertyErrorMessage').innerHTML = "Fields highlighted in red are required. Please adjust the address accordingly and click the 'Check Property' button below.";
		return;
	}
	
	Element.hide('propertyErrorMessage');
	Element.hide('propertyVerifiedMessage');
	Element.hide('addPropertyButton');
	$('checkPropertyButton').disabled = true;
	$('checkPropertyButton').value = "Verifying Address...";
	
	var addAddress = $('addAddress1').value + ", " + $('addCity').value + ", " + $('addState').value + ", " + $('addZip').value;
	var accuracy = null;
	
	if (GBrowserIsCompatible())
	{
		var gcoder = new GClientGeocoder();	
		
		gcoder.getLatLng
		(
			addAddress,
			function(point)
			{
				if(!point)
				{
					$('checkPropertyButton').disabled = false;
					$('checkPropertyButton').value = "Verify Property";
					Element.show('propertyErrorMessage');
					$('propertyErrorMessage').innerHTML = "Sorry, address " + addAddress + " could not be found. Please adjust the address accordingly and click the 'Check Property' button below.";
				}
				else
				{
					gcoder.getLocations
					(
						addAddress,
						function(details)
						{
							if(!details)
							{
								$('propertyErrorMessage').innerHTML = "Sorry, address " + addAddress + " could not be found. Please adjust the address accordingly and click the 'Check Property' button below.";
							}
							else if(details.Placemark.length > 1)
							{
								addresses = Object.toJSON(details.Placemark);
								
								new Ajax.Updater({success: 'extraProps'},"/briki/functions/brikiFunctions.php?action=showAddOptions",
								{
									method : 'post',
									asynchronous : false,
									evalScripts : true,
									parameters : {addresses: addresses, searchAddress: addAddress},
									on400 : function(resp)
									{
										alert(resp.statusText);
									},
									onFailure : function(resp)
									{
										alert('Error ' + resp.status + ' -- ' + resp.statusText);
									},
									onComplete : function(resp)
									{
										Element.show('extraProps');
									}
								});
							}
							else
							{
								place = details.Placemark[0];
								// Use Google reverse geocoding to pull/cleanup address information and insert into the form.
								$('addCity').value 		= place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
								$('addState').value 	= place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
								$('addZip').value 		= place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
								$('addCountry').value	= place.AddressDetails.Country.CountryNameCode;
								$('addCounty').value 	= place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
							
								var lat = point.lat();
								var lng = point.lng();
								
								Element.hide('checkPropertyButton');
								Element.show('propertyVerifiedMessage');
								$('propertyVerifiedMessage').innerHTML = "Address " + addAddress + " was verified! You can add the property to the Briki, by clicking the 'Add Property' button below.";
								$('addLatitude').value = lat;
								$('addLongitude').value = lng;
								Element.show('addPropertyButton');
							}
						}
					);
					
					// var lat = point.lat();
					// var lng = point.lng();
					
					// Element.hide('checkPropertyButton');
					// Element.show('propertyVerifiedMessage');
					// $('propertyVerifiedMessage').innerHTML = "Address " + addAddress + " was verified! You can add the property to the Briki, by clicking the 'Add Property' button below.";
					// $('addLatitude').value = lat;
					// $('addLongitude').value = lng;
					// Element.show('addPropertyButton');
				}
			}
		);
	}
}

function fillAddPropForm(address, city, state, zip, country, lat, lng)
{
	$('addAddress1').value = address;
	$('addCity').value =  city;
	$('addState').value =  state;
	$('addZip').value =  zip;
	$('addCountry').value =  country;
	$('addLatitude').value = lat;
	$('addLongitude').value = lng;
	
	Element.hide('checkPropertyButton');
	Element.show('propertyVerifiedMessage');
	$('propertyVerifiedMessage').innerHTML = "Address " + address + " was verified! You can add the property to the Briki, by clicking the 'Add Property' button below.";
	Element.show('addPropertyButton');
	Element.hide('addressAddOptions');
}

function verifyAndSearch(address, fromAddress)
{
	if(fromAddress) {	$('searchResults').innerHTML='<p style="text-align:center"><img src="/briki/images/search-loader.gif" class="imgNoBorder loadImg" /><br/>Searching...</p>'; }
	
	var accuracy = null;
	var message = '';
	var newaddress = '';
	var city = '';
	var state = '';
	var zip = '';
	
	if (GBrowserIsCompatible())
	{
		var gcoder = new GClientGeocoder();	
		
		gcoder.getLatLng
		(
			address,
			function(point)
			{
				if(!point)
				{
					Element.hide('searchResults');
					Element.show('searchResultsFail');
				}
				else
				{
					gcoder.getLocations
					(
						address,
						function(details)
						{
							if(!details)
							{
								Element.hide('searchResults');
								Element.show('searchResultsFail');
							}
							else if(details.Placemark.length > 1)
							{
								addresses = Object.toJSON(details.Placemark);
								
								new Ajax.Updater({success: 'searchResults'},"/briki/functions/brikiFunctions.php?action=showSearchOptions",
								{
									method : 'post',
									asynchronous : false,
									evalScripts : true,
									parameters : {addresses: addresses, searchAddress: address},
									on400 : function(resp)
									{
										alert(resp.statusText);
									},
									onFailure : function(resp)
									{
										alert('Error ' + resp.status + ' -- ' + resp.statusText);
									}
								});
							}
							else
							{
								place = details.Placemark[0];
								// Use Google reverse geocoding to pull/cleanup address information and insert into the form.
								if(place.AddressDetails.Country.AdministrativeArea)
								{
									if(place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName)
									{
										state = place.AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;
									}
									else
									{
										state = '';
									}
									
									// Get the City if Google knows it.
									if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality)
									{
										if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName)
										{
											city = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
										}
										else
										{
											city = '';
										}
										
										//Get the Updated Address if Google knows it.
										if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare)
										{
											if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName)
											{
												newaddress = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
											}
											else
											{
												newaddress = '';
											}
										}
										
										//Get the Zip if Google knows it.
										if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode)
										{
											if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber)
											{
												zip = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber;
											}
											else
											{
												zip = '';
											}
										}
										
										//Get the county if Google knows it.
										if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea)
										{
											if(place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName)
											{
												county = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName;
											}
											else
											{
												county = '';
											}
										}
										
										//Get the country if Google knows it.
										if(place.AddressDetails.Country)
										{
											if(place.AddressDetails.Country.CountryNameCode)
											{
												country = place.AddressDetails.Country.CountryNameCode;
											}
											else
											{
												country = '';
											}
										}
									}
									else if(place.AddressDetails.Country.AdministrativeArea.AddressLine)
									{
										city = place.AddressDetails.Country.AdministrativeArea.AddressLine[0];
									}
									else
									{
										city = '';
									}										
								}
								
								new Ajax.Updater({success: 'searchResults'},"/briki/functions/brikiFunctions.php?action=getSearchResults&address="+newaddress+"&city="+city+"&state="+state+"&zip="+zip+"&county="+county+"&country="+country,
								{
									method : 'post',
									asynchronous : false,
									evalScripts : true,
									on400 : function(resp)
									{
										alert(resp.statusText);
									},
									onFailure : function(resp)
									{
										alert('Error ' + resp.status + ' -- ' + resp.statusText);
									}
								});
							}
						}
					);
				}
			}
		);
	}
}

function buildReport(formname)
{
	var selectedPieces = Form.serialize(formname);
	var alertString = selectedPieces.replace(/&/g, "\n");
	var alertString2 = alertString.replace(/=on/g, "");
	
	if(alertString2)
	{
		alert(
					"The build report function does not work yet.\n\n" + 
				  "The parts of the property jacket you chose were:\n\n" + 
					alertString2
				);
	}
	else
	{
		alert(
					"The build report function does not work yet.\n\n" + 
				  "You did not choose any parts of the property jacket to include"
				);
	}
}

function ptdsUploaderSettings (propertyTypeID,functionPage, fileTypes, fileDesc)
{
	var settings = {
		upload_url: functionPage+"&propertyTypeID="+propertyTypeID+"&action=ptdsUpload",
		// File Upload Settings
		file_size_limit : "10000",	// 10MB
		file_types : fileTypes,
		file_types_description : fileDesc,
		file_upload_limit : "101",
		file_queue_limit : 0,
	
		// Event Handler Settings (all my handlers are in the Handler.js file)
		file_dialog_start_handler : fileDialogStart,
		file_queued_handler : ptdsFileQueued,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_start_handler : uploadStart,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : ptdsUploadComplete,
	
		// Button settings
		button_image_url : "/briki/scripts/flashUploadImage.gif",	// Relative to the SWF file
		button_placeholder_id : "flashButtonPlaceholder_"+propertyTypeID,
		button_width: 98,
		button_height: 25,
	
		// Flash Settings
		flash_url : "/briki/scripts/swfupload.swf",	// Relative to this file (or you can use absolute paths)
		
		swfupload_element_id : "flashUI_"+propertyTypeID,		// Setting from graceful degradation plugin
		degraded_element_id : "degradedUI_"+propertyTypeID,	// Setting from graceful degradation plugin
	
		custom_settings : {
			progressTarget : "fsUploadProgress_"+propertyTypeID,
			cancelButtonId : "btnCancel_"+propertyTypeID,
			page :	functionPage,
			propertyTypeID :	propertyTypeID
		},
		
		// Debug Settings
		debug: false
	};
	return settings;						
}

function checkPTDSFileExists (functionPage)
{
	var fe = 0;
	new Ajax.Updater({},functionPage+"&action=checkPTDSFileExists",
	{
		method : 'post',
		asynchronous : false,
		evalScripts : true,
		on400 : function(resp)
		{
			alert(resp.statusText);
		},
		onFailure : function(resp)
		{
			alert('Error ' + resp.status + ' -- ' + resp.statusText);
		},
		on200 : function (resp)
		{
			fe = resp.statusText;
		}
	});	
	return fe;
}

function ptdsRefreshFilesPhotos(functionPage, propertyTypeID)
{
	new Ajax.Updater({success: 'Photos'},functionPage+"&propertyTypeID="+propertyTypeID+"&action=ptdsRefreshFilesPhotos",
	{
		method : 'post',
		asynchronous : true,
		evalScripts : true,
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		on400 : function(resp)
		{
			alert(resp.statusText);
		},
		onFailure : function(resp)
		{
			alert('Error ' + resp.status + ' -- ' + resp.statusText);
		},
		onComplete : function(resp)
		{
			myLytebox.updateLyteboxItems();	
			Element.hide('loader');
		}
	});	
}

function autosavePTDS(propertyTypeID)
{
	new Ajax.Request('/briki/functions/brikiFunctions.php?action=autosavePTDS&propertyTypeID='+propertyTypeID,
	{
		method: 'post',
		parameters : Form.serialize('ptds'),
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		onSuccess : function(resp)
		{
			setTimeout("Element.show('autoSaveOk')", 500);
			setTimeout("Element.hide('autoSaveOk')", 3000);
			Element.hide('loader');
		}
	});
}

function autosavePTDSTable(propertyTypeID, tablename, divtoupdate)
{
	new Ajax.Request('/briki/functions/brikiFunctions.php?action=autosavePTDSTable&propertyTypeID='+propertyTypeID+'&tablename='+tablename+'&divtoupdate='+divtoupdate,
	{
		method: 'post',
		parameters : Form.serialize(tablename),
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		onSuccess : function(resp)
		{
			setTimeout("Element.show('"+divtoupdate+"OK')", 500);
			setTimeout("Element.hide('"+divtoupdate+"OK')", 3000);
			Element.hide('loader');
		}
	});
}

function ptdsAutosave(ev, propertyTypeID)
{
	if (!ev) var ev = window.event;
		knum++;
		window.setTimeout('if (' + knum + '==knum) autosavePTDS('+propertyTypeID+');', 5000);
}

function startPTDSTableAutosave(ev, propertyTypeID, tablename, divtoupdate)
{
	if (!ev) var ev = window.event;
		knum++;
		ptdsTbl = window.setTimeout('if (' + knum + '==knum) autosavePTDSTable('+propertyTypeID+',"'+tablename+'","'+divtoupdate+'");', 5000);
}

function deletePTDSFile(propertyTypeID, fileID, filename)
{
	if(confirm('You are about to delete ' + filename + '. Are you sure?'))
	{
		new Ajax.Updater ({success: 'Photos'},"/briki/functions/brikiFunctions.php?propertyTypeID="+propertyTypeID+"&fileID="+fileID+"&filename="+filename+"&action=deletePTDSFile",
		{
			method : 'post',
			asynchronous : true,
			evalScripts: true,
			onLoading : function(resp)
			{
				Element.show('loader');
			},
			onFailure : function(resp)
			{
				alert('Error ' + resp.status + ' -- ' + resp.statusText);
			},
			onComplete : function(resp)
		  {
				Element.hide('loader');
				myLytebox.updateLyteboxItems();
			}
		});
	}
}

function addRow(tablename, divname, propertyTypeID)
{
	if($(tablename))
	{
		var saved = autosavePTDSTable(propertyTypeID, tablename, divname);
		clearTimeout(ptdsTbl);
	}
	
	new Ajax.Updater ({success: divname},"/briki/functions/brikiFunctions.php?propertyTypeID="+propertyTypeID+"&action=addRow&tablename="+tablename+"&divname="+divname, 
	{
		method : 'post',
		asynchronous : true,
		evalScripts: true,
		onLoading : function(resp)
		{
			Element.show('loader');
		},
		onFailure : function(resp)
		{
			alert('Error ' + resp.status + ' -- ' + resp.statusText);
		},
		onComplete : function(resp)
		{
			Element.hide('loader');
		}
	});
}

function ptdsDelete(tablename, divname, propertyTypeID, rowID)
{
	if($(tablename))
	{
		var saved = autosavePTDSTable(propertyTypeID, tablename, divname);
		clearTimeout(ptdsTbl);
	}

	if(confirm('You are about to delete this row. Are you sure?'))
	{
		new Ajax.Updater ({success: divname},"/briki/functions/brikiFunctions.php?propertyTypeID="+propertyTypeID+"&action=ptdsDelete&tablename="+tablename+"&divname="+divname+"&rowID="+rowID, 
		{
			method : 'post',
			asynchronous : true,
			evalScripts: true,
			onLoading : function(resp)
			{
				Element.show('loader');
			},
			onFailure : function(resp)
			{
				alert('Error ' + resp.status + ' -- ' + resp.statusText);
			},
			onComplete : function(resp)
			{
				Element.hide('loader');
			}
		});
	}
}