// our main js file

var statesFull = {
	"AL" : "Alabama",
	"AK" : "Alaska",
	"AZ" : "Arizona",
	"AR" : "Arkansas",
	"CA" : "California",
	"CO" : "Colorado",
	"CT" : "Connecticut",
	"DE" : "Delaware",
	"DC" : "Washington DC",
	"FL" : "Florida",
	"GA" : "Georgia",
	"HI" : "Hawaii",
	"ID" : "Idaho",
	"IL" : "Illinois",
	"IN" : "Indiana",
	"IA" : "Iowa",
	"KS" : "Kansas",
	"KY" : "Kentucky",
	"LA" : "Louisiana",
	"ME" : "Maine",
	"MD" : "Maryland",
	"MA" : "Massachusetts",
	"MI" : "Michigan",
	"MN" : "Minnesota",
	"MS" : "Mississippi",
	"MO" : "Missouri",
	"MT" : "Montana",
	"NE" : "Nebraska",
	"NV" : "Nevada",
	"NH" : "New Hampshire",
	"NJ" : "New Jersey",
	"NM" : "New Mexico",
	"NY" : "New York",
	"NC" : "North Carolina",
	"ND" : "North Dakota",
	"OH" : "Ohio",
	"OK" : "Oklahoma",
	"OR" : "Oregon",
	"PA" : "Pennsylvania",
	"RI" : "Rhode Island",
	"SC" : "South Carolina",
	"SD" : "South Dakota",
	"TN" : "Tennessee",
	"TX" : "Texas",
	"UT" : "Utah",
	"VT" : "Vermont",
	"VA" : "Virginia",
	"WA" : "Washington",
	"WV" : "West Virginia",
	"WI" : "Wisconsin",
	"WY" : "Wyoming"};

var telephoneTypes = {
	'0' : 'Home',
	'1' : 'Work',
	'2' : 'Mobile',
	'3' : 'Fax'};

function getOnlyNumbers(v){
	var x = "";
	// regex would be easier but i think this is probably faster, and scriptaculous doesn't
	// screw around with it
	for (var i=0; i<v.length; i++){
		if (parseInt(v[i])==0 || v[i]==1 || v[i]==2 || v[i]==3 || v[i]==4 || v[i]==5 || v[i]==6 || v[i]==7 || v[i]==8 || v[i]==9){
			x += v[i]+'';
		}
	}
	return x;
}

function getNumbersAndDot(v){
	var x = "";
	v = v+' ';
	// regex would be easier but i think this is probably faster, and scriptaculous doesn't
	// screw around with it
	for (var i=0; i<v.length; i++){
		if (parseInt(v[i])==0 || v[i]==1 || v[i]==2 || v[i]==3 || v[i]==4 || v[i]==5 || v[i]==6 || v[i]==7 || v[i]==8 || v[i]==9 || v[i]=="."){
			x += v[i]+'';
		}
	}
	return x;
}

function numericFieldFilter(fld, fmt){
	var nums = getOnlyNumbers(fld.value);
	fld.value = '';

	var numCount = 0;
	var output = "";
	var breakit = 0;
	for (var i=0; i<fmt.length; i++){
		if (nums[numCount]){
			if (fmt[i]=="#"){
				output += nums[numCount];
				numCount++;
			} else {
				output += fmt[i];
			}
		}
	}
	fld.value = output;
}

function formatnum(num) {
	num = getNumbersAndDot(num);
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'(') + num + '.' + cents + ((sign)?'':')'));
}

function handleGenericMouseOverBG(ele){
	var obj = $(ele);
	if (obj){
		obj.attr('mouseOutColor', obj.css('background-color'));
		obj.css('background-color', obj.attr('mouseOverColor'));
	}
}

function handleGenericMouseOutBG(ele){
	var obj = $(ele);
	if (obj){
		obj.css('background-color', obj.attr('mouseOutColor'));
	}
}

function handleGenericMouseOverFW(ele){
	var obj = $(ele);
	if (obj){
		obj.attr('mouseOutFW', obj.css('font-weight'));
		obj.css('font-weight', obj.attr('mouseOverFW'));
	}
}

function handleGenericMouseOutFW(ele){
	var obj = $(ele);
	if (obj){
		obj.css('font-weight', obj.attr('mouseOutFW'));
	}
}

function attachGenericMouseOverEvents(){
	$('[mouseOverColor]').mouseenter(function(){
		handleGenericMouseOverBG($(this));
	});
	$('[mouseOverColor]').mouseleave(function(){
		handleGenericMouseOutBG($(this));
	});
	$('[mouseOverFW]').mouseenter(function(){
		handleGenericMouseOverFW($(this));
	});
	$('[mouseOverFW]').mouseleave(function(){
		handleGenericMouseOutFW($(this));
	});
}

function checkpassword(f1, f2, ns, sb, ab){
	var field1 = $('#'+f1);
	var field2 = $('#'+f2);
	var bttn = $('#'+sb);
	var notify = $('#'+ns);
	if (field1 && field2){
		if (field1.val()==field2.val() && (field1.val()!='' || ab==true)){
			if (bttn){
				bttn.attr('disabled', '');
			}
			if (notify){
				notify.css('color', 'green');
				notify.css('font-weight', 'bold');
				if (field1.val()!=''){
					notify.html('Password Accepted');
				} else {
					notify.html('');
				}
			}
		} else {
			if (bttn){
				bttn.attr('disabled', 'disabled');
			}
			if (notify){
				notify.css('color', 'red');
				notify.css('font-weight', 'bold');
				notify.html('Passwords Do Not Match!');
			}
		}
	}
}

function toggleCheckboxes(cbxs){
	if (!cbxs)return;
	for (var i=0; i<cbxs.length; i++){
		cbxs[i].checked = !cbxs[i].checked;
	}
}

function createFormQueryString(f){
	if (typeof f=="undefined"){
		return alert("Invalid form object.");
	}
	var tf;
	var qs = "";
	for (var fi=0; fi<f.elements.length; fi++){
		tf = f.elements[fi];
		switch (tf.type){
			case "hidden":
			case "text":
			case "textarea":
				qs = qs + tf.name + "=" + escape(tf.value) + "&";
			break;
			case "select":
			case "select-one":
				for (var si=0; si<tf.options.length; si++){
					if (tf.options[si].selected){
						qs = qs + tf.name + "=" + escape(tf.options[si].value) + "&";
					}
				}
			break;
			case "radio":
			case "checkbox":
				if (tf.checked){
					qs = qs + tf.name + "=" + escape(tf.value) + "&";
				}
			break;
		}
	}
	return qs;
}

function postForm(f, url, os){
	$.ajax({
		type: 'POST',
		url: url,
		data: $(f).serialize(),
		success: function(data, textStatus, XMLHttpRequest) {
			os(data);
		},
		dataType: 'html'
	});
}

function confirmDelete(url){
	if (confirm("Are you sure you wish to delete this?")){
		document.location.href = url;
	}
}

var modalDialogID = '';

function dialogWithUrl(url, title, w, h) {
	modalDialogID = 'modalDialog_' + Math.floor(Math.random()*1000);
	$('body').append('<div style="font-size: 12px;" id="'+modalDialogID+'">Content Loading...</div>');
	$('#'+modalDialogID).dialog({
		autoOpen : true,
		modal: true,
		minHeight: h,
		minWidth: w,
		height: h,
		width: w,
		title: title,
		close: function() {
			//$('#'+modalDialogID).destroy();
		}
	});
	setDivLoading('#'+modalDialogID)
	$('#'+modalDialogID).load(url);
}

function setDivLoading(sel) {
	$(sel).html("<br /><div style='text-align: center;'><img src='/images/icons/hourglass.png' alt='loading...' /> Loading...</div>");
}

function dialogGoToUrl(url) {
	setDivLoading('#'+modalDialogID)
	$('#'+modalDialogID).load(url);
}

function killModalDialog() {
	$('#'+modalDialogID).hide();
}

function showHistory(logurl, obj, objid, columns, w, h) {
	var url = logurl + '?object=' + obj + '&object_id=' + objid + '&w=' + w + '&h=' + h + '&columns=' + columns;
	dialogWithUrl(url, 'Change History', w, h);
}





