﻿	function fmtMoney(CtlID)
	{
		document.all(CtlID.id).value = formatCurrency(document.all(CtlID.id).value);
	}
	
	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));
		
		if (cents == '00'){
			return (((sign)?'':'-') + '$' + num);
		}
		else{
			return (((sign)?'':'-') + '$' + num + '.' + cents);
		}
	}
	
	function checkForNegativeValue(oSrc, args) {
		args.Value = replaceSubstring(args.Value,'$','');
		args.Value = replaceSubstring(args.Value,',','');
		if(args.Value < 0.0) {
			args.IsValid = false;
		}
	}
	
	
	function validateDwellingValue(oSrc, args) {
		args.Value = replaceSubstring(args.Value,'$','');
		args.Value = replaceSubstring(args.Value,',','');
		args.IsValid = (args.Value > 14999.99 && args.Value < 200000.01);
	}
	
	
	function LiabCheck(oSrc, args) {
		args.Value = replaceSubstring(args.Value,' ','');
		args.Value = replaceSubstring(args.Value,'-','');
		if((document.all.rblPersonalLiab_0.checked) && (args.Value == '0'))  {
			args.IsValid = false;
		}
	}
	
	function OwnerCheck(oSrc, args) {
		args.Value = replaceSubstring(args.Value,' ','');
		args.Value = replaceSubstring(args.Value,'-','');
		if((document.all.rblOwners_0.checked) && (args.Value == '0'))  {
			args.IsValid = false;
		}
	}
	
	function MineSubCheck(oSrc, args) {
		args.Value = replaceSubstring(args.Value,' ','');
		args.Value = replaceSubstring(args.Value,'-','');
		if((document.all.rblMineSubCov_0.checked) && (args.Value == 'Select'))  {
			args.IsValid = false;
		}
	}
	
	function VacantLiabCheck(oSrc, args) {
		args.Value = replaceSubstring(args.Value,' ','');
		args.Value = replaceSubstring(args.Value,'-','');
		if((document.all.rblVacantLiab_0.checked) && (args.Value == '0'))  {
			args.IsValid = false;
		}
	}
	
	function CompareA_B(oSrc, args) {
		var insValue = document.getElementById('txtDwellingValue').value;
		insValue = replaceSubstring(insValue,'$','');
		insValue = replaceSubstring(insValue,',','');

		args.Value = replaceSubstring(args.Value,'$','');
		args.Value = replaceSubstring(args.Value,',','');
		args.IsValid = ((insValue * .5) >= args.Value);
	}
	
	function CompareA_CVacant(oSrc, args) {
		var insValue = document.getElementById('txtDwellingValue').value;
		insValue = replaceSubstring(insValue,'$','');
		insValue = replaceSubstring(insValue,',','');

		args.Value = replaceSubstring(args.Value,'$','');
		args.Value = replaceSubstring(args.Value,',','');
		args.IsValid = ((insValue * .1) >= args.Value);
	}
	
	function CompareA_CDP1(oSrc, args) {
		var insValue = document.getElementById('txtDwellingValue').value;
		insValue = replaceSubstring(insValue,'$','');
		insValue = replaceSubstring(insValue,',','');
		
		var ocType = document.getElementById('ddlOccupancy').value;
		var cusValidator = document.getElementById('custPD1Personal');

		args.Value = replaceSubstring(args.Value,'$','');
		args.Value = replaceSubstring(args.Value,',','');
		
		if(ocType == 1) {
		args.IsValid = ((insValue * .7) >= args.Value);
		}
		
		if(ocType == 2 || ocType == 3) {
		args.IsValid = ((insValue * .5) >= args.Value);
		}
	}
	
	function Compare_Related(oSrc, args) {
		args.Value = replaceSubstring(args.Value,'$','');
		args.Value = replaceSubstring(args.Value,',','');
		
		if(document.all.rblPrivateStruc_0.checked) {
			if(args.Value < 1) {
			args.IsValid = false;
			}
		}
	}
	
	function Compare_Personal(oSrc, args) {
		args.Value = replaceSubstring(args.Value,'$','');
		args.Value = replaceSubstring(args.Value,',','');
		
		if(document.all.rblPersonalProp_0.checked) {
			if(args.Value < 1) {
			args.IsValid = false;
			}
		}
	}
	
	function Compare_AddedLiving(oSrc, args) {
		args.Value = replaceSubstring(args.Value,'$','');
		args.Value = replaceSubstring(args.Value,',','');
		
		if(document.all.rblAddedCosts_0.checked) {
			if(args.Value < 1) {
			args.IsValid = false;
			}
		}
	}
	
	function Compare_Burglary(oSrc, args) {
		args.Value = replaceSubstring(args.Value,'$','');
		args.Value = replaceSubstring(args.Value,',','');
		
		if(document.all.rblBurglary_0.checked) {
			if(args.Value < 1) {
			args.IsValid = false;
			}
		}
	}
	
	function replaceSubstring(inputString, fromString, toString) {
		// Goes through the inputString and replaces every occurrence of fromString with toString
		var temp = inputString;
		if (fromString == "") {
			return inputString;
		}
		if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
			while (temp.indexOf(fromString) != -1) {
				var toTheLeft = temp.substring(0, temp.indexOf(fromString));
				var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
				temp = toTheLeft + toString + toTheRight;
			}
		} else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
			var midStrings = new Array("~", "`", "_", "^", "#");
			var midStringLen = 1;
			var midString = "";
			// Find a string that doesn't exist in the inputString to be used
			// as an "inbetween" string
			while (midString == "") {
				for (var i=0; i < midStrings.length; i++) {
					var tempMidString = "";
					for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
					if (fromString.indexOf(tempMidString) == -1) {
					midString = tempMidString;
					i = midStrings.length + 1;
					}
				}
			} // Keep on going until we build an "inbetween" string that doesn't exist
			// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
			while (temp.indexOf(fromString) != -1) {
				var toTheLeft = temp.substring(0, temp.indexOf(fromString));
				var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
				temp = toTheLeft + midString + toTheRight;
			}
			// Next, replace the "inbetween" string with the "toString"
			while (temp.indexOf(midString) != -1) {
				var toTheLeft = temp.substring(0, temp.indexOf(midString));
				var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
				temp = toTheLeft + toString + toTheRight;
			}
		} // Ends the check to see if the string being replaced is part of the replacement string or not
		return temp; // Send the updated string back to the user
	}
	
	///////////////////////////////////////////////////////////////////////////////////////
	// PAGE PRINT CODE ////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////
	var mainContentsClass;
	
	function startPrint(){

		var mainContents = document.getElementById('MainContents');

		window.onafterprint = endPrint;

		// Save the MainContents's Class so we can reset it
		mainContentsClass = mainContents.className;

		// Set the style and dimensions of the MainContents to cover the screen
		mainContents.className = 'clsLayout_Print';
		mainContents.style.height = document.body.clientHeight;
		mainContents.style.width = document.body.clientWidth;
		
		window.print();
	}

	function endPrint(){
		
		var mainContents = document.getElementById('MainContents');
		
		// Reset the MainContents dimensions and Class back to normal
		mainContents.className = mainContentsClass;
		mainContents.style.height = '100%';
		mainContents.style.width = '100%';
		
		window.onafterprint = null;
		
	}	