﻿/* COMMON FUNCTIONS */

//get element by id
function $(id) {
    //return
    return document.getElementById(id);
}

//confirm and redirect
function confirm_entry(text, link) {
    var input_box = confirm(text);
    if (input_box == true) {
        window.location = link;
    }
    else {
        //nop
    }
}

//confirm and execute command
function confirm_entry_eval(text, command) {
    var input_box = confirm(text);
    if (input_box == true) {
        eval(command);
    }
    else {
        //nop
    }
}

//open div
function DivOpen(id) {
    //open / hide
    if ($(id).style.display != 'block') {
        $(id).style.display = 'block';
    }
}

//open div
function DivClose(id) {
    //open / hide
    if ($(id).style.display != 'none') {
        $(id).style.display = 'none';
    }
}

//toggle div
function toggleDiv(id) {
    //open / hide
    if ($(id).style.display == 'none') {
        $(id).style.display = 'block';
    }
    else {
        $(id).style.display = 'none';
    }
}

//limits input field - trim
function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
}

//submit form on enter pressed
function submitOnEnter(e, formid) {

    if (e && e.which) {
        //character code is contained in NN4's which property
        e = e
        characterCode = e.which
    }
    else {
        //character code is contained in IE's keyCode property
        e = event
        characterCode = e.keyCode 
    }
    //if generated character code is equal to ascii 13 (if enter key)
    if (characterCode == 13) {
        //submit the form
        document.getElementById(formid).submit() 
        return false
    }
    else {
        return true
    }
}

//submit form on enter pressed
function submitOnEnterAndExec(e, exec) {

    if (e && e.which) {
        //character code is contained in NN4's which property
        e = e
        characterCode = e.which
    }
    else {
        //character code is contained in IE's keyCode property
        e = event
        characterCode = e.keyCode
    }
    //if generated character code is equal to ascii 13
    if (characterCode == 13) {
        //submit the form
        eval(exec);
        return false
    }
    else {
        return true
    }
}

//submit form on enter pressed
function submitOnTabAndExec(e, exec) {

    if (e && e.which) {
        //character code is contained in NN4's which property
        e = e
        characterCode = e.which
    }
    else {
        //character code is contained in IE's keyCode property
        e = event
        characterCode = e.keyCode
    }
    //if generated character code is equal to ascii 13
    if (characterCode == 9) {
        //submit the form
        eval(exec);
        return false;
    }
    else {
        return true;
    }
}

//key: only numeric 
function keyOnlyNumericWEnter(e) {
    //validate input
    if (navigator.appName == "Netscape")
        var keyIndex = e.which; //for netscape
    else
        var keyIndex = window.event.keyCode; //other

    //only allow numbers and backspace
    if ((keyIndex > 47 && keyIndex < 58) || keyIndex == 0 || keyIndex == 8 || keyIndex == 46 || keyIndex == 44 || keyIndex == 13)
        return true;
    else
        return false;
}

//currency format 
function formatCurrency(name, dec, showComma) {
    //init variables
    inp = name.value;
    comma = 0;
    sign = ""

    //get sign
    if (inp.substring(0, 1) == '-') {
        sign = "-";
        inp = inp.substring(1, inp.length);
    };

    //validate input value
    if (inp == "")
        return;
    outt = "";

    //loop characters
    for (var i = 0; i < inp.length; i++) {
        //get character
        sChar = inp.charAt(i);
        if (sChar == ".")
            continue;
        else if (sChar == ",") {
            if (dec == 0)
                break;
            if (comma > 0)
                break;
            comma = 1;
            outt = outt + sChar;
        }
        else if (sChar < "0" || sChar > "9")
            break;
        else
            outt = outt + sChar;
    }

    //get value with zeros
    out = "";
    if (dec) {
        zeros = "000000000";
        out = ",";
        pos1 = outt.indexOf(",");
        if (pos1 != -1) {
            pos2 = outt.length;
            if (pos2 - pos1 - 1 > dec)
                pos2 = pos1 + dec + 1;
            out = out + outt.substring(pos1 + 1, pos2);
            outt = outt.substring(0, pos1);
        }
        out = out + zeros.substring(0, 3 - out.length);
    }
    //format value with separators
    if (showComma == "true") {
        for (i = outt.length; i > 3; i -= 3)
            out = "." + outt.substring(i - 3, i) + out;
    }
    //return formated value
    out = outt.substring(0, i) + out;
    name.value = sign + out;
}

//add load event on page load. example: addLoadEvent(function() { alert('loaded!'); });
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

//trim function
function trim(str) {
    str = str.replace(/^\s+/, '');
    for(var i = str.length-1; i > 0; -i) {
        if( /\S/.test(str[i])) {
            str = str.substring(0, i+1);
            break;
        }
    }
    return str;
}

//send/get survey
function sendSurvey(nDocId, nValidate) {
    var nNodePos = 0;
    var sTarget = "surveycontent";

    //show survey
    if (nValidate == 0) {
        sTarget = "survey";
    }
    //vote
    else if (nValidate == 1) {
        //get radio buttons
        var inputs = $(sTarget).getElementsByTagName('input');

        //loop radio buttons
        for (i = 0; i < inputs.length; i++) {
            //set checked value
            if (inputs[i].checked == true) nNodePos = inputs[i].value;
        }
    }
    else if (nValidate == 2) {
        //nop
    }

    //show hide buttons
    if ($('surveybackbutton').style.display == 'none') {
        //hide survey button
        $('surveysubmitbutton').style.display = 'none';
        $('surveyresultbutton').style.display = 'none';
        $('surveybackbutton').style.display = 'inline';
    }
    else {
        $('surveysubmitbutton').style.display = 'inline';
        $('surveyresultbutton').style.display = 'inline';
        $('surveybackbutton').style.display = 'none';
    }

    //send ajax
    sendAjaxRequest(rooturl + "ajaxsurvey.aspx?docid=" + nDocId + "&nodepos=" + nNodePos + "&validate=" + nValidate, sTarget, "");
}
