// Oildom Publishing Inc : Global Scripts
function cats(me){
	var thisIndex = me.innerHTML;
	var thisNode = me;
	var thisDiv = $(thisIndex);
	var thisBlock = $('menu');
	var resetMe = $$('.sub-nav a.current');
	var hideMe = $$('#menu div');
		
	if(thisDiv.visible()){
		Effect.toggle(thisDiv,'slide');
		thisNode.className = '';
	} else {
		for(i = 0; i < resetMe.length; i++) {
			 resetMe[i].className = '';
		}
		hideMe.invoke('hide');
		Effect.toggle(thisDiv,'slide');
		thisNode.className = 'current';
	}
}
function catsEdit(me){
	var thisIndex = me.innerHTML + 'e';
	var thisNode = me;
	var thisDiv = $(thisIndex);
	var thisBlock = $('menu-select');
	var resetMe = $$('.sub-nav a.current');
	var hideMe = $$('#menu-select div');

	if (thisDiv.visible()) {
	    Effect.toggle(thisDiv, 'slide');
	    thisNode.className = '';
	} else {
	    for (i = 0; i < resetMe.length; i++) {
	        resetMe[i].className = '';
	    }
	    hideMe.invoke('hide');
	    Effect.toggle(thisDiv, 'slide');
	    thisNode.className = 'current';
	}
}

function populateCategoryHidden() {
    var thisPosition = $('cat-list'); //.lastChild; //Get the parent Element

    //Get all the IDs
    var ids = '';
    for (i = 0; i < thisPosition.childNodes.length; i++) {
        var curr = thisPosition.childNodes[i];
        if (curr.tagName.toUpperCase() == 'SPAN') {
            ids += curr.id + ',';
        }
    }

    //Trim last comma
    if (ids.length > 0) {
        ids = ids.substring(0, ids.length - 1);
    }

    //Update the hidden field
    $('categories_selectedcategories').value = ids;
}

function alreadyAdded(id) {
    //parse the hidden field contents looking for id. return true if found
    //word[0] contains "Welcome", word[1] contains "to" etc
    var ids = $('categories_selectedcategories').value.split(",");
    for(i=0; i<ids.length; i++) {
        if (ids[i] == id)
            return true;
    }
    return false;
}

function catsAdd(me, id){
	var thisList = $('cat-list'); //container to append
	var thisCat = me.innerHTML; //selected category
	var thisPosition = thisList; //.lastChild; //position to insert
	
	if (!alreadyAdded(id)) {
	    var span = new Element('span').update(thisCat + '<img src="/inc/img/remove.png" title="Delete" onclick="catsDel(' + id + ');" />');  //onclick="this.parentNode.remove();" />');
	    span.id = id;
    	
        //TODO: test for not already in the list.
	    //thisPosition.insert({'after':span});
	    thisPosition.appendChild(span);

	    //Update the hidden field
	    populateCategoryHidden();
	}
}
function catsDel(id) {
    $jq('#' + id).remove();

    //Update the hidden field
    populateCategoryHidden();
}
   
function del(me){
	var thisRemove = me;//delete what?!
	
	if(confirm('Are you sure you want to delete this?\n\nThis will delete the company listing entirely.')){
	    Element.remove(thisRemove);
	}
	return false;
}

function confirmRemoveAndSetIndicator(toRemove, prompt, fieldToSet, valueToSet) {

    if (confirm(prompt)) {
        $(fieldToSet).value = valueToSet;
        Element.remove(toRemove);
    }
    return false;
}

