if(typeof HTMLElement!="undefined" && ! HTMLElement.prototype.insertAdjacentElement){ 
	HTMLElement.prototype.insertAdjacentElement = function (where,parsedNode) { 
		switch (where){ 
			case 'beforeBegin': 
			case 'BeforeBegin':
				this.parentNode.insertBefore(parsedNode,this) 
				break; 
			case 'afterBegin': 
			case 'AfterBegin':
				this.insertBefore(parsedNode,this.firstChild); 
				break; 
			case 'beforeEnd': 
			case 'BeforeEnd':			
				this.appendChild(parsedNode); 
				break; 
			case 'afterEnd': 
			case 'AfterEnd':
				if (this.nextSibling) 
					this.parentNode.insertBefore(parsedNode,this.nextSibling); 
				else 
					this.parentNode.appendChild(parsedNode); 
				break; 
		} 
	} 
}

var internalDivHeight = 140;
if (isBrowserIE){
	internalDivHeight = 170;
}

var focusAuxBGColor;
var focusCurrentBGColor = '#FFFFBB';
var focusCurrentFormName = '';
var focusCurrentFieldName= '';
var focusCurrentPageName = '';
var focusIsFixed = false;
var focusCurrentHelpURL  = '';

var http = getHTTPObject();

var focusHelpIMG = new Image();
focusHelpIMG.id  = 'focusHelpIMG';
focusHelpIMG.src = '/images/glasshelp.gif';
focusHelpIMG.hspace=5;
focusHelpIMG.style.height = 15;
focusHelpIMG.style.width  = 16;
focusHelpIMG.style.cursor = 'pointer';
focusHelpIMG.onclick = focusHelp;

//help div
document.write('<iframe id=focushdivi style="display:none;width:300px;height:250px;position:absolute;top:50%;left:50%;border:0px;margin-left:-150px;margin-top:-125px;z-index:50;"></iframe>');
document.write('<div id=focushdiv style="text-align:center;display:none;width:300px;height:250px;position:absolute;top:50%;left:50%;border:2px solid #C0C0C0;background-color:#EBE8DE;margin-left:-150px;margin-top:-125px;z-index:51;">');
document.write('<table width="100%" cellspacing=0 cellpadding=0 border=0 class="gralbarra">');
document.write('<tr><td style="padding-left:10px;">Tip</td></tr>');
document.write('</table>');
document.write('<div id=focushdivinternal style="width:290px;height:'+internalDivHeight+'px;padding-top:15px;padding-left:10px;padding-right:10px;padding-bottom:15px;text-align:left;overflow:auto;">');
document.write('</div><br><div align="center"><input type=image src="/lang/'+activeLang+'/images/bot_close.jpg"  style="width:55px;height:18px;" onClick="focusHelpClose();"></div>');
document.write('</div>');

function fieldGetsFocus(pageName,formName,fieldName,withHelp,helpURL){
	if (arguments.length < 5) helpURL = '';
	var fld = document.forms[formName].elements[fieldName];
	focusAuxBGColor = fld.style.backgroundColor;
	fld.style.backgroundColor = focusCurrentBGColor;	
	focusCurrentFormName = formName;
	focusCurrentFieldName= fieldName;
	focusCurrentPageName = pageName;
	focusIsFixed         = false;
	focusCurrentHelpURL  = helpURL;
	if (withHelp){
		//insert help image icon right after the field in document hierarchy
		fld.insertAdjacentElement("AfterEnd",focusHelpIMG);
		focusHelpIMG.src = '/images/glasshelp.gif';
		focusHelpIMG.onclick = focusHelp;
		focusHelpIMG.style.cursor = 'pointer';
	}
}

function fieldLosesFocus(){
	var fld = document.forms[focusCurrentFormName].elements[focusCurrentFieldName];
	fld.style.backgroundColor = focusAuxBGColor;
	focusHelpIMG.src = '/images/noglass.gif';
	//focusHelpIMG.onclick = '';
	focusHelpIMG.style.cursor = 'default';
}

function focusHelp(){
	//fetch help text using Ajax
	if (focusCurrentHelpURL.length == 0)
		var url = "ax_get_htip.php";
	else
		var url = focusCurrentHelpURL;
	var rand= new Date().getTime();	
	var params = "rnd="+rand+"&fon="+focusCurrentFormName+"&fin="+focusCurrentFieldName;
	params += '&pg='+focusCurrentPageName+'&lng='+activeLang;
	params += '&fromhtip=1';
  	http.abort();
    http.open("POST", url, true);
  	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  	    
 	http.onreadystatechange = focusHelpEnd;
  	http.send(params);		
}

function focusHelpEnd(){
	if (http.readyState == 4){
		//display div with help text
		var hdivi = document.getElementById("focushdivi");
		var hdiv  = document.getElementById("focushdiv");
		var hdivc = document.getElementById("focushdivinternal");

		//position centered in screen considering scrolling
		var margin = -125;
		if (typeof window.pageYOffset != 'undefined'){
			margin += window.pageYOffset;
		}else if (document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop != 0){
		    margin += document.documentElement.scrollTop;   
		}else if (typeof document.body.scrollTop != 'undefined'){
		   margin += document.body.scrollTop;
		}
		
		hdivc.innerHTML = http.responseText;
		
		hdivi.style.marginTop = margin + 'px';
		hdiv.style.marginTop  = margin + 'px';
		hdivi.style.display   = "block";
		hdiv.style.display    = "block";
	}
}

function focusHelpClose(){
	var hdivi = document.getElementById("focushdivi");
	var hdiv  = document.getElementById("focushdiv");

	hdivi.style.display = "none";
	hdiv.style.display  = "none";

	if (! focusIsFixed){
		document.forms[focusCurrentFormName].elements[focusCurrentFieldName].focus();
	}
}

function fixedHelpIcon(pageName,formName,fieldName){
	document.write('<img src="/images/glasshelp.gif" width=16 height=15 align=middle border=0 onClick="focusHelpFixed(\''+pageName+'\',\''+formName+'\',\''+fieldName+'\');" style="cursor:pointer;">');
}

function focusHelpFixed(pageName,formName,fieldName,helpURL){
	if (arguments.length < 4)
		helpURL = '';
	focusCurrentFormName = formName;
	focusCurrentFieldName= fieldName;
	focusCurrentPageName = pageName;
	focusIsFixed         = true;
	focusCurrentHelpURL  = helpURL;
	focusHelp();
}
