$(document).ready(function(){
    //change CAPTCHA on each click or on refreshing page
    $("#reload").click(function() {
        $("#img").remove();
		var id = Math.random();
        $('<img id="img" src="captcha/captcha.php?id='+id+'"/>').appendTo("#imgdiv");
		 id ='';
    });
    $("#reload1").click(function(){
        $("#img").remove();
        var id = Math.random();
        $('<img id="img" src="../captcha/captcha_admin.php?id='+id+'"/>').appendTo("#imgdiv");
         id ='';
    });
    //validation function
    $('#contact').click(function(event) {
        event.preventDefault();
        var subject = $("#subject").val();
        var fname = $("#fname").val();
        var Organisation = $("#Organisation").val();
        var email = $("#email").val();
        var captcha = $("#captcha").val();
        var phone = $("#phone").val();
        var filename = $('#input_5').val();
        var message = $("#message").val();
        var pattrnname = /^[a-zA-z\s]{3,100}$/;
        var pattrnemail = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
        var patternmobile = /^[\d]{10}$/;
        if (fname=='')
        {
            alert("Name Field is Required...");
            return false;
        }
        if(!fname.match(pattrnname))
        {
            alert("For your name please use alphabets only...");
            return false;
        }
         if (Organisation=='')
        {
            alert("Organisation Field is Required...");
            return false;
        }
        
        if(email=='')
        {
            alert("Email field is Required...");
            return false;
        }
        if(!email.match(pattrnemail)) 
        {
            alert("Please enter a valid email address...");
            return false;
        }
        if(!phone.match(patternmobile))
        {
            alert("Mobile Number is Require Enter only 10 digit number...");
            return false;
        }
        else
        {
            $.ajax({
                method: "POST",
                url: "captcha/verify.php",
                data: {contact:1,captcha:captcha,subject:subject,name:name,fname:fname,filename:filename,email:email,phone:phone,message:message,Organisation:Organisation},
                success: function(html) {
                    if(html == "Mail Not Send!!!")
                    {
                        alert(html);
                        document.getElementById("myForm").reset();
                        $("#img").remove();
                        var id = Math.random();
                        $('<img id="img" src="captcha/captcha.php?id='+id+'"/>').appendTo("#imgdiv");
                         id ='';
                    }
                    else if (html=="Enter invalid capcha try again") {
                        alert(html);
                       document.getElementById("myForm").reset();
                        $("#img").remove();
                        var id = Math.random();
                        $('<img id="img" src="captcha/captcha.php?id='+id+'"/>').appendTo("#imgdiv");
                        id ='';
                    }
                    else
                    {
                        alert(html);
                        document.getElementById("myForm").reset();
                        $("#img").remove();
                        var id = Math.random();
                        $('<img id="img" src="captcha/captcha.php?id='+id+'"/>').appendTo("#imgdiv");
                        id ='';
                    }
                    
                }
            });
            }
    });

    $("#Readto").click(function(event){
        event.preventDefault();
        var subject = $("#subject").val();
        var size = $("#size").val();
        var budget = $("#budget").val();
        var location = $("#location").val();
        var name = $("#name").val();
        var email = $("#email").val();
        var phone = $("#phone").val();
        var captcha = $("#captcha").val();
        var pattrnname = /^[a-zA-z\s]{3,100}$/;
        var pattrnemail = /^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/;
        var patternmobile = /^[\d]{10}$/;
        if(name==''){
            alert("Name Field is Required...");
            return false;
        }
        if(!name.match(pattrnname)){
            alert("For your name please use alphabets only...");
            return false;
        }
        if(email==''){
            alert("Email field is Required...");
            return false;
        }
        if(!email.match(pattrnemail)){
            alert("Please enter a valid email address...");
            return false;
        }
        if(!phone.match(patternmobile)){
            alert("Mobile Number is Require Enter only 10 digit number...");
            return false;
        }
        else{
            $.ajax({
                method: "POST",
                url: "captcha/verify.php",
                data: {readto:1,captcha:captcha,subject:subject,name:name,email:email,phone:phone,size:size,budget:budget,location:location},
                success: function(html) {
                    if(html == "Mail Not Send!!!")
                    {
                        alert(html);
                        document.getElementById("myForm").reset();
                        $("#img").remove();
                        var id = Math.random();
                        $('<img id="img" src="captcha/captcha.php?id='+id+'"/>').appendTo("#imgdiv");
                         id ='';
                    }
                    else if (html=="Enter invalid capcha try again") {
                        alert(html);
                        document.getElementById("myForm").reset();
                    }
                    else
                    {
                        alert(html);
                        document.getElementById("myForm").reset();
                        $("#img").remove();
                        var id = Math.random();
                        $('<img id="img" src="captcha/captcha.php?id='+id+'"/>').appendTo("#imgdiv");
                        id ='';
                    }
                    
                }
            });
        }

    });
    // Panel of the Admin panel user id password
    $("#button_admin").click(function(event) {
            event.preventDefault();
            var uname = $("#username").val();
            var pass = $("#password").val();
            var cap_admin = $("#captcha1").val();
            $.ajax({
                method: "POST",
                url: "../captcha/verify.php",
                data: {cap_admin_click:1,cap_admin:cap_admin,uname,uname,pass:pass},
                success: function(resp) {
                    if(resp=="ldsdlgfsdo")
                    {
                        window.location.href="dashboard.php";
                    }
                    else
                    {
                        $("#resp_alert").html(resp);
                        $( "#resp_alert" ).dialog({
                          modal: true,
                          buttons: {
                            Ok: function() {
                              $( this ).dialog( "close" );
                            }
                          }
                        });
                    }
                }
            });
        });
})