// ==UserScript==
// @name          myAutoFill
// @namespace     http://kailashnadh.name/code
// @description	  This is a nifty little script that'll autofill formfields with preset data ;)
// ==/UserScript==

// Code by Kailash Nadh
// http://kailashnadh.name
// 20th June 2005

// Licensed for unlimited modification and redistribution as long as
// this notice is kept intact.



// NOTE : Once you are on a webpage, you can invoke
//		  this script by pressing CTRL+SHIFT+F




// ================================ Set data here ============================= //

// Field names, each one in an array. Can specify multiple possibilities separated by a comma
// You can add as many fields as you want

myFieldNames = [
	"first_name,name_first",
	"name_last,last_name",
	"full_name,name",
	"email,mail",
	"url,site,website",
	"city",
	"adress",
	"state,province",
	"country",
	"phone"];

// Values for the fieldnames above, in the exact array order
myFieldVals = [
	"Your_First_Name",
	"Your_last_name",
	"Your Full name :)",
	"you@email.com",
	"http://yoursite.com.com",
	"Your City",
	"Your street address",
	"Your State",
	"Your Country",
	"Your Phone"];


// ================================= No need to alter below ==================== //


function doAutoFill() {

var allFields, currentField, fieldName, splitFields, test;
allFields = document.getElementsByTagName('input');

for (var i = 0; i < allFields.length; i++) {
	currentField = allFields[i];

	if(currentField.type == "text") {
		fieldName=currentField.name;	// The current field name

			for(var n = 0; n < myFieldNames.length; n++) {
				splitFields=myFieldNames[n].split(',');

				if(splitFields.length > 1) {
					// We have more than one field name, go through them
					for(var j = 0; j < splitFields.length; j++) {
						if(matchField(splitFields[j], fieldName)) {
								currentField.value=myFieldVals[n];	// Put the value
							break;
						}
					}
				} else {
					// Just one fieldname. Match it
					if(matchField(myFieldNames[n], fieldName)) {
						currentField.value=myFieldVals[n];	// Put the value
					}
				}
			}
	}

}

}

// =============================

// Does a case insensitive string-to-string match using regexp
function matchField(the_match, the_field) {
	var chkField;

	chkField = new RegExp(the_match, "i");	// Create a regexp to match the fieldname (case insensitive)
	return the_field.match(chkField);
}

// =============================

// Track keypresses
function keyDown(e) {	
	if (e.shiftKey&&e.ctrlKey) {
		switch (e.keyCode) {
			case 70: // CTRL+SHIFT+F
				doAutoFill();
				break;
		}
	}
}

// =============================

// Add a handler for the keyboard shortcut
window.addEventListener("keydown", keyDown, true);

// =============================
