﻿function initializeRegistrationForm() {
	/* Grey out disabled heading options */
	SetDropdownHeadingStyles();
	
	HighlightValidationErrors();
	
	/* Set initial focus to first element in form */
	$("input.firstname").focus();
	
	/*
	$("li.email input").blur( function() {
		EmailAddressInUse();
	});
	*/
	
}

function initializeMagazineSubscribeForm() {
	/* Grey out disabled heading options */
	SetDropdownHeadingStyles();

	/* Set initial focus to first element in form */
	$("input.address1").focus();
	
	/* Validate Magazine Format each time a checkbox is changed. */
	$("div.formats input:checkbox").click(function (e) {
		ValidatorValidate($("span[rel=format]")[0]);
	});

	/* 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 initializeWebinarWhitepaperSubscribeForm() {
	/* Grey out disabled heading options */
	SetDropdownHeadingStyles();
	
    	/* Set Initial Form state */	
	UpdateCustomRegFormElements();

	/* Toggle validators when country is changed */
	$("select.country").change( function(e){
		UpdateCustomRegFormElements();
	});
	
	/* 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]);
				}
			});
		}
	});
}

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");
	
	/* Disable Print Format and check, hide U.S. only messages */
	$("span.print input").attr("disabled", "true");
	$("span.both input").attr("disabled", "true");
	$("span.digital input").attr("checked", "true");
	$("p.printformatavailability").css("visibility","visible");
}

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();
	
	/* Enable Print Format, show U.S. only message */
	$("span.print input").removeAttr("disabled");
	$("span.both input").removeAttr("disabled");
	$("span.both").css("display", "inline");
	$("p.printformatavailability").css("visibility","hidden");
}	

function UpdateCustomRegFormElements() {
	switch($("select.country").val()) {
			case '-1':
			  /*SetUsCustomRegFormElements();*/
			  break;
			case '0':
			  SetUsCustomRegFormElements();
			  break;
			case '32':
			  SetCanadaCustomRegFormElements();
			  break;
			default:
			  SetInternationalCustomRegFormElements();
			}
}

function SetInternationalCustomRegFormElements(){
	/* Disable Phone Validators */
	$("span[rel=phoneUS]").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 SetCanadaCustomRegFormElements(){
	SetInternationalCustomRegFormElements();
	
    /* Enable Zipcode Validators */
	$("span[rel=zipcode]").each( function(i) {
		ValidatorEnable(this, true);
	});
}

function SetUsCustomRegFormElements(){
	/* Enable Phone Validators */
	$("span[rel=phoneUS]").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();
}	


/* Custom Validator Logic to make sure at least one magazine format is checked. 
function MagazineFormatValidator(sender, args) {
	args.IsValid = false;
	$("div.formats").find(":checkbox").each(function() {
		if ($(this).attr("checked")) {
			args.IsValid = true;
			return;
		}
	});
}*/

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 SetDropdownHeadingStyles() {
	$("option[value^='-']").css({"color" : "#666666"});
}

/*
function EmailAddressInUse() {
	EmailAddress = $("li.email input").val();
	
	if (EmailAddress != "") {
		$.ajax({
			type: "POST",
			url: "Register.aspx/EmailAddressInUse",
			data: "{'EmailAddress':'" + EmailAddress  + "'}",
			contentType: "application/json; charset=utf-8",
			dataType: "json",
			success: function(data, textStatus) {
				if (data) {
					$("#EmailAddressInUse").show();
				} else {
					$("#EmailAddressInUse").hide();
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				
			}
		});
	}
}
*/

