/*---------------------------------------------------  
 *  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.SeparatorTemplate = "";
    this.RightHostControlID = null;
    this.LeftHostControlID = null;
    
    this.DataSource = null;
}

RepeaterControl.prototype.DataBind = function() {
    var htmlStringRight = "";
    htmlStringRight += this.HeaderTemplate;
    //alert("RepeaterControl");
    for (var count = 0; count < this.DataSource.length; /*count++*/count += 2) {
        var node = this.DataSource[count];
        var row = this.ItemTemplate

        if (this.AlternatingItemTemplate != "" && (count + 1) % 2 == 0) {
            row = this.AlternatingItemTemplate;
        }

        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 != 3 && node.childNodes[i].firstChild != null) {
                var re = new RegExp("{\\$" + node.childNodes[i].nodeName + "}", "g");
                //row = row.replace(re, node.childNodes[i].firstChild.nodeValue);
                if (node.childNodes[i].firstChild.nodeValue == "-") {
                    row = row.replace(re, "");
                    //alert(node.childNodes[i].firstChild.nodeValue);
                } 
                else
                    row = row.replace(re, node.childNodes[i].firstChild.nodeValue);
                //alert(node.childNodes[i].firstChild.nodeValue);
                //if (node.childNodes[i].nodeName == "HostLogo") alert(row);
            }
        }

        // 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);

            }
        }

        htmlStringRight += row;

        if (this.SeparatorTemplate != "" && count + 1 != this.DataSource.length) {
            htmlStringRight += this.SeparatorTemplate;
        }


    }

    var htmlStringLeft = "";
    htmlStringLeft += this.HeaderTemplate;

    for (var count = 1; count < this.DataSource.length; count += 2) {
        var node = this.DataSource[count];
        var row = this.ItemTemplate

        if (this.AlternatingItemTemplate != "" && (count + 1) % 2 == 0) {
            row = this.AlternatingItemTemplate;
        }

        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 != 3 && node.childNodes[i].firstChild != null) {
                var re = new RegExp("{\\$" + node.childNodes[i].nodeName + "}", "g");
                if (node.childNodes[i].firstChild.nodeValue == "-") {
                    row = row.replace(re, "");
                } else row = row.replace(re, node.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);

            }
        }

        htmlStringLeft += row;

        if (this.SeparatorTemplate != "" && count + 1 != this.DataSource.length) {
            htmlStringLeft += this.SeparatorTemplate;
        }


    }


    htmlStringLeft += this.FooterTemplate;

    $(this.RightHostControlID).innerHTML = htmlStringRight;
    $(this.LeftHostControlID).innerHTML = htmlStringLeft;
}


