

function tsinit()
{
	if (location.href.indexOf("/admin") != -1) { return; }
	var browser = navigator.appName; var version = navigator.appVersion;
	if (browser == 'Microsoft Internet Explorer')
	{
		if (version.indexOf('MSIE 5') != -1 && version.indexOf('Mac') != -1)
		{
			var loc = window.location.href;
			var coll = document.all.tags('div');
			for (i = 0; i < coll.length; i++)
			{
				if (coll(i).className == 'scrollable')
				{
					if ((loc.indexOf('#') != -1 || loc.indexOf('localref') != -1) && (loc.indexOf('#top') == -1))
					{
						coll(i).style.height = '100%';
						coll(i).style.overflow = 'visible';
					} else
					{
						coll(i).style.paddingRight = '16px';
						coll(i).style.marginRight = '-16px';
					}
				}
			}
			var obj = null;
			coll = document.all.tags('td');
			for (i = 0; i < coll.length; i++)
			{
				if (coll(i).className == 'maincell')
				{
					obj = coll(i);
				}
			}
			if (obj)
			{
				var srcData = obj.innerHTML;
				var mask = String.fromCharCode(173)
				var newData = "";
				var tmp = srcData;
				while (tmp != newData)
				{
					newData = tmp;
					tmp = newData.replace(mask, "");
				}

				if (newData != srcData)
				{
					obj.innerHTML = newData;
				}
			}
		}
	}
	highlightSearchwords();
}

function MiniCalendar (formname,fieldname,value,settingsid,ownerid,windowwidth,windowheight,windowcolor,containstime) {
    if (!containstime) {
        containstime=0;
    }
    value=eval ("document."+formname+"."+fieldname).value;
	window.open ('/lib/minicalendar.aspx?formname='+formname+'&fieldname='+fieldname+'&value='+value+'&settingsid='+settingsid+'&ownerid='+ownerid+'&windowcolor='+windowcolor+'&firsttime=1&containstime='+containstime,'','width='+windowwidth+',height='+windowheight+',dependent=yes');
}

function MiniCalendar2 (formname,fieldname,value,containstime,FromAdmin,SettingsId,pageid,datefmt) {
    if (formname==null || formname=="") 
    {   
        formname = document.forms[0].id;        
    }
    value=eval ("document."+formname+"."+fieldname).value;
	var CalendarDiv=document.getElementById ('calendardiv_'+fieldname);
	var Calendar=document.getElementById ('calendar_'+fieldname);
	if (datefmt) {
	}
	else {
	  datefmt="-1";
	}
	if (Calendar.style.visibility=='visible') {
		Calendar.style.visibility='hidden';
	}
	else {
		if (!FromAdmin && typeof (FromAdmin)!="undefined") {
			Calendar.src='/lib/minicalendar.aspx?inline=1&thedate='+value+'&formname='+formname+'&fieldname='+fieldname+'&settingsid='+SettingsId+'&pageid='+pageid+'&dateformat='+datefmt;
	    }
	    else {
			if (containstime) {
				Calendar.src='module.aspx?modid=1&refpath=calendar&thedate='+value+'&formname='+formname+'&fieldname='+fieldname+'&containstime='+containstime;
			}
			else {
				Calendar.src='module.aspx?modid=1&refpath=calendar&thedate='+value+'&formname='+formname+'&fieldname='+fieldname;
			}
			if (!Tangora.Browser.IE) {
			    Calendar.style.width='150px';
			}
		}
		Calendar.style.visibility='visible';
		
		// -----
		// HDN 31.08.2009: Firefox/Safari needs this in news module or else WYSIWYG buttons shine through... 
		Calendar.style.zIndex = 1;
		// -----
		
		TSSetCapture(CalendarDiv);
	}
	//CalendarDiv.style.display='inline';
}

function OpenWYSIWYGLink (s,width,height,screenx,screeny,canresizable,canscrollbars,canmenubar,cantoolbar,canlocation,candirectories,canstatus) {
	if (document.getElementById("maineditdiv") || document.getElementById("mainadminwindowtable")) {
		// ignore link when editing page
		if (window.event) {
			// this function is probably called from an onclick handler
			return false;
		} else {
			// this function is probably called from href
			return;
		}
	}
	var Specs=new String;
	if (width && (screenx || screenx==0)) {
		Specs="";
		if (width!=0) {
			Specs=Specs+"width="+width;
		} 
		if (height!=0) {
			if (Specs!="") {
				Specs=Specs+",";
			}
			Specs=Specs+"height="+height;
		}
		if (Specs!="") {
			Specs=Specs+",";
		}
		Specs=Specs+"left="+screenx;
		Specs=Specs+",top="+screeny;
		if (canresizable==1) {
			Specs=Specs+",resizable=yes";
		}
		if (canscrollbars==1) {
			Specs=Specs+",scrollbars=yes";
		}
		if (canmenubar==1) {
			Specs=Specs+",menubar=yes";
		}
		if (cantoolbar==1) {
			Specs=Specs+",toolbar=yes";
		}
		if (canlocation==1) {
			Specs=Specs+",location=yes";
		}
		if (candirectories==1) {
			Specs=Specs+",directories=yes";
		}
		if (canstatus==1) {
			Specs=Specs+",status=yes";
		}
		window.open (s,"",Specs);
	}
	else if (width) {
		if (width==0 || height==0) {
			window.open (s);
		}
		else {
			window.open (s,'','width='+width+',height='+height+',menubar=yes,scrollbars=yes,resizable=yes,status=yes');
		}
	}
	else {
		window.open (s);
	}
}

function StrReplace (Str,Find,Replace) {
	srs=Str;
	while (srs.indexOf (Find)!=-1) {
		srs=srs.replace (Find,Replace);
	}
	return srs;
}

function URLEncode (s) {
	s = unescape(s);	
	s = escape (s);
	return s;
}

function URLEncode2 (s) {
	s = StrReplace (StrReplace (StrReplace (StrReplace (StrReplace (StrReplace(s,unescape("%C6"), "%C3%86"),unescape("%D8"), "%C3%98"),unescape("%C5"), "%C3%85"),unescape("%E6"), "%C3%A6"),unescape("%F8"), "%C3%B8"),unescape("%E5"), "%C3%A5");
	s=StrReplace (s,"%","OPERCENTO");
	s=URLEncode (s);
	s=StrReplace (s,"OPERCENTO","%");
	return s;
}

function openMiniCal (formname,fieldname,value,settingsid,ownerid,windowwidth,windowheight,pageid,dateformat) {
	value=eval ("document."+formname+"."+fieldname).value;
	window.open ('/lib/minicalendar.aspx?pageid='+pageid+'&formname='+formname+'&fieldname='+fieldname+'&value='+value+'&settingsid='+settingsid+'&ownerid='+ownerid+'&dateformat='+URLEncode (dateformat)+'&firsttime=1','','width='+windowwidth+',height='+windowheight+',dependent=yes');
}

function MiniBasketLoaded() {
	//var obj=document.getElementById ('MiniBasketFrame');
	var obj=GetIframeDocument ('MiniBasketFrame');
	var obj2=document.getElementById ('MiniBasketDiv');
	
	if (obj && obj.body && obj.body.innerHTML!='' && obj2) {
		obj2.innerHTML=obj.body.innerHTML;
	}
}

function RandomNumber (From,To) {
	return (Math.floor ((To - From + 1) * Math.random() + From));
}

function RandomName (Length) {
	var ValidChars="abcdefghijklmnopqrstuvwxyz";
	var s="";
	for (i=0;i<Length;i++) {
		var j=RandomNumber (0,25);
		s=s+ValidChars.substring (j,j+1);
	}
	return s;
}

function hlWord(node,word) {	
	if (node.hasChildNodes) {
		var iterator;
		for (iterator=0;iterator<node.childNodes.length;iterator++) {			
			hlWord(node.childNodes[iterator],word);
		}
	}
		
	if (node.nodeType == 3) {
		tempNodeValue = node.nodeValue.toLowerCase();
		tempWordValue = word.toLowerCase();
		if (tempNodeValue.indexOf(tempWordValue) != -1) {
			pn = node.parentNode;
			if (pn.className != "searchword") {				
				nv = node.nodeValue;
				ni = tempNodeValue.indexOf(tempWordValue);				
				before = document.createTextNode(nv.substr(0,ni));
				docWordValue = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				hiwordtext = document.createTextNode(docWordValue);
				hiword = document.createElement("span");
				hiword.className = "searchword";
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function doHighlight(ref) {
	if (ref.indexOf('?') == -1) return;
		var refElement = document.getElementById("maincell");
		if (!refElement) {refElement = document.getElementById("mergefield_main");}
		if (!refElement) {refElement = document.getElementById("scrollable");}
		if (!refElement) {refElement = document.getElementsByTagName("body")[0];}		
		var paramStr = ref.substr(ref.indexOf('?')+1);
		var paramList = paramStr.split('&');
		for (i=0;i<paramList.length;i++) {
			var valueList = paramList[i].split('=');			
			if (valueList.length == 1) continue;
        	if (valueList[0] == 'q') { 
				var words = decodeURI(valueList[1].replace(/\+/g,' ')).split(/\s+/);
				for (w=0;w<words.length;w++) {						
					hlWord(refElement,words[w]);
                }
			}
		}	
}

function highlightSearchwords() {
	if (!document.createElement) return;		
	var ref = document.location.href;
	doHighlight(ref); // Highlight our local searchwords		
	var ref = document.referrer;
	doHighlight(ref); // Highlight words based on referer (google)
}

function getHTTPObject() {	
  var xmlhttp;    
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var TCookies = function ()
{
	this.SetCookieValue = function (name, key, value)
	{
		var argv = arguments;
		var argc = arguments.length;
		var expires = (argc > 3) ? argv[3] : null;
		var path = (argc > 4) ? argv[4] : null;
		var domain = (argc > 5) ? argv[5] : null;
		var secure = (argc > 6) ? argv[6] : false;

		// read previous state
		var state = this.GetCookie(name);

		// no cookie?
		if (!state)
		{
			state = '';
		}
		// remove identifier if already set
		var pos = state.indexOf('&' + key + '=');
		if (pos >= 0)
		{
			var pos2 = state.indexOf('&', pos + 1);
			if (pos2 == -1)
			{
				pos2 = state.length;
			}
			state = state.substr(0, pos) + state.substr(pos2);
		}

		// add new value
		if (typeof (value) != 'undefined')
		{
			state += '&' + key + '=' + escape(value);
		}

		// set cookie
		document.cookie = name + '=' + state +
			((expires == null) ? '' : ('; expires=' + expires.toGMTString())) +
			((path == null) ? '' : ('; path=' + path)) +
			((domain == null) ? '' : ('; domain=' + domain)) +
			((secure == true) ? '; secure' : '');
	}

	this.SetCookie = function (name, value)
	{
		var argv = arguments;
		var argc = arguments.length;
		var expires = (argc > 2) ? argv[2] : null;
		var path = (argc > 3) ? argv[3] : null;
		var domain = (argc > 4) ? argv[4] : null;
		var secure = (argc > 5) ? argv[5] : false;
		document.cookie = name + '=' + escape(value) +
			((expires == null) ? '' : ('; expires=' + expires.toGMTString())) +
			((path == null) ? '' : ('; path=' + path)) +
			((domain == null) ? '' : ('; domain=' + domain)) +
			((secure == true) ? '; secure' : '');
	}

	this.GetCookie = function (name)
	{
		var arg = name + '=';
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		while (i < clen)
		{
			var j = i + alen;
			if (document.cookie.substring(i, j) == arg) return this._getCookieVal(j);
			i = document.cookie.indexOf(' ', i) + 1;
			if (i == 0) break;
		}
		return null;
	}

	this.GetCookieValue = function (name, key)
	{
		var state = this.GetCookie(name);
		if (state)
		{
			var str = '&' + key + '=';
			var pos = state.indexOf(str);
			if (pos >= 0)
			{
				var pos2 = state.indexOf('&', pos + 1);
				if (pos2 == -1)
				{
					pos2 = state.length;
				}
				// remove prefix (key)
				pos += str.length;
				return state.substr(pos, pos2 - pos);
			}
		}
		return null;
	}

	this.DeleteCookie = function (name)
	{
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval = this.GetCookie(name);
		document.cookie = name + '=' + cval + '; expires=' + exp.toGMTString();
	}

	this._getCookieVal = function (offset) // private!
	{
		var endstr = document.cookie.indexOf(';', offset);
		if (endstr == -1)
		{
			endstr = document.cookie.length;
		}
		return unescape(document.cookie.substring(offset, endstr));
	}

}

function nameSpace()
{
	this.GetType = function () { return 'nameSpace' };
}

var fixObjects = false;

function resetObjects()
{
	if (!fixObjects) return;
	var coll = document.getElementsByTagName('object');
	for (var i = 0; i < coll.length; i++)
	{
		var nonia = coll[i].className;
		if (!nonia || nonia.toLowerCase().indexOf('noninteractive') == -1) coll[i].outerHTML = coll[i].outerHTML;
	}
}

if (typeof (Tangora) == 'undefined') var Tangora = new Object();
Tangora.Cookies = new TCookies();
Tangora.Events = new nameSpace();
// methods are added to Tangora.Events in compatibility.js //
Tangora.ServerVariables = new nameSpace();
var tmp = Tangora.Cookies.GetCookieValue('ServerVars', 'AcceptLanguage');
Tangora.ServerVariables.AcceptLanguage = (tmp != null) ? tmp.replace(/\|/g, ';') : "";

if (typeof (AddEventListener) == 'undefined')
{
	if (typeof (addEventListener) != 'undefined')
	{
		//AddEventListener = addEventListener;
		AddEventListener = function (what, where, elm)
		{
			if (!elm) window.addEventListener(what, where, false);
			else
			{
				elm.addEventListener(what, where, false);
			}
		}
	}
	else
	{
		AddEventListener = function (what, where, elm)
		{
			if (!elm) window.attachEvent('on' + what, where);
			else
			{
				elm.attachEvent('on' + what, where);
			}
		}
	}
}
