	// Fix selected state on the calc-menu
	$(document).ready(function() {
		
		// Check if we're on the correct page
		if (!$('#calc-menu').length) {return false;};
		
		// Set default item to have the selected class
		$('#calc-menu a#defaultLink').parents('li').addClass('selected');
		
		// Set selected on the menu items when clicked
		$('#calc-menu a').click(function() {
			
			$('#calc-menu li.selected').removeClass('selected');
			$(this).parents('li').addClass('selected');
			
		});
		
	});
	
	function loadMonthlyRevenue() {
		$('#calc-container').load('ajax/calculator-tools/load-monthly-revenue.html');
	}
	
	
	function loadSeoPotential() {
	
		$('#calc-container').load('ajax/calculator-tools/load-seo-potential.html');
	
	}

	
	function loadKeywordGenerator() {
		$('#calc-container').load('ajax/calculator-tools/load-keyword-generator.html');
	}
	
	
	function loadHowManyVisitors() {
		$('#calc-container').load('ajax/calculator-tools/load-how-many-visitors.html');
	}
	
	
	function loadExpectedCategoryConversion() {
		$('#calc-container').load('ajax/calculator-tools/load-expected-category-conversion.html');
	}


	function loadAverageCommissionPerSegment() {
		$('#calc-container').load('ajax/calculator-tools/load-average-commission-per-segment.html');
	}

	function calculateVisitorsRemainder( updatedId )
	{	
		// If we entered a negative number
		if( ! validateNaturalNumber( '#' + updatedId ) )
		{
			//	set it to zero and exit
			alert( 'You can not enter a negative number, value set to 0.' );
			return;
		}		
		
		// Initialize data from all fields
		var fields = getFields( updatedId );
				
		if ( ! validateTotal( fields, updatedId, 100 ) )
		{
			alert( 'The sum can not exceed 100, value clamped.' );
		}		
		
		assignRemainder( fields, updatedId, 100 );
	}
	
	function validateNaturalNumber( elementId )
	{
		if( parseInt( $( '#' + elementId ).val() ) < 0 )
		{
			$( '#' + elementId ).val( '0' );
			return false;
		}
		
		return true;			
	}


	function getFields( elementId )
	{
		var fields = new Object();		
		
		switch( elementId )
		{
			
			case 'search_engines':
			case 'type_ins':
			case 'other_domains':
				fields['search_engines'] = parseInt($('#search_engines').val());
				fields['type_ins'] = parseInt($('#type_ins').val());
				fields['other_domains'] = parseInt($('#other_domains').val());
				break;		
			
			case 'high_converting_keywords':
			case 'medium_converting_keywords':
			case 'low_converting_keywords':
				fields['high_converting_keywords'] = parseInt($('#high_converting_keywords').val());
				fields['medium_converting_keywords'] = parseInt($('#medium_converting_keywords').val());
				fields['low_converting_keywords'] = parseInt($('#low_converting_keywords').val());			
				break;
			
		}
		
		return fields;		
	}
	
	
	function validateTotal( fields, elementId, total )
	{
		var preSum = 0;
		for( fieldName in fields )
		{
			if( fieldName != elementId )
			{
				preSum += fields[fieldName];
			}
		}
				
		// If the Sum + the value of the one we entered exceeds total
		if ( total < ( preSum + fields[elementId] ) )
		{
			//	Clamp the value we entered so the sum is sum
			$( '#' + elementId ).val( ( total - preSum ).toString() );
			fields[elementId] = total - preSum;
			return false;
		}		
		
		return true;
	}
	
	
	function assignRemainder( fields, elementId, total )
	{
		var emptyCount = 0;
		var emptyField = '';
		var sum = 0;
		
		for( fieldName in fields )
		{
			if(!(0 < fields[fieldName]))
			{
				++emptyCount;
				emptyField = fieldName;
			}
			else
			{
				sum += fields[fieldName]; 
			}
		}
				
		//	Assign the difference between total and the sum of the other fields		
		if (emptyCount == 1 && emptyField != elementId)
		{
			emptyFieldVal = total - sum;
			$('#' + emptyField).val( emptyFieldVal.toString() );
		}	
	}
	

	function validateMonthlyRevenue(){
	
		var uv = $('#unique_visitors').val();
		var se =  $('#search_engines').val();	
		var od =  $('#other_domains').val();	
	    var ti = $('#type_ins').val();
		var hck =  $('#high_converting_keywords').val();
		var mck = $('#medium_converting_keywords').val();
		var lck = $('#low_converting_keywords').val();
		
		if ((isNaN(uv)) || ((uv) < 1))
		{
		alert('Unique visitors must be a positive number');
		return false;
		}
		
		if (parseInt(se) + parseInt(ti) + parseInt(od) != 100)
		{
		alert('Total share of visitors must total 100%');
		return false;
		}
		
		if (parseInt(hck) + parseInt(mck) + parseInt(lck) != 100)
		{
		alert('Converting keywords must total 100%');
		return false;
		}			
		
	return true;
	}


	function validateSeoPotential(){
	
		var dn = $('#domain_name').val();
		
		if (!dn)
		{
		alert('You must input a domain');
		return false;
		}	
		return true;
		}



	function validateHowManyVisitors(){
	
	var revenue = $('#revenue').val();
	
	var segment2 = $('#segment2').val();
	var segment3 = $('#segment3').val();
	var segment1_percent = $('#segment1_percent').val();
	var segment2_percent = $('#segment2_percent').val();
	var segment3_percent = $('#segment3_percent').val();

	if (!segment1_percent) { segment1_percent = 0; }
	if (!segment2_percent) { segment2_percent = 0; }
	if (!segment3_percent) { segment3_percent = 0; }
		
		if ((isNaN(revenue)) || ((revenue) < 1))
		{
		alert('Revenue must be a positive number');
		return false;
		}
	
		if ((segment2 == '') && (segment2_percent > 0))
		{
		alert('Please choose a segment #2');
		return false;
		}
	
		if ((segment3 == '') && (segment3_percent > 0))
		{
		alert('Please choose a segment #3');
		return false;
		}
	
		if ((isNaN(segment1_percent)) || ( ( parseInt(segment1_percent) + parseInt(segment2_percent) + parseInt(segment3_percent) ) != 100 ))
		{
		alert('Site segments must total 100%');
		return false;
		}
		
		return true;
	}



	function validateExpectedCategoryConversion(){
	
		var se =  $('#search_engines').val();	
		var od =  $('#other_domains').val();	
	    var ti = $('#type_ins').val();
		var hck =  $('#high_converting_keywords').val();
		var mck = $('#medium_converting_keywords').val();
		var lck = $('#low_converting_keywords').val();
		
		if (parseInt(se) + parseInt(ti) + parseInt(od) != 100)
		{
		alert('Total share of visitors must total 100%');
		return false;
		}
		
		if (parseInt(hck) + parseInt(mck) + parseInt(lck) != 100)
		{
		alert('Converting keywords must total 100%');
		return false;
		}			
		
	return true;
	}




	function calculateMonthlyRevenue() {
		
		if (validateMonthlyRevenue() == true)
		{
			$('#calc-container').load('ajax/calculator-tools/calculate-monthly-revenue.html', 
			{
			'segment' : $('#segment').val(),
			'unique_visitors' : $('#unique_visitors').val(),
			'search_engines' : $('#search_engines').val(),
			'type_ins' : $('#type_ins').val(),
			'other_domains' : $('#other_domains').val(),
			'high_converting_keywords' : $('#high_converting_keywords').val(),
			'medium_converting_keywords' : $('#medium_converting_keywords').val(),
			'low_converting_keywords' : $('#low_converting_keywords').val()
			});
	
		}
	}


	function calculateSeoPotential(){
	
		if (validateSeoPotential() == true)
		{		
			$('#calc-container').load('ajax/calculator-tools/calculate-seo-potential.html', 
			{
			'segment' : $('#segment').val(),
			'site_year' : $('#site_year').val(),
			'domain_name' : $('#domain_name').val(),
			'backlinks' : $('#backlinks').val(),
			'page_rank' : $('#page_rank').val(),
			'indexed_pages' : $('#indexed_pages').val()
			});
		}
	}
	
	
	function calculateKeywordGenerator(){
	
			$('#calc-container').load('ajax/calculator-tools/calculate-keyword-generator.html', 
			{
			'segment' : $('#segment').val(),
			'keyword_type' : $('#keyword_type').val()
			});
	
	}
	
	
		function calculateHowManyVisitors(){

		if (validateHowManyVisitors() == true)
		{		

			$('#calc-container').load('ajax/calculator-tools/calculate-how-many-visitors.html', 
			{
			'revenue' : $('#revenue').val(),
			'segment1' : $('#segment1').val(),
			'segment2' : $('#segment2').val(),
			'segment3' : $('#segment3').val(),
			'segment1_percent' : $('#segment1_percent').val(),
			'segment2_percent' : $('#segment2_percent').val(),
			'segment3_percent' : $('#segment3_percent').val()
			});
		}
	}

	function calculateExpectedCategoryConversion(){

		if (validateExpectedCategoryConversion() == true)
		{
			$('#calc-container').load('ajax/calculator-tools/calculate-expected-category-conversion.html', 
			{
			'segment' : $('#segment').val(),
			'search_engines' : $('#search_engines').val(),
			'type_ins' : $('#type_ins').val(),
			'other_domains' : $('#other_domains').val(),
			'high_converting_keywords' : $('#high_converting_keywords').val(),
			'medium_converting_keywords' : $('#medium_converting_keywords').val(),
			'low_converting_keywords' : $('#low_converting_keywords').val()
			});
	
		}
	}

	
	function calculateAverageCommissionPerSegment(){
	
			$('#calc-container').load('ajax/calculator-tools/calculate-average-commission-per-segment.html', 
			{
			'segment' : $('#segment').val()
			});
	
	}
	
