var $j = jQuery.noConflict();
$j(document).ready(function(){
	$j('#cms').gradient({
		from:      'B1D0DE',
		to:        'FFFFFF',
		direction: 'vertical',
	});

	$j("#threeinone").click(function(){
		var defaultVal = document.getElementById('threeinone').defaultValue;
		if ($j("#threeinone").val() == defaultVal) {
			$j("#threeinone").val("");
			$j("#threeinone").css({color: 'black'});
		}
	})
	$j("#threeinone").focus(function(){
		var defaultVal = document.getElementById('threeinone').defaultValue;
		if ($j("#threeinone").val() == defaultVal) {
			$j("#threeinone").val("");
			$j("#threeinone").css({color: 'black'});
		}
	})
	$j("#threeinone").blur(function(){
		if($j("#threeinone").val() == ''){ 
			$j("#threeinone").val('Type keywords here');
			$j("#threeinone").css({color: '#C0C0C0'});
		}
	})
	
	$j("#projectTabs").hide();
	$j("#projectTabs").tabs();
	$j("#projectTabs").show("slow");
	if($j("#market").val() == "rent"){ 
			$j("#pbf").selectOptions("");
			$j("#pbt").selectOptions("");
			$j("#pbf").hide();
			$j("#pbt").hide();
			$j("#prf").show();
			$j("#prt").show();
		}
		else{
			$j("#prf").selectOptions("");
			$j("#prt").selectOptions("");
			$j("#pbf").show();
			$j("#pbt").show();
			$j("#prf").hide();
			$j("#prt").hide();
	}
	$j("#market").change(function(){
		if($j("#market").val() == "rent"){ 
			$j("#pbf").selectOptions("");
			$j("#pbt").selectOptions("");
			$j("#pbf").hide();
			$j("#pbt").hide();
			$j("#prf").show();
			$j("#prt").show();
		}
		else{
			$j("#prf").selectOptions("");
			$j("#prt").selectOptions("");
			$j("#pbf").show();
			$j("#pbt").show();
			$j("#prf").hide();
			$j("#prt").hide();
		}
	})

	
	$j("#commentSubmit").click(function(){
		$j("#commentForm").validate({
			rules: {
				commentName: { required: true },
				commentEmail: { required: true, email: true },
				commentTxt: { required: true}
			},
			messages: {
				commentName: { required: "Name cannot be left empty." },
				commentEmail: { required: "Email address cannot be left empty.", email: "This is not valid email address." },
				commentTxt: { required: "Comment cannot be left empty."}
			},
			submitHandler: function() {
				$j("#commentForm").ajaxSubmit({
					url: "/template/kimrealty/action.php?propertyid=" + $j("#propertyid").html() + "&action=postComment",
					type: "post",
					timeout: 15000,
					beforeSubmit: function(){},
					error: function(xhr, desc, e) {
						var msg = (desc == 'timeout') ? 'Connection 20 sec Time Out, Please try again Later.': desc;
						alert("Failed to save: " + msg);
						// alert(xhr.responseText);
					},
					success: function(msg){
						var msg_arr = msg.split("||");
						switch(msg_arr[0]){
							case "true":
								$j('#oldComment').append(msg_arr[1]);
								$j("#commentForm").clearForm();
								break;
							case "false":
								$j("#errDialog").html(msg_arr[1]);
								$j("#errDialog").dialog("open");
								break;
							default:
								alert(msg_arr[1]);
						}
					},
					complete: function(){
						
					}
				})
			}
		})
	})
	
	$j("#state").change(function(){
		$j().ajaxSubmit({
			url: "/template/kimrealty/action.php?action=getSuburb&selectedState="+$j("#state").val(),
			type: "post",
			timeout: 15000,
			beforeSubmit: function(){},
			error: function(xhr, desc, e) {
				var msg = (desc == 'timeout') ? 'Connection 20 sec Time Out, Please try again Later.': desc;
				alert("Failed to save: " + msg);
				// alert(xhr.responseText);
			},
			success: function(msg){
				var msg_arr = msg.split("||");
				switch(msg_arr[0]){
					case "true":
						$j("#suburb").html("");
						$j("#suburb").html(msg_arr[1]);
						break;
					case "false":
						alert(msg_arr[1]);
						break;
					default:
						alert(msg_arr[1]);
				}
			},
			complete: function(){}
		});
	})
	
	$j("#estateResi").click(function(){
		getPropType($j("#estateResi").val());
	})
	$j("#estateComm").click(function(){
		getPropType($j("#estateComm").val());
	})
	
	getPropType = function(estate){
		$j().ajaxSubmit({
			url: "/template/kimrealty/action.php?action=getPropType&estate=" + estate,
			type: "post",
			timeout: 15000,
			beforeSubmit: function(){},
			error: function(xhr, desc, e) {
				var msg = (desc == 'timeout') ? 'Connection 20 sec Time Out, Please try again Later.': desc;
				alert("Failed to save: " + msg);
				// alert(xhr.responseText);
			},
			success: function(msg){
				var msg_arr = msg.split("||");
				switch(msg_arr[0]){
					case "true":
						$j("#cat").html("");
						$j("#cat").html(msg_arr[1]);
						break;
					case "false":
						alert(msg_arr[1]);
						break;
					default:
						alert(msg_arr[1]);
				}
			},
			complete: function(){}
		});
	}
	$j("#emSubmit").click(function(){
		emailMeValidate();
	});
	emailMeValidate = function(){
		$j("#emailme").validate({
			rules: {
				emName: { required: true },
				emEmail: { required: true, email: true },
				emContact: { required: true, number: true }
			},
			messages: {
				emName: { required: "Name cannot be left empty." },
				emEmail: { required: "Email address cannot be left empty.", email: "This is not valid email address." },
				emContact: { required: "Contact number cannot be left empty." }
			},
			submitHandler: function() { 
				$j("#emailme").ajaxSubmit({
					url: "/template/kimrealty/action.php?action=emailme&propertyid="+$j("#propertyid").html(),
					type: $j("#listingForm").attr("method"),
					timeout: 20000,
					beforeSubmit: function(){
					}, 
					error: function(xhr, desc, e) {
						var msg = (desc == 'timeout') ? 'Connection 20 sec Time Out, Please try again Later.': desc;
						alert("Failed to save: " + msg);
						// alert(xhr.responseText);
					},
					success: function(msg){
						var msg_arr = msg.split("||");
						switch(msg_arr[0]){
							case "true":
								$j("#sucDialog").html(msg_arr[1]);
								$j("#sucDialog").dialog("open");
								break;
							case "false":
								$j("#errDialog").html(msg_arr[1]);
								$j("#errDialog").dialog("open");
								break;
							default:
								alert(msg);
						}
					},
					complete: function(){
						//$j('form').clearForm();// clear data
					}
				});
			}
		});
		return false;
	}
	$j('#errDialog').dialog({
		autoOpen: false,
		width: '400',
		modal: true,
		zIndex: 3999,
		resizable: false,
		buttons: {
			"Ok": function() {
				$j(this).dialog("close"); 
			}
		}
	});
	$j('#sucDialog').dialog({
		autoOpen: false,
		width: '400',
		modal: true,
		zIndex: 3999,
		resizable: false,
		buttons: {
			"Ok": function() {
				$j(this).dialog("close"); 
			}
		}
	});
})