jQuery(document).ready(function() {
   // generate markup
  if((window.location.href.indexOf('step=1')+1) || (window.location.href.indexOf('step')==-1)){
	  var country= jQuery('#country option:selected').val();	   
		jQuery.post("/wp-content/themes/lowdpoa/update-region-list.php", {country: country}, function(data) {
			// format and output result
			myOutput = '';
			if(data.length > 0){
			if(data.length == 14){
				$selected = data[13];
				data.pop();
			}
			jQuery.each(data, function() { 
				title = this;
				output = createOuput(title, $selected);
				myOutput = myOutput + output;
			});
			jQuery('#province-state').empty();
			jQuery('#province-state').append('<select name="province" id="province" class="cformselect"></select>');
			jQuery('#province').append('<option value="">(Select &darr;)</option>');
			jQuery('#province').append(myOutput);
			}
			else {
			 jQuery('#province-state').empty();
			 jQuery('#province-state').append('<input type="text" name="province" id="province" value="" class="single fldrequired" />');
			}   	
		 }, "json");
		 
	  jQuery('#country').change(function(){
		var country = jQuery('#country option:selected').val();
		jQuery.post("/wp-content/themes/lowdpoa/update-region-list.php", {country: country}, function(data) {
			// format and output result
			myOutput = '';
			if(data.length > 0){
			jQuery.each(data, function() { 
				title = this;
				output = createOuput(title);
				myOutput = myOutput + output;
			});
			jQuery('#province-state').empty();
			jQuery('#province-state').append('<select name="province" id="province" class="cformselect fldrequired"></select>');
			jQuery('#province').append('<option value="">(Select &darr;)</option>');
			jQuery('#province').append(myOutput);
			}
			else {
			 jQuery('#province-state').empty();
			 jQuery('#province-state').append('<input type="text" name="province" id="province" value="" class="single fldrequired" />');
			}   	
		 }, "json");     
	  });
  }
 
  else if(window.location.href.indexOf('step=2')+1){
	 
	  var i=1;
	  var c=0;
	  jQuery('.countryselect').each(function(){
	  	var country= jQuery('#country_'+i+' option:selected').val();		 
		  jQuery.post("/wp-content/themes/lowdpoa/update-region-list.php", {country: country}, function(data) {				
				if(c==0)
					c=i-i+1;
				myOutput = '';
				if(data.length > 0){
					jQuery.each(data, function() { 
						title = this;
						output = createOuput(title);
						myOutput = myOutput + output;
					});					
					//alert('province-state_'+c);
					jQuery('#province-state_'+c).empty();
					jQuery('#province-state_'+c).append('<select name="province_'+c+'" id="province_'+c+'" class="cformselect fldrequired"></select>');
					jQuery('#province_'+c).append('<option value="">(Select &darr;)</option>');
					jQuery('#province_'+c).append(myOutput);
				}
				else {
					 jQuery('#province-state_'+c).empty();
					 jQuery('#province-state_'+c).append('<input type="text" name="province_'+c+'" id="province_'+c+'" value="" class="single fldrequired" />');
				}
				c++
		}, "json");
		
		jQuery('#country_'+i).change(function(){
		var id = jQuery(this).attr("id");
		var c = id.substr(id.length-1,1);
		var country = jQuery('#country_'+c+' option:selected').val();
		
		jQuery.post("/wp-content/themes/lowdpoa/update-region-list.php", {country: country}, function(data) {
			// format and output result
			myOutput = '';
			if(data.length > 0){
			jQuery.each(data, function() { 
				title = this;
				output = createOuput(title);
				myOutput = myOutput + output;
			});
			jQuery('#province-state_'+c).empty();
			jQuery('#province-state_'+c).append('<select name="province_'+c+'" id="province_'+c+'" class="cformselect fldrequired"></select>');
			jQuery('#province_'+c).append('<option value="">(Select &darr;)</option>');
			jQuery('#province_'+c).append(myOutput);
			}
			else {
			 jQuery('#province-state_'+c).empty();
			 jQuery('#province-state_'+c).append('<input type="text" name="province_'+c+'" id="province_'+c+'" value="" class="single fldrequired" />');
			}   	
		 }, "json");		 
	  });
	  i++;
	});		  
  }   
});

function createOuput(title, selected) {
	var output = '';
	if(selected && selected != '' && title == selected)
		output = '<option value="' + title + '" selected>' + title + '</option>';
	else
		output = '<option value="' + title + '">' + title + '</option>';
	return output;
}
