//================================================
//=== Instinctools(c) 2000-2004  						 
//===	Utils
//===	ver. 1.2
//===	requires dhtml.js, hashmap.js
//================================================

//=============================================================
//===	URL manipulation
//=============================================================

function parseURL(url) {
	var result = new Hashmap();
	var tokens = url.split('?');
	result.put('BASE_URL', tokens[0]);	
	
	if (tokens.length>1) {
		tokens = tokens[1].split('#');
		if (tokens.length>1) {
			result.put('ANCHOR_URL', tokens[1]);
		}
		tokens = tokens[0].split('&');
		for (var i in tokens) {
			var token = tokens[i];
			var pair = token.split('=');
			result.forcePut(pair[0], ((pair.length>1)? pair[1] : ''));
		}
	}
	return result;
}

function constructURL(hashmap) {
	var result = hashmap.get('BASE_URL');
	for (var i in hashmap.pairs) {
		var pair = hashmap.pairs[i];
		if (pair.key!='BASE_URL' && pair.key!='ANCHOR_URL') {
			var s = (result.indexOf('?')==-1)? '?' : '&';
			result += s+pair.key+'='+pair.value;
		}
	}
	var a = hashmap.get('ANCHOR_URL');
	if (a!=null) {
		result += '#'+a;
	}
	return result;
}

function removeParamsFromURL(url) {
	return url.split('?')[0];
}

//=============================================================
//===	Arrays manipulation
//=============================================================

function ArrayIndexOf(arr, obj) {
	for (var i in arr)
		if (arr[i]==obj)
			return i;
	return -1;
}

function cloneArray(source){
	
	if(source == null)
		return null;

	var dest = new Array(source.length);
	var element = null;
	
	for (var i in source) {
	
		element = source[i];
		
		if (element == null)
			dest[i] = element;
		else if (element.join)
			dest[i] = this.clone(element);
		else if (element.substring)
			eval('dest[i] = "'+element+'"');
		else
			eval('dest[i] = '+element);
	}
	
	return dest;
} 

function checkPopups(){
	if(top.popupDisabled && top.popupDisabled != '')
		alert(top.popupDisabled);
	else if(top.opener.top.popupDisabled && top.opener.top.popupDisabled != '')
		alert(top.opener.top.popupDisabled);
	return null;
}

//=============================================================
//===	Windows manipulation
//=============================================================
var checkNewWindowPos = true;
var checkNewWindowSize = true;

function openNewWindow(url, width, height, name, params) {	
	width = new Number(width);
	height = new Number(height);
	var screenWidth = screen.width;
	var screenHeight = screen.height;
	
	if (checkNewWindowSize) {
		if (width<1 || width>screenWidth) width = screenWidth;
		if (screen.availWidth && width>screen.availWidth) width=screen.availWidth;
		if (height<1 || height>screenHeight) height = screenHeight;
		if (screen.availHeight && height>screen.availHeight) height=screen.availHeight;
	}

	if (name==null)
		name='CMS';
	params = (params==null)? "width="+width+",height="+height : "width="+width+",height="+height+","+params; 
	var win = window.open("/blank.jsp",name, params);
	if(win == null){
		return checkPopups();
        }	
	
	var screenLeft = 0; 
	var screenTop = 0;
	
	screenLeft = (screenWidth)*0.5-width*0.5;
	if (navigator.userAgent.indexOf('Opera 7')>0) {
		win.moveTo(screenLeft, 0);
		screenHeight = win.screen.availHeight - win.screenTop;
	}
	screenTop = (screenHeight)*0.5-height*0.5;
	if (screenTop<0) screenTop=0;
	win.moveTo(screenLeft, screenTop);
	win.location.href = url;
	return win;
}

function openNewWindowWithCookie(url, defaultWidth, defaultHeight, name, defaultParams) {	
	//alert('!');
	var owidth = defaultWidth;
	var oheight = defaultHeight;
	var oleft = null;
	var otop = null;
	if (name==null)
		name='CMS';
    var cookieName = name;
	var cookie = getCookie(cookieName);
	if (cookie == '') {
		var expDate = new Date();
		if (expDate.getYear()<1000)
			expDate.setYear(expDate.getYear()+1901);
		else
			expDate.setYear(expDate.getYear()+1);
		setCookie(cookieName, defaultWidth+'_'+defaultHeight, expDate);
	} else {
		var params = cookie.split('_');
		owidth = params[0];
		oheight = params[1];
		if (params.length>3) {
			oleft = params[2];
			otop = params[3];
		}
	}
	
	if (oleft==null || otop==null)
		openNewWindow(url, owidth, oheight, name, defaultParams);
	else
		openNewWindowAt(url, owidth, oheight, oleft, otop, name, defaultParams);
}

function openNewWindowAt(url, width, height, left, top, name, params) {	
	width = new Number(width);
	height = new Number(height);
	left = new Number(left);
	top = new Number(top);
	var screenWidth = screen.width;
	var screenHeight = screen.height;
	
	if (checkNewWindowSize) {
		if (width<1 || width>screenWidth) width = screenWidth;
		if (screen.availWidth && width>screen.availWidth) width=screen.availWidth;
		if (height<1 || height>screenHeight) height = screenHeight;
		if (screen.availHeight && height>screen.availHeight) height=screen.availHeight;
	}
	
	if (checkNewWindowPos) {
		if (left<0) {
			left = 0;
		} 
		if (left+width>screenWidth) {
			left = screenWidth - width;
		}
		if (screen.availWidth && left+width>screen.availWidth) {
			left = screen.availWidth - width;
		}
		if (top<20) {
			top = 20;
		} 
		if (top+height>screenHeight) {
			top = screenHeight - height;
		}
		if (screen.availHeight && top+height>screen.availHeight) {
			top = screen.availHeight - height;
		}
	}
	if (name==null)
		name='CMS';
	params = (params==null)? "width="+width+",height="+height+",left=0,top=0" : "width="+width+",height="+height+",left=0,top=0,"+params; 
	var win = window.open("/blank.jsp",name, params);
	if(win == null){
		return checkPopups();
	}
	if (is.ie) {
		var dx = win.screenLeft;
		var dy = win.screenTop;
		left -= dx;
		top -= dy;
		win.resizeBy(-dx,-dx);
	}
	win.moveTo(left, top);
	
	win.location.href = url;
	return win;
}


var win;

function openDialog(url, width, height, name, params, passedParams) 
{
		var scr_x, scr_y, loc_x, loc_y;
		//var blank;
		var ie = ((navigator.appName.indexOf("Explorer") != -1) && (navigator.userAgent.indexOf("Opera") == -1));
		if(name==null) name="CMS";
		scr_x = screen.width;
		scr_y = screen.height;
		
		loc_x = (scr_x) * 0.5 - width * 0.5;
	    
		if (loc_y<0) loc_y=0;
		if(ie){
			if(params == null)params = "help=no;dialogWidth=" + width + "px;dialogHeight=" +height+"px;";
			else params="help=no;dialogWidth=" + width + "px;dialogHeight=" +height+"px;"+params;
			win = window.showModalDialog(url,passedParams,params);
			if(win == null){
				return checkPopups();
		        }
		}else{
			if(params == null)params = "width=" + width + ",height=" + height;
			else params = "width=" + width + ",height=" + height+","+params;
			win=window.open(url,name, params);
			if(win == null){
				return checkPopups();
		        }
			if (navigator.userAgent.indexOf('Opera 7')>0) {
				win.moveTo(screenLeft, 0);
				scr_y = win.screen.availHeight - win.screenTop;
			}
			loc_y = (scr_y)*0.5-height*0.5;
			win.moveTo(loc_x,loc_y);
			window.onfocus = checkModal;
			opener.onfocus = checkModal;
			parentFrames=new Array();
			var j=1;
			num =new Array();
			num[0]=top.frames;
			if(num.length>0){
				for(k=0;k<num.length;k++){
				parentFrames=num[k];
					for(i=0;i<parentFrames.length;i++){
						if(parentFrames[i].frames.length>0){
							num[j]=parentFrames[i].frames;
							j++;
						}else{
							parentFrames[i].document.onmousedown = checkModal;
						}
					}
				}
			}else{
				top.document.onmousedown = checkModal;
			}
		win.focus();
		}
return win;
}


function checkModal() {
	if (win && !win.closed ) {
		win.focus();
		
	}
}

var MAX_CHILD_FIELD_OWNER_RECURSION_LEVEL = 30;

function getChildNestedFieldOwner(fieldName, fromWin, curLevel) {
	var retResult = null;		
	if (fieldName!=null && fieldName!='') {
		if (fromWin==null) {
			fromWin = self;
		}
		if (curLevel==null) {
			curLevel = 1;
		}
		var objRef = eval('fromWin.'+fieldName);
		if (typeof objRef!='undefined') {
			retResult = fromWin;
		} else {
			var hasFrames = (fromWin.frames.length>0);
			if (hasFrames && curLevel<MAX_CHILD_FIELD_OWNER_RECURSION_LEVEL) {
				for (var i=0; i<fromWin.frames.length; i++) {
					retResult = getChildNestedFieldOwner(fieldName, fromWin.frames[i], curLevel+1);
					if (retResult!=null) {
						break;
					}
				}
			}
		}
	}
	return retResult;
}

