/*
    JS DIRECTORY
        __FORMS
        __SLIDESHOW
        __MODALWINDOWS
        __MISC

/*__FORMS
==========================================================*/

    /* Regular Expressions */
    var REGEXP_PHONE = new RegExp("^((1)?[(-.\\s]*\\d{3}[)-.\\s]*)?[-.\\s]*\\d{3}[-.\\s]*\\d{4}$");
    var REGEXP_EMAIL = new RegExp("^[a-zA-Z0-9-_.]+@([a-zA-Z0-9-_]+\\.)+[a-zA-Z]{2,6}$");
    var REGEXP_NAME = new RegExp("^[a-zA-Z]+\\s?[a-zA-Z]*$");

	function validate_field(field, regex, message){

        if( field.value == "" || (regex && !regex.test(field.value)) ){
            alert(message);
            field.focus();
            return false;
        }
        /* valid imput, return true */
        return true;
	}

	function validate_form(theform){

        if( theform.name && !validate_field(theform.name, REGEXP_NAME, 'Not a valid name!') ) return false;
        if( theform.phone && !validate_field(theform.phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
		if( theform.cphone && !validate_field(theform.cphone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
        if( theform.email && !validate_field(theform.email, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
		if( theform.cname && !validate_field(theform.cname, REGEXP_NAME, 'Not a valid name!') ) return false;
		if( theform.fname && !validate_field(theform.fname, REGEXP_NAME, 'Not a valid name!') ) return false;
		if( theform.lname && !validate_field(theform.lname, REGEXP_NAME, 'Not a valid name!') ) return false;
        if( theform.bname && !validate_field(theform.bname, null, 'Company Name cannot be blank!') ) return false;
		if( theform.company && !validate_field(theform.company, null, 'Company cannot be blank!') ) return false;
        if( theform.address && !validate_field(theform.address, null, 'Address cannot be blank!') ) return false;
        if( theform.city && !validate_field(theform.city, null, 'City cannot be blank!') ) return false;
        if( theform.state && !validate_field(theform.state, null, 'State cannot be blank!') ) return false;
        if( theform.zip && !validate_field(theform.zip, null, 'Zip cannot be blank!') ) return false;
		if( theform.holdcity && !validate_field(theform.holdcity, null, 'City cannot be blank!') ) return false;
        if( theform.holdstate && !validate_field(theform.holdstate, null, 'State cannot be blank!') ) return false;
        if( theform.holdzip && !validate_field(theform.holdzip, null, 'Zip cannot be blank!') ) return false;
		if( theform.polnum && !validate_field(theform.polnum, null, 'Policy Number cannot be blank!') ) return false;
		if( theform.holdname && !validate_field(theform.holdname, null, 'Certificate Holder Name cannot be blank!') ) return false;
		if( theform.holdaddress && !validate_field(theform.holdaddress, null, 'Certificate Holder Address cannot be blank!') ) return false;


     // Submit Data
        if( !theform.noajax ){
            $.ajax({
                url: "form-handler.php",
                type: "POST",
                data: 'json=true&' + $(theform).serialize(),
                dataType: 'json',
                cache: false,
                error: function(a,b,c){ alert('An Error Occured??  Try again?  Maybe the server is down?  That should be rare, like 1 in a million, so maybe you should go play the lottery, then come back.'); },
                success: function(json){

                    if(json.error){

                        showmodal(json.error);

                    }else{

                        $(theform).html(json.success);

                    }

                }
            });

            return false;
        }

	}




/*__SLIDESHOW
    Functions used for slideshows, call slideshowInit to make a slideshow
==========================================================*/

    var slideshows = [];

    function slideshowInit(containerid, transitionTime, slideTimeLength){

     // Set Slideshow Array & get container
        var slideshow = slideshows[slideshows.length] = [];
        var container = $('#'+containerid);

     // Set Slideshow Buttons
        $('a.'+containerid+'-btn').each( function(){
            $(this).click(function(e){ e.preventDefault(); slideBtnClick( slideshow, $(this).attr('href') ); });
        });
        if( $('a.'+containerid+'-btn[href="1"]').length ){
            slideshow.trackNumberButtons = true;
        } else {
            slideshow.trackNumberButtons = false;
        }

     // Get Slideshow Timings
        if( transitionTime == null ) transitionTime = 1000;
        if( slideTimeLength == null ) slideTimeLength = 5000;

     // Get Slideshow Slides
        var slides = $(' > div', container);
        $(slides).hide();

     // Set Slideshow Variables
        slideshow.containerid = containerid;
        slideshow.container = container;
        slideshow.slides = slides;
        slideshow.maxSlide = slides.length;
        slideshow.transitionTime = transitionTime;
        slideshow.slideTimeLength = slideTimeLength;
        slideshow.inTransition = false;
        slideshow.currentSlide = 1;
        slideshow.previousSlide = 1;

     // Start Slideshow
        advanceSlide(slideshow, 1);

    }


    function advanceSlide(slideshow, action){

        slideshow.inTransition = true;

     // figure out new slide number
        if(action == 'next'){
            var newSlide = slideshow.currentSlide + 1;
        }else if(action == 'prev'){
            var newSlide = slideshow.currentSlide - 1;
        }else{
            var newSlide = Number(action);
        }

     // make sure new slide within bounds
        if ( newSlide > slideshow.maxSlide ){
            newSlide = 1;
        }else if ( newSlide < 1 ){
            newSlide = slideshow.maxSlide;
        }

     // activate / animate new slide
        slideshow.previousSlide = slideshow.currentSlide;
        slideshow.currentSlide = newSlide;
        newSlide = newSlide - 1;
        $(slideshow.slides).hide();
        slideshow.container.append(slideshow.slides[newSlide]);
        $(slideshow.slides[newSlide])
            .css({opacity: 0}).show()
            .animate({opacity: 1}, slideshow.transitionTime, function(){ slideshow.inTransition = false; })
            .animate({opacity: '+=0'}, slideshow.slideTimeLength, function(){ advanceSlide(slideshow, 'next') } );


    // if track numbered buttons, do it.
        if( slideshow.trackNumberButtons ){

         // Remove class "current" from numbered buttons
            $('a.'+slideshow.containerid+'-btn[href="'+slideshow.previousSlide+'"]').removeClass('current');
         // Add class "current" to current button.
            $('a.'+slideshow.containerid+'-btn[href="'+slideshow.currentSlide+'"]').addClass('current');

        }

    }


    function slideBtnClick(slideshow, action){

        if( slideshow.inTransition == false ){

         // No current slide transition, advance slide
            $("div", slideshow.container).stop(true, false);
            advanceSlide(slideshow, action);

        }

    }


/*__MODALWINDOWS
==========================================================*/

    function showmodal($html){

        //Set height and width to mask to fill up the whole screen
        $('#modalmask').css({'width':$(document).width(),'height':$(document).height()});

        //transition effect
        $('#modalmask').fadeIn(250);

        //load html if set
        if($html)
            $('#modalcontent').html($html);

        //Set the popup window to center
        $('#modalwindow').css("top", ( $(window).height() - $('#modalwindow').height() ) / 2+$(window).scrollTop() + "px");
        $('#modalwindow').css("left", ( $(window).width() - $('#modalwindow').width() ) / 2+$(window).scrollLeft() + "px");
        $('#modalwindow').css("width", $('#modalwindow').width() ) ;

        //transition effect
        $('#modalwindow').fadeIn(500);

    }



    function refreshmodal($html){

        //transition effect
        $('#modalwindow').fadeOut(250, function(){

            //load html if set
            if($html)
                $('#modalcontent').html($html);

            //Set the popup window to center
            $('#modalwindow').css("top", ( $(window).height() - $('#modalwindow').height() ) / 2+$(window).scrollTop() + "px");
            $('#modalwindow').css("left", ( $(window).width() - $('#modalwindow').width() ) / 2+$(window).scrollLeft() + "px");

            //transition effect
            $('#modalwindow').fadeIn(500);

        });

    }



    function closemodal(){

        // fade both mask and modal window
        $('#modalmask, #modalwindow').fadeOut(250);

    }


    $(function(){
        //if close button is clicked, hide mask and modal window
        $('#modalclose').click(function (e) {
            e.preventDefault();
            closemodal();
        });

        //if mask is clicked, hide mask and modal window
        $('#modalmask').click(function () {
            closemodal();
        });
    });




/*__MISC
==========================================================*/

 $(document).ready(function(){
        $("a[rel^='prettyPhoto']").prettyPhoto();
    });


$(document).ready(function() {
            $("#ButtonAcceptTerms").attr("disabled", "disabled");

            $("#CheckBoxTerms").click(function() {
                var checked_status = this.checked;
                if (checked_status == true) {
                    $("#ButtonAcceptTerms").removeAttr("disabled");
                }
                else {
                    $("#ButtonAcceptTerms").attr("disabled", "disabled");
                }
            });
        });
