﻿//	****************************************************************************************************
//	*																								   *
//	*	File Name :		olrjslib.js																	   *
//	*	Version :		1.0.0.0																		   *
//	*	Created By :	Martin Holly																   *
//	*	Last Updated :	01/13/09																	   *
//	*	Updates :																					   *
//	*	01/19/09	- OLRBasicSelect.SetValues now does not create a default option specified by 	   *
//	*				  "label" argument if "label" is null or empty string.							   *
//	*	02/04/09	- Added ArrayList object. This is a rudimentary array object with a couple of 	   *
//	*				  usefull functions: AddValue(v), RemoveAll(v) and RemoveOne(v)					   *
//	*	02/05/09	- Added the functions IndexOf(v), Copy() and AddRange(arr) to ArrayList			   *
//	*	02/12/09	- Added the Pager object. This is allready pretty well developed.				   *
//	*	02/24/09	- Added the MortgageCalculator object. This is still not completely fool proof.	   *
//	*	03/07/09	- Added the ability to have a template divider for the Pager object. The 		   *
//					  constructor takes an optional 5th argument that can be an jQuery object or a 	   *
//					  string html representation.													   *
//	*	03/16/09	- Added the previous & next button separator for the Pager object. 				   *
//	*				  This is the 6th argument.														   *
//	****************************************************************************************************

function ArrayList()
{
	//CLASS DATA
	var _array = new Array();
	
	//FUNCTIONS
	this.Length = function(){return _array.length;};
	this.Copy = function()
	{
		var _newArrayList = new ArrayList();
		_newArrayList.AddRange(this);
		return _newArrayList;
	};
	this.AddValue = function(value)
	{
		if(value==null){return this;}
		else{_array[this.Length()]=value; return this;}
	};
	this.AddRange = function(array)
	{
		if(array==null){return this;}
		
		if(array instanceof Array){for(var i=0; i<array.length; i++){_array[this.Length()]=array[i];} return this;}
		if(array instanceof ArrayList){var _tarr = array.ToArray(); for(var i=0; i<_tarr.length; i++){_array[this.Length()]=_tarr[i];} return this;}
		return this;
	};
	this.ToArray = function()
	{
		var _arrcpy = new Array();
		for(var i=0; i<this.Length(); i++)
			_arrcpy[i] = _array[i];
		return _arrcpy;
	};
	this.IndexOf = function(value)
	{
		if(value==null){return null;}
		for(var i=0; i<this.Length(); i++){if(_array[i]==value){return i;}}
		return null;
	};
	this.RemoveAll = function(value)
	{
		if(value==null){return this;}

		var _narr = new Array();
		for(var i=0; i<this.Length(); i++)
		{
			if(_array[i]==value){continue;}
			else{_narr[_narr.length]=_array[i];}
		}//End for
		_array = _narr;
		return this;
	};
	this.RemoveOne = function(value)
	{
		if(value==null){return this;}

		var _narr = new Array();
		var _isRemoved = false;
		for(var i=0; i<this.Length(); i++)
		{
			if(_array[i]==value && !_isRemoved){_isRemoved=true; continue;}
			else{_narr[_narr.length]=_array[i];}
		}//End for
		_array = _narr;
		return this;
	};
	return this;
}//End ArrayList()


var OLRString = OLRString ? OLRString :
{
    IsNullOrEmpty: function(string){return (string=="" || string==null);},
    IsValidDateMMDDYYYY: function(string)
    {
        var _regex = /^\d{2}\/\d{2}\/\d{4}$/;
        if(this.IsNullOrEmpty(string)){return false;}
        else 
        {
            if(string.match(_regex)!=null){return true;}
            else{return false;}
        }
    },
    IsValidDateMMDDYY: function(string)
    {
        var _regex = /^\d{2}\/\d{2}\/\d{2}$/;
        if(this.IsNullOrEmpty(string)){return false;}
        else
        {
            if(string.match(_regex)!=null){return true;}
            else{return false;}
        }
    },
    IsValidPhoneNumber: function(string)
    {
        var _regex = /^\(\d{3}\)\s*\-\s*\d{3}\s*\-\s*\d{4}$/;
        if(this.IsNullOrEmpty(string)){return false;}
        else
        {
            if(string.match(_regex)!=null){return true;}
            else{return false;}
        }
    },
	IsValidPhoneNumber10Digit: function(string)
    {
        var _regex = /^\d{3}[-]{0,1}\d{3}[-]{0,1}\d{4}$/;
        if(this.IsNullOrEmpty(string)){return false;}
        else
        {
            if(string.match(_regex)!=null){return true;}
            else{return false;}
        }
    },
    IsValidEmail: function(string)
    {
        var _regex =/^\w+([+-_]\w+)*@\w+([-]\w+)*\.\w+([-.]\w+)*$/;
        if(this.IsNullOrEmpty(string)){return false;}
        else
        {
            if(string.match(_regex)!=null){return true;}
            else{return false;}
        }
    }
}

var OLRBasicSelect = OLRBasicSelect ? OLRBasicSelect :
{
	SetValues:	function(containerId, array, label, formatter)
	{
		if(OLRString.IsNullOrEmpty(containerId)){return;}
		if(containerId.charAt(0)!="#"){containerId="#"+containerId;}
		if(!OLRString.IsNullOrEmpty(label)){jQuery(containerId).empty().append(OLRBasicSelect.CreateOption("",label));}
		else{jQuery(containerId).empty();}
		jQuery.each(array, function(i)
		{
			if(formatter!=null&&typeof(formatter)=="function"){jQuery(containerId).append(OLRBasicSelect.CreateOption(array[i],formatter(array[i])));}
			else{jQuery(containerId).append(OLRBasicSelect.CreateOption(array[i],array[i]));}
		});
		jQuery(containerId).get(0).selectedIndex=0;
	},
	CreateOption:	function(value,text){return jQuery("<option></option>").attr("value",value).append(text);},
	CurrencyFormatter:	function(number)
	{
                //Check if the number is allready a formatted string
                if(typeof(number)=="string")
                {
                        if(number.match(/^[$]{0,1}\d{0,3}(,\d{3})*(\.\d+){0,1}$/)!=null){return number.charAt(0)=="$"?number:"$"+number;}
                        else if(isNaN(number)){return "$0";}
                }//End if
               
                var _tempStringArray = number.toString().split(".");
                var _wholePart = _tempStringArray.length > 0 ? _tempStringArray[0] : null;
                var _decimalPart = _tempStringArray.length > 1 ? _tempStringArray[1] : null;
               
                if(_wholePart==null){return "$0";}
               
                var _formattedWholePart = "";
                var L = _wholePart.length;
                for(var i=0;i<L;i++)
                {
                        _formattedWholePart = (i%3==0&&i!=0)?_wholePart.charAt(L-1-i)+","+_formattedWholePart : _wholePart.charAt(L-1-i)+_formattedWholePart;
                }//End for
               
                return "$" + _formattedWholePart + ((_decimalPart)?"."+_decimalPart.substring(0,2):"");
	},
	BedroomFormatter:	function(number)
	{
		if(number==null){return null;}
		if(number==0){return "Studio";}
		if(number==1){return number+" Bedroom";}
		if(number>1&&number<4){return number+" Bedrooms";}
		return number+"+ Bedrooms";
	}
}

var OLRArray = OLRArray ? OLRArray :
{
	Contains: function(array,object)
	{
		if(array==null||object==null){return false;}
		else{for(i in array){if(array[i]==object){return true;}}return false;}
	}
}

var OLRCheckBox = OLRCheckBox ? OLRCheckBox :
{
    IsChecked: function(id){return ($("#"+id+":checked").val()!=null);},
    SetExclusivity: function(nspace)
    {
        if(OLRString.IsNullOrEmpty(nspace))return;
    }
}

function SetExclusiveSelect(checkBoxClass)
{
    if(OLRString.IsNullOrEmpty(checkBoxClass))return; //Invalid input
    $("input."+checkBoxClass).bind("click", function(e)
    {
		var current = $(this);
		$("input."+checkBoxClass).removeAttr("checked");   //uncheck everything
		current.attr("checked","checked");  //check selected
	});
}

function ForceToggle(sourceCheckBox, checkBoxClass)
{
    if(IsNullOrEmpty(sourceCheckBox)||IsNullOrEmpty(checkBoxClass))return;  //Invalid input
    $("#"+sourceCheckBox).bind("click",function(e)
    {
        if($("#"+sourceCheckBox+":checked").val()!=null){$("input."+checkBoxClass).attr("checked","checked");}
        else{$("input."+checkBoxClass).removeAttr("checked");}
    });    
}

var OLRData = OLRData ? OLRData : 
{
    Rents: [ 500,600,700,800,900,1000,1250,1500,1750,2000,2500,3000,3500,4000,4500,5000,6000,7000,8000,9000,10000,15000,20000,25000,30000 ],
    Prices:[ 100000,200000,300000,400000,500000,600000,700000,800000,900000,1000000,1250000,1500000,
            1750000,2000000,2500000,3000000,3500000,4000000,4500000,5000000,5500000,6000000,6500000,7000000,
            7500000,8000000,8500000,9000000,9500000,10000000,15000000,20000000,30000000,40000000,50000000,
            60000000,70000000,80000000,90000000,100000000 ],
	ApartmentBedrooms:[ 0,1,2,3,4 ]
}

function Pager(object,interval,previousButton,nextButton)
{
	//Critical error handling
	var _divider = arguments.length=5?arguments[4]:null;
	var _pandnDivider = arguments.length=6?arguments[5]:null;	//Previous and next button divider
	
	//Divider Setup
	if(_divider!=null&&(_divider instanceof jQuery)){_divider.addClass("divider");}
	else if(!OLRString.IsNullOrEmpty(_divider)&&typeof(_divider)=="string"){_divider = $(_divider).addClass("divider");}
	else{_divider = null;}
	
	//P & N Button Divider Setup
	if(!OLRString.IsNullOrEmpty(_pandnDivider)&&typeof(_pandnDivider)=="string"){_pandnDivider=jQuery(_pandnDivider);}
	else if(_pandnDivider!=null&&!(_pandnDivider instanceof jQuery)){_pandnDivider=null;}	//Error case - don't know what this is
	
	if(!((object instanceof Array)||(object instanceof $))){return null;}
	if((interval==null) || ((typeof(interval)=="string"||interval instanceof String) && OLRString.IsNullOrEmpty(interval)) || isNaN(interval/1)){return null;}
	
	var _interval = (interval*1);	//Force conversion
	var _pageIndex = 1;
	var _pageCount = Math.ceil(object.length/interval);
	
	var _currentPageElement = null;
	var _pageCountElement = null;
		
	var $contentArray = object;
	
	if(_pandnDivider!=null){_pandnDivider.hide();}
	var $pButton = null;	//Default case
	if(previousButton instanceof $){$pButton = previousButton.hide();}
	else if((previousButton instanceof String)||(typeof(previousButton)=="string")){$pButton = $("#"+previousButton).hide();}
	
	var $nButton = null;	//Default case
	if(nextButton instanceof $){$nButton = nextButton;}
	else if((nextButton instanceof String)||(typeof(nextButton)=="string")){$nButton = $("#"+nextButton);}
	
	$contentArray.slice(interval).hide();
	if(_divider!=null){$contentArray.slice(0,interval-1).after(_divider);}
	
	if(_interval >= $contentArray.length){$nButton.hide();}
    
	if($pButton!=null)
	{
		$pButton.click(function(e)
		{
			--_pageIndex;
			if(_pageIndex>1){$pButton.show();if(_pandnDivider!=null){_pandnDivider.show();}}
			else{$pButton.hide();if(_pandnDivider!=null){_pandnDivider.hide();}}
		
			if(_pageCount != _pageIndex){$nButton.show();}
			else{$nButton.hide();}
		
			$contentArray.hide();
			$(".divider").remove(".divider");
			var _start = _interval*(_pageIndex-1);
			$contentArray.slice(_start,_start+_interval).show();
			if(_divider!=null){$contentArray.slice(_start,_start+_interval-1).after(_divider);}
			
			if(_currentPageElement!=null){_currentPageElement.text(_pageIndex);}
		});
	}//End if
	
	if($nButton!=null)
	{
		$nButton.click(function(e)
		{
			++_pageIndex;
			if(_pageIndex>1){$pButton.show();}
			else{$pButton.hide();}
		
			if(_pageCount != _pageIndex){$nButton.show();if(_pandnDivider!=null){_pandnDivider.show();}}
			else{$nButton.hide();if(_pandnDivider!=null){_pandnDivider.hide();}}
		
			$contentArray.hide();
			$(".divider").remove(".divider");
			var _start = _interval*(_pageIndex-1);
			var len = $contentArray.slice(_start,_start+_interval).show().length;
			if(_divider!=null){$contentArray.slice(_start,_start+len-1).after(_divider);}
			
			if(_currentPageElement!=null){_currentPageElement.text(_pageIndex);}
		});
	}//End if
	
	//FUNCTIONS
	this.CurrentPage = function(element)
	{
		if(element != null && element instanceof $){_currentPageElement = element;}
		else if((typeof(element)=="string"||element instanceof String)&&!OLRString.IsNullOrEmpty(element)){_currentPageElement = $("#"+element);}
		if(_currentPageElement!=null){_currentPageElement.text(_pageIndex)}
		
		return _pageIndex;
	};
	this.PageCount = function(element)
	{
		return _pageCount;
	};
	
	return this;
}




function MortgageCalculator(principal, interest, period)
{
	//CLASS DATA
	var P;	//Principal (original loan amount)
	var I;	//Anual interest rate
	var L;	//The length (in years) over which the loan is amortized
	var J;	//Monthly interest rate I/(12*L)
	var N;	//Number of months over which loan is amortized L*12
	var M;	//Monthly payment M = P*J/(1-(1+J)^(-N))
	
	P = principal;
	I = interest;
	L = period;
	
	//PROPERTIES
	this.Principal = function(){return P;};
	this.Interest = function(){return I;};
	this.Period = function(){return L;};
	
	//FUNCTION
	this.MonthlyInterest = function(){return I/12;};
	this.PeriodInMonths = function(){return L*12;};
	this.MonthlyPayment = function()
	{
		N = this.PeriodInMonths();
		J = this.MonthlyInterest();
		M = P*(J*Math.pow((1+J),N))/(Math.pow((1+J),N)-1);
		return M;
	};
	this.GetPrincipal = function(propertyvalue,percentdown){return propertyvalue*(1-percentdown);}	
}

String.prototype.trim = function() {
	    return this.replace(/^\s+|\s+$/g,"");
    }
    String.prototype.ltrim = function() {
	    return this.replace(/^\s+/,"");
    }
    String.prototype.rtrim = function() {
	    return this.replace(/\s+$/,"");
    }

    //get X-coordinate of element e:
    function getX(e)
    {
        var x = 0;
        while(e)
        {
            x += e.offsetLeft;
            e = e.offsetParent;        
        }
        return x;
    }
    
    //get Y-coordinate of element e:
    function getY(element)
    {
        var y = 0;
        for (var e = element; e; e.offsetParent)
        {
            y += e.offsetTop;
        }
        
        for (e = element.parentNode; e && e != document.body; e = e.parentNode)
        {
            if (e.scrollTop)
                y -= e.scrollTop;
        }
        return y; 
    }    

    function RoundCents(number)
    {
        var _regex = /^\d+\.\d{2}/;
        var _match = number.toString().match(_regex);
        if(_match != null){return _match[0]*1;}
        else{return 0;}
    }

//function MortgageCalculator(principal, interest, period)
//{
//	//CLASS DATA
//	var P;	//Principal (original loan amount)
//	var I;	//Anual interest rate
//	var L;	//The length (in years) over which the loan is amortized
//	var J;	//Monthly interest rate I/(12*L)
//	var N;	//Number of months over which loan is amortized L*12
//	var M;	//Monthly payment M = P*J/(1-(1+J)^(-N))
//	
//	P = principal;
//	I = interest;
//	L = period;
//	
//	//PROPERTIES
//	this.Principal = function(){return P;};
//	this.Interest = function(){return I;};
//	this.Period = function(){return L;};
//	
//	//FUNCTION
//	this.MonthlyInterest = function(){return I/12;};
//	this.PeriodInMonths = function(){return L*12;};
//	this.MonthlyPayment = function()
//	{
//		N = this.PeriodInMonths();
//		J = this.MonthlyInterest();
//		M = P*(J*Math.pow((1+J),N))/(Math.pow((1+J),N)-1);
//		return M;
//	};
//	this.GetPrincipal = function(propertyvalue, percentdown) {


//	    propertyvalue = propertyvalue.replace("$", "");
//	    propertyvalue = propertyvalue.replace(",", "");

//	    percentdown = percentdown.replace("$", "");
//	    percentdown = percentdown.replace(",", "");

//	//    alert(propertyvalue); 
//	    return propertyvalue * (1 - percentdown);
//	}	
//}
