﻿function initializeRegistrationForm() {
	/* Grey out disabled heading options */
	SetDropdownHeadingStyles();
}

function initializeMagazineSubscribeForm() {
	/* Grey out disabled heading options */
	SetDropdownHeadingStyles();

	/* Set initial focus to first element in form */
	$("input.address1").focus();

	/* Autocomplete city and state fields for users in U.S. with valid zipcode */
	$("input.zipcode").blur(function() {
		if ( $("select.country").val() == 0 && $("input.zipcode").val() != "") {
			$.getJSON("http://www.geonames.org/postalCodeLookupJSON?&country=US&callback=?", {postalcode: this.value }, function(response) {
				if (response && response.postalcodes.length && response.postalcodes[0].placeName && response.postalcodes[0].adminName1) {
					$("select.state option").each(function(e){
						if ($(this).text() == response.postalcodes[0].adminName1) {
							$("select.state").val(response.postalcodes[0].adminName1);
							ValidatorValidate($("span[rel=state]")[0]);
						}
					});
					
					$("input.city").val(response.postalcodes[0].placeName);
					ValidatorValidate($("span[rel=city]")[0]);
				}
			});
		}
	});
	
	/* Set Initial Form state */	
	UpdateFormElements();

	/* Toggle validators when country is changed */
	$("select.country").change( function(e){
		UpdateFormElements();
	});
	
	HighlightValidationErrors()
}
	
function SetDropdownHeadingStyles() {
	$("option[value^='-']").css({"color" : "#666666"});
}

function UpdateFormElements() {
	switch($("select.country").val()) {
			case '-1':
			  /*SetUsFormElements();*/
			  break;
			case '0':
			  SetUsFormElements();
			  break;
			case '32':
			  SetCanadaFormElements();
			  break;
			default:
			  SetInternationalFormElements();
			}
}


function SetInternationalFormElements(){
	/* Disable Phone Validators */
	$("span[rel=phone]").each( function(i) {
		ValidatorEnable(this, false);
	});
		
	/* Disable State Validators */
	$("span[rel=state]").each( function(i) {
		ValidatorEnable(this, false);
	});
	
	/* Disable Zipcode Validators */
	$("span[rel=zipcode]").each( function(i) {
		ValidatorEnable(this, false);
	});
	
	/* Disable State Field */
	$("select.state").hide();
	$("select.state").val("-1");

	
	/* Enable Province Field */
	$("input.province").show();
	$("span.state").text("Province");
}

function SetCanadaFormElements(){
	SetInternationalFormElements();
	
    /* Enable Zipcode Validators */
	$("span[rel=zipcode]").each( function(i) {
		ValidatorEnable(this, true);
	});
}

function SetUsFormElements(){
	/* Enable Phone Validators */
	$("span[rel=phone]").each( function(i) {
		ValidatorEnable(this, true);
	});

    /* Enable State Validators */
	$("span[rel=state]").each( function(i) {
		ValidatorEnable(this, true);
	});
	
	/* Enable Zipcode Validators */
	$("span[rel=zipcode]").each( function(i) {
		ValidatorEnable(this, true);
	});
	
	/* Enable State Field */
	$("select.state").show();
	$("span.state").text("State");
	
	/* Disable Province Field */
	$("input.province").hide();
}

function HighlightValidationErrors() {
	$("span.validation-error")   
		.bind("DOMAttrModified propertychange", function(e) {   
			// Exit early if IE because it throws this event lots more   
			if (e.originalEvent.propertyName && e.originalEvent.propertyName != "isvalid") return;   
			
			var controlToValidate = $("#" + this.controltovalidate);   
			var validators = controlToValidate.attr("Validators");   
			if (validators == null) return;   

			var isValid = true;   
			$(validators).each(function() {   
				if (this.isvalid !== true) {   
					isValid = false;   
				}   
			});   

			if (isValid) {   
				controlToValidate.parent("li").removeClass("error");
					
			} else {   
				controlToValidate.parent("li").addClass("error");
			}   

		});   
}

function SetEnterKeyAction (textbox, button) {
	$("#" + textbox + "").keydown( function(e) {
        var code = (e.keyCode ? e.keyCode : e.which); 
        if(code == 13) {
            $("#" + button + "").click();
            return false;
        } 
    });
}
