function UserAgent()
{
    var v = navigator.appVersion.toLowerCase(), 
        u = navigator.userAgent.toLowerCase(), 
        n = navigator.appName;
    
    this.mac = (v.indexOf("mac") + 1);
    this.win = (v.indexOf("win") + 1);
    this.netscape = (n == "Netscape" && u.indexOf("firefox") == -1);
    this.firefox = (u.indexOf("firefox") > -1);
    this.ie = (n == "Microsoft Internet Explorer");
    this.aol = (u.indexOf("aol") + 1);
    this.opera = (u.indexOf("opera") + 1);
    this.ver = (this.ie) ? parseFloat(v.split('msie ')[1]) : parseFloat(v);
    this.v4 = (parseInt(v) == 4);
    this.os = (this.mac) ? 'mac' : (this.win) ? 'win' : navigator.platform;
    this.name = (this.netscape) ? 'nn' : (this.ie) ? 'ie' : n;
    this.codeName = this.name +'_'+ parseInt(this.ver) + '_'+ this.os;
}

var agent = new UserAgent();

function autoTab(input, e) 
{
    var keyCode = (agent.netscape) ? e.which : ((e.keyCode) ? e.keyCode : e.charCode);
    var filter = (agent.netscape) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
    if((input.getAttribute && input.value.length >= input.getAttribute("maxlength")) && !containsElement(filter, keyCode)) 
    {
        input.value = input.value.slice(0, input.getAttribute("maxlength"));
        
        var found = false, offset = 1, inputIndex = getIndex(input);
        while (!found)
        {
            try 
            { 
                // Make sure we're not past the end of the form.
                if (inputIndex + offset <= input.form.length)
                    input.form[(inputIndex + offset) % input.form.length].focus();
                
                found = true;  // we were able to successfully move focus to the next control.
            } 
            catch (e) 
            {
                offset++;
            }
        }        
    }
}
function containsElement(arr, ele) 
{
    var found = false, index = 0;
    while(!found && index < arr.length)
    {
        if(arr[index] == ele)
            found = true;
        else
            index++;
    }
    return found;
}
function getIndex(input) 
{
    var index = -1, i = 0;
    while (i < input.form.length && index == -1)
    {
        if (input.form[i] == input)
            index = i;
        else
            i++;
    }
    return index;
}



function format(control, type, format, decimals, group)
{
	var v;
	if (format == null) format = "";
	if(control.value=="") 
	    return;
	
	switch (type.toLowerCase()) {
		case "number":
			switch (format.toLowerCase()) {
				case "currency":
					v = getDecimals(decimals, getValue("[0-9.]", control.value), group);
					break;
				case "ssn":
					v = getValue("[0-9]", control.value);
			        if (v.length > 9)
			            v = v.substring(0, 9);
					if (v.length > 5)
					    v = v.substring(0,3) + "-" + v.substring(3,5) + "-" + v.substring(5);
					else if (v.length > 3)
					    v = v.substring(0,3) + "-" + v.substring(3);
					break;
				case "zipcode":
					v = getValue("[0-9]", control.value);
					v = (v.length > 5) ? (v.substring(0,5) + "-" + v.substring(5, 9)) : (v.substring(0,5));
					break;
			    case "date": 
			        v = getValue("[0-9]", control.value);			        
			        if (v.length > 8)
			            v = v.substring(0, 8);
			        if (v.length > 4) 
			            v = v.substring(0,2) + "/" + v.substring(2,4) + "/" + v.substring(4);
			        else if (v.length > 2)
			            v = v.substring(0,2) + "/" + v.substring(2);
			        break;
				default:
					v = getValue("[0-9]", control.value);
					break;
			}
			break;
	}
	
	function getDecimals(len, value, group) {
		var a = value.split(".", 2);
		var v;
		
		if (len == null) len = 0;
		if ((isNaN(len)) || (len < 0)) len = 0;
		if (group != true) group = false;
		
		if (isNaN(parseInt(a[0]))) a[0] = 0;
		if (isNaN(parseInt(a[1]))) a[1] = 0;
		
		switch (len) {
			case 0: v = a[0]; break;
			default: v = a[0] + "." + zeroPad(len, a[1]); break;
		}
		
		return v;
	}
	
	function getValue(pattern, value) {
		var re = new RegExp(pattern, "g");
		var m = value.match(re);
		var v = (m != null) ? m.join("") : "";
		
		return v;
	}
	
	function zeroPad(len, value) {
		var v = String(value);
		
		if (len > v.length) while (v.length < len) v +=  "0";
		if (v.length > len) v = v.substring(0, len);
		
		return v;
	}
	
	control.value = v;
	return;
}


function select_debt(select) {
	if( select.value != '' ) {
		$('#amount').focus();
	}
	
}

$( document ).ready(function(){
	$('#learn-more-link').hover(
		function(){ $('#tooltip-debt').fadeIn('fast'); },
		function(){ $('#tooltip-debt').fadeOut('fast'); }
	);
	
	$('#firstName2')
		.focus(function(){ $('#field-tip').fadeIn('fast'); })
		.blur(function(){ $('#field-tip').fadeOut('fast'); });
		
	$('#field-tip .close-tip').click(function(){ return false;})
})