// Copyright (c) 2008 Canada Lawyer Listing, All Rights Reserved.

$(document).ready( function( ) {
	
	if( $("#inputType").val( ) == "firm" ) {
		$("#promote-firm").fadeIn( "normal", showRecaptcha( 'firmCaptcha' ) );
	} else {
		$("#promote-lawyer").fadeIn( "normal", showRecaptcha( 'lawyerCaptcha' ) );
	}
	
	$("#showLawyers").click( function( ) {
		$("#promote-firm").hide( );
		$("#promote-lawyer").fadeIn( "normal", showRecaptcha( 'lawyerCaptcha' ) );
	});
	
	$("#showFirms").click( function( ) {
		$("#promote-lawyer").hide( );
		$("#promote-firm").fadeIn( "normal", showRecaptcha( 'firmCaptcha' ) );
	});
	
	$("#lawyerFormSubmit").click( function( ) {
										   
		var categories = [];
		$("#addLawyerForm input[type=checkbox]" ).each( function( ) {
			if( $(this).is( ":checked" ) ) {
				categories.push( $(this).val( ) );
			}
		});								   
		
		var formElements = {
			'listingID': $("#addLawyerForm #listingID").val( ),
			'listingName': $("#addLawyerForm #listingName").val( ),
			'phoneNumber': $("#addLawyerForm #phoneNumber").val( ),
			'streetAddress': $("#addLawyerForm #streetAddress").val( ),
			'postalCode': $("#addLawyerForm #postalCode").val( ),
			'city': $("#addLawyerForm #city").val( ),
			'website': $("#addLawyerForm #website").val( ),
			'contactEmail': $("#addLawyerForm #contact-email").val( ),
			'description': $("#addLawyerForm #description").val( ),
			'categories': categories.join( "|" ),
			'submissionEmail': $("#addLawyerForm #submissionEmail").val( ),
			'confirmEmail': $("#addLawyerForm #confirmEmail").val( ),
			'recaptcha_challenge_field': Recaptcha.get_challenge( ),
			'recaptcha_response_field': Recaptcha.get_response( )
		};	
		
		$.ajax({
			 type: "POST",
			 url: "scripts/submitLawyer.php",
			 data: formElements,
			 success: function( msg ){
			 	$("#lawyerMessage").html( msg );
			 },
			 complete: function( ) {
				destroyRecaptchaWidget( 'lawyerCaptcha' ); 
			 }
		 });

	});
	
	$("#addLawyerForm #description").keyup( function( ) {
		var currentText = $("#addLawyerForm #description").val( );
		var textLength = currentText.length;
		
		if( textLength > 240 ) {
			$("#addLawyerForm #description").val( currentText.substr( 0, 240 ) );	
		} 
		
		var textRemaining = 240 - textLength;
		if( textRemaining < 0 ) {
			textRemaining = 0;
		}
		
		$("#addLawyerForm #charsRemaining").html( textRemaining + " Remaining" );
		
	});
	
	$("#addFirmForm #description").keyup( function( ) {
		var currentText = $("#addFirmForm #description").val( );
		var textLength = currentText.length;
		
		if( textLength > 240 ) {
			$("#addFirmForm #description").val( currentText.substr( 0, 240 ) );	
		} 
		
		var textRemaining = 240 - textLength;
		if( textRemaining < 0 ) {
			textRemaining = 0;
		}
		
		$("#addFirmForm #charsRemaining").html( textRemaining + " Remaining" );
		
	});
	
	$("#firmFormSubmit").click( function( ) {
		
		var categories = [];
		$("#addFirmForm input[type=checkbox]" ).each( function( ) {
			if( $(this).is( ":checked" ) ) {
				categories.push( $(this).val( ) );
			}
		});
		
		var formElements = {
			'listingID': $("#addFirmForm #listingID").val( ),
			'listingName': $("#addFirmForm #listingName").val( ),
			'phoneNumber': $("#addFirmForm #phoneNumber").val( ),
			'streetAddress': $("#addFirmForm #streetAddress").val( ),
			'postalCode': $("#addFirmForm #postalCode").val( ),
			'city': $("#addFirmForm #city").val( ),
			'website': $("#addFirmForm #website").val( ),
			'contactEmail': $("#addFirmForm #contact-email").val( ),
			'description': $("#addFirmForm #description").val( ),
			'categories': categories.join( "|" ),
			'submissionEmail': $("#addFirmForm #submissionEmail").val( ),
			'confirmEmail': $("#addFirmForm #confirmEmail").val( ),
			'recaptcha_challenge_field': Recaptcha.get_challenge( ),
			'recaptcha_response_field': Recaptcha.get_response( )
		};	
		
		$.ajax({
			 type: "POST",
			 url: "scripts/submitFirm.php",
			 data: formElements,
			 success: function( msg ){
				var splitMsg = msg.split( "|" );
			 	$("#firmMessage").html( splitMsg[1] );
				if( splitMsg[0] == "SUCCESS" ) {
					//window.location = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=DSQW4MCQS763E&custom=" + splitMsg[2];
					window.location = "https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=PDAA3KQ2HL7B2&custom=" + splitMsg[2];
				}
			 },
			 complete: function( ) {
				destroyRecaptchaWidget( 'firmCaptcha' ); 
			 }
		 });

	});
	
});

function showRecaptcha( element ) {
  Recaptcha.create( "6LeZTAoAAAAAAO17PH1BnUYNaNoR032plgTEb_sQ", element, {
        theme: "red",
        tabindex: 0
  });
}

function destroyRecaptchaWidget( element ) {
  Recaptcha.destroy( );
  showRecaptcha( element );
}