// whitespace characters: ' ', '\t', '\r', '\n'
var whitespace = " \t\n\r";
var isIE = (navigator.appName == "Microsoft Internet Explorer")? true : false;

// isEmpty: Check whether string strVal is empty
function isEmpty(strVal)
{
	return ((strVal == null) || (strVal.length == 0));
}

// isWhitespace: Check if string strVal has only the whitespace characters
function isWhitespace(strVal)
{
	var nPos = 0;

	if (isEmpty(strVal))
		return false;

	for (nPos = 0; nPos < strVal.length; nPos++)
		if (whitespace.indexOf(strVal.charAt(nPos)) == -1)
			return false;

	return true;
}

// rtrim: Right trim the string and return the trimmed value
function rtrim(strVal)
{
	var nPos = 0;

	if (isEmpty(strVal))
		return strVal;

	for (nPos = strVal.length-1; nPos >= 0; nPos--) {
		if (whitespace.indexOf(strVal.charAt(nPos)) == -1)
			break;
	}

	return (nPos == strVal.length-1 ? strVal.substring(0) : strVal.substring(0, nPos+1));
}

// ltrim: Left trim the string and return the trimmed value
function ltrim(strVal)
{
	var nPos = 0;

	if (isEmpty(strVal))
		return strVal;

	for (nPos = 0; nPos < strVal.length; nPos++) {
		if (whitespace.indexOf(strVal.charAt(nPos)) == -1)
			break;
	}

	return strVal.substring(nPos);
}

function alltrim(strVal){ return strVal.replace(/^[ \t]+|[ \t]+$/g, ""); }

/********************************************************
 replace: useful for JavaScript 1.0 and 1.1 where replace 
	  is not available. With JavaScript 1.2 (and above)
	  replace function is available.
	: replaces one occurrence of strVal with strWith in strSrc
********************************************************/
function replace(strSrc, strVal, strWith)
{
	var nPos = 0, strLeft="", strRight="";

	// check if empty (or) no string is found to replace
	if (isEmpty(strSrc) || (strSrc.indexOf(strVal) < 0))
		return strSrc;

	nPos = strSrc.indexOf(strVal);
	strLeft = strSrc.substring(0, nPos);
	nPos += strVal.length;
	strRight = strSrc.substring(nPos);

	return (strLeft + strWith + strRight);
}

// replaceall : replace all occurrences of strVal with strWith in strSrc
function replaceall(strSrc, strVal, strWith)
{
	var strBuffer=strSrc;

	while (strBuffer.indexOf(strVal) >= 0)
		strBuffer = replace(strBuffer, strVal, strWith);

	return (strBuffer);
}

// occurs: return no of occurrences of strVal within strSrc
function occurs(strVal, strSrc)
{
	var nCnt = 0, strBuffer=strSrc;

	while (strBuffer.indexOf(strVal) >= 0)
	{
		strBuffer = replace(strBuffer, strVal, "");
		nCnt++;
	}

	return (nCnt);
}

// replicate: returns string that contains strVal repeated nCnt times
function replicate(strVal, nCnt)
{
	var i = 0, strBuffer = "";

	for (i = 0; i < nCnt; i++)
		strBuffer += strVal;

	return (strBuffer);
}

/**************************************************************
 stuff: returns string after replacing nCnt characters (starting
	from nStartPos) with strReplacement in strSrc. 

 NOTE: starting position is 0
**************************************************************/
function stuff(strSrc, nStartPos, nCnt, strReplacement)
{
	var strLeft= "", strRight="";

	// check boundary values
	if (nCnt < 0 || nStartPos < 0 || (nStartPos > strSrc.length-1))
		return strSrc;

	strLeft = strSrc.substring(0, nStartPos);
	strRight = strSrc.substring(nStartPos+nCnt);

	return (strLeft + strReplacement + strRight);
}



/*function from cdc site*/
function autoTab(input,len, e){

  var isNN = (navigator.appName.indexOf("Netscape")!=-1);
  var keyCode = (isNN)?e.which:e.keyCode;
  var filter = (isNN)?[0,8,9]:[0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)){

    input.value = input.value.slice(0,len);
	  
    input.form[(getLoanAppIndex(input)+1)%input.form.length].focus();
	
  }
  return true;
}

function containsElement(arr, ele){
//alert("in");
  var found = false, index = 0;
  while(!found && index < arr.length)
    if(arr[index]==ele)
      found = true;
    else
      index++;

  return found;
}

function getLoanAppIndex(input){
  var index = -1, i = 0, found = false;
  while (i < input.form.length && index==-1)
    if (input.form[i] == input)index = i;
    else i++;
  return index;
}


function doDigit(myfield, e, dec){
	var key, keychar;	
	if (window.event){key = window.event.keyCode;}
	else if (e){key = e.which;}
	else{return true;}	
	//allow input (.)dot (once only) with money fields	
	if(dec){if(key==46){
			oneDotOnly = /([.]{1})/;
			if(myfield.value.search(oneDotOnly) == -1){return true; }
			else{ return false;}}}	
	keychar = String.fromCharCode(key);
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ){return true;}
	else if ((("1234567890").indexOf(keychar) > -1)){return true;}
	else{return false;}
}
	

function CheckForm(fm){
 	var result = false;
	result = chkMandatory(fm);
 	return result;
}


var IFrameObj;
function closeIframePrompt(){
	iframeCall().style.visibility="hidden";
}

function closeWindowOrIframe () {
	if (parent.iframeCall() && parent.closeIframePrompt() ) {
		parent.closeIframePrompt();
	} else {
		window.close();
	}
}

function iframeCall(){
	Iframe=eval('document.getElementById("iframePrompt")');
	return Iframe;
}



function showIframePrompt(theSrc, theWidth, theHeight) {
	if (!iframeCall()) {return true};
	numArgs = arguments.length;
	
	theTop = pos.y + 15;
	theLeft = pos.y -(theWidth/2);
	IFrameObj=iframeCall();
	//the purpose of appending 2 argument toward the end of this func
	//is to fixed the iframe popup was display incorrectly
	//assuming the fourth args for fixing top pos.
	 theTop = ((numArgs >= 4)? theTop + parseInt(arguments[3]): theTop);
	 theLeft = ((numArgs >= 5)?theLeft + parseInt(arguments[4]): theLeft);

	
	iframeCall().src = theSrc;
	iframeCall().style.left= theLeft + "px";
	iframeCall().style.top=theTop + "px";
	
	if (!isIE) {
		theWidth = (theWidth/1)-5;
		theHeight = (theHeight/1)-5;
	}

	iframeCall().style.width = theWidth;
	iframeCall().style.height = theHeight;
	iframeCall().style.visibility="visible";


}

function closeIframePrompt(){
	iframeCall().style.visibility="hidden";
}


/*
this function assigns alternating row colors to table with id=tableId

Usage:
1. include this *.js file in the report page.
2. If dojo is used on the report page, make a call to paintRows() after </table>. E.g.:
	<script type="text/javascript">paintRows('table1');</script>

3. If dojo is not used, window.onload call to paintRows() can be used. E.g.:
	window.onload = function(){
		paintRows('table1');
	}

4. Default class names are 'even' and 'odd'.
*/
function paintRows(tableId){
	switch (arguments.length){
		case 1:
			var classEven = 'even';
			var classOdd = 'odd';
		break;
		case 2:
			var classEven = arguments[1];
			var classOdd = 'odd';
		break;
		case 3:
			var classEven = arguments[1];
			var classOdd = arguments[2];
	}
	var r = document.getElementById(tableId).tBodies[0].rows; //get reference to an array of table rows. Assign class names to even / odd rows.
	for(var i=0; i < r.length; i++){
		var tmp = r[i].className;
		if(tmp.length > 0)
			r[i].className = (i % 2 == 0) ? tmp+' '+classEven : tmp+' '+classOdd; 
		else	
			r[i].className = (i % 2 == 0) ? classEven : classOdd; 
	}
}

function doHighlightRows(TableId){
	NumArgs = arguments.length;
	ClsHighLight = (NumArgs >=2)? arguments[1]: "HighLight"; //assuming arg. 2 contain tr highlight class name
	SkipFirstRow = (NumArgs >=3)?arguments[2]: false; //assuming arg 3 contain true/false, default false
	SkipLastRow = (NumArgs >=4)?arguments[3]: false; //assuming arg 4 contain true/false, default false	
	t = document.getElementById(TableId).tBodies[0];
	if(!t) t = document.getElementById(TableId).rows;
	if(t){
		r = t.rows		
		for(i=0; i < r.length; i++){
			if(SkipFirstRow && i==0){continue;} //skip first row
			if((i+1) == r.length && SkipLastRow){break;}	//skip last row			
			r[i].onmouseover = function(){
				this.setAttribute("OldClass", this.className);
				this.className = ClsHighLight;
			};
			r[i].onmouseout = function(){
				this.className = this.getAttribute("OldClass");
			};
		}
	}
}

function $createEl(el){return document.createElement(el);}

function $el(id){return	document.getElementById(id);}

function $createTn(textNode){return document.createTextNode(textNode);}

function resetSelectOption(el){el.length = 0;}

function $tagElms(tagName){return document.getElementsByTagName(tagName);}


function makeBr(){return $createEl('br');}

function makeSpace(){	
	return $createTn(' ');	
}

function makePipe(numRepeat){
	var pipe = ' ';
	for(i=0; i<numRepeat; i++){
		pipe += '|';	
	}
	pipe +=  ' ';
	return $createTn(pipe);
}


function makeInputBox(attributes){
	//attributes = "name=fieldName|more key=valu....
	//IE won't work for radio button
	var checkBox = $createEl("input");
	var tempArr = attributes.split("|");
	
	for(i=0; i <tempArr.length; i++){
		kvPair = tempArr[i].split("=");
		checkBox.setAttribute(kvPair[0], kvPair[1]);
	}
	return checkBox;
}

function getParentNode(elmt,tagMatch) {
	if (elmt) {
		tagMatch = tagMatch.toLowerCase();
		elmt = elmt.parentNode;
		while (elmt && (!elmt.tagName || (elmt.tagName && (elmt.tagName.toLowerCase() != tagMatch)))) 
			elmt = elmt.parentNode;
		return elmt;
	} else {
		return null;
	}
}//getParentNode

function getChildNode(elmt,tagMatch) {
	if (elmt) {
		tagMatch = tagMatch.toLowerCase();
		elmt = elmt.firstChild;
		while (elmt && (!elmt.tagName || (elmt.tagName && (elmt.tagName.toLowerCase() != tagMatch)))) 
			elmt = elmt.nextSibling;
		return elmt
	} else {
		return null;
	}
}//getChildNode

function getNextSibling(elmt,tagMatch) {
	if (elmt) {
		tagMatch = tagMatch.toLowerCase();
		elmt = elmt.nextSibling;
		while (elmt && (!elmt.tagName || (elmt.tagName && (elmt.tagName.toLowerCase() != tagMatch))))
			elmt = elmt.nextSibling;
		return elmt;
	} else {
		return null;
	}
}//getNextSibling

function doMaxLength(el, maxLimit){
	cLen = el.value.length;	
	if(cLen > maxLimit){el.value = el.value.substring(0, maxLimit-1);}	
}

function incrementCounter(targetEl, evalEl){
	targetEl.value = evalEl.value.length;
}



function isValidEmail(email){
	strEmailPattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if (strEmailPattern.test(email)){return true;}
	else{return false;}}

function isValidURL(url){		
	var urlPatern = /^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i;
	url = url.toLowerCase();
	if(urlPatern.test(url)){
		return true;
	}
	return false;
}

function openWindow(url){
	numArgs = arguments.length;
	w=500; //default
	h=400; //default
	if(numArgs >=2){ //window width
		w=arguments[1];
	}
	if(numArgs >=3){ //window height
		h=arguments[2];
	}
	
	window.open(url,'newwindow',config='height='+h+',width='+w+',toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=no,status=no')
}