function strip_common_elements(data_in){
	var disallowed_charset=new Array(" ","!","&","*","£","$","%",",","+","\"","'","_","=");
	for(i=0;i<disallowed_charset.length;i++){
		data_in=data_in.replace(disallowed_charset[i],"");
	}
	return data_in;
}

function check_number_valid(num_data){
	if(!num_data){
		return false;
	}
	if(num_data<0){
		return false;
	}
	var num_charset="0123456789.";
	for(var i=0;i<num_data.length;i++){
		if(num_charset.indexOf(num_data.substring(i,i+1))==-1){
			return false;
		}
	}return true;
}

function format_number_output(number_input,use_decimal,use_commas,currency_sign){
	var tmp_num=parseFloat(number_input);
	if(isNaN(parseInt(number_input))){
		return false;
	}
	if(use_decimal){
		var decimal_num=use_decimal;
		tmp_num=tmp_num.toFixed(decimal_num);
		var tmp_num_string=tmp_num.toString();
		var decimal_value="";
		decimal_position=tmp_num_string.indexOf(".");
		decimal_value=tmp_num_string.substring(decimal_position+1);
		if(decimal_value==0){
			tmp_num=Math.round(tmp_num);
		}
	}
	if(use_commas){
		tmp_num=format_number_add_commas(tmp_num,'.','.',use_commas);
	}
	if(currency_sign){
		tmp_num=(currency_sign+tmp_num);
	}
	return tmp_num;
}

function format_number_add_commas(numStr,inD,outD,sep){
	numStr+='';
	var dpos=numStr.indexOf(inD);
	var numStrEnd='';if (dpos!=-1){numStrEnd=outD+numStr.substring(dpos+1,numStr.length);numStr=numStr.substring(0,dpos);}
	var rgx=/(\d+)(\d{3})/;
	while(rgx.test(numStr)){
		numStr=numStr.replace(rgx,'$1'+sep+'$2');
	}
	return numStr+numStrEnd;
}

function gross_profit_calculator(form_name){var gp_margin=strip_common_elements(document.getElementById(form_name).gp_margin.value);var gp_sales=strip_common_elements(document.getElementById(form_name).gp_sales.value);var error_array=new Array;var error_count=0;if(!check_number_valid(gp_sales)){error_array[error_count]="\tThe sales revenue field";error_count++;}if(!check_number_valid(gp_margin)){error_array[error_count]="\tThe profit margin field";error_count++;}if(error_array.length>0){var error_message;error_message=(error_array.length>1)?"The following fields are incorrect. Please check the fields and try again:\n\n":"The following field is incorrect. Please check the fields and try again:\n\n";for(i=0;i<error_array.length; i++){error_message+=error_array[i];error_message+="\n";}document.getElementById(form_name).gp_profit.value='';document.getElementById(form_name).gp_cost.value='';alert(error_message);return false;}var gp_profit=0;var gp_cost=0;gp_profit=(gp_margin*(gp_sales/100));gp_cost=(gp_sales-gp_profit);document.getElementById(form_name).gp_cost.value=format_number_output(gp_cost,2,',','$');document.getElementById(form_name).gp_profit.value=format_number_output(gp_profit,2,',','$');return false;}function gross_profit_margin_calculator(form_name){var gpm_sales=strip_common_elements(document.getElementById(form_name).gpm_sales.value);var gpm_cost=strip_common_elements(document.getElementById(form_name).gpm_cost.value);var error_array=new Array;var error_count=0;if(!check_number_valid(gpm_sales)){error_array[error_count]="\tThe sales revenue field";error_count++;}if(!check_number_valid(gpm_cost)){error_array[error_count]="\tThe total cost field";error_count++;}if(error_array.length>0){var error_message;error_message=(error_array.length>1)?"The following fields are incorrect. Please check the fields and try again:\n\n":"The following field is incorrect. Please check the fields and try again:\n\n";for(i=0;i<error_array.length;i++){error_message+=error_array[i];error_message+="\n";}document.getElementById(form_name).gpm_profit.value='';document.getElementById(form_name).gpm_margin.value='';alert(error_message);return false;}var gpm_profit=0;var gpm_margin=0;gpm_profit=(gpm_sales-gpm_cost);gpm_margin=(((gpm_sales-gpm_cost)/gpm_sales)*100);document.getElementById(form_name).gpm_profit.value=format_number_output(gpm_profit,2,',','$');document.getElementById(form_name).gpm_margin.value=format_number_output(gpm_margin,2,',')+'%';return false;}
