function setValueIfDefault(object, defaultVal, newVal) {
	if (object.value == defaultVal) {
		object.value = newVal
	}
}

function setError(fieldName, errText) {
	//document.getElementById(fieldName + 'Err').innerText = errText
	document.getElementById(fieldName + 'Err').innerHTML = errText;	
	if (errText) document.getElementById(fieldName).focus()
}

function clearError(fieldName) {
	document.getElementById(fieldName + 'Err').innerText = ''
}

function PopUp(url, width, scrollbars, height) {
	if (!height) height = 1;
	if (!width) width = 550;
	if (!scrollbars) {
		strScrollbars = "no";
	}	else {
		strScrollbars = "yes";
	}
	window.open(url,'','top=0,left=100,width=' + width + ',height=' + height + ',scrollbars=' + strScrollbars + ',resizable=yes,status=no')
}

function iif(bolExpression, varTruePart, varFalsePart) {
	if (bolExpression) 
 		return varTruePart 
	else
 		return varFalsePart
}

function strToDate(val) // forventer at dato strengen er i formatet DD-MM-YYYY
{ 
	var arrDate = val.split('-')
    return new Date(parseInt(arrDate[2],10), parseInt(arrDate[1],10)-1, parseInt(arrDate[0],10))
}

function trim(str) {
	return str.replace(/ /g,"").replace(/\n/g,"")
}

function isChecked(docObj) {
    returnThis = false;
    docObject = document.getElementsByName(docObj);
       
    for (i=0; i<docObject.length; i++){
        if (docObject[i].checked==true) 
        returnThis = true;
        }
    return returnThis;
}

function getCheckedItems(docObject) {
    returnThis = "";
    docObject = document.getElementsByName("id");
       
    for (i=0; i<docObject.length; i++){
        if (docObject[i].checked==true) {
            
            if (returnThis == "") {
                returnThis = docObject[i].value
                } else {
                returnThis = returnThis + ',' + docObject[i].value
                }
            }
        }
    return returnThis;
}
    
function ClickAll(inpName,checkAll) {
	inpElem=document.getElementsByName(inpName);
	inpSwitch=document.getElementsByName(checkAll);
	if (inpElem) {
		AllSel=inpSwitch[0].checked;
		if (AllSel == false) 
		//AllSel = true;
		AllSel = false;
		else
		//AllSel = false;
		AllSel = true;
			
		for (var i=0;i<inpElem.length;i++) {
		
			inpElem[i].checked=AllSel;
		}
	}
}

function cbClickAll(inpName) {
	inpElem=document.getElementsByName(inpName);
	if (inpElem) {
		AllSel=inpElem[0].checked
		for (var i=1;i<inpElem.length;i++) {
			inpElem[i].checked=AllSel;
		}
	}
}

function cbClickOne(inpName) {
	inpElem=document.getElementsByName(inpName);
	var AllSel = true
	if (inpElem) {
		for (var i=1;i<inpElem.length;i++) {
			AllSel = AllSel && inpElem[i].checked;
		}
		inpElem[0].checked = AllSel
	}
}

function trimField(fieldName, size) 
{
	var field = document.getElementById(fieldName)
	if (field.value.length > size)
	{
		field.value = field.value.substr(0, size)
	}
}	
/* ----- RESIZE POPUPS ----- */

function bidWidth()																// BrowserInDependantWidth
{
	if (self.innerHeight)						
		return self.innerWidth;												// all except Explorer
	else if (document.documentElement && document.documentElement.clientHeight) 
		return document.documentElement.clientWidth;	// Explorer 6 Strict Mode
	else if (document.body) 									
		return document.body.clientWidth;							// other Explorers
}

function bidHeight()															// BrowserInDependantHeight
{
	if (self.innerHeight) 
		return self.innerHeight;											// all except Explorer
	else if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;	// Explorer 6 Strict Mode
	else if (document.body)
		return document.body.clientHeight;						// other Explorers
}

function FitWindowHoriz()
{
	window.resizeBy(50 - bidWidth(), 0);
	window.resizeBy(document.body.scrollWidth - bidWidth(), 0);
}

function FitWindowVert(AddY)
{
	if (isNaN(AddY)) AddY = 0;
	window.resizeBy(0, 400 - bidHeight());
	y = Math.min(document.body.scrollHeight + AddY, screen.availHeight - 90) - bidHeight();
	window.resizeBy(0, y);
}

function FitWindow()
{
	FitWindowHoriz();
	FitWindowVert();
}
