$(document).ready(function () {
	$(document).pngFix(); 
	
	$("form input").each(function () {
		// settings for initial suggested values
		if ($(this).hasClass("text")) {
		
			if ($(this).val() == $(this).attr("alt")) {
				$(this).css("color", "#666666"); // fade the color to indicate user input required
			}
			
			// on focus, if the value has not changed, clear the text box
			$(this).focus(function () {
				if ($(this).val() == $(this).attr("alt") && $(this).attr("alt") != "") {
					$(this).val("");
					$(this).css("color", "#000000");
				}
			// on blur, if the value is empty, replace with the initial value
			}).blur(function () {
				if ($(this).val() == "" && $(this).attr("alt") != "") {
					$(this).val($(this).attr("alt"));
					$(this).css("color", "#666666");
				}
			});
		}
	});
	
	$(".promo .newsitem").hide();
	$(".promo .newsitem:first").show();
	$("#moreNews a").click(function () {
		$("#moreNews a").removeClass("active");
		
		$(".promo .newsitem").hide();
		$(".promo .newsitem:eq(" + $("#moreNews a").index(this) + ")").show();
		
		$(this).addClass("active");
		return false;
	});
});
	
// validate the contact form
function checkContactForm(objForm) {
	var submitForm = true;
	var contactName = $(objForm).find("#contactName");
	var contactEmail = $(objForm).find("#contactEmail");
	var contactPhone = $(objForm).find("#contactPhone");
	var contactSource = $(objForm).find("#contactSource");
	var contactText = $(objForm).find("#contactText");
	var contactNewsletter = $(objForm).find("#contactNewsletter");
	
	var emailreg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	var phonereg = /^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/;
	
	// check each field against a number of criteria
	// make the field yellow if there is an error, white otherwise
	if (contactName == "" || contactName.val() == contactName.attr("alt")) {
		contactName.css("background", "url(images/textbox_bg_h.jpg) no-repeat");
		submitForm = false;
	} else {
		contactName.css("background", "url(images/textbox_bg.jpg) no-repeat");
	}
	if (contactEmail == "" || contactEmail.val() == contactEmail.attr("alt") || contactEmail.val().match(emailreg) == null) {
		contactEmail.css("background", "url(images/textbox_bg_h.jpg) no-repeat");
		submitForm = false;
	} else {
		contactEmail.css("background", "url(images/textbox_bg.jpg) no-repeat");
	}
	if (contactPhone == "" || contactPhone.val() == contactPhone.attr("alt") || contactPhone.val().match(phonereg) == null) {
		contactPhone.css("background", "url(images/textbox_bg_h.jpg) no-repeat");
		submitForm = false;
	} else {
		contactPhone.css("background", "url(images/textbox_bg.jpg) no-repeat");
	}
	if (contactSource.val() == "") {
		contactSource.css("background", "#ffff66");
		submitForm = false;
	} else {
		contactSource.css("background", "url(images/textbox_bg.jpg) no-repeat");
	}
	if (contactText.val() == "") {
		contactText.css("background", "url(images/textbox_bg_h.jpg) no-repeat");
		submitForm = false;
	} else {
		contactText.css("background", "url(images/textbox_bg.jpg) no-repeat");
	}
	
	// collect all of the data and submit to an AJAX call
	// provide a notification upon completion
	if (submitForm) {
		var dataString = 'f=cn&contactName=' + contactName.val() + '&contactEmail=' + contactEmail.val() + '&contactPhone=' + contactPhone.val() + '&contactSource=' + contactSource.val() + '&contactText=' + contactText.val() + '&contactNewsletter=';
		if (contactNewsletter.is(":checked")) {
			dataString += contactNewsletter.val();
		} else {
			dataString += 'No';
		}
		//alert(dataString);
		
		$.ajax({
			type: "GET",
			url: "contact.php",
			data: dataString,
			success: function(data) {
				var json = eval( "(" + $(data).filter(".results").html() + ")" );
				
				// if an error is returned, highlight error fields in yellow
				if (json.ERROR) {
					for (i = 0; i < json.ERROR.length - 1; i++) {
						$("#" + json.ERROR[i]).css("background", "url(images/textbox_bg_h.jpg) no-repeat");
					}
					$("#contactWrap p.notice").html(json.ERROR[i]);
				}
				
				// if success, change form to success message
				if (json.SUCCESS) {
					$("#contactWrap").html(json.SUCCESS[0]);
				}
				
				// if mail failure, notify user and allow for retry
				if (json.FAIL) {
					$("#contactWrap p.notice").html(json.FAIL[0]);
					$("a#retry").click(function () {
						checkThisForm($("#contactForm"));
					});
				}
			}
		});
		
		return false;
	}
	
	// alert the user for errors
	if (!submitForm) {
		alert("Please correct the fields highlighted in yellow.");
	}
	
	// if there have been no errors, this will be true
	return submitForm;
};


// validate the newsletter form
function checkNewsletterForm(objForm) {
	var submitForm = true;
	var newsletterEmail = $(objForm).find("#newsletterEmail");
	
	var emailreg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	
	// check each field against a number of criteria
	// make the field yellow if there is an error, white otherwise
	if (newsletterEmail == "" || newsletterEmail.val() == newsletterEmail.attr("alt") || newsletterEmail.val().match(emailreg) == null) {
		newsletterEmail.css("background", "url(images/textbox_bg_h.jpg) no-repeat");
		submitForm = false;
	} else {
		newsletterEmail.css("background", "url(images/textbox_bg.jpg) no-repeat");
	}
	
	// collect all of the data and submit to an AJAX call
	// provide a notification upon completion
	if (submitForm) {
		var dataString = 'f=nl&newsletterEmail=' + newsletterEmail.val();
		//alert(dataString);
		
		$.ajax({
			type: "GET",
			url: "contact.php",
			data: dataString,
			success: function(data) {
				var json = eval( "(" + $(data).filter(".results").html() + ")" );
				
				// if an error is returned, highlight error fields in yellow
				if (json.ERROR) {
					for (i = 0; i < json.ERROR.length - 1; i++) {
						$("#" + json.ERROR[i]).css("background", "url(images/textbox_bg_h.jpg) no-repeat");
					}
					$("#newsletterForm p label").html(json.ERROR[i]);
				}
				
				// if success, change form to success message
				if (json.SUCCESS) {
					$("#newsletterForm p label").html(json.SUCCESS[0]);
				}
				
				// if mail failure, notify user and allow for retry
				if (json.FAIL) {
					$("#newsletterForm p label").html(json.FAIL[0]);
					$("a#retry").click(function () {
						checkNewsletterForm($("#newsletterForm"));
					});
				}
			}
		});
		
		return false;
	}
	
	// alert the user for errors
	if (!submitForm) {
		alert("Please enter a correct email address.");
	}
	
	// if there have been no errors, this will be true
	return submitForm;
};


//Popup mailing list form
function popup(){
	if(document.cookie.indexOf('diveshop') == -1){
		var NewWin = window.open('MailingListForm.htm', 'NewWin', 'height=150,width=300,scrollbars=0,top=' + parseInt((screen.availHeight/2) - 75) + ',left=' + parseInt((screen.availWidth/2) - 150));
		if(NewWin){
			NewWin.focus();
			document.cookie = "name=diveshop;value=true";
		}
	}
}
function popupClicked(){
	var NewWin = window.open('MailingListForm.htm', 'NewWin', 'height=150,width=300,scrollbars=0,top=' + parseInt((screen.availHeight/2) - 75) + ',left=' + parseInt((screen.availWidth/2) - 150));
	NewWin.focus();
}