jQuery.fn.cityselect = function(settings) {
	settings = jQuery.extend({
		citystart:'City',
		provstart:'Province / State / Region',
		hilite: 'A5E8FF',
		hilitetime: 500
	}, settings);
	return this.each(function(){
		var othertext = "please specify";
		ps = "";
		bindprov();
		bindcities();
		jQuery("#country").change(function(){
			country = jQuery(this).find("option:selected").val().toLowerCase();
			if(country == "canada"){
				gocanada();
			} else if(country == "united states"){
				gousa();
			} else {
				goworld();
			}
		});
		function resetcity(){
			jQuery("#c1").addClass("c-hide");
			if(!jQuery("#c2").hasClass("error")){
				jQuery("#c2").addClass("c-hide");
			}
			if(jQuery("#province").length>0){
				jQuery("#province").unbind("change");
				jQuery("#province").unbind("blur focus");
			}
		}
		function gocanada(){
			resetcity();
			ps = "Province";
			list = new Array(
				'Alberta',
				'British Columbia',
				'Manitoba',
				'New Brunswick',
				'Newfoundland and Labrador',
				'Northwest Territories',
				'Nova Scotia',
				'Nunavut',
				'Ontario',
				'Prince Edward Island',
				'Quebec',
				'Saskatchewan',
				'Yukon'
			);
			buildlist(list);
		}
		function gousa(){
			resetcity();
			ps = "State";
			list = new Array(
				'Alabama',
				'Alaska',
				'Arizona',
				'Arkansas',
				'California',
				'Colorado',
				'Connecticut',
				'Delaware',
				'Florida',
				'Georgia',
				'Hawaii',
				'Idaho',
				'Illinois',
				'Indiana',
				'Iowa',
				'Kansas',
				'Kentucky',
				'Louisiana',
				'Maine',
				'Maryland',
				'Massachusetts',
				'Michigan',
				'Minnesota',
				'Mississippi',
				'Missouri',
				'Montana',
				'Nebraska',
				'Nevada',
				'New Hampshire',
				'New Jersey',
				'New Mexico',
				'New York',
				'North Carolina',
				'North Dakota',
				'Ohio',
				'Oklahoma',
				'Oregon',
				'Pennsylvania',
				'Rhode Island',
				'South Carolina',
				'South Dakota',
				'Tennessee',
				'Texas',
				'Utah',
				'Vermont',
				'Virginia',
				'Washington',
				'West Virginia',
				'Wisconsin',
				'Wyoming'
			);
			buildlist(list);
		}
		function goworld(){
			resetcity();
			p = '<input id="province" name="province" type="text" maxlength="140" size="40" value="'+settings.provstart+'"/>';
			jQuery("#c1").find("select, input").remove();
			jQuery("#c1").append(p);
			jQuery("#c1").css({ "backgroundColor": "#"+settings.hilite }).removeClass("c-hide").animate(
				{"backgroundColor":"#ffffff"},
				settings.hilitetime
			);
			jQuery("#c2").css({ "backgroundColor": "#"+settings.hilite }).removeClass("c-hide").animate(
				{"backgroundColor":"#ffffff"},
				settings.hilitetime
			);
			bindprov();
		}
		function buildlist(cities){
			len = cities.length;
			s = '<option value="0">Select your '+ps+'</option>';
			for(i=0; i<len;i++){
				if(settings.provstart.toLowerCase() == cities[i].toLowerCase())
					s += '<option value="'+cities[i]+'" selected="selected">'+cities[i]+'</option>';
				else
					s += '<option value="'+cities[i]+'">'+cities[i]+'</option>';
			}
			s = '<select id="province" name="province">'+s+'</select>';
			jQuery("#c1").find("select, input").remove();
			jQuery("#c1").append(s);
			jQuery("#c1").css({ "backgroundColor": "#"+settings.hilite }).removeClass("c-hide").animate(
				{"backgroundColor":"#ffffff"},
				settings.hilitetime
			);
			jQuery("#province").bind("change", function(){
				if( jQuery("#c2").hasClass("c-hide") ){
					jQuery("#c2").css({ "backgroundColor": "#"+settings.hilite }).removeClass("c-hide").animate(
						{"backgroundColor":"#ffffff"},
						settings.hilitetime
					);
				}
			});
		}
		function bindprov(){
			prov = jQuery("#province");
			prov.bind("focus", function(){
				proval = trim(prov.val());
				if(proval == settings.provstart){
					prov.val("");	
				}
			});
			prov.bind("blur", function(){
				proval = trim(prov.val());
				if(proval == ""){
					prov.val(settings.provstart);
				}
			});
		}
		function bindcities(){
			city = jQuery("#city");
			jQuery("#city").bind("focus", function(){
				cityval = trim(city.val());
				if(cityval == settings.citystart){
					city.val("");
				}
			});
			jQuery("#city").bind("blur", function(){
				cityval = trim(city.val());
				if(cityval == ""){
					city.val(settings.citystart);
				}
			});
		}
		function trim(str, chars) {
			return ltrim(rtrim(str, chars), chars);
		}
		function ltrim(str, chars) {
			chars = chars || "\\s";
			return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
		}
		function rtrim(str, chars) {
			chars = chars || "\\s";
			return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
		}
		jQuery("#tadd-reg-btn").click(function(){
			if(jQuery("#user_email").val() != jQuery("#re_user_email").val()){
				jQuery("#user_email").parent().addClass("form-invalid").find("label").after("<em>Password does not match</em>");
				jQuery("#re_user_email").parent().addClass("form-invalid").find("label").after("<em>Password does not match</em>");
				return false;
			}
			if(!checkcity()){
				bindprov();
				jQuery("#c1").removeClass("c-hide");
				jQuery("#c2").removeClass("c-hide");
				jQuery("#c2").addClass("form-invalid").find("label").after("<em>City is Required</em>");
				return false;
			}
		});
		function checkcity(){
			if(jQuery("#city").val() == settings.citystart){
				return false;
			} else {
				return true;
			}
		}
		jQuery("#how_other_select").click(function(){
			if(jQuery(this).is(':checked')){
				jQuery(this).next("span").hide();
				jQuery(this).parent().find("#how_other").css({"display":"inline"});
				bindother();
			} else {
				jQuery(this).next("span").css({"display":"inline"});
				jQuery(this).parent().find("#how_other").css({"display":"none"});
				jQuery("#how_other").unbind("focus blur");
			}
		});
		function bindother(){
			other = jQuery("#how_other");
			jQuery("#how_other").bind("focus", function(){
				otherval = trim(other.val());
				if(otherval == othertext){
					other.val("");
				}
			});
			jQuery("#how_other").bind("blur", function(){
				otherval = trim(other.val());
				if(otherval == ""){
					other.val(othertext);
				}
			});
		}
	});
};
function winOpen(legal) {
	pageurl="http://totallyadd.com/forum/my-templates/totallyadd/legal.php?doc="+legal;
	window.open(pageurl,'','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=600,height=400,left=50,top=200');
}
jQuery(function(){
	jQuery("#reg-terms").click(function(){
		winOpen("terms");
		return false;
	});
	jQuery("#reg-privacy").click(function(){
		winOpen("privacy");
		return false;
	});
});
