var isIE = document.all?true:false;

function moveOption(moveFrom, moveTo, toDo, errorMessage) {
  var selectFrom = eval('document.compose.'+moveFrom);
  var selectTo = eval('document.compose.'+moveTo);
  var selectedIndex = selectFrom.options.selectedIndex;
  var container;

  if (toDo=='add') {
    container=eval('document.compose.' + toDo + capitalize(moveTo));
  }

  if (toDo=='remove') {
    container=eval('document.compose.' + toDo + capitalize(moveFrom));
  }

  if (selectedIndex == -1) {
    alert(errorMessage);
  } else {
    for (i=0; i<selectFrom.options.length; i ++) {
      if(selectFrom.options[i].selected) {
        var name = selectFrom.options[i].text;
        var ID = selectFrom.options[i].value;

        selectFrom.options[i] = null;
        selectTo.options[selectTo.options.length]= new Option(name,ID);
        i = i-1;

        if(toDo == 'add' || toDo == 'remove') {
          container.value = container.value + name + '^' + ID + ':';
        }

        sortSelect(selectTo);
      }
    }
  }
}

function sortSelect(selectToSort, ascendingOrder) {
    if (arguments.length == 1) ascendingOrder = true;    // default to ascending sort

    // copy options into an array
    var myOptions = [];
    for (var loop=0; loop<selectToSort.options.length; loop++) {
        myOptions[loop] = { optText:selectToSort.options[loop].text, optValue:selectToSort.options[loop].value };
    }

    // sort array
    if (ascendingOrder) {
        myOptions.sort(sortFuncAsc);
    } else {
        myOptions.sort(sortFuncDesc);
    }

    // copy sorted options from array back to select box
    selectToSort.options.length = 0;
    for (var loop=0; loop<myOptions.length; loop++) {
        var optObj = document.createElement('option');
        optObj.text = myOptions[loop].optText;
        optObj.value = myOptions[loop].optValue;
        selectToSort.options.add(optObj);
    }
}

function capitalize(word) {
    // the first character
    ch = word.substring(0,1);

    // the rest of the value
    rest = word.substring(1);

    // convert first character to uppercase
    up = ch.toUpperCase();

    // concatenate the uppercase with the rest
    word = up + rest;

    return word;
}

// sort function - ascending (case-insensitive)
function sortFuncAsc(record1, record2) {
    var value1 = record1.optText.toLowerCase();
    var value2 = record2.optText.toLowerCase();
    if (value1 > value2) return(1);
    if (value1 < value2) return(-1);
    return(0);
}

// sort function - descending (case-insensitive)
function sortFuncDesc(record1, record2) {
    var value1 = record1.optText.toLowerCase();
    var value2 = record2.optText.toLowerCase();
    if (value1 > value2) return(-1);
    if (value1 < value2) return(1);
    return(0);
}

function addEvent(obj, evType, fn){
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

function isUndefined(obj) {
	return (typeof(obj) == "undefined" || typeof(obj) == "undefined0");
}

function isString(obj) {
	if (typeof obj == 'string') {
		return true;
	}

	if (isObject(obj)) {
		var criterion =  obj.constructor.toString().match(/string/i);
		return (criterion != null);
	}

	return false;
}

function isFunction(obj) {
    return typeof obj == 'function';
}

function isObject(obj) {
    return (obj && typeof obja == 'object') || isFunction(obj);
}

function hasNativeCode(obj) {
	if(isString(obj) && obj.indexOf("native code") != -1) {
		return true;
	}

	return false;
}

function getAttributeValues(obj) {
	var attributeValues;

	if(isString(obj)) {
		attributeValues = obj;
	} else {
		for(attributeName in obj) {
			var attributeValue = obj[attributeName];

			if(isObject(attributeValue)) {
				attributeValue = attributeValue.toString();
			}

			if(!hasNativeCode(attributeValue)) {
				if(attributeName && !isUndefined(attributeName)) {
					attributeValues += attributeName + " = ";
				}

				attributeValues += attributeValue + "\n";
			}
		}
	}

	return attributeValues;
}

function showAttributes(obj) {
	var attributeValues = getAttributeValues(obj);

	if(attributeValues) {
		alert(attributeValues);
	}
}

function getStyle(targetSelector, attribute) {
	var styleSheets = document.styleSheets;
	targetSelector = targetSelector.toLowerCase();

	for(i = 0; i < styleSheets.length; i++) {
		var styleSheet = styleSheets[i];

		var cssRules;

		if(styleSheet.cssRules) {
			// moz
			cssRules = styleSheet.cssRules;
		} else if(styleSheet.rules) {
			// ie
			cssRules = styleSheet.rules;
		}

		for(j = 0; j < cssRules.length; j++) {
			var cssRule = cssRules[j];

			// if selector has commas, split these
			var selector = cssRule.selectorText.toLowerCase();
			var aSelectors = selector.split(', ');

			for(k = 0; k < aSelectors.length; k++) {
				selector = aSelectors[k];

				if(selector == targetSelector) {
					if(cssRule.style[attribute]) {
						var style = cssRule.style[attribute];

						return style;
					}
				}
			}
		}
	}

	return false;
}

document.getElementsByClassName = function (needle) {
	var my_array = document.getElementsByTagName("*");
	var retvalue = new Array();
	var i;
	var j;

	for (i = 0, j = 0; i < my_array.length; i++) {
		var c = " " + my_array[i].className + " ";

		if (c.indexOf(" " + needle + " ") != -1) {
		  retvalue[j++] = my_array[i];
		}
	}

	return retvalue;
}