 function validateEmail(email) { 
		var reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
		return reg.test(email);
	}

 
 $(document).ready(function() {

    
$('a[data-confirm]').click(function(ev) {
		var href = $(this).attr('href');
		if (!$('#dataConfirmModal').length) {
			$('body').append(
                           '<div id="dataConfirmModal" class="modal fade" role="dialog" aria-labelledby="dataConfirmLabel" aria-hidden="true">\n\
                              <div class="modal-dialog">\n\
                                <div class="modal-content col-md-9">\n\
                                    <div class="modal-header">\n\
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\n\
                                    <h3 id="dataConfirmLabel">Please Confirm</h3>\n\
                                </div>\n\
                                <div class="modal-body">\n\
                                </div>\n\
                                <div class="modal-footer">\n\
                                    <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">Cancel</button>\n\
                                    <a class="btn btn-primary" id="dataConfirmOK">OK</a>\n\
                                </div>\n\
                              </div>\n\
                             </div>\n\
                            </div>');
		} 
		$('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));
		$('#dataConfirmOK').attr('href', href);
		$('#dataConfirmModal').modal({show:true});
		return false;
	});
  
     
        $('a[data-id]').click(function() {
            
        var idz = $(this).data('id');
        var val = $(this).data('va');
        
		if (!$('#idz-' + idz).length) {
			$('body').append(
                           '<div id="idz-'+idz+'" class="modal fade" role="dialog" aria-labelledby="dataMessageLabel" aria-hidden="true">\n\
                              <div class="modal-dialog">\n\
                                <div id="cStyl" class="modal-content col-md-9">\n\
                                  <form id="contact" name="contact" action="#" method="post">\n\
                                    <div class="modal-header">\n\
                                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\n\
                                    <h3 id="dataMessageLabel">Contact Member</h3>\n\
                                </div>\n\
                                <div id="bStyl" class="modal-body form-horizontal">\n\
                                    <div class="form-group">\n\
                                    <label for="emailm" class="col-sm-3 control-label">Email:</label>\n\
                                        <div class="col-md-9">\n\
                                        <input type="email" class="form-control" id="emailm" name="emailm" autocomplete="off" placeholder="Email"><span class="error"></span>\n\
                                        </div>\n\
                                    </div>\n\
                                    <input type="hidden" name="hiddenfield" id="hiddenfield">\n\
                                <div class="form-group">\n\
                                    <label for="messageboxm" class="col-sm-3 control-label">Message:</label>\n\
                                        <div class="col-md-9">\n\
                                        <textarea type="text" class="form-control" rows="7" id="messageboxm" name="messageboxm" placeholder="Message"></textarea><span class="error"></span>\n\
                                        </div>\n\
                                    </div>\n\
                                </div>\n\
                                <div id="fStyl" class="modal-footer">\n\
                                    <div id="response"></div>\n\
                                    <button class="btn btn-default" id="close" aria-hidden="true">Cancel</button>\n\
                                    <button type="submit" class="btn btn-primary" id="send">Send</button>\n\
                                </div>\n\
                               </form>\n\
                              </div>\n\
                             </div>\n\
                            </div>');
		} 
                //var myBookId = "<?php echo $list->email; ?>";
                //$(".modal-body #emailm").val(val);
                $("#hiddenfield").val(val);
                
		$('#idz-'+idz).modal({show:true});
                
                $("#close").click(function(event){
                    event.preventDefault();
                    $('#idz-'+idz).modal('hide');
                    $('#idz-'+idz).remove();
                    $('.modal-backdrop').remove();
                    $('body').removeClass("modal-open");
                    
                });
               
                $( '#idz-' + idz ).unbind("submit").submit(function(e) {
               
                    e.preventDefault();
                    var emailval  = $("#emailm").val(); 
                    var msgval    = $("#messageboxm").val();
                    var msglen    = msgval.length;
                    var mailvalid = validateEmail(emailval);
                    
                    
                    if(mailvalid == false) {
                    //$("#emailm").addClass("error");
                    $("#emailm").css('border-color','#d9534f');
                    $("#emailm").next(".error").fadeIn('fast').text('Enter a valid email');
                    }
        
                    else if(mailvalid == true){
                    //$("#emailm").removeClass("error");
                    $("#emailm").css('border-color','#5cb85c');
                    $("#emailm").next('.error').fadeOut();
                    }
        
                    if(msglen < 4) {
                    //$("#messageboxm").addClass("error");
                    $("#messageboxm").css('border-color','#d9534f');
                    $("#messageboxm").next(".error").fadeIn('fast').text('Enter a message with minimum size of 4 characters');
                    }
        
                    else if(msglen >= 4){
                    //$("#messageboxm").removeClass("error");
                    $("#messageboxm").css('border-color','#5cb85c');
                    $("#messageboxm").next('.error').fadeOut();
                    
                    }

                                        if(mailvalid == true && msglen >= 4) {                 
                                $.ajax({
                                    
                                    type: "POST",
					url: CI_ROOT + "epc_listings/email/sendMessage", 
					data: $("#contact").serialize(),
					success: function(msg){                                              
						$("#response").html(msg);
                                                                                      
					},
					error: function(){
						alert("failure");
					}
                                    });
                                    
                                    setTimeout(function() {                                    
                                    $('#idz-'+idz).modal('hide');                                
                                    $('#idz-'+idz).remove();
                                    $('.modal-backdrop').remove();
                                    $('body').removeClass("modal-open");
                                    }, 1900);
                }
});       
		return false;               
	});
        
        
    // hide #back-top first
    $("#back-top").hide();
    
    // fade in #back-top
    $(function () {
        $(window).scroll(function () {
            if ($(this).scrollTop() > 100) {
                $('#back-top').fadeIn();
            } else {
                $('#back-top').fadeOut();
            }
        });

        // scroll body to 0px on click
        $('#back-top a').click(function () {
            $('body,html').animate({
                scrollTop: 0
            }, 500);
            return false;
        });
        
        
        $(window).load(function() {
            $('#atime').text(moment().format("ddd, MM-DD-YYYY"));
        });
        
    });
    
    
        $('.datepicker').datepicker();
    
    
        
 });  

