var layerNameVariable;

var loadedLocation = "";

var isCSS, isW3C, isIE4, isNN4, isIE6CSS;



function initDHTMLAPI() {

	if(document.images) {

	isCSS = (document.body && document.body.style) ? true : false;

	isW3C = (isCSS && document.getElementById) ? true : false;

	isIE4 = (isCSS && document.all) ? true : false;

	isNN4 = (document.layers) ? true : false;

	isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >=0) ? true : false;

	}

}



//Converts object name string or object reference into valid element obj ref

function getRawObject(obj) {

var theObj;



	if(typeof obj == "string") {

		if(isW3C) {

			theObj = document.getElementById(obj);

		} else if (isIE4) {

			theObj = document.all(obj);

		} else if (isNN4) {

			theObj = seeLayer(document, obj);

		}



	} else {

		//pass through object ref

		theObj = obj;

	}

	if(theObj==null) {

		theObj = eval("document.forms[0]." + obj);

		}



	return theObj;

}

function trimAll(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}


function emailCheck (emailStr) {



/* The following variable tells the rest of the function whether or not

to verify that the address ends in a two-letter country or well-known

TLD.  1 means check it, 0 means don't. */



var checkTLD=1;



/* The following is the list of known TLDs that an e-mail address must end with. */



var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|COM|NET|ORG|EDU|INT|MIL|GOV|ARPA|BIZ|AERO|NAME|COOP|INFO|PRO|MUSEUM|com |net |org |edu |int |mil |gov |arpa |biz |aero |name |coop |info |pro |museum |COM |NET |ORG |EDU |INT |MIL |GOV |ARPA |BIZ |AERO |NAME |COOP |INFO |PRO |MUSEUM )$/;



/* The following pattern is used to check if the entered e-mail address

fits the user@domain format.  It also is used to separate the username

from the domain. */



var emailPat=/^(.+)@(.+)$/;



/* The following string represents the pattern for matching all special

characters.  We don't want to allow special characters in the address. 

These characters include ( ) < > @ , ; : \ " . [ ] */



var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";



/* The following string represents the range of characters allowed in a 

username or domainname.  It really states which chars aren't allowed.*/



var validChars="\[^\\s" + specialChars + "\]";



/* The following pattern applies if the "user" is a quoted string (in

which case, there are no rules about which characters are allowed

and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com

is a legal e-mail address. */



var quotedUser="(\"[^\"]*\")";



/* The following pattern applies for domains that are IP addresses,

rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal

e-mail address. NOTE: The square brackets are required. */



var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;



/* The following string represents an atom (basically a series of non-special characters.) */



var atom=validChars + '+';



/* The following string represents one word in the typical username.

For example, in john.doe@somewhere.com, john and doe are words.

Basically, a word is either an atom or quoted string. */



var word="(" + atom + "|" + quotedUser + ")";



// The following pattern describes the structure of the user



var userPat=new RegExp("^" + word + "(\\." + word + ")*$");



/* The following pattern describes the structure of a normal symbolic

domain, as opposed to ipDomainPat, shown above. */



var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");



/* Finally, let's start trying to figure out if the supplied address is valid. */



/* Begin with the coarse pattern to simply break up user@domain into

different pieces that are easy to analyze. */



var matchArray=emailStr.match(emailPat);



if (matchArray==null) {



/* Too many/few @'s or something; basically, this address doesn't

even fit the general mould of a valid e-mail address. */



alert("Email address seems incorrect (check @ and .'s)");

return false;

}




var user=matchArray[1];

var domain=trimAll(matchArray[2]);
//alert(domain);



// Start by checking that only basic ASCII characters are in the strings (0-127).



for (i=0; i<user.length; i++) {

if (user.charCodeAt(i)>127) {

alert("Ths username contains invalid characters.");

return false;

   }

}

//alert(charCodeAt(' '));

for (i=0; i<domain.length; i++) {

if (domain.charCodeAt(i)>127) {
/*alert(domain.charCodeAt(1));*/
alert("The domain name contains invalid characters.");

return false;

   }

}



// See if "user" is valid 



if (user.match(userPat)==null) {



// user is not valid



alert("The username doesn't seem to be valid.");

return false;

}



/* if the e-mail address is at an IP address (as opposed to a symbolic

host name) make sure the IP address is valid. */



var IPArray=domain.match(ipDomainPat);

if (IPArray!=null) {



// this is an IP address



for (var i=1;i<=4;i++) {

if (IPArray[i]>255) {

alert("Destination IP address is invalid!");

return false;

   }

}

return true;

}






// Domain is symbolic name.  Check if it's valid.

 

var atomPat=new RegExp("^" + atom + "$");

var domArr=domain.split(".");

var len=domArr.length;

for (i=0;i<len;i++) {

if (domArr[i].search(atomPat)==-1) {

alert("The domain name does not seem to be valid.");

return false;

   }

}



/* domain name seems valid, but now make sure that it ends in a

known top-level domain (like com, edu, gov) or a two-letter word,

representing country (uk, nl), and that there's a hostname preceding 

the domain or country. */



if (checkTLD && domArr[domArr.length-1].length!=2 && 

domArr[domArr.length-1].search(knownDomsPat)==-1) {

alert("The address must end in a well-known domain or two letter " + "country.");

return false;

}



// Make sure there's a host name preceding the domain.



if (len<2) {

alert("Your email address is missing a hostname");

return false;

}



return true;

}



function check_fld_val(objectName, errorMsg) {

	

	var obj = getRawObject(objectName);

	//alert (obj.type);

	

		if(obj.value == "") {

			alert(errorMsg);

			obj.focus();

			return false;

		} else {

			return true;

		}



}



function submit_doc(pageRef) {

	switch (pageRef) {

		case "support":

			if(!check_fld_val('name','Please enter your name')) return;

			if(!check_fld_val('phoneNum','Please enter your phone number')) return;

			if(!check_fld_val('email','Please enter your email')) return;

			

			var email = getRawObject('email');

			

			if(!emailCheck(email.value)) {

				//alert("The email that you entered isn't in a valid format. Please check and re-susbmit");

				email.focus();

				return;

			}

			

		if(!check_fld_val('supportIssue','Please enter details of your support issue')) return;

	
			window.document.forms[0].email.value = trimAll(email.value);
			window.document.forms[0].submit();

			return;

			
case "seminar":

			if(!check_fld_val('name','Please enter your name')) return;

			//if(!check_fld_val('position','Please enter your position')) return;

			//if(!check_fld_val('companyName','Please enter your Company Name')) return;

			//if(!check_fld_val('address','Please enter your Address')) return;
			
			if(!check_fld_val('phone','Please enter your phone number')) return;

			var email = getRawObject('email');
			
			if(!emailCheck(email.value)) {
				alert("The email that you entered isn't in a valid format. Please check and re-susbmit");
				email.focus();
				return;
			}

			window.document.forms[0].email.value = trimAll(email.value);
			window.document.forms[0].submit();

			return;

		case "contactus":



				if(!check_fld_val('name','Please enter your name')) return;
				
				if(!check_fld_val('email1','Please enter your email')) return;

				//if(!check_fld_val('position','Please enter your position')) return;

				//if(!check_fld_val('account_name','Please enter your company name')) return;

				if(!check_fld_val('phone_work','Please enter your phone number')) return;	


				//if(!check_fld_val('regular_users_c','Please enter the number of users')) return;

				

var email = getRawObject('email1');

				

				if(!emailCheck(email.value)) {

					//alert("The email that you entered isn't in a valid format. Please check and re-susbmit");

					email.focus();

					return;

					}



				//if(!check_fld_val('industry','Please enter your company industry')) return;

				//if(!check_fld_val('staffNo','Please enter the number of staff to use Signifo Expenses')) return;

				

			window.document.forms[0].email1.value = trimAll(email.value);
				window.document.forms[0].submit();

				return;
				case "contactus_1":



				if(!check_fld_val('name','Please enter your name')) return;
				
				if(!check_fld_val('email1','Please enter your email')) return;

				//if(!check_fld_val('position','Please enter your position')) return;

				//if(!check_fld_val('account_name','Please enter your company name')) return;

				if(!check_fld_val('phone_work','Please enter your phone number')) return;	


				//if(!check_fld_val('regular_users_c','Please enter the number of users')) return;

				

var email = getRawObject('email1');

				

				if(!emailCheck(email.value)) {

					//alert("The email that you entered isn't in a valid format. Please check and re-susbmit");

					email.focus();

					return;

					}



				//if(!check_fld_val('industry','Please enter your company industry')) return;

				//if(!check_fld_val('staffNo','Please enter the number of staff to use Signifo Expenses')) return;

				

			window.document.forms[0].email1.value = trimAll(email.value);
				mail();

				return;

			

		case "demo" :


				if(!check_fld_val('name','Please enter your name')) return;
				
				if(!check_fld_val('email1','Please enter your email')) return;

				//if(!check_fld_val('position','Please enter your position')) return;

				if(!check_fld_val('account_name','Please enter your company name')) return;

				if(!check_fld_val('phone_work','Please enter your phone number')) return;	


				if(!check_fld_val('regular_users_c','Please enter the number of users')) return;

				

var email = getRawObject('email1');

				

				if(!emailCheck(email.value)) {

					//alert("The email that you entered isn't in a valid format. Please check and re-susbmit");

					email.focus();

					return;

					}



				//if(!check_fld_val('industry','Please enter your company industry')) return;

				//if(!check_fld_val('staffNo','Please enter the number of staff to use Signifo Expenses')) return;

				

			window.document.forms[0].email1.value = trimAll(email.value);
				window.document.forms[0].submit();

				return;
				
						case "demo_1" :


				if(!check_fld_val('name','Please enter your name')) return;
				
				if(!check_fld_val('email1','Please enter your email')) return;

				//if(!check_fld_val('position','Please enter your position')) return;

				if(!check_fld_val('account_name','Please enter your company name')) return;

				if(!check_fld_val('phone_work','Please enter your phone number')) return;	


				if(!check_fld_val('regular_users_c','Please enter the number of users')) return;

				

var email = getRawObject('email1');

				

				if(!emailCheck(email.value)) {

					//alert("The email that you entered isn't in a valid format. Please check and re-susbmit");

					email.focus();

					return;

					}



				//if(!check_fld_val('industry','Please enter your company industry')) return;

				//if(!check_fld_val('staffNo','Please enter the number of staff to use Signifo Expenses')) return;

				

			window.document.forms[0].email1.value = trimAll(email.value);
				//window.document.forms[0].submit();
				mail();

				return;


/**********************************************************************/
case "Price" :

				if(!check_fld_val('name','Please enter your name')) return;
				var email = getRawObject('email');
				if(!emailCheck(email.value)) {

					//alert("The email that you entered isn't in a valid format. Please check and re-susbmit");

					email.focus();

					return;

					}
				if(!check_fld_val('company','Please enter your company')) return;
				if(!check_fld_val('phoneNum','Please enter your phone number')) return;		
				if(!check_fld_val('staffNo','Please enter Number of staff submitting expenses ')) return;
				//if(!check_fld_val('numbercorporatecards','Please enter Number of staff with corporate cards ')) return;		
				
				//if(!check_fld_val('cardissuer','Please enter your Card issuer')) return;		
				//if(!check_fld_val('software','Please enter your Accounting software')) return;	
				//if(!check_fld_val('requireoffline','Please enter Number of staff requiring the offline version')) return;	
				if(!check_fld_val('email','Please enter your email')) return;

				
			window.document.forms[0].email.value = trimAll(email.value);
				window.document.forms[0].submit();

				return;

				



				//if(!check_fld_val('industry','Please enter your company industry')) return;

				//if(!check_fld_val('staffNo','Please enter the number of staff to use Signifo Expenses')) return;

				


 case"envelopes":
				if(!check_fld_val('name','Please enter your name')) return;
				if(!check_fld_val('email','Please enter your email')) return;

				

				var email = getRawObject('email');

				

				if(!emailCheck(email.value)) {

					//alert("The email that you entered isn't in a valid format. Please check and re-susbmit");

					email.focus();

					return;

					}

				if(!check_fld_val('company','Please enter company name')) return;
				if(!check_fld_val('phoneNum','Please enter Telephonenumber')) return;
				if(!check_fld_val('numenvelope','Please enter Number ofenvelopes required')) return;
				if(!check_fld_val('printedonevelope','Please enter Address as you’d like it to be printed on the envelopes:')) return;
				if(!check_fld_val('envelopesend','Address you’d like the envelopes to be sent to')) return;
				window.document.forms[0].submit();

			window.document.forms[0].email.value = trimAll(email.value);
				return;
/***********************************************************************/
		case "trylive" :

				if(!check_fld_val('FirstName','Please enter your first name')) return;

				if(!check_fld_val('LastName','Please enter your last name')) return;

				//if(!check_fld_val('company','Please enter your company name')) return;

				//if(!check_fld_val('phoneNum','Please enter your phone number')) return;	

				if(!check_fld_val('email','Please enter your email')) return;

				

				var email = getRawObject('email');

				

				if(!emailCheck(email.value)) {

					//alert("The email that you entered isn't in a valid format. Please check and re-susbmit");

					email.focus();

					return;

					}



				//if(!check_fld_val('industry','Please enter your company industry')) return;

				//if(!check_fld_val('staffNo','Please enter the number of staff to use Signifo Expenses')) return;

				

			window.document.forms[0].email.value = trimAll(email.value);
				window.document.forms[0].submit();

				return;

			
/*********************************************************************************/

		case "cd" :

			if(!check_fld_val('name','Please enter your name')) return;

			if(!check_fld_val('position','Please enter your position')) return;

			if(!check_fld_val('company','Please enter your company name')) return;

			if(!check_fld_val('address1','Please enter your address')) return;

			if(!check_fld_val('town','Please enter your town')) return;

			if(!check_fld_val('postCode','Please enter your postcode')) return;

			

			if(!check_fld_val('phoneNum','Please enter your phone number')) return;	

			if(!check_fld_val('email','Please enter your email')) return;

			

			var email = getRawObject('email');

			

			if(!emailCheck(email.value)) {

				//alert("The email that you entered isn't in a valid format. Please check and re-susbmit");

				email.focus();

				return;

				}

			

			//if(!check_fld_val('industry','Please enter your company industry')) return;

			if(!check_fld_val('staffNo','Please enter the number of staff to use Signifo Expenses')) return;

					

			window.document.forms[0].email.value = trimAll(email.value);
			window.document.forms[0].submit();

			return;

			

		case "ecademy":	

			if(!check_fld_val('name','Please enter your name')) return;

			if(!check_fld_val('position','Please enter your position')) return;

			if(!check_fld_val('company','Please enter your company name')) return;

			if(!check_fld_val('phoneNum','Please enter your phone number')) return;	

			if(!check_fld_val('email','Please enter your email')) return;

			

			var email = getRawObject('email');

			

			if(!emailCheck(email.value)) {

				//alert("The email that you entered isn't in a valid format. Please check and re-susbmit");

				email.focus();

				return;

			}

			

			if(!check_fld_val('industry','Please enter your company industry')) return;

			

			window.document.forms[0].email.value = trimAll(email.value);
			window.document.forms[0].submit();

			return;

		

		case "rewards":

			window.document.forms[0].submit();

			return;		

		

		case "demodload":

			window.document.forms[0].submit();

			return;		

		

			}



}



function fmtPrice(value, addCents)

   {

   result=""+Math.floor(value);

   

   if(addCents) {

   	var cents=100*(value-Math.floor(value))+0.5;

   	result += ".";

   	result += Math.floor(cents/10);

   	result += Math.floor(cents%10);

   }

   return result;

   }



// Called by the COMPUTE button; computes and displays the formatted result

function compute()

   {

// the following var statements set the script the same way the spreadsheet rich wrote to calc the amounts

// work hours is per year

// userprice is the price of signifo expenses for a single user for one year

// the others are the decimal equiv of the percentage savings

	var workhours = 1736;

	var emptimesaving = 0.4;

	var bosstimesaving = 0.6;

	var acctimesaving = 0.8;

	var userprice = 40;

	var errortime = 0.03;

	var errorred = 0.8;

	var taxred = 0.8;



//check values

	var claimantVal = parseFloat(document.forms[0].claimant.value); 

	var claimsVal = parseFloat(document.forms[0].claims.value);



	var empTime = parseFloat(document.forms[0].emptime.value);

	var bossTime = parseFloat(document.forms[0].bosstime.value);

	var acctsTime = parseFloat(document.forms[0].acctstime.value);

	var prepTime = parseFloat(document.forms[0].preptime.value);



// calculate total time to process manually

	

	var claimsyr = ((claimantVal*claimsVal)*12);

	var timespent = (parseFloat(document.forms[0].emptime.value) + parseFloat(document.forms[0].bosstime.value) + parseFloat(document.forms[0].acctstime.value)) / 60;



	var manualtotal = (claimsyr*timespent)+prepTime;

	document.forms[0].manualtotal.value=Math.round(manualtotal);



// calculate after expenses

	var sigtime = 

	(

	claimsyr*(((empTime*(1-emptimesaving))+(bossTime*(1-bosstimesaving))+(acctsTime*(1-acctimesaving)))/60)+ (prepTime*(1-taxred))

	);

	

	sigtime = Math.round(sigtime);

	document.forms[0].sigtime.value=sigtime;



// calculate cost of manual processing

	var salary = parseFloat(document.forms[0].salary.value);

	var overhead = 1+(parseFloat(document.forms[0].overhead.value)/100);

	var hrcost = (salary * overhead) / workhours;

	var manualcost = manualtotal*hrcost;

	var finmanualcost = fmtPrice(manualcost, false);

	document.forms[0].manualcost.value=finmanualcost;



// calculate cost of signifo processing

	var sigcost = (salary * overhead) / workhours;

	sigcost = sigcost*sigtime

	//(userprice*(document.forms[0].claimant.value)+sigtime*hrcost);

	var finsigcost = fmtPrice(sigcost, false);

	document.forms[0].sigcost.value=finsigcost;



//travel and entertain reduction	

	var travel = parseFloat(document.forms[0].avgExpenseValue.value);

	var travelReduct = claimantVal*travel*12*0.1;

	travelReductFinal = fmtPrice(travelReduct);

	document.forms[0].travelReduction.value=travelReductFinal;



//gross saving

	var gSaving = manualcost-sigcost+travelReduct;

	var gSavingFinal = fmtPrice(gSaving);

	document.forms[0].grossSaving.value=gSavingFinal;



// calculate savings

	var pricePerSignifo = parseFloat(document.forms[0].signifoPrice.value);

	var difference = gSaving-(claimantVal*pricePerSignifo*12);

	difference = fmtPrice(difference, false);

	document.forms[0].difference.value=difference;





//	var costbefore = manualcost / ((document.forms[0].claimant.value)*(document.forms[0].claims.value)*12);

//	var fincostbefore = fmtPrice(costbefore, true);

//	document.forms[0].costbefore.value=fincostbefore;



//	var costafter = sigcost / ((document.forms[0].claimant.value)*(document.forms[0].claims.value)*12);

//	var fincostafter = fmtPrice(costafter,true);

//	document.forms[0].costafter.value=fincostafter;



//	var saving = ((costbefore - costafter) / costbefore)*100;

//	document.forms[0].saving.value=Math.round(saving);



//	var hrsaving = (manualtotal - sigtime);

//	document.forms[0].timesaved.value=Math.round(hrsaving);



//calculate ROI

	

	var roiValue = (gSaving/(pricePerSignifo*claimantVal*12)*100)-100;

//	var netSavings = (manualcost - sigcost);

//	var roiValue = (netSavings/sigcost)*100;

	document.forms[0].roiValue.value = Math.round(roiValue);

	

//calculate payback

//	var savingPerMth = netSavings/12;

	var payback =(pricePerSignifo*claimantVal*12)/(gSaving/12);

	document.forms[0].payback.value = Math.round(payback*100)/100;









}



function filterNum(str)

{

	re = /^\£|,/g;

// remove "£" and ","

	return str.replace(re, "");

}




