// takes a string and removes leading whitespace
function ltrim(s)
{
	s=String(s);
	if(s != null && s.length>0) {
	var l=0;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	return s.substring(l, s.length);
	} else {
		return "";
	}
}
// takes a string and removes trailing whitespace
function rtrim(s)
{
	s=String(s);
	if(s != null && s.length>0) {
		var r=s.length-1;
		while(r > 0 && s[r] == ' ')
		{	r-=1;	}
		return s.substring(0, r+1);
	} else {
		return "";
	}
}
// takes a string and removes leading and trailing whitespace
function trim (s)
{
	s=String(s);
	if(s != null && s.length>0) {
		return rtrim(ltrim(s));
	} else {
		return "";
	}
}
function validateZip (zip) {
  if (trim(zip).length != 5) { return false; }
  else {
    var i;

    for (i = 0; i < zip.length; i++)
    {
        // Check that current character is number.
        var c = zip.charAt(i);

        if (!(c >= 0 && c<= 9)) return false;
    }

    // All characters are numbers.
    return true;
  }
}

function checkNumber(theNumber) {
 var pass = false;
 var count=0;
 trimNumber=trim(theNumber);
 if(trimNumber.length==0 ){
	 pass = true;
 }else{
   for (var i=0; i < rtrim(theNumber).length; i++) {
		var j = theNumber.charAt(i);
		if ((j < "0" || j > "9") && (j != "." && j!=",")) {
		  pass = true;
		}
		if(j=="."){
			count++;
		}
	 }
	  if(count>1){
		 pass = true;
	}
 }
if(pass==false){
	//if(trimNumber.search(/^\d{1,3}(,?\d{3})*\.?(\d{1,2})?$/) == -1) {
	if(trimNumber.search(/^\d{1,3}(,?\d{3})*\.?(\d)*?$/) == -1) {
	   pass = true;
	}
}
return pass;
}

function checkAPR(theNumber) {
 var pass = false;
 var count=0;
 var trimNumber=trim(theNumber);
	 if(trimNumber.length==0 ){
		 pass = true;
	 }else{
		   for (var i=0; i < rtrim(theNumber).length; i++) {
				var j = theNumber.charAt(i);
				if ((j < "0" || j > "9") && (j != ".")) {
				  pass = true;
				}
				if(j=="."){
					count++;
				}
		 }
		  if(count>1){
			 pass = true;
		}
	 }
/*	if(pass==false){
		if(trimNumber.indexOf('.')>0){
			if(trimNumber.substring(trimNumber.indexOf('.')+1,trimNumber.length).length>1) {
			   pass = true;
			}
		}
	}*/
return pass;
}


function checkNumberAllowBlanks(theNumber) {
 var pass = false;
 var count=0;
 trimNumber=trim(theNumber);
 if(trimNumber.length==0 ){
	 pass = false;
 }else{
   for (var i=0; i < rtrim(theNumber).length; i++) {
		var j = theNumber.charAt(i);
		if ((j < "0" || j > "9") && (j != ".")) {
		  pass = true;
		}
		if(j=="."){
			count++;
		}
		
	 }
	  if(count>1){
			 pass = true;
		}
 }

  return pass;
}

function formatDecimalNumber(num) {
	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;
	
	return (  num + '.' + cents);
}

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));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
function formatCurrencyWithoutDollar(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));
	return (((sign)?'':'-') + num + '.' + cents);
}
function getTime(){
	var funDate= new Date();
	return funDate.getTime();
}
function checkMaxLimit(val){
	if(	parseFloat(val)>9999999.00){
		return true;
	}
	else{
		return false;
	}

}

var isNav;
var isIE;
var isFirefox;
if (navigator.userAgent.indexOf("Firefox") > 0) { 
	isFirefox = 1;
}else if (navigator.appName == "Netscape") {
	isNav = 0;
	//document.captureEvents(Event.KEYPRESS);
}else if (navigator.appName == "Microsoft Internet Explorer") {
	isIE = 1;
}  else {
	isIE = 1;
}


function checkKey(e) {
	var keyChar;
	if (isNav == 0)	{
		if(((e.which != 8) && (e.which != 0))&&(e.which < 48 || e.which > 57 )) {
			e.returnValue = false;
			return false;			
		}
	}  else if (isFirefox==1){
		if(((e.which != 8) && (e.which != 0))&&(e.which < 48 || e.which > 57 )) {
			e.returnValue = false;
			return false;			
		}
	} else {
		if(e.keyCode <48 || e.keyCode > 57) e.returnValue = false;
	}
}

function checkAPRKey(e,tdId) {
	//Added
	if (isNav == 0)	{
		if((e.which != 46) && (e.which != 8) && (e.which != 0)&&(e.which < 48 || e.which > 57 )&& (e.which != 13) ) {
			e.returnValue = false;
			return false;			
		}
	}  else if (isFirefox==1){
		if((e.which != 46) && (e.which != 8) && (e.which != 0)&&(e.which < 48 || e.which > 57 )&& (e.which != 13) ) {
			e.returnValue = false;
			return false;			
		}
	} else {
		if((e.keyCode != 46) && (e.keyCode != 8) && (e.keyCode != 0)&&(e.keyCode < 48 || e.keyCode > 57 )&& (e.keyCode != 13) ) {
			e.returnValue = false;
			return false;
		}
	}
	return true;
}

function checkEnteredKey(e) {
	var keyChar;
	if (isNav == 0)	{
		if((e.which != "44" ) && (e.which != "46") && (e.which != 8) && (e.which != 0)&&(e.which < 48 || e.which > 57 )&& (e.which != 13) )
			{e.returnValue = false;}
		else {e.returnValue=true;}
			return e.returnValue;			
		}  else if (isFirefox==1){
		if((e.which != "44" ) && (e.which != "46") && (e.which != 8) && (e.which != 0)&&(e.which < 48 || e.which > 57 )&& (e.which != 13) )
			{e.returnValue = false;}
			else {e.returnValue=true;}
			return e.returnValue;			
		} else {
		if((e.keyCode != "44" ) && (e.keyCode != "46") && (e.keyCode != 8) && (e.keyCode != 0)&&(e.keyCode < 48 || e.keyCode > 57 )&& (e.keyCode != 13) ) {e.returnValue = false;}
		else {e.returnValue=true;}
		
	}
}

function checkEnteredKeyForMileage(e){
	var keyChar;
	if (isNav == 0)	{
		if((e.which != 8) && (e.which != 0)&&(e.which < 48 || e.which > 57 )&& (e.which != 13) )
			{e.returnValue = false;}
		else {e.returnValue=true;}
			return e.returnValue;			
		}  else if (isFirefox==1){
		if((e.which != 8) && (e.which != 0)&&(e.which < 48 || e.which > 57 )&& (e.which != 13) )
			{e.returnValue = false;}
			else {e.returnValue=true;}
			return e.returnValue;			
		} else {
		if((e.keyCode != 8) && (e.keyCode != 0)&&(e.keyCode < 48 || e.keyCode > 57 )&& (e.keyCode != 13) ) {e.returnValue = false;}
		else {e.returnValue=true;}
		
	}
}
function formateMileage(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));
	return (((sign)?'':'-')+ num); //+ '.' + cents);
}
function formatDecimalNumberWithNegatives(num) {
	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;
	return (((sign)?'':'-') + num + '.' + cents);
}
//Function to get a province value
function getProvince(provinceCode) {
		var province;
		if(provinceCode=="AB") { province = "Alberta"; }
		else if(provinceCode=="BC") { province = "British Columbia"; }
		else if(provinceCode=="MB") { province = "Manitoba"; }
		else if(provinceCode=="NB") { province = "New Brunswick"; }
		else if(provinceCode=="NL") { province = "Newfoundland"; }
		else if(provinceCode=="NT") { province = "Northwest Territories"; }
		else if(provinceCode=="NS") { province = "Nova Scotia"; }
		else if(provinceCode=="NU") { province = "Nunavut"; }
		else if(provinceCode=="ON") { province = "Ontario"; }
		else if(provinceCode=="PE") { province = "Prince Edward Island"; }
		else if(provinceCode=="QC") { province = "Quebec"; }
		else if(provinceCode=="SK") { province = "Saskatchewan"; }
		else if(provinceCode=="YK") { province = "Yukon"; }
		return province;
	}
function checkBrowser() {
	var browser = "ie";
	try	{
		var usrAgent = navigator.userAgent.toLowerCase();
		if(usrAgent.indexOf("opera") != -1) {
			browser = "opera";
		} else if(usrAgent.indexOf("msie") != -1) {
			if(usrAgent.indexOf("msie 7") != -1) {
				browser = "ie7";
			} else {
				browser = "ie";
			}
		} else if(usrAgent.indexOf("safari") != -1) {
			browser = "safari";
		} else if(usrAgent.indexOf("navigator") != -1) {
			browser = "navigator";
		} else if(usrAgent.indexOf("firefox") != -1) {
			browser = "firefox";
		}
	} catch (err) {
		browser = "ie"
	}

	return browser;
}