/* $Id: standard.js,v 1.4 2010-07-10 07:35:04 olejakob Exp $ */
/* Javascript */

/* ##### Cookie read/set/delete ##### */
function readCookie(name) {
	var nameEQ=name+"=";
	var ca=document.cookie.split(';');
	for(var i=0; i < ca.length; i++) {
		var c=ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function setCookie(name, value, days) {
	var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*3600*1000));
		var expires = "; expires="+date.toGMTString();
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function rmCookie(name) {
	setCookie(name, "", -1);
}

/* ##### Get/set values and content ##### */
function GetObj( name ) {
	obj=null;
	/* IE */
	if (document.all) {
		obj=document.all(name);
		if (obj!=null) return obj;
	}
	/* Firefox / Opera */
	if (document.getElementById) {
		obj=document.getElementById(name);
		if (obj!=null) return obj;
	}
	if (document.getElementByName) {
		obj=document.getElementByName(name);
		if (obj!=null) return obj;
	}
	alert('Objektet '+name+' ble ikke funnet...');
	return null;	
}

function SetVal( name, val ) {
	obj=GetObj(name);
	obj.value=val;
}

function SetContent(name, value) {
	obj=GetObj(name);
	try {
		obj.innerHtml=value;
	} catch(e) {
	}
}

var CalcCbField = 'calc';
function CalcCb(cb, val, field) {
	if (field==undefined) { 
		field = CalcCbField;
	}
	var calc = GetObj(field);
	var sum  = eval(calc.innerHTML);
	if (cb.checked) {
		sum = eval(sum + val);
	} else {
		sum = eval(sum - val);
	}
	calc.innerHTML = Math.round(sum*100)/100;
}

/* ##### Focus and toggle ##### */
function Focus(name) {
	obj=GetObj(name);
	try {
		obj.focus();
		obj.select();
	} catch(e) {
	}
}

function FocusClean(name, ignore) {
	obj=GetObj(name);
	if (obj.value == ignore)
		obj.value='';
}


function DivToggle( ctrl ) {
//	ctl = eval(ctrl);
	ctl = GetObj(ctrl);
	if (ctl.style.display == "none") {
		ctl.style.display = "";
	} else {
		ctl.style.display = "none";
	}
}

function alter(id) {
	if (document.getElementsByTagName) {
		var table = document.getElementById(id);
		var rows = table.getElementsByTagName("tr");
		for(i=0; i<rows.length; i++) {
			if (i%2 == 0) {
				rows[i].className=rows[i].className+" alter";
			}
		}
	}
}

/* ########### POPUP ############# */
var popup;

function PopupOpen(url, width, height) {
	PopupClose();
	if ((width == null) || (width < 100)) 
		width=readCookie('UA_POPUPW');
	if ((width == null) || (width < 100))
		width=650;

	if ((height == null) || (height < 100)) 
		height=readCookie('UA_POPUPH');
	if ((height == null) || (height < 100))
		height = 450;

	size = 'width='+width+",height="+height;
	var p = window.open(url, 'popup', size+',status=yes,toolbar=no,navigation=no,scrollbars=yes,resizable=yes');
	p.focus();
	popup = p;

	//alert("Windows type: " + typeof popup);
}

function PopupClose() {
	// TODO - fungerer ikke korrekt i Opera
	if ((popup) && (popup != undefined)) {
		//alert('POPUP close: ' + typeof popup);
		popup.close();
	}
}

function WindowClose() {
	if (window.opener != null) {
		window.opener.location.reload();
	}
	window.close();
}

function WindowOpenerReload() {
	if (window.opener != null) {
		url = window.opener.location.href;
		window.opener.location.href = url + '&reload=1';
	}
	window.close();
}

/* ########### FORMS ########### */
function confirmDelete(formname) {
	var hid = GetObj('bDelete');
	if (!hid) {
		alert('bDelete hidden not found..');
		return false;
	}
	
	var agree = confirm('Er du sikker du vil slette denne?');
	if (agree) {
		hid.value = '1';
		window.document.forms[0].submit();
		return true;
	}
	return false;
}

/* ########### URL ############# */
function Goto(url) {
	document.location.href = url;
}

/* ########## Guid ######### */
function S4() {
	return (((1+Math.random())*0x10000)|0).toString(16).substring(1); 	
}
function NewGuid() {
	return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4()).toUpperCase(); 
} 

/* ########## Form for SpellSuppoert ####### */
function SpellAdd(word) {
	document.main.spellword.value = word;
	document.main.spellaction.value = 'add';
	document.main.submit();
}
function SpellFix(word, fix) {
	document.main.spellword.value = word;
	document.main.spellfix.value = fix;
	document.main.spellaction.value = 'fix';
	document.main.submit();
}
