/********************************************************************
	File:   
		contact.js
	Brief:  
		Implementation of JavaScript functionality for 
		the contact.html page
	Dependencies:
		jquery-1.3.2.min.js             (jQuery library)
		jquery.easing.1.2.js            (jQuery library plugin)
		cufon-yui.js                    (font replacement tool)
		sendMessage.php                 (php script used to send contact message via email) 
	Author:
		DigitalCavalry
	Author URI:
		http://graphicriver.net/user/DigitalCavalry
*********************************************************************/ 

// alias to jQuery library, function noConflict release control of the $ variable 
// to whichever library first implemented it
var $j = jQuery.noConflict();
// if true the send button is blocked
var g_blockSendButton = false;
var g_numberOfFails = 0;

/***************************************
	SETUP CONTACT FORM
****************************************/

function setupInputControls()
{
	// change border color wehen controls take focus
	$j(".commonInput, .commonTextarea, .contactInputHuman").focus(
		function()
		{
			$j(this).css("border", "1px solid #3399cc");
		}
	);
	
	// restore border color wehen controls lost focus
	$j(".commonInput, .commonTextarea, .contactInputHuman").blur(
		function()
		{
			$j(this).css("border", "1px solid #ccc");
			$j(this).css("border-right", "1px solid #eee");
			$j(this).css("border-bottom", "1px solid #eee");
		}
	);
	
	// when input name lost focus, validate the value
	$j("#inputName").blur(
		function()
		{
			if($j(this).val() != "")
			{
				$j("#nameErrorMsg").css("visibility", "hidden"); 
			} else
			{
				$j(this).css("border", "1px solid #FF0000");
				$j("#nameErrorMsg").html("&nbsp;please insert your name").css("visibility", "visible");            
			}
		}
	);
	
	// when input subject lost focus validate the value 
	$j("#inputPassword").blur(
		function()
		{
			if($j(this).val() != "")
			{
				$j("#passwordErrorMsg").css("visibility", "hidden"); 
			} else
			{
				$j(this).css("border", "1px solid #FF0000");
				$j("#passwordErrorMsg").html("&nbsp;you forgot to put in your password").css("visibility", "visible");            
			}
		}
	);    

	// when input message lost focus validate the value 
	$j("#inputCompany").blur(
		function()
		{
			if($j(this).val() != "")
			{
				$j("#companyErrorMsg").css("visibility", "hidden"); 
			} else
			{
				$j(this).css("border", "1px solid #FF0000");
				$j("#companyErrorMsg").html("&nbsp;please insert your company name").css("visibility", "visible");            
			}
		}
	);       
	
} // end of function setupInputControl
	
function setupSendButton()
{
	$j("#contactSendButton").click(
		function()
		{
			// prevent multiple send call by user
			if(true == g_blockSendButton)
			{
				return;
			}
			
			g_blockSendButton = true;
			// get all data from contact form and save it in local variables
			var inputCompany = $j("#inputCompany").val();
			var inputName = $j("#inputName").val();
			var inputPassword = $j("#inputPassword").val();
		   
			// create regular expression object
			var regExp = new RegExp(/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9]([-a-z0-9_]?[a-z0-9])*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z]{2})|([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})(\.([1]?\d{1,2}|2[0-4]{1}\d{1}|25[0-5]{1})){3})(:[0-9]{1,5})?$/i);
			// check email address, if result is null the email string dont match to pattern
			var error = (inputName == "") || (inputCompany == "") || (inputPassword == "");

			// if there was an error we must display some informotion and mark
			// input cotrol with wrong data    
			if(error)
			{                
				$j("#nameErrorMsg").css("visibility", "hidden");
				$j("#companyErrorMsg").css("visibility", "hidden");
				$j("#passwordErrorMsg").css("visibility", "hidden");
				$j("#contactErrorPanel").slideUp(300);
				
				// errors processing
				if(inputName == "")
				{
					$j("#inputName").css("border", "1px solid #FF0000");
					$j("#nameErrorMsg").html("&nbsp;please insert your name").css("visibility", "visible");
				}
				if(inputCompany == "")
				{
					$j("#inputCompany").css("border", "1px solid #FF0000");
					$j("#companyErrorMsg").html("&nbsp;please insert your company name").css("visibility", "visible"); 
				} else                
				if(inputPassword == "")
				{
					$j("#inputPassword").css("border", "1px solid #FF0000");
					$j("#passwordErrorMsg").html("&nbsp;you forgot to put in your password").css("visibility", "visible"); 
				}
				// unblock send button
				g_blockSendButton = false;                
			} else // if no error, if all data is set correctly
			{
				// let's define function called after ajax successfull call 
				function phpCallback(data)
				{   
					// if success        
					if(data == "NO")
					{
						g_numberOfFails++;
						if (g_numberOfFails > 1) {
							$j("#contactErrorPanel").text("");            
							$j("#contactErrorPanel").css("background-color", "#FBFEB1");
							$j("#contactErrorPanel").css("height", "35px");
							$j("#contactErrorPanel").append("Your login credentials were not accepted. Please make sure you have the right Username, Company Name, and Password to proceed. If you are still not able to login, please contact us at 1 (727) 498-8671 or email at <a href='mailto:contact_tlp_now@trafficlogpro.com'>contact_tlp_now@trafficlogpro.com</a><br>&nbsp;");
							$j("#contactErrorPanel").css("border", "1px solid #FBEC2C");
							$j("#contactErrorPanel").slideDown(300, function(){  g_blockSendButton = false;});

						} else {
							$j("#contactErrorPanel").text("");            
							$j("#contactErrorPanel").css("background-color", "#ccFFcc");
							$j("#contactErrorPanel").css("height", "35px");
							$j("#contactErrorPanel").append("Your login credentials were not accepted. Please make sure you have the right Username, Company Name, and Password to proceed.");
							$j("#contactErrorPanel").css("border", "1px solid #339933");
							$j("#contactErrorPanel").slideDown(300, function(){  g_blockSendButton = false;});
						}
					} else // if error/problem during email sending in php script
					{
						window.location = data;
					}
				} // end of function phpCallback            
			
			
				// all data is correct so we can hide error/success panel
				$j("#contactErrorPanel").slideUp(300);
				
				
				// try to send email via php script executed by server
				$j.post("php/login.php", { username : inputName, password : inputPassword, company : inputCompany }, phpCallback, "text");
				// unblock send button
			} // end else all dara
		}
	);
} // end of function setupSendButton
	
/***************************************
	MAIN CODE - CALL THEN PAGE LOADED
****************************************/
	   
// binding action to event onload page
$j(document).ready(
	function()
	{
		// common.js
		setupGlobal();
		setupCommunityButtons();            
		setupToolTipText();
		setupSearchBox();
		setupCufonFontReplacement();
		setupSideBarMiniSlider();
		setupMultiImageLightBox();
		setupSidebarTabsPanel();
		setupLoadingAsynchronousImages();
		setupToolTipImagePreview();
		setupTextLabelImagePreview();
		// this file
		setupInputControls();
		setupSendButton();
	}
);




	
