
var customer_logo_number = 1;
var max_customer_logo    = 4;
var next_image = new Image();  // preloader var to cache next image

var do_submit_form = false;

$(document).ready(
	function(){

		// add event handlers
		// be sure to return false so link is not followed
		$('.tal_contact').focus( function (event) { return doClearInput(event); } );
		$('.tal_contact').blur( function (event) { return doRefillInput(event); } );
		
		//setInterval(doChangeCustomerLogo, 6000);
		//$('#customer_logo').fadeOut(2000, function () { doChangeCustomerLogo() } );
		
		$('#tal_contact_request').submit( function (event) { return doCheckContactRequestForm(); } );
		
		// add links to header
		$('#tal_header_logo_link').click( function () { window.location = 'http://www.textalibrarian.com/'; return true; } ).css("cursor", "pointer");
		$('#tal_header_right_link').click( function () { window.location = 'http://www.textalibrarian.com/webinar.php'; return true; } ).css("cursor", "pointer");
		
		// chat popup
		$('a.chat_popup').click( function () { window.open( $(this).attr('href'), 'mychatwindow', 'height=500,width=300,status=1,toolbar=0,location=0,menubar=1,resizable=1,scrollbars=1'); return false; } );
		
		// add open in new window attribute to new_window class links
		$('a.new_window').each( function () { $(this).attr('target', '_blank'); } );

		// screen size
		try {
			var i_w = window.innerWidth;
			var i_h = window.innerHeight;
			if (i_w == null) {
				i_w = document.documentElement.clientWidth;
				i_h = document.documentElement.clientHeight;
			}
			$.post('/ss.php', {iw : i_w, ih : i_h, sw : screen.width, sh : screen.height});
		}
		catch (err) { }


	}
);


function doCheckContactRequestForm() {
	var do_submit_form = 0;
	
	do_submit_form = true;
	
	$('.tal_contact').each( 
		function (i) {
			var item = $(this);
			if ('firstname' == item.attr('name') 
					|| 'lastname' == item.attr('name') 
					|| 'email' == item.attr('name') 
					|| 'phone' == item.attr('name') 
					|| 'university' == item.attr('name')) {
				if (!item.val() 
						|| 'First Name*' == item.val() 
						|| 'Last Name*' == item.val() 
						|| 'Email*' == item.val() 
						|| 'Phone*' == item.val() 
						|| 'Library or School Name*' == item.val()) {
					do_submit_form = false;
				}
			}
		}
	);
	if (!do_submit_form) {
		alert("Please fill out at least your Name, Library, Email, and Phone number");
		return false;
	}
	return do_submit_form;
}

//function doCheckContactRequestForm() {
//	if (do_submit_form) {
//		return true;
//	}
//	else {
//		alert("Please fill out at least your Name, Email, and Phone number");
//		return false;
//	}
//}

function doClearInput(event) {
	var i = $(event.target);
	
	if ('First Name*' == i.val()
			|| 'Last Name*' == i.val()
			|| 'Library or School Name*' == i.val()
			|| 'Title' == i.val()
			|| 'Email*' == i.val()
			|| 'Phone*' == i.val()
			|| 'Notes' == i.val()
		 ) 
	{
		// reset the value of the field
		i.val('');
	}
//	else
//	{
//		i.val('"' + i.attr('name') + '"');
//	}
}



function doRefillInput(event) {
	var i = $(event.target);
	
	if ('' == i.val()) {
		if ('firstname' == i.attr('name')) {
			i.val('First Name*');
		}
		else if ('lastname' == i.attr('name')) {
			i.val('Last Name*');
		}
		else if ('university' == i.attr('name')) {
			i.val('Library or School Name*');
		}
		else if ('title' == i.attr('name')) {
			i.val('Title');
		}
		else if ('email' == i.attr('name')) {
			i.val('Email*');
		}
		else if ('phone' == i.attr('name')) {
			i.val('Phone*');
		}
		else if ('notes' == i.attr('name')) {
			i.val('Notes');
		}
	}
}

function doChangeCustomerLogo() {
	// change the logo, then fade in
	customer_logo_number++;
	if (customer_logo_number > max_customer_logo) {
		customer_logo_number = 1;
	}
	// preload the next image
	$(next_image).attr('src', '/images/images/customer_' + customer_logo_number + '.gif');

	// fade out, then change the logo
	$('#customer_logo').fadeOut(2000, doIncrementLogo);
}

function doIncrementLogo() {
	// swap image src, then fade in
	$('#customer_logo').attr('src', '/images/images/customer_' + customer_logo_number + '.gif');
	$('#customer_logo').fadeIn(2000);
}
