// --------------------- OPEN WINDOW -------------------
// Takes care of opening the documentinfo in the correct browser window
var ResDocPagwWidthDocWindow = 850;
var idWin;
var winProp;
var idWinParam = "channelmode=no,dependent=no,directories=no," +
				 "fullscreen=no,location=no,menubar=no,resizable=yes,scrollbars=yes," +
				 "status=no,toolbar=no";
var idWinParamNoResize =	"channelmode=no,dependent=no,directories=no," +
							"fullscreen=no,location=no,menubar=no,resizable=no,scrollbars=no," +
							"status=no,toolbar=no";


// args: *docid/off/exp
function EKDocPage() {
	var sURL = "docpage.aspx?" + docPageUrlTrail(arguments);
	infoShow(sURL);
}

function EKDocPageFromMyPage() {
	var sURL = "docpage.aspx?exp=IS&" + docPageUrlTrail(arguments);
	infoShow(sURL);
}

function EKDocPageTask() {
	var sURL = "docpage.aspx?start=task&" + docPageUrlTrail(arguments);
	infoShow(sURL);
}

function EKDocPageShowTask(DocId,TaskId) {
	var sURL = "docpage.aspx?did=" + DocId + "&TaskID=" + TaskId;
	infoShow(sURL);
}

function EKResDocPageShowTask(DocId,TaskId) {
	var iWidth	= ResDocPagwWidthDocWindow;
	var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
	var dUrl = "ResDocPageProperties.aspx?rid=" + DocId + "&TaskID=" + TaskId;
	winProp = window.open(dUrl, "NewResultDocument", idWinParam + ",left=" + iLeft + ",width=" + iWidth + ",top=30,height=650");
	winProp.window.focus();
}

function EKTaskPage(TaskID)
{
	var iWidth	= 720;
	var iHeight = 600;
	var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
	var dUrl = "Task.aspx?id=" + TaskID;
	winProp = window.open(dUrl, "EKTask", idWinParam + ",left=" + iLeft + ",width=" + iWidth + ",top=30,height=" + iHeight);
	try
	{
		winProp.window.focus();
	}
	catch(er)
	{}
}

function EKNewTaskPage(dUrl)
{
	var iWidth	= 720;
	var iHeight = 600;
	var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
	//var dUrl = "Task.aspx?id=" + TaskID;
	winProp = window.open(dUrl, "EKTask", idWinParam + ",left=" + iLeft + ",width=" + iWidth + ",top=30,height=" + iHeight);
	winProp.window.focus();
}

function EKResDocPage(id) {
	
	if(window.name == "EKDocPage")
	{
		document.location="resdocpage.aspx?rid=" + id;
		window.resizeTo(400, 400);
	}
	else
	{
		var sURL = "resdocpage.aspx?rid=" + id;
		infoShow(sURL);
	}
}

function EKResDocPageFromMyPage(id) {
	var sURL = "resdocpage.aspx?exp=IS&rid=" + id;
	infoShow(sURL);
}

function EKResDokPageEdit(id){
		var iWidth	= ResDocPagwWidthDocWindow;
		var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
		var dUrl = "ResDocPageProperties.aspx?rid=" + id;
		winProp = window.open(dUrl, "NewResultDocument", idWinParam + ",left=" + iLeft + ",width=" + iWidth + ",top=30,height=650");
		winProp.window.focus();
}

function EKSysTask(id){
		var iWidth	= 600;
		var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
		var dUrl = "SysTasksProperties.aspx?TaskId=" + id;
		winProp = window.open(dUrl, "SysProperties", idWinParam + ",left=" + iLeft + ",width=" + iWidth + ",top=100,height=400");
		winProp.window.focus();
}

function EKResDokPageEditTask(id){
		var iWidth	= ResDocPagwWidthDocWindow;
		var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
		var dUrl = "ResDocPageProperties.aspx?start=task&rid=" + id;
		winProp = window.open(dUrl, "NewResultDocument", idWinParam + ",left=" + iLeft + ",width=" + iWidth + ",top=30,height=650");
		winProp.window.focus();
}

function EKEditResDok(id){;
	var sURL = "EditResDoc.aspx?" + id;
	window.open(sURL, '', 'width=400,height=200,left=300,top=300');
	//winRef.Close()
}

function EKEditDok(id){
	var sURL = "EditResDoc.aspx?" + id + "&type=steering";
	window.open(sURL, '', 'width=400,height=200,left=300,top=300');
	//winRef.Close()
}

function EKOpenDokOffice(id,ext, RequestedDirectory, AppendOpenNewConstraint)
{
	if(navigator.appName == "Microsoft Internet Explorer") 
	{
		var sURL = "EditResDoc.aspx?" + id + "&type=steering&mode=read";
		if(AppendOpenNewConstraint)
			sURL = sURL + "&cfi=1"; //Called from [i]
			
		if(ext)
			if(ext.length > 0)
				sURL = sURL + "&ext=" + ext;
		
		if(RequestedDirectory)
			if(RequestedDirectory.length > 0)
				sURL = sURL + "&dir=" + RequestedDirectory;
				
		window.open(sURL, '', 'width=400,height=200,left=300,top=300');
	}
	else
		EKOpenDoc(id);
	
}

function EKOpenResOffice(id,ext)
{
	
	if(navigator.appName == "Microsoft Internet Explorer") 
	{
		var sURL = "EditResDoc.aspx?" + id + "&type=result&mode=read";
		if(ext)
			if(ext.length > 0)
				sURL = sURL + "&ext=" + ext;		
		window.open(sURL, '', 'width=400,height=200,left=300,top=300');
	}
	else
		EKOpenDoc(id,2);
}


function EKUpdateResDok(id){
	var sURL = "EditResDoc.aspx?" + id + "&update=1";
	window.open(sURL, '', 'width=400,height=200,left=300,top=300');
	//winRef.Close()
}

function NewResFromResDok(resdok){
	var iWidth	= ResDocPagwWidthDocWindow;
	var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
	var dUrl = "ResDocPageProperties.aspx?resdok=" + resdok;
	winProp = window.open(dUrl, "NewResultDocument", idWinParam + ",left=" + iLeft + ",width=" + iWidth + ",top=30,height=650");
	winProp.window.focus();
}

function NewResFromResDokTemplate(resdok)
{
	var iWidth	= ResDocPagwWidthDocWindow;
	var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
	var dUrl = "ResDocPageProperties.aspx?NewFromSameTemplate=" + resdok;
	winProp = window.open(dUrl, "NewResultDocument", idWinParam + ",left=" + iLeft + ",width=" + iWidth + ",top=30,height=650");
	winProp.window.focus();
}

function NewResFromDok(template){
	var iWidth	= ResDocPagwWidthDocWindow;
	var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
	var dUrl = "ResDocPageProperties.aspx?mal=" + template;
	winProp = window.open(dUrl, "NewResultDocument", idWinParam + ",left=" + iLeft + ",width=" + iWidth + ",top=30,height=650");
	winProp.window.focus();
}

function SendMail(id,did,rid){
	var sURL = "SendMail.aspx?user=" + id + "&did=" + did + "&rid=" + rid;
	var iWidth	= 690;
    var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
	window.open(sURL, "", idWinParamNoResize  + ",left=" + iLeft + ",width=" + iWidth + ",top=200,height=450");
}

function EKChangeStatusToWriting(id,template,clas,grading){
	var sURL = "SelectBaseDocumentToWriting.aspx?id=" + id;
	sURL += "&mal=" + template;
	sURL += "&klasse=" + clas;
	if(grading.length>0)
		sURL += "&grad=" + grading;
	ShowWaitInfo();
    
    var iWidth	= 400;
    var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
	idWin = window.open(sURL, "EKDocPage", idWinParam  + ",left=" + iLeft + ",width=" + iWidth + ",top=200,height=700");
	idWin.window.resizeTo(iWidth, 700);
}

function EKReleaseDok(id){
	var sURL = "ReleaseDocument.aspx?rid=" + id;
	var iWidth	= 300;
    var iLeft	= (screen.availWidth - iWidth) / 2;
	window.open(sURL, "", idWinParamNoResize  + ",left=" + iLeft + ",width=" + iWidth + ",top=300,height=60");
}

function EKReleaseDokSteering(id){
	var sURL = "ReleaseDocument.aspx?did=" + id;
	var iWidth	= 300;
    var iLeft	= (screen.availWidth - iWidth) / 2;
	window.open(sURL, "", idWinParamNoResize  + ",left=" + iLeft + ",width=" + iWidth + ",top=300,height=60");
}

function EKReleaseDokNoCopy(id){
	var sURL = "ReleaseDocument.aspx?rid=" + id + "&nocopy=1";
	var iWidth	= 300;
    var iLeft	= (screen.availWidth - iWidth) / 2;
	window.open(sURL, "", idWinParamNoResize  + ",left=" + iLeft + ",width=" + iWidth + ",top=300,height=60");
}

function EKReleaseDokSteeringNoCopy(id){
	var sURL = "ReleaseDocument.aspx?did=" + id + "&nocopy=1";
	var iWidth	= 300;
    var iLeft	= (screen.availWidth - iWidth) / 2;
	window.open(sURL, "", idWinParamNoResize  + ",left=" + iLeft + ",width=" + iWidth + ",top=300,height=60");
}

function EKEksRefPage(id,sid){
	var sURL = "EksRef.aspx?id=" + id + "&sid=" + sid;
	infoShow(sURL);
}

function docPageUrlTrail(p)
{
	var l = p.length;
	
	var sURL = "";		
	if (l > 0) { sURL =	'did='	 + p[0]; }
	if (l > 1) { sURL += '&off=' + p[1]; }
	if (l > 2) { sURL += '&exp=' + p[2]; }
	return sURL;	
}

function infoIsShowing()
{
	var ret = false;
	if (idWin) {
		ret = !(idWin.closed);
	}	
	return ret;
}

function infoShow(dUrl)
{
	ShowWaitInfo();
    
    var iWidth	= 900;
    var iLeft	= (screen.availWidth > iWidth) ? (screen.availWidth - iWidth) / 2 : 0;
	idWin = window.open(dUrl, "EKDocPage", idWinParam  + ",left=" + iLeft + ",width=" + iWidth + ",top=200,height=650");
	idWin.window.resizeTo(iWidth, 650);
	idWin.window.focus();
}

function ShowWaitInfo()
{		
	if (infoIsShowing() == true) {

		// Write out a message to the window, 
		// so that old info is not stuck there!
		idWin.focus();
		var d = idWin.document;
		d.clear();
		d.write("<HTML><TITLE>Henter data...</TITLE>" +
		"<HEAD></HEAD><BODY>Henter data...</BODY></HTML>");
	}
}
// --------------------- /OPEN WINDOW -------------------
