
function showStat(project) {
    var m = null, y = null;
    $('#StatsForm select').each(function () {
        if($(this).attr('name') == "StartDateMonth") {
            m = $(this).val();
        } else
        if($(this).attr('name') == "StartDateYear") {
            y = $(this).val();
        }
    });
    $('#StatsImg').attr('src', "chart,line," + project + "," + y + "," + m);
    return true;
}

function au_filter(id1,id2) {
    sea=document.getElementById(id1).value;
    sel=document.getElementById(id2);
    reg = new RegExp(sea);
    for(i=0;i<sel.length;i=i+1) {
        if(reg.test(sel.options[i].innerHTML)) {
            sel.options[i].style.display="block";
        } else {
            sel.options[i].style.display="none";
        }
    }
}

function au_clear_filter(id1,id2) {
    sea=document.getElementById(id1).value;
    sel=document.getElementById(id2);
    for(i=0;i<sel.length;i=i+1) {
        sel.options[i].style.display="block";
    }
}

function vote(poll,answer) {
    $('#poll').html('<img src="img/loading.gif" />').load("polls,"+poll+","+answer);
}

function bt_add() {
    a=document.getElementById('bt_add_stuff');
    if(a.style.display=="none") {
        a.style.display="block";
        a.innerHTML+='<br />Title:&nbsp;&nbsp;&nbsp;<input type="text" name="title" /><br />';
        a.innerHTML+='Description:<br /><textarea name="desc" rows="10" cols="80"></textarea><br />';
        a.innerHTML+='<input type="submit" />';
    }
}

function shop_delete_item_from_cart(a) {

    item = a.parentElement;
    
    if(item) { item = item.getElementsByTagName('input'); }
    if(item) { item = item[0]; }
    if(item) { item.value = "0"; }
    if(item) { item = item.parentElement; }
    
    while(item) {
        
        if(item.tagName == "FORM") {
            break;
        } else {
            item = item.parentElement;
        }
        
    }
    
    if(item) {
        item.submit();
        return true;
    }
    
    return false;
    
}

function toggle(element) {
    $(element).parent().children('ul').slideToggle();
    return true;
}

function ajax() {
    
    try {
      return new XMLHttpRequest();
    } catch(e) {}
    
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {}
    
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {}
    
    alert("Your browser does not support AJAX, please upgrade!");
    return false;
    
}

function expand_collapse(id) {
    var el = $('#expand_collapse_' + id);
    
    if(el.hasClass("loading")) {
        return ;
    }
    
    if(el.hasClass("expand")) {
        expand(id);
    } else {
        collapse(id);
    }
    
}

function expand(id) {
    var ec = $('#expand_collapse_' + id);
    var el = $('#projects_' + id);
    
    ec.removeClass("expand");
    
    if(el.html() == "") {
        
        ec.addClass("loading");
        
        el.load("projects,category," + id, function () {
            ec.removeClass("loading");
            ec.addClass("collapse");
            el.slideDown(600);
        });
        
    } else {
        
        ec.addClass("collapse");
        el.slideDown(600);
        
    }
    
}

function collapse(id) {
    
    $('#projects_' + id).slideUp(600);
    
    var el = $('#expand_collapse_' + id);
    
    el.removeClass("collapse");
    el.addClass("expand");
    
}

function add_clock() {
    
    clocks = document.getElementById('clocks');
    
    clock = document.createElement('div');
    
    clock.innerHTML += '\
        <input type="text" name="clock_name[]" />\
        <input type="text" name="clock_freq[]" size="4" />\
        <select name="clock_unit[]">\
            <option>Hz</option>\
            <option>kHz</option>\
            <option>MHz</option>\
            <option>GHz</option>\
            <option>THz</option>\
        </select>\
    ';
    
    clocks.appendChild(clock);
    
}

function check(object) {
    
    var $this = $(object);
    var $span = $this.parent().find('span').last();
    
    if($this.val()) {
        
        $span.text("Loading");
        
        $.ajax({
            data: { value: $this.val() },
            type: "POST",
            url: "register,check," + object.name
        }).success(function (data) {
            $span.html(data);
        });
        
    } else {
        
        $span.text("");
        
    }
    
}
/* Project functions */

function pedit(id,l) {
    f=document.getElementById('f_'+id);
    p=document.getElementById('p_'+id);
    
    if(l != -1) {
        f.text.style.width="99%";
        f.text.style.height=16*(l+3)+"px";
    }
    p.style.display="none";
    f.style.display="inline";
}

function pdelete(id,pr,pa) {
    if(!confirm("Sure?")) return false;
    
    var a,b;
    a=document.getElementById("d_"+id);
    b=ajax();
    
    b.onreadystatechange=function() {
        if(b.readyState==4) {
            a.innerHTML=b.responseText;
        }
    };
    
    a.innerHTML='<img src="img/loading.gif" />';
    
    b.open("GET","?ajax&do=pdelete&proj="+pr+"&page="+pa+"&sort=1&titl="+id,true);
    b.send(null);
    
    return true;
}

function addmaint(pr) {
    m=prompt("Please enter the new maintainer's username, firstname or lastname.");
    if(m == null) return false;
    if(m.length < 1) return false;
    window.location.href="/project_edit_maintainers," + pr + ",search," + m;
    return true;
}

function psort(dir,id) {
    var a=document.getElementById(id);
    if(dir == "add" || dir == "rem") {
        if(dir == "add") {
            var b = document.createElement('option');
            var c = document.getElementById('addpagename');
            if(c.value == "" || c.value == " ") { return false; }
            b.value=c.value.replace(' ','_').toLowerCase();
            b.text=c.value;
            if(a.selectedIndex == a.length-1) {
                a.add(b,null);
            } else {
                a.add(b,a.options[a.selectedIndex+1]);
            }
            a.selectedIndex += 1;
            a.size += 1;
        } else {
            a.remove(a.selectedIndex);
            a.size -= 1;
        }
    } else {
        if(dir == "upp") {
            if(a.selectedIndex == 0) return false;
        } else {
            if(a.selectedIndex == a.size-1) return false;
        }
        var b = a.options[a.selectedIndex];
        if(dir == "upp") {
            a.selectedIndex-=1;
        } else {
            a.selectedIndex+=1;
        }
        var c = a.options[a.selectedIndex];
        var d = document.createElement('option');
        d.value=b.value;
        d.text=b.text;
        a.remove(b.index);
        if(dir == "upp") {
            a.add(d,c);
            a.selectedIndex = c.index-1;
        } else {
            a.add(d,a.options[c.index+1]);
            a.selectedIndex = c.index+1;
        }
    }
    return true;
}

function psort_done() {
    a=document.getElementById("blocks");
    a.multiple=true;
    for(i=0;i<a.size;i++) {
        //a.options[i].value=a.options[i].innerHTML;
        a.options[i].selected=true;
    }
    return true;
}

/* End project functions */
function check_pw(element) {
    
    $this = $(element);
    
    if(/^[a-zA-Z0-9 ]*$/.test($this.attr('value'))) {
        $('#pww').fadeOut();
    } else {
        $('#pww').fadeIn();
    }
    
}

function fedit(id) {
    document.getElementById("f_"+id).style.display="block";
    document.getElementById("t_"+id).style.display="none";
}

