var stopWatch = 0;
var stopWatchInterval;
var checkAlarmInterval;
function JSUtils()
{
  JSUtils.prototype.startStopWatch = startStopWatch;
  JSUtils.prototype.stopStopWatch = stopStopWatch;
  JSUtils.prototype.stopWatchSetAlarm = stopWatchSetAlarm;
  JSUtils.prototype.cancelStopWatchAlarm = cancelStopWatchAlarm;  
}

function startStopWatch()
{
  stopWatchInterval = window.setInterval(
  function () 
  {  
    stopWatch++;
  }
  ,1000);
}

function stopStopWatch()
{
    var stopTmp = stopWatch;
    stopWatch = 0;
    clearInterval(stopWatchInterval);
    return stopTmp;
}

function stopWatchSetAlarm(sec,scriptToExec)
{
  if(stopWatch == 0)
    this.startStopWatch();

  checkAlarmInterval = window.setInterval(
  function () 
  {  
    if(stopWatch >= sec)
    {
        this.stopStopWatch();
        clearInterval(checkAlarmInterval);
        eval(scriptToExec);
    }
  }
  ,1000);
}
    
function cancelStopWatchAlarm()
{
     this.stopStopWatch();
     clearInterval(checkAlarmInterval);
}

function addOmnitureSiteCatalyst(objToSave,keywordToSave)
{
	var s=s_gi('rsid');
	s.linkTrackVars='prop21';
	s.prop21=keywordToSave;
	s.tl(objToSave,'o','OU keyword: '+keywordToSave);
}

var autoFlow = "DESC";
function sortTableByColumn(tabl,colNum,flow,dataType)
{
	flow = flow==undefined?"ASC":flow;
	if(flow == "AUTO")
	{
		flow = autoFlow=="DESC"?"ASC":"DESC";		
	}
	autoFlow = flow;
	
	//setArrows(allTabl.getElementsByTagName("THEAD")[0],colNum,flow);
	dataType = dataType==undefined?"STR":dataType;
	
	//alert("flow :" +flow+"\ndataType:"+dataType);
	
	//var tabl = allTabl.getElementsByTagName("TBODY")[0];
	
	var rowAux;	
	var rowList = tabl.getElementsByTagName("tr");
		
	
	var rtArr = new Array();
	var rl = rtArr.length
		
	if(rowList.length != undefined && rowList.length > 0 )
	{
		for(var x=0;x<rowList.length;x++)
		{
		  var isHeaderRow = rowList[x].getElementsByTagName("th")[0] != undefined?true:false;
		  if(!isHeaderRow)
			  rtArr[x] = rowList[x];
		}
		
		//alert(rtArr);
		
		for(var j=0;j<rtArr.length;j++)
		{
			for(var i=0;i<rtArr.length;i++)
			{						
				if(compareItemsSort(rtArr[i].getElementsByTagName("td")[colNum],rtArr[j].getElementsByTagName("td")[colNum],flow,dataType))
				{
					var rowAux = new Array();
					var rowAux = rtArr.splice(i,1, rtArr[j].cloneNode(true));
					rtArr.splice(j,1, rowAux[0].cloneNode(true));				
					//rowsToArray[i+1] = rowAux.cloneNode(true);
				}
			}
		}	
		for(var x=rowList.length-1;x>=0;x--)
		{
			tabl.removeChild(rowList[x]);
		}
		for(var i=0;i<rtArr.length;i++)
		{
			tabl.appendChild(rtArr[i]);			 
		}		
	}
}


function compareItemsSort(itemA,itemB,flow,dataType)
{
	//  DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
	
	if(itemA.getElementsByTagName("A")[0] == undefined)
	{
		itemA = itemA.innerHTML;		
	}
	else
	{
		itemA = itemA.getElementsByTagName("A")[0].innerHTML;
	}
	if(itemB.getElementsByTagName("A")[0] == undefined)
	{
		itemB = itemB.innerHTML;
	}
	else
	{		
		itemB = itemB.getElementsByTagName("A")[0].innerHTML;
	}
	//alert(typeof(itemA));
	
	var boolResult;
	if(dataType == "STR")
	{
		itemA = (itemA.replace(" ","")).toLowerCase();
		itemB = (itemB.replace(" ","")).toLowerCase();
	}
	else if(dataType == "NUM")
	{
		itemA = parseFloat(itemA.replace(/[^0-9.-]/g,""));
		itemB = parseFloat(itemB.replace(/[^0-9.-]/g,""));
	}
	else if(dataType == "DATE")
	{
		itemA = formatDateType(itemA);
		itemB = formatDateType(itemB);
	}	
	
	if(flow == "ASC")
		boolResult=itemA>itemB?true:false;	
	else
		boolResult=itemA<itemB?true:false;	
	
	return boolResult;
}

function formatDateType(dateItem)
{
	dateItem = dateItem.replace(/-/g,"/");	
	var checkGlobForArr = dateItem.split("/");
	
	if(checkGlobForArr[2].length > 2)
	{
		dateItem = checkGlobForArr[2]+"/"+checkGlobForArr[1]+"/"+checkGlobForArr[0];
	}	
	var currDate = new Date(dateItem);
	return currDate;
}

function setSortArrows(tHead,tdSelected)
{
	var flow = autoFlow=="DESC"?"ASC":"DESC";
	var imgSelected;
	
	for(var i=0;i<tdSelected.getElementsByTagName("img").length;i++)
	{
		if(tdSelected.getElementsByTagName("img")[i].name == "sortArr")
		{
			imgSelected = tdSelected.getElementsByTagName("img")[i];
			break;
		}
	}
	
	for(var i=0;i<tHead.getElementsByTagName("img").length;i++)
	{
		if(tHead.getElementsByTagName("img")[i].name == "sortArr")
		{			
				tHead.getElementsByTagName("img")[i].src = "/education/images/upDownImg.gif";		
		}
	}	
	
	if(flow == "ASC")
		imgSelected.src = "/education/images/descImg.gif";
	else
		imgSelected.src = "/education/images/ascImg.gif";
	
}

function createLoadingDiv(styleBack)
{
	var newBackDiv = document.createElement("DIV");
	newBackDiv.id="loadingBack";	
	
	if(styleBack != undefined && styleBack != "")
	{
		newBackDiv.className = styleBack;
	}
	else
	{
		newBackDiv.style["background-color"]="#FFFFFF";
		newBackDiv.style["position"]="absolute";
		newBackDiv.style["width"]="100%";
		newBackDiv.style["height"]="100%";
		newBackDiv.style["z-index"]="20002";
		newBackDiv.style["left"]="0";
		newBackDiv.style["top"]="0";
		// FOR IE
		newBackDiv.style["filter"]="alpha(opacity=60)";
		// CSS3 standard
		newBackDiv.style["opacity"]="0.6";
		newBackDiv.style["background-image"]="url(/education/images/loading2.gif)";
		newBackDiv.style["background-repeat"]="no-repeat";
		newBackDiv.style["background-position"]="center";
		newBackDiv.style["list-style-position"]="outside";
	}
	document.body.appendChild(newBackDiv);
	return newBackDiv;
}

function showLoadingPage(styleBack)
{
	var divLoad = document.getElementById("loadingBack");	
	divLoad = (divLoad)?divLoad:createLoadingDiv(styleBack);
	var scrTop = parseFloat(document.documentElement.scrollTop);	
	document.documentElement.style.overflow = 'hidden';	 // firefox, chrome		
	window.scrollTo(0,scrTop);
	divLoad.style["top"]=scrTop + "px";				
	divLoad.style["display"]="";	
}

function stopLoadingPage()
{
	var divLoad = document.getElementById("loadingBack");
	divLoad = (divLoad)?divLoad:createLoadingDiv();
	var scrTop = parseFloat(document.documentElement.scrollTop);
	document.documentElement.style.overflow = 'scroll';	 // firefox, chrome
	window.scrollTo(0,scrTop);
	divLoad.style["display"]="none";
	
}

function lockEntiredPage()
{
	//backColor != null?backColor:"#CCCCCC";
	
	window.scrollTo(0,0);
		var newBackDiv = document.createElement("DIV");
		newBackDiv.id="lockBackground";	
		newBackDiv.style.backgroundColor="#CCCCCC";
		newBackDiv.style.position="absolute";
		newBackDiv.style.width="100%";
		newBackDiv.style.height="100%";
		newBackDiv.style.zIndex="20002";
		newBackDiv.style.left="0px";
		newBackDiv.style.top="0px";		
				
		if(document.all)
			newBackDiv.style.filter="alpha(opacity=60)"; // FOR IE
		else
			newBackDiv.style.opacity="0.6"; 	// CSS3 standard
		
				
		newBackDiv.style["list-style-position"]="outside";
		
		
		//var darkDiv = document.getElementById("darkBack");	
		var scrTop = parseFloat(document.documentElement.scrollTop);	
		document.documentElement.style.overflow = 'hidden';	 // firefox, chrome		
		window.scrollTo(0,scrTop);
		newBackDiv.style["top"]=scrTop + "px";				
		//darkDiv.style["display"]="";				
		 	
		document.body.appendChild(newBackDiv);
		return newBackDiv;	
}

function unlockEntiredPage()
{
	document.body.removeChild(document.getElementById("lockBackground"));	
}


function showIframeLightBox(src,w,h)
{
	var backDiv = lockEntiredPage(true);
	
	var iframContainer = document.createElement("div");
	iframContainer.style.position="absolute";
	iframContainer.id= "iFrameFlexCont";
	iframContainer.style.zIndex="20006";
	iframContainer.style.left="15%";
	iframContainer.style.top="9%";	
	iframContainer.style.width=(parseInt(w)+35)+"px";
	iframContainer.style.height=(parseInt(h)+35)+"px";
	iframContainer.style.overflow = 'hidden';	 // firefox, chrome
	
	
	var iFramCont = document.createElement("iframe");	
	//iFramCont.id="iFrameFlexCont";		
	iFramCont.style.position="absolute";
	iFramCont.style.width=w+"px";
	iFramCont.style.height=h+"px";
//	iFramCont.style.zIndex="20006";
//	iFramCont.style.left="15%";
	iFramCont.style.bottom="0px";
	iFramCont.style.border = "0px";
	iFramCont.style.overflow = 'hidden';	 // firefox, chrome
	iFramCont.scrolling = "no";
	iFramCont.src = src;
		
	var closeImg = document.createElement("div");
	closeImg.innerHTML = "<img src='/education/images/tod_closebutton.png' border='0' />";
	closeImg.style.top = "0px";
	closeImg.style.right = "0px";
	closeImg.style.position = "absolute";
	closeImg.style.cursor = "pointer";
	closeImg.style.zIndex="20007";
	closeImg.id = "iFrameFlexCloseButt";
	closeImg.onmousedown = function()
	{	
		closeIframeLightBox();
	} 
		
	iframContainer.appendChild(closeImg);
	iframContainer.appendChild(iFramCont);
	
	document.body.appendChild(iframContainer);		
	//document.body.appendChild(iFramCont);
}

function closeIframeLightBox()
{
	document.body.removeChild(document.getElementById("iFrameFlexCont"));
	//document.body.removeChild(document.getElementById("iFrameFlexCloseButt"));	
	unlockEntiredPage();
	document.documentElement.style.overflow = 'scroll';	 // firefox, chrome				
	window.scrollTo(0,scrTop);	
}

function extractOnlyNumber(_num)
{
	return parseFloat(_num.replace(/[^0-9.-]/g,""));
}

//-------- PROTOTYPE UTILS --------------------

Number.prototype.toMoney = function(decimals, decimal_sep, thousands_sep)
{ 
   var n = this,
   c = isNaN(decimals) ? 2 : Math.abs(decimals), //if decimal is zero we must take it, it means user does not want to show any decimal
   d = decimal_sep || ',', //if no decimal separetor is passed we use the comma as default decimal separator (we MUST use a decimal separator)

   /*
   according to [http://stackoverflow.com/questions/411352/how-best-to-determine-if-an-argument-is-not-sent-to-the-javascript-function]
   the fastest way to check for not defined parameter is to use typeof value === 'undefined' 
   rather than doing value === undefined.
   */   
   t = (typeof thousands_sep === 'undefined') ? '.' : thousands_sep, //if you don't want ot use a thousands separator you can pass empty string as thousands_sep value

   sign = (n < 0) ? '-' : '',

   //extracting the absolute value of the integer part of the number and converting to string
   i = parseInt(n = Math.abs(n).toFixed(c)) + '', 

   j = ((j = i.length) > 3) ? j % 3 : 0; 
   return sign + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ''); 
}

