var mainpage_sender=null;
var selectWindow=null;
var cc=null;
var prov=null;

/* MACROMEDIA IMAGE PRELOADING FUNCTIONS */

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr;
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; 
	if(d.images){ 
		if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
		for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0) { 
				d.MM_p[j]=new Image;
				d.MM_p[j++].src=a[i];
			}
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  
	if(!d) d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
  if(!(x=d[n])&&d.all) x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
		x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; 
	document.MM_sr=new Array; 
	for(i=0;i<(a.length-2);i+=3)
		if ((x=MM_findObj(a[i]))!=null){
			document.MM_sr[j++]=x; 
			if(!x.oSrc) x.oSrc=x.src; 
			x.src=a[i+2];
		}
}

/* UNIQUE FUNCTIONS */

function grab_user_data(x){
	x.user_width.value = screen.width ? screen.width : 0;
	x.user_height.value = screen.height ? screen.height : 0;
	x.user_bpp.value = screen.pixelDepth ? screen.pixelDepth : screen.colorDepth;
	return true;
}

function yesclock(tz) {
	if (document.images) {
		setTimeout("renewClock("+tz+")",60000);
	}
}

function renewClock(tz) {
	var x=document.images["clock"];
	/* alert("Clock reloading!"); */
	x.src="/mkclock.php?tz="+tz+"&dummy="+((Math.random()).toString()).replace(".","");
}

function printablePage(url) {
	window.open(url,"","scrollbars=yes,menubar=yes,width=820,height=600,resizable=1");
	return false;
}

function popup_image(img_id,section) {
	window.open("/misc/show_image.php?id="+img_id+"&section="+section,"",
							"width=640,height=480,toolbar=no,scrollbars=no");
}

/* used from show_image.php */
function zoomImage(url,width,height){
	var myRnd="pw"+((Math.random()).toString()).replace(".","");
	
	var pw=window.open("",myRnd,"toolbar=no,scrollbars=no,width="+(width+20)+",height="+(height+20));

	pw.document.write("<html><head><title>Image "+url+"</title></head><body>");
	pw.document.write("<img src='"+url+"'></body></html>");
	return false;
}

/* sas == "set and submit". Arguments are:

   y: calling element (which is inside a form!)
   args: a list of value pairs (element name: string, element value: mixed)

	  The argument may also be "action" or "method", which then redirects the 
		 form to another page. This is used by PHP function "inputButton" in order 
		  not to send a special JavaScript function through PHP only to change the 
			 form receiver.
*/

function sas(y,args) {
	var v=sas.arguments;
	var btnset=false;

	if (v.length>1) {
		for (var i=1; i<v.length; i+=2) {
			if (v[i]!='') {
				if (v[i]=='action') y.form.action=v[i+1];
				if (v[i]=='method') y.form.method=v[i+1];
				else y.form.elements[v[i]].value=v[i+1];
			}
			if (v[i]=='btn') btnset=true;
		}
	}

	if (!btnset && y.form.btn!=undefined)y.form.btn.value="";

	/* input type=image and type=submit will submit by themselves, 
		 while input type=text is a workaround for planner, IIRC */

	if (y.type!='image' && y.type!='text' && y.type!='submit') y.form.submit();
}

function cfsas(y,msg,args) {
	var v=cfsas.arguments;
	var btnset=false;
	
	if (window.confirm(msg)) {
		if (v.length>1) {
			for (var i=2; i<v.length; i+=2) {
				if (v[i]!='') {
					if (v[i]=='action') y.form.action=v[i+1];
					if (v[i]=='method') y.form.method=v[i+1];
					else y.form.elements[v[i]].value=v[i+1];
				}	
				if (v[i]=='btn') btnset=true;
			}		
		}	
		
		if (!btnset && y.form.btn!=undefined) y.form.btn.value="";
		
		/* input type=image and type=submit will submit by themselves, 
			 while input type=text is a workaround for planner, IIRC */
		
		if (y.type!='image' && y.type!='text' && y.type!='submit') y.form.submit();
	}
}

function enterSubmit(x,e,of,ov) {
	var keycode;
	if (window.event) keycode=window.event.keyCode;
	else if (e) keycode=e.which;
	else return true;

	if (keycode==13) {
		x.form.elements[of].value=ov;
		x.form.submit();
		return false;
	}
	else return true;
}

function ays(message,url,x) {
  if (window.confirm(message+"?")) {
		x.href=url;
		return true; 
	} else return false;
}

function updateForm(sender){
	var x = document.profileForm;
	var intVal = parseInt(sender.value);
	if(isNaN(intVal) || intVal>0){
		x.submit();
	}
}

function validateImages(x){
	var i,j=0,jend=1,jcurr=1,filename,idx=-1,errmsg="";
	var newurl,imgact,imgarr=x.elements['imgnr[]'];
	
	if (imgarr) {
		while (j<jend) {
			if (imgarr.length) {
				jend=imgarr.length;
				jcurr=imgarr[j].value;
			}
			else jcurr=imgarr.value;

			imgact=x.elements['imgact['+jcurr+']'];
			newurl=x.elements['newurl['+jcurr+']'];
			
			if (imgact.value=='repl') {
				filename=newurl.value;
				for (i=0; i<filename.length; i++) {
					if (filename.charAt(i)==".") idx=i;
				}
				if (idx==-1) return true;
				else {
					var ext = filename.substr(idx+1);
					if (ext=="jpg" || ext=="gif")return true;
					else {
						// error
						errmsg += filename + " has an unknown extention. Supported types "+
							"are gif and jpg.\n";
					}
				}
			}
			j++;
		}
	}

	if (errmsg!="") {
		alert(errmsg);
		return false;
	} else return true;
}

function validateForm(x,args) {
	var i,j,k,d,s,ioc,iac,joc,jac,mark,v=validateForm.arguments,ok=true;
		
	for (i=1; i<v.length; i+=2) {
		s="txt"+v[i];
		d=document.getElementById(s);
		
		mark=(x.elements[v[i]].value=="");
		
		/* sekundärt argument
			 ------------------
			 
			  __________ fält där alla inom gruppen måste vara satta (1-9)
			 | 
			 v
			 00
			 ^
			 |_________ fält där något inom gruppen måste vara satt (1-9)
			 
		*/
		
		if (v[i+1]!="" && mark) {
			
			iac=v[i+1].substring(0,1);
			ioc=v[i+1].substring(1,2);
		
			/* kolla om det finns alternativt fält ifyllt */
	
			if (ioc>0) {
				j=1;
				while (j<v.length && mark) {
					if (i!=j && v[j+1]!="") {
						joc=v[j+1].substring(1,2);
						if (ioc==joc && x.elements[v[j]].value!="") mark=false;
					}
					j+=2;
				}
			}

			/* kolla om något av gruppens övriga obligatoriska fält
				 har ett alternativt fält ifyllt => hela gruppen kvittar */
			
			if (iac>0) {
				j=1;
				while (j<v.length && mark) {
					if (i!=j && v[j+1]!="") {
						jac=v[j+1].substring(0,1);
						
						ioc=v[j+1].substring(1,2);
						if (iac==jac && ioc>0) {
							k=1;
							while (k<v.length && mark) {
								if (j!=k && v[k+1]!="") {
									joc=v[k+1].substring(1,2);
									if (ioc==joc && 
											x.elements[v[k]].value!="") mark=false;
								}
								k+=2;
							}
						}
					}
					j+=2;
				}
			}
		}
		
		if (mark) {
			d.style.color="#ff0000";
			ok=false;
		} else d.style.color="#000000";
	}
	return ok;
}

function disableField(x,y) {
	var cmp,action,v=disableField.arguments,f=document.getElementById(y);

	cmp=(v.length>2 ? v[2] : 0);
	action=(v.length>3 ? v[3] : "");

  if (x.value!=cmp) {
    f.value = "";
    f.disabled = true;
		f.style.visibility='hidden';
		if (action=="reload" && x.value!=0) x.form.submit();
  }
  else {
		f.disabled = false;
		f.style.visibility='visible';
	}
}

function disableMultiField(x,args) {
	var i,vis,e,v=disableMultiField.arguments;

	vis=(x.value!=v[1] ? "hidden" : "visible");

	for (i=2; i<v.length; i++) {
		e=v[i];
		if (e.type==undefined) e=document.getElementById(e);
		else {
			if (x.value!=v[1]) {
				e.disabled=true;
				e.value="";
			}
			else e.disabled=false;
		}
		e.style.visibility=vis;
	}
}

function addOption(ele,val) {
	var i=Member(ele.options,val);
	if (i==ele.length) {
		ele.options[ele.length] = new Option("",val,true,true);
		ele.selectedIndex=ele.length-1;
	} else ele.selectedIndex=i;
}

function ArrMember(arr,str) {
	var found=false,i=-1;
	while (!found && ++i<arr.length) found=(arr[i]==str);
	return i;
}

function Member(arr,str) {
	var found=false,i=-1;
	while (!found && ++i<arr.length) found=(arr[i].value==str);
	return i;
}

/* y is a predicate (true or false), while o is a colour string */
function setColandOK(y,o) {
  col = (y ? "#ff0000" : o);
  ok = ok && !y;
}

function urlspace(str) {
	return (((str.toString()).replace(/ /g,"+")).
					replace(/&/g,"%26")).replace(/\?/g,"%3f");
}

function fakeHover(obj,event) {
	/*
		Mozilla 1.4 and Opera 7 uses document.defaultView.getComputedStyle
		while MSIE 6 uses obj.currentStyle. Not sure about the rest.
	*/

	var cstyle;

	switch (event) {
	case 'over':
		if (obj.currentStyle !== undefined) {
			obj.prevcolor=obj.currentStyle.color;
			obj.prevweight=obj.currentStyle.fontWeight;
		}
		else {
			cstyle=document.defaultView.getComputedStyle(obj,'');
			obj.prevcolor=cstyle.getPropertyValue("color");
			obj.prevweight=cstyle.getPropertyValue("fontWeight");
		}
		obj.style.color="#000000";
		obj.style.fontWeight="bold";
		break;
	case 'out':
		if (obj.prevcolor) obj.style.color=obj.prevcolor;
		if (obj.prevweight) obj.style.fontWeight=obj.prevweight; 
		break;
	}
}

function toggleVis(ele) {
		var vel=document.getElementById(ele);
    var vis=vel.style.visibility;     

		vel.style.visibility=(vis=="" || vis=="visible" ? "hidden" : "visible");
}

function setVis(ele,vis) {
		var vel=document.getElementById(ele);
		vel.style.visibility=vis;
}

function myFileBrowser (field_name, url, type, win) {

	// alert("Field_Name: " + field_name + "\nURL: " + url + "\nType: " + type + "\nWin: " + win); // debug/testing

	var fileBrowserWindow = new Array();

	fileBrowserWindow["file"] = "/upload.php?type=" + type + "&url=" + url;
	fileBrowserWindow["title"] = "File Browser";
	fileBrowserWindow["width"] = "574";
	fileBrowserWindow["height"] = "273"; /* need room for current image */
	fileBrowserWindow["close_previous"] = "no";
	tinyMCE.openWindow(fileBrowserWindow, {
		window : win,
    input : field_name,
    resizable : "yes",
    inline : "yes"
  });
	return false;
}


function myFileBrowser3 (field_name, url, type, win) {

	tinyMCE.activeEditor.windowManager.open({
    file : "/upload3.php?type=" + type + "&url=" + url,
    width: 574,
    height: 273,
    resizable: "yes",
    inline : "yes",
    close_previous: "no"
},{
    window: win,
    input: field_name
  });
	return false;
}


function DNS(x) {
	var dns1=document.getElementById("dns");
	var dns2=document.getElementById("dns2");

	dns1.style.visibility=(x==0 ? "hidden" : "visible");
	dns2.style.visibility=(x==0 ? "hidden" : "visible");
}

function popDNS(dns,tld) {
  var myRnd = "pw"+((Math.random()).toString()).replace(".","");

	var param="toolbar=0,location=0,directories=0,status=0,resizeable=0";
	param=param+",menubar=0,scrollbars=1,width=300,height=200";

	window.open("/dns/index.php?dns="+dns+"&tld="+tld,myRnd,param);
	return false;
}

function validate_form(obj,args) {
		var i,j,k,el,vl,e=0,errms="";
		var v=validate_form.arguments;

		for (i=0; i<obj.elements.length; i++) {
				el=obj.elements[i];
				if (el.className.indexOf('check')>0 || el.className == 'check') {
					
						switch (el.type) {
						case "text":
								if(el.value == "") {
										e = 1;
										el.style.backgroundColor = '#dddddd';
										errms = 'Du har missat att fylla i en del obligatoriska fält. Dessa har markerats.\n';
								}  else {
										el.style.backgroundColor = '#ffffff';
								}
								break;
								
						case "checkbox":
								if (!el.checked) {
										e=1;
										errms=errms+'Du har glömt att kryssa i rutan för hantering av personuppgifter.\n';
								}
						}
				}
		}

		/* Args structure: 

			 checkfield,checkvalue,valuefield,checkfield,checkvalue,valuefield

			 This simplified structure is possible because even
			 if many valuefields belong to one checkfield, the
			 first valuefield still needs to be filled in.
		*/
	
		if (v.length>1) {
				for (i=1; i<v.length; i+=3) {
						el=obj.elements[v[i]];

						for (j=0; j<el.length; j++) {
								if (el[j].value==v[i+1]) {
										vl=obj.elements[v[i+2]];
										if (el[j].checked && vl.value=="") {
												e=1;
												vl.style.backgroundColor = '#dddddd';
												errms = 'Du har missat att fylla i en del obligatoriska fält. Dessa har markerats.\n';
										} else {
												vl.style.backgroundColor = '#ffffff';
										}
								}
						}
				}
		}
	
		if(e == 1) {
				alert(errms);
				return false;
		}
		else {
				return true;
		}
}

/* dålig lösning för att få lokaliserade javascripts */

function validate_form_en(obj) {
	var e = 0;
	var errms = '';
	
	for (var i = 0; i < obj.elements.length; i++) {
		var el = obj.elements[i];
		if (el.className.indexOf('check')>0 || el.className == 'check') {
					
			switch (el.type) {
			case "text":
				if(el.value == "") {
					e = 1;
					el.style.backgroundColor = '#dddddd';
					errms = 'You have forgotten to fill in a few required fields. These have been highlighted.\n';
				}  else {
					el.style.backgroundColor = '#ffffff';
				}
				break;
				
			case "checkbox":
				if (!el.checked) {
					e=1;
					errms=errms+'You have forgotten to check the box for handling personal details.\n';
				}
			}
		}
	}
	
	if(e == 1) {
		alert(errms);
		return false;
	}
	else {
		return true;
	}
}

/* används denna? */

function validateCheckedFields(form) {
	var e = 0;
	var errms = '';
	var obj = document.getElementById(form);
	
	for (var i = 0; i < obj.elements.length; i++) {
		var el = obj.elements[i];

		// || el.type == 'text'
		if (el.className.indexOf('check')>=0) {

				switch (el.type) {
				case "text":
				case "password":
				case "textarea":
						if(el.value == "") {
								e = 1;
								el.style.backgroundColor = '#c8aaaa';
								errms = 'Följande fel har uppstått:\nDu har missat att fylla i en del obligatoriska fält. Dessa har markerats.\n';
						}  else {
								el.style.backgroundColor = '#ffffff';
						}
						break;
						
				case "checkbox":
						if (!el.checked) {
								e=1;
								errms=errms+'Du har glömt att kryssa i rutan för hantering av personuppgifter.\n';
						}
				}
		}
	}
	
	if(e == 1) {
		alert(errms);
		return false;
	}
	else {
		return true;
	}
}

