function XKtoggle(id) {
	if (document.getElementById) {
		var elementID = document.getElementById(id);
		if (elementID && typeof elementID.className == 'string') {
			if (elementID.className == 'off') {
				elementID.className = 'on';
			} else {
				elementID.className = 'off';
			}
		}
	}
}

function XKtoggleOff(id) {
	if (document.getElementById) {
		var elementID = document.getElementById(id);
		if (elementID && typeof elementID.className == 'string') {
			if (elementID.className == 'off') {
				elementID.className = 'off';
			} else {
				elementID.className = 'off';
			}
		}
	}
}

function XKtoggleOn(id) {
	if (document.getElementById) {
		var elementID = document.getElementById(id);
		if (elementID && typeof elementID.className == 'string') {
			if (elementID.className == 'off') {
				elementID.className = 'on';
			} else {
				elementID.className = 'on';
			}
		}
	}
}

/*
 * ussage: <input type="button" onClick="XKmoveUp(LISTOBJECT)" value=" &uarr; "><br>
 *         <input type="button" onClick="XKmoveDown(LISTOBJECT)" value=" &darr; "><br>
 */


// get the location in a form that an element is
function getIndex(input) {
    var index = -1, i = 0;
    while (i < input.form.length && index == -1){
        if (input.form[i] == input){
            index = i;
        } else {
            i++;
        }
    }
    return index;
}

// swap 2 indexes in a select box
function XKswap(obj, in1, in2){
    if (!obj)return false;
    if (in1==in2)return true;
    if (in1>obj.options.length || in2>obj.options.length)return false;
    var opar = new Array();
    for (i=0; i<obj.options.length; i++){
        if (i==in1){
            opar[in2] = obj.options[i];
        } else if (i==in2){
            opar[in1] = obj.options[i];
        } else {
            opar[i] = obj.options[i];
        }
    }
    for (i=0; i<opar.length; i++)obj.options[i]=opar[i];
}


function XKmoveUp(obj){
    moveLast = true;
    for (xi=0; xi<obj.options.length; xi++){
        if (moveLast==false && xi==obj.options.length-1)return;
        if (obj.options[xi].selected==true && xi>0){
            XKswap(obj, xi, xi-1);
        } else if (obj.options[xi].selected==true && !(xi>0)){
            return false;
        }
    }
}

function XKmoveDown(obj){
    var moveZero = true;
    for (xi=obj.options.length-1; xi>-1; xi--){
        if (moveZero==false && xi==0)return;
        if (obj.options[xi].selected==true && xi+1<obj.options.length){
            XKswap(obj, xi, xi+1);
        } else if (obj.options[xi].selected==true && !(xi+1<obj.options.length)){
            return false;
        }
    }
}

// function for filtering a string with allowedchars

// example usage: <input type="text" name="phonenumber" onkeyup="this.value=XKfilterString(this.value, '1234567890-');">
// this would allow only numbers and hyphens

function XKfilterString(str, allowchars){

    var ret = '';
    for (i = 0; i < str.length; i++){
        if (allowchars.indexOf(str.charAt(i))!=-1){
            ret = ret+''+str.charAt(i);
        }
    }
    return ret;

}


// function that will take a form and a string as input, then go through the form and get the input
// from any field whos name matches the string and add it up for a total.  ONLY uses decimal point
// for currency decimal notation.

function XKgetFormFieldTotals(f, fieldname){

    var total = 0;
    var stringnum = '';
    var tmp = '';
    for (xxi = 0; xxi < f.elements.length; xxi++){
        tmp = f.elements[xxi].type;
        if (tmp=="text"){
            tmp = f.elements[xxi].name.toString();
            tmp = tmp.substring(0,fieldname.length);
            if (tmp.toString()==fieldname.toString()){
                stringnum = XKfilterString(f.elements[xxi].value.toString(), '1234567890.-');
                total = total + XKround(stringnum, 2);
            }
        }
    }

    return total;

}


// function that will take a form and a string as input, then go through the form and get the input
// from any field whos name matches the string and add it up for a total.  ONLY uses decimal point
// for currency decimal notation.

function XKcheckDebitCredit(f, fieldname){

    var stringnum = '';
    var tmp = '';
    var amount = 0;
    var amount2 = 0;
    var alerted = false;
    var ret = true;
    for (xxi = 0; xxi < f.elements.length; xxi++){
        tmp = f.elements[xxi].type;
        if (tmp=="text"){
            tmp = f.elements[xxi].name.toString();
            tmp = tmp.substring(0,fieldname.length);
            if (tmp.toString()==fieldname.toString()){
                stringnum = XKfilterString(f.elements[xxi].value.toString(), '1234567890.-');
                amount = XKround(stringnum, 2);
                stringnum = XKfilterString(f.elements[xxi+1].value.toString(), '1234567890.-');
                amount2 = XKround(stringnum, 2);
                if (amount*1>0 && amount2*1>0){
                    f.elements[xxi].style.backgroundColor = 'red';
                    f.elements[xxi+1].style.backgroundColor = 'red';
                    f.elements[xxi].style.color = 'white';
                    f.elements[xxi+1].style.color = 'white';
                    ret = false;
                    if (alerted==false){
                        alerted = true;
                        alert("You cannot have a debit and a credit in the same entry.\nPlease create separate entries.");
                    }
                } else {
                    f.elements[xxi].style.backgroundColor = '';
                    f.elements[xxi+1].style.backgroundColor = '';
                    f.elements[xxi].style.color = '';
                    f.elements[xxi+1].style.color = '';
                }
            }
        }
    }

    return ret;

}


// function for formatting a currency number on the fly in javascript similar to our locale array.
// uses: ANYTHING#,##0.00ANYTHING;NEGANYTHING#,##0.00NEGANYTHING style format string
// and gets the comma length by getting       ^---  (number of characters between the comma and decimal)
// gets the number of decimals by checking how many zeros are after the decimal

function XKformatNumber(formatstring, number, decSep, groupSep){

    if (decSep==""){ // get the decimal separator if not passed
        decSep = ".";
    }
    if (groupSep==""){  // get the grouping separator if not passed
        groupSep = ",";
    }

    number = number.toString(); // set the number as a string
    var neg = (number.indexOf("-")==-1)?0:1; // index of the format string to use, 1 = second one, which is the negative number format string
    var fmtStrs = formatstring.split(";"); // array of format strings
    formatstring = fmtStrs[neg].replace("¤", ""); // choose the negative or positive format string and filter it for special characters
    while (formatstring.indexOf("0")>0){
        formatstring = formatstring.replace("0", "#");
    }

    stringparts = formatstring.split("#");
    numfirst = stringparts[0];
    numlast = stringparts[stringparts.length-1];
    firstpos = numfirst.length;
    lastpos = formatstring.length;

    if (numlast!=""){
        lastpos = formatstring.indexOf(numlast);
    }

    if (lastpos==firstpos){
        lastpos = formatstring.length-1;
    }

    var innerformatstring = formatstring.substring(firstpos, lastpos);

    innerformatstring = innerformatstring.split(".");
    var decimals = (innerformatstring[1])?innerformatstring[1].length:0;
    innerformatstring[0] = innerformatstring[0].split(",");
    var commadigits = innerformatstring[0][innerformatstring[0].length-1].length;

    number = parseFloat(XKfilterString(number, "1234567890."));
    number = XKround(number, decimals);
    number = number.toString();
    number = number.split(".");
    if (!number[1]){
        number[1] = "";
    }
    while (number[1].length<decimals){
        number[1] = number[1]+""+"0";
    }

    numdefault = 1/Math.pow(10, decimals+1);
    numdefault = numdefault.toString().split(".");
    numdefault = numdefault[1].substring(0, decimals);
    if (!numdefault){
        numdefault = "";
    }
    while (numdefault.length<decimals){
        numdefault = numdefault+"0";
    }

    if (numfirst==numlast){
        numlast = 0;
    }

    if (parseInt(number[0])>0){

        var assume = (number[0].length % commadigits);
        if (assume>0)assume = commadigits-assume;

        var numberparts = XKchunkSplit(number[0], commadigits, assume);
        var endnumber = numfirst+''+numberparts[0];
        for (i = 1; i < numberparts.length; i++){
            endnumber = endnumber+groupSep+numberparts[i];
        }

    } else {
        var endnumber = numfirst+'0';
    }

    if (parseInt(number[1])>0){
        endnumber = endnumber+''+decSep+number[1]+''+numlast;
    } else {
        endnumber = endnumber+''+decSep+numdefault+''+numlast;
    }

    return endnumber;

}

// splits a given string into an array of given chunk sizes

function XKchunkSplit(str, size, assume){

    var retarray = new Array();
    for (i = 0; i < str.length/size; i++){
        retarray[i] = '';
        for (x = 0; x < size; x++){
            if ((i*size)+x-assume<str.length && (i*size)+x-assume>-1){
                retarray[i] = retarray[i]+''+str.charAt((i*size)+x-assume);
            }
        }
    }

    return retarray;

}

// rounding function for decimal places in javascript

function XKround(number, decimals){

    number = parseFloat(number); // get float
    number = number * Math.pow(10, decimals); // multiply times 10 to the power of "decimals"
    number = Math.round(number); // round to whole number
    number = number / Math.pow(10, decimals); // divide by 10 to the power of "decimals"
    return number; // return number

}


// functions to copy text from one field to another on the fly with a conditional arguments

var XKfieldCopyFields = new Array();
var XKfieldCopyToFields = new Array();
var XKconditionalFields = new Array();
var XKconditionalUpdateData = new Array();

function XKcopyRegisterConditionalField(conditionaltype, conditionalfield){
    db('registering conditional field');
    if (typeof conditionalfield=='undefined')return false;
    db('got past test for conditional validity');
    XKconditionalFields[conditionalfield.name] = new Array(conditionaltype, conditionalfield);
    return true;
}

function XKcopyRegisterCopyField(copyfield){
    db("trying to register a copy field");
    if (typeof copyfield=='undefined')return false;
    db("copy field is an actual field, registering");
    XKfieldCopyFields[copyfield.name] = copyfield;
    //copyfield.onkeyup = XKcopyCheckUpdate(copyfield.name);
    //db("adding onkeyup action to copy field");
}

function XKcopyRegisterCopyToField(copyfieldname, copytofield, conditionalfieldname){
    db('trying to register copy to field');
    if (typeof copytofield=='undefined')return false;
    db('copy to field is a valid field');
    if (typeof XKfieldCopyToFields[copyfieldname]=='undefined'){
        db('created copyfieldname item in copy to fields array');
        XKfieldCopyToFields[copyfieldname] = new Array();
    }
    XKfieldCopyToFields[copyfieldname][XKfieldCopyToFields[copyfieldname].length] = new Array(copytofield, conditionalfieldname);
    XKconditionalUpdateData[XKconditionalUpdateData.length] = new Array(copytofield, conditionalfieldname, copyfieldname);
    db('done registering copy to field');
}

function XKcopyCheckMe(copyfield){
    return XKcopyCheckUpdate(copyfield.name);
}

function XKcopyCheckUpdate(copyfieldname){
    db('checking an update');
    if (typeof XKfieldCopyFields[copyfieldname]=='undefined')return false;
    db('there is a field object');
    var copyfield = XKfieldCopyFields[copyfieldname];
    db('got copy field object');
    var updateFields = XKfieldCopyToFields[copyfield.name];
    db('got updatefields');
    var thisField = new Array();

    if (typeof updateFields!='undefined'){
        db("there are fields to update");
        for (xi=0; xi<updateFields.length; xi++){

            thisField = updateFields[xi];
            db('got '+xi+' numbered field to check');
            if (XKcopyCheckConditional(thisField[1])){
                db('checked conditional and now initiating copy');
                XKcopyCopyValue(copyfield, thisField[0]);

            }

        }
    }

}


function XKcopyCheckConditional(conditionalname){
    db('checking if conditional exists');
    if (typeof XKconditionalFields[conditionalname]=='undefined')return false;

    var thisConditional = XKconditionalFields[conditionalname];
    if (thisConditional[0]=='checkbox'){
        db('conditional was checkbox returning:'+thisConditional[1].checked);
        return thisConditional[1].checked;
    }

}


function XKcopyCopyValue(copyfield, copytofield){
    db('copying values...');
    if (copyfield.type=="text"){
        copytofield.value = copyfield.value;
    } else if (copyfield.type.toString().indexOf('select') != -1) {
        copytofield.selectedIndex = copyfield.selectedIndex;
    }
}

function XKcopyUpdateConditional(conditionalfieldname){
    if (typeof XKconditionalFields[conditionalfieldname]=='undefined')return false;
    var conditionalfield = XKconditionalFields[conditionalfieldname][1];

    var updateFields = XKconditionalUpdateData;
    var thisField = new Array();

    for (xi=0; xi<updateFields.length; xi++){

        thisField = updateFields[xi];
        if (XKcopyCheckConditional(thisField[1])){
            if (typeof XKfieldCopyFields[thisField[2]]!='undefined'){
                XKcopyCopyValue(XKfieldCopyFields[thisField[2]], thisField[0]);
            }

        }

    }

}

function db(x){
    if (typeof XKdebugBox=='undefined')return true;
    XKdebugBox.value = XKdebugBox.value+"\n"+x;
    return true;
}

/*****************************

sample usage:
in your checkbox:  onmouseup="setTimeout('XKcopyUpdateConditional(\'checkboxname\');', 10);"
in your copy fields:  onkeyup="XKcopyCheckMe(this);"
<script language=javascript>
<!--

    var tf = document.tf;
    XKcopyRegisterConditionalField('checkbox', tf.conditional);

    XKcopyRegisterCopyField(tf.text1);
    XKcopyRegisterCopyToField('text1', tf.text1copy, 'conditional');


//-->
</script>

<p id="bentag">
sadf
</p>

document.getElementById('bentag').onclick = hideobjbyid('bentag');
******************************/


function showobj(x){
    x.style.display = '';
    x.style.visibility = 'visible';
}

function hideobj(x){
    x.style.display = 'none';
    x.style.visibility = 'hidden';
}

function showobjbyid(x){
    showobj(document.getElementById(x));
}

function hideobjbyid(x){
    hideobj(document.getElementById(x));
}


// function to repopulate a select menu given its object and an array of values and display values

function repopulateSelect(field, values){

    for (i=0; i<field.options.length; i++){
        field.options[i] = null;
    }

    for (i=0; i<values.length; i++){
        field.options[i] = new Option(unescape(values[i][0]), unescape(values[i][1]));
    }

}

/***********************
example:

<script language="javascript">
<!--

var info = new Array(

    new Array(new Array('3one', '3onename'), new Array('3two', '3twoname')),

    new Array(new Array('4one', '4onename'), new Array('4two', '4twoname'), new Array('4three', '4threename'))

);

function updateSelect(val){
    repopulateSelect(document.tform.otherfield, info[val]);
}

//-->
</script>

<form name="tform">

<select name="name" onchange="updateSelect(this.selectedIndex);">
    <option value="3">3333</option>
    <option value="4">4444</option>
</select>
<select name="otherfield">
</select>

</form>


***********************/