// populates forms with values if they exist
function populateForm (loc, pre) {
    if (arguments.length < 2) {
        pre = '';
    }
    if (loc.address) {
        document.getElementById(pre + 'addr').value = loc.address;
    }
    if (loc.city) {
        document.getElementById(pre + 'city').value = loc.city;
    }
    if (loc.stateProvince) {
        document.getElementById(pre + 'state').value = loc.stateProvince;
    }
    if (loc.county) {
        document.getElementById(pre + 'county').value = loc.county;
    }
    if (loc.postalCode) {
        document.getElementById(pre + 'zip').value = loc.postalCode;
    }
    if (loc.country) {
        document.getElementById(pre + 'country').value = loc.country;
    }
	if (loc.recordId) {
        document.getElementById(pre + 'record').value = loc.recordId;
    }
	//else {
	//	alert("There is no record value. Please search again.")
	//}
}

// reset the form 
function formReset(fName) {
	eval("document." + fName + ".reset();");
}

// submit with action + only allows one submit per form 
var submittedformcount = 0;
function formSub(fName,fAction) {
	if (submittedformcount == 0) {
		submittedformcount++;
		//if (fAction > "") eval("document." + fName + ".action = '" + fAction + "';");
			// next line is for lowercasing search terms
		//	eval("if(typeof document." + fName + ".keyword != 'undefined') {document." + fName + ".keyword.value = document." + fName + ".keyword.value.toLowerCase();}");
		//	eval("document." + fName + ".submit();");
		//	document.body.style.cursor='wait'; 
		//	status='Loading...';
		//	if (cookies['KIOSK_NBR'] || cookies['STORE_NBR']) {
		//		loadlink();
		//	}
	}
}

// Capture enter key in forms
function formskeypress(e) {
	if (window.event) {
		if (window.event.keyCode == 13) {
			window.event.returnValue = false;
			if (window.event.srcElement.form.action) {
				if (!window.event.srcElement.form.enteroverride) {
					formSub(window.event.srcElement.form.name);
				} else {
					eval(window.event.srcElement.form.enteroverride.value);
				}
			}
		}
	} else {
		if (e.which == 13) {
			e.preventDefault();
			if (e.target.form.action) {
				if (!e.target.form.enteroverride) {
					formSub(e.target.form.name);
				} else {
					eval(e.target.form.enteroverride.value);								
				}
			}
		}
	}
}

// Hook up enter key capturing
function formsenterhook() {
	if (document.getElementById) {
		if (document.captureEvents) document.captureEvents(Event.KEYPRESS);
		for (f=0; f < document.forms.length; f++) {
			for (i=0; i < document.forms[f].elements.length; i++) {
				if (document.forms[f].elements[i].type != "textarea" ) { 
					document.forms[f].elements[i].onkeypress = formskeypress;
				}
			}
		}
		var a = document.getElementsByTagName("a");
		for(i=0; i < a.length; i++) {
			if ( a[i].className.match(/^d\d+$/) ) { 
					a[i].onkeypress = buttonskeypress;
			}
		}
	}
}

// Capture spacebar in buttons
function buttonskeypress(e) {
	if (window.event) {
		if (window.event.keyCode == 32) {
			window.event.returnValue = false;
			if (window.event.srcElement.href > '') window.location = window.event.srcElement.href;
		}
	} else {
		if (e.which == 32) {
			e.preventDefault();
			if (e.target.href > '') window.location = e.target.href;
		}
	}

}