/*
    JS DIRECTORY
        __MODALWINDOWS
        __FORMS



/*__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();
        });
    });








/*__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){
	

     // Validate

        if( !validate_field(theform.name, REGEXP_NAME, 'Not a valid name!') ) return false;
        if( !validate_field(theform.phone, REGEXP_PHONE, 'Not a valid phone number!') ) return false;
        if( !validate_field(theform.email, REGEXP_EMAIL, 'Not a valid email address!') ) return false;
        /* the following first check if field exists then make sure they are not blank */
        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;




     // Submit Data
        showmodal('Sending your information...');

        $.ajax({
            url: "/formmail.php",
            type: "POST",
            data: 'json=true&' + $(theform).serialize(),
            dataType: 'json',
            cache: false,
            error: function(){ alert('An Error Occured, We are currently working on fixing the problem.'); },
            success: function(json){

                if(json.error){

                    refreshmodal(json.error);

                }else{

                    refreshmodal(json.success);
                    $(theform)[0].reset();

                }

            }
        });

        return false;
            
		
	}

