//-- ----------------------------------------------------------------------------------------------------
//-- NAME: Utilities-1.js
//-- ABSTRACT: Utilities to be used with /Contents/Company/Submit-Comment/Submit-Comment-1.asp
//--
//--	       * These scripts rely on functions in Client-Utilities-1.js
//--
//-- AUTHOR: Brian van Loveren Geis
//-- DATE: 01-28-08
//-- ----------------------------------------------------------------------------------------------------

//-- ----------------------------------------------------------------------------------------------------
//-- CONSTANTS
//-- ----------------------------------------------------------------------------------------------------
var m_INVALID_COLOR = "#DDD";
var m_VALID_COLOR = "#FEFEFE";



//-- ----------------------------------------------------------------------------------------------------
//-- Variables
//-- ----------------------------------------------------------------------------------------------------
var m_strErrorMessage = "";



	// ----------------------------------------------------------------------------------------------------
	// Name: InitializeCommentBox
	// Abstract: Get the page ready for the error report
	// ----------------------------------------------------------------------------------------------------
	function InitializeCommentBox( frmCommentBox ) 
	{	
		var blnResult = true;
		
		try
		{
			// Disable Submit Button by default
			if( frmCommentBox.btnSubmit )
				frmCommentBox.btnSubmit.disabled = "disabled";

			// Uncheck the agreement when the page loads.  Make them check it every time.
			if( frmCommentBox.chkIAgree )
			    frmCommentBox.chkIAgree.checked = false;

			// Make sure the comment is not too big
			if( frmCommentBox.txtComment )
			{
				LimitTextLength(frmCommentBox.txtComment, 
					frmCommentBox.txtCharactersLeft, 1024);
			}

			// Is there a first element in the form?  Yes, focus on it
			if( frmCommentBox.elements[0] )
				frmCommentBox.elements[0].focus();
		}

		catch( expError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'ValidateErrorReport' ) );

			// Fail
			blnResult = false;
		}

		finally	// Return
		{ return blnResult; }
			
	} // InitializeCommentBox



	// ----------------------------------------------------------------------------------------------------
	// Name: SubmitComment
	// Abstract: Validates the forms data.  If the data is valid then submit the form.
	// ----------------------------------------------------------------------------------------------------
	function SubmitComment( frmCommentBox)
	{		
		var blnResult = false;

		try  
		{ 	
			// Set busy cursor
			document.body.style.cursor = "wait";

			// Is the data valid?
			if( ValidateErrorReport(frmCommentBox) == true )
			{	
				// Yes, valid
				blnResult = true;

				
				// Disable the submit button
				frmCommentBox.btnSubmit.disabled = "disabled";
			}

			// Put cursor back
			document.body.style.cursor = "default";
		}

		catch( expError )  
		{ 	
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'SubmitComment' ) );

			// Fail
			blnResult = false;
		}

		finally  // Return
		{ return blnResult; }
		
	} // SubmitComment



	// ----------------------------------------------------------------------------------------------------
	// Name: ValidateErrorReport
	// Abstract: Make sure all the required form data is present and well formed.  Provide a detailed error
	//	     message when errors are found.
	//
	//	     August 24, 2008.  Added a check for a specific spammer.  Don't allow the First Name, 
	//   	     Last Name, and Company to be the same.
	// ----------------------------------------------------------------------------------------------------
	function ValidateErrorReport(frmCommentBox) 
	{	
		var blnResult = true;
		
		try
		{
			// Clear the current error message
			m_strErrorMessage = "";

			// Validate the Agreement
			if( ValidateAgreement(frmCommentBox) == false ) { blnResult = false; }

			// Validate the Comment
			if( ValidateComment(frmCommentBox) == false ) { blnResult = false; }

			// Validate the Confirmation Email
			if( ValidateConfirmationEmail(frmCommentBox) == false ) { blnResult = false; }

			// Validate the Email
			if( ValidateEmail(frmCommentBox) == false ) { blnResult = false; }			
			
			// Validate the Phone Number
			if( ValidatePhoneNumber(frmCommentBox) == false ) { blnResult = false; }	

			// Validate the Last Name
			if( ValidateLastName(frmCommentBox) == false ) { blnResult = false; }
			
			// Validate the FirstName
			if( ValidateFirstName(frmCommentBox) == false ) { blnResult = false; }

			// Spammer Check - Are the first, last, and company names the same?
			if( frmCommentBox.txtFirstName.value.toLowerCase() == frmCommentBox.txtLastName.value.toLowerCase() &&
				frmCommentBox.txtLastName.value.toLowerCase() == frmCommentBox.txtCompanyName.value.toLowerCase() )
			{
				// Yes, invalid.  Add to error string
				blnResult = false;
				m_strErrorMessage = "- The First Name, Last Name, and Company Name can not be the same.\n" + m_strErrorMessage;
			}

			// Valid?  No, give error
			if( blnResult == false )
			{ alert("Please fix the following errors:\n\n" + m_strErrorMessage + "\n"); }

		}

		catch( expError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'ValidateErrorReport' ) );

			// Fail
			blnResult = false;
		}

		finally	// Return
		{ return blnResult; }
			
	} // ValidateErrorReport




	


	// ----------------------------------------------------------------------------------------------------
	// Name: ValidateAgreement
	// Abstract: Make sure the user agreed to the Terms and Conditions.
	// ----------------------------------------------------------------------------------------------------
	function ValidateAgreement(frmCommentBox) 
	{	

		var blnResult = true;
		var idAgreementContainer;
		
		try
		{
			// Get the agreement Table
			idAgreementContainer = document.getElementById('idAgreementContainer');

			// Set default
			idAgreementContainer.style['background'] = m_VALID_COLOR;

			// Is it checked?
			if( frmCommentBox.chkIAgree.checked == false ) 
			{
				// No, save error
				m_strErrorMessage = "- You must agree to the terms and conditions.\n" + m_strErrorMessage;
				
				// Fail
				blnResult = false;
			}
			
			// Valid?
			if( blnResult == false )
			{
				// No, set background to invalid color
				idAgreementContainer.style['background'] = m_INVALID_COLOR;
				
				// Focus on it
				frmCommentBox.chkIAgree.focus();
			}

		}

		catch( expError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'ValidateAgreement' ) );

			// Fail
			blnResult = false;
		}

		finally	// Return
		{ return blnResult; }
			
	} // ValidateAgreement



	// ----------------------------------------------------------------------------------------------------
	// Name: ValidateFirstName
	// Abstract: Make sure the First Name is valid
	// ----------------------------------------------------------------------------------------------------
	function ValidateFirstName(frmCommentBox) 
	{	

		var blnResult = true;
		
		try
		{
			// Trim it
			frmCommentBox.txtFirstName.value = TrimString(frmCommentBox.txtFirstName.value);
			
			// Set background to default
			frmCommentBox.txtFirstName.style['background']= m_VALID_COLOR;

			// Is there a value?
			if( frmCommentBox.txtFirstName.value == '' ) 
			{
				// No, save error
				m_strErrorMessage = "- First Name cannot be blank.\n" + m_strErrorMessage;
				
				// Fail
				blnResult = false;
			}
			
			// Valid?
			if( blnResult == false )
			{
				// No, set background to invalid color
				frmCommentBox.txtFirstName.style['background'] = m_INVALID_COLOR;
				
				// Focus on it
				frmCommentBox.txtFirstName.focus();
			}

		}

		catch( expError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'ValidateFirstName' ) );

			// Fail
			blnResult = false;
		}

		finally	// Return
		{ return blnResult; }
			
	} // ValidateFirstName
	
	
	
	// ----------------------------------------------------------------------------------------------------
	// Name: ValidateLastName
	// Abstract: Make sure the Last Name is valid
	// ----------------------------------------------------------------------------------------------------
	function ValidateLastName(frmCommentBox) 
	{	

		var blnResult = true;
		
		try
		{
		
			// Trim it
			frmCommentBox.txtLastName.value = TrimString(frmCommentBox.txtLastName.value);
		
			// Set background to default
			frmCommentBox.txtLastName.style['background']= m_VALID_COLOR;

			// Is there a value?
			if( frmCommentBox.txtLastName.value == '' ) 
			{
				// No, give error
				m_strErrorMessage = "- Last Name cannot be blank.\n" + m_strErrorMessage;
				
				// Invalid
				blnResult = false;
			}
			
			// Valid?
			if( blnResult == false)
			{
				// No, set background to gray
				frmCommentBox.txtLastName.style['background'] = m_INVALID_COLOR;
				
				// Focus on it
				frmCommentBox.txtLastName.focus();
			}

		}

		catch( expError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'ValidateLastName' ) );

			// Fail
			blnResult = false;
		}

		finally	// Return
		{ return blnResult; }
			
	} // ValidateLastName



	// ----------------------------------------------------------------------------------------------------
	// Name: ValidatePhoneNumber
	// Abstract: Make sure the Phone Number is seven number.
	// ----------------------------------------------------------------------------------------------------
	function ValidatePhoneNumber(frmCommentBox) 
	{	

		var blnResult = true;
		var strPhoneNumber = "";
		var strExpression = "";
		var regExpression;
		
		try
		{	
			// Trim
			frmCommentBox.txtPhoneNumberArea.value = TrimString(frmCommentBox.txtPhoneNumberArea.value);
			frmCommentBox.txtPhoneNumberExchange.value = TrimString(frmCommentBox.txtPhoneNumberExchange.value);
			frmCommentBox.txtPhoneNumber.value = TrimString(frmCommentBox.txtPhoneNumber.value);

			// Make default background color
			frmCommentBox.txtPhoneNumberArea.style['background'] = m_VALID_COLOR;
			frmCommentBox.txtPhoneNumberExchange.style['background'] = m_VALID_COLOR;
			frmCommentBox.txtPhoneNumber.style['background'] = m_VALID_COLOR;

			// Put the phone number together
			strPhoneNumber = frmCommentBox.txtPhoneNumberArea.value + frmCommentBox.txtPhoneNumberExchange.value + frmCommentBox.txtPhoneNumber.value;

			// Is there a Phone Number
			if( strPhoneNumber != '' ) 
			{
				// Yes, setup a Regular expression to find 7 numbers
				strExpression = "^\\d{10}$";
				regExpression = new RegExp(strExpression);

				// Is it 10 digits
				if( regExpression.test(strPhoneNumber) == false )
				{
				
					// No, save error
					m_strErrorMessage = "- Phone Number does not appear valid.\n" + m_strErrorMessage;

					// Fail
					blnResult = false;
				}
			}
			
			// Valid?
			if( blnResult == false )
			{
				// No, set background to gray
				frmCommentBox.txtPhoneNumberArea.style['background'] = m_INVALID_COLOR;
				frmCommentBox.txtPhoneNumberExchange.style['background'] = m_INVALID_COLOR;
				frmCommentBox.txtPhoneNumber.style['background'] = m_INVALID_COLOR;
				
				// Focus on it
				frmCommentBox.txtPhoneNumberArea.focus();
			}

		}

		catch( expError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'ValidatePhoneNumber' ) );

			// Fail
			blnResult = false;
		}

		finally	// Return
		{ return blnResult; }

	} // ValidatePhoneNumber



	// ----------------------------------------------------------------------------------------------------
	// Name: ValidateEmail
	// Abstract: Make sure the email is valid
	// ----------------------------------------------------------------------------------------------------
	function ValidateEmail(frmCommentBox) 
	{	

		var blnResult = true;
		
		try
		{	
			// Trim it
			frmCommentBox.txtEmail.value = TrimString(frmCommentBox.txtEmail.value);

			// Make default background color
			frmCommentBox.txtEmail.style['background'] = m_VALID_COLOR;
			
			// Is there a value?
			if( frmCommentBox.txtEmail.value == '' ) 
			{
				// No, save error
				m_strErrorMessage = "- Email cannot be blank.\n" + m_strErrorMessage;
				
				// Fail
				blnResult = false;
			}
			else
			{	
				// Yes, validate eMail format
				if( ValidateEmailFormat( frmCommentBox.txtEmail.value ) == false )
				{
					// No, save error
					m_strErrorMessage = "- Email does not appear valid.\n" + m_strErrorMessage;

					// Fail
					blnResult = false;
				}
			}
			
			// Valid?
			if( blnResult == false )
			{
				// No, set background to gray
				frmCommentBox.txtEmail.style['background'] = m_INVALID_COLOR;
				
				// Focus on it
				frmCommentBox.txtEmail.focus();
			}

		}

		catch( expError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'ValidateEmail' ) );

			// Fail
			blnResult = false;
		}

		finally	// Return
		{ return blnResult; }
			
	} // ValidateEmail



	// ----------------------------------------------------------------------------------------------------
	// Name: ValidateConfirmationEmail
	// Abstract: Make sure the confirmation email is valid
	// ----------------------------------------------------------------------------------------------------
	function ValidateConfirmationEmail(frmCommentBox) 
	{	

		var blnResult = true;
		
		try
		{	
			// Trim it
			frmCommentBox.txtConfirmEmail.value = TrimString(frmCommentBox.txtConfirmEmail.value);

			// Make default background color
			frmCommentBox.txtConfirmEmail.style['background'] = m_VALID_COLOR;

			// Is there a value?
			if( frmCommentBox.txtConfirmEmail.value == '' ) 
			{
				// No, save error
				m_strErrorMessage = "- Confirmation Email cannot be blank.\n" + m_strErrorMessage;
				
				// Fail
				blnResult = false;
			}
			else
			{
			
				// Yes, validate eMail format
				if( ValidateEmailFormat( frmCommentBox.txtConfirmEmail.value ) == true )
				{
					// Yes, is this the same as the eMail?
					if( frmCommentBox.txtEmail.value.toLowerCase() != frmCommentBox.txtConfirmEmail.value.toLowerCase() )
					{
						// No, save error
						m_strErrorMessage = "- Confirmation Email does not match.\n" + m_strErrorMessage;

						// Fail
						blnResult = false;
					}

				}
				else
				{
					// No, save error
					m_strErrorMessage = "- Confirmation Email does not appear valid.\n" + m_strErrorMessage;

					// Fail
					blnResult = false;
				}
			}
			
			// Valid?
			if( blnResult == false )
			{
				// No, set background to gray
				frmCommentBox.txtConfirmEmail.style['background'] = m_INVALID_COLOR;
				
				// Focus on it
				frmCommentBox.txtConfirmEmail.focus();
			}

		}

		catch( expError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'ValidateConfirmationEmail' ) );

			// Fail
			blnResult = false;
		}

		finally	// Return
		{ return blnResult; }
			
	} // ValidateConfirmationEmail




	// ----------------------------------------------------------------------------------------------------
	// Name: ValidateEmailFormat
	// Abstract: Make sure the email has a valid format  Something@Somewhere.Specific.com
	//
	//		** Called from ValidateEmail() and ValidateConfirmationEmail() only.
	// ----------------------------------------------------------------------------------------------------
	function ValidateEmailFormat(strEmailAddress) 
	{	

		var blnResult = false;
		//var strEmailTest = "";
		//var regEmailTest;
		
		try
		{	
			// Trim it
			strEmailAddress = TrimString(strEmailAddress);

			// Set up Regular expression
			var strEmailTest =  "/(^[0-9a-z_]([0-9a-z_\.]*)@([0-9a-z_\.]*)([.][a-z]{2,3})$)";
				strEmailTest += "|(^[0-9a-z_]([0-9a-z_\.]*)@([0-9a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)";
			var strDoubleDotTest = "\\.\\.";
			var regEmailTest;
			var regDoubleDotTest;
		   
			// Setup email test
			regEmailTest = new RegExp(strEmailTest, "i");

			// Test eMail validity
			if( regEmailTest.test(strEmailAddress) )
			{
				// Success, setup double dot test
				regDoubleDotTest = new RegExp(strDoubleDotTest, "i");
				
				// Test for double dots
				blnResult = !regDoubleDotTest.test(strEmailAddress);
			}

		}

		catch( expError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'ValidateEmailFormat' ) );

			// Fail
			blnResult = false;
		}

		finally	// Return
		{ return blnResult; }
			
	} // ValidateEmailFormat


	




	// ----------------------------------------------------------------------------------------------------
	// Name: ValidateComment
	// Abstract: Make sure the Comment is valid
	// ----------------------------------------------------------------------------------------------------
	function ValidateComment(frmCommentBox) 
	{	

		var blnResult = true;
		var lngTEXT_LIMIT = 1024;
		
		try
		{	
			// Trim it
			frmCommentBox.txtComment.value = TrimString(frmCommentBox.txtComment.value);

			// Reset the count
			frmCommentBox.txtCharactersLeft.value = lngTEXT_LIMIT - frmCommentBox.txtComment.value.length;

			// Make default background color
			frmCommentBox.txtComment.style['background'] = m_VALID_COLOR;
			
			// Is there a value?
			if( frmCommentBox.txtComment.value == '' ) 
			{
				// No, save error
				m_strErrorMessage = "- Comment cannot be blank.\n" + m_strErrorMessage;
				
				// Fail
				blnResult = false;
			}
			else
			{
				// Yes, are there too many characters?
				if( frmCommentBox.txtComment.value.length > lngTEXT_LIMIT )
				{
					// Yes, save error
					m_strErrorMessage = "- Comment cannot be more than " + lngTEXT_LIMIT + " characters.\n" + m_strErrorMessage;
				
					// Fail
					blnResult = false;
				}
			}
			
			// Valid?
			if( blnResult == false )
			{
				// No, set background to gray
				frmCommentBox.txtComment.style['background'] = m_INVALID_COLOR;
				
				// Focus on it
				frmCommentBox.txtComment.focus();
			}

		}

		catch( expError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'ValidateComment' ) );

			// Fail
			blnResult = false;
		}

		finally	// Return
		{ return blnResult; }
			
	} // ValidateComment





	// --------------------------------------------------------------------------------
	// Name: chkIAgree_Click
	// Abstract: Disable the submit button by default.  If chkAgree is checked, enable
	//	     the button.
	// --------------------------------------------------------------------------------
	function chkIAgree_Click()
	{ 
		var blnResult = true;

		try
		{	
			// Disable Submit button
			document.frmCommentBox.btnSubmit.disabled = "disabled";
			
			// Do they agree with the terms and conditions?
			if( document.frmCommentBox.chkIAgree.checked == true )
			{
				// Yes, enable the navigate button
				document.frmCommentBox.btnSubmit.disabled = "";
			}
		}
		catch( errError )
		{
			// Format and display the error
			alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'chkIAgree_Click' ) );
			
			// Fail
			blnResult = false;
		}
		finally	// Return
		{ return blnResult; }

	} // chkIAgree_Click();

	
	
	
	// ----------------------------------------------------------------------------------------------------
	// Name: TrimString
	// Abstract: Remove leading and trailing white space
	// ----------------------------------------------------------------------------------------------------
	function TrimString( strString )
	{
		var strReturn = strString;
		
		try  // Trim
		{ strReturn = strReturn.replace(/^\s*|\s*$/g,""); }

		catch( expError )  // Format and display the error
		{ alert( BuildExceptionMessage( expError, 'Submit-Comment-Utilities-1.js', 'TrimString' ) ); }

		finally  // Return
		{ return strReturn; }
		
	} // TrimString
