﻿$(document).ready(function() {
	
	$('.contact_form').hide();
	$('div#loading').hide();	
	
	 $("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)  
  
    $("ul.topnav>li").hover(function() { //When trigger is clicked...  
  		
        //Following events are applied to the subnav itself (moving subnav up and down)  
        $(this).find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
  
        $(this).hover(function() {
        }, function(){  
            $(this).find("ul.subnav").slideUp('fast'); //When the mouse hovers out of the subnav, move it back up  
        });  
  
        //Following events are applied to the trigger (Hover events for the trigger)  
        }).hover(function() {  
            $(this).addClass("subhover"); //On hover over, add class "subhover"  
        }, function(){  //On Hover Out  
            $(this).removeClass("subhover"); //On hover out, remove class "subhover"  
	}); 
	
	//Finance Calculator
	$('td.err').html('');
	
	// Error Handling With Finance Form
	$('td.field input').blur(function() {
		var x = $(this).attr('name');
		var e = $(this).attr('title');
		if($(this).val() == "") {
			$('td#" + x + "Err').html(e);
		}
		else {
			$('td#" + x + "Err').html('');
		}
	});
	
	/* Mailer */
	$('form#messenger').submit(function() { 

		$('li#li_err').html("");
		$('li#li_err').css({'color' : 'red', 'font-weight' : 'bold'});
		
			$('input#name').css('border', '1px solid #d6d6d6');
			$('input#email').css('border', '1px solid #d6d6d6');
			$('input#phone').css('border', '1px solid #d6d6d6');
			$('input#part').css('border', '1px solid #d6d6d6');
			$('textarea#message').css('border', '1px solid #d6d6d6');
		
		if(($('input#name').val()) == "") {
			$('input#name').css('border', '1px solid red');
			$('li#li_err').html('Name field must not be blank.').fadeIn(500);
			return false;
		}
		else if(($('input#email').val()) == "") {
			$('input#email').css('border', '1px solid red');
			$('li#li_err').html('Email field must not be blank.').fadeIn(500);
			return false;
		}
		else if(($('input#phone').val()) == "") {
			$('input#phone').css('border', '1px solid red');
			$('li#li_err').html('Please provide a valid phone number').fadeIn(500);
			return false;
		}
		else if(($('textarea#message').val()) == "") {
			$('textarea#message').css('border', '1px solid red');
			$('li#li_err').html('Please enter a detailed message.').fadeIn(500);
			return false;
		}
		else {       

		$('input#sendMessage').slideUp();
		setTimeout(function() { $('div#loading').slideDown(); }, 500);
		
		var name = $('input#name').val(); 
		var email = $('input#email').val();
		var phone = $('input#phone').val();
		var message = $('textarea#message').val();
		
		$.post("admin/proc/mailer.php", { name: $('input#name').val(), email: $('input#email').val(), phone: $('input#phone').val(), part: $('input#part').val(), message: $('textarea#message').val()}, 
		function(data) {
			if(data==1) {
				$('div#loading').slideUp();
				setTimeout(function() { $('input#sendMessage').slideDown(); }, 500);
				$('li#li_err').css('color', 'green');  
				setTimeout(function() { $('li#li_err').html('Email Successfully Sent.'); }, 500);
			}  
			else {
				$('div#loading').slideUp();
				setTimeout(function() { $('input#sendMessage').slideDown(); }, 500);
				setTimeout(function() { $('li#li_err').html('Error While Sending Email.'); }, 500);
			}
				$('input#name').val("");
				$('input#email').val("");
				$('input#phone').val("");
				$('input#part').val("");
				$('textarea#message').val("");
		});
		}
		
		return false;   
	});	 
	
		
	/* Image Toggle */
	$('img.image-thumb').click(function() {
		
		var img_to_disp = $(this).attr('src');
		
		$('img.image-display').attr('src', img_to_disp);
		return false;
	});
 
 });
