function setCurrency(obj) {
	obj.value = formatCurrency(numericReal(obj.value));
}
function setCurrency2(obj) {
	obj.value = formatCurrency2(numericReal(obj.value));
}

function setNumeric(obj) {
	obj.value = formatNumeric(numericInt(obj.value));
}

function setNumeric2(obj) {
	obj.value = formatNumeric2(numericReal(obj.value));
}

function formatCurrency(num) {
	if ( (isNaN(num)) || (num == 0) )
		return "";
	num = Math.round(num); //added by DSB	
	elems = currency_body(num);		
	return (((elems[0])?'':'-') + '$' + elems[1]);
}

function formatCurrency2(num) {
	if ( (isNaN(num)) ) //if ( (isNaN(num)) || (num == 0) )
		return "";
	elems = currency_body(num);	
	return (((elems[0])?'':'-') + '$' + elems[1] + '.' + elems[2]);
}
function formatNumeric(num) {
	if ( (isNaN(num)) || (num == 0) )
		return "";
	num = Math.round(num); //added by DSB	
	elems = currency_body(num);		
	return (((elems[0])?'':'-') + elems[1]);
}
function formatNumeric2(num) {
	if ( (isNaN(num)) || (num == 0) )
		return "";
	elems = currency_body(num);	
	return (((elems[0])?'':'-') + elems[1] + '.' + elems[2]);
}

function currency_body(num) {
	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 new Array(sign, num, cents);
}
//END format numbers to currency

//START Numeric Functions
function numericOnly(str) { //for legacy functions that we haven't converted yet.
	var strValidChars = "0123456789.-";
  var strChar;
  var numString = '';
	str = str+''; //convert to string to get the .length;
	if (str.length == 0) return '';	
	for (i = 0; i < str.length; i++) {
		strChar = str.charAt(i);
		if (strValidChars.indexOf(strChar) > -1) {
			numString = numString +''+strChar;
		}
	}
	return numString*1;
}
/*
function numericOnly(str) {
	numString = str.toString().replace(/[^0-9\^.\^-]/gi,""); //not sure if the "-" replace works...
	return numString;
}
*/
function numericReal(str) {
	var strValidChars = "0123456789.-";
  var strChar;
  var numString = '';	
	str = str+''; //convert to string to get the .length;
	for (i = 0; i < str.length; i++) {
		strChar = str.charAt(i);
		if (strValidChars.indexOf(strChar) > -1) {
			numString = numString +''+strChar;
		}
	}
	return numString*1;
}

function numericInt(str) {
	var strValidChars = "0123456789";
  var strChar;
  var numString = '';
	str = str+''; //convert to string to get the .length;
	for (i = 0; i < str.length; i++) {
		strChar = str.charAt(i);
		if (strValidChars.indexOf(strChar) > -1) {
			numString = numString +''+strChar;
		}
	}
	return numString*1;
}

function validateNumeric(obj) {
	currentLength = obj.value.length;	
	var newNum = numericInt(obj.value)+'';
	newLength = newNum.length;
	if ( (currentLength != newLength) && (obj.value != '') ) {	
		alert("This field can only accept numbers that are greater than zero.");
		obj.value = numericInt(obj.value);
	}
}

function roundIt(num,places) {
	return Math.round(num*Math.pow(10,places))/Math.pow(10,places);
}
//END Numeric Functions

function highlight(obj) {
	obj.select();
}
function formatPhone(p) {
	p = numericOnly(p)+'';
	phone = '';
	if(p.length>3){
		phone = '('+p.substr(0,3)+') ';
	}
	if(p.length>5){
		phone = phone+p.substr(3,3)+'-'+p.substr(6,4);
	}
	return phone;
}



//START Format phone numbers
<!-- This script is based on the javascript code of Roman Feldblum (web.developer@programmer.net) -->
<!-- Original script : http://javascript.internet.com/forms/format-phone-number.html -->
<!-- Original script is revised by Eralper Yilmaz (http://www.eralper.com) -->
<!-- Revised script : http://www.kodyaz.com -->

var zChar = new Array('(', ')', ' ', '-', 'x');
var maxphonelength = 21;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object){
	phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object){
	phonevalue2 = ParseChar(object.value, zChar);
}

function formatPhoneUP(object,e) {
	if(e) {
		e = e
	} else {
		e = window.event
	}
	if(e.which){
		var keycode = e.which
	} else {
		var keycode = e.keyCode
	}

	ParseForNumber1(object);

	if(keycode >= 48){
		ValidatePhone(object)
	}
}

function formatPhoneDOWN(object,e) {
	if(e){
		e = e
	} else {
		e = window.event
	}
	if(e.which){
		var keycode = e.which
	} else {
		var keycode = e.keyCode
	}
	ParseForNumber2(object)
}

function GetCursorPosition(){
	var t1 = phonevalue1;
	var t2 = phonevalue2;
	var bool = false
	for (i=0; i<t1.length; i++) {
		if (t1.substring(i,1) != t2.substring(i,1)) {
			if(!bool) {
				cursorposition=i
				bool=true
			}
		}
	}
}

function ValidatePhone(object) {
	var p = phonevalue1;

//p = p.replace(/[^\d*?x\d*]/gi,"");
	p = p.replace(/[^\d*]/gi,"");

	if (p.length < 3) {
		object.value=p;
	} else if(p.length==3) {
		pp=p;
		d4=p.indexOf('(');
		d5=p.indexOf(')');
		if(d4==-1) {
			pp="("+pp;
		}
		if(d5==-1) {
			pp=pp+") ";
		}
		object.value = pp;
	} else if(p.length>3 && p.length < 7) {
		p ="(" + p;
		l30=p.length;
		p30=p.substring(0,4);
		p30=p30+") ";

		p31=p.substring(4,l30);
		pp=p30+p31;

		object.value = pp;

	} else if(p.length >= 7) {
		p ="(" + p;
		l30=p.length;
		p30=p.substring(0,4);
		p30=p30+") ";

		p31=p.substring(4,l30);
		pp=p30+p31;

		l40 = pp.length;
		p40 = pp.substring(0,9);
		p40 = p40 + "-";

// *JGK - Handle extensions. Add in the x
// after (nnn)nnn-nnnn to ensure extensions
// appear correctly (i.e. (nnn)nnn-nnnnxnnnnnn )
		if (l40>13) {
			p41 = pp.substring(9,13);			
			ppp = p40 + p41 + "x" + pp.substring(13,l40);
		} else {
			p41=pp.substring(9,l40);
			ppp=p40 + p41;
		}
		object.value = ppp.substring(0, maxphonelength);
	}

	GetCursorPosition();

	if(cursorposition >= 0){
		if (cursorposition == 0) {
			cursorposition = 2
		} else if (cursorposition <= 2) {
			cursorposition = cursorposition + 1
		} else if (cursorposition <= 6) {
			cursorposition = cursorposition + 2
		} else if (cursorposition == 7) {
			cursorposition = cursorposition + 2
		} else if (cursorposition == 8) {
			cursorposition = cursorposition + 4
			e1=object.value.indexOf(')')
			e2=object.value.indexOf('-')
			if (e1>-1 && e2>-1){
				if (e2-e1 == 5) {
					cursorposition = cursorposition - 1
				}
			}
		} else if (cursorposition < 12) {
			cursorposition = cursorposition + 3
		} else if (cursorposition == 12) {
			cursorposition = cursorposition + 1
		} else if (cursorposition >= 13) {
			cursorposition = cursorposition
		}

		var txtRange = object.createTextRange();
		txtRange.moveStart( "character", cursorposition);
		txtRange.moveEnd( "character", cursorposition - object.value.length);
		txtRange.select();
	}

}

function ParseChar(sStr, sChar) {
	if (sChar.length == null) {
		zChar = new Array(sChar);
	} else zChar = sChar;

	for (i=0; i<zChar.length; i++) {
		sNewStr = "";

		var iStart = 0;
		var iEnd = sStr.indexOf(sChar[i]);

		while (iEnd != -1) {
			sNewStr += sStr.substring(iStart, iEnd);
			iStart = iEnd + 1;
			iEnd = sStr.indexOf(sChar[i], iStart);
		}
		sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

		sStr = sNewStr;
	}

	return sNewStr;
}
//END Format phone numbers

//START Format SSN numbers
<!-- This script is based on the javascript code of Roman Feldblum (web.developer@programmer.net) -->
<!-- Original script : http://javascript.internet.com/forms/format-phone-number.html -->
<!-- Original script is revised by Eralper Yilmaz (http://www.eralper.com) -->
<!-- Revised script : http://www.kodyaz.com -->

var ssnChar = new Array('-');
var maxSSNLength = 11;
var ssnValue1;
var ssnValue2;
var ssnCursPos;

function ParseForSSNNum1(object){
	ssnValue1 = ParseSsnChar(object.value, ssnChar);
}
function ParseForSSNNum2(object){
	ssnValue2 = ParseSsnChar(object.value, ssnChar);
}

function formatSsnUP(object,e) {
	if(e) {
		e = e
	} else {
		e = window.event
	}
	if(e.which){
		var keycode = e.which
	} else {
		var keycode = e.keyCode
	}

	ParseForSSNNum1(object);

	if(keycode >= 48){
		ValidateSSN(object)
	}
}

function formatSsnDOWN(object,e) {
	if(e){
		e = e
	} else {
		e = window.event
	}
	if(e.which){
		var keycode = e.which;
	} else {
		var keycode = e.keyCode;
	}
	ParseForSSNNum2(object);
}

function GetSsnCursorPosition(){
	var t1 = ssnValue1;
	var t2 = ssnValue2;
	var bool = false;
	for (i=0; i<t1.length; i++) {
		if (t1.substring(i,1) != t2.substring(i,1)) {
			if(!bool) {
				ssnCursPos=i;
				bool=true;
			}
		}
	}
}

function ValidateSSN(object) {
	var p = ssnValue1;

	p = p.replace(/[^\d*]/gi,"");

	if (p.length < 3) {
		object.value=p;
		
	} else if(p.length==3) {
		pp=p;
		d4=p.indexOf('-');
		if(d4==-1) {
			pp=pp+"-";
		}
		object.value = pp;
		
	} else if((p.length>3) && (p.length<5)) {
		l30=p.length;
		p30=p.substring(0,3);
		p30=p30+"-";

		p31=p.substring(3,l30);
		pp=p30+p31;

		object.value = pp;

	} else if(p.length == 5) {
		p30=p.substring(0,3);
		p30=p30+"-";

		p31=p.substring(3,5);
		pp=p30+p31+"-";
		
		object.value = pp;
		
	} else if(p.length > 5) {
		l30 = p.length;
		p30=p.substring(0,3);
		pp=p30+"-"+p.substring(3,l30);

		l40 = pp.length;
		p40=pp.substring(0,6);
		p41=pp.substring(6,l40);
		ppp=p40+"-"+p41;
	
		object.value = ppp.substring(0, maxSSNLength);
	}

	GetSsnCursorPosition();

	if(ssnCursPos >= 0){
		if (ssnCursPos == 0) {
			ssnCursPos = 2
		} else if (ssnCursPos <= 2) {
			ssnCursPos = ssnCursPos + 1
		} else if (ssnCursPos >= 5) {
			ssnCursPos = ssnCursPos + 2
		} 
/*		var txtRange = object.createTextRange();
		txtRange.moveStart( "character", ssnCursPos);
		txtRange.moveEnd( "character", ssnCursPos - object.value.length);
		txtRange.select();*/
	}

}

function ParseSsnChar(sStr, sChar) {
	if (sChar.length == null) {
		ssnChar = new Array(sChar);
	} else ssnChar = sChar;

	for (i=0; i<ssnChar.length; i++) {
		sNewStr = "";

		var iStart = 0;
		var iEnd = sStr.indexOf(sChar[i]);

		while (iEnd != -1) {
			sNewStr += sStr.substring(iStart, iEnd);
			iStart = iEnd + 1;
			iEnd = sStr.indexOf(sChar[i], iStart);
		}
		sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

		sStr = sNewStr;
	}

	return sNewStr;
}
//END Format SSN numbers

function calcIns(loanAmt, insRate) {
	if ( (loanAmt != '') && (insRate != '') ){
		mtginspmt = (numericReal(insRate)/1200)*numericReal(loanAmt);		
		return formatNumeric2(mtginspmt);
	} else {
		return 0;
	}
}

function stateChanged(xmlHttp) { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		//parent.saveButtonClass = "button100_";
		if (xmlHttp.responseText != '') {
			document.getElementById('rates_div').innerHTML = xmlHttp.responseText;
			//determine if we have a success or a failure...
			if (xmlHttp.responseText.indexOf("....") != -1) { //success
				//start the timer...
				InitializeTimer(1);
//					setScroll('large_'+cid);
			}				
		}
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		//Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function loadPage(objDIV,pageUrl,params,waitPage) {	//objDIV is the object where the new data will be written
	
	if (waitPage != null) {
//		console.log(top);
		objDIV.innerHTML = top.frames.header.document.getElementById(waitPage).innerHTML;
		setScroll(objDIV);
	}	
				
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
	 alert ("You need to be using a Mozilla Firefox browser or at least IE 7.0");
	 return;
	}	
	urlParam="?sid="+Math.random();
//	xmlHttp.open("GET", "http://dev.secure.mconnects.com/" + pageUrl+urlParam+params, true);
	xmlHttp.open("GET", pageUrl+urlParam+params, true);
	xmlHttp.onreadystatechange = function() { returnPage(xmlHttp,objDIV); };	
	xmlHttp.send(null);
}
function returnPage(xmlHttp,objDIV) { //objDIV is where the results get written
	try {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			if (xmlHttp.status == 200) {	

				objDIV.innerHTML = xmlHttp.responseText;
				
			} else {
				alert('There was a problem returning the requested page. The status is: '+xmlHttp.status+' and the text is: '+xmlHttp.responseText);
			}
		} //There is no else clause because it may take a long time to execute the transaction...
	} catch( e ) {
		alert('Caught Exception -returnPage-: ' + e.description);
	}
}