// Fix background image flickering in IE
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) { }

/* Special search behavior */
var Search = {
	prompt: "Search", // default value - should get real value from markup
	init: function() {
		Search.prompt = jQuery('#search').focus(Search.focus).blur(Search.blur).val();
	},
	focus: function() { if (this.value == Search.prompt) { this.value = ''; } },
	blur: function() { if (this.value.length == 0) { this.value = Search.prompt; } }
};
$(document).ready( Search.init );

var DD_belatedPNG;
if (DD_belatedPNG) { // Only execute for IE6
	// Selectors need to match what's in the CSS exactly
	DD_belatedPNG.fix('img');
}

// Convert un-tabbed display into tabbed display and activate tabs
var ANTabs = {
	init: function() {
		// Only run if we have tabs
		if (! $.fn.tabs) return;

		var $tabs = $('div.tabs');
		if ($tabs.length == 0) return;

		var $tabnav = $('<ul class="tabNav selfClear"></ul>');
		$tabs.prepend($tabnav);
		$tabs	.find('> div')
					.each( function(i) {
						var id = this.id;
						// Descending search for header values
						var $header = $(this).find('h2:first');
						if ($header.length == 0)
							$header = $(this).find('h3:first');
						if ($header.length == 0)
							$header = $(this).find('h4:first');
						// Use title value if available
						var title = $header.attr('title') || $header.html();

						$tabnav.append('<li id="' + id + 'Tab"><a href="#' + id + '" class="noPopup"><span>' + title + '</span></a></li>');
						$header.remove();
					});
		// Need delay for browsers to get their DOM in order
		setTimeout(function() {
			$tabs.tabs({
				// Prevent outline on selection
				select: function(event, ui) {
					$(ui.tab).blur();
				}
			});
			$tabs.find('.hideOnLoad').removeClass('hideOnLoad');
		}, 500);
	}
};
$(document).ready(ANTabs.init);

$(document).ready(function() {
	$('a[href="#todo"]').addClass('todo');
});

var ButtonNav = {
	init: function() {
		var $$ = $('ul.buttonNav');
		if ($$.length == 0) return;
		
		if ($$.find('li.active').length == 0) {
			ButtonNav.activateButton($$.find('li:eq(0)'));
		}
		$$.find('a').click(function(e) {
			e.preventDefault();
			ButtonNav.activateButton($(this).blur().parent());
		});
	},
	activateButton: function($li) {
		$li.siblings('.active').removeClass('active');
		var href = $li.addClass('active').find('a').attr('href');
		$('#PanelContent').load(href, function(data) {
			$('#PanelContent').find('tr:odd').addClass('alt');
		});
	}
};
$(document).ready(ButtonNav.init);

var SubmitRequest = {
	init: function() {
		var $$ = $('#SubmitRequest');
		if ($$.length == 0) return;
		
		$$.find('.button').click(SubmitRequest.buttonClicked);
	},
	buttonClicked: function(e) {
		var name = $("input#name").val();
		var email = $("input#email").val();
		var phone = $("input#phone").val();
		var req = $("select#req").val();
		var comments = $("textarea#comments").val();
		var captcha = $("input#captcha").val();
		var formdata = 'name=' + name + '&email=' + email + '&phone=' + phone + '&req=' + req + '&captcha=' + captcha + '&comments=' + comments;

		var formval = 0;
		$('.error').hide();
		if (phone == "") { $("label#phone_error").show(); $("input#phone").focus(); formval=1; }
		if (email == "") { $("label#email_error").show(); $("input#email").focus(); formval=1; }
		if (!IsEmail(email)) { $("label#email_error").show(); $("input#email").focus(); formval=1; }
		if (name == "") { $("label#name_error").show(); $("input#name").focus(); formval=1; }



		if (formval == 1) { return false; }

		$.ajax({
  			type: "POST", url: "/cgi-bin/request.pl", data: formdata, success: function() {
  				$('#sent').html("<h2>Your request has been sent successfully.</h2>")
  				.append("<br>Only some fields have been reset, to ease further submissions")
  				.hide()
  				.fadeIn(1500, function() {
    				$('#message').append(".");
   				});
				$("select#req").val(0);
				$("textarea#comments").val("");
  			}, error: function() {
 				alert("There was an error submitting your form. Please send email to scan-admin@coverity.com and mention this error.");
			}
		});
		return false;
	}
};
$(document).ready(SubmitRequest.init);

function IsEmail(email) {
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (regex.test(email)) return true;
	else return false;
}  

/* begin - open new window for external links and pdfs */
$(function() {
	$('a[href^=http://]').not($('a.noPopup')).popupwindow(); // Fully qualified links
	$('a[href^=https://]').not($('a.noPopup')).popupwindow(); // Secure HTTP connection links
	$('a[href$=.pdf]:not([href^=http://])').popupwindow(); // PDFs by URL
	$('a.popup').popupwindow(); // specific links
});
/* end - open new window for external links and pdfs */

