var messageLoad = '<html><head><title>Please wait...</title><script language="JavaScript" src="xsl/js/general.js"></script></head><body style=\"background-color: #FFFFFF; color: #000000; cursor: wait\" topmargin=\"0\" leftmargin=\"0\" marginwidth=\"0\" marginheight=\"0\"><div align=\"center\" style=\"font-size:14pt; font-family:Arial, Helvetica, sans-serif\"><br/>Please wait while the page loads<marquee direction=\"left\" width=\"30\" height=\"10\" loop=\"\" scrolldelay=\"100\">. . .</marquee></div></body></html>',
	messageUpdate = '<html><head><script language="JavaScript" src="xsl/js/general.js"></script></head><body style=\"background-color: #FFFFFF; color: #000000; cursor: wait\" topmargin=\"0\" leftmargin=\"0\" marginwidth=\"0\" marginheight=\"0\"><div align=\"center\" style=\"font-size:14pt; font-family:Arial, Helvetica, sans-serif\"><br/>Please wait while the database is updated...</div></body></html>',
	submitted = 1,
	onCloseUserHandler = '',
	theForm = null,
	isWindowPushed = false;
var winNameGlobal = '', fromWindowGlobal = '';
var nav8=window.navigator.userAgent.indexOf('Netscape/8')>0 ? true : false;
var nav4 = window.Event ? true : false;
var woPopupCheck;
function get_correct_form_number()
{
    if(typeof correct_form_number == 'undefined') return 0;
    if(correct_form_number > 0)
    {
        return correct_form_number;
    }
    return 0;
}
function popWindow() {
    var frm_number = get_correct_form_number();
	if (!theForm) theForm = document.forms[frm_number];
	var winName = '';
	if (theForm.prevWindowName && theForm.prevWindowName.value.length) {
		var pos = theForm.prevWindowName.value.lastIndexOf(';');
		if (pos >= 0) {
			winName = theForm.prevWindowName.value.substr(pos+1);
			theForm.prevWindowName.value = theForm.prevWindowName.value.substr(0, pos);
		} else {
			winName = theForm.prevWindowName.value;
			theForm.prevWindowName.value = '';
		}
	}
	return winName;
}

function pushWindow(name) {
    var frm_number = get_correct_form_number();
	if (!theForm) theForm = document.forms[frm_number];
    if (theForm.prevWindowName) {
    	if (theForm.prevWindowName.value.length) theForm.prevWindowName.value += ';' + name;
	    else theForm.prevWindowName.value = name;
    }
}
		
// winParams:
// scroll = 1/0
// resize  = 1/0
function processSubmit(winName, width, height, winParams, fromWindow, formObj) {
	winNameGlobal = winName; 
	fromWindowGlobal = fromWindow;
	if (isWindowPushed) { popWindow(); isWindowPushed = false; }
	if (!checkSessionExist()) return;
    var frm_number = get_correct_form_number();
	theForm = formObj ? formObj : document.forms[frm_number];
	if (winName && winName != 'same' && winName != '_top' && winName!='woLoadingMessage') {
		if (!width) width = 800;
		if (!height) height = 600;
		if (screen.availWidth <= width) width = screen.availWidth-10;
		if (screen.availHeight <= height) height = screen.availHeight-30;
		// Get and process winParams
		winParams = winParams ? winParams.trim() : '';
		if (winParams) {
			winParamsArr = winParams.split(',');
			winParams = '';
			for (var i=0; i<winParamsArr.length; i++) if (winParamsArr[i].trim()) {
				param = winParamsArr[i].trim().split('=');
				if (param.length == 2) {				
					param[0] = param[0].trim();
					param[1] = param[1].trim();
					if (param[0] == 'scroll' && (param[1] == '1' || param[1] == '0' || param[1] == '2')) winParams += (winParams.length ? ',' : '') + 'scrollbars=' + (param[1] == '1' ? 'yes' : (param[1] == '0' ? 'no' : 'auto'));
					else if (param[0] == 'resize' && (param[1] == '1' || param[1] == '0')) winParams += (winParams.length ? ',' : '') + 'resizable=' + (param[1] == '1' ? 'yes' : 'no');
				}	
			}			
		}

		if (!winParams) winParams = 'scrollbars=yes,resizable=yes';
		winParams += ',width='+width+',height='+height+',status=yes,top=0,left=0';

		// Open new window
		var baseWin = _getBaseWindow();
		addChildWindowLocal(winName);
		theForm.target = winName;
		window.defaultOnError = window.onerror;
		window.onerror = handleProcessSubmitError;
		var openedWindow = baseWin.getWindowObj(winName);
		if (openedWindow && !openedWindow.closed && openedWindow.submitted) openedWindow.submitted = 2;
		var openedWindow = baseWin.openWindow('empty.html', winName, winParams);
		window.onerror = window.defaultOnError;
		if (nav4) for (i=0; i<500; i++) { a=1; }
		else for (i=0; i<100000; i++) { a=1; }
	} else {
        if(winName!='woLoadingMessage') {
    		if (document.getElementById('tcmain')) document.getElementById('tcmain').style.visibility='hidden';
    		if (document.getElementById('main')) document.getElementById('main').style.visibility='hidden';
    		if (document.getElementById('loading')) {
    			document.getElementById('loading').style.display='';
    			document.getElementById('loading').style.visibility='visible';
    		}
        }
		if (winName && winName == '_top') {
			theForm.target = '_top';
                        try {
                            if (topObj.submitted) topObj.submitted = isBaseWindow ? 1 : 2;
                        } catch(e) {}
		} else {
			theForm.target = '_self';
			submitted = isBaseWindow ? 1 : 2;
		}
		if (window.navigator.platform.indexOf('Mac') == -1 && !nav4) document.styleSheets[document.styleSheets.length-1].addRule('body', 'cursor: wait');
	}
	if (winName && winName != '_top') { pushWindow(fromWindow ? fromWindow : window.name); isWindowPushed = true; }

	theForm.submitted = true;
	if(baseUrl && theForm && typeof(theForm.action)!='object' && theForm.act) {
        theForm.action=baseUrl+'index.php?'+theForm.act.value;
    }
    try{
	theForm.submit();
    }catch(e){}
}

// params:
// noWaitMessage = 1/0
function processReturn(formObj, params) {

	if (isWindowPushed) { popWindow(); isWindowPushed = false; }
	if (!checkSessionExist()) return;
	var param = [];
	if (params && params.length) {
		for (var i=0; i<params.length; i++) if (params[i].length==2) param[params[i][0].trim()] = params[i][1];
	}
    var frm_number = get_correct_form_number();
	theForm = formObj ? formObj : document.forms[frm_number];
	winName = popWindow();
    if (winName == window.name) {
        winName = popWindow();
    }

	if (winName != window.name) {
		topObj.onunload = null;

		closeWindowLocal();

		theForm.target = winName;

		var prevWindow = _getParentWindowByName(theForm.target);

		if (prevWindow) {
			if (prevWindow.submitted) prevWindow.submitted = 2;
			if (!param['noWaitMessage'] || param['noWaitMessage'] != 1) {
				if (window.navigator.platform.indexOf('Mac') == -1 && !nav8) {
					prevWindow.document.open();
					prevWindow.document.write(theForm.prevWindowName.value.length ? messageLoad : messageUpdate);
					prevWindow.document.close();
				}
			}
		}
	} else {
		theForm.target = '_self';
	}

	submitted = 2;
	theForm.submitted = true
	if (winName != window.name) {
		topObj.submitted = 3;
		if(param['reloadInsteadPost'] == 1) {
            var fn = prevWindow.get_correct_form_number();
            var pForm = prevWindow.document.forms[fn];
            var phpsid = pForm.elements['PHPSESSID'] ? pForm.elements['PHPSESSID'].value : theForm.elements['PHPSESSID'].value;
            var className = pForm.elements['class'].value;
            var recID = pForm.elements['recruiterID'].value;
            var loc = prevWindow.location+'';
            loc = prevWindow.location+(loc.indexOf('&') == -1 ? '?' : '&')+'class='+className+'&recruiterID='+recID+'&PHPSESSID='+phpsid;
            prevWindow.location = loc;
        } else if ((window.navigator.platform.indexOf('Mac') == -1 || nav4) && theForm.closeWindowWithName && !nav8) {
			theForm.closeWindowWithName.value=window.name
			theForm.submit();
            window.close();
		} else {
			if(nav8) {
                theForm.target = '_parent';
                window.setTimeout('_forNav8()', 1000);
            } else {
    			theForm.submit();
                window.close()
            }
		}
	}
}

function _forNav8() {
    var frm_number = get_correct_form_number();
    document.forms[frm_number].submit();
    window.close();
}
function addChildWindowLocal(winName) {
	var baseWin = _getBaseWindow();
	if (baseWin) baseWin.addChildWindow(baseWin.name == window.name ? window.name : topObj.name, winName);
}

function closeWindowLocal() {
    try {
        var baseWin = _getBaseWindow();
        if (baseWin) baseWin.closeWindow(topObj.window.name);
    } catch(e) {}
}

function checkSessionExist() {
    if(woPopupCheck) return true;
    var frm_number = get_correct_form_number();
	var theForm = document.forms[frm_number];
	window.defaultOnError = window.onerror;
	window.onerror = handleCheckSessionError;
	var baseWin = _getBaseWindow();
	var baseWinSid = (baseWin && baseWin.sid ? baseWin.sid : false);
	window.onerror = window.defaultOnError;
	if (sid) currentSid = sid
	else if (theForm && theForm.sid) currentSid = theForm.sid.value;
	if (baseWinSid && currentSid && baseWinSid == currentSid) {
		return true;
	} else if (window.name!=baseWindow) {
		topObj.onunload = null;
		window.close();
		return false;
	}
}	

function handleCheckSessionError(err, url, line) {
    try {
        if (err.indexOf('denied') != -1) {
            topObj.onunload = null;
            return true;
        } else {
            return false;
        }
    } catch(e) {
        return false;
    }
}

function onCloseHandler() {
	if (!checkSessionExist()) return;
	if (topObj.submitted == 1 || topObj.submitted == 3) closeWindowLocal();
	if (topObj.submitted == 1 && onCloseUserHandler) eval(onCloseUserHandler + '()');
}	

function setOnCloseUserHandler(userHandler) { onCloseUserHandler = userHandler }

var isBaseWindow = _isBaseWindow(window);
var topObj = isBaseWindow ? window : _getTopWindow(window);
try {
    topObj.onunload = onCloseHandler;
} catch(e) {}

function _getTopWindow(winObj) {
	while (typeof(winObj.parent) == 'object' && winObj.parent != winObj) winObj = winObj.parent
	return winObj;
}

function _getParentWindowByName(name) {
	var isFound = false;
	var winObj = window.opener;
	for (var i=0; i<100; i++) {
		if (winObj) {
			if (winObj.name == name) {
				isFound = true;
				break;
			}
			if (_isBaseWindow(winObj)) break;
            if (!winObj.opener) break;
			winObj = winObj.opener;
		}
	}
	return isFound ? winObj : false;
}

function _getBaseWindow() {
	var winObj = window;
	for (var i=0; i<100; i++) {
		if (_isBaseWindow(winObj)) break;
        if (!winObj.opener) break;
        winObj = winObj.opener;
	}
	return winObj;
}

function _isBaseWindow(winObj) {
    if(woPopupCheck) return true;
    window.defaultOnError = window.onerror;
    window.onerror = handleAccessDeniedError;

    result=(!winObj.name || !winObj.opener || typeof(winObj.opener) != 'object' || !winObj.opener.name || winObj.opener.name == winObj.name);
    if(result && winObj.name &&
       ((winObj.parent && winObj.parent.frames[winObj.name]) || (winObj.top && winObj.top.frames[winObj.name])) ) {
        result = false;
    }

    window.onerror = window.defaultOnError;
	return result;
}

//from eC login.jsinc
var openedWindows = [], closedWindows = [], openedWindowObjs = []

function addChildWindow(parentName, childName) {
	for (pName in openedWindows) if (pName != 'contains' && pName != 'contain') {
		if (pName == parentName) {
			if (!lib_contains(openedWindows[parentName], childName)) openedWindows[parentName][openedWindows[parentName].length] = childName;
			return;
		}
	}
	openedWindows[parentName] = [childName];
}

function openWindow(url, winName, winParams) {
	openedWindowObjs[winName] = window.open(url, winName, winParams);
	openedWindowObjs[winName].name = winName;
	return openedWindowObjs[winName];
}

function closeWindow(winName) {
	closedWindows = [];
	closedWindows[closedWindows.length++] = winName;
	closeChildWindows(winName);
	var openedWindowsNew = [];
	for (var pName in openedWindows) if (pName != 'contains' && pName != 'contain') {
		if (!lib_contains(closedWindows, pName)) {
			for (var i=0; i<openedWindows[pName].length; i++) if (!lib_contains(closedWindows, openedWindows[pName][i])) {
				if (!openedWindowsNew[pName]) openedWindowsNew[pName] = [];
				openedWindowsNew[pName][openedWindowsNew[pName].length] = openedWindows[pName][i];					
			}
		}
	}
	openedWindows = openedWindowsNew;
}
				
function closeChildWindows(parentName) {
	for (var pName in openedWindows) if (pName != 'contains' && pName != 'contain') {
		if (pName == parentName) {
			for (var i=0; i<openedWindows[pName].length; i++) {
//				closeChildWindows(openedWindows[pName][i]);
				if (openedWindowObjs[openedWindows[pName][i]] && !openedWindowObjs[openedWindows[pName][i]].closed) {
					if (openedWindowObjs[openedWindows[pName][i]].submitted) openedWindowObjs[openedWindows[pName][i]].submitted = 2;
					openedWindowObjs[openedWindows[pName][i]].close();
				}
				closedWindows[closedWindows.length] = openedWindows[pName][i];
			}
			break;
		}
	}
}

function printArray(arr) {		
	var str = '';
	for (param1 in arr) if (param1 != 'contains' && param1 != 'contain') {
		var str1 = '';
		for (var i=0; i<arr[param1].length; i++) {
			str1 += (str1.length ? ',' : '') + arr[param1][i];
		}
		str += (str.length ? ' ; ' : '') + param1 + ' : ' + str1;
	}
	return str;
}

function printSimpleArray(arr) {
	var str = '';
	for (var i=0; i<arr.length; i++) {
		str += (str.length ? ',' : '') + arr[i];
	}
	return str;
}

function handleProcessSubmitError (err, url, line) {
	if (err.indexOf('denied') != -1) {
		_delay();
		_processSubmit(winNameGlobal, fromWindowGlobal);
		return true;
	} else {
		return false;
	}
}

function handleAccessDeniedError (err, url, line) {
	if (err.indexOf('denied') != -1) {
		return true;
	} else {
		return false;
	}
}

function getWindowObj(winName) {
	for (var pName in openedWindowObjs) if (pName == winName) return openedWindowObjs[winName];
	return null;
}

if(typeof(isInactivityAlreadySet) == 'undefined') {
    var isInactivityAlreadySet=0;
}

if(!isInactivityAlreadySet) {
    tryInactivityLogoutReset_oldLoadHandler = window.onload ? window.onload : new Function;
    window.onload = function() { 
        var oldBodyOnclick = document.body.onclick ? document.body.onclick : new Function; 
        document.body.onclick = function() { 
            try { inactivityLogoutObj.do_reset(true); } catch (e) {} 
            try { window.frames.top.inactivityLogoutObj.do_reset(true); } catch (e) {}
            oldBodyOnclick(); 
        };  
        var oldBodyKeyPress = document.body.onkeypress ? document.body.onkeypress : new Function; 
        document.body.onkeypress = function() { 
            try { inactivityLogoutObj.do_reset(true); } catch (e) {} 
            try { window.frames.top.inactivityLogoutObj.do_reset(true); } catch (e) {}
            oldBodyKeyPress(); 
        };  
        tryInactivityLogoutReset_oldLoadHandler(); 
    };
    isInactivityAlreadySet=1;
}

