/*---------------------------------------------------  
 *  RepeterControl for AJAX
 *  (c) 2005 Jigar Desai <desaijm@hotmail.com>
 *  Date Change: 12/31/2005
 *  requires: prototype.js http://prototype.conio.net/
/*--------------------------------------------------*/

function RepeaterControl(){
    
    this.HeaderTemplate = "";
    this.ItemTemplate = "";
    this.SelectedItemTemplate = "";
    this.FooterTemplate = "";
    this.AlternatingItemTemplate = "";
    this.AlternatingItemTemplate2 = "";
    this.NoAlternatingItemTemplate = "";
    this.NoAlternatingItemTemplate2 = "";
    this.SeparatorTemplate = "";
    this.HostControlID = null;
    
    this.DataSource = null;
    this.DataSource2 = null;
}

RepeaterControl.prototype.DataBind = function(){
  var htmlString = "";
  htmlString += this.HeaderTemplate;
        
  for(var count=0;count<this.DataSource.length;count++){
    var node = this.DataSource[count];
    var row = this.ItemTemplate

    if(this.AlternatingItemTemplate != "" && (count+1)% 2 == 0 ){
      row = this.AlternatingItemTemplate;
    }

//    if(this.AlternatingItemTemplate2 != "" && (count+2)% 3 == 0 ){
//      row = this.AlternatingItemTemplate2;
//    }
       
    for (var i = 0; i < node.attributes.length; i++){
			var re = new RegExp("{@" + node.attributes[i].nodeName + "}","g");
			row = row.replace(re,node.attributes[i].nodeValue);
		}
		
		for (var i = 0; i < node.childNodes.length; i++){
	    if(node.nodeType != 2 && node.childNodes[i].firstChild != null){
		    var re = new RegExp("{\\$" + node.childNodes[i].nodeName + "}","g");
		    row = row.replace(re,node.childNodes[i].firstChild.nodeValue);
			}
		}

		if(this.DataSource2 != null){
			for(var count2=0;count2<this.DataSource2.length;count2++){
				var node2 = this.DataSource2[count2];
				for (var i = 0; i <node2.childNodes.length; i++){
					if(node2.nodeType != 2 && node2.childNodes[i].firstChild != null){
						var re = new RegExp("{\\!" + node2.childNodes[i].nodeName + "}","g");
						row = row.replace(re,node2.childNodes[i].firstChild.nodeValue);
					}
				}
			}
		}
		
		// eval javascript
		evalRe = new RegExp("{Eval(.*?)}","g");
		
		myArray = row.match(evalRe);
		
		if(myArray != null){
		    for(var i=0;i<myArray.length;i++){
		        var result = eval(myArray[i].replace(evalRe,"$1"));
		        row = row.replace(myArray[i],result);
		    }
		}
        
        htmlString += row;
        
        if(this.SeparatorTemplate != "" && count + 1 != this.DataSource.length){
            htmlString += this.SeparatorTemplate;
        }
        
        
    }

    if(this.DataSource.length < 2){
    	for (var i=this.DataSource.length; i<2; i++){
        if(this.NoAlternatingItemTemplate != "" && i == 1){
            htmlString += this.NoAlternatingItemTemplate;
        }

//        if(this.NoAlternatingItemTemplate2 != "" && i == 2){
//            htmlString += this.NoAlternatingItemTemplate2;
//        }
    	}
    }
    
    htmlString += this.FooterTemplate;
    
    $(this.HostControlID).innerHTML = htmlString;
}


