// Copyright 1999-2005 Tangora Software A/S. All rights reserved.

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,toolbar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes');
			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 oldURLEncode (s) {
	s=StrReplace (s,"%","[PERCENT]");
	s=StrReplace (s,"[PERCENT]","%25");
	s=StrReplace (s,unescape("%E6"),"%E6");
	//s=StrReplace (s,unescape("%E6"),"%c3%a6");
	s=StrReplace (s,unescape("%F8"),"%F8");
	//s=StrReplace (s,unescape("%F8"),"%C3%B8");
	s=StrReplace (s,unescape("%E5"),"%E5");
	//s=StrReplace (s,unescape("%E5"),"%c3%a5");
	s=StrReplace (s,unescape("%C6"),"%C6");
	//s=StrReplace (s,unescape("%C6"),"%c3%86");
	s=StrReplace (s,unescape("%D8"),"%D8");
	//s=StrReplace (s,unescape("%D8"),"%c3%98");
	s=StrReplace (s,unescape("%C5"),"%C5");
	//s=StrReplace (s,unescape("%C5"),"%c3%85");
	s=StrReplace (s,unescape("%FC"),"%c3%bc");
	s=StrReplace (s,unescape("%F6"),"%c3%b6");
	s=StrReplace (s,unescape("%E4"),"%c2%a8");
	s=StrReplace (s,"<","%3C");
	s=StrReplace (s,"=","%3D");
	s=StrReplace (s,"\"","%22");
	s=StrReplace (s,"-","%2D");
	s=StrReplace (s,":","%3A");
	s=StrReplace (s,"#","%23");
	s=StrReplace (s,";","%3B");
	s=StrReplace (s,"/","%2F");
	s=StrReplace (s,"*","%2A");
	s=StrReplace (s,"+","%2B");
	s=StrReplace (s," ","+");
	s=StrReplace (s,"&","%26");
	s=StrReplace (s,unescape("%2E"),"%2E");
	s=StrReplace (s,unescape("%3E"),"%3E");
	return s;
}

function openMiniCal (formname,fieldname,value,settingsid,ownerid,windowwidth,windowheight,pageid,dateformat) {
	value=eval ("document."+formname+"."+fieldname).value;
	window.open (LocalReference+'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 getWindowHeight(minheight, usedheight) {	
	isIEMac = false;
	var browser = navigator.appName;
	var version = navigator.appVersion;
	if (browser == 'Microsoft Internet Explorer'){
		if (version.indexOf('MSIE 5') != -1 && version.indexOf('Mac') != -1) {
			isIEMac = true
		}
	}
	
	var h;
	if (document.all) {
		var a = new String (document.body.style.marginTop);
		a = a.replace ("px","");
		var b = new Number (a);		
		h = document.body.clientHeight - (b*2);
		//h = document.body.clientHeight;
		//alert (document.body.clientHeight);		
	}
	else {
		h = window.innerHeight;	
	}
	h = h - usedheight;
	if (h < minheight) {
			h=minheight;
	}
	if (isIEMac) {h -= 16} else {h -= 1}
	//alert ("H: "+minheight+","+usedheight+","+h)	
	return h;		
}

function getWindowWidth(minwidth, usedwidth) {		
	isIEMac = false;
	var browser = navigator.appName;
	var version = navigator.appVersion;
	if (browser == 'Microsoft Internet Explorer'){
		if (version.indexOf('MSIE 5') != -1 && version.indexOf('Mac') != -1) {
			isIEMac = true
		}
	}
	
	
	var w;
	if (document.all) {
		w = document.body.clientWidth;
	}
	else {
		w = window.innerWidth;	
	}		
	w = w - usedwidth;
	if (w < minwidth) {
			w=minwidth;
	}	
	if (isIEMac) {w -= 16}
	w = w - 25;
	//alert ("W: "+minwidth+","+usedwidth+","+w)
	return w;
}

function tsinit() {	
	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;
				}
			}
		} 
	}	
}

function setWindowStatus (str) {
	if (str) {
		var s = new String (str);
		re = /\<\w*\>/gi;
		s = s.replace (re,"");
		re = /\<\/\w*\>/gi;
		s = s.replace (re,"");
		window.status = s;
	} else {
		window.status ="";
	}
}
	
function get_url (layoutnumber) {
	var l = new String (location);
	var p = l.indexOf ("&layout=");
	var s = "";
	s = l;
	if (p!=-1) {		
		var p2 = l.indexOf ("&", p+8);
		if (p2!=-1) {
			s = l.substring (0, p);
			s += l.substring (p2, l.length - p2);
		} else {
			s = l.substring (0, p);
		}
		s = l.substring (0, p);
	}	
	s = s.replace ("#","");
	if (l.indexOf("?")>0) {
		s += "&layout=" + layoutnumber;
	} else {
		s += "?layout=" + layoutnumber;
	}	
	return s
}
	
function oldget_url (layoutnumber) {
	var l = window.location;
	alert (l);
	s = l.replace (/&layout=\d+/,"");	
	alert (s);
	//var s = location + "&layout=" + layoutnumber;
	s = s + "&layout=" + layoutnumber;
	alert (s);
	s = s.replace ("#","")	
	alert (s);	
	return s
}

function TestDummy(menu) {
	alert (document.getElementById ('maintextemailtext').outerHTML);
}

function TSCA_LoadContentArea (o, rightID, thisID, lp, rp, suffix) {
	o.onload="";
	if (!suffix) {suffix=''}
	if (rightID==thisID) {
		o.src='/setup/'+lp + 'act' + suffix + rp;
	} else {
		o.src='/setup/'+lp + 'inact' + suffix + rp;
	}
}

function TSCA_Hover(o, rightID, thisID, lp, rp, show) {
	if (rightID==thisID) {
		o.src='/setup/'+lp + 'act' + (show?"on":"off")+ rp;
		window.status=o.alt;
	} else {
		o.src='/setup/'+lp + 'inact' + (show?"on":"off") + rp;
		window.status="";
	}
}

function FixTab (s) {
	re=/&#39;/g;
	s=s.replace (re,"'");
    re=/%%13/g;
    s=s.replace (re,unescape('%0D'));
    re=/%%10/g;
    s=s.replace (re,unescape('%0A'));
	re=/%script%/g;
	s=s.replace (re,'script');
		
	//var i=s.indexOf ('<FONT STYLE="',i)
	//while (i!=-1) {
		//j=s.indexOf ('">',i+1);
		//s=s.substring (0,j-1)+'\\'+s.substring (j);
		//alert (s.substring (i,j+20));
		//i=s.indexOf ('<FONT STYLE="',i+10);
	//}
	
	//re=/<FONT STYLE="/gi;
	//s=s.replace (re,'<FONT STYLE=\\"');
	re=/<FONT STYLE="+this.style.backgroundColor/gi;
	s=s.replace (re,'<FONT STYLE=\\"+this.style.backgroundColor');
	re=/<FONT STYLE="BACKGROUND-COLOR:/gi;
	s=s.replace (re,'<FONT STYLE=\\"BACKGROUND-COLOR:');
	re=/">","<\/FONT>"/g;
	s=s.replace (re,'\\">","</FONT>"');
	re=/">","<\/font>"/g;
	s=s.replace (re,'\\">","</font>"');
	re=/<font color="/g;
	s=s.replace (re,'<font color=\\"');
	return (s);
}

function Number2fractionArray (n, ch) {
    var ch2 = ",";
	if(ch==",") {ch2="."} else {ch2=","}
	var pDec = n.indexOf(ch);
	var pThu = n.indexOf(ch2);	
	if (pDec>pThu) {
	  return n.replace(ch2,"").split(ch)
	} else {
		if ((pDec==-1) && (pThu==-1)) {
			var tmp = n+=ch+"0";
			return tmp.split(ch)
		} 
		if ((pDec==-1) && (pThu>-1)) {
			var tmp = n+=ch+"0";
			return tmp.replace(ch2,"").split(ch)
		} 
		if ((pDec>-1) && (pThu==-1)) {			
			return n.split(ch)
		} 
	}
}

function ToLegalNumberString(n,ch) {
  if (ch==",") {ch2="."} else {ch2=","}
  n = n.replace(ch2, "");
  return n.replace(ch,".");
}

function GetIframeDocument(framename) {
	if (window.frames[framename] && window.frames[framename].document) {
		return window.frames[framename].document;
	}
	var obj=document.getElementById(framename);
	if (obj && obj.contentDocument) {
		return obj.contentDocument;
	}
	return null;
}

function MiniBasketLoaded() {
	//var obj=document.getElementById ('MiniBasketFrame');
	var obj=GetIframeDocument ('MiniBasketFrame');
	var obj2=document.getElementById ('MiniBasketDiv');
	
	if (obj.body && obj.body.innerHTML!='') {
		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 NumberValidation (CtrlName,DecimalSep) {
	OK=((event.keyCode>=48 && event.keyCode<=57) || (event.keyCode>=96 && event.keyCode<=105) || event.keyCode==37 || event.keyCode==39 || event.keyCode==35 || event.keyCode==36 || event.keyCode==17 || event.keyCode==16 || event.keyCode==46 || event.keyCode==116 || event.keyCode==8 || event.keyCode==189 || event.keyCode==9);
	if (!OK && DecimalSep!='') {
		if ((DecimalSep=',' && event.keyCode==188) || (DecimalSep='.' && event.keyCode==190)) {
			OK=true;
		}
	}
	if (!OK) {
		event.keyCode=0;
		event.returnValue=false;
	}
}
