var globArray = Array();
var jQueryPost = '';
var xtime = '';
var t_time = 1000;
var err;
var b;
var errorMessages = '';
var errorText = '';
var errors = false;

function checkValidZipCityField(type) {
	var re = new RegExp("^((f|t)[a-zA-Z]*)([0-9]?)[_]?(force)*$","g");
	var matches = re.exec(type);
	var ftbit = matches[2];
	var numbit = matches[3];
	if((jQuery('#'+ftbit+'zip'+numbit).val() == '' || jQuery('#'+ftbit+'state'+numbit).val() == '' || jQuery('#'+ftbit+'city'+numbit).val() == '') && jQuery('#'+ftbit+'cityorzip'+numbit).val() != '')
	  	return false;
	return true;
}

function checkZipLength(name) {
	_type = parseInt(jQuery('#' + name).val());
	if (!isNaN(_type)) {
		if (String(_type).length != 5) {
			jQuery('#' + name).css('border', '1px solid #ff0000');
			return true;
		}
	}

	return false;
}

function setCityZipIfOne(name) {
	if (getCheckZipInf(name) == true) {
		eval(jQuery('#state-from-popup-0 li a').attr('href'));
	}
}

function getCheckZipInf(name) {
	if (jQuery('#state-from-popup-0 li').length == 1) {
		return true;
	} else {
		jQuery('#state-from-popup-0').show();
		return false;
	};
}

function countSelectedCityZip(name) {
	if (jQuery('#state-from-popup-0 li').length == 1) {
		if (name != undefined) {
			setCityZipIfOne(name);
		}

		return true;
	} else {
		jQuery('#state-from-popup-0').show();
		return false;
	};
}

function fetchZipInfo(zip, type) {
	zip = escape(zip);
	zip = trim(zip);
	type = escape(type);
	cleanTarget('', '', '', type);
	clearTimeout(xtime);
	if(type.search("force")!=-1)
		fetchZipInfoTime(zip,type);
	else
		xtime = setTimeout("fetchZipInfoTime('" + zip + "', '" + type + "')", 1000);
}

function fetchZipInfoTime(zip, type)

 {
	zip = zip.replace(/%20/g, ' ');
	zip = zip.replace('%2C', ',');
	var re = new RegExp("^([-0-9a-zA-Z ,'\.\s]+)$");
	if (re.exec(zip) != null) {
		url = 'http://www.a1autotransport.com/quoteforms/util/get_state.php' + "?qtype=" + type + "&" + "query=" + zip;
		xss_ajax(url);
	}
}

function showVariants(data, type) {
	var re = new RegExp("^((f|t)[a-zA-Z]*)([0-9]*)([a-zA-Z_]*)","g");
	var matches = re.exec(type);
	var basetype = matches[1];
	var ftbit = matches[2];
	var numbit = matches[3];
	var force = matches[4];
	jQuery('#state-from-popup-0').remove();
	if(data){
		jQuery('#'+ftbit+'cityorzip'+numbit).after('<div id="state-from-popup-0" style="display:none" class="state-from-popup">' + data + '</div>');
		globArray[basetype] = countSelectedCityZip(basetype);
		if(force){
			eval(jQuery('#state-from-popup-0 li a:first-child').attr('href'));
		}
	} else {

		jQuery('#'+ftbit+'cityorzip'+numbit).css('border', '1px solid #ff0000');
		globArray[basetype] = false;
	}
}

function cleanTarget(city, state, zip, type) {
	var re = new RegExp("^((f|t)[a-zA-Z]*)([0-9]*)","g");
	var matches = re.exec(type);
	var ftbit = matches[2];
	var numbit = matches[3];
	jQuery('#'+ftbit+'state'+numbit).val(state);
	jQuery('#'+ftbit+'city'+numbit).val(city);
	jQuery('#'+ftbit+'zip'+numbit).val(zip);
}

function setCityZipFrom(fromname, city, state, zip, type, flag) {
	jQuery('#fcityorzip, #fcityorzip2, #fcityorzip3').val(ucwords(city.toLowerCase()) + ", " + ucwords(state.toUpperCase()));
	jQuery('#fstate, #fstate2, #fstate3').val(state);
	jQuery('#fcity, #fcity2, #fcity3').val(city);
	jQuery('#fzip, #fzip2, #fzip3').val(zip);
	jQuery(fromname).css('border', '1px solid #d2d2d2');
	jQuery('#state-from-popup-0').remove();
	return true;
}

function setCityZipTo(toname, city, state, zip, type, flag) {
	jQuery('#tcityorzip, #tcityorzip3').val(ucwords(city.toLowerCase()) + ", " + ucwords(state.toUpperCase()));
	jQuery('#tstate, #tstate3').val(state);
	jQuery('#tcity, #tcity3').val(city);
	jQuery('#tzip, #tzip3').val(zip);
	jQuery(toname).css('border', '1px solid #d2d2d2');
	jQuery('#state-from-popup-0').remove();
	return true;
}

function setTarget(city, state, zip, type, flag) {
	if (flag == undefined) {
		flag = true;
	}
	var re = new RegExp("^((f|t)[a-zA-Z]*)([0-9]*)([a-zA-Z_]*)","g");
	var matches = re.exec(type);
	var basetype = matches[1];
	var ftbit = matches[2];
	var numbit = matches[3];
	var force = matches[4];
	var fname = 'setCityZip'+ucwords(basetype);
	if(force && jQuery('#'+ftbit+'zip').val())
			jQuery('#state-from-popup-0').remove();
	else 
		window[fname]('#'+ftbit+'cityorzip'+numbit, city, state, zip, type, flag);
}

jQuery(document).ready(function(){

	jQuery('#QuoteForm input, #international_quote input, #movingQuoteForm input').bind('focus', function() {
		jQuery('#state-from-popup-0').remove();
	});

	jQuery('#fcityorzip,#tcityorzip,#fcityorzip2,#tcityorzip2,#fcityorzip3,#tcityorzip3').bind('blur', function(){
	  	var str = this.id;
		var re = new RegExp("^([tf])cityorzip([0-9]*)$", "g");
		var matches = re.exec(str);
		var ftstr = matches[1]=='f'? 'from':'to'
		if(!jQuery('#'+matches[1]+'city'+matches[2]).val()&&!jQuery('#'+matches[1]+'state'+matches[2]).val()&&!jQuery('#'+matches[1]+'zip'+matches[2]).val()){
			fetchZipInfo(jQuery(this).val(), ftstr+matches[2]+'_force');
		} 
		var re = new RegExp(",");
		if (!checkValidZipCityField(ftstr) && re.exec(jQuery('#'+matches[1]+'cityorzip'+matches[2]).val) == null ) {
			jQuery('#'+matches[1]+cityorzip+matches[2]).css('border', '1px solid #ff0000');
		} 	  
	});

	jQuery('#fcityorzip,#tcityorzip,#fcityorzip2,#tcityorzip2,#fcityorzip3,#tcityorzip3').bind('change keyup', function(e){
	  	var str = this.id;
		var re = new RegExp("^([tf])cityorzip([0-9]*)$", "g");
		var matches = re.exec(str);
		var ftstr = matches[1]=='f'? 'from':'to';
		if(e.keyCode != '38' && e.keyCode != '40'){
			cleanTarget('', '', '', ftstr+matches[2]);
			fetchZipInfo(trim(jQuery(this).val()), ftstr+matches[2]);
		}
	});
		
	jQuery('#fcityorzip,#tcityorzip,#fcityorzip2,#tcityorzip2,#fcityorzip3,#tcityorzip3').bind('keydown', function(e){
		var keyCode = e.keyCode || e.which;
		var str = this.id;
		var re = new RegExp("^([tf])cityorzip([0-9]*)$", "g");
		var matches = re.exec(str);
		if(e.keyCode == '40'){
			if(jQuery('#state-from-popup-0 > ul > li.selected').first().html()==null){
				jQuery('#state-from-popup-0 > ul > li').first().addClass('selected');
			} else {
				jQuery('#state-from-popup-0 > ul > li.selected').next().addClass('selected');
				jQuery('#state-from-popup-0 > ul > li.selected').first().removeClass('selected');
				jQuery('#state-from-popup-0').scrollTo(jQuery('#state-from-popup-0 > ul > li.selected'),50,{offset:-20});
			}
		} else if(e.keyCode == '38'){
			if(jQuery('#state-from-popup-0 > ul > li.selected').first().html()==null){
				// do nothing
			} else {
				jQuery('#state-from-popup-0 > ul > li.selected').prev().addClass('selected');
				jQuery('#state-from-popup-0 > ul > li.selected').first().next().removeClass('selected');
				jQuery('#state-from-popup-0').scrollTo(jQuery('#state-from-popup-0 > ul > li.selected'),50,{offset:-20});
			}
		} else if(e.keyCode == '13'){
			eval(jQuery('#state-from-popup-0 li.selected a').attr('href'));
			cur_focus = jQuery('input:enabled:visible').index(jQuery(':focus'));
			jQuery('input:enabled:visible:gt('+cur_focus+')').first().focus();
		} else {
			removeErrorDiv(matches[1]+'city_zip');
		}
	});
	

});
