// JavaScript Document
///////
/*
 * jQuery doubleSelect Plugin
 * version: 1.2
 * @requires jQuery v1.3.2 or later
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * @version $Id: jquery.doubleSelect.js 3 2009-04-24 12:00:00Z $
 * @author  Johannes Geppert <post at jgeppert dot com> http://www.jgeppert.com
 *  7/23/2009 - J. Hinds change to two pass method to locate element for 'selected' attribute
 */

/**
 * Converts passed JSON options into <select> elements.
 * 
 * @param String
 *            id of the second select box
 * @param String
 *            option values
 * @param array
 *            options additional options (optional)
 */

 (function($) {
	
	$.fn.countryStateZipCombo = function(countryid, stateid, zipid, country_id, state_id, zip_id) {
		countrysel = $('#'+countryid);
		statesel = $('#'+stateid);
		zipsel = $('#'+zipid);
		countrysel.change(function() {
			$.fn.stateChange(countryid, stateid, zipid);
		});
		statesel.change(function() {
			$.fn.zipChange(countryid, stateid, zipid);
		});
		$.fn.selectCountryStateZip(countryid, country_id, stateid, state_id, zipid, zip_id)
	};
	$.fn.countryStateCombo = function(countryid, stateid, country_id, state_id) {
		countrysel = $('#'+countryid);
		statesel = $('#'+stateid);
		countrysel.change(function() {
			$.fn.stateChange(countryid, stateid);
		});
		$.fn.selectCountryState(countryid, country_id, stateid, state_id);
	};

	$.fn.stateChange = function(countryid, stateid, zipid) {
		statesel = $('#'+stateid).get(0);
		if(zipid)
			zipsel = $('#'+zipid).get(0);
		statesel.options.length = 0;
		if(zipid)
			zipsel.options.length = 0;
		var j = 0;
		country_id = $('#'+countryid).val();
		if(country_id == '') {
			if(typeof CountryStateZipCombo.pleaseSelectFirstCountry != 'undefined' && CountryStateZipCombo.pleaseSelectFirstCountry != null) {
			statesel.options[0]=new Option(CountryStateZipCombo.pleaseSelectFirstCountry.text, CountryStateZipCombo.pleaseSelectFirstCountry.value);
			zipsel.options[0]=new Option(CountryStateZipCombo.pleaseSelectFirstCountry.text, CountryStateZipCombo.pleaseSelectFirstCountry.value);
			}
			return;
		}

		if(typeof CountryStateZipCombo.pleaseSelectState != 'undefined' && CountryStateZipCombo.pleaseSelectState != null) {
			statesel.options[0]=new Option(CountryStateZipCombo.pleaseSelectState.text, CountryStateZipCombo.pleaseSelectState.value);
			j++;
		}

		if(zipid && typeof CountryStateZipCombo.pleaseSelectFirstState != 'undefined' && CountryStateZipCombo.pleaseSelectFirstState != null) {
			zipsel.options[0]=new Option(CountryStateZipCombo.pleaseSelectFirstState.text, CountryStateZipCombo.pleaseSelectFirstState.value);
		}
		
		this.loadStates(countryid, country_id, stateid);
	}; 

	$.fn.zipChange = function(countryid, stateid, zipid) {
	 	country_id = $('#'+countryid).val();
	 	state_id =  $('#'+stateid).val();
		this.loadZips(countryid, country_id, stateid, state_id, zipid);
	};

	$.fn.selectCountryState = function(countryid, country_id, stateid, state_id) {
		if(country_id)
			$('#'+countryid).val(country_id);
		else
			country_id = $('#'+countryid).val();
			var countrysel = $('#'+countryid).get(0);
			var statesel = $('#'+stateid).get(0);
			if(!country_id) {
				if(typeof CountryStateZipCombo.pleaseSelectFirstCountry != 'undefined' && CountryStateZipCombo.pleaseSelectFirstCountry != null) {
				statesel.options[0]=new Option(CountryStateZipCombo.pleaseSelectFirstCountry.text, CountryStateZipCombo.pleaseSelectFirstCountry.value);
				}
				return;
			}
		this.loadStates(countryid, country_id, stateid, state_id);


	};

	$.fn.selectCountryStateZip = function(countryid, country_id, stateid, state_id, zipid, zip_id) {
		if(country_id)
			$('#'+countryid).val(country_id);
		else
			country_id = $('#'+countryid).val();
		var statesel = $('#'+stateid).get(0);
		var zipsel = $('#'+zipid).get(0);
		if(!country_id) {
				if(typeof CountryStateZipCombo.pleaseSelectFirstCountry != 'undefined' && CountryStateZipCombo.pleaseSelectFirstCountry != null) 		{
				statesel.options[0]=new Option(CountryStateZipCombo.pleaseSelectFirstCountry.text, CountryStateZipCombo.pleaseSelectFirstCountry.value);
				zipsel.options[0]=new Option(CountryStateZipCombo.pleaseSelectFirstCountry.text, CountryStateZipCombo.pleaseSelectFirstCountry.value);
				}
				return;
			}
		this.loadStateZips(countryid, country_id, stateid, state_id, zipid, zip_id);
	};
	
	
	$.fn.loadStates = function(countryid, country_id, stateid, state_id, zipid) {
		$.getJSON(context_path+"geo/states?country_id="+country_id+"&state_id="+state_id,
		        function(json){

					var statesel = $('#'+stateid).get(0);
					if(zipid)
						var zipsel = $('#'+zipid).get(0);
					if(json['states'] != null) {
						var j = 0;
						if(typeof CountryStateZipCombo.pleaseSelectState != 'undefined' && CountryStateZipCombo.pleaseSelectState != null) {
							statesel.options[0]=new Option(CountryStateZipCombo.pleaseSelectState.text, CountryStateZipCombo.pleaseSelectState.value);
							j++;
						}
						for (i=0;i<json['states'].length;i++) {
							statesel.options[j]=new Option(json['states'][i]['name'],json['states'][i]['id']);			
							if(typeof state_id != 'undefined' && state_id != null && json['states'][i]['id'] == state_id) {
								statesel.options[j].selected = true;
							}
							j++;
						}
					}
		        });
				
		
	};
	
	
	$.fn.loadStateZips = function(countryid, country_id, stateid, state_id, zipid, zip_id) {
		$.getJSON(context_path+"geo/stateZips?country_id="+country_id+"&state_id="+state_id+"&zip_id="+zip_id,
		        function(json){
					var countrysel = $('#'+countryid).get(0);
					var statesel = $('#'+stateid).get(0);
					var zipsel = $('#'+zipid).get(0);
					if(json['states'] != null) {
						var j = 0;
						if(typeof CountryStateZipCombo.pleaseSelectState != 'undefined' && CountryStateZipCombo.pleaseSelectState != null) {
							statesel.options[0]=new Option(CountryStateZipCombo.pleaseSelectState.text, CountryStateZipCombo.pleaseSelectZip.value);
							j++;
						}
						for (i=0;i<json['states'].length;i++) {
							statesel.options[j]=new Option(json['states'][i]['name'],json['states'][i]['id']);			
							if(typeof state_id != 'undefined' && state_id != null && json['states'][i]['id'] == state_id) {
								statesel.options[j].selected = true;
							}
							j++;
						}
					}
					if(json['zips'] != null) {					
						var j = 0;
						if(typeof CountryStateZipCombo.pleaseSelectZip != 'undefined' && CountryStateZipCombo.pleaseSelectZip != null) {
							zipsel.options[0]=new Option(CountryStateZipCombo.pleaseSelectZip.text, CountryStateZipCombo.pleaseSelectZip.value);
							j++;
						}
						for (i=0;i<json['zips'].length;i++) {
							zipsel.options[j]=new Option(json['zips'][i]['zip_city'],json['zips'][i]['id']);			
							if(typeof zip_id != 'undefined' && zip_id != null && json['zips'][i]['id'] == zip_id) {
								zipsel.options[j].selected = true;
							}
							j++;
						}
					}
		        });
				
		
	};
	
	
	$.fn.loadZips = function(countryid, country_id, stateid, state_id, zipid, zip_id) {
		var countrysel = $('#'+countryid).get(0);
		var statesel = $('#'+stateid).get(0);
		var zipsel = $('#'+zipid).get(0);
		$.getJSON(context_path+"geo/zips?country_id="+country_id+"&state_id="+state_id+"&zip_id="+zip_id,
		        function(json){
					if(json['zips'] != null) {					
						var j = 0;
						if(typeof CountryStateZipCombo.pleaseSelectZip != 'undefined' && CountryStateZipCombo.pleaseSelectZip != null) {
							zipsel.options[0]=new Option(CountryStateZipCombo.pleaseSelectZip.text, CountryStateZipCombo.pleaseSelectZip.value);
							j++;
						}
						for (i=0;i<json['zips'].length;i++) {
							zipsel.options[j]=new Option(json['zips'][i]['zip_city'],json['zips'][i]['id']);			
							if(typeof zip_id != 'undefined' && zip_id != null && json['zips'][i]['id'] == zip_id) {
								this.zipsel.options[j].selected = true;
							}
							j++;
						}
					}
		        });
				
		
	};
    
})(jQuery);

