function AddToPanel(tbl, txt, ddl, hidden) {
    var x = $get(tbl);
    var y = $get(txt);
    var z = $get(ddl);
    var Hidden1 = $get(hidden);

    if (y.value == '' && z.selectedIndex == -1) return;

    if (y.value != '') {
        cells = x.getElementsByTagName("TD");
        for (var j = 0; j < cells.length; j++) {
            if (cells[j].innerHTML == y.value)
                return;
        }
    }
    else if (z.selectedIndex > -1) {
        cells = x.getElementsByTagName("TD");
        for (var j = 0; j < cells.length; j++) {
            if (cells[j].innerHTML == z[z.selectedIndex].text)
                return;
        }
    }

    x.style.display = 'block';
    var row = x.insertRow(x.rows.length);
    var cell = row.insertCell(0);

    if (y.value != '')
        cell.innerHTML = y.value;
    else
        cell.innerHTML = z[z.selectedIndex].text;
    var cell2 = row.insertCell(1);
    var button = document.createElement("input");
    button.type = "image";
    button.setAttribute("class", "rollover");
    button.setAttribute("className", "rollover");
    button.src = "/App_Themes/Default/images/remove_off.jpg";
    button.onclick = function() {
        removeThis(this, hidden, tbl);
    };
    cell2.appendChild(button);
    Hidden1.value += cell.innerHTML + ",";
    y.value = "";
}

function removeThis(ths, hidden, tbl) {
    var Hidden1 = $get(hidden);
    var x = $get(tbl);
    var i = ths.parentNode.parentNode.rowIndex;
    Hidden1.value = Hidden1.value.replace(x.rows[i].cells[0].innerHTML + ",", "");
    x.deleteRow(i);

    if (x.rows.length == 0)
        x.style.display = 'none';
}

function HideShowProfileDetails() {
    var x = $get('ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_ViewAllLit');
    var y = $get('ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder2_FullDetailsLit');

    if (y.style.display = 'none') {
        y.style.display = 'block';
        x.style.display = 'none';
    }
    else {
        x.style.display = 'block';
        y.style.display = 'none';
    }
}

function AddHour(tbl, day, from, am, to, am2, hidden) {
    var x = $get(tbl);
    var a = $get(day);
    var b = $get(from);
    var c = $get(am);
    var d = $get(to);
    var e = $get(am2);
    var f = $get(hidden);

    if (a.selectedIndex < 1 || b.value == '' || d.value == '') return;

    re = /^(\d{1,2}):(\d{2})([ap]m)?$/;

    if (b.value != '') {
        if (regs = b.value.match(re)) {
            if (regs[3]) {
                if (regs[1] < 1 || regs[1] > 12) {
                    alert("Invalid value for hours: " + regs[1]);
                    b.focus();
                    return false;
                }
            } else {
                if (regs[1] > 23) {
                    alert("Invalid value for hours: " + regs[1]);
                    b.focus();
                    return false;
                }
            }
            if (regs[2] > 59) {
                alert("Invalid value for minutes: " + regs[2]);
                b.focus();
                return false;
            }
        } else {
            alert("Invalid time format: " + b.value);
            b.focus();
            return false;
        }
    }

    if (d.value != '') {
        if (regs = d.value.match(re)) {
            if (regs[3]) {
                if (regs[1] < 1 || regs[1] > 12) {
                    alert("Invalid value for hours: " + regs[1]);
                    d.focus();
                    return false;
                }
            } else {
                if (regs[1] > 23) {
                    alert("Invalid value for hours: " + regs[1]);
                    d.focus();
                    return false;
                }
            }
            if (regs[2] > 59) {
                alert("Invalid value for minutes: " + regs[2]);
                d.focus();
                return false;
            }
        } else {
            alert("Invalid time format: " + d.value);
            d.focus();
            return false;
        }
    }

    x.style.display = 'block';
    var row = x.insertRow(x.rows.length);
    var cell = row.insertCell(0);

    cell.innerHTML = a[a.selectedIndex].value + ' : ' + b.value + (c.checked ? " am" : " pm") + " to " + d.value + (e.checked ? " am" : " pm");
    
    var cell2 = row.insertCell(1);
    var button = document.createElement("input");
    button.type = "image";
    button.setAttribute("class", "rollover");
    button.setAttribute("className", "rollover");
    button.src = "/App_Themes/Default/images/remove_off.jpg";
    button.onclick = function() {
        RemoveHours(this, f.id, x.id);
    };
    cell2.appendChild(button);
    f.value += cell.innerHTML + ",";
    a.selectedIndex = 0;
    b.value = '';
    c.checked = false;
    d.value = '';
    e.checked = false;
}

function RemoveHours(ths, f, x) {
    var a = $get(f);
    var b = $get(x);
    var i = ths.parentNode.parentNode.rowIndex;
    a.value = a.value.replace(b.rows[i].cells[0].innerHTML + ",", "");
    b.deleteRow(i);

    if (b.rows.length == 0)
        b.style.display = 'none';
}

function AddAffiliate(tbl, url, hidden) {
    var x = $get(tbl);
    var a = $get(url);
    var f = $get(hidden);

    x.style.display = 'block';
    var row = x.insertRow(x.rows.length);
    var cell = row.insertCell(0);

    cell.innerHTML = a.value;

    var cell2 = row.insertCell(1);
    var button = document.createElement("input");
    button.type = "image";
    button.setAttribute("class", "rollover");
    button.setAttribute("className", "rollover");
    button.src = "/App_Themes/Default/images/remove_off.jpg";
    button.onclick = function() {
        RemoveAffiliate(this, f.id, x.id);
    };
    cell2.appendChild(button);
    f.value += cell.innerHTML + ",";
    a.value = '';
}

function RemoveAffiliate(ths, f, x) {
    var a = $get(f);
    var b = $get(x);
    var i = ths.parentNode.parentNode.rowIndex;
    
    a.value = a.value.replace(b.rows[i].cells[0].innerHTML + ",", "");
    b.deleteRow(i);

    if (b.rows.length == 0)
        b.style.display = 'none';
}

function AddToRepsTbl(tbl, txt) {
    var x = $get(tbl);
    var y = $get(txt);
    
    if (y.value == '') return;

    if (y.value != '') {
        if (y.value > 10)
            y.value = 10;
        
        for (i = x.rows.length - 1; i > -1; i--)
            x.deleteRow(i);

        x.style.display = 'block';
        for (i = 0; i < y.value; i++) {
            var row = x.insertRow(x.rows.length);
            var cell = row.insertCell(0);
            cell.innerHTML = "Set #" + (i+1);
           
            var cell2 = row.insertCell(1);
            var txt = document.createElement("input");
            txt.style.width = "50px";
            txt.setAttribute("maxLength", 3);
            txt.type = "text";
            cell2.appendChild(txt);
            cell2.innerHTML += " reps";
        }
        
        if (y.value == 0)
            x.style.display = 'none';
    }
}

function UpdateRepsHiddenField(tbl, hidden) {
    var x = $get(tbl);
    if (x != null) {
        var Hidden1 = $get(hidden);
        Hidden1.value = "";
        for (i = 0; i < x.rows.length; i++) {
            txts = x.rows[i].cells[1].getElementsByTagName("input");
            Hidden1.value += txts[0].value + ",";
        }
    }
}