
var ha_costs = 0;
var client_bill_rate = 0;
var pay_rate = 0;
var profit = 0;

function resetValues()
{
	ha_costs = 0;
	client_bill_rate = 0;
	pay_rate = 0;
	profit = 0;
	
	if(getObj('pay_rate'))
		getObj('pay_rate').value = "";
	if(getObj('bill_rate'))
		getObj('bill_rate').value = "";
	if(getObj('hours_worked'))
		getObj('hours_worked').value = "";
	
	getObj('total_results').style.display = 'none';
	
}

function calcCosts()
{
	if(!getObj('hours_worked') || getObj('hours_worked').value.trim().length == 0)
	{
		alert('Please enter the number of hours worked. 160 equals an average month of billing');
		return false;	
	}
	
	if(!getObj('pay_rate') || getObj('pay_rate').value.trim().length == 0)
	{
		alert('Please enter an hourly pay rate');
		return false;	
	}
	
	if(!getObj('bill_rate') || getObj('bill_rate').value.trim().length == 0)
	{
		alert('Please enter an hourly bill rate');
		return false;	
	}
	
	if(!getObj('state') || getObj('state').value.trim().length == 0)
	{
		alert('Please the state that this contract will take place in');
		return false;	
	}	
	
	pay_rate = cleanDollar('pay_rate');
	//alert(pay_rate);
	if(!pay_rate)
	{
		alert("Please enter a valid number for the hourly pay rate");
		return false;
	}
	
	bill_rate = cleanDollar('bill_rate');
	//alert(pay_rate);
	if(!bill_rate)
	{
		alert("Please enter a valid number for the hourly bill rate");
		return false;
	}
	
	if(Number(bill_rate) < Number(pay_rate))
	{
		alert("Your client bill rate must be higher than the total hourly costs");
		return false;
	}
	
	calcProfit(pay_rate,bill_rate,getObj('hours_worked').value);
}

function calcProfit(pay_rate,bill_rate,hours_worked)
{
	// TFI Calculations
	/* 
	For this calculator, we actually will calculate the payment to the recruiter twice, 
	each with a different formula (see below).  Whichever calculation results in a highest 
	hourly payout to the recruiter is the only result that will display on our web 
	page as the 'pay out' to that customer.    												

	Formula 1:								
	Pay Rate (times) Burden = loaded pay rate								
	Bill Rate (times) .08 = TFI fee								
	Loaded pay rate (plus) TFI fee = Total Costs								
	Bill Rate (minus) Total Costs = Hourly Payout to Recruiter								
	160 (times) Hourly Payout to Recruiter= amount displayed as result on our web page								
	
	Formula 2:								
	Pay Rate (times) Burden = loaded pay rate								
	Pay Rate (times) .25 = TFI Fee								
	Loaded pay rate (plus) TFI fee = Total Costs								
	Bill Rate (minus) Total Costs = Hourly Payout to Recruiter								
	160 (times) Hourly Payout to Recruiter= amount displayed as result on our web page

	The 'burden' percentage is a fixed number in all states (12.44%) except 
	in California (where it's 13.66%). 
	
	*/
	
	if(getObj('state').value == "CA")
		burden = '.001366';
	else
		burden = '.001244';
	
	//alert("pay rate: "+pay_rate+" bill rate: "+bill_rate+ " hours worked: "+hours_worked);
	// Forumula 1
	loaded_pay_rate1 = Number(pay_rate)*Number(burden);
	tfi_fee1 = Number(bill_rate)* .08;
	total_costs1 = loaded_pay_rate1+tfi_fee1;
	hourly_payout1 = Number(bill_rate)-total_costs1;
	result1 = (Number(hours_worked)*hourly_payout1) - (Number(pay_rate)*Number(hours_worked));
	//alert(result1);
	
	// Forumual 2
	loaded_pay_rate2 = Number(pay_rate)*Number(burden);
	tfi_fee2 = Number(pay_rate)* .25;
	total_costs2 = loaded_pay_rate2+tfi_fee2;
	hourly_payout2 = Number(bill_rate)-total_costs2;
	result2 = (Number(hours_worked)*hourly_payout2) - (Number(pay_rate)*Number(hours_worked));
	//alert(result2);
	
	if(hourly_payout1 > hourly_payout2)
	{
		results = result1;	
	}
	else
	{
		results = result2;	
	}
	
	getObj('total_results').style.display='';
	getObj('total_results_profit').innerHTML = "$"+roundMoney(results);
	getObj('total_results_hours').innerHTML = hours_worked;
}

function calcExtendedProfit()
{
	if(!getObj('hours_per_week') || getObj('hours_per_week').value.trim().length == 0)
	{
		alert("Please enter the number of weeks your contracter will be working");
		return false;	
	}
	
	hours_per_week = cleanDollar('hours_per_week');
	
	if(!hours_per_week)
	{
		alert("Please enter a valid number for the hourly pay week");
		return false;
	}
	
	getObj('weekly_monthly_profit').style.display = '';
	
	
	weekly_profit = profit*Number(hours_per_week);
	
	getObj('weekly_profit_value').innerHTML = "$"+roundMoney(weekly_profit);
	
	getObj('monthly_profit_value').innerHTML = "$"+roundMoney(weekly_profit*4);
}


function roundMoney(value)
{
	// First round the value
	value = Math.round(value*100)/100;
	
	// convert to a string so we can append string data to it
	value = String(value);
	
	// if there is no decimal place, then append
	if(value.indexOf(".") == -1)
	 	value = value + ".00";
	
	// if the value rounded out to an even 10 place cent (like .80 becoming .8)
	// then add the extra zero.
	
	if(value.substr(value.indexOf(".")+1,2).trim().length == 1)
		value = value+ "0";

return value;
}

function cleanDollar(obj)
{
	 if(!getObj(obj) || getObj(obj).value.trim().length == 0)
	 	return false;
	 
	 getObj(obj).value = getObj(obj).value.trim();
	 getObj(obj).value = getObj(obj).value.replace("$","");
	 getObj(obj).value = getObj(obj).value.replace("%","");
	 getObj(obj).value = getObj(obj).value.replace(",","");
	 
	 // if the value isn't a number, then we need to set it to null
	// alert(getObj(obj).value + " NAN? "+notAnumber( getObj(obj).value));
	 if(notAnumber( getObj(obj).value) )
	 {
	 	getObj(obj).value = "";
	 	return false;
	 }
	else	 // round to the nearest cent
	 	getObj(obj).value = roundMoney( getObj(obj).value );
	 
	 //myregexp = new RegExp("^.[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$");
	 
	 //getObj(obj).value.replace(myregexp, "");
	 
	 //alert(getObj(obj).value);
	    //return /^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(getObj(obj).value);
	   
return Number(getObj(obj).value);
}
 
function notAnumber(value)
{
	//alert(Number(value)+"-"+String(Number(value)));
	if(String(Number(value)) == "NaN")
	{
		//alert(value+" is "+String(Number(value)));
		return true;
	}
	else
		return false;
} 

// This string prototype allows you to append .trim() to any string to trim the value
String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

String.prototype.toNumber = function()
{
	return Number(a);
}



 /*
 * Returns an object if one is found with the id
 * passed in the parameters
 *
 * @param	String;	Object ID
 * @return	Object;	Returns the object found, if any
 */
function getObj( elementID )
{
	
	if( !isString(elementID) || elementID.length < 1 )
		return null

	// First use getElementById
	theObj = document.getElementById(elementID);
	//theObj = eval( 'document.getElementById("' + elementID + '")' );
	
	if(document.getElementById(elementID))
		theObj = eval( 'document.getElementById("' + elementID + '")' );
	//else
		//alert("Missing Obj on page: " +elementID);
	
	// If an object wasn't found, try using getElementByName, if available
	if( !isValidObject(theObj) && isFunction(document.getElementsByName) )
	{
		theObj = document.getElementsByName(elementID);
	}
	
	if( isValidObject(theObj) )
		return theObj;
	else
		return null;		
	
}


function isObject( variable )
{
	return ( typeof variable == 'object' );
}

function isValidObject( variable )
{
	return ( isObject(variable) && !isNull(variable) );	
}

function isNull( variable )
{
	return ( variable == null );
}

function isString( variable )
{
	return ( typeof variable == 'string' );
}

function isValidString( variable )
{
	return ( typeof variable == 'string' && variable.length > 0 );
}

function isArray( variable )
{
	return ( typeof variable == 'array' );
}

function isUndefined( variable )
{
	return ( typeof variable == 'undefined' );
}

function isDefined( variable )
{
	return ( typeof variable != 'undefined');
}

function isNumber( variable )
{
	return ( typeof variable == 'number' );
}

function isFunction( variable )
{
	return ( typeof variable == 'function' );
}