//adding he new methods
Element.addMethods({
	//removes any child noes from the element
	//example: <div id="myDiv"><b>hello</b></div>
	//         $('myDiv').clearChildren();
	//     ==> <div id="myDiv"></div>
	clearChildren: function(element) {
		element = $(element);
		$A(element.childNodes).each(function(e){
			  e.parentNode.removeChild(e);
		});
		return element;
	},
	//method that creates a new element and appends to the current element
	// example: <div id="myDiv">Please</div>
	//          $('myDiv').append('A',{href:'otherpage.html', className:'red'}).update('Continue...');
	//     ==>  <div id="myDiv">Please<a href="otherpage.html" class="red">Continue...</a></div>
	append: function(element, tagName, attributes, styles) {
		element = $(element);
		var newEl = $CE(tagName, attributes, styles);
		element.appendChild(newEl);
		return newEl;//<-- this one returns the new element
	},
	//appends a text node to the element
	// example: <div id="myDiv"><b>hello</b></div>
	//          $('myDiv').appendText(', John');
	//      ==> <div id="myDiv"><b>hello</b>, John</div>
	appendText: function(element, text){
		element = $(element);
		var t = document.createTextNode(text);
		element.appendChild(t);
		return element;
	  }
});
    
    
/*var ShortSharesTables = new Shares();
ShortSharesTables.rowIdPrefix = 'Row';
ShortSharesTables.setNumberOfRow(5);
ShortSharesTables.getRowData = function (rowNumber)
{
     var className = rowNumber % 2 ? 'estimator_on' : 'estimator_off';

     return "<tr id='Row" + rowNumber + "' class='" + className + "'>" +
        "<th class='number'>" + rowNumber + "</th>" +
            "<td><input type='checkbox' name='purchaseInput_array[" + (rowNumber - 1) + "][isEmployeeOption]' value='1' /></td>" +
        "</tr>";
}*/

function DynamicTable()
{
    this.numberOfRow = 0;
    this.rowIdPrefix = 'SharesRow';

    this.setNumberOfRow = function (nbRow)
    {
        this.numberOfRow = nbRow;
    }

    this.addRow = function (nbRow)
    {
        //Use for scrollTo but, see below
        //var NextRowId = this.getNextRowId();
        
        for(var i = 1 ; i <= nbRow ; i++)
        {
            var LastRowNumber = this.numberOfRow;
            var LastRow = this.getLastRow();
            new Insertion.After(LastRow,this.getRowData(LastRowNumber + 1));
            this.setNumberOfRow(LastRowNumber + 1);
        }
        
        //Doesn't work since it's in a inner div
        //$(NextRowId).scrollTo();
    }

    this.getLastRow = function ()
    {
        var RowId = this.rowIdPrefix + this.numberOfRow;
        return $(RowId);
    }
    
    this.getNextRowId = function ()
    {
       return this.rowIdPrefix + (this.numberOfRow + 1);
    }

    this.getRowData = function (rowNumber)
    {
       alert("Method getRowData need to override");
    }
    
    this.validate = function ()
    {   
        Element.hide('formErrorList');
        Element.clearChildren($('formErrorList'));

        var isValid = true;
        var nbRow = this.numberOfRow
        
        for(var i = 0 ; i < nbRow ; i++)
        {
            if(this.rowHaveData(i))
            {
                if(!this.rowIsValid(i))
                {
                    isValid = false;
                }
            }
        }
        if(!isValid){
            Element.show('formErrorList');
            window.scroll(0,125);
        }
        return isValid;
       
    }
    
    this.rowIsValid = function (nbRow)
    {
       alert("Method rowIsValid need to be override");
    }
    
    this.rowHaveData = function (nbRow)
    {
        alert("Method rowHaveData need to be override");
    }
    

    this.getFormFieldValue = function (element)
    {

        return $F(element);
    
    }
    
    this.addErrorMessage = function (message)
    {
        new Insertion.Bottom('formErrorList','<li>' + message + '</li>');
    }
    
     this.clearErrorMessage = function (message)
    {
        new Insertion.Bottom('formErrorList','<li>' + message + '</li>');
    }
}

function redirectButton(form, action) {
		document.forms[form].action = action
		document.forms[form].submit()
}

function redirectConfirm(msg, url)
{
	var where_to= confirm(msg);
	if (where_to== true)
 	{
 		window.location=url;
 	}
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
      var keyCode = (isNN) ? e.which : e.keyCode; 
      var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
      if(input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
       
      }

    function containsElement(arr, ele) {
        var found = false, index = 0;
        while(!found && index < arr.length)
        if(arr[index] == ele)
        found = true;
        else
        index++;
        return found;
    }
    
    function getIndex(input) {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
        if (input.form[i] == input)index = i;
        else i++;
        return index;
    }
  return true;
}
function pop(page){
    window.open(page, '', 'width=650,height=600,scrollbars,menubar,toolbar');
}
function devdashnull(){
    //empty redirection
}
