﻿var menuOpen=false;
var timeout;
var closing;
var mouse_pos_x;
var mouse_pos_y;
if(trainor == null) var trainor={};
if(trainor.popup==null) trainor.popup={};
if(trainor.tools==null) trainor.tools={};
function initKampanje()
{
	//Fjerne boxHel
	var e = document.getElementsByTagName("div");
	for(var i=0; i<e.length;i++)
	{
		if(e[i].className=="boxHel")
		{
			e[i].style.display='none';
		}
	}
	var head=document.getElementsByTagName('head').item(0);
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', '/AC_RunActiveContent.js');
	head.appendChild(js);
	//alert(js);
	e=document.getElementsByTagName("img");
	for(var i=0; i<e.length;i++)
	{
		if(e[i].src.toString().indexOf("top_midle.gif")!=-1)
			e[i].width='650';
	}

}


function disableEnterKey(e)
{
     var key;


     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
          return false;
     else
          return true;
}

function changeLanguage(id)
{
	var e =document.endrespraak;
	e.spraakid.value=id;
	e.url.value=document.location;	
	e.submit();
}

function setPos()
{
	var pos=document.getElementById("mousepos");
	pos.innerHTML=window.event.clientX+","+window.event.clientY;
}
function uploader(useid)
{
	var msg='<applet code=trainor.magnus.fileuploaderapplet.MainFrame.class width=300 height=200 >';
	msg+='<param name="userid" value="'+useid+'"/>';
	msg+='<param name="url" value="'+location.hostname+'"/>';
	msg+='<param name=archive value="/trainoruploader/TrainorFileUploader.jar, /trainoruploader/lib/jcommon-1.0.14.jar,  /trainoruploader/lib/substance.jar, /trainoruploader/lib/edtftpj.jar" />';
	msg+='</applet>';
	document.getElementById("up").innerHTML=msg;
}
function isArray(obj) 
{
	try{
	   if (obj.constructor.toString().indexOf("Array") == -1)
		  return false;
	   else
		  return true;
	  }catch(error){return false;}
}
function openChildWindow(file, window)
{

	//childWindow=open(file, window, 'width=800 heigth=700 resizable=no ');
	childWindow=open(file, window, 'resizable=no, width=1100, height=750');
	if(childWindow.opener==null)childWindow.opener=self;
	
}
function forceDXPUpdate()
{
	var div=document.getElementById("dxpp");
	div.innerHTML="Oppdaterer DXP lisenser...";
	DwrMethods.forceDxpUpdate(
		function(data)
		{
			if(data=="ok")
				div.innerHTML="Dxp lisenser er oppdatert!";
			else
				div.innerHTML=data;
		}
	);
}
function forcePostCodesUpdate()
{
	var div=document.getElementById("postcodes");
	div.innerHTML="Oppdaterer postnummere...";
	DwrMethods.forcePostCodesUpdate(
		function(data)
		{
			if(data=="ok")
				div.innerHTML="Postnummere er oppdatert!";
			else
				div.innerHTML=data;
		}
	);
}

function isInteger(s)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	for (var i = 0; i < s.length && IsNumber == true; i++) 
    { 
		Char = s.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			IsNumber = false;
	}
   return IsNumber;
}

trainor.popup.fadeElement=function (id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
	
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) 
	{ 
        for(var i = opacStart; i >= opacEnd; i--) 
		{ 
            setTimeout("trainor.popup.changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
	else if(opacStart < opacEnd) 
	{ 
	
        for(var i = opacStart; i <= opacEnd; i++) 
		{ 
            setTimeout("trainor.popup.changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    }  
};

//change the opacity for different browsers 
trainor.popup.changeOpac=function (opacity, id) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
};

trainor.popup.handlePopupMenuClose=function ()
{
	if(!menuOpen)
		return;
	var menu=document.getElementById("popupmenu");
	var x1, x2, y1, y2;
	x1=menu.style.pixelLeft;
	x2=menu.style.pixelLeft+menu.offsetWidth;
	y1=menu.style.pixelTop;
	y2=menu.style.pixelTop+menu.offsetHeight;
	
	if(mouse_pos_x<x1 || mouse_pos_x>x2)
		trainor.popup.closePopupMenu();
	else if(mouse_pos_y<y1 || mouse_pos_y>y2)
		trainor.popup.closePopupMenu();
};
trainor.popup.openPopupMenu=function(left, top, width, opac)
{
	menuOpen=true;
	try{clearTimeout(timeout);}catch(ignored){} //clears closemenu timeout
	try{clearTimeout(closing);}catch(ignored){} //clears fading
	menuOpen=true;
	var menu=document.getElementById("popupmenu");
	if(!left || !top || !width)
	{
		menu.style.pixelLeft=mouse_pos_x;
		menu.style.pixelTop=mouse_pos_y;
		menu.style.top=mouse_pos_y;
		menu.style.left=mouse_pos_x;
	}
	else
	{
		menu.style.pixelLeft=left;
		menu.style.pixelTop=top;
		menu.style.top=top;
		menu.style.left=left;
		menu.style.width=width;
	}
	trainor.popup.changeOpac(0, menu.id);	 //setting opac to zero before fading in...
	menu.style.display='';
	var o = 90;
	if(opac)
		o=opac;
	trainor.popup.fadeElement(menu.id, 0, o, 500);	
};
trainor.popup.closePopupMenu = function(instant)
{
	if(!instant)
	{
		closing=setTimeout("trainor.popup.closePopupMenu(true)", 50);
		return;
	}
	if(!menuOpen)
		return; 
	var menu=document.getElementById("popupmenu");
	try{
		trainor.popup.fadeElement(menu.id, 90,0,650);
		timeout=setTimeout("document.getElementById('popupmenu').style.display='none'",700);
		menuOpen=false;
		if(trainor)
		{
			if(trainor.store)
			{
				trainor.store.closeTkaCoursePopup();
			}
		}
			
	}
	catch(ignore){}
};
trainor.tools.getReq = function()
{
	var xmlHttp=null;
	try
	{
	// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
  // Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if(xmlHttp==null)
		alert("Missing AJAX support!");
	return xmlHttp;
};
trainor.tools.reloadCaptcha = function(img)
{
	if(!img.src)
		img=document.getElementById(img);
	var s = img.src;
	if(s.indexOf("&rand=") !=-1)
	{
		s=s.substring(0,s.indexOf("&rand="));
		s+="&rand="+Math.random();
	}
	else if(s.indexOf("?rand=")!=-1)
	{
		s=s.substring(0,s.indexOf("&rand="));
		s+="&rand="+Math.random();
	}
	else if(s.indexOf("?")!=-1)
		s+="&rand="+Math.random();
	else
		s+="?rand="+Math.random();
		
	img.src=s;
};

trainor.tools.loadCaptcha = function(img, type, width, height)
{
	if(!img.src)
		img=document.getElementById(img);
	var s="scaptcha";
	var first=true;
	if(type)
	{
		s+="?";
		first=false;
		s+="type="+type;
	}
	if(width)
	{
		if(first)
			s+="?";
		else
		{
			first=false;
			s+="&";
		}
		
		s+="width="+width;
	}
	if(height)
	{
		if(first)
			s+="?";
		else
		{
			first=false;
			s+="&";
		}
		s+="height="+height;
	}
	
	s+="&rand="+Math.random();
	img.src=s;

};

function prepareOpenCert(ids, cids, type)
{
	var id = new Vector(10);
	var cid = new Vector(10);
	for(var i=0; i<ids.length;i++)
	{
		id.addElement(ids[i].value);
		cid.addElement(cids[i].value);
	}
	openCertificateDialog(id.getArray(), cid.getArray(), type);
}
function prepareOpenCertFromEReport()
{
	var e=document.getElementsByName("certbox");
	var id=new Vector(10);
	var courseid=new Vector(10);
	for(var i=0; i<e.length;i++)
	{
		if(e[i].checked)
		{
			var a=e[i].value.split("|");
			id.addElement(Number(a[0]));
			courseid.addElement(Number(a[1]));
		}
	}
	if(id.getSize()<1)
		return;
	openCertificateDialog(id.getArray(), courseid.getArray(), 1);
	

}


function openCertificateDialog(id, courseid, type)
{
	
	var div=document.getElementById("overlaydata9");
	var main=document.getElementById("overlay9");
	div.innerHTML="Loading...";
	try{
		openOverlay(9);
	}catch(error)
	{
		alert("Din nettleser støtter ikke dialoger.\nScroll ned på bunnen for å se en begrenset dialog ");
		div.style.display='';
		main.style.display='block';
		main.style.top='0px';
		main.style.right='500px';
	}
	try{
		if(!courseid)
			courseid=0;
	}catch(ignored){}
	
	try{
		if(!isArray(id))
			id=[id];
		if(!isArray(courseid))
			courseid=[courseid];
	}catch(error){div.innerHTML+="<br>"+error;return;}
	try{
		DwrMethods.getCertificateDialog(id, courseid, type,
			function(data)
			{
				var d = document.createElement("div");
				d.innerHTML=data;
				div.innerHTML="";
				div.appendChild(d);
			}
		);
	}catch(error){div.innerHTML="En feil oppstod: "+error;}
}
function commitCertificate(download, mail)
{
	var form=document.certform;
	form.download.value=download.toString();
	var f=form.formatc;
	var ftot=0;
	for(var i=0; i<f.length;i++)
	{
		if(f[i].checked)
			ftot+=Number(f[i].value);
	}
	form.format.value=ftot;
	form.submit();
}
function enableCertificateMail()
{
	var d = document.getElementById("maildiv");
	var d2=document.getElementById("bottominfo");
	try{d2.style.display='none';}catch(ignored){}
	d.style.display='';

}
function sendCourseCertificateMail()
{
	var form=document.certform;
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	if(!emailPattern.test(form.mail.value))
	{
		alert("Ugyldig e-postadresse\nInvalid e-mail address");
		return;
	}
	var e = form.elements;
	var s = "";
	var f=form.formatc;
	var ftot=0;
	for(var i=0; i<f.length;i++)
	{
		if(f[i].checked)
			ftot+=Number(f[i].value);
	}
	form.format.value=ftot;
	for(var i=0; i<e.length;i++)
	{
		if(e[i].type == "radio")
			if(!e[i].checked)
				continue;
		if(e[i].name=="formatc")
			continue;
		if(e[i].name=="mail")
			continue;
		if(s=="")
			s+="";
		else
			s+="&";
		s+=e[i].name+"="+e[i].value;
	}
	setCertMailActive(1);
	DwrMethods.sendCertificateMail(form.thistype.value, form.mail.value, s,
		function(data)
		{
			setCertMailActive(2);
			setTimeout("resetCertMail()", 5000);
		}
	);
}
function setCertMailActive(id)
{
	var s=[document.getElementById("sendlink"),document.getElementById("sendlinkp"),document.getElementById("sentmail")];
	for(var i=0; i<s.length;i++)
		s[i].style.display='none';
	s[id].style.display='';

}
function resetCertMail()
{
	document.certform.mail.value="";
	setCertMailActive(0);
}
function checkAccessGroupResponsible()
{
	var form=document.userdata;
	if(!form.rettigheter)
		return;
	var show=false;
	if(!form.rettigheter.length)
	{
		var area =Number(form.rettigheter.value);
		if((area==1 || area==2 || area==4 || area==9 || area==10)&&form.rettigheter.checked)
			show =true;
	}
	else
	{
		for(var i=0; i<form.rettigheter.length;i++)
		{
			if(!form.rettigheter[i].checked)
				continue;
			var area =Number(form.rettigheter[i].value);
			if(area==1 || area==2 || area==4 || area==9 || area==10)
			{
				show=true;
				break;
			}
			
		}
	}
	document.getElementById("groupresp").style.display=(show?"":"none");
	setNotifyDisabled(!show);
		
	
		
}
//SPEED TEST EXAMPLE - Load image into hidden container, calculate time and then speed based on preset image size
function speedTest(go)
{
	if(!go)
	{
		setTimeout("speedTest(true)",1000);
		return;
	}
	var div = document.getElementById("speedtest");
	var start = (new Date()).getTime();
	var loc = "http://"+location.hostname+(location.hostname=="localhost"?":8080":"")+"/grafikk/bg-card.jpg?"+Math.random();
	div.style.display='none';
	div.innerHTML="<img src='"+loc+"' onload='calculateSpeed("+start+")'>";
	
}
function calculateSpeed(start)
{
	var size=79872; //BYTES
	size=size/1024;
	var time=(new Date()).getTime()-start;
	time=time/1000;
	
	var speed=Math.ceil(size/time);
	alert("TIME: "+time+" | "+speed+" kb/sec");
}
	

function giveAccess(useid)
{
	var courseid=document.getElementById("givecourseid").value;
	
	if(courseid==-1)
	{
		alert("You must choose a course");
		return;
	}
	
	document.getElementById("givingcourse").style.display='';
	DwrMethods.giveQuickAccessToFreeCourse(Number(useid),Number(courseid),
			function(data)
			{
				document.getElementById("givingcourse").style.display='none';
				if(data=='ok')
					window.location.reload();
				else
					alert(data);
			}
	);
};

function showAdminsCanOrderOnly(checkbox)
{
	//$('#showadminstable').find('tr:gt(0)').find('.canorder').hide();
	$('.cannotorder').toggle(!checkbox.checked);
};
function showAdminsToggleAccess(e, id)
{
	$('.access'+id).css('font-weight', (e.checked?"bold":"normal"));
	$('.access'+id).css('background-color',(e.checked?'yellow':''));
}




