// JavaScript Document
$(document).ready(function(){
				   
	// ------------- scroll ------------
	if($('#menu_scroll').length > 0)
	{
		$("ul#menu_scroll").show();
		$("ul#menu_scroll").liScroll(); 
		//$("ul#menu_scroll").show();
	}
	
	$('.newsticker-jcarousellite').show();
	$(".newsticker-jcarousellite").jCarouselLite({
		vertical: true,
		hoverPause:true,
		visible: 3,
		auto:500,
		speed:1000
	});
	
		
	
	var baseUrl = $('#baseUrl').val();
	
	if($('.tableList').length > 0)
	{
		$('.tableList tr:odd').css("background-color", "#F4F4F8");
		$('.tableList tr:even').css("background-color", "#EFF1F1");
		$('.tableList tr:first').css("background-color", "#999999").addClass('listHeader');
	}
	if($('.states_dd').length > 0)
	{
		$('.states_dd').change(function(){
			if($(this).val != '')
			{
				if($('#loading_city').length > 0)
				{
					$('#loading_city').html('<img src="'+$('#baseUrl').val()+'/public/images/loader.gif" align="absmiddle">');
					$("#loading_city").ajaxStart(function(){
						$(this).show();
					});
					$("#loading_city").ajaxComplete(function(){
						$(this).hide();
						$('#loading_city').html('');
					});
				}
				$.ajax({
					type:"POST",
					url:$('#baseUrl').val()+"/Ajaxutility/getcity",
					data: "sid="+ $(this).val(),
					success: function(options_res)
					{
						if($('.city_dd').length > 0){
							$('.city_dd').html(options_res);
						}
					}
				});
				/*if($('#loading_city').length > 0)
				{
					$('#loading_city').html('');
				}*/
			}
		});	
	}
	if($('.city_dd').length > 0)
	{
		$('.city_dd').change(function(){
			//alert($('#city_dd').val());
			if($(this).val != '')
			{
				//alert($('#loading_location').length);
				if($('#loading_location').length > 0)
				{
					$('#loading_location').html('<img src="'+$('#baseUrl').val()+'/public/images/loader.gif" align="absmiddle">');
					$("#loading_location").ajaxStart(function(){
						$(this).show();
					});
					$("#loading_location").ajaxComplete(function(){
						$('#loading_location').html('');
						$(this).hide();
					});
				}
				$.ajax({
					type:"POST",
					url:$('#baseUrl').val()+"/Ajaxutility/getlocation",
					data: "cid="+ $(this).val(),  
					success: function(options_res)
					{
						if($('.location_dd').length > 0){
							$('.location_dd').html('<option value="">Select Location</option>'+options_res);
						}
						if($('.location_list').length > 0){
							var options_res = (options_res == '') ? '<option value="">Select Location</option>' : options_res;
							$('.location_list').html(options_res);
						}
					}
				});
				/*if($('#loading_location').length > 0)
				{
				$('#loading_location').html('');
				}*/
			}
		});
	}
	
	 if($('.datepicker').length > 0)
    {
        $('.datepicker').attr('autoComplete','off');
        $('.datepicker').attr('readonly','readonly');
		$('.datepicker').datepicker({/*maxDate: '+1w', minDate: '-1w',*/ dateFormat : 'dd/mm/yy', showButtonPanel: true/*, changeYear: true, yearRange: '-100:+0'*/});
    }	
	 if($('.ldatepicker').length > 0)
    {
        $('.ldatepicker').attr('autoComplete','off');
        $('.ldatepicker').attr('readonly','readonly');
		$('.ldatepicker').datepicker({/*maxDate: '+1w',*/ minDate: '0w', dateFormat : 'dd/mm/yy', showButtonPanel: true/*, changeYear: true, yearRange: '-100:+0'*/});
    }	
	
	
	// --------------------------------------Search----------------------------------------------- 
	
	var search_buy_opt = '<option value="1">Buy</option><option value="2">Rent</option><option value="3">Lease</option><option value="89">Joint Venture</option>';
	var search_prop_opt = '<option value="1">Sale</option><option value="2">Rent</option><option value="3">Lease</option><option value="89">Joint Venture</option>';
	
	if($('#header_search_for').length > 0)
	{
		$('.td_property_is').show();
		//alert($('#header_search_for').val());
				if($('#header_search_for').val() != 2 && $('#sproperty_type').val() != 89 )
				{
					$('#search_budget').show();
					$('#search_price_rent').hide();
				}
				else if($('#header_search_for').val() == 2 && $('#sproperty_type').val() != 89 )
				{
					$('#search_budget').hide();
					$('#search_price_rent').show();
				}
				$('#header_search_for').change(function() {
					if($('#header_search_for').val() == 2 && $('#sproperty_type').val() != 89 )
					{
						$('#search_budget').hide();
						$('#search_price_rent').show();
						$('#search_price_rent').val('');
						$('#search_budget').val('');
					}
					else if($('#header_search_for').val() != 2 && $('#sproperty_type').val() != 89 )
					{
						$('#search_budget').show();
						$('#search_price_rent').hide();
						$('#search_price_rent').val('');
						$('#price').val('');
					}
					else
					{
						$('#search_budget').hide();
						$('#search_price_rent').hide();
						$('#search_price_rent').val('');
						$('#price').val('');
					}
					if($(this).val() == 89)
					{
						$('#search_type').removeAttr('disabled');
						$('#search_type').html('');
						$('#search_type').html('<option value="891" >Site Owner</option><option value="892">Builder</option>');
						//$('#search_budget').attr('disabled','disabled');
						$('.sbud').hide();
						$('.td_property_is').hide();
						$('#search_budget').hide();
						$('#search_budget').val('');
						$('#search_bedroom').hide();
						$('#search_bedroom').val('');
						$('#Bedrooms_lable').hide();
						$('#Bedrooms_lable').val('');
						$('#search_price_rent').hide();
						$('#search_price_rent').val('');
					}
					else
					{
						if($('#sproperty_type').val() != '')
						{
								$.ajax({
								type:"POST",
								url:$('#baseUrl').val()+"/Ajaxutility/getpropertyis",
								data: "pid="+ $('#sproperty_type').val(),  
								success: function(options_res)
								{
									$('#search_type').html('<option value="">Select</option>'+options_res);
								}
								});
								$('#search_type').removeAttr('disabled');
								$('.sbud').show();
								if($('#header_search_for').val() == 2)
								{
									$('#search_price_rent').show();
									$('#search_budget').hide();
								}
								else
								{
									$('#search_price_rent').hide();
									$('#search_budget').show();
								}
						}
						else
						{
							$('#search_type').html('<option value="">Select</option>');
							$('#search_type').val('');
							$('#search_type').attr('disabled','disabled');
							$('#search_bedroom').hide();
							$('#search_bedroom').val();
							$('#Bedrooms_lable').hide();
							$('#search_type').attr('disabled','disabled');
							$('.sbud').show();
							$('#search_budget').show();
							if($('#header_search_for').val() == 2)
							{
								$('#search_price_rent').show();
								$('#search_budget').hide();
							}
							else
							{
								$('#search_price_rent').hide();
								$('#search_budget').show();
							}
							$('.td_property_is').show();
						}
					}
					
					/*if($('#header_search_for').val() != 1)
					{
						$('#sproperty_type').find('option[value=89]').remove();
					}
					else if($('#header_search_for').val() != 1 && $('#sproperty_type').val() != 89)
					{
						$('#sproperty_type').find('option[value=89]').remove().end().append('<option value="89" >Joint Venture</option>');
					}*/
				});
	}
	
	var spty = 1;
	$('#sproperty_lable').css("color", '#000');
	$('#sproperty_lable').hover(function() {
		$('#sproperty_lable').css("cursor", "pointer");
		});
	$('#sreq_lable').hover(function() {
		$('#sreq_lable').css("cursor", "pointer");
		});
	$('#sproperty_lable').click(function(e) {
				 $('#sproperty_lable').css("color", '#000');
				 $('#sreq_lable').css("color", '#44469E');
				 $('#sproperty_type').val('');
				$('#search_states').val('');
				if($('#search_type').html() != '')
				$('#search_type').val('');
				if($('#search_city').html() != '')
				$('#search_city').val('');
				if($('#search_states').val() != '')
				$('#search_city').removeAttr('disabled');
				else
				$('#search_city').attr('disabled','disabled');
				if($('#sproperty_type').val() != '')
				$('#search_type').removeAttr('disabled');
				else
				$('#search_type').attr('disabled','disabled');
				/*if($('#search_location').html() != '')
				$('#search_location').val('');
				$('#header_loc').hide();
				$('#search_location').hide();*/
				$('.td_property_is').show();
				//alert('123')
				$('#header_search_for').html(search_prop_opt);
				//$('#sproperty_type').find('option[value=89]').remove();
				//$('#sproperty_type').find('option[value=89]').remove().end().append('<option value="89" >Joint Venture</option>');
				spty = 1;
			 });
	$('#sreq_lable').click(function(e) {
			$('#sproperty_lable').css("color", '#44469E');
			$('#sreq_lable').css("color", '#000');
			//$('#sproperty_type').find('option[value=89]').remove();
			$('#search_states').val('');
			//$('#search_budget').hide();
			$('#search_budget').val('');
			$('#search_bedroom').hide();
			$('#search_bedroom').val('');
			$('#Bedrooms_lable').hide();
			$('#Bedrooms_lable').val('');
			//$('.sbud').hide();
			$('#sproperty_type').val('');
			$('#search_states').val('');
			if($('#search_type').html() != '' && ( $('#search_type').val() != 891 && $('#search_type').val() != 892))
			{
				$('#search_type').val('');
			}
			else
			{
				$('#search_type').html("<option>Select</option>");
			}
			if($('#search_city').html() != '')
			$('#search_city').val('');
			if($('#search_states').val() != '')
			$('#search_city').removeAttr('disabled');
			else
			$('#search_city').attr('disabled','disabled');
			if($('#sproperty_type').val() != '')
			$('#search_type').removeAttr('disabled');
			else
			$('#search_type').attr('disabled','disabled');
			$('#header_search_for').html(search_buy_opt);
			
			$('#header_loc').show();
			$('#search_location').show();
			
			if($('#search_city').val() != '')
			$('#search_location').removeAttr('disabled');
			else
			$('#search_location').attr('disabled','disabled');
			
			$('.td_property_is').show();
			
		   spty = 2;
   });
	//alert($('#sproperty_type').children().remove().end().append('<option>JJJJJJJJJJ</option>'));
	//alert($('#sproperty_type').find('option:contains(89)').remove());
	if($('#header_search_for').length > 0)
	{
		if(spty == 1)
		{
			$('#header_search_for').html(search_prop_opt);
		}
	}
	
	if($('#search_city').length > 0)
	{
		/*if(spty == 1)
		{
			//if($('#search_location').html() != '')
			//$('#search_location').val('');
			//$('#header_loc').hide();
			//$('#search_location').hide();
		}*/
		$('#search_location').attr('disabled','disabled');
		$('#search_city').change(function(){
			//alert($('#city_dd').val());
			if($(this).val != '')
			{
				//$('#loading_location').html('<img src="'+$('#baseUrl').val()+'/public/images/loader.gif" align="absmiddle">');
				$.ajax({
					type:"POST",
					url:$('#baseUrl').val()+"/Ajaxutility/getlocation",
					data: "cid="+ $(this).val(),  
					success: function(options_res)
					{
						if($('#search_location').length > 0){
							$('#search_location').html('<option value="">Select Location</option>'+options_res);
						}
						$('#search_location').removeAttr('disabled');
					}
				});//$('#loading_location').html('');
			}
			else
			{
				$('#search_location').attr('disabled','disabled');
			}
		});
		
	}
	
	
	if($('#search_states').length > 0)
	{
		if($('#search_states').val() != '')
		$('#search_city').removeAttr('disabled');
		else
		$('#search_city').attr('disabled','disabled');
		
		$('#search_states').change(function(){
								//alert($(this).val());
			if($(this).val() != '')
			{
				$.ajax({
					type:"POST",
					url:$('#baseUrl').val()+"/Ajaxutility/getcity",
					data: "sid="+ $(this).val(),  
					success: function(options_res)
					{
						if(options_res != '<option value="">Select City</option>')
						{
							if($('#search_city').length > 0){
								$('#search_city').html(options_res);
							}
						}
					}
				});
				$('#search_city').removeAttr('disabled');	
			}
			else
			{
				$('#search_city').val('');
				$('#search_city').attr('disabled','disabled');
			}
		});	
	}
	
	//alert($('#submit_search').length);
	//alert($('#sproperty_type').length);
	if($('#sproperty_type').length > 0)
	{
		if($('#sproperty_type').val() == '')
		{
			$('#search_bedroom').hide();
			$('#Bedrooms_lable').hide();
			$('#search_type').attr('disabled','disabled');
		}
		$('#sproperty_type').change(function(){
											 
				//alert($(this).val());							 
				if($(this).val() == 2)
				{
					$('#search_bedroom').hide();
					$('#search_bedroom').val('');
					$('#Bedrooms_lable').hide();
					$('#Bedrooms_lable').val('');
				}
					if($(this).val() != '' && $('#header_search_for').val() != 89)
					{
						$.ajax({
						type:"POST",
						url:$('#baseUrl').val()+"/Ajaxutility/getpropertyis",
						data: "pid="+ $(this).val(),  
						success: function(options_res)
						{
							$('#search_type').html('<option value="">Select</option>'+options_res);
						}
						});
						$('#search_type').removeAttr('disabled');
						$('.sbud').show();
						if($('#header_search_for').val() == 2)
						{
							$('#search_price_rent').show();
							$('#search_budget').hide();
						}
						else
						{
							$('#search_price_rent').hide();
							$('#search_budget').show();
						}
					}
					else if($('#header_search_for').val() == 89)
					{
						$('#search_type').removeAttr('disabled');
						$('#search_type').html('');
						$('#search_type').html('<option value="891" >Site Owner</option><option value="892">Builder</option>');
						//$('#search_budget').attr('disabled','disabled');
						$('.sbud').hide();
						$('#search_budget').hide();
						$('#search_budget').val('');
						$('#search_bedroom').hide();
						$('#search_bedroom').val('');
						$('#Bedrooms_lable').hide();
						$('#Bedrooms_lable').val('');
						$('#search_price_rent').hide();
						$('#search_price_rent').val('');
					}
					else
					{
						$('#search_type').val('');
						$('#search_type').attr('disabled','disabled');
						$('#search_bedroom').hide();
						$('#search_bedroom').val();
						$('#Bedrooms_lable').hide();
						$('#search_type').attr('disabled','disabled');
						$('.sbud').show();
						$('#search_budget').show();
						if($('#header_search_for').val() == 2)
						{
							$('#search_price_rent').show();
							$('#search_budget').hide();
						}
						else
						{
							$('#search_price_rent').hide();
							$('#search_budget').show();
						}
					}
			});
		
		
	}
	if($('#search_type').val() >= 4 || $('#search_type').val() == '')
	{
		$('#search_bedroom').hide();
		$('#search_bedroom').val('');
		$('#Bedrooms_lable').hide();
		$('#Bedrooms_lable').val('');
	}
	else
	{
		$('#search_bedroom').show();
		$('#Bedrooms_lable').show();
	}
	
	if($('#search_type').length > 0)
	{
		$('#search_type').change(function(){
			if($('#search_type').val() >= 4 || $('#search_type').val() == '')
			{
				//alert(spty);
				/*if(spty != 2)
				{*/
					$('#search_bedroom').hide();
					$('#search_bedroom').val('');
					$('#Bedrooms_lable').hide();
					$('#Bedrooms_lable').val('');
				//}
			}
			else
			{
				///alert(spty);
				/*if(spty != 2)
				{*/
					$('#search_bedroom').show();
					$('#Bedrooms_lable').show();
				//}
			}
		});
	}
	
	
	if($('#submit_search').length > 0)
	{
		$('#submit_search').click(function(){
					///alert(spty);
					//alert($('#header_search_for').val());
				if($('#sproperty_type').val() == '')
				  {
					  alert('Please select Property type');
					  return;
				  }
				if(spty == 1 && $('#header_search_for').val() == 89)
				{
					$('#search_type').val('891');
				}
				if(spty == 2 && $('#header_search_for').val() == 89)
				{
					$('#search_type').val('892');
					spty = 1;
				}
				
					$.ajax({
					type:"POST",
					url:$('#baseUrl').val()+"/Ajaxutility/search",
					data: "pty="+ $('#sproperty_type').val()+'&pis='+$('#search_type').val()+'&state='+$('#search_states').val()+'&bedroom='+$('#search_bedroom').val()+'&city='+$('#search_city').val()+'&budget='+$('#search_budget').val()+'&location='+$('#search_location').val()+'&search_for='+$('#header_search_for').val()+'&search_price='+$('#search_price_rent').val(),  
					success: function(res)
					{	
						if(spty == 1)
						window.location = $('#baseUrl').val()+'/search/buyer/sid/'+ res;
						if(spty == 2)
						window.location = $('#baseUrl').val()+'/search/seller/stid/'+ res;
					}
				}); 		   
			});
	}
		// --------------------------------------End Search----------------------------------------------- 
		
		
		
		//----------------------------------------- login ------------------------------------------------//
			
			//alert($('#loginsubmit').length);
			if($('#loginsubmit').length > 0 )
			{
				$('#loginusername').blur(function(){
													  if($('#loginusername').val() == "")
													  {
													  	 $('#loginusername').css("border-color","red")
													  }
													  else
													  {
														   $('#loginusername').css("border-color","#999")
													  }
												
													   });
				$('#loginpassword').blur(function(){
													  if($('#loginpassword').val() == "")
													  {
													  	 $('#loginpassword').css("border-color","red")
													  }
													  else
													  {
														   $('#loginpassword').css("border-color","#999")
													  }
												
													   });
				$('#loginsecurity_code').blur(function(){
													  if($('#loginsecurity_code').val() == "")
													  {
													  	 $('#loginsecurity_code').css("border-color","red")
													  }
													  else
													  {
														   $('#loginsecurity_code').css("border-color","#999")
													  }
												
													   });
				$('#loginsubmit').click(function() {
									
									//alert('123');
									if($('#loginusername').val() == '')
									{
										//alert ('lo name');
										$('#loginusername').css("border-color","red")
										//return;
									}
									if($('#loginpassword').val() == '')
									{
										//alert ('lo name');
										$('#loginpassword').css("border-color","red")
										//return;
									}
									if($('#loginsecurity_code').val() == '')
									{
										$('#loginsecurity_code').css("border-color","red")
									}
									
									if($('#loginsecurity_code').val() != '' && $('#loginusername').val() != '' && $('#loginpassword').val() != '' )
									{
										$('#user_login_form').submit();
									}
									
									});
			}
			
		//----------------------------------------- End login ------------------------------------------------//
		
		
		
	// -------------------------------------- EMI ----------------------------------------------- 
	
	//alert($('#calc').length );
	$('#calc').click(function(){
							  
							//$('emi-from')  
							  
							  $('#emi-from').validate({
													  errorContainer : '#error_container' ,
													  errorLabelContainer: '#error_container ul',wrapper: 'li',
												   rules: {
													  pamount: {required:true,number:true}, 
													  intrate: {required:true,number:true}, 
													  tenure: {required:true,number:true}
												   },
												   messages: {
													   pamount : {
														   required : 'Loan Amt is required.' , 
														   number : 'Please enter a valid number for Loan Amt. ' },
														  intrate : {
														   required : 'Interest Rate is required.' , 
														   number : 'Please enter a valid number for Interest Rate. ' },
														    tenure : {
														   required : 'Loan Tenure is required.' , 
														   number : 'Please enter a valid number for Loan Tenure. ' }
												   }
												   });
							 	if($('#emi-from').valid())
								{  
								 /* alert($(this).val());
								  alert('pamount :' + $('#pamount').val());
								  alert('intrate : ' + $('#intrate').val());
								  alert('tenure : ' + $('#tenure').val());*/
								  
								  var tmonths = $('#tenure').val()*12;
								  var i =  $('#intrate').val();
									if (i > 1.0) {
										i = i / 100;
									}
									i /= 12;
									var pow = 1;
									for (var j = 0; j < tmonths; j++)
										pow = pow * (1 + i);
										money = "" + .01* Math.round(100*($('#pamount').val() * pow * i) / (pow - 1));
										dec = money.indexOf(".");
										if(dec!=-1){
											dollars = money.substring(0,dec); 
											cents = money.substring(dec+1,dec+3);
											cents = (cents.length < 2) ? cents + "0" : cents;
											money = dollars + "." + cents;
										}
										else{
									}
									//alert(money);
									$('#emiamt').val(money);
									//document.emi_frm.emiamt.value = money;
								  
								  
								} 
					 });
	// Reference ID Search
	if($('#refbut').length > 0)
	{
		$('#refbut').click(function(){
			if($('#refid').val() == '')
			{
				alert('Enter the Property Reference id');
			}
			else
			{
				//alert($('#refid').val());
				window.location = $('#baseUrl').val()+'/ad/'+$('#refid').val();
			}
		});
	}
	
	
});
