// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// function for toggling the outline buttons
var ToggleImage = Class.create();
ToggleImage.prototype = {
  initialize: function() {
	this.expImg=new Image;
	this.colImg=new Image;
	this.expImg.src='/images/outline_expanded.jpg';
	this.colImg.src='/images/outline_collapsed.jpg';
  },

  toggle: function(imgId) {
	elem=document.getElementById(imgId);
	if(elem.src==this.expImg.src)
		elem.src=this.colImg.src
	else
		elem.src=this.expImg.src	
  }
}

var ImageToggler = new ToggleImage();


// function for onKeyPress to restrict to numbers only
function acceptNumbersOnly(e)
{
	var keynum;
	var keychar;
	var numcheck;

	if(window.event) // IE
	  {
	  keynum = e.keyCode;
	  }
	else if(e.which) // Netscape/Firefox/Opera
	  {
	  keynum = e.which;
	  }

	if(keynum==8)
		return true;	// allow backspace
	if(e.keyCode==9)
		return true;	// allow tab
		
	keychar = String.fromCharCode(keynum);
	numcheck = /\d/;
	return numcheck.test(keychar);
}

String.prototype.capitalize = function() {
	var words = this.split(" ");
	for(var i = 0; i < words.length; ++i)
		words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);
	return words.join(" ");
};

jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ?
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}

function load_date_pickers() {
    jQuery('input:regex(id, _date)').each(function(){
        if(jQuery(this).is(":visible"))
            jQuery(this).datepicker();
    });
}

function load_phone_masks() {
    jQuery('input:regex(id, _phone_)').each(function(){
        if(jQuery(this).attr('id').indexOf('_phone_work')==-1)
            jQuery(this).mask("(999) 999-9999");
    });

    jQuery('input:regex(id, _phone_work)').each(function(){
        jQuery(this).mask("(999) 999-9999? x99999");
    });
}

function load_postal_code_masks() {
    jQuery('input:regex(id, _postal_code)').each(function(){
        jQuery(this).mask("a9a 9a9");
    });
}

function uppercase(element) {
    jQuery(element).val(jQuery(element).val().toUpperCase());
}

function lowercase(element) {
    jQuery(element).val(jQuery(element).val().toLowerCase());
}

function capitalize(element) {
    var words=jQuery(element).val().split(" ");
    for(var i = 0; i < words.length; ++i)
        words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);

    jQuery(element).val(words.join(" "));
}

function load_uppercase_fields() {
    jQuery('input:regex(id, _postal_code)').each(function(){
        jQuery(this).blur(function() {
            uppercase(jQuery(this));
        });
    });

    jQuery('input:regex(id, _state)').each(function(){
        jQuery(this).blur(function() {
            uppercase(jQuery(this));
        });
    });

    jQuery('input:regex(id, _initials)').each(function(){
        jQuery(this).blur(function() {
            uppercase(jQuery(this));
        });
    });
}

function load_lowercase_fields() {
    jQuery('input:regex(id, _email_address)').each(function(){
        jQuery(this).blur(function() {
            lowercase(jQuery(this));
        });
    });

    jQuery('input:regex(id, _user_name)').each(function(){
        jQuery(this).blur(function() {
            lowercase(jQuery(this));
        });
    });
}

function load_capitalized_fields() {
    jQuery('input:regex(id, _first_name)').each(function(){
        jQuery(this).blur(function() {
            capitalize(jQuery(this));
        });
    });

    jQuery('input:regex(id, _last_name)').each(function(){
        jQuery(this).blur(function() {
            capitalize(jQuery(this));
        });
    });

    jQuery('input:regex(id, _city)').each(function(){
        jQuery(this).blur(function() {
            capitalize(jQuery(this));
        });
    });

    jQuery('input:regex(id, _owner_name)').each(function(){
        jQuery(this).blur(function() {
            capitalize(jQuery(this));
        });
    });

    jQuery('input:regex(id, _svcmgr_name)').each(function(){
        jQuery(this).blur(function() {
            capitalize(jQuery(this));
        });
    });

    jQuery('input:regex(id, _street_address)').each(function(){
        jQuery(this).blur(function() {
            capitalize(jQuery(this));
        });
    });

    jQuery('input:regex(id, _employer)').each(function(){
        jQuery(this).blur(function() {
            capitalize(jQuery(this));
        });
    });

    jQuery('input:regex(id, _position)').each(function(){
        jQuery(this).blur(function() {
            capitalize(jQuery(this));
        });
    });

    jQuery('input:regex(id, _spouse_name)').each(function(){
        jQuery(this).blur(function() {
            capitalize(jQuery(this));
        });
    });

    jQuery('input:regex(id, dealer_name)').each(function(){
        jQuery(this).blur(function() {
            capitalize(jQuery(this));
        });
    });
}

function load_jquery_elements() {
    load_date_pickers();
    load_phone_masks();
    load_postal_code_masks();
    load_uppercase_fields();
    load_lowercase_fields();
    load_capitalized_fields();
}

function reload_jquery_elements() {
    load_date_pickers();
}

jQuery(function () {
    load_jquery_elements();
});
