function saveProperty() {
	var propertyName = $('#propertyName');
	var propertyAddress = $('#propertyAddress');
	var propertySuburb = $('#propertySuburb');
	var propertyType = $('#propertyType');
	var propertyMinStay = $('#propertyMinStay');
	var propertyNoBeds = $('#propertyNoBeds');
	var propertyNoBath = $('#propertyNoBath');
	var propertySize = $('#propertySize');
	var propertyDaily = $('#propertyDaily');
	var propertyWeekly = $('#propertyWeekly');
	var propertyMonthly = $('#propertyMonthly');
	var propertyLocation = $('#propertyLocation');
	var personSharing = $('#personSharing');
	var extraPerson = $('#extraPerson');
	var propertySum = $('#summary');
	var propertyNightVis = $('#propertyDailyVis');
	var propertyWeekVis = $('#propertyWeeklyVis');
	var propertyMonthVis = $('#propertyMonthlyVis');
    var extraPersVis = $('#extraPersVis');

    var oDesc = FCKeditorAPI.GetInstance('propertyDesc');
    var desc = oDesc.GetHTML(true);
		
	if ((propertyName).val() == "") {
		alert("Please enter a property name");
		propertyName.focus();
	} else if (propertyAddress.val() == "") {
		alert("Please enter the property address");
		propertyAddress.focus();
	} else if (propertySuburb.val() == "") {
		alert("Please select a suburb");
		propertySuburb.focus();
	} else if (propertyType.val() == "") {
		alert("Please select a property type");
		propertyType.focus();
	} else if (propertySize.val() == "") {
		alert("Please enter a property size");
		propertySize.focus();
	} else {
		var property = $('#propertyId').length;
		if (property == 1) {
			propertyId = $('#propertyId').val();
		} else {
			propertyId = '';
		}
		$.ajax({
			cache : false,
			type  : "POST",
			data : {
                    'propertyName' : propertyName.val(),
					'propertyAddress' : propertyAddress.val(),
					'propertySuburb':propertySuburb.val(),
					'propertyType' : propertyType.val(),
					'propertyMinStay':propertyMinStay.val(),
					'propertyNoBeds' : propertyNoBeds.val(),
					'propertyNoBath' : propertyNoBath.val(),
					'propertySize' : propertySize.val(),
					'propertyDaily' : propertyDaily.val(),
					'propertyWeekly' : propertyWeekly.val(),
					'propertyMonthly' : propertyMonthly.val(),
					'propertyDesc' : desc, 
					'propertyId' : propertyId,
					'propertyLocation' : propertyLocation.val(),
					'personSharing' : personSharing.val(),
					'extraPerson' : extraPerson.val(),
					'propertySum' : propertySum.val(),
					'propertyNightVis' : propertyNightVis.attr("checked"),
					'propertyWeekVis' : propertyWeekVis.attr("checked"),
					'propertyMonthVis' : propertyMonthVis.attr("checked"),
                    'extraPersVis' : extraPersVis.attr("checked")
				   },
					url : "scripts/saveProperty.php",
					success : function(msg) {
						var ret = msg.split("-|||-");
						if (ret[0] == 1) {//Success						
							$('#flex1').flexReload("loadProperties.php");
						} else {
							alert("An error occurred while saving.\n\n"+
								  "Error Message : " + ret[1]);
						}
						$.nyroModalRemove();
					}
				});
	}
}


function delProperty(propertyId) {
	if (confirm("Are you sure you want to delete this property?")) {
    	var date = new Date();
        var timestamp = date.getTime();
        $.ajax({
			cache : false,
			type  : "POST",
			data : {'timestamp' : timestamp, 'propertyId' : propertyId},
					url : "scripts/delProperty.php",
					success : function(msg) {
						var ret = msg.split("-|||-");
						if (ret[0] == 1) { //Success
							$('#flex1').flexReload("loadResorts.php");	
						} else {
							alert("An error occurred : \n"
								  +ret[1]);
						}
					}
		});
	}
}

function newProperty() {
	var date = new Date();
	var timestamp = date.getTime();
			
	$.fn.nyroModalManual({
		modal : true,
		type : 'iframe',
		minWidth : 800,
		minHeight : 600,
		resizeable: true,
		padding : 30,
		url : 'scripts/addProperty.php?timestamp='+timestamp
	});
	return false;
}

function cancelAdd(msg) {
 	var str = "";
    if (msg == "edit") {
		 str = "Are you sure you want to cancel editing this property?";
	} else {
		 str = "Are you sure you want to cancel adding this property?";
	}
	if (confirm(str)) {
		$.nyroModalRemove();	
	}
}

function loadPropImages(propertyId){
	var propPic = $('#propPics');
	propPic.html("<p align='center'><img src='images/checker.gif' align='absmiddle'><br/><br/>Loading Images</p>");
	var date = new Date();
    var timestamp = date.getTime();
    $.ajax({
		cache : false,
		type  : "POST",
		data : {'timestamp' : timestamp, 'propertyId' : propertyId},
		url : "scripts/loadPropImages.php",
		success : function(msg) {
			$('#propPics').html(msg);
		}
	});
}

function delImage(dir,file,propertyId) {
	if (confirm("Are you sure you want to delete this image?")) {
		var propPic = $('#propPics');
		propPic.html("<p align='center'><img src='images/checker.gif' align='absmiddle'><br/><br/>Deleting Image</p>");
		var date = new Date();
	    var timestamp = date.getTime();
	    $.ajax({
			cache : false,
			type  : "POST",
			data : {'timestamp' : timestamp, 'dir' : dir, 'file' : file, 'propertyId' : propertyId},
			url : "scripts/deleteImage.php",
			success : function(msg) {
				msg = msg.replace(/^\s*|\s*$/g,'');
				if (msg != "") { //Success
					loadPropImages(msg);
				}
			}
		});	
	}
}

function searchProperty(){
	var date = new Date();
	var timestamp = date.getTime();
	var propertyType = $('#propertyType');
	var propertyBeds = $('#propertyBeds');
	var propertyBath = $('#propertyBath');
	var propertySuburb = $('#propertySuburb');
	var searchResult = $('#searchResult');

	$.ajax({
		cache : false,
		type  : "POST",
		data : {'timestamp' : timestamp, 
				'propertyType' : propertyType.val(),
				'propertyBeds' : propertyBeds.val(),
				'propertyBath' : propertyBath.val(),
				'propertySuburb' : propertySuburb.val()
			   },
		url : "admin/scripts/propertySearch.php",
		beforeSend : function(){
			searchResult.html("<p align='center'><img src='images/ajaxLoader.gif' align='absmiddle'><br/>Searching...</p>");
		},
		success : function(msg) {
			searchResult.css("visibility","visible");
			searchResult.html(msg);
		}
	});	
}

function updatePropertyPage(pageId,propertyId) {
    var selected = $('#page_'+pageId).attr("checked");
    var date = new Date();
	var timestamp = date.getTime();
    var status = $("#pagesStatus");
    $.ajax({
		cache : false,
		type  : "POST",
		data : {'timestamp' : timestamp,
				'propertyId' : propertyId,
				'pageId' : pageId,
				'selected' : selected
			   },
		url : "scripts/updatePropertyPage.php",
		beforeSend : function(){
			$('#pagesFS :checkbox').attr("disabled","disabled");
            status.html("Saving Changes...");
			status.show(1000);
		},
		success : function(data) {
            var res = data.split("-|||-");
            status.html("");
           	if (res[0] != 0) { //Failed
                status.removeClass("saveSuccess");
				status.addClass("saveError").html(res[1]);
				status.show(3000);
			} else {
				status.show(1000);
				status.removeClass("saveError");
				status.addClass("saveSuccess").html("Changes saved").animate({opacity:'toggle'},4000);
			}            
            $('#pagesFS :checkbox').attr("disabled","");
		}
	});
}

function setDefault(propertyId,fileName) {
    var date = new Date();
	var timestamp = date.getTime();
    $.ajax({
		cache : false,
		type  : "POST",
		data : {'timestamp' : timestamp,
				'propertyId' : propertyId,
				'fileName' : fileName
			   },
		url : "scripts/updateDefaultImage.php",
		success : function(data) {
            var res = data.split("-|||-");
            if (res[0] == 0) { //Error
                var msg = "An error occurred : \n";
                    msg += res[1];
                 alert(msg);
            }
		}
	});
}


