/**
*	JQuery plugin - Lead form
*	purpose : to manipulate the country - state- county selection
*	usage: make sure countries_countylookup table is available
*
*	@author Madie <abdul.othman@i-to-i.com> 13/03/2009
*/
(function($) {

	$.fn.leadform = function(options) {
	  var opts = $.extend({}, $.fn.leadform.defaults, options);

	  return this.each(function() {
	  	$.fn.leadform.updateRegionChoices(opts);	// props
	  });
	};

	$.fn.leadform.defaults = {
	  isfocus: true,
	  countrycode: 'GB',	  //countrycode: 'UK',
	  countryname: 'United Kingdom',
	  statename: '',
	  countyname: '',
	  hasregion: 0
	};

	// update region or state
	$.fn.leadform.updateRegionChoices = function(optz) {
		// is required or not?
		isreqRegion = ($("label#labelRegion > em").length > 0) ? "required" : "";
		isreqCounty = ($("label#labelCounty > em").length > 0) ? "required" : "";

		// reset region
		regionSelectionHtml = '<input id="primary_address_state" class="'+isreqRegion+'" type="text" title="Please enter your region or state" value="'+optz.statename+'" name="primary_address_state"/>';
		$("#leadRegionSpan").html(regionSelectionHtml);
		// reset county
		countySelectionHtml = '<input id="primary_address_county_c" class="'+isreqCounty+'" type="text" title="Please enter your county" value="'+optz.countyname+'" name="primary_address_county_c"/>';
		$("#leadCountySpan").html(countySelectionHtml);

		// get regions data
		$.getJSON("/ajax/processMagic.ajax.php?magiccontent=panel:countrypostal:regions&countryCode="+optz.countrycode, function(jsondata){
			var regionListHtml;

			if (jsondata.length > 0) {
				$.each(jsondata, function(j,item){
					statedefaultval = (item == optz.statename) ? 'selected="selected"' : '';
		            regionListHtml += '<option value="'+item+'" '+statedefaultval+'>'+item+'</option>';
		   		});

				regionSelectionHtml = '<select name="primary_address_state" id="primary_address_state" class="'+isreqRegion+'" title="Please select your region or state" onchange="updateCountyChoices(this.value,\''+optz.countyname+'\');">';
				regionSelectionHtml += '<option value="">--- Please Select ---</option>'+regionListHtml;
				regionSelectionHtml += '</select>';

				optz.hasregion = 1;
			}
			$("#leadRegionSpan").html(regionSelectionHtml);
			if (optz.isfocus == true) $("#primary_address_state").focus();
			// repopulate the county
			if (optz.hasregion > 0) updateCountyChoices(optz.statename,optz.countyname);
		});
	};

	// update region or state
	updateCountyChoices = function(regionChoice,defaultCountyVal) {
		// is required or not?
		isreqCounty = ($("label#labelCounty > em").length > 0) ? "required" : "";

		if (regionChoice == null) {
			// reset county
			countySelectionHtml = '<input id="primary_address_county_c" class="'+isreqCounty+'" type="text" title="Please enter your county" value="'+defaultCountyVal+'" name="primary_address_county_c" />';
			$("#leadCountySpan").html(countySelectionHtml);
		}
		else {
			$.getJSON("/ajax/processMagic.ajax.php?magiccontent=panel:countrypostal:counties&region="+regionChoice, function(jsondata){
				var countyListHtml;
				if (jsondata.length > 0) {
					$.each(jsondata, function(j,item){
						countydefaultval = (item == defaultCountyVal) ? 'selected="selected"' : '';
			            countyListHtml += '<option value="'+item+'" '+countydefaultval+'>'+item+'</option>';
			   		});

					countySelectionHtml = '<select name="primary_address_county_c" id="primary_address_county_c" class="'+isreqCounty+'" title="Please select your county">';
					countySelectionHtml += (jsondata.length == 1) ? countyListHtml : '<option value="">--- Please Select ---</option>'+countyListHtml;
					countySelectionHtml += '</select>';
				}
				$("#leadCountySpan").html(countySelectionHtml);
				$("#primary_address_county_c").focus();
			});
		}
	};


})(jQuery);
