var property_purpose_cont = 0;
$(document).ready(function(){
	$('.login').click(function(){
	   closeDataDiv();
		var a = $(this).attr('id');
		var cap = 'Agent Login';
		
		if( a == '2')
			cap = 'Agent Login';
		if( a == '3')
			cap = 'User Login';
		$.ajax({
				type: "POST",
				url: "login.php",
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
					$('#caption').html(cap)
				}
			});
	});
	$('.registration').click(function(){
		closeDataDiv();
		var a = $(this).attr('id');
		var cap = 'Agent Registration';
		if( a == '2')
			cap = 'Agent Registration';
		if( a == '3')
			cap = 'User Registration';
		$.ajax({
				type: "POST",
				url: "templates/registration.tpl?id=2",
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
					$('#userType').val(a);
					$('#caption').html(cap);
				}
			});
	});
	$(['images/top_bar_bg.png','images/logo_bg.png','images/box_top_mid.png','images/box_top_left.png','images/box_top_right.png','images/box_body_left.png','images/box_body_right.png','images/box_body_bg.png','images/box_bottom_mid.png','images/box_bottom_left.png','images/box_bottom_right.png','images/login_input_bg.png','images/button_bg.png','images/panel_button_img.png','images/tab_left.png','images/tab_right.png','images/textarea_bg.png','images/grid_heading_bg.jpg','images/login_left_bg.png','images/login_input_bg1.png']).preload();
});
function changeProPurpose(val)
{
	dataLoad(val);
	property_purpose_cont = val;
}
function openDetailsPopup(property_id)
{
	closeDataDiv();
	$.ajax({
		type: "POST",
		url: "property-details.php",
		data: "property_id="+property_id,
		success: function(msg){
			$('#contentDiv').show();
			$('#contentDiv').html(msg);
			getProperty(property_id);
		}
	});
}
function openlogin()
{
		closeDataDiv();
		var cap = 'Login';
		$.ajax({
				type: "POST",
				url: "login.php",
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
					//$('#userType').val(a);
					$('#caption').html(cap)
				}
			});
}
function openregister(ty)
{
	closeDataDiv();
	var a = ty;
		var cap = 'Registration';
		
		if( a == '2')
			cap = 'Registration';
		if( a == '3')
			cap = 'Registration';
		$.ajax({
				type: "POST",
				url: "templates/registration.tpl?id="+ty,
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
					$('#userType').val(a);
					$('#caption').html(cap);
				}
			});
}
function login()
{
	if( $.trim( $('#email').val() ) == "")
	{
		$('#loginMsg').html("Please enter email.");
		$('#email').focus();
	}
	else if($.trim($('#email').val()) != "" && !checkEmail($('#email').val()) )
	{
		$('#loginMsg').html('Please enter valid email.');
		$('#email').focus();
	}
	else if( $.trim( $('#password').val() ) == "")
	{
		$('#loginMsg').html("Please enter password.");
		$('#password').focus();		
	}
	else
	{
		$.ajax({
				type: "POST",
				url: "memberManager.php",
				data: "mode=login&email="+$('#email').val()+"&password="+$('#password').val(),
				dataType: "json",
				success: function(msg){
					if( msg[0] == 'Sucess')
					{
						$('#msgDiv').html(msg[1]);
						$('#contentDiv').html(msg[2]);
						$('#preLoginDiv').hide();
				if(property_purpose_cont == 0)
					$('#for_sale').attr('checked');
				if(property_purpose_cont == 1)
					$('#to_let').attr('checked');
					$('#mapType').val($('#map_type_cont').val());	
					}
					else
					{
						$('#loginMsg').html(msg[0]);
					}
				}
			});
	}
}
function registration()
{
	if( $.trim( $('#fname').val() ) == "")
	{
		$('#loginMsg').html("Please enter first name.");
		$('#fname').focus();
	}
	else if( $.trim( $('#lname').val() ) == "")
	{
		$('#loginMsg').html("Please enter last name.");
		$('#lname').focus();
	}
	else if( $.trim( $('#email').val() ) == "")
	{
		$('#loginMsg').html("Please enter email.");
		$('#email').focus();
	}
	else if( $.trim( $('#cell').val() ) == "")
	{
		$('#loginMsg').html("Please enter cell number.");
		$('#cell').focus();
	}
	else if($.trim($('#email').val()) != "" && !checkEmail($('#email').val()) )
	{
		$('#loginMsg').html('Please enter valid email.');
		$('#email').focus();
	}
	else if( $.trim( $('#password').val() ) == "")
	{
		$('#loginMsg').html("Please enter password.");
		$('#password').focus();		
	}
	else
	{
		$.ajax({
				type: "POST",
				url: "memberManager.php",
				data: "mode=registration&fname="+$('#fname').val()+"&lname="+$('#lname').val()+"&email="+$('#email').val()+"&cell="+$('#cell').val()+"&password="+$('#password').val()+"&userType="+$('#userType').val()+"&imgname="+$('#imgname').val(),
				success: function(msg){
					if( msg = 'Sucess')
					{
						$.ajax({
							type: "POST",
							url: "templates/sucess.tpl",
							success: function(msg){
								$('#contentDiv').show();
								$('#contentDiv').html(msg);
							}
						});
					}
					else
					{
						$('#loginMsg').html(msg);
						$('#loginMsg').focus();		
					}
				}
			});
	}
}
function logout()
{
	$.ajax({
			type: "POST",
			url: "memberManager.php",
			data: "mode=logOut",
			success: function(msg){
				alert('You are sucessfully logout.');
				if(property_purpose_cont == 0)
					$('#for_sale').attr('checked');
				if(property_purpose_cont == 1)
					$('#to_let').attr('checked');
				$('#mapType').val($('#map_type_cont').val());
				$('#msgDiv').html('');
				$('#contentDiv').html('');
				$('#contentDiv').hide();
				$('#preLoginDiv').html(msg);
				$('#preLoginDiv').show();
			}
		});
}
function showHome()
{
	closeDataDiv();
	$.ajax({
			type: "POST",
			url: "memberManager.php",
			data: "mode=showHome",
			success: function(msg){
				$('#contentDiv').show();
				$('#contentDiv').html(msg);
			}
		});
}
function viewFeatureExterior()
{
	closeDataDiv();
	$.ajax({
				type: "POST",
				url: "feature-exterior.php",
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
				}
			});
}
function viewFeatureSecurity(){
	closeDataDiv();
	$.ajax({
			type: "POST",
			url: "feature-security.php",
			success: function(msg){
				$('#contentDiv').show();
				$('#contentDiv').html(msg);
			}
		});
}
function viewFeatureInterior()
{
	closeDataDiv();
	$.ajax({
				type: "POST",
				url: "feature-interior.php",
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
				}
			});
}
function viewUser()
{
	closeDataDiv();
	$.ajax({
				type: "POST",
				url: "user.php?ty=user",
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
				}
			});
}
function viewFavourite()
{
	closeDataDiv();
	$.ajax({
		type: "POST",
		url: "favourite-property.php",
		success: function(msg){
			$('#contentDiv').show();
			$('#contentDiv').html(msg);
		}
	});	
}
function viewAgent()
{
	closeDataDiv();
	$.ajax({
				type: "POST",
				url: "user.php?ty=agent",
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
				}
			});
}
function viewProperty()
{
	closeDataDiv();
	$.ajax({
				type: "POST",
				url: "view-proparty.php",
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
				}
			});
}
function checkEmail(value) {
		var emailexpr=/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
		return emailexpr.test(value);
	}
function closeDataDiv()
{
	closeDataDiv1();	
	$('#contentDiv').hide();
	$('#contentDiv').html('');
	$('.ad-preloads').remove();
}
function closeDataDiv1()
{
	$('#contentDiv1').hide();
	$('#contentDiv1').html('');
	$('.ad-preloads').remove();
}
function addProp()
{
	closeDataDiv();
	$.ajax({
				type: "POST",
				url: "add-proparty.php",
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
				}
			});
}
function editProp(id)
{
	closeDataDiv();
	$.ajax({
				type: "POST",
				url: "edit-proparty.php?propId="+id,
				success: function(msg){
					$('#contentDiv').show();
					$('#contentDiv').html(msg);
				}
			});
}
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}
function askForOptionView()
{
	 dataLoad(0);	
}
function openFilterDiv()
{
	closeDataDiv();
	$.ajax({
				type: "POST",
				url: "filter.php",
				success: function(msg){
					$('#contentDiv1').show();
					$('#contentDiv1').html(msg);
				}
			});	
}
function searchTy()
{
	if($(".filty").length==$(".filty:checked").length)
	{
		$('.prifield').attr('readonly',true);
	}
	else
	{
		$('.prifield').attr('readonly',false);
	}
}
function openUserMenu()
{
	if($('#menushowdiv').css('display') == "none")
	{
		$('#menushowdiv').show();
	}
	else
	{
		$('#menushowdiv').hide();
	}
}
function openFeedbackFrm()
{
	closeDataDiv();
	$.ajax({
			type: "POST",
			url: "templates/feedback-frm.tpl",
			success: function(msg){
				$('#contentDiv1').show();
				$('#contentDiv1').html(msg);
			}
		});	
}
function sendFeedback()
{
	$('#feedbackmsg').css('color', '#FF0000');	
	if( $.trim( $('#fname').val() ) == "")
	{
		$('#feedbackmsg').html("Please enter firstname.");
		$('#fname').focus();
	}
	else if( $.trim( $('#lname').val() ) == "")
	{
		$('#feedbackmsg').html("Please enter Surname.");
		$('#lname').focus();
	}
	else if( $.trim( $('#email').val() ) == "")
	{
		$('#feedbackmsg').html("Please enter email.");
		$('#email').focus();
	}
	else if($.trim($('#email').val()) != "" && !checkEmail($('#email').val()) )
	{
		$('#feedbackmsg').html('Please enter valid email.');
		$('#email').focus();
	}
	else if($(".chkbx:checked").length == 0)
	{
		$('#feedbackmsg').html('Please enter type of feedback.');
	}
	else if( $.trim( $('#description').val() ) == "")
	{
		$('#feedbackmsg').html("Please enter description.");
		$('#description').focus();
	}
	else
	{
		$('#feedbackmsg').html("Please wait...");	
		$('#feedbackmsg').css('color', '#003300');
		$.ajax({
			type: "POST",
			url: "feedback.php",
			data: "mode=sendFeedback&"+$('#feedbackfrm').serialize(),
			success: function(msg){
				alert("Thank you for your feedback.");
				closeDataDiv();
			}
		});
	}
}
function charCount( id, obj, totChar)
{
	var len = $(obj).val().length;
	
		
	$('#'+id).html(totChar-len);
	if( len > totChar)
	{
		var da = $(obj).val();
		$(obj).val(da.substr(0,totChar));
	}
}
function viewComplainFeedback()
{
	closeDataDiv();
	$.ajax({
		type: "POST",
		url: "feedback.php?ty=complain",
		success: function(msg){
			$('#contentDiv').show();
			$('#contentDiv').html(msg);
		}
	});	
}
function viewComplainFeedbackAdmin()
{
	closeDataDiv();
	$.ajax({
		type: "POST",
		url: "feedback.php?ty=complainAdmin",
		success: function(msg){
			$('#contentDiv').show();
			$('#contentDiv').html(msg);
		}
	});
}
function viewSuggestFeedback()
{
	closeDataDiv();
	$.ajax({
		type: "POST",
		url: "feedback.php?ty=suggest",
		success: function(msg){
			$('#contentDiv').show();
			$('#contentDiv').html(msg);
		}
	});	
}
function viewSuggestFeedbackAdmin()
{
	closeDataDiv();
	$.ajax({
		type: "POST",
		url: "feedback.php?ty=suggestAdmin",
		success: function(msg){
			$('#contentDiv').show();
			$('#contentDiv').html(msg);
		}
	});	
}
function viewReport(id)
{
	closeDataDiv();
	$.ajax({
		type: "POST",
		url: "report-listing-admin.php?id="+id,
		success: function(msg){
			
			$('#contentDiv').show();
			$('#contentDiv').html(msg);		
		}
	});		
	
}
