/*	Application  : John Day River Boater Permits, Oregon
	Name         : js/formsajax.js
	Author       : Tim Carley 
	Created      : March 01, 2009
	Last Updated : 5/13/2010
	History      : 5/13/2010; added support for DateofBirth
	Purpose		 : funtions to handle the AJAX calls
*/
reg1 = /^\d{5,}$/ ;
reg2 = /^\d{1,}$/ ;
function getZip(fld)	{
	// send data to CF
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getZip', $(fld + 'ZipCode').value, fld, getZipResult);
}
// call back function
function getZipResult (r) {
	if (r) {
		// set city and state
		if (r.city.indexOf("not found") >= 0) {
			//alert(r.city);
			if (confirm(r.city + '\n\nWould you like to override the zipcode lookup?\nClick \'OK\' to manually enter the zipcode, city and state.\nClick \'Cancel\' to do another zipcode lookup.')) {
				var thezipinput = $(r.field + 'ZipCode') ;
				var thecityinput = $(r.field + 'City') ;
				thecityinput.onfocus = function() { };
				thecityinput.onchange = function() { };
				thezipinput.focus();
			} else {
				$(r.field + 'City').value = '';
				$(r.field + 'ZipCode').value = '';
				$(r.field + 'ZipCode').focus();
				for (i=0; i < $(r.field + 'State').length; i++) {
					if ($(r.field + 'State')[i].value == r.state) {
						$(r.field + 'State')[i].selected = true ;
					}
				}
			}
		}
		else {
			$(r.field + 'City').value = r.city;
			for (i=0; i < $(r.field + 'State').length; i++) {
				if ($(r.field + 'State')[i].value == r.state) {
					$(r.field + 'State')[i].selected = true ;
				}
			}
		}
	}
}
function getVNum(fld)	{
	// send data to CF
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getLeader', $(fld + 'LeaderID').value, fld, getVNumResult);
}
// call back function
function getVNumResult (r) {
	if (r) {
		//chk error
		if (r.errorcode==1) {
			alert(r.errormsg);
			//$(r.field + 'LeaderID').value = '';
			$(r.field + 'LeaderID').focus();
		}
		else {
			$(r.field + 'FirstName').value = r.firstname;
			$(r.field + 'MiddleName').value = r.middlename;
			$(r.field + 'LastName').value = r.lastname;
			$(r.field + 'DateofBirth').value = r.dateofbirth;
			if ($(r.field + 'Address1')) {$(r.field + 'Address1').value = r.address1;}
			if ($(r.field + 'Address')) {$(r.field + 'Address').value = r.address1;}
			$(r.field + 'ZipCode').value = r.zipcode;
		    $(r.field + 'City').value = r.city;
			for (i=0;i<$(r.field + 'State').length;i++) {
				if ($(r.field + 'State')[i].value == r.state) {
					$(r.field + 'State')[i].selected = true ;
				}
			}
			$(r.field + 'Phone').value = r.phone;
		    $(r.field + 'Email').value = r.email;
		}
	}
}
