function trim(str) {
    for(var i = 0 ; i<str.length && str.charAt(i)==" " ; i++ );
    for(var j = str.length; j>0 && str.charAt(j-1)==" " ; j--);
    if(i>j) return "";
    return str.substring(i,j);
}

function dah_invoice_info(invoiced) {
    if (invoiced.checked) {
        document.getElementById('payment_invoice_title').disabled = false;
        document.getElementById('payment_invoiced_at_1i').disabled = false;
        document.getElementById('payment_invoiced_at_2i').disabled = false;
        document.getElementById('payment_invoiced_at_3i').disabled = false;
    } else {
        document.getElementById('payment_invoice_title').disabled = true;
        document.getElementById('payment_invoiced_at_1i').disabled = true;
        document.getElementById('payment_invoiced_at_2i').disabled = true;
        document.getElementById('payment_invoiced_at_3i').disabled = true;
    }
}

function copy_to_clipboard(txt) {
    if(window.clipboardData) {
        window.clipboardData.clearData();
        window.clipboardData.setData("Text", txt);
    } else if(navigator.userAgent.indexOf("Opera") != -1) {
        window.location = txt;
    } else if (window.netscape) {
        try {
            netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
        } catch (e) {
            alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
        }
        var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
        if (!clip)
            return;
        var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
        if (!trans)
            return;
        trans.addDataFlavor('text/unicode');
        var str = new Object();
        var len = new Object();
        var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
        var copytext = txt;
        str.data = copytext;
        trans.setTransferData("text/unicode",str,copytext.length*2);
        var clipid = Components.interfaces.nsIClipboard;
        if (!clip)
            return false;
        clip.setData(trans,null,clipid.kGlobalClipboard);
        alert("复制成功！")
    }
}

function validate_keywords() {
    var keywords = document.getElementById('search_keywords');
    if (trim(keywords.value) == "") {
        alert('请输入关键词');
        return false;
    }
}

function switch_check_box(checked, check_box_name) {
    var check_boxes = document.getElementsByName(check_box_name);
    for(i = 0; i < check_boxes.length; i++) {
        check_boxes[i].checked = checked
    }
}

function clear_value(input) {
    input.value = ''
}

function extract_nodes(pNode){
    if(pNode.nodeType == 3)return null;
    var node,nodes = new Array();
    for(var i=0;node= pNode.childNodes[i];i++){
        if(node.nodeType == 1)nodes.push(node);
    }
    return nodes;
}

function move_organization(){
    var obj=document.getElementById('l-organization');
    if(obj.scrollTop % (obj.clientHeight) ==0){
        settime+=1;
        if(settime==50){
            obj.scrollTop+=1;
            settime=0;
        }
    }else{
        obj.scrollTop+=1;
        if(obj.scrollTop==(obj.scrollHeight-obj.clientHeight)){
            obj.scrollTop=0;
        }
    }
}

function roll_organization() {
    var obj=document.getElementById('l-organization');
    for(i=0;i<3;i++){
        obj.appendChild(extract_nodes(obj)[i].cloneNode(true));
    }
    settime=0;
    var t=setInterval(move_organization,50);
    obj.onmouseover=function(){
        clearInterval(t)
    }
    obj.onmouseout=function(){
        t=setInterval(move_organization,50)
    }
}

function move_book(){
    var obj=document.getElementById('l-book');
    if(obj.scrollTop % (obj.clientHeight) ==0){
        settime+=1;
        if(settime==50){
            obj.scrollTop+=1;
            settime=0;
        }
    }else{
        obj.scrollTop+=1;
        if(obj.scrollTop==(obj.scrollHeight-obj.clientHeight)){
            obj.scrollTop=0;
        }
    }
}

function roll_book() {
    var obj=document.getElementById('l-book');
    for(i=0;i<3;i++){
        obj.appendChild(extract_nodes(obj)[i].cloneNode(true));
    }
    settime=0;
    var t=setInterval(move_book,50);
    obj.onmouseover=function(){
        clearInterval(t)
    }
    obj.onmouseout=function(){
        t=setInterval(move_book,50)
    }
}

function copy_value(source, destination_id) {
    var destination = document.getElementById(destination_id);
    destination.value = source.value;
}