/* $Id: standard.js,v 1.8 2011-10-17 13:25:36 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);
}

/* ##### screen size ##### */
setCookie('UA_width', window.innerWidth);
setCookie('UA_height', window.innerHeight);

/* ##### 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;
}

/* usage: <input type="..." onblur="Calc(this);"> */
function Calc(field) {
	var operators = new RegExp(/\+|\-|\*|\//);
	if (operators.test(field.value)) {
		var txt = field.value.replace(',', '.');
		var val = eval(txt);
		field.value = Math.round(val*100)/100;
	}
	/*var regex = new RegExp("/^\d*\.?\d*$/");
	if (!regex.test(field.value)) {
		field.value = "";
	}*/
}

function Currency(field) {
	Calc(field);
}

/* ##### 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;
}

function selectMove(srcname, dstname) {
	var src=GetObj(srcname);
	var dst=GetObj(dstname);
	var srcLength = src.length;
	var st = new Array();
	var sv = new Array();
	var count = 0;
	
	var i;
	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for (i=srcLength-1; i>=0; i--) {
		if (src.options[i].selected) {
			st[count] = src.options[i].text;
			sv[count] = src.options[i].value;
			if (src.length > 0)
				src.options[i] = null;
			count++;
		}
	}
	
	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for (i=count-1; i>=0; i--) {
		var newopt=new Option(st[i], sv[i]);
		var pos=dst.length;
		dst.options[pos] = newopt;
	}
	//if(NS4) history.go(0);
}

function selectValues(name) {
	var select=GetObj(name);
	var length=select.options.length;
	var result='';
	for(i=0; i<length; i++) {
		result = result + select.options[i].value + ',';
	}
	return result;
}

function selectSubmit(n1, n2, args) {
	var v1=selectValues(n1);
	var v2=selectValues(n2);

	url=location.href;
	document.location=url+args+"&"+n1+"="+v1+"&"+n2+"="+v2;
}

/* ########### 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();
}

