//  AutoZip by BizTrack.org
//  
//  
//
var autozipdir = "autozip/";

function AutoZip() {
	var zipField;
	var procFrame;
	var countyField;
	var stateField;
	var visSetup;
	var cityField;
	
	this.check = function() {
		if( !this.isSetup() ) {
			alert( "AutoZip not set up properly" );
		} else {
			str = this.zipField.value;
			if( str.length == 5 ) {
				this.zipField.style.backgroundColor='#ffee88';
				this.procFrame.location.href=autozipdir + "validate_zip.php?zip=" + str;
			}
		}
	}
	
	this.setc = function(c) {
		this.zipField.style.backgroundColor=c;
	}
	
	this.blockNums = function(e) {
		var code;
		if( typeof(e) == "object" ){
			code = e.which;
		} else {
			code = event.keyCode;
		}
		
		var keyChar = String.fromCharCode(code);
		
		if( code == '8' || code == '0' || code == '13' )
			return true;
			
		if( this.value.length == 5 ) {
			if( keyChar == '-' ) {
				return true;
			} else {
				if( typeof(e) != "object" )event.returnValue = false;
				return false;
			}
		}
		
		if (keyChar < '0' || keyChar > '9') {
			this.style.backgroundColor = "red";
			setTimeout("az.setc('white');",10);
			if( typeof(e) != "object" )event.returnValue = false;
			return false;
		}
		
		if( this.value.length == 10 ) {
			this.style.backgroundColor = "red";
			setTimeout("az.setc('white');",10);
			if( typeof(e) != "object" )event.returnValue = false;
			return false;
		}
	}
	
	this.ccheck = function(){
		az.check();
	}
	this.setup = function( zf_id, pf_id, sf_id, cyf_id, cf_id ) {
		this.zipField = document.getElementById(zf_id);
		this.zipField.onkeypress = az.blockNums;
		this.zipField.onkeyup = this.ccheck;
		this.procFrame = frames[pf_id];
		this.cityField = document.getElementById(cf_id);
		this.countyField = document.getElementById(cyf_id);
		this.stateField = document.getElementById(sf_id);
		this.visSetup = true;
	}

	this.isSetup = function () {
		return this.visSetup;
	}
	
}



