//==============================================================================
// File     : app.js
// Created  : 2008-01-09 Chris Wirth
// Modified : 2008-06-20 Chris Wirth
// Content  : Application specific js
//==============================================================================
var gD = new Object();
gD.fHideFormElementsCache = null;
gD.fHideFormElementsRestoreStates = null;
gD.showHideSelects = function(state,restoreStates) {
	var i, f, e, fe;
	var currentStates = new Array();
	if (gB.IE) {
		if (this.fHideFormElementsCache == null) {
			this.fHideFormElementsCache = new Array();
			for (f=0; f<document.forms.length; f++) {
				for (e=0; e<document.forms[f].elements.length; e++) {
					fe = document.forms[f].elements[e];
					if (fe.type != null) {
						if (fe.type.indexOf("select")==0) {
							this.fHideFormElementsCache[this.fHideFormElementsCache.length] = fe;
						} /* if */
					} /* if */
				} /* for */
			} /* for */
		} /* if */
		for (i=0;i<this.fHideFormElementsCache.length;i++) {
			fe = this.fHideFormElementsCache[i];
			currentStates[i] = fe.style.visibility;
			if (restoreStates != null && i < restoreStates.length) {
				fe.style.visibility  = restoreStates[i];
			} else {
				fe.style.visibility = state;
			} /* if */
		} /* for */
	} /* if */
	return(currentStates);
} /* showHideSelects */

function getByIdOrName(ident,onlyOne) {
	var obj = document.getElementById(ident);
	if (obj == null) {
		obj = document.getElementsByName(ident);
		if (onlyOne && obj.length > 0) {
			obj = obj[0];
		}
	}
	return obj;
}

function isChildOf(parentId, element) {
   while(element != null) {        
       if (element.id && element.id.indexOf(parentId) > -1) {                               
           return true;
       }
       element = element.parentNode;
   }
   return false;
}
function getWindowSize() {
    var innerW, innerH;
    if (document.body.clientWidth != null) {
        innerW = document.body.clientWidth;
    } else {
        innerW = window.innerWidth;
    }
    if (document.body.clientHeight != null) {
        innerH = document.body.clientHeight;
    } else {
        innerH = window.innerHeight;
    }
    return [parseInt(innerW),parseInt(innerH)];
}

function dialogs_Open(dialogID,strAnchorL,strAnchorT,strAnchorR,strAnchorB,intX_L,intY_T,intW_R,intH_B,dx,dy,doAdapt,doClose,doHideFormElements) {
    var dlg = new Object();
    dlg.fLayer = null;
    dlg.fLayer_bw = null;
    dlg.fLayer_bh = null;
    dlg.fAnchor_l = strAnchorL;
    dlg.fAnchor_t = strAnchorT;
    dlg.fAnchor_r = strAnchorR;
    dlg.fAnchor_b = strAnchorB;
    dlg.fAdapt = doAdapt;
	dlg.fAnchor_dx = dx;
	dlg.fAnchor_dy = dy;
	dlg.fClose = doClose;
	dlg.fHideFormElements = doHideFormElements;

	if (dlg.fAnchor_l != null && intX_L != null) {
		var l = getImgArea(dlg.fAnchor_l);
		intX_L = l[0]+intX_L;
	}
	if (dlg.fAnchor_t != null && intY_T != null) {
		var t = getImgArea(dlg.fAnchor_t);
		intY_T = t[1]+intY_T;
	}
	if (dlg.fAnchor_r != null && intX_L != null && intW_R != null) {
		var r = getImgArea(dlg.fAnchor_r);
		intW_R = r[0]+intW_R-intX_L;
	}
	if (dlg.fAnchor_b != null && intY_T != null && intH_B != null) {
		var b = getImgArea(dlg.fAnchor_b);
		intH_B = b[1]+intH_B-intY_T;
	}

	dlg.fLayer = getObj(dialogID);
	dlg.fLayer.fContainer = getObj(dialogID+"Container");
	if (dlg.fLayer_bw == null) {
		var a1 = dlg.fLayer.area();
		var a2 = dlg.fLayer.fContainer.area();
		dlg.fLayer_bw = a1[2]-a2[2];
		dlg.fLayer_bh = a1[3]-a2[3];
	}

	if (intW_R != null && intH_B != null && !isNaN(dlg.fLayer_bw)) {
		dlg.fLayer.sizeTo(intW_R+dlg.fLayer_bw,intH_B+dlg.fLayer_bh);
		dlg.fLayer.fContainer.sizeTo(intW_R,intH_B);
	}

    if (dlg.fAnchor_dx == null || dlg.fAnchor_dy == null) {
        dlg.render = function() {
            if (this.fLayer != null) {
                var x, y, w, h;
                x = 0;
                y = 0;
                var size = getWindowSize();
                w = size[0];
                h = size[1];
                
                //alert("huhu: "+w+"/"+h);
                this.fLayer.sizeTo(w,h);
                this.fLayer.moveTo(x,y);
                if (!isNaN(this.fLayer_bw)) {
                    this.fLayer.fContainer.sizeTo(w-this.fLayer_bw,h-this.fLayer_bh);
                }
            }
        }
    } else {
        dlg.render = function() {
            var x = this.fAnchor_dx;
            var y = this.fAnchor_dy;
            if (this.fLayer != null && (x != null || y != null)) {
                if (x == null || y == null) {
                    var area = this.fLayer.area();
                    if (x == null) {
                        x = area[0];
                    }
                    if (y == null) {
                        y = area[1];
                    }
                }
                if (this.fAnchor_l != null) {
                    var l = getImgArea(this.fAnchor_l);
                    x = x+l[0];
                }
	            if (this.fAnchor_t != null) {
                    var l = getImgArea(this.fAnchor_t);
                    y = y+t[1];
                }
	            this.fLayer.moveTo(x,y);
            }
        }
    }
    
    dlg.onResize = function(e) {
        if (this.fAdapt) {
            this.render();
        }
    }
    dlg.show = function () {
        if (this.fHideFormElements) {
            gD.fHideFormElementsRestoreStates = gD.showHideSelects("hidden",null);
        }
        dlg.fLayer.show();
    }
    dlg.hide = function() {
    	this.fLayer.hide();
	    this.fLayer = null;
	    if (this.fHideFormElements) {
	        var ignore = gD.showHideSelects("visible",gD.fHideFormElementsRestoreStates);
			gD.fHideFormElementsRestoreStates = null;
		}
	    if (this.fAdapt) {
	        gB.removeOnResize(this);
	    }
	    if (this.fClose) {
		    window.close();
	    }
    }
	if (doAdapt) {
		gB.installOnResize(dlg);
		dlg.onResize(null);
	} else {
	    dlg.render();
	}
	return dlg;
}

function dialogs_SetHtml(aDialog,elemID,strHtml) {
    var e = eval("window."+elemID);
    if (e == null && !gB.NS) {
	    e = document.getElementById(elemID);
    }
    if (e != null && e.innerHTML != null) {
	    e.innerHTML = strHtml;
    } else if (strHtml != "") {
	    alert("dialogs_SetHtml: "+elemID+" not found?!\n"+strHtml);
    }
}

var gConfirmDialog = null;
function doConfirm(objCtrl,postBackRefOrFunction,strHtml) {
    gConfirmDialog = dialogs_Open("ConfirmDialog","anchorheader","anchorheader","anchorfooter","anchorfooter",-12,0,12,6,-8-12,-8,true,false,true);
    dialogs_SetHtml(gConfirmDialog,"ConfirmDialogMessage",strHtml);
    gConfirmDialog.onCancel = function() { this.hide(); };
    gConfirmDialog.onOK = function() { this.hide(); this.doOK(); };
    if (postBackRefOrFunction != null && typeof (postBackRefOrFunction) == "function") {
        gConfirmDialog.doOK = postBackRefOrFunction;
    } else {
        // build the doOK function
        var fs = objCtrl.onclick.toString();
        var re = new RegExp();
        var fhead = fs.match(/(function[^\{]*)\{/);
        var fbody = fs.match(/var\ sepa\;([^\}]*)\}/);
        if (fhead != null && fhead.length >= 2 && fbody != null && fbody.length >= 2) {
            if (fbody[1].search(/\S/) >= 0) {
                gConfirmDialog.itsCode = fbody[1];
                gConfirmDialog.doOK = function() {
                    eval(this.itsCode);
                };
            } else {
                gConfirmDialog.postBackRefOrFunction = postBackRefOrFunction;
                gConfirmDialog.doOK = function() {
                    __doPostBack(this.postBackRefOrFunction, '');
                }
            }
        } else {
            alert("doConfirm: onClick function not correctly formatted!\n" + fs + "\nhead=" + fhead + "\nbody=" + fbody);
            gConfirmDialog.doOK = function() { alert("doConfirm: override doOK ..."); };
        }
    }
    gConfirmDialog.show();
    return false;
}

var gConfirmDeleteDialog = null;
function doConfirmDelete(objCtrl,postBackRefOrFunction,strHtml) {
    gConfirmDeleteDialog = dialogs_Open("ConfirmDeleteDialog","anchorheader","anchorheader","anchorfooter","anchorfooter",-12,0,12,6,-8-12,-8,true,false,true);
    dialogs_SetHtml(gConfirmDeleteDialog,"ConfirmDeleteDialogMessage",strHtml);
    gConfirmDeleteDialog.onCancel = function() { this.hide(); };
    gConfirmDeleteDialog.onDelete = function() { this.hide(); this.doDelete(); };
    if (postBackRefOrFunction != null && typeof (postBackRefOrFunction) == "function") {
        gConfirmDeleteDialog.doDelete = postBackRefOrFunction;
    } else {
        // build the doDelete function
        var fs = objCtrl.onclick.toString();
        var re = new RegExp();
        var fhead = fs.match(/(function[^\{]*)\{/);
        var fbody = fs.match(/var\ sepa\;([^\}]*)\}/);
        if (fhead != null && fhead.length >= 2 && fbody != null && fbody.length >= 2) {
            if (fbody[1].search(/\S/) >= 0) {
                gConfirmDeleteDialog.itsCode = fbody[1];
                gConfirmDeleteDialog.doDelete = function() {
                    eval(this.itsCode);
                };
            } else {
                gConfirmDeleteDialog.postBackRefOrFunction = postBackRefOrFunction;
                gConfirmDeleteDialog.doDelete = function() {
                    __doPostBack(this.postBackRefOrFunction, '');
                }
            }
        } else {
            alert("doConfirmDelete: onClick function not correctly formatted!\n" + fs + "\nhead=" + fhead + "\nbody=" + fbody);
            gConfirmDeleteDialog.doDelete = function() { alert("doConfirmDelete: override doDelete ..."); };
        }
    }
    gConfirmDeleteDialog.show();
    return false;
}

var gToolTipBox = null;
function showToolTip(objCtrl,strHtml,intAtOrDx,intAtOrDy,imgName,intMinWidth,intMaxWidth,strHorMode,strVerMode) {
    var e = document.getElementById("ToolTipBoxMessage");
    if (e != null) {
        if (gToolTipBox == null) {
           gToolTipBox = getObj("ToolTipBox");
        }
        if (e.style.minWidth != null) {
            if (intMinWidth != null) {
                e.style.minWidth = ""+intMinWidth+"px";
            } else {
                e.style.minWidth = "";
            }
        }
        if (e.style.maxWidth != null) {
            if (intMaxWidth != null) {
                e.style.maxWidth = ""+intMaxWidth+"px";
            } else {
                e.style.maxWidth = "";
            }
        }
        e.innerHTML = strHtml;

        var w = parseInt(e.offsetWidth);
        var h = parseInt(e.offsetHeight);
        
        if ((imgName == null || imgName == "") && objCtrl != null && objCtrl.src != null) {
            imgName = objCtrl.name;
        }
        if (intAtOrDx == null) {
            intAtOrDx = 0;
        }
        if (intAtOrDy == null) {
            intAtOrDy = 0;
        }
        if (strHorMode != null) {
            if (strHorMode == "center") {
                intAtOrDx = intAtOrDx-w/2;
            } else if (strHorMode == "right") {
                intAtOrDx = intAtOrDx-w;
            }
        }
        if (strVerMode != null) {
            if (strVerMode == "middle") {
                intAtOrDy = intAtOrDy-h/2;
            } else if (strVerMode == "bottom") {
                intAtOrDy = intAtOrDy-h;
            }
        }
        
        if (imgName != null) {
            var area = getImgArea(imgName);
            if (strHorMode != null) {
                intAtOrDx = intAtOrDx+area[0];
                if (strHorMode == "center") {
                    intAtOrDx = intAtOrDx+area[2]/2;
                } else if (strHorMode == "right") {
                    intAtOrDx = intAtOrDx+area[2];
                }
            } else {
                if (intAtOrDx < 0) {
                    intAtOrDx = intAtOrDx+area[0]-parseInt(e.offsetWidth);
                } else {
                    intAtOrDx = intAtOrDx+area[0]+area[2];
                }
            }
            if (strVerMode != null) {
                intAtOrDy = intAtOrDy+area[1];
                if (strVerMode == "middle") {
                    intAtOrDy = intAtOrDy+area[3]/2;
                } else if (strVerMode == "bottom") {
                    intAtOrDy = intAtOrDy+area[3];
                }
            } else {
                if (intAtOrDy < 0) {
                    intAtOrDy = intAtOrDy+area[1]-parseInt(e.offsetHeight);
                } else {
                    intAtOrDy = intAtOrDy+area[1]+area[3];
                }
            }
        }
        gToolTipBox.moveTo(intAtOrDx,intAtOrDy);
        gToolTipBox.show();
        return true;
    } else {
        return false;
    }
}
function hideToolTip() {
    if (gToolTipBox != null) {
        gToolTipBox.hide();
    }
    gToolTipBox = null;
    return true;
}
	
var gMessageBox = null;
function showMessage(anchor,strHtml,strHorMode,intAtOrDx,strVerMode,intAtOrDy,intHideDelay,intMinWidth,intMaxWidth) {
    var e = document.getElementById("MessageBoxMessage");
    if (e != null) {
        if (gMessageBox == null) {
            gMessageBox = getObj("MessageBox");
        }
        if (e.style.minWidth != null) {
            if (intMinWidth != null) {
                e.style.minWidth = ""+intMinWidth+"px";
            } else {
                e.style.minWidth = "";
            }
        }
        if (e.style.maxWidth != null) {
            if (intMaxWidth != null) {
                e.style.maxWidth = ""+intMaxWidth+"px";
            } else {
                e.style.maxWidth = "";
            }
        }
        e.innerHTML = strHtml;
        e = document.getElementById("MessageBoxContainer");
        var w = parseInt(e.offsetWidth);
        var h = parseInt(e.offsetHeight);

        if (intAtOrDx == null) {
            intAtOrDx = 0;
        }
        if (intAtOrDy == null) {
            intAtOrDy = 0;
        }
        if (strHorMode == "center") {
            intAtOrDx = intAtOrDx-w/2;
        } else if (strHorMode == "right") {
            intAtOrDx = intAtOrDx-w;
        }
        if (strVerMode == "middle") {
            intAtOrDy = intAtOrDy-h/2;
        } else if (strVerMode == "bottom") {
            intAtOrDy = intAtOrDy-h;
        }  
        if (anchor != null) {
            var area;
            if (typeof(anchor) == "string") {
                area = getImgArea(anchor);
            } else {
                area = getHtmlElementArea(anchor);
            }
            intAtOrDx = intAtOrDx+area[0];
            if (strHorMode == "center") {
                intAtOrDx = intAtOrDx+area[2]/2;
            } else if (strHorMode == "right") {
                intAtOrDx = intAtOrDx+area[2];
            }
            intAtOrDy = intAtOrDy+area[1];
            if (strVerMode == "middle") {
                intAtOrDy = intAtOrDy+area[3]/2;
            } else if (strVerMode == "bottom") {
                intAtOrDy = intAtOrDy+area[3];
            }
        }
        gMessageBox.moveTo(intAtOrDx,intAtOrDy);
        gMessageBox.show();
        if (intHideDelay > 0) {
            window.setTimeout('hideMessage()',intHideDelay);
        }
        return true;
    } else {
        return false;
    }
}
function hideMessage() {
    if (gMessageBox != null) {
        gMessageBox.hide();
    }
    gMessageBox = null;
    return true;
}

var gProgressPopup = null;
function setupProgressPopup() {
    if (gProgressPopup == null) {
        gProgressPopup = dialogs_Open("ProgressPopup",null,null,null,null,null,null,null,null,null,null,true,false,true);

        gProgressPopup.itsPanel = document.getElementById("ProgressPopupPanel");
        gProgressPopup.itsRotator = findImg(document,"ProgressPopupRotator");
        gProgressPopup.itsControls = document.getElementById("ProgressPopupControls");
        gProgressPopup.itsTitle = document.getElementById("ProgressPopupTitle");
        gProgressPopup.itsTask = document.getElementById("ProgressPopupTask");
        gProgressPopup.itsInfo = document.getElementById("ProgressPopupInfo");
        gProgressPopup.itsLog = document.getElementById("ProgressPopupLog");

	    gProgressPopup.onClose = function() { if (this.onCloseDo != null) { eval(this.onCloseDo); } else { this.hide(); };  };
    }
}
function clearProgress() {
    setupProgressPopup();
    gProgressPopup.itsControls.style.visibility = "hidden";
    gProgressPopup.itsTitle.innerHTML = "";
    gProgressPopup.itsTask.innerHTML = "";
    gProgressPopup.itsInfo.innerHTML = "";
    gProgressPopup.itsLog.value = "";
}
function showProgress(imgName,strHorMode,intAtOrDx,strVerMode,intAtOrDy,intHideDelay) {
    setupProgressPopup();
    gProgressPopup.itsControls.style.visibility = "hidden";
    gProgressPopup.onCloseDo = null;
    
    if (strHorMode != null || strVerMode != null) {
        var w = parseInt(gProgressPopup.itsPanel.width);
        var h = parseInt(gProgressPopup.itsPanel.height);

        if (intAtOrDx == null) {
            intAtOrDx = 0;
        }
        if (intAtOrDy == null) {
            intAtOrDy = 0;
        }
        if (strHorMode == "center") {
            intAtOrDx = intAtOrDx-w/2;
        } else if (strHorMode == "right") {
            intAtOrDx = intAtOrDx-w;
        }
        if (strVerMode == "middle") {
            intAtOrDy = intAtOrDy-h/2;
        } else if (strVerMode == "bottom") {
            intAtOrDy = intAtOrDy-h;
        }  
        if (imgName != null) {
            var area = getImgArea(imgName);
            intAtOrDx = intAtOrDx+area[0];
            if (strHorMode == "center") {
                intAtOrDx = intAtOrDx+area[2]/2;
            } else if (strHorMode == "right") {
                intAtOrDx = intAtOrDx+area[2];
            }
            intAtOrDy = intAtOrDy+area[1];
            if (strVerMode == "middle") {
                intAtOrDy = intAtOrDy+area[3]/2;
            } else if (strVerMode == "bottom") {
                intAtOrDy = intAtOrDy+area[3];
            }
        }
        gProgressPopup.itsPanel.style.left = ""+intAtOrDx+"px";
        gProgressPopup.itsPanel.style.top = ""+intAtOrDy+"px";
    }
    
    gProgressPopup.show();
    if (intHideDelay > 0) {
        window.setTimeout('hideProgress()',intHideDelay);
    }
    return false;
}
function hideProgress() {
    if (gProgressPopup != null) {
        showProgressRotator(true);
        clearProgress();
        gProgressPopup.hide();
    }
    gProgressPopup = null;
}
function setProgressTitle(text) {
    setupProgressPopup();
    if (gProgressPopup != null) {
        gProgressPopup.itsTitle.innerHTML = text;
    }
}
function setProgressInfo(task,info) {
    setupProgressPopup();
    if (gProgressPopup != null) {
        gProgressPopup.itsTask.innerHTML = task;
        gProgressPopup.itsInfo.innerHTML = info;
    }
}
function logProgressInfo(text) {
    setupProgressPopup();
    if (gProgressPopup != null) {
        gProgressPopup.itsLog.value = gProgressPopup.itsLog.value+(text.split("<br/>").join("\n")+"\n");
    }
}
function showProgressCloseButton(strOnCloseDo) {
    setupProgressPopup();
    if (gProgressPopup != null) {
        gProgressPopup.onCloseDo = strOnCloseDo;
        gProgressPopup.itsControls.style.visibility = "visible";
    }
}
function showProgressRotator(doShow) {
    setupProgressPopup();
    if (gProgressPopup != null && gProgressPopup.itsRotator != null) {
        var s = gProgressPopup.itsRotator.src;
        s = s.split("_off.gif").join(".gif");
        if (!doShow) {
            s = s.split(".gif").join("_off.gif");
        }
        gProgressPopup.itsRotator.src = s;
    }
}

function doPopup(strURL,strTag,strParams) {
    var w = window.open(strURL,strTag,strParams);
    return w;
}

function doPopupWindow(strURL,strTag,strPopupWidth,strPopupHeight,strContentWidth,strContentHeight,strParams) {
    var strAppend = "";
    if (strParams == null) {
        strParams = "location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,hotkeys=0";
    }
    if (strPopupWidth != null) {
        strParams = strParams+",width="+strPopupWidth;
    }
    if (strContentWidth != null) {
        strAppend = strAppend+"&cntW="+strContentWidth;
    } else if (strPopupWidth != null) {
        strAppend = strAppend+"&cntW="+strPopupWidth;
    }
    if (strPopupHeight != null) {
        strParams = strParams+",height="+strPopupHeight; 
    }
    if (strContentHeight != null) {
        strAppend = strAppend+"&cntH="+strContentHeight;
    } else if (strPopupHeight != null) {
        strAppend = strAppend+"&cntH="+strPopupHeight;
    }
    if (strAppend.length > 0) {
        if (strURL.indexOf("?") < 0) {
            strURL = strURL+"?"+strAppend.substring(1);
        } else {
            strURL = strURL+strAppend;
        }
    }
    return doPopup(strURL,strTag,strParams);
}

function doXtnPopup(strURL,strTag,intWidth,intHeight) {
    var strParams = "location=0,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0,hotkeys=0";
    if (intWidth != null) {
        strParams = "width="+intWidth+","+strParams;
    }
    if (intHeight != null) {
        strParams = "height="+intHeight+","+strParams;
    }
    return doPopup(strURL,strTag,strParams);
}
function doOpenHtmlEditor(openerControlID, openerControlInfo, popupTag) {
    doPopupWindow("/addons/xtn/shared/html_editor.aspx?cid=" + openerControlID + "&cinfo=" + openerControlInfo, popupTag, 760, 640, 720, null, "location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,hotkeys=0");
}

function portalControlRedirect(storeID,target,content,query) {
    var store = getByIdOrName(storeID);
    var poco = content;
    if (query != null && query.length > 0) {
        poco = poco+"&"+query;
    }
    store.value = poco;
    __doPostBack(target,'');
}


