/* $Id: shared.js,v 1.10 2009/11/01 13:33:26 sap Exp $ */

 
$(document).ready(function () {

    // connect with us form
    $('input#e-send').click(function(e){
        
        var isvalid = true;
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        clear_errors();
        
            // validate...
            if ($('input#f-name').val() == '')
                isvalid = set_error('input', 'name');
            
            if ($('input#f-company').val() == '')
                isvalid = set_error('input', 'company');
            
            if (! reg.test($('input#f-email').val()) && $('input#f-phone').val() == '')
            {                
                if (! reg.test($('input#f-email').val())) 
                    isvalid = set_error('input', 'email');
                else if ($('input#f-phone').val() == '')
                    isvalid = set_error('input', 'phone');
            }

            if ($('textarea#f-comments').val() == '') 
                isvalid = set_error('textarea', 'comments');
        
            if (isvalid)
            {
                $.post(
                    '/cgi-bin/connect.php',
                    {
                        name: $('input#f-name').val(),
                        company: $('input#f-company').val(),
                        email: $('input#f-email').val(),
                        phone: $('input#f-phone').val(),
                        comments: $('textarea#f-comments').val()
                    },
                    function(data)
                    {
                    clear_fields();
                    }
                );
                
            var content = $('#connect_confirm_message').html();
            var height = $('div#pop').height();
            var top_val = e.pageY - (height / 2) + "px";
            $('div#pop_bg').show();
            $('div#pop #content').html(content);

            $('div#pop').removeClass('pop_green');
            $('div#pop').addClass('pop_blue');
            $('div#pop').centerInClient();
            $('div#pop').show('slow');
            return false;
            }

        return false;
    });

    // subnav click open popup
    $('div#mid_menu a').click(function(e){
        var target_id = $(this).attr('id') + '_content';
        var content = $('#' + target_id).html();
        var height = $('div#pop').height();
        var top_val = e.pageY - (height / 2) + "px";
        $('div#pop_bg').show();
        $('div#pop #content').html(content);
        
        $('div#pop').removeClass('pop_blue');
        $('div#pop').addClass('pop_green');
        $('div#pop').centerInClient();        
        $('div#pop').show('slow');
        return false;
    });

    // click popup close [x]
    $('div#pop div#close a').click(function(){
        $('div#pop_bg').hide();
        $('div.mm_content').hide();
        $('div#pop').hide();
        return false;
    });

    // click out of popup to close
    $('div#pop_bg').click(function(){
        $('div#pop_bg').hide();
        $('div.mm_content').hide();
        $('div#pop').hide();
        return false;
    });

    // drag popup around for some juvenille amusement
	$(function() {
		$("div#pop").draggable();
	});
});

// clear_errors()
function clear_errors () {
    $('div#connect_wrapper label').removeClass('error_field');
    $('div#connect_wrapper input').removeClass('error_field');
    $('div#connect_wrapper textarea').removeClass('error_field');
}

// clear_fields()
function clear_fields () {
    $('input#f-name').val('');
    $('input#f-company').val('');
    $('input#f-email').val('');
    $('input#f-phone').val('');
    $('textarea#f-comments').val('');
}

// set_error()
function set_error (tag, id) {
    $(tag + '#f-' + id).addClass('error_field');
    $('label#label-' + id).addClass('error_field');
    return false;
}

// http://www.west-wind.com/weblog/posts/459873.aspx
$.fn.centerInClient = function(options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
                container: window,    // selector of element to center in
                completeHandler: null
              };
    $.extend(opt, options);
   
    return this.each(function(i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

        el.css("left", x + jWin.scrollLeft());
        el.css("top", y + jWin.scrollTop());

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}
