/*Support for calculator functions*/

function DecimalPlaces(ipt, num) {  // ipt - number to format  num - number of decimal places
  var dec = Number(num); // edit arg
    dec = (dec<0) ? 0 : dec; // insure not negative
  var str = ipt.toString(); // edit arg
    str += (str.indexOf(".")==(-1)) ? "." : ""; // insure period
    for (var i=1; i<=dec; i++) { str += "0"; } // pad zeros
  var pos = str.indexOf("."); // find period
    pos += (dec>0) ? 1 : 0; // adjust for formatting
    return str.substr(0, pos+dec); // format and return
}

function displayIt(theStr,dname,classname){
  //if IE 4+ 
  if (document.all) {
    document.all[dname].className=classname;
    document.all[dname].innerHTML=theStr;}
  //else if NS 6 (supports new DOM) 
  else if (document.getElementById){ 
    rng = document.createRange(); 
    el = document.getElementById(dname); 
    rng.setStartBefore(el); 
    htmlFrag = rng.createContextualFragment(theStr); 
    while (el.hasChildNodes()) 
    el.removeChild(el.lastChild); 
    el.appendChild(htmlFrag); 
    el.className=classname; 
  }
} 

function goHelp(elNumber,show,buttons) {
var i, ok;

  if (typeof buttons=='undefined') {buttons=-1;}
  var ie=document.all;
  var helpSec;
  for (i=1;i<15;i++) {
    ok=-1;
    helpSec="help"+i;
    if (ie) {
      if (typeof document.all[helpSec]=='undefined') {ok=0;}
    } else {
      if (document.getElementById(helpSec)==null) {ok=0;}
    }
    if (ok) {
      helppane=(ie) ? document.all['help'+i] : document.getElementById('help'+i);
      helppane.style.visibility= "hidden";
      onclk=(ie) ? document.all['helpon'+i] : document.getElementById('helpon'+i);
      offclk=(ie) ? document.all['helpoff'+i] : document.getElementById('helpoff'+i);
      onclk.style.visibility= "visible";
      offclk.style.visibility="hidden";
    }
  }
  helppane= (ie) ? document.all['help'+elNumber] : document.getElementById('help'+elNumber);
  helppane.style.visibility= (show) ? "visible" : "hidden";
  iscontacthelp=(ie) ? document.all.contacthelp : document.getElementById('contacthelp');
  if (iscontacthelp) {iscontacthelp.style.visibility=(show) ? "hidden" : "visible";}
  if (buttons) {
    onclk=(ie) ? document.all['helpon'+elNumber] : document.getElementById('helpon'+elNumber);
    offclk=(ie) ? document.all['helpoff'+elNumber] : document.getElementById('helpoff'+elNumber);
    onclk.style.visibility= (show)?"hidden" : "visible";
    offclk.style.visibility= (show)?"visible" : "hidden";
  }
 } 

function error(numb,dNames) {
 switch (numb) {
  case 0 : displayIt('','dstat','SHL'); return;
  case 1 : displayIt('Select an age group','dstat','SHL'); break;
  case 2 : displayIt('Enter gross earnings','dstat','SHL'); break;
  case 3 : displayIt('You wish, ha ha ha','dstat','SHL'); break;
  case 4 : displayIt('Invalid tax code','dstat','SHL'); break;
  case 5 : displayIt('Enter a tax code','dstat','SHL'); break;
  case 6 : displayIt('Please enter your requirements','dstat','SHL'); break;
  case 7 : displayIt('Please enter your turnover','dstat','SHL'); break;
  case 8 : displayIt('Catchup not available alone','dstat','SHL'); break;
  case 9 : displayIt('Service too short','dstat','SHL'); break;
  case 10 : displayIt('Employee too old','dstat','SHL'); break;
  case 11 : displayIt('Employee too young','dstat','SHL'); break;
  case 12 : displayIt('Maximum months are 11','dstat','SHL'); break;
  case 13 : displayIt('Average weeks pay required ','dstat','SHL'); break;
  case 14 : displayIt('Select holiday year start month','dstat','SHL'); break;
  case 15 : displayIt('Select number of weeks annual paid holiday','dstat','SHL'); break;
  case 16 : displayIt('Select number of working days per week','dstat','SHL'); break;
  case 17 : displayIt('Select the date to calculate the holiday entitlement to','dstat','SHL'); break;
  case 18 : displayIt('Date outside tax year','dstat','SHL'); break;
  case 19 : displayIt('End date before start date','dstat','SHL'); break;
  case 20 : displayIt('Holidays already taken either equal or exceed this years total entitlement','dstat','SHL'); break;
  case 21 : displayIt('No days holiday have been accrued','dstat','SHL'); break;
  case 22 : displayIt('Contribution exceeds list price','dstat','SHL'); break;
  case 23 : displayIt('Invalid date','dstat','SHL'); break;
  case 24 : displayIt('List price required','dstat','SHL'); break;
  case 25 : displayIt('Employee does not qualify','dstat','SHL'); break;
  case 26 : displayIt('Enter number of sick days','dstat','SHL'); break;
  case 27 : displayIt('Exceeds sick days payable','dstat','SHL'); break;
  case 28 : displayIt('Enter the payment rate','dstat','SHL'); break;
  case 29 : displayIt('Enter the hours for a week','dstat','SHL'); break;
  case 30 : displayIt('Enter the work days in a week','dstat','SHL'); break;
  case 31 : displayIt('A day only has 24 hours','dstat','SHL'); break;
  case 32 : displayIt('Enter the expected date of birth or placement date','dstat','SHL'); break;
  case 33 : displayIt('Enter gross NI contribution','dstat','SHL'); break;
  case 34 : displayIt('Enter total SSP paid','dstat','SHL'); break;
  case 35 : displayIt('13% NI greater than SSP','dstat','SHL'); break;
  case 36 : displayIt('Enter the days not worked','dstat','SHL'); break;
  case 37 : displayIt('Period number too great for pay frequency','dstat','SHL'); break;
  case 38 : displayIt('Enter the days worked','dstat','SHL'); break;
  case 39 : displayIt('Enter CO2 emmisions','dstat','SHL'); break;
 default : displayIt('Undefined error','dstat','SHL'); return;
 }
  if (dNames.length>0) {
   for(i=0;i<dNames.length;i++){ 
    displayIt("",'d'+dNames.charAt(i),'SHL'); }
  }
}

function stripJunk(checkStr,junkStr) {
 if (checkStr.length && junkStr.length) {
  for(i=0;i<junkStr.length;i++){
   while (checkStr.indexOf(junkStr.charAt(i))!=-1) {checkStr=checkStr.replace(junkStr.charAt(i),'');}
  }
  return checkStr;
 }
}

function evalDate(element,dNames) {
	if(verifyDate(element.value)) {
		var en = formatDate(verifyDate(element.value));
		en = unformatDate(en);
		element.value = verifyDate(en);
		return true;
	}
	else { 
if (!element.id=="EWCDateStart" || !element.id=="LeaveDateStart") {
		error(23,dNames);
		element.select();
		element.focus();
}
		return false;
	}
}

function formatDate(dateString) {
	var day1, month1, year1;
	year1 = dateString.substring(6, 10);
	month1 = dateString.substring(3, 5);
	day1 = dateString.substring(0, 2);
	return year1 + month1 + day1;
}

function unformatDate(dateString) {
	var day1, month1, year1;
	year1 = dateString.substring(0, 4);
	month1 = dateString.substring(4, 6);
	day1 = dateString.substring(6, 8);
	return day1 + "/" + month1 + "/" + year1;
}

function verifyDate(str) {
	var ret = "";
	if (str.length==8) { str=str.substring(0,2)+"./"+str.substring(2,4)+"./"+str.substring(4,8);}
	if (str!="" && str==str.replace(/[^-\/0-9]/g, "")) {
		str = str.replace(/\//g, "-");
		var pdate = str.split("-");
		if (pdate.length==3) {
			var d=0; var m=0; var y=0;
			d = toInt(pdate[0]);
			m = toInt(pdate[1]);
			y = pdate[2];
			if(y<1850) {
				if(y<100 && y>0) {
					//presume 2 figure:
					if ( y<20) { y = toInt(y) + 2000; }
					else { y = toInt(y) + 1900; }
				}
				else { y = 0; }
			} else { if(y > 2050) { y = 0; } }
			if (d>0 && m>0 && y>0) {
				var vdate = new Date(y, (m-1), d);
				if (d==vdate.getDate() && (m-1)==vdate.getMonth() && y==vdate.getFullYear()) {
					if (d<10) { ret = ret + "0"; }
					ret = ret + String(d) + "/";
					if (m<10) { ret = ret + "0"; }
					ret = ret + String(m) + "/" + String(y);
				}
			}
		}
	}
	return ret;
}

function toInt(str) {
	var ret=0;
	var state=1;
	str = String(str);
	str = str.replace(/N| |,/g, "");
	if (str.charAt(0) == "-") {
		state = -1;
		str = str.replace(/-/g, "");
	}
	if (!isNaN(str)) {
		while (str.length>1 && str.charAt(0)=="0") {
			str = str.substr(1, str.length-1);
		}
		ret = parseInt(str) * state;
	}
	return ret;
}

/*CODE END*/

