<!--
var page = "";
$(function(){
	$('#quick_contact:not(body #quick_contact)').hide();
	//$('body.request #quick_contact').show();
	$('#contact_more').removeClass('hide');
	if($("a[rel*='info']").length) {
		$("a[rel*='info']").click(function(){
			//page = $("body").hasClass("repair_services") ? unescape(getUrlVar("product",$(this).attr('href'))) : $("#breadcrumbs").text() ;
			//$('#requesttype').text(page);
			$("#quick_contact")
					.append($('<p class="close"><span class="">Close</span></p>'))
					.modal({close: false, onClose: closeModal, onOpen: openModal, persist: false, onShow: onshowModal});
			return false;
		});
	}
		   });

function closeModal (dialog) {
	dialog.data.fadeOut('slow', function () {
		dialog.container.fadeOut('slow', function () {
			dialog.overlay.slideUp('slow', function () {
				$.modal.close();
				$('.close').remove();
			});
		});
	});
}
function openModal (dialog) {
	dialog.overlay.fadeIn('slow', function () {
		dialog.container.fadeIn('slow', function () {
			dialog.data.hide().slideDown('slow');	 
		});
	});
}
function onshowModal (dialog) {
	$(document).keypress(function(e){
	  var key  = (window.event) ?  event.keyCode : e.keyCode;
 	 if( key == 27 ) $.modal.close();
	});
	$("#contact_info").hide();
	if($("#quick_contact").length) {
		$("#quick_contact input:not(.optional), #quick_contact textarea:not(.optional)").blur(function(){
			if ($(this).val() == "") $(this).addClass("form_error"); else $(this).removeClass("form_error");
		});
		$("#quick_contact select[name='contact_how']").change(function(){
			$(this).removeClass("form_error");
			if ($(this).val() == "email") $("label[for='contact']").text("Email Address");
			else if ($(this).val() == "phone") $("label[for='contact']").text("Phone Number");
			else { $(this).addClass("form_error"); $("#contact_info").hide(); return;}
			$("#contact_info").hide().show();
			$("#quick_contact input[name='contact']").val("");
			$('.contact_error').remove();
		});

		$("#quick_contact input[name='contact']").change(function(){
			var self = $("#quick_contact input[name='contact']");
			$('.contact_error').remove();
			if ($("#quick_contact input[name='contact']").val() == "") {
					$(self).addClass("form_error");
			}
			else if ($("#quick_contact label[for='contact']").text() == "Email Address") {
				var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (!regex.test($(self).val())) {
					$(self).addClass("form_error");
					$(self).after($("<p class='contact_error form_error'>Invalid email format. Please correct.</p>"));
				} else {
					$(self).removeClass("form_error");
				}
			} else { //we're not doing any major checks on phone numbers, because of internationalization
				var regex = /^[0-9ext,.\s-()\+]+$/;
				if (!regex.test($(this).val())) {
					$(self).addClass("form_error");
					$(self).after($("<p class='contact_error form_error'>Phone numbers cannot contain letters (with the exception of 'ext'). Please correct.</p>"));
				} else {
					$(self).removeClass("form_error");
				}
			}
		});
		$("#quick_contact button[name='request_send'],#quick_contact button[name='request_send']").click(function(){
			var request = $(this).attr('name')=="request_send" ? "true" : "false";
			$("#quick_contact input:not(.optional), #quick_contact textarea:not(.optional)").each(function(){
				if ($(this).val() == "")	$(this).addClass("form_error"); else $(this).removeClass("form_error");
			});
			if ($("#quick_contact input, #quick_contact textarea").hasClass('form_error')) {
				alert("Please complete all fields.");
			} else { 
					$(this).attr("disabled", "true");
					$("#quick_contact form").append($('<p class="sending">Please wait while we send your message...</p>'));
					name = $("#quick_contact input[name='name']").val();			
					data_string = $("#quick_contact form").serialize();
					$('#quick_contact').fadeTo(500,.5);
					$.ajax({
					   type: "POST",
					   url: "/scripts/sb_contactprocess.php",
					   data: data_string+"&page="+page+"&request="+request+"&ajax=true",
					   async: false,
					   success: function(msg){
						//DEBUG	alert(msg);
						$("#quick_contact h2").after(
							$("<p class='success column span-4 last'>Hello, <strong>"
								+ name	+"</strong>:<br />" + 
								"Thank you for your interest. You'll hear back from us within 1 business day.</p>")); 
						$('#quick_contact').fadeTo(500,1);
					   },
					   error: function(obj,text,error) {
						$("#quick_contact h2").after(
							$("<p class='error column span-4 last'>Hello, <strong>"
								+ name	+ "</strong>:<br />" + 
								"Something went wrong when we tried to send your message. You can try again later "
								+ "or contact us another way: by phone at 1.800.775.3824 or by e-mail at "
								+ "<a href='info@img-electromark.com'>IMG</a>.</p>")); 
						//alert(obj.responseText);
						$('#quick_contact').fadeTo(500,1);
					   }
					 });
	
					$("#quick_contact form").remove();
			}
			return false; // We don't want to submit the form since AJAX handled it
		});
	}
}
function getUrlVar(varname, link){  var href = link ? link : window.location.href; 
	varname = varname.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&]"+varname+"=([^&#]*)"; 
	var regex = new RegExp(regexS); var results = regex.exec(href); 
	return results == null ? "" : results[1];
}
