﻿var intPageNo = 1,
intLastPage = 0;

//check numeric value
function isNumeric(value){ 
  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
       return true;
   } else { 
      return false;
   } 
}

//change pages when data is hidden on screen
function pagePanelChange(pageNo, containerID) {
    var container = document.getElementById('');

    if (!isNumeric(pageNo)) {        
        if (pageNo == 'Next page >') {
            intPageNo = parseInt(intPageNo) + 1;
        }
        else if (pageNo == '< Previous page') {            
            intPageNo = parseInt(intPageNo) - 1;
        }        
    }    
    else{
        intPageNo = parseInt(pageNo);
    }

    //get panel based on page number
    var panel = document.getElementById(containerID + "_" + intPageNo);
    if (panel != null) {
        //hide all panels then show the correct one
        $('#' + containerID + ' .main_results .page_holder').css('display', 'none');    
        panel.style.display = 'block';
        return true;
    }
}


$(document).ready(function () {

    //validate contact us form
    $("#contact-us-form-submit").parents('form').validate({

        //set the validation rules
        rules: {
            firstname: "required",
            lastname: "required",
            email: {
                required: true,
                email: true
            },
            registration: {
                required: true,
                maxlength: 8
            },
            description: "required",
            price: {
                required: true,
                number: true
            },
            milage: {
                required: true,
                digits: true
            },
            phonenumber: {
                required: true,
                digits: true,
                minlength: 11,
                maxlength: 11
            },
            contactphonenumber: {
                required: true,
                digits: true,
                minlength: 11,
                maxlength: 11
            },
            yourlocation: {
                required: true
            }
        },
        //set the error messages that are displayed
        messages: {
            firstname: "Please enter your first name",
            lastname: "Please enter your last name",
            email: "Please enter a valid email address",
            registration: {
                required: "Please enter your registration",
                maxlength: "Max 8 Characters"
            },
            price: {
                required: "Please enter a valid price",
                number: "Please enter a valid price"
            },
            milage: {
                required: "Please enter valid mileage",
                digits: "Please enter valid mileage"
            },
            phonenumber: {
                required: "Please enter a valid phone number",
                digits: "Please enter a UK phone number (11 digits with no spaces)",
                minlength: 'Please enter a UK phone number (11 digits with no spaces)',
                maxlength: 'Please enter a UK phone number (11 digits with no spaces)'
            },
            contactphonenumber: {
                required: "Please enter a valid phone number",
                digits: "Please enter a UK phone number (11 digits with no spaces)",
                minlength: 'Please enter a UK phone number (11 digits with no spaces)',
                maxlength: 'Please enter a UK phone number (11 digits with no spaces)'
            },
            yourlocation: {
                required: "Please enter your location"
            }
        },
        //submit the form if no errors
        submitHandler: function (form) {
            //disable the submit button
            $("input#contact-us-form-submit").attr('disabled', true).css('opacity', '0.5');

            //post the form data to the form handler page so an email can be sent

            //url needs changing as it points to dev
            $.post('/ajax/EmailPrivateSellerArchant.ashx',
                {
                    firstname: $("#firstname").val(),
                    lastname: $("#lastname").val(),
                    email: $("#email").val(),
                    registration: $("#registration").val(),
                    description: $("#description").val(),
                    price: $("#price").val(),
                    mileage: $("#mileage").val(),
                    phonenumber: $("#phonenumber").val(),
                    contactphonenumber: $("#contactphonenumber").val(),
                    yourlocation: $("#yourlocation").val()
                },
                function (data) {
                    //display relevant message if the form has been submitted or failed to submit
                    if (data == 'Success') {
                        var formResponse = '<p id="formReponse"><strong>Your details have been submitted and one of our representatives will be in touch with you shortly.</strong></p>';
                    }
                    else if (data == 'Failed') {
                        var formResponse = '<p id="formReponse" style="color: #FF0000;"><strong>There was an error submitting your details, please try again.</strong></p>';
                        //enable the submit button if the form didnt send 
                        $("input#contact-us-form-submit").removeAttr('disabled').css('opacity', '1');
                    }
                    //insert a message letting the user know if the form has been submitted or not at the bottom of the form
                    $("p#formReponse").remove();
                    $(formResponse).insertAfter($('div.form-handler-container'));
                });

        }
    });

    //On click on panel pagination
    $('#local-dealers .pagination a, #local-towns .pagination a').click(function () {
        if ($(this).attr('class') != 'current') {
            //get the parent div with class of panel_box    
            var id = $(this).closest("div.link_panel_box").attr("id");

            //work out what's the last page
            intLastPage = $("#" + id + " .pagination li").length / 2;
            intLastPage = intLastPage - 2;
            //get the current page inside the panel by checking class                                   
            intPageNo = $("#" + id + " .pagination li:not(.previous_page, .next_page) a.current:first").text();

            //call function to show/hide panels
            if (pagePanelChange($(this).text(), id)) {
                //Remove all classes 'current' then if function returns true then use JQuery to search for the correct page number by taking the text of the clicked element 
                //and then checking the top and bottom pagination to see if it contains that text then add the class 'current'
                $('#' + id + ' .pagination a').removeClass('current');
                $("#" + id + " .pagination a:contains('" + intPageNo + "')").addClass("current");
                if (intPageNo == 1) {
                    $("#" + id + " .pagination a:contains('Previous')").addClass("current");
                }
                else if (intPageNo == intLastPage) {
                    $("#" + id + " .pagination a:contains('Next')").addClass("current");
                }
            }
        }
    });

});


