

var cityJSON = [];
var pincodeJSON = [];
var pincodeCache = {};
function filterCitySuggestions(autoSuggestInputBoxId,callBackFunction){
	jQuery("#"+autoSuggestInputBoxId ).autocomplete({
		minLength: 2,
		source: function( request, response ) {
		  var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
		  response( $.grep( cityJSON, function( item ){
			  return matcher.test( item.value );
		  }) );
		},
		select: function( event, se ) {
			var cityUrlName = se.item.name;
			var cityId = se.item.id;
			var cityName = se.item.value;
			jQuery('#'+autoSuggestInputBoxId).attr('city_id',cityId);
			jQuery('#'+autoSuggestInputBoxId).attr('city_url_name',cityUrlName);
			jQuery('#'+autoSuggestInputBoxId).val(cityName);
			
			if(callBackFunction != undefined && callBackFunction != ''){
				callBackFunction();
			}
			
		}
	});	
}

function getCitySuggestions(autoSuggestBoxId,callBackFunction){ 
	if(cityJSON.length==0){
		jQuery.getJSON(JSFILEPATH +'/city_json.js',function(result){
			cityJSON = result; 
                        if(addDelhiNCR==1){
                            cityJSON.push({"id": "56", "value": "Delhi and NCR", "name": "Delhi-and-NCR"});
                        }
			filterCitySuggestions(autoSuggestBoxId,callBackFunction);
		});
	}
	else{
		filterCitySuggestions(autoSuggestBoxId,callBackFunction);
	}
			
}

function validateAutoSuggestCity(autoSuggestInputBoxId){
    var cityName = jQuery('#'+autoSuggestInputBoxId).val();
	if(cityName == undefined || cityName=="null" || cityName==''){
		return 'Blank';		
	}
	var checkCity = cityJSON.filter(function(data){return data.value.toLowerCase() == cityName.toLowerCase()});
	if(checkCity == ""){
	//for check a valid city name
		return 'Invalid';
	}else{
			var cityId = checkCity[0].id;
			var cityName = checkCity[0].value;
			var cityUrlName = checkCity[0].name;
			jQuery('#'+autoSuggestInputBoxId).attr('city_id',cityId);
			jQuery('#'+autoSuggestInputBoxId).val(cityName);
			jQuery('#'+autoSuggestInputBoxId).attr('city_url_name',cityUrlName);
			return 'Valid';
	}
	
}

function setAutoCitiesValueByName(autoSuggestInputBoxId,cityName){
	var checkCity = cityJSON.filter(function(data){return data.value.toLowerCase() == cityName.toLowerCase()});
	var	cityId = checkCity[0].id;
	cityName = checkCity[0].value;
	var cityUrlName = checkCity[0].name;
		jQuery('#'+autoSuggestInputBoxId).val(cityName);
		jQuery('#'+autoSuggestInputBoxId).attr('value',cityId);
		jQuery('#'+autoSuggestInputBoxId).attr('city_id',cityId);
		jQuery('#'+autoSuggestInputBoxId).attr('city_url_name',cityUrlName);
}

function setAutoCitiesValueById(autoSuggestInputBoxId,cityId){
	var checkCity = cityJSON.filter(function(data){return data.id == cityId});
	var	cityName = checkCity[0].value;
	var cityUrlName = checkCity[0].name;
		jQuery('#'+autoSuggestInputBoxId).val(cityName);
		jQuery('#'+autoSuggestInputBoxId).attr('value',cityId);
		jQuery('#'+autoSuggestInputBoxId).attr('city_id',cityId);
		jQuery('#'+autoSuggestInputBoxId).attr('city_url_name',cityUrlName);
		
}

function filterPincodeSuggestions(autoSuggestPincodeInputBoxId, cityDivBoxID, callBackPincodeFunction){
	jQuery("#"+autoSuggestPincodeInputBoxId ).autocomplete({
		minLength: 1,
		source: function( request, response ) {
		var option = { 
			cityId   : jQuery('#'+cityDivBoxID).attr('city_id'),
		} 
		if(option) {
			$.extend(request, option);
		}
			var rqterm = request.term;
			 rqterm += request.cityId;
			if ( rqterm in pincodeCache ) {
				response( $.map( pincodeCache[ rqterm ], function( item ) {
				return {
                   id: item.id ,
                   pincode: item.pincode ,
                   value: item.value,
                   cityId: item.cityId,
                   cityName: item.cityName
				}
				}));
				return;
			}	
			messages: {
				noResults: null;       
			}
			$.getJSON( SITEPATH+"/2db/get_pincode_json.php", request, function( data, status, xhr ) {
			  pincodeJSON = pincodeCache[ rqterm ] = data;
			  response( $.map( data, function( item ) {
			   return {
					   id: item.id ,
					   pincode: item.pincode ,
					   value: item.value,
					   cityId: item.cityId,
					   cityName: item.cityName
				   }
			   }));
			});
			
		},
		select: function( event, se ) {
			var pincode_id = se.item.id;
			var pincode = se.item.pincode;
			var pincodeName = se.item.value;
			var cityId = se.item.cityId;
			jQuery('#'+autoSuggestPincodeInputBoxId).attr('city_id',cityId);
			jQuery('#'+autoSuggestPincodeInputBoxId).attr('pincode_name',pincodeName);
			jQuery('#'+autoSuggestPincodeInputBoxId).attr('pincode_id',pincode_id);
			jQuery('#'+autoSuggestPincodeInputBoxId).val(se.item.pincode);
			
			if(cityDivBoxID != undefined && cityDivBoxID != ''){
				setAutoCitiesValueById(cityDivBoxID,cityId);
			}			
			if(callBackPincodeFunction != undefined && callBackPincodeFunction != ''){
				callBackPincodeFunction();
			}
			return false;

			
		}
	})
}

function validateAutoSuggestPincode(autoSuggestInputBoxId,cityDivBoxID){
    var pincode = jQuery('#'+autoSuggestInputBoxId).val();
	if(pincode == undefined || pincode=="null" || pincode==''){
		return 'Blank';		
	}
	var checkPincode = pincodeJSON.filter(function(data){return data.pincode.toLowerCase() == pincode.toLowerCase()});
	if(checkPincode == ""){
	//for check a valid city name
		return 'Invalid';
	}else{
			var pincode_id = checkPincode[0].id;
			var pincodeName = checkPincode[0].value;
			var pincode = checkPincode[0].pincode;
			var cityId = checkPincode[0].cityId;
			jQuery('#'+autoSuggestInputBoxId).attr('city_id',cityId);
			jQuery('#'+autoSuggestInputBoxId).attr('pincode_name',pincodeName);
			jQuery('#'+autoSuggestInputBoxId).val(checkPincode[0].pincode);
			
			if(cityDivBoxID != undefined && cityDivBoxID != ''){
				setAutoCitiesValueById(cityDivBoxID,cityId);
			}	
			return 'Valid';
	}
	
}
