var blnFirstTimeResize = true;

function form_refresh_parent(bClose){
	try {
		if (bClose) {
			if (top.window.opener) top.window.opener.location.reload(true);
			top.window.close();
		} else {
			if (top.window.opener) {
				top.window.opener.location.reload(true);
			} else {
				if (parent.parent.frames['L']) 
					parent.parent.frames['L'].location.reload(true);
			}
		}
	}					
	catch(e){} 
}

function size_c() {
    var c=layer_get("c");
    var intW;

    try {
        if (top.window.opener && blnFirstTimeResize) {
            var intH=Math.min(c.scrollHeight + 120, window.screen.availHeight);
            // kan misgaan als we geen access tot de properties van top.window.opener hebben!
            intW=Math.min(top.window.opener.top.window.document.body.offsetWidth, window.screen.availWidth );
            // HARD-CODED! als een subform op haar beurt weer subforms heeft: deze ruimte vrijmaken
            if (parent.document.all.sets.rows=='*,200') {
                intH += 200;
            }
            blnFirstTimeResize = false;
            top.moveTo( Math.max(0,(window.screen.availWidth-intW)/2),Math.max(0,(window.screen.availHeight-intH)/2));
            top.resizeTo( intW, intH);
        }
    } 
    catch(e)  {      
//        var txt="There was an error on this page.\r\n";
//        txt+="Function: size_c() height \r\n";
//        txt+="Error description: " + e.description + "\r\n";
//        alert(txt);
    }

    try {
        if (c) {
		  c.style.posTop = 25;
	      var intRemainHeight=lib_screen_height()-c.style.posTop+1;
          c.style.posHeight  = Math.max(intRemainHeight,1);
          c.style.posWidth   = Math.max(lib_screen_width()-4,1);
        	
          if (window.frames.length>0) {
	        var sp=(c.offsetHeight-c.scrollHeight)/window.frames.length;
	        if (sp>0){
		        for(f=0;f<window.frames.length;f++){
			        with(window.frames[f].frameElement.style){
				        posHeight=posHeight+sp;
			        }
		        }
	        }
          } else {
			if (c.offsetHeight-c.scrollHeight>0) {
				if (parent.document.all.sets.rows=='*,200') {
					// bit of a hack, but it works
				  parent.document.all.sets.rows='*,' + (200 + c.offsetHeight-c.scrollHeight - 8).toString();
				}
			}
		  }
        }
   }
   catch (e) {
//      var txt="There was an error on this page.\r\n";
//      txt+="Function: size_c() width \r\n";
//      txt+="Error description: " + e.description + "\r\n";
//      alert(txt);
   }
}

function w(sForm, subID, sParent, sParentID){
    var sUrl="d.asp?formID=" + sForm + "&ID=" + subID + "&parentID=" + sParentID + "&parentField=" + sParent;
    var x=window.open(sUrl,'sub_form_details_'+sForm,'toolbar=0,location=0,resizable=1,status=0,scrollbars=1,height=0,width=0,top=0,left=0');x.opener=window;x.focus();
}

function fChangeImage(sControl,sPath){ 
    var args = new Array();
    var arr  = null;
    var sValue=''+window.document.main[sControl].value;
    	
    // Display image/file wizard
    args["descr"]='Selecteer afbeelding';
    args["content"]='wizards/file-pages.asp?image=1&file='+sValue+'&basepath='+sPath;
    args["is_image"]=true;
    args["resizetype"]=window.document.main[sControl+"_resizetype"].value;
    args["resizeheight"]=window.document.main[sControl+"_resizeheight"].value;
    args["resizewidth"]=window.document.main[sControl+"_resizewidth"].value;
    args["random"]=window.document.main[sControl+"_random"].value;
    args["include_layout"]=false;
    args["scalable"]=true;

    arr = ShowWizard(args);
    if (arr) {
	    fSetImage(sControl, sPath, arr["filename"], arr["width"], arr["height"]);
    }
}

function fChangeImageWizard(sControl,sPath){ 
    var blnModal = true;
    var args = new Array();
    var arr  = null;
	var scr_width = window.screen.availWidth-200;
	var scr_height = window.screen.availHeight-200;

    args["mode"]   = parseInt(window.document.main[sControl+"_resizetype"].value)==2 ? 1 : 2 ;
    args["height"] = window.document.main[sControl+"_resizeheight"].value;
    args["width"]  = window.document.main[sControl+"_resizewidth"].value;
    args["random"] = window.document.main[sControl+"_random"].value;
    args["path"]   = sPath; 

    // 1 is mode_fixed: 2 = maximum	(the first 2 is the internal ASP define from imgtypes.inc!)
    if (blnModal ) {
	    arr = showModalDialog( "imageupload.asp",
						       args,
							   "dialogWidth:"+scr_width.toString()+"px;dialogHeight:"+scr_height.toString()+"px;status:no;resize:yes;help:no;scroll:no");
	    if (arr) {
		    fSetImage(sControl, sPath, arr["filename"],arr["width"],arr["height"]);
	    } 
    } else {
	    var sUrl = 'imageupload.asp?mode='+args["mode"].toString()+'&height='+args["height"]+'&width='+args["width"]+'&random='+args["random"]+'&path='+sPath+'&control='+sControl;
	    var w = screen.availWidth;
	    var h = screen.availHeight;
	    var popW = scr_width, popH = scr_height;
	    var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	    var x=window.open(sUrl,'popup_image_wizard','resizable=yes,scrollbars=no,toolbar=no,status=no,location=no,menubar=no,width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos);
	    if (x) {x.opener=window;x.focus()}
	    // image is set inside the called window
    }
}

function fClearImage(sControl,sSrc){
    var oMain = window.document.main;

    // update velden in formulier (bestandsnaam is nu altijd zonder vaste pad!) 
    oMain[sControl].value='';
    oMain[sControl+'_width'].value='';
    oMain[sControl+'_height'].value='';
    oMain[sControl+'_preview'].src=sSrc;
    oMain[sControl+'_preview'].style.height=40;
    oMain[sControl+'_preview'].style.width =40;
}

function fViewFile(sControl,sSrc){
    var oMain = window.document.main;
    if (oMain[sControl].value=="") {
        alert ("Selecteer of plaats eerst een bestand.")
    } else {
        lib_OpenWindowCentered( sSrc + oMain[sControl].value, "view", 800, 600)
    }
}

function fSetImage(sControl,sPath,filename,width,height){
    var oMain = window.document.main, oPreview

    // update velden in formulier (bestandsnaam is nu altijd zonder vaste pad!) 
    oMain[sControl].value = filename; 
    oMain[sControl+'_width'].value  = ''+width;
    oMain[sControl+'_height'].value = ''+height;

    // update preview
    oPreview = oMain[sControl+'_preview'];
    if (oPreview) {
	    if (width>height){
		    oPreview.width =40;
		    oPreview.height=Math.round(height*40/width,0);
	    } else {
		    oPreview.width =Math.round(width*40/height,0);
		    oPreview.height=40;
	    }
	    oPreview.src = sPath + filename;
    }
}

function fChangeFile(sControl,sPath){
    var args = new Array();
    var arr  = null;
    var sValue=''+window.document.main[sControl].value;
    	
    // Display image/file wizard
    args["descr"]			= 'Selecteer een bestand';
    args["content"]			= 'wizards/file-pages.asp?file='+sValue+'&basepath='+sPath;
    args["is_image"]		= false;
    args["include_layout"]	= false;
    args["random"]			= window.document.main[sControl+"_random"].value;
    args["scalable"]		= true;

    arr = ShowWizard( args );
    if (arr) window.document.main[sControl].value=arr["filename"];
}

function fPreviewImage(sControl,event){
    var iWidth =parseInt(window.document.main[sControl+'_width'].value, 10);
    var iHeight=parseInt(window.document.main[sControl+'_height'].value, 10);
    if(window.document.main[sControl].value!=''){
	    iWidth+=((iWidth==0)?300:6);
	    iHeight+=((iHeight==0)?300:40);
	    sTop=Math.min(event.screenY-event.offsetY,screen.height-iHeight).toString(); 
	    sLeft=Math.min(event.screenX-event.offsetX,screen.width-iWidth).toString();
	    window.showModalDialog('showimage.asp?img='+escape(window.document.main[sControl+'_preview'].src),null,'dialogWidth:'+iWidth+'px;dialogHeight:'+iHeight+'px;dialogTop:'+sTop+'px;dialogLeft:'+sLeft+'px;status:no;help:no');
	    return(true);
    }else{
	    alert ("Selecteer of plaats eerst een afbeelding.")
	    return(false);
    }
}

function fClearFile(sControl){window.document.main[sControl].value='';}

function fShowSite(fld){
    var strUrl=fld.value;
    if(strUrl==""){
	    alert("Geef eerst het internet-adres aan!")
    }else{
	    if(strUrl.substring(0,3)=='www')fld.value='http://'+strUrl;
	    var x=window.open(fld.value,'zoom','toolbar=0,location=0,resizable=1,status=0,scrollbars=1');
  	    if (x.focus) x.focus();
    }
}
function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name, value) {
    var expDays=365;
    var expDate=new Date();
    expDate.setTime(expDate.getTime() +  (24 * 60 * 60 * 1000 * expDays)); 
    document.cookie = name + "=" +escape(value) + ";expires=" + expDate.toGMTString();
}

function grp_set( id, display_value){
    // each row has been given an ID 
    var row = 1;
    var new_src;
    var img;
    var sID = id.toString();
    var elt = document.getElementById ("_g" + sID + '_' + row.toString() );
    while (elt) {
	    if (elt.style.display!=display_value) {
		    elt.style.display = display_value;
	    }
	    row++;
	    elt = document.getElementById ("_g" + sID + '_' + row.toString() );
    }	
    if (last_elt=document.getElementById ("_g"+sID+"_last")) {last_elt.className='groupbox_close_'+(display_value=='none'?'in':'')+'active';}
    if (img=document.getElementById('_gimg_'+sID)) img.src = "images/group_" + (display_value=='none'?'open':'close')+'.gif';
    if (img=document.getElementById('_ggrip_'+sID)) img.src = "images/group_grip_" + (display_value=='none'?'gray':'blue')+'.gif';
    if (title_elt=document.getElementById ("_gtitle_" + sID)) {title_elt.className='groupbox_'+(display_value=='none'?'in':'')+'active';}
}

function grp_flip( id, form_id){
    var cookie_name = 'frm_' + form_id.toString() + "_" + id.toString();
    cookie_val = Get_Cookie( cookie_name )=='none' ? 'block' : 'none';
    grp_set( id, cookie_val);
    Set_Cookie( cookie_name, cookie_val);
}

function grp_init(form_id){
    var id = 1
    var elt = document.getElementById ("_g" + id.toString() + '_1');
    while (elt) {
	    var cookie_name = 'frm_' + form_id.toString() + "_" + id.toString();
	    // hide elements that need to be hidden
	    if (Get_Cookie(cookie_name)=='none') grp_set(id, 'none');
	    id ++;
	    var elt = document.getElementById ("_g" + id.toString() + '_1');
    }
}

function lb_sortup( listbox ) {    if (listbox.selectedIndex!=-1) {
	    if (listbox.selectedIndex==0) {	    } else {
		    lb_swapitem( listbox, listbox.selectedIndex, listbox.selectedIndex-1)		    listbox.selectedIndex--;	    }
    }
}
function lb_sortdown( listbox ) {    if (listbox.selectedIndex!=-1) {
	    if (listbox.selectedIndex==(listbox.options.length-1)) {	    } else {		    lb_swapitem( listbox, listbox.selectedIndex, listbox.selectedIndex+1)
		    listbox.selectedIndex++;	    }    }}

function lb_key() {
    if (event.ctrlKey || event.altKey || event.shiftLeft || event.shiftRight) {
	    if (event.keyCode==40) {
            lb_sortdown( event.srcElement )
		    return false;   //  skip standaard handler
	    }
	    if (event.keyCode==38) {
		    lb_sortup( event.srcElement )
		    return false;   //  skip standaard handler
	    }
    }
}

function lb_sort(listbox,is_desc) {
	var listlen = listbox.length;
	var options = listbox.options;

	if(listlen < 1) return;

	for(x=listlen-1;x>0;x--) {
		for(y=0;y<x;y++) {
			try {
				var swap = false;

				if(is_desc) {
					if(options[y].text < options[y+1].text)
						swap = true;
				}
				else {
					if(options[y].text > options[y+1].text)
						swap = true;
				}

				if(swap) {
					lb_swapitem(listbox,y,y+1);
				}

			} catch(e) {
//				alert(e + " x: " + x + " y: " + y);
			}
		}
	}
}

function lb_swapitem( listbox, item1, item2) {    with (listbox) {
	    var txt = options[item2].text;	    var val = options[item2].value;	    options[item2].value = options[item1].value;
	    options[item2].text  = options[item1].text;
	    options[item1].value = val;
	    options[item1].text  = txt;
    }
	lb_fill_info( listbox );}

function lb_fill_info( listbox ) {
	with (listbox) {
	    var info = '';	
	    for (var i=0; i< options.length; i++) {
	        info = info + (info=='' ? '' : ',') + options[i].value;
	    }
	    lib_form_findfield(listbox.name + '_info').value = info;
	}
}
