/* Function printf(format_string,arguments...)
 * Javascript emulation of the C printf function (modifiers and argument types 
 *    "p" and "n" are not supported due to language restrictions)
 *
 * Copyright 2003 K&L Productions. All rights reserved
 * http://www.klproductions.com 
 *
 * Terms of use: This function can be used free of charge IF this header is not
 *               modified and remains with the function code.
 * 
 * Legal: Use this code at your own risk. K&L Productions assumes NO resposibility
 *        for anything.
 ********************************************************************************/
function sprintf(fstring) { 
  var pad = function(str,ch,len)
      { var ps='';
        for(var i=0; i<Math.abs(len); i++) ps+=ch;
        return len>0?str+ps:ps+str;
      }
    var processFlags = function(flags,width,rs,arg)
      { var pn = function(flags,arg,rs)
          { if(arg>=0)
              { if(flags.indexOf(' ')>=0) rs = ' ' + rs;
                else if(flags.indexOf('+')>=0) rs = '+' + rs;
              }
            else
                rs = '-' + rs;
            return rs;
          }
        var iWidth = parseInt(width,10);
        if(width.charAt(0) == '0')
          { var ec=0;
            if(flags.indexOf(' ')>=0 || flags.indexOf('+')>=0) ec++;
            if(rs.length<(iWidth-ec)) rs = pad(rs,'0',rs.length-(iWidth-ec));
            return pn(flags,arg,rs);
          }
        rs = pn(flags,arg,rs);
        if(rs.length<iWidth)
          { if(flags.indexOf('-')<0) rs = pad(rs,' ',rs.length-iWidth);
            else rs = pad(rs,' ',iWidth - rs.length);
          }    
        return rs;
      }
    var converters = new Array();
    converters['c'] = function(flags,width,precision,arg)
      { if(typeof(arg) == 'number') return String.fromCharCode(arg);
        if(typeof(arg) == 'string') return arg.charAt(0);
        return '';
      }
    converters['d'] = function(flags,width,precision,arg)
      { return converters['i'](flags,width,precision,arg); 
      }
    converters['u'] = function(flags,width,precision,arg)
      { return converters['i'](flags,width,precision,Math.abs(arg)); 
      }
    converters['i'] =  function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        var rs = ((Math.abs(arg)).toString().split('.'))[0];
        if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
        return processFlags(flags,width,rs,arg); 
      }
    converters['E'] = function(flags,width,precision,arg) 
      { return (converters['e'](flags,width,precision,arg)).toUpperCase();
      }
    converters['e'] =  function(flags,width,precision,arg)
      { iPrecision = parseInt(precision);
        if(isNaN(iPrecision)) iPrecision = 6;
        rs = (Math.abs(arg)).toExponential(iPrecision);
        if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) rs = rs.replace(/^(.*)(e.*)$/,'$1.$2');
        return processFlags(flags,width,rs,arg);        
      }
    converters['f'] = function(flags,width,precision,arg)
      { iPrecision = parseInt(precision);
        if(isNaN(iPrecision)) iPrecision = 6;
        rs = (Math.abs(arg)).toFixed(iPrecision);
        if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) rs = rs + '.';
        return processFlags(flags,width,rs,arg);
      }
    converters['G'] = function(flags,width,precision,arg)
      { return (converters['g'](flags,width,precision,arg)).toUpperCase();
      }
    converters['g'] = function(flags,width,precision,arg)
      { iPrecision = parseInt(precision);
        absArg = Math.abs(arg);
        rse = absArg.toExponential();
        rsf = absArg.toFixed(6);
        if(!isNaN(iPrecision))
          { rsep = absArg.toExponential(iPrecision);
            rse = rsep.length < rse.length ? rsep : rse;
            rsfp = absArg.toFixed(iPrecision);
            rsf = rsfp.length < rsf.length ? rsfp : rsf;
          }
        if(rse.indexOf('.')<0 && flags.indexOf('#')>=0) rse = rse.replace(/^(.*)(e.*)$/,'$1.$2');
        if(rsf.indexOf('.')<0 && flags.indexOf('#')>=0) rsf = rsf + '.';
        rs = rse.length<rsf.length ? rse : rsf;
        return processFlags(flags,width,rs,arg);        
      }  
    converters['o'] = function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        var rs = Math.round(Math.abs(arg)).toString(8);
        if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
        if(flags.indexOf('#')>=0) rs='0'+rs;
        return processFlags(flags,width,rs,arg); 
      }
    converters['X'] = function(flags,width,precision,arg)
      { return (converters['x'](flags,width,precision,arg)).toUpperCase();
      }
    converters['x'] = function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        arg = Math.abs(arg);
        var rs = Math.round(arg).toString(16);
        if(rs.length<iPrecision) rs=pad(rs,' ',iPrecision - rs.length);
        if(flags.indexOf('#')>=0) rs='0x'+rs;
        return processFlags(flags,width,rs,arg); 
      }
    converters['s'] = function(flags,width,precision,arg)
      { var iPrecision=parseInt(precision);
        var rs = arg;
        if(rs.length > iPrecision) rs = rs.substring(0,iPrecision);
        return processFlags(flags,width,rs,0);
      }
    farr = fstring.split('%');
    retstr = farr[0];
    fpRE = /^([-+ #]*)(\d*)\.?(\d*)([cdieEfFgGosuxX])(.*)$/;
    for(var i=1; i<farr.length; i++)
      { fps=fpRE.exec(farr[i]);
        if(!fps) continue;
        if(arguments[i]!=null) retstr+=converters[fps[4]](fps[1],fps[2],fps[3],arguments[i]);
        retstr += fps[5];
      }
    return retstr;
  }
/* Function printf() END */

function createId(prefix) {
	select = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	
	result = prefix;
	for (var c = 0; c < 16; c++) {
		result += select[Math.floor(Math.random() * select.length)];
	}
	return result;
}

var __currentPopup = null;
var __skipOneClick = false;
var __closefunc = null;

function handleAsPopup(element, byclick, closefunc) {
	if ((__currentPopup != null) && (__currentPopup != element)) {
		if (__closefunc == null) {
			__currentPopup.style.visibility = 'hidden';
			__currentPopup = null;
		} else {
			__closefunc();
			__currentPopup = null;
		}
	}

	__skipOneClick = byclick;
	__currentPopup = element;
	__closefunc = closefunc;
	
	document.onmousedown = handleMouseDown;
}

function hideCurrentPopup() {
	if (__currentPopup != null) {
		if (__closefunc == null) {
			__currentPopup.style.display = 'none';
			__currentPopup = null;
		} else {
			__closefunc();
			__currentPopup = null;
		}
	}

	__skipOneClick = false;
	__currentPopup = null;
	__closefunc = null;	
}

function handleMouseDown(event) {
	/*
	if (__skipOneClick) {
		__skipOneClick = false;
		return;
	}*/	
	var e = event || window.event;
	var elem = e.target || e.srcElement;
	var cur = __currentPopup.firstChild;
	
	if (__currentPopup != null) {
		while (elem != null) {
			if (elem.id == cur.id) { 
				//alert("found");
				return; 
			}

			elem = elem.parentNode;
		}
		
		//current popup not clicked
		if (__closefunc == null) {
			__currentPopup.style.display = 'none';
			__currentPopup = null;
		} else {
			__closefunc();
			__currentPopup = null;
		}
		
		hidePopup();
	}
}

function handleFormSubmit() {
	return __currentPopup == null;
}

function elementsByClass(className) {
   classElems = Array();
   getElementsByClassName(className, document);
   return classElems;
}

function getElementsByClassName( strClassName, obj ) {
   if ( obj.className == strClassName ) {
      classElems[classElems.length] = obj;
   }
   for ( var i = 0; i < obj.childNodes.length; i++ )
      getElementsByClassName( strClassName, obj.childNodes[i] );
}

function unformatDate(dateStr, formatInfo) {
	var result = new Date();
	result.setTime(0);
	var pointer = 0;
	for (var i = 0; i < formatInfo.length; i++) {
		switch (formatInfo[i]) {
			case 'Y': {
				result.setFullYear(dateStr.substr(pointer, 4));
				pointer += 4;
				break;
			}
			case 'm': {
				var month = dateStr.substr(pointer, 1);
				pointer += 1;
				
				if (dateStr.substr(pointer, 1).match('[0-9]') != null) {
					month += dateStr.substr(pointer, 1);
					pointer++;
				}
				
				result.setMonth(month - 1);
				break;
			}
			case 'd': {
				var day = dateStr.substr(pointer, 1);
				pointer += 1;
				
				if (dateStr.substr(pointer, 1).match('[0-9]') != null) {
					day += dateStr.substr(pointer, 1);
					pointer++;
				}
				result.setDate(day);
				break;
			}
			default: {
				pointer += 1;
			}
		}
	}
	return result;						
}

function formatDate(dateInfo, formatInfo) {
	var result = '';
	for (var i = 0; i < formatInfo.length; i++) {
		switch (formatInfo[i]) {
			case 'Y': {
				result += dateInfo[0];
				break;
			}
			case 'm': {
				result += dateInfo[1];
				break;
			}
			case 'd': {
				result += dateInfo[0];
				break;
			}
			default: {
				result += formatInfo[i];
			}
		}
	}
	return result;
}

function getScrollData() {
  var scrOfX = 0, scrOfY = 0;
  var width = 0, height = 0;

  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
	scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
	width = window.innerWidth;
	height = window.innerHeight;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
	scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
	height = document.body.clientHeight;
	width = document.body.clientWidth;
  } else {//if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	//IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
	width = document.documentElement.clientWidth;
	height = document.documentElement.clientHeight;
  }
  return [ scrOfX, scrOfY, width, height ];
}

function scrollIntoView(element) {
	var pos = Position.cumulativeOffset(element);
	var dim = new Array(20, 20); //element.getDimensions();
	var scroll = getScrollData();
	
	dim[1] = element.scrollHeight;
	dim[0] = element.scrollWidth;
	
	if ((scroll[1] + scroll[3]) < pos[1] + dim[1]) {
		window.scrollTo(0, pos[1] - scroll[3] + dim[1]);
	} else if (scroll[1] > pos[1]) {
		window.scrollTo(pos[0], pos[1]);
	}
}

function needsIFrames() {
	//return false;
	var browser=navigator.appName
	if (browser == "Microsoft Internet Explorer") {
		var b_version=navigator.appVersion
		var version=b_version.split(';')[1];
		var ieversion = parseFloat(version.split(' ')[2]);
		if (ieversion < 6.5) {
			return true;
		}	
	}
	return false;
}

function isIE() {
	return (navigator.appName == "Microsoft Internet Explorer");
}

function setTopMost(element) {
	while (element != null) {
		element.style.zIndex = '2000';
		element = element.parent;
	}
}

function resetTopMost(element) {
	while (element != null) {
		element.style.zIndex = '';
		element = element.parent;
	}
}

function showPopup(target, msg, offset) {
	hidePopup();
	var div = document.getElementById('form_help');
		
	if (div != null) {
		div.style.display = 'none';
	}
				
	if (div == null) {
		div = document.createElement('div');
		if (needsIFrames()) {
			selhid = document.createElement('iframe');
			selhid.id = 'form_help_iframe';
			selhid.src = '/null.html';
			selhid.style.position = 'absolute';
			selhid.style.left = 0;
			selhid.style.top = 0;
			selhid.style.filter = 'mask()';
			selhid.style.border = 'none';
			selhid.style.zIndex = -1;
			div.appendChild(selhid);
		}
			
		div.id = 'form_help';
		div.style.display = 'none';		
		div.style.position = 'absolute';
		div.style.zIndex = '1100';
		/*
		div.onclick = function() {
			this.parentNode.removeChild(this);
		}*/

		var top = document.createElement('div');
		top.className = 'form_help_top';
		div.appendChild(top);
		
		var content = document.createElement('div');
		content.className = 'form_help_content';
		content.id = 'form_help_content';					
		div.appendChild(content);
		
		var bottom = document.createElement('div');
		bottom.className = 'form_help_bottom';
		div.appendChild(bottom);
							
		$('body').appendChild(div);
		var hoffset = getOffset($('pbody'), $(target));
		//alert(hoffset);
		hoffset[0] += 20;
		if (offset != null) {
			hoffset[0] += offset[0];
			hoffset[1] += offset[1];
		}
		div.style.left = hoffset[1] + 'px';
		div.style.top = hoffset[0] + 'px';
		div.style.display = 'block';
		div.style.zIndex = 2000;
	}
	
	var content = $('form_help_content');
	content.innerHTML = msg;
	
	
	if (needsIFrames()) {
		selhid.style.width = div.offsetWidth - 20;
		selhid.style.height = div.offsetHeight - 4;
	}
	
	setTopMost(div);
	//handleAsPopup(div);
	return div;
}

function showMovingPopup(target, msg, offset) {
	hidePopup();
	var div = document.getElementById('form_help');
		
	if (div != null) {
		div.style.display = 'none';
	}
				
	if (div == null) {
		div = document.createElement('div');
		if (needsIFrames()) {
			selhid = document.createElement('iframe');
			selhid.id = 'form_help_iframe';
			selhid.src = '/null.html';
			selhid.style.position = 'absolute';
			selhid.style.left = 0;
			selhid.style.top = 0;
			selhid.style.filter = 'mask()';
			selhid.style.border = 'none';
			selhid.style.zIndex = -1;
			div.appendChild(selhid);
		}
			
		div.id = 'form_help';
		div.style.display = 'none';		
		div.style.position = 'absolute';
		div.style.zIndex = '1100';
		/*
		div.onclick = function() {
			this.parentNode.removeChild(this);
		}*/

		var top = document.createElement('div');
		top.className = 'form_help_top';
		div.appendChild(top);
		
		var content = document.createElement('div');
		content.className = 'form_help_content';
		content.id = 'form_help_content';					
		div.appendChild(content);
		
		var bottom = document.createElement('div');
		bottom.className = 'form_help_bottom';
		div.appendChild(bottom);
							
		$(target).appendChild(div);

		div.style.left = offset[1] + 'px';
		div.style.top = offset[0] + 'px';
		div.style.display = 'block';
		div.style.zIndex = 2000;
	}
	
	var content = $('form_help_content');
	content.innerHTML = msg;
	
	
	if (needsIFrames()) {
		selhid.style.width = div.offsetWidth - 20;
		selhid.style.height = div.offsetHeight - 4;
	}
	
	setTopMost(div);
	//handleAsPopup(div);
	return div;
}

function getOffset(base, target) {

	var valueT = 0, valueL = 0;
    do {
      //alert(target + '->' + target.parentNode + ': (' + target.offsetLeft + ', ' +  target.offsetTop + ')');
	  valueT += target.offsetTop  || 0;
      valueL += target.offsetLeft || 0;
      target = $(target.offsetParent);
	  
      if (target) {
        if (target.tagName == 'BODY') break;
        var p = Element.getStyle(target, new String('position'));
		//if (p == 'relative' || p == 'absolute') break;
		if (p == 'fixed') {
			valueL += Position.realOffset(target)[0];
			valueT += Position.realOffset(target)[1];
			break;
		}
      }
    } while ((target) && (target != document.body) && (target != base));
	
    return Element._returnOffset(valueT, valueL);
}

function showDialog(target, msg, options, offset) {
	var optionsStr = '';
	
	for (var o = 0; o < options.length; o++) {
		optionsStr +=
			'<a href="' + options[o]['action'] + '">' + options[o]['title'] + '</a>&nbsp;';
	}

	showPopup(target, msg + '<br><center>' + optionsStr + '</center>', offset);
}

function hidePopup() {
	if ($('form_help') != null) {
		resetTopMost($('form_help'));
		if ($('form_help_iframe') != null) {
			$('form_help_iframe').parentNode.removeChild($('form_help_iframe'));
		}
		$('form_help').parentNode.removeChild($('form_help'));
	}
}

function nothing() {
}	

function loadScript(filename) {
	var head = document.getElementsByTagName('map').item(0);
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', filename);
	head.appendChild(js);
}