
function validateDownloadForm() {
	var fname = document.getElementById("fname").value;
	var lname = document.getElementById("lname").value;
	var business = document.getElementById("business").value;
	var email = document.getElementById("email").value;

	if(!fname || fname.length == 0 ||
			!lname || lname.length == 0 ||
			!business || business.length == 0 ||
			!email || email.length == 0
	) {
		return false;
	}
	return true;
}

function validateMailForm() {
	var address = document.getElementById("address").value;
	var city = document.getElementById("city").value;
	var state = document.getElementById("state").value;
	var country = document.getElementById("country").value;
	if(	!validateDownloadForm() ||
			!address || address.length == 0 ||
			!city || city.length == 0 ||
			!state || state.length == 0 ||
			!country || country.length == 0
	) {
		return false;
	}
	return true;
}

function checkEmail(email) {
	if(
		email.indexOf("@ppg.com") >= 0 ||
		email.indexOf("@akzonobel.com") >= 0 ||
		email.indexOf("@metlac.com") >= 0 ||
		email.indexOf("@grace.com") >= 0 ||
		email.indexOf("@Watsonstandard.com") >= 0 ||
		email.indexOf("@Colormatrix.com") >= 0 ||
		email.indexOf("@Invista.com") >= 0 ||
		email.indexOf("@Honeywell.com") >= 0 ||
		email.indexOf("@Constar.net") >= 0 ||
		email.indexOf("@schekolin.com") >= 0 ||
		email.indexOf("@gruppomgus.com") >= 0 ||
		email.indexOf("@knscompanies.com") >= 0 ||
		email.indexOf("@altana.com") >= 0 ||
		email.indexOf("@Sherwin.com") >= 0 ||
		email.indexOf("@") == -1
	) {
		return false;
	}
	return true;
}

function submitRequest(url, query) {
    http_request = xmlHttpRequestHandler.createXmlHttpRequest();
    try {
        http_request.onreadystatechange = function() {
            if (http_request.readyState == 4) {
                switch (http_request.status) {
                    case 200: // Success
                        parseResults(http_request);
                        break;
                    case 204: // Do nothing, no content.
                    case 1223: // Handle IE Bug of converting 204 to 1223
                        break;
                    default: // Error
                        alert("There was a problem with the request.\nStatus code: " +
                            http_request.status);
                }
            }
        }
        http_request.open("POST",url,true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
        http_request.send(encodeURI(query));
    }
    catch (xmlHttpRequestObjectError){
    }
}

function parseResults(response) { }

$(function() {
	// a workaround for a flaw in the demo system (http://dev.jqueryui.com/ticket/4375), ignore!
	$( "#dialog:ui-dialog" ).dialog( "destroy" );

	var downloadButton = function() {
		if(!validateDownloadForm()) {
			alert("All fields are required.");
		} else {
			var email = document.getElementById("email").value;
			if(checkEmail(email)) {
			document.getElementById("downloadContent").style.display="none";
			document.getElementById("thankyou").style.display="block";
			document.getElementById("mailContentThankyou").style.display="none";
			document.getElementById("requiredMessage").style.display="none";
			_gaq.push(['_trackEvent', 'Engagement', 'Downloads - PDF', '/downloadBrochure.html']);
			_gaq.push(['_trackPageview', '/brochure/download/complete.html']);

			$( "#dialog-form" ).dialog( "option", "buttons", {
				"Download": downloadButton,
				"Mail Hard Copy": mailButton
				} );

			document.getElementById("downloadForm").submit();
			} else {
				document.getElementById("downloadContent").style.display="none";
				document.getElementById("accessDenied").style.display="block";
				document.getElementById("requiredMessage").style.display="none";
				_gaq.push(['_trackPageview', '/brochure/download/competitor.html']);
				$( "#dialog-form" ).dialog( "option", "buttons", { } );
			}
		}
	};

	var backToDownloadButton = function() {
		if(!validateDownloadForm()) {
			document.getElementById("mailContent").style.display="none";
			$( "#dialog-form" ).dialog( "option", "height", 400 );
			$( "#dialog-form" ).dialog( "option", "buttons", {
				"Download": downloadButton,
				"Mail Hard Copy": mailButton
			});
		} else {
			var email = document.getElementById("email").value;
			if(checkEmail(email)) {
			document.getElementById("downloadContent").style.display="none";
			document.getElementById("mailContent").style.display="none";
			document.getElementById("thankyou").style.display="block";
			document.getElementById("mailContentThankyou").style.display="none";
			document.getElementById("requiredMessage").style.display="none";
			$( "#dialog-form" ).dialog( "option", "height", 400 );
			_gaq.push(['_trackEvent', 'Engagement', 'Downloads - PDF', '/downloadBrochure.html']);
			_gaq.push(['_trackPageview', '/brochure/download/complete.html']);

			$( "#dialog-form" ).dialog( "option", "buttons", {
				"Download": backToDownloadButton
				} );

			document.getElementById("downloadForm").submit();
			} else {
				document.getElementById("downloadContent").style.display="none";
				document.getElementById("accessDenied").style.display="block";
				document.getElementById("mailContent").style.display="none";
				document.getElementById("requiredMessage").style.display="none";
				$( "#dialog-form" ).dialog( "option", "height", 400 );
				_gaq.push(['_trackPageview', '/brochure/download/competitor.html']);
				$( "#dialog-form" ).dialog( "option", "buttons", { } );
			}
		}
	};

	var submitButton = function() {
		if(!validateMailForm()) {
			alert("All fields are required.");
		} else {
			document.getElementById("downloadContent").style.display="none";
			document.getElementById("mailContent").style.display="none";
			document.getElementById("requiredMessage").style.display="none";
			document.getElementById("thankyou").style.display="none";
			document.getElementById("mailContentThankyou").style.display="block";
			$( "#dialog-form" ).dialog( "option", "height", 400 );
			_gaq.push(['_trackPageview', '/brochure/hard_copy/complete.html']);
			var fname = document.getElementById("fname").value;
			var lname = document.getElementById("lname").value;
			var business = document.getElementById("business").value;
			var email = document.getElementById("email").value;
			var address = document.getElementById("address").value;
			var city = document.getElementById("city").value;
			var state = document.getElementById("state").value;
			var country = document.getElementById("country").value;
			if(checkEmail(email)) {
			$( "#dialog-form" ).dialog( "option", "buttons", {
				"Download": backToDownloadButton
			});
			submitRequest(context+"/downloadBrochure.html","fname="+fname+"&lname="+lname+"&business="+business+"&email="+email+"&address="+address+"&city="+city+"&state="+state+"&country="+country+"&shouldMail="+"true");
			} else {
				document.getElementById("downloadContent").style.display="none";
				document.getElementById("accessDenied").style.display="block";
				document.getElementById("requiredMessage").style.display="none";
				$( "#dialog-form" ).dialog( "option", "height", 400 );
				_gaq.push(['_trackPageview', '/brochure/download/competitor.html']);
				$( "#dialog-form" ).dialog( "option", "buttons", { } );
			}
		}
	};

	var mailButton = function() {
		document.getElementById("mailContent").style.display="block";
		document.getElementById("downloadContent").style.display="block";
		document.getElementById("thankyou").style.display="none";
		document.getElementById("mailContentThankyou").style.display="none";
		document.getElementById("requiredMessage").style.display="block";
		$( "#dialog-form" ).dialog( "option", "height", 620 );
		$( "#dialog-form" ).dialog( "option", "buttons", {
			"Download" : backToDownloadButton,
			"Submit" : submitButton
			} );
	};

	$( "#dialog-form" ).dialog({
		autoOpen: false,
		height: 400,
		width: 350,
		modal: true,
		buttons: {
			"Download": downloadButton,
			"Mail Hard Copy": mailButton
		},
		close: function() {
			document.getElementById("mailContent").style.display="none";
			document.getElementById("downloadContent").style.display="block";
			document.getElementById("thankyou").style.display="none";
			document.getElementById("mailContentThankyou").style.display="none";
			document.getElementById("requiredMessage").style.display="block";
			document.getElementById("accessDenied").style.display="none";
			$( "#dialog-form" ).dialog( "option", "height", 400 );
			$( "#dialog-form" ).dialog( "option", "buttons", {
				"Download": downloadButton,
				"Mail Hard Copy": mailButton
				} );
		}
	});

	$( "#download" )
		.click(function() {
			$( "#dialog-form" ).dialog( "open" );
			_gaq.push(['_trackPageview', '/brochure/download/start.html']);
		});
});
