/*
Examples:
instead of: <body onload="myfunction()">
use: addEvent(window, 'load', myfunction);

instead of: window.onload = myfunction;
use: addEvent(window, 'load', myfunction);
*/
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 addEventToId(id, evType, fn) {
	addEvent(document.getElementById(id), evType, fn);
}
function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

function CapCheck(Element,CapitalizeOff) {
	var ElementValue = Element.val();
	if (!CapitalizeOff) {
		Element.val(CapFirstTitle(ElementValue,false));
	}
}

function CapFirstTitle(initText, UseNonCap) {
    
    var Words = new Array();
    var j = 1; var m = 1;
    var doCap = "";
    var thisWord = "";
    var excludeWords = new Array();
    var outputString = "";
    
    initText = initText.toLowerCase();
    
    if (UseNonCap) {
	    //Words to never capitalize
	    excludeWords[1] = "an";
	    excludeWords[2] = "the";
	    excludeWords[3] = "at";
	    excludeWords[4] = "by";
	    excludeWords[5] = "for";
	    excludeWords[6] = "of";
	    excludeWords[7] = "in";
	    excludeWords[8] = "up";
	    excludeWords[9] = "on";
	    excludeWords[10] = "to";
	    excludeWords[11] = "and";
	    excludeWords[12] = "as";
	    excludeWords[13] = "but";
	    excludeWords[14] = "if";
	    excludeWords[15] = "or";
	    excludeWords[16] = "nor";
	    excludeWords[17] = "a";
    }
    
    //Make each word in text an array variable
    Words = initText.split(" ");
    
    //Check words against exclude list
    for(j = 0; j <= Words.length-1; j = j+1){
        doCap = true;
		
        //Word must be less than four characters to be in the list of excluded words
        if(Words[j].length < 4 && excludeWords.length){
            if(listFindNoCase(excludeWords.join(','),Words[j])){
                doCap = false;
            }
        }

        //Capitalize hyphenated words        
        if(listLen(Words[j],"-") > 1){
            for(m=2; m <= listLen(Words[j], "-"); m=m+1){
                thisWord = listGetAt(Words[j], m, "-");
                thisWord = (thisWord.substr(0, 1).toUpperCase() + thisWord.substr(1,thisWord.length));
                Words[j] = listSetAt(Words[j], m, thisWord, "-");
            }
        }
        
        //Automatically capitalize first and last words
        if(j == 0 || j == Words.length-1){
            doCap = true;
        }
        
        //Capitalize qualifying words
        if(doCap){
            Words[j] = Words[j].substr(0, 1).toUpperCase() + Words[j].substr(1,Words[j].length-1);
        }
    }
    
    outputString = Words.join(' ');

    return outputString;
}

function listFindNoCase(theList, theValue, theDelim)
{
  var i = 0;
  var returnValue = 0;
  var theArr = new Array();
  
  if (!theDelim) {
	  theDelim = ",";
  }
  
  theArr = theList.split(theDelim);
  
  for(i=0; i < theArr.length; i++)
  {
    if(theArr[i].toLowerCase() == theValue.toLowerCase())
    {
      returnValue = i+1;
      break;
    }
  }
  
  return returnValue;
}

function listLen(c,d)
{
	if( c == "" ) return 0;
	if(arguments.length<2)d=",";
	return c.split(d).length;
}

function listGetAt(c,p,d)
{
	if(arguments.length<3)d=",";
	var e = c.split(d);
	if( p >= 1 && p <= e.length )
	{
		return e[p-1];
	}
	alert("Out of range");
	return "";
}

function listSetAt(c,p,v,d)
{
	if(arguments.length<4) d=",";
	var e = c.split(d);
	if( p >= 1 && p <= e.length )
	{
		e[p-1] = v;//set the value
		//rebuild the string
		for( var i=0,c=""; i < e.length; i++ )
			c = (c!=""?(c+d):"")+e[i];
	}
	else alert("listSetAt: Index " + p + " is Out of range");
	return c;
}

