$('.dropdown-toggle').dropdown();
	$('#searchlist').click(function(){
		var action=$('#home-search').attr('action');
		action=action+'/';
		if($('#search').val().length ==0 ) var search='all'; else var search=$('#search').val();
		action=action+''+search;
		window.location.href=action;
		
	});
        
        $(document).ready(function() {
           if($('a[href="'+document.location+'"]')){
               var $this=$('a[href="'+document.location+'"]');
               $('#'+$this.attr('alt')).addClass('active');
           }
	//*************************** Login function and validation ********************************************//
	$('.sign-in').live('click',function(){
		$.fn.colorbox( {
			href: base_url+"signin",
			title: "Sign in to your  Account",
			width: "600px",
			height: "410px;",
			onComplete: function() {
				//$('#colorbox').show();
				$("#loginform").validate( {
                // Jquery Validation
                rules: {
                    // Rules for validation
                    
                    
                    email: {
                        required: true,
                        email: true
                    },
                    password: "required"
                },
                messages: {
                    // messages for valided fields
                    
                    email: {
                        required: "Enter your email.",
                        email: "Enter a valid email."
                    },
                    password: "Enter your password."
                },
                submitHandler: function(form)
                {
                    $.ajax( {
                        type: "POST",
                        url: base_url+"signin/validate",
                        data:'email=' + $('#email').val() + '&password=' + $('#password').val(),
                        dataType: "json",
                        success: function(response)
                        {
                           if(response.success===false){ 
			   	$('#err_msg').html(response.message).show();
			   }else{
				window.location.href=base_url+'account';
			   }
                        }
                    });
                }
            });
				$('#cancel').click(function(){$.fn.colorbox.close();});
			}
		});
	});
        
        //*************************** Reply to enquiry function and validation ********************************************//
	$('.viewEnquiry').live('click',function(){
            var enqid=$(this).attr('id');
		$.fn.colorbox( {
			href: base_url+"account/replyEnquiry/"+enqid,
			title: "View / Reply to Inquiry",
			width: "600px",
			height: "430px;",
			onComplete: function() {
				//$('#colorbox').show();
		$("#replyenq").validate( {
                // Jquery Validation
                rules: {
                    // Rules for validation
                    
                    
                    comment: {
                        required: true,
                        minlength: 50
                    }
                },
                messages: {
                    // messages for valided fields
                    
                    comment: {
                        required: "Please enter your response.",
                        minlength: "Your response should have atleast 50 chars."
                    }
                },
                submitHandler: function(form)
                {
                    $.ajax( {
                        type: "POST",
                        url: base_url+"account/replyEnquiry/"+enqid,
                        data:'response=' + $('#comment').val() + '&enqid=' + $('#enqid').val(),
                        dataType: "json",
                        success: function(response)
                        {
                           if(response.success===false){ 
			   	$('#err_msg').html(response.message).show();
			   }else{
				window.location.href=base_url+'account/enquiries';
			   }
                        }
                    });
                }
            });
				//$('#cancel').click(function(){$.fn.colorbox.close();});
			}
		});
                return false;
	});
	//*************************** Forgot function and validation ********************************************//
	$('#forgot-password').live('click',function(){
		$.fn.colorbox( {
			href: base_url+"forgotpass",
			title: "Forgot Password",
			width: "600px",
			height: "410px;",
			onComplete: function() {
				//$('#colorbox').show();
				$("#forgotpass").validate( {
                // Jquery Validation
                rules: {
                    // Rules for validation
                    
                    
                    email: {
                        required: true,
                        email: true
                    }
                },
                messages: {
                    // messages for valided fields
                    
                    email: {
                        required: "Enter your email.",
                        email: "Enter a valid email."
                    }
                },
                submitHandler: function(form)
                {
                    $.ajax( {
                        type: "POST",
                        url: base_url+"account/sendpassword",
                        data:'email=' + $('#email').val(),
                        dataType: "json",
                        success: function(response)
                        {
                           if(response.success===false){ 
			   	$('#err_msg').html(response.message).show();
			   }else{
				$.fn.colorbox({html:"<div style='padding:50px;text-align:center;'><strong>New password has been sent your email address.</strong></div>",height:'200px',title: "Forgot Password"});
			   }
                        }
                    });
                }
            });
				$('#cancel').click(function(){$.fn.colorbox.close();});
			}
		});
                return false;
	});
        //*************************** Change Password function and validation ********************************************//
	$('#change-password').live('click',function(){
		$.fn.colorbox( {
			href: base_url+"account/changepassword",
			title: "Change Password",
			width: "600px",
			height: "410px;",
			onComplete: function() {
				//$('#colorbox').show();
				$("#changepassword").validate( {
                // Jquery Validation
                rules: {
                    // Rules for validation
                    currentpassword: {
                        required: true,
                        minlength: 6
                    },
                   newpassword: {
                        required: true,
                        minlength: 6
                    },
                    cnewpassword: {
                        required: true, 
                        minlength: 6,
                        equalTo: "#newpassword"
                    }
                },
                messages: {
                    // messages for valided fields
                    currentpassword: {
                        required: "Enter your current password.",
                        minlength: "Enter min 6 characters."
                    },
                   newpassword: {
                        required: "Enter new password.",
                        minlength: "Enter min 6 characters."
                    },
                    cnewpassword: {
                        required: "Enter confirm password.",
                        minlength: "Enter min 6 characters.",
                        equalTo: "The two passwords didn't match."
                    }
                },
                submitHandler: function(form)
                {
                    $.ajax( {
                        type: "POST",
                        url: base_url+"account/changepassword",
                        data:'currentpassword='+$('#currentpassword').val()+'&newpassword=' + $('#newpassword').val(),
                        dataType: "json",
                        success: function(response)
                        {
                           if(response.success===false){ 
			   	$('#err_msg').html(response.message).show();
			   }else{
				$.fn.colorbox({html:"<div style='padding:50px;text-align:center;'><strong>Your password has been changed successfully.</strong></div>",height:'200px',title: "Forgot Password"});
			   }
                        }
                    });
                }
            });
				$('#cancel').click(function(){$.fn.colorbox.close();});
			}
		});
                return false;
	});
//*************************** Join now function and validation ********************************************//
	$('.join-now').live('click',function(){
		$.fn.colorbox( {
			href: base_url+"join",
			title: "Join Dewas.co.in",
			width: "600px",
			height: "510px;",
			onComplete: function() {
				//$('#colorbox').show();
				$("#signupform").validate({   		        // Jquery Validation 
                rules: {					// Rules for validation
                    name: "required",
                    email: {
                        required: true,
                        email: true
                    },				
                    password: {
                        required: true,
                        minlength: 6
                    },
                    cpassword: {
                        required: true, 
                        minlength: 6,
                        equalTo: "#password"
                    }
                    
                    
                },
                messages: {
                    name: "Enter your name.",
                    email: "Enter a valid email.",
                    password: {
                        required: "Enter password.",
                        minlength: "Enter min 6 characters."
                    },
                    cpassword: {
                        required: "Enter confirm password.",
                        minlength: "Enter min 6 characters.",
                        equalTo: "The two passwords didn't match."
                    }
                },
                submitHandler: function(form)
                {
                    $.ajax( {
                        type: "POST",
                        url: base_url+"join/addnew",
                        data:'name='+$('#name').val()+'&email=' + $('#email').val() + '&password=' + $('#password').val(),
                        dataType: "json",
                        success: function(response)
                        {
                           if(response.success===false){ 
			   	$('#err_msg').html(response.message).show();
			   }else{
				$.fn.colorbox({html:"<div style='padding:50px;text-align:center;'><strong>Your account has been created.<br>A link has been sent to your email adress to verify your account.<br>Please verify your account and continue using dewas.co.in</strong></div>",height:'200px',title: "Join Dewas.co.in"});
				//$.fn.colorbox.resize();
			   }
                        }
                    });
                }	
				
            });
				$('#cancel').click(function(){$.fn.colorbox.close();});
			}
		});
	});
//*************************** Post ad validation ****************************************************************//
var captchavalue='';
	$.ajax( {
        	type: "GET",
                url: base_url+"captcha/index",
                dataType: "json",
                success: function(response)
               	{
			if(response.success==="true"){ 
			   	captchavalue=response.val;
			}
                }
        });
$.validator.addMethod("validatecaptcha", function(value, element, params) {
	
	
	if(value != '' && value==captchavalue){
		return true;	 
	}else return false;
//return this.optional(element) || value == params[0] + params[1];
},"Please enter code as displayed in image.");

		$("#postadform").validate( {
                // Jquery Validation
                rules: {
                    // Rules for validation
                    
                    category:{
			required: true
		    },
                    title: {
                        required: true
                    },
		    
                    description:{ 
			required:true
			//minlength:100
		   },
		   price:{ 
			number: true
		   },
		   name:{
			required:true
			},
                   email:{
                    required:true,
		    email:true
                    },
                    contact:{
                        number:true
                    },
		    captcha:{
			validatecaptcha:true
		    }
                },
                messages: {
                    // messages for valided fields
                    category:"Please select a category.",
                    title: {
                        required: "Enter ad title."
                    },
                    description: {
			required:"Enter ad description."
			//minlength:"Ad description should have atleast 100 characters."
		    },
		    price:{
			number:"Please a valid decimal number only."
		    },
		    name:"Please enter your name",
		    email:{
			required:"Please enter a valid email.",
			email:"Please enter a valid email."
		   },
                   contact:{
                       number: "Please enter only numbers."
                   }
                }
            });
$("#adtype").change(function(event) {
    var adtype=$("#adtype").val();
    //alert(adtype);
    if(adtype==2){ $('#pricefield').hide();}
    if(adtype==1){ $('#pricefield').show();}
});
//*************************** Response to ad function and validation ********************************************//
	$('#replyad').live('click',function(){
		var listid=$('#listid').val();
                
		$.fn.colorbox( {
			href: base_url+"reply",
			title: "Reply to ad",
			width: "600px",
			height: "480px;",
			onComplete: function() {
			
		$("#contactform").validate( {
                // Jquery Validation
                rules: {
                    // Rules for validation
                    
                    cname:{
			required: true
		    },
                    cemail: {
                        required: true,
                        email: true
                    },
		    
                    comment:{ 
			required:true,
			minlength:100
		   } 
                },
                messages: {
                    // messages for valided fields
                    cname:"Enter your name",
                    cemail: {
                        required: "Enter your email.",
                        email: "Enter a valid email."
                    },
                    comment: {
			required:"Enter your comment.",
			minlength:"Your message should have atleast 100 characters"
		    }
                },
                submitHandler: function(form)
                {
                    $.ajax( {
                        type: "POST",
                        url: base_url+"postad/response",
                        data:'cname='+$('#cname').val()+'&cemail=' + $('#cemail').val() + '&comment=' + $('#comment').val()+ '&contact=' + $('#contact').val()+'&listid='+listid+'&cid='+$('#cid').val(),
                        dataType: "json",
                        success: function(response)
                        {
                           if(response.success===false){ 
			   	$('#err_msg').html(response.message).show();
			   }else{
				window.location.reload();
			   }
                        }
                    });
                }
            });
				$('#cancel').click(function(){$.fn.colorbox.close();});
			}
		});
	});
//*************************** Logout and validation ********************************************//
	$('#logout').live('click',function(){
		return confirm("Are you sure to logout?");	
	});
        //*************************** Home Search validation ****************************************************************//

	$("#home-search").validate( {
                // Jquery Validation
                rules: {
                    // Rules for validation
                    
                    seacrh:{
			required: true
		    } 
                },
                messages: {
                    // messages for valided fields
                    search:"Please select a search term."
                }
            });
		$(".alladimages").colorbox({rel:'alladimages',transition:"fade"});
	});

function deleteItem(itemId){
    if(confirm('Are you sure to delete this ad?')){
        window.location.href=base_url+"account/deletead/"+itemId;
    }
} 
function deleteEnquiry(enqId){
    if(confirm('Are you sure to delete this inquiry?')){
        window.location.href=base_url+"account/deleteenq/"+enqId;
    }
}
function deleteReply(id){
    if(confirm('Are you sure to delete?')){
        window.location.href=base_url+"account/deletereply/"+id;
    }
}
!function ($) {

  $(function(){

    // fix sub nav on scroll
    var $win = $(window)
      , $nav = $('.subnav')
	  , navHeight = $('.navbar').first().height()
      , navTop = $('.subnav').length && $('.subnav').offset().top - navHeight
      , isFixed = 0;

    processScroll();

    $win.on('scroll', processScroll);

    function processScroll() {
      var i, scrollTop = $win.scrollTop();
      if (scrollTop >= navTop && !isFixed) {
        isFixed = 1;
        $nav.addClass('subnav-fixed');
      } else if (scrollTop <= navTop && isFixed) {
        isFixed = 0;
        $nav.removeClass('subnav-fixed');
      }
    }

  });

}(window.jQuery);
