var callbacks=new Array();
/*
    label: modal window unique label
    callback: JS function which will get updated values
    url: URL of script
    width, height: window dimension (not counting window gray frame and close icon)
*/
function open_modal_iframe(label,callback,url,width,height) {
    var wnd=window;
    while (wnd.frames.parent.document.getElementById(this.window.name)&&wnd.frames.parent.document.getElementById(this.window.name).modal_window_label) {
        wnd=wnd.frames.parent;
    }
    wnd.real_open_modal_iframe(label,callback,url,width,height);
}
function real_open_modal_iframe(label,callback,url,width,height) {
    if (!width) {
        width='90%';
    }
    if (!height) {
        height='90%';
    }
    if (width==width+0) {
        width=width+'px';
    } else {
        width=width.substring(0,width.length-1);
        width=Math.floor(width*document.documentElement.clientWidth/100)+'px';
    }
    if (height==height+0) {
        height=height+'px';
    } else {
        height=height.substring(0,height.length-1);
        height=Math.floor(height*document.documentElement.clientHeight/100)+'px';
    }

    var mask=document.createElement('div');
    mask.id='modal_iframe_mask_'+label;
    mask.style.position='absolute';
    mask.style.zIndex=100+callbacks.length;
    mask.style.left='0px';
    mask.style.top='0px';
    mask.style.width=document.documentElement.clientWidth+'px';
    mask.style.height=document.documentElement.scrollHeight+'px';
    mask.style.display='block';
    mask.style.background='url(images/transparency.gif)';
    var frame=document.createElement('div');
    frame.id='modal_iframe_frame_'+label;
    frame.style.position='absolute';
    frame.style.left='0px';
    frame.style.top=document.documentElement.scrollTop+'px';
    frame.style.width=document.documentElement.clientWidth+'px';
    frame.style.height=document.documentElement.clientHeight+'px';
    frame.innerHTML='<table align="center" height="100%" border="0"><tr><td align="left">\
            <div id="modal_iframe_container_'+label+'">\
                <div style="background:#CCCCCC;padding:3px;width:'+width+';height:20px" align="right"><a href="javascript:real_close_modal_iframe(\''+label+'\')"><img src="images/i_close.gif" width="20" height="20" alt="Click to close" border="0" class="block"></a></div>\
                <div style="width:'+width+';padding:3px;height:22px;background:#FFFFFF" align="center" id="modal_iframe_loading_'+label+'" class="backendText">Loading...</div>\
                <div id="modal_around_cell_'+label+'" style="width:1px;height:1px;overflow:hidden"><iframe onload="set_modal_iframe_on(\''+label+'\',\''+width+'\',\''+height+'\')" src="'+url+'" frameborder="0" marginheight="0" marginwidth="0" scrolling="auto" hspace="0" vspace="0" id="modal_iframe_cell_'+label+'" name="modal_iframe_cell_'+label+'" style="padding:0px;width:100%;height:'+height+';overflow-x:auto;overflow-y:scroll">\
                   \
                </iframe></div>\
            </div>\
        </td></tr></table>\
';
    mask.appendChild(frame);
    
    document.body.appendChild(mask);
    $('modal_iframe_cell_'+label).modal_window_label=label;
    
    callbacks[label]=callback;
}
function set_modal_iframe_on(label,width,height) {
    $('modal_iframe_loading_'+label).style.display='none';
    $('modal_iframe_cell_'+label).style.display='block';
    $('modal_around_cell_'+label).style.width=width;
    $('modal_around_cell_'+label).style.height=height;
    $('modal_around_cell_'+label).style.background='#FFFFFF';
    $('modal_around_cell_'+label).style.border='3px solid #CCCCCC';
}
function close_modal_iframe(wnd) {
    var label=wnd.frames.parent.document.getElementById(wnd.name).modal_window_label;
    if (callbacks[label]) {
        // pass updated values
        callbacks[label](false);
    }
    real_close_modal_iframe(label);
}
function real_close_modal_iframe(label) {
//    $('modal_iframe_container_'+label).removeChild($('modal_iframe_cell_'+label));
//    $('modal_iframe_frame_'+label).removeChild($('modal_iframe_container_'+label));
    $('modal_iframe_mask_'+label).removeChild($('modal_iframe_frame_'+label));
    document.body.removeChild($('modal_iframe_mask_'+label));
}
