// JavaScript Document

		//for WebTrends tracking
		var trackimage = new Image;
		function track(ID){
			trackimage.src = "flash/dummy.swf" + ID;
		}

		function initFlash(){
			if (qs.get("flash")!="no"){// for debugging HTML: set ?flash=no 
				var so = new SWFObject("flash/main_shell.swf", "Flash", "1000", "525", "9", "#0066a3");
				document.getElementById("container").style.visibility = 'visible';
				so.write("main");
			}
			else{
				document.getElementById("container").style.visibility = 'visible';
			}
		}

		function initialize(){
			//populates calculator YES page from querystring
			if (qs.get("available") > ""){
				document.getElementById("txtAvailable").innerHTML = CurrencyFormattedString(unescape(qs.get("available")));
			}
		}
		
		function calculateEligibility(){
			//determine elegibility and go to Yes or No
			balance = document.forms['frmCalculate'].txtBalance.value
			value =  document.forms['frmCalculate'].txtValue.value
			if (balance > "" &&  value > ""){//ensure non-null entries
				balance = stripChars(balance);
				value = stripChars(value);
				if 	(value > 0 && balance > 0){//ensure non-bogus values
					available =  parseInt(value)*4/5 - parseInt(balance);
					if 	(available > 0){//the actual decision
						location.href = "pp_calcYes.html?available=" + escape(CurrencyFormattedValue(available));
					}
					else{
						location.href = "pp_calcNo.html";
					}
				}
			}
			return false;
		}


		function calc80(){
			//auto-populate houseValue field
			var houseValue = parseInt(stripChars(document.getElementById("txtValue").value));
			if (houseValue > 0){
				document.getElementById("autofield").value = CurrencyFormattedString(parseInt(houseValue*.8));
			}
			else{
				document.getElementById("autofield").value = "";
			}
			document.getElementById("txtBalance").focus();
		}

		function stripChars(amount){
			amount = amount.replace(",","");
			amount = amount.replace("$","");
			return amount
		}
		
		var isIE = document.all;
		var reValidChars = /\d/; //allow numbers only
		var reKeyboardChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/; //allow special chars (backsp, del, home etc.)
		var reValidString = /^\d*$/;//ensures pasted content is valid

		function maskInput(objEvent){
			var iKeyCode, strKey;
			if (isIE){
				iKeyCode = objEvent.keyCode;
				objInput = objEvent.srcElement;
			} 
			else{
				iKeyCode = objEvent.which; 
				objInput = objEvent.target;
			}
			strKey = String.fromCharCode(iKeyCode);
			if (isValid(objInput.value)) {//checks for valid pasted code
				objInput.validValue = objInput.value;
					if (!reValidChars.test(strKey) && !reKeyboardChars.test(strKey) ) {
       				//alert("Invalid Character Detected!\nKeyCode = " + iKeyCode + "\nCharacter =" + strKey);
       				return false;
				}
			}
		}
		
		function isValid(strValue) {
			//test for right-click pasting of illegal values into inoput field
			return reValidString.test(strValue) || strValue.length == 0;
		}

		function CurrencyFormattedString(amount){
			// adds commas and $
			// called when outputting
			var delimiter = ","; // replace comma if desired
			var i = parseInt(amount);
			if(isNaN(i)) { return ''; }
			var minus = '';
			if(i < 0) { minus = '-'; }
			i = Math.abs(i);
			var n = new String(i);
			var a = [];
			while(n.length > 3)
			{
				var nn = n.substr(n.length-3);
				a.unshift(nn);
				n = n.substr(0,n.length-3);
			}
			if(n.length > 0) { a.unshift(n); }
			n = a.join(delimiter);
			amount = n
			amount = minus + amount;
			return "$" + amount;
		}

		function CurrencyFormattedValue(amount){
			//converts value to dollar format
			// called when values are input
			var i = parseInt(amount);
			if(isNaN(i)) { i = 0.00; }
			var minus = '';
			if(i < 0) { minus = '-'; }
			i = Math.abs(i);
			i = parseInt((i + .005) * 100);
			i = i / 100;
			s = new String(i);
			//if(s.indexOf('.') < 0) { s += '.00'; } - not necessary
			//if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
			s = minus + s;
			return s;
		}
		
		



		/* Client-side access to querystring name=value pairs
			Version 1.2.3
			22 Jun 2005
			Adam Vandenberg
		*/
		var qs = new Querystring()
		
		function Querystring(qs) { // optionally pass a querystring to parse
			this.params = new Object()
			this.get=Querystring_get
			
			if (qs == null)
				qs=location.search.substring(1,location.search.length)
		
			if (qs.length == 0) return
		
		// Turn <plus> back to <space>
		// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
			qs = qs.replace(/\+/g, ' ')
			var args = qs.split('&') // parse out name/value pairs separated via &
			
		// split out each name=value pair
			for (var i=0;i<args.length;i++) {
				var value;
				var pair = args[i].split('=')
				var name = unescape(pair[0])
		
				if (pair.length == 2)
					value = unescape(pair[1])
				else
					value = name
				
				this.params[name] = value
			}
		}
		
		function Querystring_get(key, default_) {
			// This silly looking line changes UNDEFINED to NULL
			if (default_ == null) default_ = null;
			
			var value=this.params[key]
			if (value==null) value=default_;
			
			return value
		}