if(!TCNA)
{
	var TCNA = {
		GLOBAL : {},
		Utils : {},
      CALENDAR: {},
      AJAX : {}
	};
}

TCNA.Utils.setDefaultText = function(element, defaultText, isFocused) {
   if(element)
   {
      if (element.value===defaultText && isFocused)
      {
         element.value=""
      }
      else if (element.value==="")
      {
         element.value=defaultText;
      }
   }
}

TCNA.Utils.padZero = function(value) {
	if(value < 10) return '0' + value;
	return value;
}

/**
 * DHTML date validation script for mm/dd/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
TCNA.Utils.isValidDate = function(dtStr) {
	// Declaring valid date character, minimum year and maximum year
	var dtCh= "/";
	var minYear=1900;
	var maxYear=2100;

	function isInteger(s){
		var i;
		for (i = 0; i < s.length; i++){   
			// Check that current character is number.
			var c = s.charAt(i);
			if (((c < "0") || (c > "9"))) return false;
		}
		// All characters are numbers.
		return true;
	};

	function stripCharsInBag(s, bag){
		var i;
		var returnString = "";
		// Search through string's characters one by one.
		// If character is not in bag, append to returnString.
		for (i = 0; i < s.length; i++){   
			var c = s.charAt(i);
			if (bag.indexOf(c) == -1) returnString += c;
		}
		return returnString;
	};

	function daysInFebruary (year){
		// February has 29 days in any year evenly divisible by four,
		// EXCEPT for centurial years which are not also divisible by 400.
		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	};
	
	function DaysArray(n) {
		for (var i = 1; i <= n; i++) {
			this[i] = 31
			if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
			if (i==2) {this[i] = 29}
	   } 
	   return this
	};


	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
	return true
}

/* 
This method only works if it is given the body id of the parent window you 
wish to retrieve.  If 'targetBodyId' is not given, we recursively retrieve the parents 10 times 
and return the topmost parent. 
An aside note: it seems like the parent.parent method 
returns the same parent regardless of the number of times you call it.  
Not sure why

parentWin - current parent window object
targetBodyId - the target body id of the parent window
num - the number of times it should call recursively
*/
TCNA.Utils.getMyTopParent = function(parentWin, targetBodyId, num) {
	var bodyId = parentWin.document.body.id;
	if (bodyId == targetBodyId || num <= 1) {
		//alert(num + ' ' + targetBodyId + ' ' + bodyId);	
      if (num <= 1)
         parentWin = top;
		return parentWin;
	}
	else {
		num--;
		return TCNA.Utils.getMyTopParent(parentWin.parent, bodyId, num)
	}
}
	
//Browser detection
TCNA.GLOBAL.isIE = function()
{
	if(document.all)
		return true;
	return false;
}

TCNA.Utils.resizeIframeHeight = function(iframe)
{
   if (TCNA.GLOBAL.isIE())
   {
      //is it the issue with IE 6 that we have to access the scrollHeight property twice?
      iframe.contentWindow.document.body.scrollHeight;
   }
   else
      iframe.height=0;
   iframe.height = iframe.contentWindow.document.body.scrollHeight;
}

//Either to show the select boxes or hide them
TCNA.Utils.toggleAllSelectBox = function(show)
{
   if(TCNA.GLOBAL.isIE())
   {
      var elements = document.getElementsByTagName("select");
      for(var i=0; i < elements.length; i++)
      {
         if (show)
            elements[i].style.visibility = "visible";
         else
            elements[i].style.visibility = "hidden";
      }
   }
}

TCNA.Utils.popWin = function(url,w,h,scroll,x,y,winname){
	var X = x + 50;
	var Y = y + 0;
	window.open(url,winname,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars='+ scroll + ',resizable=1,width=' + w + ',height=' + h + ',top=' + Y + ',left=' +X);
}

TCNA.Utils.getXYPosition = function(obj)
{
   var x = obj.offsetLeft;
   var y = obj.offsetTop + obj.offsetHeight ;
   // deal with elements inside tables and such
   var parent = obj;
   while (parent.offsetParent) {
   
      parent = parent.offsetParent;
      x += parent.offsetLeft;
      y += parent.offsetTop ;
   }
   return [x,y];
}

TCNA.Utils.setMaxWidth = function(obj, width){
  
   if (obj)
   {
      obj.style.width = "1px";
      obj.style.width = (document.body.scrollWidth > width ? width +"px" : "auto");
      alert(obj.style.width);
   }
}

 
TCNA.Utils.addEventListener = function(eStr, functionRef)
{
   if (window.addEventListener) {
      if (eStr.toLowerCase().startsWith("on"))
         eStr = eStr.substr(2, eStr.length);
      window.addEventListener(eStr, functionRef, false)
   }
   else if (window.attachEvent)
      window.attachEvent(eStr, functionRef)
}


/*
   Given a group of radio buttons, returns the checked value
*/
TCNA.Utils.getRadioButtonGroupValue = function (rbGroup)
{
   var rbgroupVal = "";
   if (rbGroup) {
      for (i=0;i<rbGroup.length;i++) 
      { 
         if (rbGroup[i].checked) 
         { 
            rbgroupVal = rbGroup[i].value; 
            break;
         } 
      }
   }
   return rbgroupVal;
}

/*
   Set the value of the current radio button group. 
   Trigger a callback in case the user wants to perform any other function
*/
TCNA.Utils.setRadioButtonGroupValue = function (rbGroup, rbgValue, callbackFunction)
{
   if (rbGroup) {
      for (i=0;i<rbGroup.length;i++) 
      { 
         if (rbgValue == rbGroup[i].value) 
         { 
            rbGroup[i].checked = true;
            if (callbackFunction)
               callbackFunction(rbGroup[i]);
            break;
         } 
      }
   }
}

/*
 * return the list of elements in an array
 * @parent - the parent where all of its child will be retrieved
 * @tag - filter the children base on the tag (ie. div)
 * @name - the name of the element to search for (note that some tag doesn't support name attribute
 * therefore use this with discretion)
 */
TCNA.Utils.getElementsByName = function(parent, tag, name) {
   if (!parent || parent == "")
      parent = document;
   var children;
   if (tag && tag != "")
      children = parent.getElementsByTagName(tag);
   else
      children = parent.childNodes;
   
   var retArray = new Array();
   for(var i=0; i < children.length; i++)
   {
      if(children[i].name = name)
      { 
         retArray[retArray.length] = children[i];
      }
   }
   return retArray;
}

TCNA.Utils.loadXML = function(sXML)
{
   var xmlDoc = null;
   // code for IE
   if (window.ActiveXObject)
   {
      xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(sXML);
      return xmlDoc;
   }
   // code for Mozilla, Firefox, Opera, etc.
   else
   {
      parser=new DOMParser();
      xmlDoc=parser.parseFromString(sXML,"text/xml");
      return xmlDoc;
   }
}

TCNA.Utils.reloadPage = function()
{
   var childrenFrames = document.getElementsByTagName("iframe");
   for ( var i = 0; i < childrenFrames.length; i++ )
   {
      childrenFrames[i].src = childrenFrames[i].src;
   }
   window.location.reload();
}

TCNA.Utils.setClass = function(obj, newClass) {
   obj.setAttribute('class', newClass);
}
/* String extensions */
String.prototype.trim = function()
{return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}
String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}
String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}