jQuery.noConflict();

jQuery(document).ready(function($){
	
	$('input[name="email"]').keyup(function() {	
		
		var email = $(this).attr('value');

		if(email == '') {
			$(this).prev('.formalert').animate({
				opacity: '0'
				}, 200, "swing", function() {
					$(this).remove();
				}
			);
		}

		if(!isValidEmailAddress(email)) {
			if($(this).prev().attr('class') != 'formalert') {
				$(this).before('<div class="formalert">This e-mail address is not valid.</div>');
				$(this).prev('.formalert').animate({
					opacity: '1'
					}, 200, "swing"
				);
			}
		} else {
			$(this).prev('.formalert').animate({
				opacity: '0'
				}, 200, "swing", function() {
					$(this).remove();	
				}
			);
		}			
	});	
	
	$('input[rel="error_check"], textarea').focus(function() {
		if($(this).prev().attr("class") == "formalert") {
			$(this).prev().animate({
				opacity: '0'
				}, 200, "swing", function() {
					$(this).remove();
				}
			);
		}
		
		var inputCheck = $(this).attr("value");
		
		if(inputCheck == $(this).attr("defaultfield"))	{							
			$(this).attr("value", "");
		}
	});								 
	
	$('.enabled').live('click', function() {	
		var name = $('input[name="name"]').attr('value');
		var email = $('input[name="email"]').attr('value');
		var title = $('input[name="title"]').attr('value');
		var code = $('input[name="code"]').attr('value');
		var details = $('textarea[name="details"]').attr('value');


		var result = '';
		
		$.ajax({
			type: "POST",
			url: "/php/quotemail.php",
			dataType: "text",
			success: function(result) {
				$('.submitting').fadeOut(300, function() {
					$(this).html(result);
					$(this).css('display', 'block');
					$(this).animate({
						opacity: '1'
					}, 300, "swing");
				});
			},
			error: function() {
				$('.submitting').fadeOut(300, function() {
					$('.ajax_error').css('display', 'block');					
					$('.ajax_error').animate({
						opacity: '1'
					}, 300, "swing");
				});
				$('.ajax_error');
			},
			data: "email=" + email + "&name=" + name + "&title=" + title + "&code=" + code + "&details=" + details,
			beforeSend: function() {
				$('.error_window').css('display', 'block');
				$('.error_window').css('opacity', '1');
				$('#contact_form').animate({
						opacity: '.2'
					}, 300, "swing", function() { 
					$('.submitting').css('display', 'block');
					$('.submitting').animate({
						opacity: '1'
					}, 300, "swing");
				});
			}
		});
	});
	
	$('.error_window').live('click', function() {
		$(this).animate({
			opacity: 0
		}, 300, "swing", function() {
			$(this).css('display', 'none');
			$('.submitting').text('Submitting...');
		});
		$('#contact_form').animate({
			opacity: 1
		});
		
		document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); 
		
	});	
	
	$('input[rel="error_check"], textarea').blur(function() {	
			
		if($(this).attr("value") == "") {
			
			if($(this).prev().attr('class') == 'formalert') {
				$(this).prev().animate({
					opacity: '0'
					}, 200, "swing", function() {
						$(this).remove();
					}
				);
			}
			
			$(this).before('<div class="formalert">This field may not be left blank.</div>');
			$(this).prev('.formalert').animate({
				opacity: '1'
				}, 200, "swing"
			);
		}
		
		var errorcount = $('.formalert').size();
		var formsfilled = 'yes';
		
		$('input[rel="error_check"], textarea').each( function() {
			var thisvalue = $(this).attr('value');
			
			if(thisvalue == "") {
				formsfilled = 'no';
			}
		}); 
		
		if(errorcount == 0) {
			if(formsfilled == 'yes') {
				$('#submit').attr('class', 'enabled');
				$('#submit').attr('value', 'Send away!');		
			} 

		} else {
			$('#submit').attr('class', 'disabled');
			$('#submit').attr('value', 'Please fill out the form correctly to continue.');
		}
	});
		
	$("p.year").click(function () {
		if ($(this).next(".months").is(":hidden")) {
			$(this).next(".months").slideDown("fast");
		} else {
			$(this).next(".months").slideUp("fast");
		}
	});
	
	if(!Get_Cookie("adbox")) {
	  $("#adbox").show();
	}
		
	$(".closeadbox").click(function () {
	  $("#adbox").slideUp();
	  Set_Cookie( 'adbox', 'closed', '1', '/', '', '' );
	});
		
	$(".answer").each(function() {
		$(this).css("height", $(this).height());
	});
	
	$(".answer").hide();
	
	$(".question").click(function () {
		if ($(this).next(".answer").is(":hidden")) {
			$(this).next(".answer").slideDown("slow");
			$(this).children('span').text('-');								   
		} else {
			$(this).next(".answer").slideUp("slow");
			$(this).children('span').text('+');								   			
		}
	});
	
	$('a[rel="lightbox"]').lightBox();	

});

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
			
// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
  if ( Get_Cookie( name ) ) document.cookie = name + "=" +
    ( ( path ) ? ";path=" + path : "") +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}