/**
 * Define the getElementById() method 
 * to eliminate some cross browser problems
 */
if( ! document.getElementById){
  document.getElementById = function(element_id) {
    // This method returns the element corresponding to the id
    if (document.all) {
      return document.all[element_id];
    }
    else if (document.layers) {
      return document.layers[element_id];
    } else {
      return undefined;
    }
  }
}

/**
 * verifies dates to make sure they match the yyyy-mm-dd format used by mySQL
 * @param date the date to check
 * @return boolean of whether or not the date is formatted correctly
 * @author jkunz
 */
function sqlDateVerify(date){
    var pattern=/^[0-9][0-9][0-9][0-9]-[0-1][0-9]-[0-3][0-9]$/;
    
    return pattern.test(date);
}

/**
 * verifies integers 
 * @param number the integer to check
 * @return boolean of whether or not the integer is formatted correctly
 * @author jkunz
 */
function sqlDateVerify(number){
    var pattern=/^[0-1]+$/;
    
    return pattern.test(number);
}

/**
 * takes a combo box and an id and selects the correct item from the combo
 * @param combo a combo box or <select> tag item
 * @param value the value to find in the options
 */
function LoadProperItem(combo,value){
    var length=combo.length;
    var index=0;

    for(var i=0;i<length;i++){
        var currvalue=combo[i].value;
        if(currvalue==value) index=i;
    }
    combo.selectedIndex=index;
}

/**
 * trims leading and trailing white spaces
 * @param str the string to trim
 * @return string w/out leading and trailing spaces
 */
function trim(str){
    str=str.replace(/^[\s]+/,"");
    str=str.replace(/[\s]+$/,"");

    return str;
}

/**
 * @param _id of the object to show/hide
 */
function showHide(_id){
	_element = document.getElementById(_id);
	if(_element){
		if(_element.style.display == "")
			_element.style.display = "none";
		else
			_element.style.display = "";
	}
}

/**
 * Finds the absolute x position of the object represented by _id
 * 
 */
function findPosX(_id){
		var obj = document.getElementById(_id);
        var curleft = 0;
        if (obj.offsetParent)
        {
            while (obj.offsetParent)
            {
                curleft += obj.offsetLeft
                obj = obj.offsetParent;
            }
        }
        else if (obj.x)
            curleft += obj.x;
        return curleft;
}
    
/**
 * Finds the absolute y position of the object represented by _id
 * 
 */
function findPosY(_id){
		var obj = document.getElementById(_id);
        var curtop = 0;
        if (obj.offsetParent)
        {
            while (obj.offsetParent)
            {
                curtop += obj.offsetTop
                obj = obj.offsetParent;
            }
        }
        else if (obj.y)
            curtop += obj.y;
        return curtop;
}

/**
 * Positions an element absolutely 
 */
function positionDiv(_id, x, y){
        var the_left=x;
        var the_top=y;
        
        // document.getElementById(_id).innerHTML = header + prompt;
        if (document.layers){
            document.getElementById(_id).style.left = the_left;
            document.getElementById(_id).style.top = the_top;
        }else{
            document.getElementById(_id).style.left = the_left + "px";
            document.getElementById(_id).style.top = the_top + "px";  
        }
}


/**
 * Opens new windows 
 * @param url the target url 
 */
function openSmall(url){
 var win = window.open(url,'_blank','height=400,width=440,toolbar=no,scrollbars=yes');
 win.focus();
 return;
}

function openLarge(url){
 var win = window.open(url,'_blank','height=580,width=750,toolbar=no,scrollbars=yes');
 win.focus();
 return;
}

function openEbilling(url){
 var win = window.open(url,'_blank','height=580,width=750,toolbar=no,scrollbars=yes,resizable=yes');
 win.focus();
 return;
}

/**
 * Functions specific to the Call Centers website
 */
	function getFileName(){
		if (location.href.lastIndexOf('/') > -1){
			path = document.location.pathname;
			firstpos = path.lastIndexOf('/')+1;
			lastpos = path.length;
			return path.substring(firstpos,lastpos);
		}
		return null;
	}
	
	var cats = ["ts", "cs", "ot", "os", "ec"];
	function setCategoryLinkStyle(filename){
	
		for(i=0; i < cats.length; i++){
			if(filename.indexOf(cats[i] + ".") == 0 || filename.indexOf(cats[i] + "_") == 0)
				$(cats[i]).className = "sideBarLinkTableTD_ON";
			else
				$(cats[i]).className = "sideBarLinkTableTD";
		}

	}
