
var TSoftJavaScriptUtil = new Object();

// Creates a new XML-HTTP object in supported browsers,
// otherwise returns NULL

TSoftJavaScriptUtil.saveDocument = function() {
	var globalForm = document.forms['global'];
	if ( globalForm.elements['dcedit_text'] )
		globalForm.elements['dcedit_text'].value = editor.document.body.innerHTML;
	globalForm.submit();
}

TSoftJavaScriptUtil.xmlHttpRequest = function () {
	var xmlHttpRequest = null;
	if ( typeof ActiveXObject == 'function' ) {
		try {
			xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch ( e ) {}
	}
	if ( xmlHttpRequest == null && typeof ActiveXObject == 'function' ) {
		try {
			xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch ( e ) {}
	}
	if ( xmlHttpRequest == null && (XMLHttpRequest) ) {
		try {
			xmlHttpRequest = new XMLHttpRequest();
		} catch ( e ) {}
	}
	if ( xmlHttpRequest == null && typeof (window.createRequest) == 'function' ) {
		try {
			xmlHttpRequest = window.createRequest();
		} catch ( e ) {}
	}
	return xmlHttpRequest;
};

TSoftJavaScriptUtil.xmlHttpResponse = function( url, parameters ) {
	var xmlHttp = TSoftJavaScriptUtil.xmlHttpRequest();
	if ( ! xmlHttp )
		return null;
	try {
		document.body.style.cursor = 'wait';
		xmlHttp.open('POST', url, false);
		xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
		if ( parameters == null )
			parameters = "";
		xmlHttp.send(parameters);
		if ( xmlHttp.readyState != 4 )
			return null;
		return xmlHttp;
	} catch ( e ) {
		return null;
	} finally {
		document.body.style.cursor = 'default';
	}
}

TSoftJavaScriptUtil.addOnloadListener = function( func ) {
	TSoftJavaScriptUtil.addEventListener( window, "onload", func );
/*	if ( ! typeof func == "function" )
		return;
	if ( typeof window.onload == "function" ) {
		var oldonload = window.onload;
		window.onload = function () {
			try { 
				oldonload(); 
			} catch ( e ) {}
			func();
		}
	} else {
		window.onload = func;
	}*/
}

TSoftJavaScriptUtil.addEventListener = function( target, method, func ) {
	if ( ! (typeof func == "function") || ! (typeof method == "string") )
		return;
	if ( typeof target[method] == "function" ) {
		var oldfunc = target[method];
		target[method] = function () {
			try { oldfunc.apply( target, arguments ); } catch ( e ) {}
			func.apply( target, arguments );
		}
	} else {
		target[method] = func;
	}
}


TSoftJavaScriptUtil.addOnfocusListener = function( func, win ) {
	if ( ! typeof func == "function" )
		return;
	if ( ! win )
		win = window;
	if ( typeof win.onfocus == "function" ) {
		var oldonfocus = win.onfocus;
		win.onfocus = function () {
			try { oldonfocus() } catch ( e ) {}
			func();
		}
	} else {
		win.onfocus = func;
	}
}

TSoftJavaScriptUtil.addOnblurListener = function( func, win ) {
	if ( ! typeof func == "function" )
		return;
	if ( ! win )
		win = window;
	if ( typeof win.onblur == "function" ) {
		var oldonblur = win.onblur;
		win.onblur = function () {
			try { oldonblur() } catch ( e ) {}
			func();
		}
	} else {
		win.onblur = func;
	}
}


TSoftJavaScriptUtil.splitOnBrackets = function( str, leftBracket, rightBracket ) {
    if ( ! leftBracket )
        leftBracket = '[';
    if ( ! rightBracket )
        rightBracket = ']';
    if ( typeof str != "string"  )
        return null;
    var i;
    var result = new Array();
    var actual = null;
    for ( i = 0; i < str.length; i++ ) {
        if ( actual == null ) {
            if ( str.charAt(i) == leftBracket ) {
                actual = "";
            }
        } else {
            if ( str.charAt(i) != rightBracket ) {
                actual += str.charAt(i);
            } else if ( i+1 < str.length && str.charAt(i+1) == rightBracket ) {
                actual += str.charAt(i);
                i++;
            } else {
                result[result.length]=actual;
                actual = null;
            }
        }
    }
    for ( i = 0 ; i < result.length; i++ )
        alert( result[i] );
    return result;
}

TSoftJavaScriptUtil.joinOnBrackets = function( arr, leftBracket, rightBracket ) {
    if ( ! leftBracket )
        leftBracket = '[';
    if ( ! rightBracket )
        rightBracket = ']';
    var i;
    var result = "";
    for ( i = 0; i < arr.length; i++ ) {
        result += leftBracket;
        var j;
        for ( j = 0; j < arr[i].length; j++ ) {
            result += arr[i].charAt(j);
            if ( arr[i].charAt(j) == rightBracket )
                result += rightBracket;
        }
        result += rightBracket;
    }
    return result;
}


TSoftJavaScriptUtil.sumInputElements = function( formName, elementNames ) {
	var sum = 0;
	var form = document.forms[formName];
	if ( ! form || !(elementNames instanceof Array) )
		return null;
	for ( var i = 0; i < elementNames.length; i++ ) {
		var element = form.elements[elementNames[i]];
		if ( ! element || element.value == null )
			return null;
		var num = parseInt( element.value, 10 );
        if ( ! isNaN(num) )
            sum += num;
	}
	return sum;
}


TSoftJavaScriptUtil.getAbsolutePosition = function( element ) {
    var x = 0, y = 0;
    while( element ) {
        y += element.offsetTop;
        x += element.offsetLeft;
        element = element.offsetParent;
    }

	return { x:x, y:y };
}

TSoftJavaScriptUtil.overlayElement = function( element, className ) {
    var pos = TSoftJavaScriptUtil.getAbsolutePosition( element );
    var div = document.createElement("div");
    document.body.appendChild(div);
    div.style.position = "absolute";
    div.style.left = pos.x + "px";
    div.style.top = pos.y + "px";
    div.style.width = element.scrollWidth + "px";
    div.style.height = element.scrollHeight + "px";
    div.className = className;
    //div.style.border = "1px solid red";
    div.appendChild( document.createTextNode("\u00A0") );
    return div;
}

TSoftJavaScriptUtil.showTempLayer = function( openerObject, tempLayerId, offsetX, offsetY ) {
    var pos = TSoftJavaScriptUtil.getAbsolutePosition( openerObject );
    var tLayer = document.getElementById(tempLayerId);
    tLayer.style.top = pos.y + offsetY + "px";
    tLayer.style.left = pos.x + offsetX + "px";
    tLayer.style.display = "block";
}
TSoftJavaScriptUtil.hideTempLayer = function( tempLayerId ) {
	document.getElementById(tempLayerId).style.display = "none";
}

TSoftJavaScriptUtil.formatNumber = function( num, sep ) {
	var str = new String(num);
	if ( ! sep || typeof sep != "string" )
		sep = " ";
	if ( str == "" )
		return "";
	var sign = "";
	if ( "+-".indexOf(str.charAt(0)) > -1 ) {
		sign = str.charAt(0);
		str = str.substring(1);
	}
	var result = "";
	for ( var i = str.length-1; i >= 0; i-- ) {
		if ( (str.length-1-i) % 3 == 0 && result != "" )
			result = " " + result;
		result = str.charAt(i) + result;
	}
	return sign + result;
}


new function() {
	var timeoutHandler = null;
	var timeoutDelay = 1 * 15 * 1000;
	var timeoutInterval = 5 * 60 * 1000; //10*60*1000;
	var dialog = 0;
	var keepAlive = function() {
		try {
			var xmlhttp = TSoftJavaScriptUtil.xmlHttpResponse('/flowmanager/common/keepAlive.jsp?'+((new Date()).getTime()), null);
			if ( xmlhttp.responseXML ) {
				var dList = xmlhttp.responseXML.getElementsByTagName("dialog");
				if ( dList.length == 1 ) {
					var url = dList.item(0).firstChild.data;
					var width = dList.item(0).getAttribute("width");
					var height = dList.item(0).getAttribute("height");
					if ( dialog == 0 ) {
						dialog = 1;
						TSoftJavaScriptUtil.virtualDialogHolder.openDialog( url, width, height );
						//TSoftJavaScriptUtil.openModal( url, width, height );
						//dialog = 0;
					}
				}
			}

			//var xmlhttp = TSoftJavaScriptUtil.xmlHttpRequest();
		    //xmlhttp.open('GET', '/flowmanager/common/keepAlive.jsp?' + ((new Date()).getTime()), true);
	    	//xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
		    //xmlhttp.send(null);
	    } catch ( exception ) {
	    }
	    timeoutHandler = setTimeout( keepAlive, timeoutInterval );
	}
	timeoutHandler = setTimeout( keepAlive, timeoutDelay );
	this.clear = function() {
		clearTimeout(timeoutHandler);
	}
}();


var saveFormSendDone = false;

function safeFormSend(formName,command) {
	if ( saveFormSendDone )
		return;
	var formElement = document.forms[formName];
	if ( formElement == null )
		return;
	var commandElement = formElement.elements['command'];
    if ( commandElement == null )
    	return;
    if ( command )
    	commandElement.value = command;
   	saveFormSendDone = true;
   	formElement.submit();
}


TSoftJavaScriptUtil.KEYCODE_BACKSPACE = 8;
TSoftJavaScriptUtil.KEYCODE_DELETE = 46;
TSoftJavaScriptUtil.KEYCODE_INSERT = 45;

TSoftJavaScriptUtil.isNumber = function( num ) {
	var str = new String(num);
	var integerDigits = 0;
	var integerPart = true;
	for ( var i = 0; i < str.length; i++ ) {
		if ( "+-".indexOf(str.charAt(i)) > -1 ) {
			if ( i > 0 )
				return false;
		} else if ( "0123456789".indexOf(str.charAt(i)) > -1 ) {
			if ( integerPart )
				integerDigits++;
		} else if ( "." == str.charAt(i) ) {
			if ( ! integerPart || integerDigits == 0 )
				return false;
			integerPart = false;
		} else {
			return false;
		}
	}
	return integerDigits>0;
}

TSoftJavaScriptUtil.formatNumber = function( num, decSep, fracSep ) {
	var str = new String(num);
    if ( typeof decSep != "string" )
    	decSep = " ";
    if ( typeof fracSep != "string" )
    	fracSep = ".";

    // if value is empty then result is empty
    if ( str == "" )
        return "";
    // if value is not a number there is no formatting instruction
    if ( ! TSoftJavaScriptUtil.isNumber(str) )
    	return str;

    var signum = "";
    if ( "+-".indexOf(str.charAt(0)) > -1 ) {
    	signum = str.charAt(0);
        str = str.substring(1);
    }

    var integerDigits, fractionDigits, fractionSeparator;
    if ( str.indexOf(".") > -1 ) {
    	integerDigits = str.substring(0,str.indexOf("."));
    	fractionDigits = str.substring(str.indexOf(".")+1);
    	fractionSeparator = fracSep;
	} else {
		integerDigits = str;
		fractionDigits = "";
		fractionSeparator = "";
	}

    var integerResult = "";
    for ( var i = integerDigits.length-1; i >= 0; i-- ) {
        if ( (integerDigits.length-1-i) % 3 == 0 && integerResult != "" )
        	integerResult = decSep + integerResult;
        integerResult = integerDigits.charAt(i) + integerResult;
    }

    var fractionResult = "";
    for ( var i = 0; i < fractionDigits.length; i++ ) {
    	//if ( i != 0 && i % 3 == 0 )
    	//	fractionResult += decSep;
    	fractionResult += fractionDigits.charAt(i);
    }

    return signum + integerResult + fractionSeparator + fractionResult;
}

TSoftJavaScriptUtil.numericKeyPress = function(e,formattedElement,rawElement,decSep,maxDigit, fracSep, maxFrac){
    var keynum = window.event ? e.keyCode : e.which;
    if ( keynum == TSoftJavaScriptUtil.KEYCODE_BACKSPACE )
        return false;
    if ( keynum == 0 || keynum == 9 )
        return true;
    //formattedElement = document.getElementById("nagy");
    var keyChar = String.fromCharCode(keynum);
    if ( ! keyChar || keyChar == "" )
        return false;

    if ( "-" == keyChar ) {
    	if ( rawElement.value == "" ) {
    		rawElement.value = "-";
	    	formattedElement.value = TSoftJavaScriptUtil.formatNumber(rawElement.value, decSep, fracSep);
    	}
    } else if ( fracSep && maxFrac > 0 && fracSep == keyChar ) {
    	if ( rawElement.value.indexOf(".") == -1 ) {
    		var digits = rawElement.value.indexOf('-') == 0 ? rawElement.value.length-1 : rawElement.value.length;
    		if ( digits > 0 ) {
    			rawElement.value += ".";
    	    	formattedElement.value = TSoftJavaScriptUtil.formatNumber(rawElement.value, decSep, fracSep);
    		}
    	}
    } else if ( "0123456789".indexOf(keyChar)>-1 ) {
    	var sep = rawElement.value.indexOf(".");
    	if ( sep == -1 ) {
    		var digits = rawElement.value.indexOf('-') == 0 ? rawElement.value.length-1 : rawElement.value.length;
        	if ( digits < maxDigit ) {
        		rawElement.value += keyChar;
    	    	formattedElement.value = TSoftJavaScriptUtil.formatNumber(rawElement.value, decSep, fracSep);
        	}
    	} else {
    		var digits = rawElement.value.length-sep-1;
        	if ( digits < maxFrac ) {
        		rawElement.value += keyChar;
    	    	formattedElement.value = TSoftJavaScriptUtil.formatNumber(rawElement.value, decSep, fracSep);
        	}
    	}
    }
    return false;
}

TSoftJavaScriptUtil.numericKeyDown = function(e,formattedElement,rawElement,decSep,fracSep){
    var keynum = window.event ? e.keyCode : e.which;
    if ( keynum == TSoftJavaScriptUtil.KEYCODE_BACKSPACE || keynum == TSoftJavaScriptUtil.KEYCODE_DELETE || keynum == TSoftJavaScriptUtil.KEYCODE_INSERT ) {
        if ( keynum == TSoftJavaScriptUtil.KEYCODE_BACKSPACE && rawElement.value.length > 0 ) {
            rawElement.value = rawElement.value.substring(0, rawElement.value.length-1);
        }
        if ( keynum == TSoftJavaScriptUtil.KEYCODE_DELETE && rawElement.value.length > 0  ) {
            rawElement.value = "";
        }
        if ( keynum == TSoftJavaScriptUtil.KEYCODE_INSERT && window.clipboardData && window.clipboardData.getData ) {
        	var text = window.clipboardData.getData('Text');
        	if ( ! isNaN(parseInt(text,10)) )
        		rawElement.value = parseInt(text,10);
        }
        formattedElement.value = TSoftJavaScriptUtil.formatNumber(rawElement.value,decSep,fracSep);
        return false;
    } else if ( keynum >= 35 && keynum <= 40 ) {
        return false;
    }
    return true;
}

TSoftJavaScriptUtil.setInputNumber = function( formName, fieldName, value, decSep, fracSep ) {
	var form = document.forms[formName];
	var rawElement = form.elements[fieldName];
	var formattedElement = rawElement.previousSibling;
	rawElement.value = value;
	formattedElement.value = TSoftJavaScriptUtil.formatNumber(rawElement.value,decSep, fracSep);
}

TSoftJavaScriptUtil.getCookie = function( name ) {
	var cookies = document.cookie;
	if ( ! cookies )
		return null;
	var prefix = name+"=";
	var beginIndex = cookies.indexOf(prefix);
	if ( beginIndex == -1 )
		return null;
	beginIndex += prefix.length;
	cookies = cookies.substring(beginIndex, cookies.length);
	beginIndex = cookies.indexOf(";");
	if ( beginIndex == -1 )
		return unescape(cookies);
	else
		return unescape(cookies.substring(0,beginIndex));
}

TSoftJavaScriptUtil.setCookie = function( name, value, expr ) {
	var cookie = name + "=" + escape(value);
	if ( expr instanceof Date ) {
		cookie += ";expires="+expr.toGMTString();
	} else if ( typeof expr == "number" ) {
		var d = new Date();
		d.setTime( d.getTime() + expr*1000*60*60*24 );
		cookie += ";expires="+d.toGMTString();
	}
	document.cookie = cookie;
}

TSoftJavaScriptUtil.delCookie = function( name ) {
	var d = new Date();
	d.setTime(0);
	TSoftJavaScriptUtil.setCookie(name,"",d);
}

TSoftJavaScriptUtil.openModal = function( url, width, height ) {
	if ( window.showModalDialog ) {
		var args = new Object();
		window.showModalDialog(url, args, "dialogWidth:"+width+"px;dialogHeight:"+height+"px;center: yes;status: no;");
		if (args.returnPage)
			window.location.href = args.returnPage;
	} else {
		var myWindow = window.open(url,"modalwindow","width="+width+",height="+height+",status=0");
		TSoftJavaScriptUtil.addOnfocusListener( function() { myWindow.close(); } );
	}
}

TSoftJavaScriptUtil.viewDocumentLite = function(irtkey) {
	window.open('/flowmanager/viewDocumentLite.jsp?irt=' + irtkey, "mywindow", "menubar=0,resizable=1,scrollbars=1,width=825,height=700,status=0,toolbar=0,location=0,directories=0");
}

TSoftJavaScriptUtil.firstAncestor = function( element, nodeName ) {
	while ( element.parentNode && element.parentNode.nodeName != nodeName )
		element = element.parentNode;
	return element.parentNode;
}


TSoftJavaScriptUtil.radioButtonValue = function( element ) {
	for ( var i = 0; i < element.length; i++ )
		if ( element[i].checked )
			return element[i].value;
	return null;
}

TSoftJavaScriptUtil.selectElementValue = function( element ) {
	return element.selectedIndex > -1 ? element.options[element.selectedIndex].value : null;
}

TSoftJavaScriptUtil.removeAncestorElement = function( element, nodeName ) {
	element = TSoftJavaScriptUtil.firstAncestor( element, nodeName );
	if ( element != null )
		element.parentNode.removeChild(element);
}


/* ------------------------------------------------------------------------- */

TSoftDropDownMenuItem = function( text, value ) {
	this.text = text;
	this.value = value;
}

TSoftDropDownResult = function( list, message ) {
	this.list = list;
	this.message = message;
}

TSoftDropDownMenu = function( element ) {
	this.inputElement = element;
	this.timeout = null;
    this.container = null;
    this.result = null;

    element.__tsDropDownMenu = this;
    element.onfocus = function() {
    	var ddr = this.__tsDropDownMenu;
    	ddr.init(this);
        ddr.load(this.value);
    }
    element.onblur = function() {
    	var ddr = this.__tsDropDownMenu;
    	ddr.clearTimeout();
        ddr.close()
    }
    /*element.onchange = function(event) {
    	var ddr = this.__tsDropDownMenu;
        var keynum = window.event ? event.keyCode : event.which;
        //if ( keynum != 13 ) {
        //	ddr.load(this.value);
        //}
    }*/
    element.onkeydown = function(e) {
    	var ddr = this.__tsDropDownMenu;
        var keynum = window.event ? window.event.keyCode : e.which;
    	switch ( keynum ) {
        	case 40: ddr.selectNext(); break;
            case 38: ddr.selectPrev(); break;
            case 13: ddr.confirmSelection(); break;
        }
    }
    element.onkeyup = function(e) {
    	var ddr = this.__tsDropDownMenu;
        var keynum = window.event ? window.event.keyCode : e.which;
        if ( keynum != 13 ) {
        	ddr.load(this.value);
        }
    }
};

TSoftDropDownMenu.prototype.init = function( element ) {
    if ( this.container != null )
        this.close();
    this.result = null;

    this.container = document.createElement("div");
    var dimensions = TSoftJavaScriptUtil.getAbsolutePosition( element );
    this.container.className = "partnerListContainer";
    this.container.style.position = "absolute";
    this.container.style.left = dimensions.x+"px";
    this.container.style.top = (dimensions.y+element.clientHeight)+"px";
    this.container.style.width = element.offsetWidth-9+"px";
    this.container.style.display = "none";
    document.body.appendChild(this.container);
}

TSoftDropDownMenu.prototype.selectNext = function() {
	if ( this.result != null )
		this.select(this.result.selected+1);
}

TSoftDropDownMenu.prototype.selectPrev = function() {
	if ( this.result != null )
		this.select(this.result.selected-1);
}

TSoftDropDownMenu.prototype.select = function(index) {
    if ( this.result != null && index < this.result.list.length && index >= 0 ) {
        this.container.childNodes.item(this.result.selected).className = (this.result.selected % 2 == 0 ? "oddRow" : "evenRow");
        this.result.selected = index;
        this.container.childNodes.item(this.result.selected).className = "highlightRow";
    }
}


TSoftDropDownMenu.prototype.close = function() {
	this.clearTimeout();
	if ( this.container != null )
       document.body.removeChild( this.container );
	this.container = null;
    this.result = null;
}

TSoftDropDownMenu.prototype.newItemOnMouseOver = function( i ) {
    var __this = this;
    return function() {
        __this.select(i);
    };
}

TSoftDropDownMenu.prototype.newItemOnMouseDown = function( i ) {
    var __this = this;
    return function() {
        var selected = __this.result.list[i];
        __this.confirmSelection();
    }
}

TSoftDropDownMenu.prototype.confirmSelection = function() {
    if ( this.result != null && this.result.list.length > this.result.selected ) {
    	this.clearTimeout();
        var selected = this.result.list[this.result.selected];
        this.loadImmedite("");
    	this.inputElement.value = selected.text;
        this.onSelect(selected, this.inputElement );
    }
}

TSoftDropDownMenu.prototype.onSelect = function(selected, inputElement) {
}

TSoftDropDownMenu.prototype.processRequest = function( searchString ) {
    return new Array();
}

TSoftDropDownMenu.prototype.clearTimeout = function() {
	if ( this.timeout != null ) {
		clearTimeout(this.timeout);
		this.timeout = null;
	}
}

TSoftDropDownMenu.prototype.load = function( newSearchString ) {
	this.clearTimeout();
	var __this = this;
	var __newSearchString = newSearchString;
	this.currentSearchString = newSearchString;
	var onTimeout = function() {
		__this.loadImmedite(__newSearchString);
	};
	this.timeout = setTimeout( onTimeout, 800 );
}

TSoftDropDownMenu.prototype.createSearchString = function( newSearchString ) {
	return newSearchString;
}

TSoftDropDownMenu.prototype.loadImmedite = function( newSearchString ) {
    if ( this.container == null )
        return;
    // if no result and no search string then do nothing
    if ( this.result == null && newSearchString == "" )
        return;
    var search = this.createSearchString( newSearchString );
    // if the result matches to the search string then do nothing
    if ( this.result != null && this.result.search == search )
        return;

    if ( newSearchString == "" ) {
        this.result = null;
        this.container.style.display = "none";
    } else {
	    var oldSelection = (this.result != null && this.result.list.length>0) ? this.result.list[this.result.selected].text : null;
	    var result = new Object();
	    result.search = search;
	    var data = this.processRequest( search );
	    result.list = data.list;
	    if ( this.createSearchString(this.currentSearchString) == search ) {
	        this.container.innerHTML = "";
	        result.selected = 0;
	        for ( var i = 0; i < result.list.length; i++ ) {
	            if ( result.list[i].text == oldSelection )
	                result.selected = i;
	        }
	        for ( var i = 0; i < result.list.length; i++ ) {
	            var partnerDiv = document.createElement("div");
	            partnerDiv.className = i == result.selected ? "highlightRow" : (i % 2 == 0 ? "oddRow" : "evenRow");
	            partnerDiv.appendChild( document.createTextNode( result.list[i].text ) );
	            partnerDiv.onmouseover = this.newItemOnMouseOver(i);
	            partnerDiv.onmousedown = this.newItemOnMouseDown(i);
	            this.container.appendChild( partnerDiv );
	        }
	        this.result = result;
	        this.container.style.display = this.result.list.length > 0 ? "block" : "none";
	        if ( data.message ) {
	        	var div = document.createElement("div");
	        	div.className = "message";
	        	this.container.appendChild( div );
	        	div.appendChild( document.createTextNode(data.message) );
	        }
	    }
    }
}



TSoftJavaScriptUtil.getPageSize = function(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;


    if (self.innerHeight) {    // all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }


    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }

    return { pageWidth : pageWidth, pageHeight : pageHeight, windowWidth : windowWidth, windowHeight : windowHeight } ;
}


TSoftJavaScriptUtil.getScrollOffset = function () {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return { x: scrOfX, y : scrOfY };
}



TSoftJavaScriptUtil.virtualDialogHolder = function() {
	
	var currentFrameWindow = window;
	
	var mainWindow = window;
	while ( mainWindow.parent && mainWindow.parent.window != mainWindow && mainWindow.parent.window.TSoftJavaScriptUtil ) {
		mainWindow = mainWindow.parent.window;
	}

	if ( mainWindow !== currentFrameWindow ) {
		// when the current frame winfow is not the main window
		var pvd = mainWindow.TSoftJavaScriptUtil.virtualDialogHolder;
		return {
			setSize:     function() { return pvd.setSize.apply(pvd, arguments);	}, // wrap function
			closeDialog: function() { return pvd.closeDialog.apply(pvd,arguments); }, // wrap function
			dialog:      function() { return pvd.dialog.apply(pvd,arguments); }, // wrap function
			openDialog:  function(url, width, height) {	return pvd.openDialog(url, width, height, currentFrameWindow );	}
		};
	}
	
	
	
	var virtualDialogArray = new Array();
	var virtualDialog = function () { var v = virtualDialogArray; return v.length > 0 ? v[v.length-1] : null; };


	var resize = function() {
		for ( var i = 0; i < virtualDialogArray.length; i++ )
			virtualDialogArray[i].align();
	}


	var close = function() {
		var d = virtualDialog();
		if ( d != null ) {
			d.container.parentNode.removeChild(d.container);
			virtualDialogArray.splice(virtualDialogArray.length-1,1);
		}
	};
	
	var setSize = function( width, height ) {
		var d = virtualDialog();
		if ( d != null )
			d.setSize(width, height);
	}
	
	TSoftJavaScriptUtil.addEventListener( window, "onresize", resize );

	return {
		setSize: setSize,
		dialog: function() { return virtualDialog(); },
		openDialog: function( url, width, height, opener ) {
			if ( true ) {
				var div = document.createElement("div");
				div.style.position="absolute";
				div.style.top= "0px";
				div.style.left="0px";

				var iframe = document.createElement("iframe");
				iframe.id = "windowiframe";
				iframe.style.width = width + "px";
				iframe.style.height = height + "px";
				iframe.style.border = "none";

				var headerTable = document.createElement("table");
				headerTable.border = "0";
				headerTable.cellSpacing = 0;
				headerTable.cellPadding = 0;
				headerTable.className = "dialogHeaderTable";
				var headerRow = headerTable.insertRow(0);
				headerRow.insertCell(0);
				headerRow.insertCell(0);

				headerRow.cells[0].className = "dialogWindowTitle";
				headerRow.cells[0].appendChild( document.createTextNode("\u00A0"));
				headerRow.cells[1].className = "dialogWindowButton";
				var closeButton = document.createElement("div");
				closeButton.onclick = close;
				closeButton.className = "dialogWindowClose";
				headerRow.cells[1].appendChild(closeButton);

				// Borders:
				var cellClasses = new Array(
					new Array( "dialogWindowHeader1",  "dialogWindowHeader2",  "dialogWindowHeader3" ),
					new Array( "dialogWindowContent1", "dialogWindowContent2", "dialogWindowContent3" ),
					new Array( "dialogWindowFooter1",  "dialogWindowFooter2",  "dialogWindowFooter3" )
				);

				var borderTable = document.createElement("table");
				div.appendChild(borderTable);
				borderTable.border = "0";
				borderTable.align = "center";
				borderTable.cellSpacing = 0;
				borderTable.cellPadding = 0;
				for ( var iRow = 0; iRow < cellClasses.length; iRow++ ) {
					var row = borderTable.insertRow(iRow);
					for ( var iCell = 0; iCell < cellClasses[iRow].length; iCell++ ) {
						var cell = row.insertCell(iCell);
						cell.className = cellClasses[iRow][iCell];
						if ( iCell != 1 || iRow != 1 ) {
							cell.appendChild(document.createElement("div"));
						} else {
							cell.appendChild(headerTable);
							cell.appendChild(iframe);
						}
					}
				}
				div.className = 'dialogFilter';
				var newDialog = {
						index: virtualDialogArray.length, 
						iframe: iframe,
						opener: (opener ? opener : window),
						container: div, 
						border: borderTable,
						url: url,
						width: width,
						height: height,
						align : function() {
							this.border.style.marginTop = Math.max(TSoftJavaScriptUtil.getPageSize().windowHeight - this.height - 50,0) / 2  + "px";
						},
						setSize: function( width, height ) {
							this.width = width;
							this.height = height;
							this.iframe.style.width = width + "px";
							this.iframe.style.height = height + "px";
							this.align();
						}
					
					};
				virtualDialogArray.push( newDialog );
				
				newDialog.container.style.position = "fixed";
				newDialog.container.style.top = "0px";
				newDialog.container.style.left = "0px";
				newDialog.container.style.height = TSoftJavaScriptUtil.getPageSize().pageHeight+"px";
				
				newDialog.iframe.src = url;
				newDialog.iframe.border = "0";
				newDialog.iframe.frameBorder = "0";
				newDialog.align();
				document.body.appendChild(newDialog.container);

				return true;
			} else {
				return false;
			}
		},
		closeDialog: close
	};
} ();

TSoftJavaScriptUtil.deletePicture = function( formName, elementName ) {
	var element = document.forms[formName].elements[elementName];
	element.value = "";
	while ( element.nodeName != 'TABLE' )
		element = element.parentNode;
	var image = element.rows[0].cells[0].firstChild; 
	image.src = "/kepek/transparent.gif";
}


var InputCollectionBasic = { 
		createHandler : function( id, page, startIndex, args ) {
				var nextIndex = startIndex;
				return function() {
					var i = nextIndex;
					nextIndex++;
					
					args["item_index"] = new String(i);
					var params = "";
					for ( var aname in args ) {
						if ( params.length > 0 )
							params += "&";
						params += aname + "=" + args[aname]; 
					}
					
					var xmlRes = TSoftJavaScriptUtil.xmlHttpResponse(page, params);
					//alert(xmlRes.responseText);
					var div = document.createElement("div");
					div.innerHTML = xmlRes.responseText;
					document.getElementById(id).appendChild(div);
				};
		}
};


