// for every link inside of "products" section set onclick to popup function. If popup does it's job it will return true. If that's the case cancel <a> link 
// href follow functionality.
	function prepareProductPopups()
	{
		// need tests for getElementById
		$(".products a[href*=/models/], .productPopUp").click(function()
			{
				if (popup(this))
					return false; // if popup() was successful cancel default <a href> action
				else
					return true;
			});
	}
	
	function popup(linknode)
	{
		var url = linknode.getAttribute("href");
		
		// keep second attribute empty solves problems with reused popup window remaining behind main window. People have window.focus() disabled.
		if (ED_openWindow(url,'',720,650,'','c'))
			return true;
		return false;
	}


$(document).ready(function() {		 

	// start normal button rollover script
	ED_rollOverEffect.init('rolloverbtn', '-over', ['img','input']);
	prepareProductPopups();
	
	// IE 6 PNG fixes
	if ( $("#optional_word_area img").length ) {$("#optional_word_area img").ifixpng();}
	if ( $("#footer img").length ) {$("#footer img").ifixpng();}
	if ( $(".finishing_tool").length ) {$(".finishing_tool img").ifixpng();}
	
	// Mark external links with external icon
	// Find all a tags with external links, add target _blank (to handle window focus with named window) and add external icon
	$('a').filter(function() { return this.hostname && this.hostname !== location.hostname;	})
		.attr('target', '_blank') /// add target to all external links
		.not(':has(img)').addClass('external');// For all <a> not containing an <img> add class - instead of appeading image -> prevents IE border of image problem
		
	// Open PDF or ppt files in new window
	$('a[href$="PDF"], a[href$="pdf"], a[href$="PPT"], a[href$="ppt"]').attr('target', '_blank');
	
	
	// IF ON THE CONTACT OR THE APPOINTMENTS PAGES, HANDLE CAPTCHA INPUT CONVERSION
	if ( $("body#Contact").length ) {
		// read cookie set by popup menu, in it's email link, if available. Set message field
		var model = null;
		model = $.cookie('model');
		
		// only set message if model has a value
		if (model != null) {
			var coming_soon_message = 'This question is in regards to ' + model + ': ';
			$('#message').text(coming_soon_message);
			
			// clear cookie so next visit to contact page doesn't show message
			$.cookie('model', null, {path: '/'}); // must have path because cookie was set with path
		}
		
		// turn off autocomplete for the CAPTCHA field
		$('#access_code').attr("autocomplete","off");
										
		$('#access_code').keypress(
			function(e)
			{
				var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
				
				// UPPERCASE LETTERS or lowercase
				if ((key >= 65 && key <= 90) || (key >= 97 && key <= 122)) {
					return true;
				}
				
				// check for other keys that have special purposes
				if (
					key == 8 /* backspace */ ||
					key == 9 /* tab */ ||
					key == 13 /* enter */ ||
					key == 35 /* end */ ||
					key == 36 /* home */ ||
					key == 37 /* left */ ||
					key == 39 /* right */ ||
					key == 46 /* del , also '.' */
				)
				{
					return true;
				}
				// for detecting special keys (listed above)
				// IE does not support 'charCode' and ignores them in keypress anyway
				if(typeof e.charCode != "undefined")
				{
					// special keys have 'keyCode' and 'which' the same (e.g. backspace)
					if(e.keyCode == e.which && e.which != 0)
					{
						return true;
					}
					// or keyCode != 0 and 'charCode'/'which' = 0
					else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
					{
						return true;
					}
				}

				return false;
			}
		).keyup(
			function()
			{
				$(this).val( $(this).val().toUpperCase() );
			}
		); // end $('#access_code')
		
		// validate form on keyup and submit
		var validator = $("#sendmail").validate({
			rules: {
				first_name: "required",
				last_name: "required",
				email: {
					required: true,
					email: true
				},
				message: "required"
			},
			messages: {
				first_name: " Please enter your First Name",
				last_name: " Please enter your Last Name",
				email: {
					required: " Please enter a valid email address",
					minlength: " Please enter a valid email address"
				},
				message: " Please enter a message"
			},
			
			// set this class to error-labels to indicate valid fields
			success: function(label) {
				// set &nbsp; as text for IE
				label.html("&nbsp;").addClass("checked");
			}
		});

		
	} // end $("body#Contact").length	
});


