var checkZip = function(zip)
{
	zip = zip.replace(/^\s+|\s+$/, '');
	if ( zip.length < 5 )
	{
		alert("Please enter a valid zip code.");
		return "";
	}
	zip = zip.substring(0,5);
	if (zip != zip.match(/^[0-9]+$/)) 
	{
		alert("Please enter a valid zip code.");
        return "";
	}
	return zip;
};


var autoClicked = function()
{
	var zip = document.getElementById("txtZip").value;
	
	zip = checkZip(zip);
	if ( zip != "" )
    {          
		var f = document.getElementById("nextInsure");
	    f.zipcode.value = zip;
	    f.which.value = 1;
	    f.submit();
	}
};

keyDownZip = function(e)
{
	var code;
	if (document.all) {
		code = window.event.keyCode;
	}
	else {
		code = e.which;
	}
	if ( code == 13 ) {
		autoClicked();
	}
};

//holds dollar times global functions
DT_F = function() {};

//holds global variables
DT_V = function() {};

String.prototype.DT_trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};

DT_F.getFormFloat = function(el, unsetErr, nanError)
{
	var sVal = document.getElementById(el).value;
	sVal = sVal.DT_trim();
	if ( sVal.length === 0 ) {
		throw unsetErr;
	}
	sVal = DT_F.fixNumber(sVal);
	fVal = parseFloat(sVal);
	if ( isNaN(fVal) ) {
		throw nanError;
	}
	return fVal;
};

DT_F.getFormInt = function(el, unsetErr, nanError, roundMessage)
{
	var sVal = document.getElementById(el).value;
	sVal = sVal.DT_trim();
	if ( sVal.length === 0 ) {
		throw unsetErr;
	}
	sVal = DT_F.fixNumber(sVal);
	fVal = parseFloat(sVal);
	if ( isNaN(fVal) ) {
		throw nanError;
	}
	iVal = Math.round(fVal);
	if ( iVal != fVal ) {
		alert(roundMessage + " " + iVal);
	}
	return iVal;
};


DT_F.fixNumber = function(sVal)
{
	sVal = sVal.replace(/\$/g, "");
	sVal = sVal.replace(/\%/g, "");
	sVal = sVal.replace(/,/g, "");
	return sVal;
};


DT_F.addCommas = function(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
};

DT_F.round = function(n, dec)
{
	var s = n.toFixed(dec);
	return parseFloat(s);
};


DT_F.formatDollarsCents = function(x)
{
	y = DT_F.addCommas( Math.abs(x).toFixed(2) );
	if ( x < 0 ) {
		return "-$" + y;
	} else {
		return "$" + y;
	}
};

DT_F.formatDollars = function(x)
{
    x = Math.round(x);
	y = DT_F.addCommas( Math.abs(x) );
	if ( x < 0 ) {
		return "-$" + y;
	} else {
		return "$" + y;
	}
};


DT_F.calcPayment = function(loan, months, rate)
{
	var mRate = rate / 1200;
	var payment;

	// interest only
	if  ( months == -1 ) {
		return loan * mRate;
	}

	if ( rate === 0 )
	{
		payment = loan / months;
	}
	else
	{
		var factor = Math.pow(1 + mRate, months );
		payment = loan * ( mRate / (1 - 1/factor) );
	}
	return payment;
};

DT_F.setDateElements = function(dEl, mEl, yEl, d)
{
	document.getElementById(dEl).value = d.getDate();
	document.getElementById(mEl).value = d.getMonth();
	document.getElementById(yEl).value = d.getFullYear();
};

DT_F.isLeapYear = function(y)
{
	if( y % 4 === 0) {				
		if( y % 100 === 0) {	
			if ( y  % 400 === 0) {
				return true;
			}
			return false;
		}
		return true;
	}
	return false;
};

DT_F.getDaysInMonth = function(m, y) 
{
	switch (m)
	{
		case 0: return 31;
		case 1: 
			if ( DT_F.isLeapYear(y) ) {
				return 29;
			}
			return 28;
		case 2: return 31;
		case 3: return 30;
		case 4:	return 31;
		case 5: return 30;
		case 6:	return 31;
		case 7:	return 31;
		case 8:	return 30;
		case 9: return 31;
		case 10: return 30;
		case 11: return 31;
   }
   return -1;
};

//   1 = d1 later
//  -1 = d2 later
//   0 = same
DT_F.dateLater = function(d1, d2)
{
	if ( d1.getFullYear() > d2.getFullYear() ) {
		return 1;
	}
    if ( d1.getFullYear() < d2.getFullYear() ) {
		return -1;
	}
	if ( d1.getMonth() > d2.getMonth() ) {
		return 1;
	}
    if ( d1.getMonth() < d2.getMonth() ) {
		return -1;
	}
	if ( d1.getDate() > d2.getDate() ) {
		return 1;
	}
    if ( d1.getDate() < d2.getDate() ) {
		return -1;
	}
	return 0;
};

// Returns the difference in years between d1 and d2, where d2 is later than d1 
DT_F.getYearDifference = function(d1,d2)
{
	var y = d2.getFullYear();
	var m = d1.getMonth();
	var d = d1.getDate();

	if ( DT_F.dateLater(new Date(y,m,d), d2) == 1 ) {
		y--;
	}

	var yd = y - d1.getFullYear();
	var dd = 0;
	var ld = 0;

	while ( DT_F.dateLater(new Date(y,m,d), d2) == -1 )
	{
		if ( DT_F.isLeapYear(y) ) {
			ld++;
		}
		d++;
		dd++;
		if ( d > DT_F.getDaysInMonth(m,y) ) {
			d=1;
			m++;
			if ( m > 11 ) {
				m = 0;
				y++;
			}
		}

	}
	var diy = 365 + ld/366;
	return yd + dd/diy;
};

DT_F.getTotalSeconds = function(s, ap)
{
	var hours, minutes, seconds = 0;

	tabs = s.split(":");
	if ( tabs.length != 2 && tabs.length != 3) {
		throw("Time must be in format hh:mm or hh:mm:ss.");
	}

	hours = parseInt( tabs[0],10 );
	if ( isNaN(hours) ) {
		throw("Invalid hour: " + tabs[0] );
	}
	if ( hours < 1 || hours > 12 ) {
		throw("Invalid hours, must be between 1-12.");
	}
	if ( hours == 12 ) {
		hours = 0;
	}

	minutes = parseInt( tabs[1],10 );
	if ( isNaN(minutes) ) {
		throw("Invalid minutes: " + tabs[1] );
	}
	if ( minutes < 0 || minutes > 59 ) {
		throw("Invalid minutes, must be between 0-59.");
	}
	if  ( tabs.length == 3 )
	{
		seconds = parseInt( tabs[2],10 );
		if ( isNaN(seconds) ) {
			throw("Invalid seconds: " + tabs[2] );
		}
		if ( seconds < 0 || seconds > 59 ) {
			throw("Invalid seconds, must be between 0-59.");
		}
	}

	if ( ap != "AM" && ap != "PM" ) {
		throw("Invalid AM/PM value.");
	}

	if ( ap == "PM" ) {
		hours += 12;
	}


	var total = hours * 3600 + minutes * 60 + seconds;
	return total;
};

DT_F.getTimeString = function(t, useAp)
{
	var hours = Math.floor(t/3600);
	t -= hours * 3600;
	var minutes = Math.floor(t/60);
	t -= minutes * 60;
	seconds = t;

	var postFix = "";
	if ( useAp )
	{
		if ( hours >= 12 ) {
			hours -= 12;
			postFix = "pm";
		}
		else {
			postFix = "am";
		}
		if ( hours === 0 ) {
			hours = 12;
		}
	}

	var x = hours + ":";
	if ( minutes < 10 ) {
		x += "0";
	}
	x += minutes + ":";
	if ( seconds < 10 ) {
		x += "0";
	}
	x += seconds;
	x += postFix;

	return x;
};



