// JavaScript Document

/*
var errorInputBGColor	= '#FF9966'
var errorTxtColor		= '#FF6666'
*/

var errorInputBGColor	= '#F7DB7D'
var errorTxtColor		= '#CC6600'


$(document).ready( function(){
							
	$('body').append('<style type="text/css"> .errortxt{color: '+errorTxtColor+'; font-weight:bold;}</style>')
	$('#comment, #other').wrap('<div style="overflow:hidden;padding-bottom:5px;"></div>')
	
	// Clear all the error marked fileds whth reset button click
	$('#reset').click( function (){
		$('#other').css({ 'background' : '#fff' })
		$('#otherError').remove()
		
		$('#comment').css({ 'background' : '#fff' })
		$('#commentError').remove()
		
		$('#firstName').css({ 'background' : '#fff' })
		$('#lastName').css({ 'background' : '#fff' })
		$('#firstLastNameError').remove()
		
		$('#email').css({ 'background' : '#fff' })
		$('#emailError').remove()
		
		$('#postalCode').css({ 'background' : '#fff' })
		$('#zipError').remove()
	})
		
	// Checking the comments textarea onblur and focus events
	$('#comment').blur( function(){
		if( $(this).val() == '' ){
			$(this).css({ 'background' : errorInputBGColor })
			$(this).after('<span class="errortxt" style="display:block; float:left;" id="commentError">Please let us know your comments.</span>')
		}
	});
	
	$('#comment').focus( function(){
		$(this).css({ 'background' : '#fff' })
		$('#commentError').remove()
	});
	// Ending Comments onblur and focus event
	
	// Checking the zipcode onblur and focus events
	$('#postalCode').blur(function(){
		if( $(this).val() == '' ){
			$(this).css({ 'background' : errorInputBGColor })
			$(this).after('<span class="errortxt" id="zipError">Zip/Postal Code is required</span>')
			if ($.browser.msie)	$('#zipError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
		}
	});
	
	$('#postalCode').focus( function(){
		$(this).css({ 'background' : '#fff' })
		$('#zipError').remove()
	});
	// Ending zipcode onblur and focus events
	
	// Checking the other onblur and focus events along with the other checkbox click event
	$('#other').blur( function(){
		if( $('#boxMktg:checked').length == 1){
			if( $(this).val() == '' ){
				$(this).css({ 'background' : errorInputBGColor })
				$(this).after('<span class="errortxt" style="display:block; float:left;" id="otherError">In what else are you interested?</span>')
			}
		}
	});
	
	$('#other').focus( function(){
		$(this).css({ 'background' : '#fff' })
		$('#otherError').remove()
	});
	
	$('#boxMktg').click( function(){
		if( $('#boxMktg:checked').length == 0){
			$('#other').css({ 'background' : '#fff' })
			$('#otherError').remove()
		}
	})
	// Ending other onblur and focus events
	
	// Starting First name and last name onblur and focus event and Contact you?-> no Click
	$('#firstName').blur( function(){
		if( $(this).val() == '' ) $(this).val('First')
		
		if( $('#contact:checked').length == 1){
			if( $('#firstName').val() == '' || $('#firstName').val() == 'First'){
				$('#firstName').css({ 'background' : errorInputBGColor })
				
				$('#firstLastNameError').remove()
				$('#lastName').after('<span class="errortxt" id="firstLastNameError">First and last name are both required.</span>')
				if ($.browser.msie)	$('#firstLastNameError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
			}
		}
	});
	
	$('#firstName').focus( function(){
		if( $(this).val() == 'First' ) $(this).val('')
		$(this).css({ 'background' : '#fff' })
		/*
		if( $('#lastName').val() == 'Last' ){
			$('#firstLastNameError').remove()
			$('#lastName').after('<span class="errortxt" id="firstLastNameError">Last name is required</span>')
			if ($.browser.msie)	$('#firstLastNameError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
		}
		*/
	});
	
	$('#lastName').blur( function(){
		if( $(this).val() == '' ) $(this).val('Last')
		
		if( $('#contact:checked').length == 1){
			if( $('#lastName').val() == '' || $('#lastName').val() == 'Last'){
				$('#lastName').css({ 'background' : errorInputBGColor })
				
				$('#firstLastNameError').remove()
				$('#lastName').after('<span class="errortxt" id="firstLastNameError">First and last name are both required.</span>')
				if ($.browser.msie)	$('#firstLastNameError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
			}
		}
	});
	
	$('#lastName').focus( function(){
		if( $(this).val() == 'Last' ) $(this).val('')
		$(this).css({ 'background' : '#fff' })
		/*
		if( $('#firstName').val() == 'First' ){
			$('#firstLastNameError').remove()
			$('#lastName').after('<span class="errortxt" id="firstLastNameError">Your first name is required.</span>')
			if ($.browser.msie)	$('#firstLastNameError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
		}
		*/
	});



	// Checking the Email onblur and focus events
	$('#email').blur(function(){
		
		if( $(this).val() == '' ) $(this).val('yourname@domain.com')
		
		if( $('#contact:checked').length == 1){
			if( $('#email').val() == '' || $('#email').val() == 'yourname@domain.com' || !(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i.test($('#email').val()))){
				$('#email').css({ 'background' : errorInputBGColor })
				
				$('#emailError').remove()
				$('#email').after('<span class="errortxt" id="emailError">An email address is required.</span>')
				if ($.browser.msie)	$('#emailError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
			}
		}
	});
	
	$('#email').focus( function(){
		
		if( $('#email').val() == 'yourname@domain.com' ) $('#email').val('')
		
		$(this).css({ 'background' : '#fff' })
		$('#emailError').remove()
	});
	// Ending Email onblur and focus events


	$('#contact').click( function(){
		if( $('#contact:checked').length == 0){
			$('#firstName').css({ 'background' : '#fff' })
			$('#lastName').css({ 'background' : '#fff' })
			$('#email').css({ 'background' : '#fff' })
			$('#firstLastNameError').remove()
			$('#emailError').remove()			
		}
	})
	// Ending First name and last name onblur and focus event
	
	// Some other elements' toggle effects of showing and vanishing the values inside them
	
	$('#address1').focus( function(){
		if( $('#address1').val() == 'Address 1' ) $('#address1').val('')
	});
	$('#address1').blur( function(){
		if( $('#address1').val() == '' ) $('#address1').val('Address 1')
	});
	
	$('#address2').focus( function(){
		if( $('#address2').val() == 'Address 2' ) $('#address2').val('')
	});
	$('#address2').blur( function(){
		if( $('#address2').val() == '' ) $('#address2').val('Address 2')
	});

        $('#phone').focus( function(){
                if( $('#phone').val() == '123-456-7890' ) $('#phone').val('')
        });
        $('#phone').blur( function(){
                if( $('#phone').val() == '' ) $('#phone').val('123-456-7890');
                if( $('#phone').val() != '' && $('#phone').val() != '123-456-7890' && !(/^[^0-9]*1?([^0-9]*[0-9]){10}[^0-9]*$/i.test($('#phone').val()))){
                        $('#phone').css({ 'background' : errorInputBGColor })
                                        $('#phoneError').remove()
                                        $('#phone').after('<span class="errortxt" id="phoneError">Please enter ten digits.</span>')
                                        if ($.browser.msie)	$('#phoneError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
                }
        });
	// Ending Showing and vanishing toggle effect
		
});
// Ending ready event


function checkform(){
	
	var errorFound = false
	
	// Starting Comments Check
	if( $('#comment').val() == '' ){
		errorFound = true
		$('#comment').css({ 'background' : errorInputBGColor })
		$('#commentError').remove()
		$('#comment').after('<span class="errortxt" id="commentError">Your comment is required.</span>')
	}
	// Ending Comments Check

	// Starting Zip Code Check
	if( $('#postalCode').val() == '' ){
		errorFound = true
		$('#postalCode').css({ 'background' : errorInputBGColor })
		$('#zipError').remove()
		$('#postalCode').after('<span class="errortxt" id="zipError">A zip or postal code is required.</span>')
		if ($.browser.msie)	$('#zipError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
	}
	// Ending Zip Code Check
	
	// Starting Other Content Check
	if( $('#boxMktg:checked').length == 1){
		if( $('#other').val() == '' ){
			errorFound = true
			$('#other').css({ 'background' : errorInputBGColor })
			$('#otherError').remove()
			$('#other').after('<span class="errortxt" id="otherError">In what else are you interested?</span>')
		}
	}
	// Ending Other Content Check

	// Starting Fist Name, Last Name and Email check
	if( $('#contact:checked').length == 1){
		var firstNameError	= false
		var lastNameError	= false
		
		if( $('#firstName').val() == '' || $('#firstName').val() == 'First'){
			firstNameError	= true
			errorFound		= true
		}
		if( $('#lastName').val() == '' || $('#lastName').val() == 'Last'){
			lastNameError	= true
			errorFound		= true
		}

		
		if(firstNameError && lastNameError){ // The case when both are not provided
			$('#firstName').css({ 'background' : errorInputBGColor })
			$('#lastName').css({ 'background' : errorInputBGColor })
			
			$('#firstLastNameError').remove()
			$('#lastName').after('<span class="errortxt" id="firstLastNameError">First and last name are both required.</span>')
			if ($.browser.msie)	$('#firstLastNameError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
		}
		else if(firstNameError){ // The case when First Name not provided
			$('#firstName').css({ 'background' : errorInputBGColor })
			$('#lastName').css({ 'background' : '#fff' })
			
			$('#firstLastNameError').remove()
			$('#lastName').after('<span class="errortxt" id="firstLastNameError">Your first name is required.</span>')
			if ($.browser.msie)	$('#firstLastNameError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
		}
		else if(lastNameError){ // The case when Last name not provided
			$('#firstName').css({ 'background' : '#fff' })
			$('#lastName').css({ 'background' : errorInputBGColor })
			
			$('#firstLastNameError').remove()
			$('#lastName').after('<span class="errortxt" id="firstLastNameError">Your last name is required.</span>')
			if ($.browser.msie)	$('#firstLastNameError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
		}
		else{ // The case when both are provided
			$('#firstName').css({ 'background' : '#fff' })
			$('#lastName').css({ 'background' : '#fff' })
			$('#firstLastNameError').remove()
		}
		
		// Checking for email issue
		if( $('#email').val() == '' || $('#email').val() == 'yourname@domain.com' || !(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i.test($('#email').val()))){
			
			errorFound		= true
			
			$('#email').css({ 'background' : errorInputBGColor })
			
			$('#emailError').remove()
			$('#email').after('<span class="errortxt" id="emailError">An email address is required.</span>')
			if ($.browser.msie)	$('#emailError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' })
		}
		
		else{
			$('#email').css({ 'background' : '#fff' })
			$('#emailError').remove()
		}
	}
	//Ending First name, last Name and Email check

        if( $('#phone').val() != '' && $('#phone').val() != '123-456-7890' && !(/^[^0-9]*1?([^0-9]*[0-9]){10}[^0-9]*$/i.test($('#phone').val()))){
                errorFound = true
                $('#phone').css({ 'background' : errorInputBGColor });
                $('#phoneError').remove();
                $('#phone').after('<span class="errortxt" id="phoneError">Please enter ten digits.</span>');
                if ($.browser.msie)	$('#phoneError').css({ 'paddingBottom' : 5, 'display' : 'inline-block' });
        } else {
                $('#phone').css({ 'background' : '#fff' });
                $('#phoneError').remove();
        }

	if(errorFound) return false
	return true
}
