
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


var count=1;
var rows = new Array(150);
var namearray= new Array(150);
var	amountarray = new Array(150);
var	costarray= new Array(150);	
var printRows=new Array(150);
var totalamount=0;
var totalcost=0;
function calculate(theform){

	var cost;
	var from;
		cost = document.getElementById('indivcost').value;

	if(theform.fromperiod[0].checked){
		from=365;
	}
	else if(theform.fromperiod[1].checked){
		from=250;
	}
	else if(theform.fromperiod[2].checked){
		from=125;
	}
	else if(theform.fromperiod[3].checked){
		from=52;	
	}
	else if(theform.fromperiod[4].checked){
		from=12;
	
	}
	
	var finalval=(cost*from);	 
	var earnings;
	var earnings ;
	
	

	
	if((document.getElementById('hourlysalary').value=='')){
		earnings=document.getElementById('annualsalary').value * 0.67;
		document.getElementById('storedAnnualSalary').value=document.getElementById('annualsalary').value
		
	}
	else{
		earnings  = (document.getElementById('hourlysalary').value * 35) *  0.67 * 52
		document.getElementById('storedHourlySalary').value=document.getElementById('hourlysalary').value
		
	}
	
	
	

	//worktime = ( Math.round((((finalval / earnings) * 100)  / 100)/52));
	
	worktime = Math.round(finalval / (earnings/52) * 10)  / 10;
	
	
	if (worktime>52){
			document.getElementById('salaryError').style.display="block";
			document.getElementById('indivResult').style.display  = "none";
			return false;
		
	}
	
	worklife = finalval * 45;

	
	
	
	document.getElementById('spendayear').innerHTML= formatCurrency(finalval);
	document.getElementById('workwidth').innerHTML= worktime;
	document.getElementById('workinglife').innerHTML= formatCurrency(worklife);
	document.getElementById('spendingon').innerHTML = document.getElementById('spenddesc').value;




	namearray[count]=document.getElementById('spenddesc').value;
	amountarray[count]=finalval;
	costarray[count]=worktime;
    rows[count] = constructRow(namearray[count],amountarray[count],costarray[count],count);
	printRows[count]= constructPrintRow(namearray[count],amountarray[count],costarray[count],count);
	
	document.getElementById('indivResult').style.display  = "block";
	document.getElementById('clearTableButton2').style.display  = "block";
	
	document.getElementById('printButton').style.display  = "block";
	document.getElementById('discussDemov').style.display  = "block";
	
	document.getElementById('salaryError').style.display="none";
	
	document.getElementById('insertAllFieldsError').style.display="none";
	document.getElementById('extraLinks').style.display  = "block";
	
	//document.getElementById('demotivateMeButton').style.display="none";
	
	document.getElementById('smileyResult').style.display="none";
	
	count=count+1;
	document.getElementById('calculatedtable').innerHTML =buildTable(rows,amountarray,costarray);
	window.location = String(window.location).replace(/\#.*$/, "") + "#resultslocation";
	

}


function constructTotalRow(amountarray,costarray){
	return "<td style=\"border:1px solid #FF6600;\">Total</td><td style=\"border:1px solid #FF6600;\">£"+formatCurrency(amountarray)+"</td><td style=\"border:1px solid #FF6600;\">"+(Math.round(costarray * 100)  / 100) +" weeks</td><td style=\"border:1px solid #FF6600;\">&nbsp;</td>";
	
}

function constructTotalPrintRow(amountarray,costarray){
	return "<td style=\"border:1px solid black;\">Total</td><td style=\"border:1px solid black;\">£"+formatCurrency(amountarray)+"</td><td style=\"border:1px solid black;\">"+(Math.round(costarray * 100)  / 100) +" weeks</td>";
	
}

function constructRow(namearray,amountarray,costarray,count){

	return "<td style=\"border:1px solid #FF6600;\">"+namearray+"</td><td style=\"border:1px solid #FF6600;\">£"+formatCurrency(+amountarray)+"</td><td style=\"border:1px solid #FF6600;\">"+costarray+" weeks</td><td style=\"border:1px solid #FF6600;\"><input type=\"image\"value=\"X\" src=\"./DM_Box3_TableClear_trans.gif\" onclick=\"remove("+count+")\"></td>";	

}

function constructPrintRow(namearray,amountarray,costarray,count){

	return "<td style=\"border:1px solid black;\">"+namearray+"</td><td style=\"border:1px solid black;\">£"+formatCurrency(+amountarray)+"</td><td style=\"border:1px solid black;\">"+costarray+"</td>";	

}

function buildTable(rows,amountarray,costarray){
	var data='';
	totalamount=0;
	totalcost=0;
	var color=0
	for (x=1;x<=150;x++){
	if (rows[x]!=null){
		if (color==1){
			data +="<tr style=\"; font-style:italic;font-weight: bold; font-size: 12pt;color:#9900CC; \">"+	rows[x]+"</tr>" ;
			color=0;
			
		}
		else{
			data +="<tr style=\"background:white;font-style:italic;font-weight: bold; font-size: 12pt;color:#9900CC;\">"+	rows[x]+"</tr>" ;	
			color=1;
		}
		
		totalamount=totalamount+(amountarray[x]*1);	 
		
		totalcost=totalcost+(costarray[x]*1);	
	}
	else if (x<5){
			data +="<tr style=\"; font-style:italic;font-weight: bold; font-size: 12pt;color:#9900CC; \"><td style=\"border:1px solid #FF6600;\">&nbsp;</td><td style=\"border:1px solid #FF6600;\">&nbsp;</td><td style=\"border:1px solid #FF6600;\">&nbsp; </td><td style=\"border:1px solid #FF6600;\">&nbsp;</td></tr>" ;
		}
	
	}
	
	 data += "<tr style=\"background:#FFC06C;font-weight: bold; font-size: 14pt;color:black;\">"+constructTotalRow(totalamount,totalcost)+"</tr>";
	var fulltable;

	fulltable= "<div align=center><div id=\"clearTableButton\" style=\"background:url(http://images2.moneysavingexpert.com/images/demotivator/DM_Box3_HeaderSliver.gif) repeat-x; height:43px;\" ><div style=\"background:url(http://images2.moneysavingexpert.com/images/demotivator/DM_Box3_Skull.gif) no-repeat left; height:42px;\"><div style=\"background:url(DM_Box3_Skull.gif) no-repeat right;\"><img src=\"http://images2.moneysavingexpert.com/images/demotivator/DM_Box3_HeaderText.gif\"></div></div></div><table id=\"resulttable\" width=\"100%\"  cellspacing=\"0\" cellpadding=\"0\" style=\"text-align:center;font-size:10pt;border:1px solid #FF6600;\">		<tr style=\"background:#FFC06C;border:1px solid #FF6600;font-weight: bold; font-size: 14pt;\"><td style=\"border:1px solid #FF6600;\">Name</td><td style=\"border:1px solid #FF6600;\">Yearly Cost</td><td style=\"border:1px solid #FF6600;\">Cost in Working Life</td><td style=\"border:1px solid #FF6600;\">&nbsp;</td></tr>"+ data +"</table></div>";
	
	return fulltable;
}


function printVersion(){


var generator=window.open('','name','height=600,width=800, scrollbars=yes');
  
  generator.document.write('<html><head><title>Demotivator</title>');
totalamount=0;
	totalcost=0;
  
  var data='';
  var data='';
  var color=0
  for (x=1;x<=150;x++){
	if (printRows[x]!=null){
		if (color==1){
			data +="<tr style=\"background:#E5E7F3; font-style:italic;font-weight: bold; font-size: 12pt;color:black; height:25px; \" height=\"25px\">"+printRows[x]+"</tr>" ;
			color=0;	
		}
		else{
			data +="<tr style=\"background:white;font-style:italic;font-weight: bold; font-size: 12pt;color:black; height:25px;\" height=\"25px\">"+printRows[x]+"</tr>" ;	
			color=1;
		}
		totalamount=totalamount+(amountarray[x]*1);	 
		
		totalcost=totalcost+(costarray[x]*1);	
	}}
	var fulltable;
	 data += "<tr style=\"background:white;font-weight: bold; font-size: 14pt;color:black;\">"+constructTotalPrintRow(totalamount,totalcost)+"</tr>";
	generator.document.write('<div align=center><img src=\"http://images2.moneysavingexpert.com/images/demotivator/DM_NewMSE.gif\"  width=\"830px\" ><br/><br/><br/></div><div align=center style=\"font-size:40pt; font-style:italic;font-weight: bold;\"><div><img src=http://images2.moneysavingexpert.com/images/demotivator/DM_Print_YouSpendHeader.gif></div><div align=center style=\" border-left:5px solid black;border-right:5px solid black; width:807px\"><div style=\"padding-top:15px;text-align:center\" align=center> &pound;'+formatCurrencyZeroDP(totalamount)+' <img src=\"http://images2.moneysavingexpert.com/images/demotivator/DM_Print_AYear.gif\"></div><div style=\"padding-top:15px;text-align:center \" align=center><img src=\"DM_Print_YouWork.gif\"> '+(Math.round(totalcost * 100)  / 100) +' <img src=\"http://images2.moneysavingexpert.com/images/demotivator/DM_Print_WeeksOfLife.gif\"></div><div style=\";padding-top:15px;text-align:center\" align=center><img src=\"DM_Print_topayfor.gif\"></div><div style=\"text-align:center\" align=center><img src=\"http://images2.moneysavingexpert.com/images/demotivator/DM_Print_OverAWorkingLife.gif\"> &pound;'+formatCurrencyZeroDP((totalamount*45))+'</div><div style=\"padding-top:15px;\"><img src=\"http://images2.moneysavingexpert.com/images/demotivator/DM_Print_ThinkWhat.gif\"></div></div><img src=http://images2.moneysavingexpert.com/images/demotivator/DM_Print_YouSpendFooter.gif></div><br/>');
	
	generator.document.write('<div align=center><img src=\"http://images2.moneysavingexpert.com/images/demotivator/DM_NewSpend.gif\" width=\"830px\"><br/><br/><br/><br/>');
	fulltable= "<img src=\"http://images2.moneysavingexpert.com/images/demotivator/DM_Print_TableHeader.gif\" width=\"811px\" height=\"47px\" style=\"border:2px solid black; border-bottom:0px;\"><table id=\"resulttable\" width=\"815px\"  cellspacing=\"0\" cellpadding=\"0\" style=\"text-align:center;font-size:10pt;border:1px solid black;\">		<tr style=\"border:1px solid black;font-weight: bold; font-size: 14pt;\"><td style=\"border:1px solid black;\">Name</td><td style=\"border:1px solid black;\">Yearly Cost</td><td style=\"border:1px solid black;\">Cost in Working Time</td></tr>"+ data +"</table></div>";
	

	generator.document.write(fulltable);

	

   
 

  generator.document.write('\<script\>window.print();\</script\></body></html>');
  
  generator.document.close();

}

function remove(value){
	

	rows[value]= null;
	namearray[value]= null;
	amountarray[value]= null;
	costarray[value]= null;
	
	document.getElementById('calculatedtable').innerHTML =buildTable(rows,amountarray,costarray);
	count=count+1;




}

function clearlist(){
	var count=1;
	for (x=1;x<=150;x++){
	printRows[x]=null;
	rows[x]= null;
	namearray[x]= null;
	amountarray[x]= null;
	costarray[x]= null;}
	document.getElementById('calculatedtable').innerHTML ='';
	
	document.getElementById('clearTableButton2').style.display="none";
	document.getElementById('printButton').style.display  = "none";
	document.getElementById('indivResult').style.display  = "none";
	document.getElementById('smileyResult').style.display="block";
	document.getElementById('demotivateMeButton').style.display="block"
	document.forms['demotivatorform'].reset();
	window.location = String(window.location).replace(/\#.*$/, "") + "#demovtop";
	
	

}

function addItem(){
	document.getElementById('indivResult').style.display  = "none";
	document.forms['demotivatorform'].reset();
	
	document.getElementById('annualsalary').value=document.getElementById('storedAnnualSalary').value
	document.getElementById('hourlysalary').value=document.getElementById('storedHourlySalary').value

	document.getElementById('smileyResult').style.display="block";
	document.getElementById('demotivateMeButton').style.display="block";

	window.location = String(window.location).replace(/\#.*$/, "") + "#demovtop";

}

function toggleSavingsField(){

	if(document.getElementById('savingsline').style.display=="none"){
		document.getElementById('savingsline').style.display="block";
		document.getElementById('saveresult').style.display="block";
		
	}
	else{
		document.getElementById('savingsline').style.display="none";
		document.getElementById('saveresult').style.display="none";
	}
}


function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	//return (((sign)?'':'-') + '$' + num + '.' + cents);
	return (((sign)?'':'-') + num + '.' + cents);
}

function formatCurrencyZeroDP(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	//return (((sign)?'':'-') + '$' + num + '.' + cents);
	return (((sign)?'':'-') + num);
}
function checkPopulated(){


document.getElementById('insertAllFieldsError').style.display="none";
document.getElementById('earnStar').style.display="none";
document.getElementById('descStar').style.display="none";
document.getElementById('costStar').style.display="none";
document.getElementById('numberError').style.display="none";



var salCheck
if((document.getElementById('hourlysalary').value=='')){
	salCheck=document.getElementById('hourlysalary').value;
}
else{
	salCheck=document.getElementById('annualsalary').value;	
	}



	if(!IsNumeric(document.getElementById('indivcost').value) || !IsNumeric(salCheck)){
		document.getElementById('numberError').style.display="block";
		return false;
	}
	else if  (document.getElementById('spenddesc').value==""){
		document.getElementById('insertAllFieldsError').style.display="block";
		document.getElementById('descStar').style.display="inline";
		return false;
	}
	else if(document.getElementById('indivcost').value=="")  
		{
		document.getElementById('insertAllFieldsError').style.display="block";
		document.getElementById('costStar').style.display="inline";
		return false;
		}
	else if ( document.getElementById('annualsalary').value=="" && document.getElementById('hourlysalary').value==""){
		document.getElementById('insertAllFieldsError').style.display="block";
		document.getElementById('earnStar').style.display="inline";
		return false;
	
	}
	else {
		
		return true
		
		}
}