function selPic(fieldname, pic) {
	var field = eval("document.all." + fieldname);
	var newVal = showModalDialog("selectpic.asp", pic , "resizable: no; help: no; status: no; scroll: no;");
	
	if (newVal != null) {
		field.value = newVal;
		eval("document.pic").src=newVal;
	}
}

function getExtSel(fieldname, dsid, fieldnum, color) {
	var field = eval("document.all." + fieldname);
	var spanFld = eval("document.all.span" + fieldname);
	var strData = field.value;
	
	var newVal = showModalDialog("selectselval.asp?id=" + field.value + "&dsid=" + dsid, "" , "resizable: no; help: no; status: no; scroll: no;");
	
	if (newVal != null) {
		var arrVals = new Array();
		arrVals = newVal.split("|");
		field.value = arrVals[0];
		spanFld.innerHTML = arrVals[1];
		//if (fieldnum != '') {
			SetFieldUpdFlag(fieldnum, color);
		//}
	}
}

function selectDate(fieldname, rootfolder) {
	var fieldDay = eval("document.all." + fieldname + "Day");
	var fieldMonth = eval("document.all." + fieldname + "Month");
	var fieldYear = eval("document.all." + fieldname + "Year");
	var strDate = fieldDay.value + "|" + fieldMonth.value + "|" + fieldYear.value;
	var newDate = window.showModalDialog(rootfolder + "/admin/selectdate.asp?date=" + strDate , "" , "resizable: yes; help: no; status: no; scroll: no;");
	
	if (newDate != null) {
		var arrArgs = new Array();
		arrArgs = newDate.split("@");
		var arrDateSerial = new Array();
		arrDateSerial = arrArgs[0].split("|");
		//arrDateSerial = newDate.split("|");
		
		var iDy = arrDateSerial[0];
		var iMt = arrDateSerial[1];
		var iYr = arrDateSerial[2];

		fieldDay.value = iDy;
		fieldMonth.value = iMt;
		fieldYear.value = iYr;
		
		if(arrArgs[1] == "reopen") {
			selectDate(fieldname, rootfolder);
		}
	}
}

function checkNum(fld) {
	var oldVal = parseFloat((eval(fld).value).replace(",","."));
	var newVal = 0
	if (oldVal.toString() != "NaN") {newVal = oldVal;}
	if(newVal == "") { newVal = 0; }
	eval(fld).value=newVal;
} //function

function setAllDelCheck(tablePart, strColorDel, strColor1, strColor2) {
	var bolChkAll = document.all.chkAll.checked;
	//alert(bolChkAll);
	var strColor;

	if(bolChkAll) {
			strValue = "false";
			strUpd = "0";
	}
	else {
			strValue = "true";
			strUpd = "1";
			strColor = strColorDel;
	}
			
	tempChk = document.all.tags("INPUT");
	//alert(tempChk.length);

	for (i = 0; i < tempChk.length; i++){
		if (tempChk(i).type == "checkbox") {
			 if (tempChk(i).name.indexOf("chkDel") > -1) {
				 	var num = tempChk(i).name.substring(6,9);
					//alert(num);
				 
			 		var strCheckBox = eval(tempChk(i));
					strCheckBox.checked = eval(strValue);
					//alert("jess");

					if(bolChkAll) {
						if((i % 2) == 0) {
							strColor = strColor2;
						}
						else {
							strColor = strColor1;
						}
					}
					//alert(tablePart);
					eval(tablePart + num).bgColor = strColor;
					eval("document.all.bolUpdate" + num).value = strUpd;
			 }//if
		}//if
	}//for
	//alert(strValue);
	
//	document.all.chkAll.checked = true;
}//function

function invertDelCheck(strColor, tablePart) {
	var colorTemp = strColor;
	tempChk = document.all.tags("INPUT");
	//alert(tempChk.length);

	for (i = 0; i < tempChk.length; i++){
		if (tempChk(i).type == "checkbox") {
			if (tempChk(i).name.indexOf("chkDel") > -1) {
				var num = tempChk(i).name.substring(6,9);
				var strCheckBox = eval(tempChk(i));

				if(strCheckBox.checked) {
					eval(tempChk(i)).checked = false;
					if(tablePart == "TD") { colorTemp = "white"; }
					else { colorTemp = strColor;}
			
					eval(tablePart + num).bgColor = colorTemp;
					eval("document.all.bolUpdate" + num).value = "0";
				}
				else {
					if(tablePart == "TD") { colorTemp = strColor; }
					else { colorTemp = "white";}
					eval(tempChk(i)).checked = true;
					eval(tablePart + num).bgColor = colorTemp;
					eval("document.all.bolUpdate" + num).value = "1";
				}
			 }//if
		}//if
	}//for
}//function

function setExprFieldValues(tablename, numLines, fldName, val) {
	var i;
	
	for(i = 1 ; i < numLines + 1 ; i++) {
		if(val=="true" || val=="false") {
			eval("document." + tablename + "." + fldName + i).checked = eval(val);
		}
		else {
			eval("document." + tablename + "." + fldName + i).value = val;
		}
	}
}

function SetFieldUpdFlag(num, Color)
{
	var strField = eval("document.all.bolUpdate" + num);
	var strTablePart = eval("document.all.TR" + num);
	var strBGColor = Color;

	strTablePart.bgColor = strBGColor;
	strField.value = "1";
}

function SetRowColor(lineNum, bgcolor, updcolor, delcolor) {
	var bolField = eval("document.all.bolUpdate" + lineNum);
	var chkField = eval("document.all.chkDel" + lineNum);
	var TR = eval("document.all.TR" + lineNum);
	
	if(bolField.value=="1") {
		if(lineNum > 0 && chkField != null) {
			if (chkField.checked) {
				TR.bgColor=delcolor; 
			}
			else {
				TR.bgColor=updcolor; 
			}
		}
		else {
			TR.bgColor=updcolor; 
		}
	}
	else {
		TR.bgColor=bgcolor;
	}
}

function CheckForm(Form, strReqFields){
	window.onUnload = "";
	var strField;
	var bolField;
	var ReqFields = strReqFields.split("|");
	var bolSubmitForm = true;
	var bolDeletions = false;
	var bolDeleteRecord = false;
	var bolDeletionsActivated = false;
	var bolHasChanges = false;

	strForm = eval("document." + Form);
	
	var intNumReqFields = ReqFields.length;
	var txtFields = "";
	var NumMissingFields = 0;
	var J = 0;
	var intStart = 0;
	
	if(ReqFields[0].indexOf("1") > -1){
		//alert("Yes");
		strFirstField = ReqFields[0].replace("1","");
		J = J + 1;
		intStart = 1;
	}//if
	else {
		strFirstField = ReqFields[0].replace("0","");
	} //else

	var intLastBolUpd = 0;
	var intLenReqFields = strReqFields.length;
	var iLBU = 8;
	for ( ; iLBU > 0 ; iLBU--) {
		//alert(intLenReqFields + " - " + iLBU);
		//alert(strReqFields.substr(intLenReqFields - iLBU));
		intLastBolUpd = parseInt(strReqFields.substr(intLenReqFields - iLBU));
		//alert(intLastBolUpd);
		
		if (intLastBolUpd.toString() != "NaN" && intLastBolUpd > 0) { 
			//alert("Found it!! " + strReqFields.substr(intLenReqFields - iLBU));
			iLBU = 0; 
		}
	}
	
	//alert(strFirstField);
	//alert(intNumReqFields);
	//alert(intStart);
	
	if(ReqFields[0] != "") {
		//alert(ReqFields[0]);
		for(I = intStart ; I < intNumReqFields ; I ++){
			strField = eval("document." + Form + "." + ReqFields[I]);
			//alert("document." + Form + "." + ReqFields[I]);
			bolField = eval("document." + Form + ".bolUpdate" + J);
			var strFieldName = strField.name;
			//alert(strFieldName + " - " + strField.value);
			//alert(bolField.name + " - " + bolField.value);
			//alert(strFieldName + " = " + strFirstField + J);
			
			if(strReqFields.indexOf(strFirstField + intStart) > -1){
				//alert(strReqFields.indexOf(strFirstField + intStart));
				if(strFieldName == strFirstField + (J + 1)){ 
					J++;
					//alert("Ny Linje!!! bolUpdate" + J);
					//alert(eval("document." + Form + ".bolUpdate" + J) + " - " + "document." + Form + ".bolUpdate" + J);
				}
				//alert(J + " - " + intLastBolUpd + " / " + eval("document." + Form + ".bolUpdate" + J).value);

				if(J <= intLastBolUpd && eval("document." + Form + ".bolUpdate" + J).value == "1") {
					//alert(eval("document." + Form + ".bolUpdate" + J).value);
					if(J > 0) {
						//alert("we try to update: " + J);
						//don't run this loop if deletions are activated
						//alert(bolDeletionsActivated);
						if(!bolDeletionsActivated) {
							//check first if deletions are activated
							tempChk = document.all.tags("INPUT");
						
							for (i = 0; i < tempChk.length; i++){
								if (tempChk(i).type == "checkbox" && !bolDeletionsActivated) {
									 if (tempChk(i).name.indexOf("chkDel") > -1) {
										 //alert("Vi har et chkDelfelt! " + i);
										 bolDeletionsActivated = true;
									 }//if
								}//if
							}//for
						} //if
						if (bolDeletionsActivated) {
							//alert("document." + Form + ".chkDel" + J);
							if(eval("document." + Form + ".chkDel" + J).checked) { 
								//alert("we have deletions");
								bolDeletions = true;
								bolDeleteRecord = true;
								bolHasChanges = true;
							} //if
							else { bolDeleteRecord = false; }
						}
					}//if
				
					if (!bolDeleteRecord) {
						bolField = eval("document.all.bolUpdate" + J);
						
						if(bolField.value == "1"){
							//alert(strField.name + " - " + strField.value);
							if(strField.value == ""){
								NumMissingFields += 1;

								txtFieldName = ReqFields[I];
								txtFields = txtFields + "'" + txtFieldName + "'|"
								bolSubmitForm = false;
							}//if
							else {bolHasChanges = true;}
						}//if
					} //if
				}//if
			} // if
			else {
				//alert("has changes");
				bolHasChanges = true;
				if(strField.value == ""){
					NumMissingFields += 1;
					txtFieldName = ReqFields[I];
					txtFields = txtFields + "'" + txtFieldName + "'|"
					bolSubmitForm = false;
				}//if
			}//else
		}//for
	} //if
	else {
		bolSubmitForm = true;
		bolHasChanges = true;
	}
	
	//alert("Submit: " + bolSubmitForm + "  Endret: " + bolHasChanges);
	
	if(bolSubmitForm == true){
		if(bolDeletions) {
			if(confirm("Vil du slette de valgte postene?")) {
				strForm.submit( );
			}
		}
		else { 
			if (bolHasChanges) {
				//alert("Endringer finnes! " + bolHasChanges);
				strForm.submit( ); 
			}
		}
	}
	else{
		if (NumMissingFields > 1)	{
			var NumArrFields;
			var arrFields;
			var txtPrintFields = "";
			var strErrorMsg = "";
			
			arrFields = txtFields.substring(0, (txtFields.length - 1)).split("|");
			NumArrFields = arrFields.length;
			
			for (K = 0 ; K < NumArrFields ; K ++){	
				if (K == (NumArrFields - 1)){
					txtPrintFields = txtPrintFields.substring(0, txtPrintFields.length - 2) + " og " + arrFields[K];
				}//if
				else{
					txtPrintFields = txtPrintFields + arrFields[K] + ", " 
				}//else
			}//for
			alert("Du utelot noen påkrevde felter; feltene  " + txtPrintFields + " er tomme!\n\nVennligst fyll inn informasjon i feltene og prøv på nytt!");
		}//if
		else{
			txtFields = txtFields.substring(0, txtFields.length - 1);
			alert("Du utelot et påkrevd felt; feltet " + txtFields + " er tomt!\n\nVennligst fyll inn informasjon i feltet og prøv på nytt!");
		}//else
	} //else
} // function

function popup(popupfile,winheight,winwidth)
{
open(popupfile,"PopupWindow","resizable=no,height=" + winheight + ",width=" + winwidth + ",scrollbars=no");
}

function updateHTM(Form){
	tempTxtHidden = document.all.tags("INPUT");

	for (i = 0; i < tempTxtHidden.length; i++){
		if (tempTxtHidden(i).type == "hidden") {
			if (tempTxtHidden(i).name.indexOf("hid") > -1) {
				var HiddenFldName = tempTxtHidden(i).name;
				var HTMFldName = tempTxtHidden(i).name.replace("hid", "");
				
				var HiddenFld = eval("document.all." + HiddenFldName);
				//var HTMFld = eval("document.all." + HTMFldName);
				
				//alert(HiddenFld.value + " | " + HTMFld.value);
				HiddenFld.value = editor_getHTML(HTMFldName);
			 }//if
		}//if
	}//for
} //function

