/**
 *	toolTips.js	v1.0
 * 	Coded by George Melroy D'Souza
 
 *	v2.0
 *	Amended by Pallavi Borkar - Aug 2011
 *	to show tooltips when typing in the fields.
 */
 
var gElemAttr = new Array();
function initToolTip(frmName){
	var toolTipDivs = document.myGetElementsByClassName('toolTipTag');
	var toolTipSTATDivs = document.myGetElementsByClassName('toolTipTagSTAT');
	var toolTipBtn = document.myGetElementsByClassName('toolTip');
	var toolTipSTATBtn = document.myGetElementsByClassName('toolTipSTAT');
	if(frmName){
		var inpChkElem = document.myGetElementsByClassName('inpChkBx');
		var inpTxtElem = document.myGetElementsByClassName('txtForm');
		var inpTxtElemBig = document.myGetElementsByClassName('txtFormBig');
		var inpSelElem = document.myGetElementsByClassName('selForm');
		var inpTxtArea = document.myGetElementsByClassName('txtFormAreaB');
	}

	for(var i=0; i<toolTipDivs.length; i++) {
		var toolTipId = 'toolTip' + i;
		toolTipDivs[i].btnObj = toolTipBtn[i];
		toolTipBtn[i].id = toolTipId;
		toolTipBtn[i].leftParentPos = findPosX(toolTipDivs[i]) + toolTipDivs[i].offsetWidth;
		toolTipDivs[i].txtTitle = (toolTipBtn[i].title) ? toolTipBtn[i].title : toolTipDivs[i].txtTitle;
		toolTipDivs[i].onmouseover = function(e){
			if (this.txtTitle) this.btnObj.style.visibility = 'visible';
		}		
		toolTipDivs[i].onmouseout = function(e){
			if (this.txtTitle) this.btnObj.style.visibility = 'hidden';
		}
		if (toolTipDivs[i].txtTitle) {
			toolTipBtn[i].onmouseover = function(e) {showToolTipBox(e,this);}
			toolTipBtn[i].onmouseout = function(e) {hideToolTipBox(e,this);}
		}
	}
	for(var i=0; i<toolTipSTATDivs.length; i++) {
		var toolTipId = 'toolTipSTAT' + i;
		toolTipSTATDivs[i].btnObj = toolTipSTATBtn[i];
		toolTipSTATBtn[i].id = toolTipId;
		toolTipSTATBtn[i].leftParentPos = findPosX(toolTipSTATDivs[i]) + toolTipSTATDivs[i].offsetWidth;
		toolTipSTATDivs[i].txtTitle = (toolTipSTATBtn[i].title) ? toolTipSTATBtn[i].title : toolTipSTATDivs[i].txtTitle;
		
		if (toolTipSTATDivs[i].txtTitle) toolTipSTATDivs[i].btnObj.style.visibility = 'visible';

		toolTipSTATBtn[i].onmouseover = function(e) {showToolTipBox(e,this);}
		toolTipSTATBtn[i].onmouseout = function(e) {hideToolTipBox(e,this);}
	}
	if(inpChkElem){
		for(var i=0; i<inpChkElem.length; i++) {
			var ttDivObj = inpChkElem[i].parentNode.parentNode.parentNode;
			if(ttDivObj){
				if(ttDivObj.className.indexOf('toolTipTag')>-1){
					var inpElemObj = inpChkElem[i];
					// In order to use the ttDivObj object in the onblur function , store the required parameters in a global array 'gElemAttr'.
					gElemAttr[inpElemObj.name] = new Array();
					gElemAttr[inpElemObj.name]['obj'] = ttDivObj;
					inpElemObj.onfocus = function(e) {
						if (gElemAttr[this.name]['obj'].txtTitle){
							gElemAttr[this.name]['obj'].btnObj.style.visibility = 'visible';
							showToolTipBox(e,gElemAttr[this.name]['obj'].btnObj);
						}
					}
					gElemAttr[inpElemObj.name]['oldOnBlur'] = inpElemObj.getAttribute('onblur');
					inpElemObj.onblur = function(e) {
						// 1. if title exists then show the tooltip help
						if (gElemAttr[this.name]['obj'].txtTitle){
							gElemAttr[this.name]['obj'].btnObj.style.visibility = 'hidden';
							hideToolTipBox(e,gElemAttr[this.name]['obj'].btnObj);
						}
						// 2. if existing on blur function is there, evaluate it.
						if(gElemAttr[this.name]['oldOnBlur']) eval(gElemAttr[this.name]['oldOnBlur']);
						// 3. If this field is mandatory then evaluate Validate function
						
						if(fldErrLabel['lbl'][this.name])  validateField(this);
					}
				}
			}
		}
	}
	if(inpTxtElem){
		for(var i=0; i<inpTxtElem.length; i++) {
			var ttDivObj = inpTxtElem[i].parentNode.parentNode;
			if(ttDivObj){
				if(ttDivObj.className.indexOf('toolTipTag')>-1){
					var inpElemObj = inpTxtElem[i];
					// In order to use the ttDivObj object in the onblur function , store the required parameters in a global array 'gElemAttr'.
					gElemAttr[inpElemObj.name] = new Array();
					gElemAttr[inpElemObj.name]['obj'] = ttDivObj;
					gElemAttr[inpElemObj.name]['oldOnFocus'] = inpElemObj.getAttribute('onfocus');
					inpElemObj.onfocus = function(e) {
						if (gElemAttr[this.name]['obj'].txtTitle){
							gElemAttr[this.name]['obj'].btnObj.style.visibility = 'visible';
							showToolTipBox(e,gElemAttr[this.name]['obj'].btnObj);
						}
						if(gElemAttr[this.name]['oldOnFocus']) eval(gElemAttr[this.name]['oldOnFocus']);
					}
					gElemAttr[inpElemObj.name]['oldOnBlur'] = inpElemObj.getAttribute('onblur');
					inpElemObj.onblur = function(e) {
						// 1. if title exists then show the tooltip help
						if (gElemAttr[this.name]['obj'].txtTitle){
							gElemAttr[this.name]['obj'].btnObj.style.visibility = 'hidden';
							hideToolTipBox(e,gElemAttr[this.name]['obj'].btnObj);
						}
						// 2. if existing on blur function is there, evaluate it.
						if(gElemAttr[this.name]['oldOnBlur']) eval(gElemAttr[this.name]['oldOnBlur']);
						// 3. If this field is mandatory then evaluate Validate function
						if(fldErrLabel['lbl'][this.name])  validateField(this);
					}
				}
			}
		}
	}
	if(inpTxtElemBig){
		for(var i=0; i<inpTxtElemBig.length; i++) {
			var ttDivObj = inpTxtElemBig[i].parentNode.parentNode;
			if(ttDivObj){
				if(ttDivObj.className.indexOf('toolTipTag')>-1){
					var inpElemObj = inpTxtElemBig[i];
					// In order to use the ttDivObj object in the onblur function , store the required parameters in a global array 'gElemAttr'.
					gElemAttr[inpElemObj.name] = new Array();
					gElemAttr[inpElemObj.name]['obj'] = ttDivObj;
					inpElemObj.onfocus = function(e) {
						if (gElemAttr[this.name]['obj'].txtTitle){
							gElemAttr[this.name]['obj'].btnObj.style.visibility = 'visible';
							showToolTipBox(e,gElemAttr[this.name]['obj'].btnObj);
						}
					}
					gElemAttr[inpElemObj.name]['oldOnBlur'] = inpElemObj.getAttribute('onblur');
					inpElemObj.onblur = function(e) {
						// 1. if title exists then show the tooltip help
						if (gElemAttr[this.name]['obj'].txtTitle){
							gElemAttr[this.name]['obj'].btnObj.style.visibility = 'hidden';
							hideToolTipBox(e,gElemAttr[this.name]['obj'].btnObj);
						}
						// 2. if existing on blur function is there, evaluate it.
						if(gElemAttr[this.name]['oldOnBlur']) eval(gElemAttr[this.name]['oldOnBlur']);
						// 3. If this field is mandatory then evaluate Validate function
						if(fldErrLabel['lbl'][this.name])  validateField(this);
					}
				}
			}
		}
	}
	if(inpTxtArea){
		for(var i=0; i<inpTxtArea.length; i++) {
			var ttDivObj = inpTxtArea[i].parentNode.parentNode;
			if(ttDivObj){
				if(ttDivObj.className.indexOf('toolTipTag')>-1){
					var inpElemObj = inpTxtArea[i];
					// In order to use the ttDivObj object in the onblur function , store the required parameters in a global array 'gElemAttr'.
					gElemAttr[inpElemObj.name] = new Array();
					gElemAttr[inpElemObj.name]['obj'] = ttDivObj;
					inpElemObj.onfocus = function(e) {
						if (gElemAttr[this.name]['obj'].txtTitle){
							gElemAttr[this.name]['obj'].btnObj.style.visibility = 'visible';
							showToolTipBox(e,gElemAttr[this.name]['obj'].btnObj);
						}
					}
					gElemAttr[inpElemObj.name]['oldOnBlur'] = inpElemObj.getAttribute('onblur');
					inpElemObj.onblur = function(e) {
						// 1. if title exists then show the tooltip help
						if (gElemAttr[this.name]['obj'].txtTitle){
							gElemAttr[this.name]['obj'].btnObj.style.visibility = 'hidden';
							hideToolTipBox(e,gElemAttr[this.name]['obj'].btnObj);
						}
						// 2. if existing on blur function is there, evaluate it.
						if(gElemAttr[this.name]['oldOnBlur']) eval(gElemAttr[this.name]['oldOnBlur']);
						// 3. If this field is mandatory then evaluate Validate function
						if(fldErrLabel['lbl'][this.name])  validateField(this);
					}
				}
			}
		}
	}
	if(inpSelElem){
		for(var i=0; i<inpSelElem.length; i++) {
			var ttDivObj = inpSelElem[i].parentNode.parentNode;
			if(ttDivObj){
				if(ttDivObj.className.indexOf('toolTipTag')>-1){
					var inpElemObj = inpSelElem[i];
					// In order to use the ttDivObj object in the onblur function , store the required parameters in a global array 'gElemAttr'.
					gElemAttr[inpElemObj.name] = new Array();
					gElemAttr[inpElemObj.name]['obj'] = ttDivObj;
					inpElemObj.onfocus = function(e) {
						if (gElemAttr[this.name]['obj'].txtTitle){
							gElemAttr[this.name]['obj'].btnObj.style.visibility = 'visible';
							showToolTipBox(e,gElemAttr[this.name]['obj'].btnObj);
						}
					}
					gElemAttr[inpElemObj.name]['oldOnBlur'] = inpElemObj.getAttribute('onblur');
					inpElemObj.onblur = function(e) {
						// 1. if title exists then show the tooltip help
						if (gElemAttr[this.name]['obj'].txtTitle){
							gElemAttr[this.name]['obj'].btnObj.style.visibility = 'hidden';
							hideToolTipBox(e,gElemAttr[this.name]['obj'].btnObj);
						}
						// 2. if existing on blur function is there, evaluate it.
						if(gElemAttr[this.name]['oldOnBlur']) eval(gElemAttr[this.name]['oldOnBlur']);
						// 3. If this field is mandatory then evaluate Validate function
						if(fldErrLabel['lbl'][this.name])  validateField(this);
					}
				}
			}
		}
	}
}

function showToolTipBox(e,ttipObj) {
	var divTipId = ttipObj.id + 'TP';

	if (document.getElementById(divTipId)) {
		var divTipId = ttipObj.id + 'TP';
		var divObj = document.getElementById(divTipId);
		divObj.style.visibility = 'visible';
		
		if (parseInt(ttipObj.leftParentPos) < (findPosX(ttipObj) + parseInt(divObj.offsetWidth) + 20)) {
			divObj.style.left = (parseInt(findPosX(ttipObj)) - parseInt(divObj.offsetWidth)) + 'px';
		}else{
			divObj.style.left = (findPosX(ttipObj) + 20) + 'px';
		}
	}else{
		var divObj = document.createElement("div");
		var subDivObj =  document.createElement("div");
		divObj.id = divTipId;
		divObj.className = 'abs toolTipHelp';
		divObj.style.top = (findPosY(ttipObj) +30) + 'px';
		divObj.style.zIndex = '100';
		subDivObj.className = 'brown allpad font11';
		subDivObj.innerHTML = ttipObj.title;
		ttipObj.title = '';
		ttipObj.appendChild(divObj);
		divObj.appendChild(subDivObj);
		
		if (parseInt(ttipObj.leftParentPos) < (findPosX(ttipObj) + parseInt(divObj.offsetWidth) + 20)) {
			divObj.style.left = (parseInt(findPosX(ttipObj)) - parseInt(divObj.offsetWidth)) + 'px';
		}else{
			divObj.style.left = (findPosX(ttipObj) + 20) + 'px';
		}
	}
}

function hideToolTipBox(e, ttipObj) {
	var divTipId = ttipObj.id + 'TP';
	var divObj = document.getElementById(divTipId);
	if(divObj) divObj.style.visibility = 'hidden';
}
