function round(number)	// no decimals
{
  return Math.round(number*Math.pow(10,2))/Math.pow(10,2);
}

function floor(number)	
{
  return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function $(element)
{
    return document.getElementById(element);
}

//function for currency format
function formatCurrency(num) 
{	
	num = num.toString().replace(/\$|\,/g,'');
	if(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;
		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);
	}
	else
		return '';
}

function autoTab(frmFer,field,len)
{  
	var autotab_focus_value=frmFer.autotab_focus;		
	var field_index;
	if (field.value.length < len)
		autotab_focus_value.value=1;

	if (field.value.length == len && autotab_focus_value.value==1)
	{
		field_index=getIndex(field);
		field.form[field_index+1].focus();
		//alert(field.form[field_index+1].type=="text");
		if(field.form[field_index+1].type=="text")
			field.form[field_index+1].select();
	}
}

function getIndex(field)
{
  var ix = -1, i = 0, found = false;
  while (i < field.form.length && ix == -1)
    if (field.form[i] == field) 
		ix = i;
    else 
		i++;
  return ix;
}

function unSetautotab_focus(frmFer)
{
	frmFer.autotab_focus.value=0;
}

function Setautotab_focus(frmFer,field,len)
{
	if(field.value.length < len) 
		frmFer.autotab_focus.value=1;
}


// get the size of the window and page to make an overlay element
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function displayOverlay()
{
    // prevents making multiple overlay divs
    if(document.getElementById('overlay'))
    {
    return false;
    }
	var objBody = document.getElementsByTagName("body").item(0);
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objBody.appendChild(objOverlay);
	
	var arrayPageSize = getPageSize();
	objOverlay.style.width = arrayPageSize[0];
	objOverlay.style.height = arrayPageSize[1];
	objOverlay.style.backgroundColor = "#000";
	objOverlay.style.position = "absolute";
	objOverlay.style.zIndex = 1;
	objOverlay.className = "overlay-trans";
	//objOverlay.style.filters.alpha.opacity=70; // for MSIE
	//objOverlay.style.opacity = "0.7";
	objOverlay.style.top = "0";
	objOverlay.style.left = "0";
	return true;
}

// needed for validation ...
function IsMoney(val)
{
	valid = true;
	if(trim(val).length>-1)
	{
		allowed = "0123456789()-,.$";
		digits = "0123456789";
		numCount=0;
		for(i=0;i<val.length;i++)
		{
			if(allowed.indexOf(val.charAt(i))<0)
				valid=false;
			if(digits.indexOf(val.charAt(i))>0)
				numCount++;	
		}
		if(valid && numCount==0)
			valid=false;
	}		
	return valid;
}

function closeDiv(id)
{
    removeOverlay(id);
	$(id).style.display="none";
}

// If there are more than one popups open, prevents removing of the overlay when we close one of them
function removeOverlay(id)
{
    if(id == 'loan_type_div')
    {
        if($('app_years_at_div').style.display=="none" && $('coapp_years_at_div').style.display=="none")
        {
            $('overlay').parentNode.removeChild($('overlay'));
            return true;
        }
        return true;
    }
    
    if(id == 'app_years_at_div')
    {
        if($('loan_type_div').style.display=="none" && $('coapp_years_at_div').style.display=="none")
        {
            $('overlay').parentNode.removeChild($('overlay'));
            return true;
        }
        return true;
    }
    
    if(id == 'coapp_years_at_div')
    {
        if($('loan_type_div').style.display=="none" && $('app_years_at_div').style.display=="none")
        {
            $('overlay').parentNode.removeChild($('overlay'));
            return true;
        }
        return true;
    }    
    
}
