(function() {
var leaveSite = true;

function isBlank(str){
    return (typeof(str) == 'undefined' || str == null || $.trim(str) == '');
}
 
function isEmail(str){
    var re = new RegExp('^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]{2,4}$');
    return (str.search(re) != -1);
}

function isPhone(str){
    var re = new RegExp('^[0-9\\s\\(\\)\\+\\-]{5,}$');
    return (str.search(re) != -1);
}

function isNum(str){
    var re = new RegExp('^[0-9]+$');
    return (str.search(re) != -1);
}

function initZebraTable(id) {
	$(id + ' tbody tr:odd').removeClass('even').addClass('odd');
	$(id + ' tbody tr:even').removeClass('odd').addClass('even');
}

function initContactForm() {
	$('#contactForm').submit(function(event) {
		if (isBlank(this.visitorName.value)) {
			event.preventDefault();
			alert('Please enter your name!');
			this.visitorName.focus();
			return;
		}
		if (isBlank(this.visitorEmail.value)) {
			event.preventDefault();
			alert('Please enter your e-mail address!');
			this.visitorEmail.focus();
			return;
		}
		if (!isEmail(this.visitorEmail.value)) {
			event.preventDefault();
			alert('The e-mail address you entered is not valid!');
			this.visitorEmail.focus();
			return;
		}
		if (isBlank(this.visitorSubject.value)) {
			event.preventDefault();
			alert('Please enter your subject!');
			this.visitorSubject.focus();
			return;
		}
		if (isBlank(this.visitorMessage.value)) {
			event.preventDefault();
			alert('Please enter your message!');
			this.visitorMessage.focus();
			return;
		}
		if (isBlank(this.securityCode.value)) {
			event.preventDefault();
			alert('Please enter security code!');
			this.securityCode.focus();
			return;
		}
	});
}

function initScreenshotList() {
	 $('a[rel*=lightbox]').lightBox({
		'imageLoading': '/images/lightbox/lightbox-ico-loading.gif'
		, 'imageBtnClose': '/images/lightbox/lightbox-btn-close.gif'
		, 'imageBtnPrev': '/images/lightbox/lightbox-btn-prev.gif'
		, 'imageBtnNext': '/images/lightbox/lightbox-btn-next.gif'
		, 'imageBlank': '/images/lightbox/lightbox-blank.gif'
	});
}

function initRegAceLink() {
	$('a[href*=download]').click(function(event) {
		event.preventDefault();
		window.open('/download');
		window.location = 'http://www.regace.com/uninstall';
	});
}

function initDownload() {
	$('#downloadCollector input[name=visitorEmail]').focus(function(event) {
		if ($.trim($(this).val()) == 'email') {
			$(this).val('');
		}
	}).blur(function(event) {
		if ($.trim($(this).val()) == '') {
			$(this).val('email');
		}
	});

	var errorBox = $('<span />').insertAfter('#downloadCollector label');
	$('#downloadCollector').submit(function(event) {
		event.preventDefault();
		if (isBlank(this.visitorEmail.value)) {
			alert('Please enter your e-mail address!');
			this.visitorEmail.focus();
			return;
		}
		if (!isEmail(this.visitorEmail.value)) {
			alert('The e-mail address you entered is not valid!');
			this.visitorEmail.focus();
			return;
		}
		
		var postURL = $(this).attr('action');
		var data = $(this).serialize();
		$.ajax({
			'url': postURL
			, 'type': 'POST'
			, 'data': data
			, 'success': function(msg) {
				if (Number(msg) == 1) {
					$('#downloadCollector').css('display', 'none');
					$('#downloadReady').css('display', 'block');
					leaveSite = false;
				} else {
					errorBox.html(msg).css('display', 'block');
				}
			}
		});
		errorBox.css('display', 'none');
	});
}

function initNewsletter() {
	$('<div />', {
		'id': 'mask'
		, 'css': {
			'opacity': 0.75
		}
	}).insertBefore('#newsletter');
	
	$('#newsletter a').click(function(event) {
		event.preventDefault();
		$('#mask').css('display', 'none');
		$('#newsletter').css('display', 'none');
	});
	
	$('#newsletter input[name=visitorEmail]').focus(function(event) {
		if ($.trim($(this).val()) == 'email') {
			$(this).val('');
		}
	}).blur(function(event) {
		if ($.trim($(this).val()) == '') {
			$(this).val('email');
		}
	});
	
	$('#newsletter form').submit(function(event) {
		if (isBlank(this.visitorEmail.value)) {
			event.preventDefault();
			alert('Please enter your e-mail address!');
			this.visitorEmail.focus();
			return;
		}
		if (!isEmail(this.visitorEmail.value)) {
			event.preventDefault();
			alert('The e-mail address you entered is not valid!');
			this.visitorEmail.focus();
			return;
		}
	});
}

function initLinks() {
	$('a, input').click(function() {
		leaveSite = false;
	});
}

$(document).ready(function() {
	initScreenshotList();
	initContactForm();
	//initRegAceLink();
	initDownload();
	initNewsletter();
	initLinks();
});


window.onbeforeunload = function() {
	if (leaveSite == true && $('.saved').length == 0) {
		leaveSite = false;
		$('#mask').css('display', 'block');
		$('#newsletter').css('display', 'block');
		return 'Thank you for visiting DriverScanner.com. \nPlease stay & leave your emails to receive special offers from us!';
	}
}

})();
