/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only thing you need to change in this file is the following
variables: checkboxHeight, radioHeight and selectWidth.

Replace the first two numbers with the height of the checkbox and
radio button. The actual height of both the checkbox and radio
images should be 4 times the height of these two variables. The
selectWidth value should be the width of your select list image.

You may need to adjust your images a bit if there is a slight
vertical movement during the different stages of the button
activation.

Visit http://ryanfait.com/ for more information.

*/



/* NOTICE: This file has been modified considerably by Ryan Bernard.  Do not attempt to update from the creator's site. */

var checkboxHeight = "";
var radioHeight = "";
var selectWidth = "";
var sameWidthAsParent_select = true;
var totalPaddingWidth_select = 27;

var customClassName = "styled";
var notCustomClassName = "notstyled";

function strrev(str) {
    if (!str) return '';
    var revstr = '';
    for (i = str.length - 1; i >= 0; i--)
        revstr += str.charAt(i)
    return revstr;
}

function Debug_CustomFormElements(str) {
    Debug(str, false);
}

function ContainsCustomClassName(thisName)
{
	if(thisName == customClassName || thisName.indexOf(customClassName + " ") >= 0 || thisName.indexOf(" " + customClassName) >= 0)	
	{
		return true;
	}
	else{
		return false;
	}
}

function ContainsNotCustomClassName(thisName)
{
	if(thisName == notCustomClassName || thisName.indexOf(notCustomClassName + " ") >= 0 || thisName.indexOf(" " + notCustomClassName) >= 0)	
	{
		return true;
	}
	else{
		return false;
	}
}

function GetCheckboxHeight(element)
{
	//if(showDebug){alert("GetCheckboxHeight");}
	var checkboxHeight = 0;
	try{
	checkboxHeight = parseInt(element.style.height.replace("px",""));
	if(!checkboxHeight > 0){
		checkboxHeight = 0;
	}	
	//if(showDebug){alert("checkboxHeight = " + checkboxHeight);}
	}
	catch(err){
		Debug_CustomFormElements("error getting checkbox height");
	}
	return checkboxHeight;
}

function toggleCheckbox(name) {
	Debug_CustomFormElements("toggleCheckbox");
	var myCheckbox = getElement(name);
	if(myCheckbox){
		if(myCheckbox.checked){
			myCheckbox.checked = false;
		}
		else{
			myCheckbox.checked = true;
		}
		if(myCheckbox.id.indexOf("category") >= 0){
			updateTACCategories(myCheckbox);
		}
		else if(myCheckbox.id.indexOf("expandcollapse") >= 0){
			expandcollapseTACCategories(myCheckbox);	
		}
	}
}

// AJUST THESE FUNCTIONS TO HANDLE IDS LIKE 'category_103_105_108' RATHER THAN 'subsubcategory_105_108'

function getCatID(myobj, mystring) {
    //alert('getCatID(' + myobj.id + ')');
    var CatID;
    var mystring1;
    if (mystring) {
        mystring1 = mystring;
    }
    else {
        mystring1 = myobj.id;
    }
    mystring1 = Right(mystring1, mystring1.length - mystring1.lastIndexOf("_"));
    CatID = mystring1.replace("_", "");
    CatID = parseInt(CatID);
    //alert('CatID = ' + CatID);
    return CatID;
}

function getParentCatID(myobj, mystring) {
    //alert('getParentCatID(' + myobj.id + ')');
    var ParentCatID;
    var mystring1;
    if (mystring) {
        mystring1 = mystring;
    }
    else {
        mystring1 = myobj.id;
    }
    ParentCatID = getCatID("", getParentCatObjID("", mystring1));
    ParentCatID = parseInt(ParentCatID);
    //alert('ParentCatID = ' + ParentCatID);
    return ParentCatID
}

function getParentCatObjID(myobj, mystring) {

    //alert('getParentCatObjID(' + myobj.id + ')');

    var mystring1;
    if (mystring) {
        mystring1 = mystring;
    }
    else {
        mystring1 = myobj.id;
    }

    var ParentCatObjID = Left(mystring1, mystring1.length - strrev(mystring1).indexOf("_") - 1);

    //alert('ParentCatObjID = ' + ParentCatObjID);
    
    return ParentCatObjID
}

function writecatnumTACCategories(thisid) {

    //alert('writecatnumTACCategories(' + thisid + ')');

	var myobj = getElement(thisid);
	
	var myobj2;
	var myobj3;
	var mystring1;
	var mystring2;
	var mystring3;
	var CatID;
	var ThisParentCatID;
	var CatCheckedNum = 0;
	var CatNum = 0;
	
	var inputs;
	var a;
	
	if(myobj){
	
        //mystring1 = myobj.id;
        //mystring2 = Right(mystring1,mystring1.length - mystring1.lastIndexOf("_"));
        //CatID = mystring2.replace("_","");
	    //CatID = parseInt(CatID);

	    CatID = getCatID(myobj);
		
		myobj2 = getElement("label_" + myobj.id);
	
	}

	if (myobj2) {

	    inputs = document.getElementsByTagName("input");

	    for (a = 0; a < inputs.length; a++) {

	        if (inputs[a].type == "checkbox" && inputs[a].id.indexOf("category_") == 0) {

	            //mystring1 = inputs[a].id.replace("subcategory_","");
	            //ThisParentCatID = Left(mystring1,mystring1.indexOf("_"));
	            //ThisParentCatID = parseInt(ThisParentCatID);

	            ThisParentCatID = getParentCatID(inputs[a]);

	            if (ThisParentCatID == CatID) {
	                CatNum++;
	                if (inputs[a].checked) {
	                    CatCheckedNum++;
	                }
	            }
	        }
	    }

	    mystring3 = myobj2.innerHTML;
	    if (mystring3.indexOf("<") > 0) {
	        mystring3 = Left(mystring3, mystring3.indexOf("<"));
	    }
	    if (CatNum > 0) {
	        myobj2.innerHTML = mystring3 + ' <span>(' + CatCheckedNum + ' of ' + CatNum + ')</span>'
	    }
	}
}

function expandcollapseTACCategories(myobj,action){
	
	Debug_CustomFormElements("expandcollapseTACCategories");
	
	var myobj2;
	var myobj3;
	
	var mystring1;
	var mystring2;
	
	var CatID;
	var CatChecked = myobj.checked;
	
	mystring1 = myobj.id;
	mystring2 = Right(mystring1,mystring1.length - mystring1.lastIndexOf("_"));
	CatID = mystring2.replace("_","");
	
	myobj2 = getElement("expandcollapseblock_" + CatID);
	myobj3 = getElement("label_expandcollapse_" + CatID);

	if (myobj2 && myobj3) {
	    Debug_CustomFormElements("CatChecked = " + CatChecked);
	    Debug_CustomFormElements("action = " + action);
	    Debug_CustomFormElements("myobj3.innerHTML = " + myobj3.innerHTML);
		//if((CatChecked && action != "collapse") || action == "expand" || myobj3.innerHTML == "expand"){
	    if (myobj3.innerHTML == "expand") {
		    // expand
		    Debug_CustomFormElements("expand");
		    myobj.checked = true;
		    myobj.previousSibling.style.backgroundPosition = "0px -" + checkboxHeight + "px";
		    Debug_CustomFormElements(myobj.previousSibling + ".backgroundPosition = " + myobj.previousSibling.style.backgroundPosition);
			showElement(myobj2.id);
			if(myobj3){myobj3.innerHTML = "collapse";}
		}
		else{
		    // collapse
		    Debug_CustomFormElements("expand");
		    myobj.checked = false;
		    myobj.previousSibling.style.backgroundPosition = "0px 0px";
		    Debug_CustomFormElements(myobj.previousSibling + ".backgroundPosition = " + myobj.previousSibling.style.backgroundPosition);	    
			hideElement(myobj2.id);
			if(myobj3){myobj3.innerHTML = "expand";}
		}
	}
}

function getHasCheckedOrUncheckedChild(myobj,checktype) {

    var CatID = getCatID(myobj);
    var ParentCatID = getParentCatID(myobj);
    var ThisCatID;
    var ThisParentCatID;

    var HasCheckedChild = false;
    var HasUncheckedChild = false;

    var inputs = document.getElementsByTagName("input");

    var a;

    for (a = 0; a < inputs.length; a++) {

        if (inputs[a].type == "checkbox" && inputs[a].id.indexOf("category") == 0) {

            ThisCatID = getCatID(inputs[a]);
            ThisParentCatID = getParentCatID(inputs[a]);
		
            if (ThisParentCatID == CatID || ThisParentCatID == ParentCatID) {
                if (inputs[a].checked && checktype == "checked") {
                    HasCheckedChild = true;
                    break;
                }
                else if (!inputs[a].checked && checktype == "unchecked") {
                    HasUncheckedChild = true;
                    break;
                }              
            }
        }
    }
    if (checktype == "checked") {
        return HasCheckedChild;
    }
    else if (checktype == "unchecked") {
        return HasUncheckedChild;
    }
}

function updateTACCategories_Parents(myobj) {

    var CatChecked = false;
    var HasUncheckedChild;

    var myobj2 = getElement(getParentCatObjID(myobj));

    if (myobj2) {
        if (myobj2.checked) {
            CatChecked = true;
        }
        HasUncheckedChild = getHasCheckedOrUncheckedChild(myobj, "unchecked");   
        if (HasUncheckedChild) {
            myobj2.checked = false;
        }
        else {
            myobj2.checked = true;
        }
        if (CatChecked != myobj2.checked && myobj2 != myobj) {
            // recurse to update parent
            updateTACCategories_Parents(myobj2);
        }
        writecatnumTACCategories(myobj2.id);
    }
}

function updateTACCategories_Children(myobj) {

    //alert('updateTACCategories_Children(' + myobj.id + ')');    

    var CatID = getCatID(myobj);
    var ThisParentCatID;

    var CatChecked = false;
    if (myobj.checked) {
        CatChecked = true;
    }

    var ThisCatChecked = false;

    var inputs = document.getElementsByTagName("input");

    var a;

    for (a = 0; a < inputs.length; a++) {

        if (inputs[a].type == "checkbox" && inputs[a].id.indexOf("category") == 0) {            

            ThisParentCatID = getParentCatID(inputs[a]);

            if (ThisParentCatID == CatID) {
                if (inputs[a].checked) {
                    ThisCatChecked = true;
                }
                inputs[a].checked = CatChecked;
                //alert(inputs[a].id + '.checked = ' + CatChecked);
                if (CatChecked != ThisCatChecked  && inputs[a] != myobj) {
                    // recurse to update children
                    //alert('recurse updateTACCategories_Children(' + inputs[a].id + ')');
                    updateTACCategories_Children(inputs[a]);
                    writecatnumTACCategories(inputs[a].id);
                }
            }
        }
    }
    //alert("done");
}

function updateTACCategories(myobj){
	
	Debug_CustomFormElements("updateTACCategories");
	
	var myobj2;
	
	var mystring1;
	var mystring2;
	
	var CatID;
	var ParentCatID;
	var CatChecked = myobj.checked;
	
	CatID = getCatID(myobj);						
	ParentCatID = getParentCatID(myobj);
	
	var inputs;
	
	var CatChecked;
	
	var ThisCatChecked;	
	var ThisCatID;
	var ThisParentCatID;
	
	var HasCheckedChild = false;
	var HasUncheckedChild = false;	

	// update children
	updateTACCategories_Children(myobj);     
	
	if(ParentCatID >= 0){
		// update parents
		updateTACCategories_Parents(myobj);
    }

    writecatnumTACCategories(myobj.id);
}


function CustomFormElements_Clear(){
	Debug_CustomFormElements("clear");
	inputs = document.getElementsByTagName("input");
	for(var b = 0; b < inputs.length; b++) {
		checkboxHeight = GetCheckboxHeight(inputs[b]);
		radioHeight = checkboxHeight;
		if(inputs[b].type == "checkbox" && inputs[b].checked == true && ContainsCustomClassName(inputs[b].className)) {
			//if(showDebug){alert("1");}
			inputs[b].previousSibling.style.backgroundPosition = "0px -" + checkboxHeight + "px";
		} else if(inputs[b].type == "checkbox" && ContainsCustomClassName(inputs[b].className)) {
			//if(showDebug){alert("2");}
			inputs[b].previousSibling.style.backgroundPosition = "0px 0px";
		} else if(inputs[b].type == "radio" && inputs[b].checked == true && ContainsCustomClassName(inputs[b].className)) {
			//if(showDebug){alert("3");}
			inputs[b].previousSibling.style.backgroundPosition = "0px -" + radioHeight + "px";
		} else if(inputs[b].type == "radio" && ContainsCustomClassName(inputs[b].className)) {
			//if(showDebug){alert("4");}
			inputs[b].previousSibling.style.backgroundPosition = "0px 0px";
		}
	}
}

function CustomFormElements_Choose(myobj){
	Debug_CustomFormElements("CustomFormElements_Choose("+myobj+")");
	Debug_CustomFormElements("choose");
	option = myobj.getElementsByTagName("option");
	for(d = 0; d < option.length; d++) {
		if(option[d].selected == true) {
			document.getElementById("select" + myobj.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
		}
	}
}

function CustomFormElements_Pushed(myobj, myobjid) {

    if (!myobj) {
        Debug_CustomFormElements("CustomFormElements_Pushed(myobj= " + myobj + ")");
    }
    else {

        if (!ContainsNotCustomClassName(myobj.className)) {
            element = myobj.nextSibling;
        }
        else {
            element = myobj;
        }

        if (element) {
            Debug_CustomFormElements("CustomFormElements_Pushed(myobj.id= " + myobj.id + ", element.id= " + element.id + ")");
        }
        else {
            Debug_CustomFormElements("CustomFormElements_Pushed(myobj.id= " + myobj.id + ", element= " + element + ")");
        }

        checkboxHeight = GetCheckboxHeight(element);
        radioHeight = checkboxHeight;
        if (element.checked == true && element.type == "checkbox") {
            myobj.style.backgroundPosition = "0px -" + checkboxHeight + "px";
        } else if (element.checked == true && element.type == "radio") {
            myobj.style.backgroundPosition = "0px -" + radioHeight + "px";
        } else if (element.checked != true && element.type == "checkbox") {
            myobj.style.backgroundPosition = "0px 0px";
        } else {
            myobj.style.backgroundPosition = "0px 0px";
        }
        Debug_CustomFormElements("backgroundPosition = " + myobj.style.backgroundPosition);
    }
}

function CustomFormElements_Check(myobj){
    
	if(!ContainsNotCustomClassName(myobj.className)){
		element = myobj.nextSibling;
	}
	else{
		element = myobj;
    }

    Debug_CustomFormElements("CustomFormElements_Check(element.id= " + element.id + ")");

	checkboxHeight = GetCheckboxHeight(element);
	radioHeight = checkboxHeight;

	// wait a second for checked to be updated
	//alert('wait');

	if (!ContainsNotCustomClassName(element.className)) {
	    Debug_CustomFormElements("element.checked = " + element.checked);
	    Debug_CustomFormElements("element.type = " + element.type);
		if(element.checked == true && element.type == "checkbox") {
			Debug_CustomFormElements("uncheck " + element.name);
			myobj.style.backgroundPosition = "0px 0px";
			element.checked = false;
			Debug_CustomFormElements("set checked = false");
		} else {
			if(element.type == "checkbox") {
				Debug_CustomFormElements("check " + element.name);
				myobj.style.backgroundPosition = "0px -" + checkboxHeight + "px";
			} else {
				myobj.style.backgroundPosition = "0px -" + radioHeight + "px";
				group = myobj.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != myobj.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
			Debug_CustomFormElements("set checked = true");        }
        Debug_CustomFormElements("backgroundPosition = " + myobj.style.backgroundPosition);
	}
	
	if(element.id.indexOf("category") >= 0){
		updateTACCategories(element);
	}
	
	else if(element.id.indexOf("expandcollapse") >= 0){
		expandcollapseTACCategories(element);	
	}
		
}




/* No need to change anything after this */

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; }</style>');

var Custom = {
    test: function() {
        //alert("test");
    },
    init: function() {
        Debug_CustomFormElements("init");

        var onclick_old;
        var onchange_old;
        var onmousedown_old;
        var onmouseup_old;

        var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
        for (a = 0; a < inputs.length; a++) {
            if ((inputs[a].type == "checkbox" || inputs[a].type == "radio") && ContainsCustomClassName(inputs[a].className)) {
                span[a] = document.createElement("span");
                span[a].className = inputs[a].type;
                span[a].style.height = inputs[a].style.height;

                checkboxHeight = GetCheckboxHeight(span[a]);
                radioHeight = checkboxHeight;

                //alert("checkboxHeight = " + checkboxHeight);

                if (inputs[a].checked == true) {
                    if (inputs[a].type == "checkbox") {
                        //alert("check it");
                        position = "0px -" + checkboxHeight + "px";
                        //alert(position);
                        span[a].style.backgroundPosition = position;
                        if (inputs[a].id.indexOf("expandcollapse") >= 0) {
                            expandcollapseTACCategories(inputs[a], "expand");
                        }
                    } else {
                        position = "0px -" + radioHeight + "px";
                        span[a].style.backgroundPosition = position;
                    }
                }
                if (inputs[a].type == "checkbox" && inputs[a].id.indexOf("category") == 0) {
                    writecatnumTACCategories(inputs[a].id + '');
                }
                inputs[a].parentNode.insertBefore(span[a], inputs[a]);

                // set onchange
                onchange_old = inputs[a].onchange + '';
                onchange_old = onchange_old.substring(onchange_old.indexOf('{') + 1, onchange_old.indexOf('}'));
                inputs[a].onchange = new Function(onchange_old + "CustomFormElements_Clear();");

                // set onclick
                onclick_old = inputs[a].onclick + '';
                onclick_old = onclick_old.substring(onclick_old.indexOf('{') + 1, onclick_old.indexOf('}'));
                span[a].onclick = new Function(onclick_old);

                // set onmousedown
                onmousedown_old = inputs[a].onmousedown + '';
                onmousedown_old = onmousedown_old.substring(onmousedown_old.indexOf('{') + 1, onmousedown_old.indexOf('}'));
                span[a].onmousedown = new Function("CustomFormElements_Pushed(this);" + onmousedown_old);

                // set onmouseup
                onmouseup_old = inputs[a].onmouseup + '';
                onmouseup_old = onmouseup_old.substring(onmouseup_old.indexOf('{') + 1, onmouseup_old.indexOf('}'));
                span[a].onmouseup = new Function("CustomFormElements_Check(this);" + onmouseup_old);

                document.onmouseup = Custom.clear;

                if (ContainsNotCustomClassName(inputs[a].className)) {
                    inputs[a].onclick = new Function("CustomFormElements_Check(this);" + onclick_old);
                    inputs[a].onmousedown = new Function("CustomFormElements_Check(this);" + onmousedown_old);
                    inputs[a].onmouseup = new Function("CustomFormElements_Check(this);" + onmouseup_old);
                    span[a].onclick = "";
                    span[a].onmousedown = "";
                    span[a].onmouseup = "";
                    //document.onmouseup = "";
                }


            }
        }
        inputs = document.getElementsByTagName("select");
        for (a = 0; a < inputs.length; a++) {
            if (ContainsCustomClassName(inputs[a].className)) {
                width = inputs[a].parentNode.offsetWidth;
                option = inputs[a].getElementsByTagName("option");
                active = option[0].childNodes[0].nodeValue;
                textnode = document.createTextNode(active);
                for (b = 0; b < option.length; b++) {
                    if (option[b].selected == true) {
                        textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
                    }
                }
                span[a] = document.createElement("span");
                span[a].className = "select";
                span[a].id = "select" + inputs[a].name;
                span[a].appendChild(textnode);
                myelement = inputs[a].parentNode.insertBefore(span[a], inputs[a]);
                if (sameWidthAsParent_select) {
                    myelement.style.width = (width - totalPaddingWidth_select) + "px";
                    inputs[a].style.width = width + "px";
                }
                onchange_old = inputs[a].onchange + '';
                onchange_old = onchange_old.substring(onchange_old.indexOf('{') + 1, onchange_old.indexOf('}'));
                inputs[a].onchange = new Function(onchange_old + "CustomFormElements_Choose(this);");
            }
        }
        var labels = document.getElementsByTagName("label"), span = Array(), textnode, option, active;
        for (a = 0; a < labels.length; a++) {
            if (ContainsCustomClassName(labels[a].className)) {

                //labels[a].onmousedown = Custom.label_pushed;

                labels[a].onmousedown = new Function("CustomFormElements_Pushed(getElement(this.id.replace('label_','')).previousSibling);");
                labels[a].onmouseup = new Function("CustomFormElements_Check(getElement(this.id.replace('label_','')).previousSibling);");
                document.onmouseup = Custom.clear;

                //labels[a].onmousedown = new Function("CustomFormElements_Pushed();alert('test');");
            }
        }
    },
    pushed: function() {
        CustomFormElements_Pushed(this);
    },
    label_pushed: function() {
        if (showDebug) { alert("label pushed"); }
        if (this.name) {
            if (!ContainsNotCustomClassName(this.className)) {
                toggleCheckbox(this.name.replace("label_", ""));
            }
        }
        else if (this.id) {
            if (this.id.indexOf("expandcollapse") >= 0) {
                toggleCheckbox(this.id.replace("label_", ""));
            }
        }
        CustomFormElements_Clear();
    },
    check: function() {
        CustomFormElements_Check(this);
    },
    clear: function() {
        CustomFormElements_Clear();
    },
    choose: function() {
        CustomFormElements_Choose(this);
    }
}
window.onload = Custom.init;
