function trim ( inputStringTrim ) {
  fixedTrim = "";
  for (x=0; x < inputStringTrim.length; x++) {
    ch = inputStringTrim.charAt(x);
    if (ch != " ") break;
  }
  for (y = inputStringTrim.length-1; y>=0; y--) {
    ch = inputStringTrim.charAt(y);
    if (ch != " ") break;
  }
  if (x>y) 
       {fixedTrim = ""}
  else 
       {fixedTrim = inputStringTrim.substr(x,y-x+1)};
  return fixedTrim;
}

function Empty(num) {
  if (num == "") return true;
  num = num.toString();		/* ensure variable num is of datatype string before we use string routines */
  for (var i = 0; (i < num.length); i++) {
  	if (num.charAt(i) == " ") continue;
  	else return false; 
  }
  return true;	  
}

function LeftPad(sToBePadded, sPad, dFinalLength)
{
	var sTemp = "";
	for (i = 0; i < dFinalLength - sToBePadded.length; i++)
	{
		sTemp += sPad.toString();
	}
	
	return sTemp.concat(sToBePadded);
}

//[3]
function error(elem, text, bSelect) {
// display the first error
   var ss;

   if (typeof(text)  == "undefined") { window.alert("Undefined error."); this.select()}
   window.alert(text);
   if (typeof(elem)  == "undefined") {return}
   if (elem == "n"){return}
   if (elem.disabled == true) {return}
   if (bSelect != "false") {
      if (bSelect != "combo") {          
        try {
      	  document.forms[0].elements[elem.name].focus();        
      	  document.forms[0].elements[elem.name].select();        
      	}
      	catch (any) { }
      }
   };
   errorfound = true;
}

//[12]
function ValidMinMax(num, mini, maxi) {

        if (Empty(num)) {return true;}                
        return ((num >= mini) && (num <= maxi))
}

//[For Tab purposes]
function ShowTab(divname,tabid) {

            var TabDivs = getElementsByClassName('Tab');
            for (var tabNo = 0; tabNo < TabDivs.length; tabNo ++)
            {
				if(TabDivs[tabNo].id == divname)
				{
					//setting the main div content to respective content
					document.getElementById(divname).style.top = document.getElementById('TabContent').style.top + 60;
					document.getElementById(divname).style.left = document.getElementById('TabContent').style.left;
					document.getElementById(divname).style.display = "block";
				}
				else
				{
					TabDivs[tabNo].style.display = "none";
				}
            }
			
			//setting the css classname of the tab to active respectively
			var tb1rows = document.getElementById('TabHeaderRow');
			var cells = tb1rows.getElementsByTagName('td');

			for (var cellno=0; cellno<cells.length;	cellno++ ) {   
					if (cells[cellno].id == tabid) 
					{ 
						   cells[cellno].className = 'FormTabActive';
						   cells[cellno].onmouseover="this.className='FormTabActive'";
						   cells[cellno].onmouseout="this.className='FormTabActive'";							   
					}
					else 
					{ 
					      if((cells[cellno].id).indexOf('Tab_') >= 0)
					      {
							cells[cellno].className='FormTabInactive' ;
							cells[cellno].onmouseover="this.className='FormTabInactive'";
							cells[cellno].onmouseover="this.className='FormTabInactive'";
						  }
					}
				} //end for					
			}			
			
	var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};
//End For tab purposes

////////CHECK FOR bDataChanged Flag  i.e Warn On Unsaved Data
function CheckForBDataChanged(func) {
 var oldonload = window.onbeforeunload;
 if (typeof window.onbeforeunload != 'function') {
  window.onbeforeunload = func;
 } else {
  window.onbeforeunload = function() {
   if (oldonload) {
    oldonload();
   }
   func();
  }
 }

}

function ResetBDataChanged()
{
        var BDATACHANGED  = document.getElementById("BDATACHANGED");
        if (BDATACHANGED != null)
        {
            BDATACHANGED.value = 0;
        }   
}

function ExitForm4PortalASPX()
{
	/* version of ysilib.ExitForm for ASPX pages */
	
	/* this routine will prompt if user has changed data without saving
	 * Caller should set BSAVE = 1 if he wants to bypass this routine */

	if ( document.getElementById("BDATACHANGED") == null ) {return}
	if ( document.getElementById("BSAVE") != null && document.getElementById("BSAVE").value == "1")
	{
		document.getElementById("BSAVE").value = "0";
		return;
	}
	
	try
	{
		/* put this in try-catch because page might not have finished loading in which case querying document.activeElement result in an 'htmlfile: Unspecified error' */
		if (typeof(document.activeElement.href) != "undefined")
		{
			if (document.activeElement.href.indexOf("Lookup2") > 0)              {return}
			if (document.activeElement.href.indexOf("show_calendar") > 0)        {return}
			if (document.activeElement.href.indexOf("OpenNewWindow") > 0)        {return}
			if (document.activeElement.href.indexOf("MessageHandlerSelect") > 0) {return}
		}
	}
    catch (any)
    {
    	return;
    }
	
	/* trigger an onBlur, which will trigger the BDATACHANGED, in case they made a change without leaving the control */
	if ( typeof(document.activeElement) != "undefined" && typeof(document.activeElement.name) != "undefined" )
	{
		document.activeElement.blur();
	}
	
	if ( document.getElementById("BDATACHANGED").value == "1" )
	{
		//event.returnValue="Your data has NOT been saved!";
		return "Your data has NOT been saved!";
	}
}

