var isIE = (navigator.appName.indexOf("Microsoft") != -1);
function closeOnEscape(evt,toconfirm,msg){
	var key=isIE?event.keyCode:evt.which;
	if (key==27)
		{	var retVal=true;
			if (toconfirm) retVal=window.confirm(msg?msg:'Close Window?');
			if (retVal)
				window.close();
		}
	return;
}

function popupWindow(url, name, width, height) {
	if (width > screen.availWidth)
		width = screen.availWidth;
	if (height > screen.availHeight - 60)
		height = screen.availHeight - 60;
	var top_pos = (screen.availHeight - height) / 2;
	var left_pos = (screen.availWidth - width) / 2;
								
	var wnd = window.open(url, name, 'toolbar=0,resizable=1,height=' + height + ',width=' + width + ',top=' + top_pos + ',left=' + left_pos);
	wnd.focus();
	return wnd;
}
function popupScrollWindow(url, name, width, height) {
	if (width > screen.availWidth)
		width = screen.availWidth;
	if (height > screen.availHeight - 60)
		height = screen.availHeight - 60;
	var top_pos = (screen.availHeight - height) / 2;
	var left_pos = (screen.availWidth - width) / 2;

	var wnd = window.open(url, name, 'toolbar=0,resizable=1,scrollbars=1,height=' + height + ',width=' + width + ',top=' + top_pos + ',left=' + left_pos);
	wnd.focus();
	return wnd;
}
function popupWindowEx(url, name, width, height, options) {
	if (width > screen.availWidth)
		width = screen.availWidth;
	if (height > screen.availHeight - 60)
		height = screen.availHeight - 60;
	var top_pos = (screen.availHeight - height) / 2;
	var left_pos = (screen.availWidth - width) / 2;
								
	var wnd = window.open(url, name, options + ',height=' + height + ',width=' + width + ',top=' + top_pos + ',left=' + left_pos);
	wnd.focus();
	return wnd;
}

function urlAddArgRedirect(arg) {
	if (location.search.length == 0)
		location.href = location.pathname + '?' + arg;
	else
		location.href = location.pathname + location.search + '&' + arg;
}
function urlReplaceArgsRedirect(replaceArgs) {
	//Get name/value arrays for current args
	var curNames = new Array();
	var curValues = new Array();
	getArgsNameValuePairs(location.search, curNames, curValues);

	//Get name/value arrays for new args
	var newNames = new Array();
	var newValues = new Array();
	getArgsNameValuePairs(replaceArgs, newNames, newValues);

	var args = '?';
	//Iterate through each current arg
	for (curIx = 0; curIx < curNames.length; curIx++) {
		//Search for name match in new args
		for (newIx = 0; newIx < newNames.length && newNames[newIx] != curNames[curIx]; newIx++);
		if (newIx < newNames.length) {
			//Match found, assign new value
			args += (args.length > 1 ? '&' : '') + curNames[curIx] + '=' + newValues[newIx];
			//Clear to indicate arg used
			newNames[newIx] = null;
		} else
			//No match, use orignal value
			args += (args.length > 1 ? '&' : '') + curNames[curIx] + '=' + curValues[curIx];
	}
	//Iterate through new args to see if any not matched
	for (newIx = 0; newIx < newNames.length; newIx++)
		if (newNames[newIx])
			//Not matched, add to args
			args += (args.length > 1 ? '&' : '') + newNames[newIx] + '=' + newValues[newIx];
	
	location.href = location.pathname + args
}
function getArgsNameValuePairs(args, names, values) {
	//Skip over first char if ? or &
	var posStart;
	if (args.substr(0, 1) == '?' || args.substr(0, 1) == '&')
		posStart = 1;
	else
		posStart = 0;
	
	//Iterate through args
	var posNameEnd, posValueEnd;
	while (posStart < args.length) {
		posNameEnd = args.indexOf('=', posStart);
		if (posNameEnd == -1) {
			//Couldn't find start of value, use rest of line as name, value will be ''
			posNameEnd = args.length;
			posValueEnd = posNameEnd;
		} else {
			posValueEnd = args.indexOf('&', posNameEnd + 1);
			if (posValueEnd == -1)
				//Must be last arg, use rest of line as value
				posValueEnd = args.length;
		}
		names[names.length] = args.substr(posStart, posNameEnd - posStart);
		values[values.length] = args.substr(posNameEnd + 1, posValueEnd - posNameEnd - 1);
		
		posStart = posValueEnd + 1;
	}
}

function navigateWithArgs(page, arg) {
	if (location.search.length == 0)
		location.href = page + '?' + arg;
	else
		location.href = page + location.search + '&' + arg;
}

function getURLWithoutArg(url, argName) {
	//Find start position of arg (check for ? and &)
	var pos1 = url.indexOf('?' + argName + '=');
	if (pos1 == -1)
		pos1 = url.indexOf('&' + argName + '=');
	if (pos1 == -1)
		//Arg not on query string
		return url;
	//Find end position of arg value, use length of string if not found
	pos2 = url.indexOf('&', pos1 + 1);
	if (pos2 == -1) pos2 = url.length;
	//Return the value
	if (pos1 == 0)
		//Replaced arg was first, replace separator of subsequent args with ?
		return '?' + url.substr(pos2 + 1);
	else
		return url.substring(0, pos1) + url.substr(pos2);
}
//These functions parse the query string for specified arguments
function getQSWithoutArg(argName) {
	return getURLWithoutArg(location.search, argName);
}
function getArgPair(argName) {
	//Find start position of arg (check for ? and &)
	var pos1 = location.search.indexOf('?' + argName + '=');
	if (pos1 == -1)
		pos1 = location.search.indexOf('&' + argName + '=');
	if (pos1 == -1)
		return argName + '=';
	//Find end position of arg value, use length of string if not found
	pos2 = location.search.indexOf('&', pos1 + 1);
	if (pos2 == -1) pos2 = location.search.length;
	//Return the value
	return location.search.substring(pos1 + 1, pos2);
}
function getArgValue(argName) {
	//Find start position of arg (check for ? and &)
	var pos1 = location.search.indexOf('?' + argName + '=');
	if (pos1 == -1)
		pos1 = location.search.indexOf('&' + argName + '=');
	if (pos1 == -1)
		return '';
	//Find end position of arg value, use length of string if not found
	pos2 = location.search.indexOf('&', pos1 + 1);
	if (pos2 == -1) pos2 = location.search.length;
	//Return the value
	return location.search.substring(pos1 + 2 + argName.length, pos2);
}
function trimString(str) {
	str = trim(str);
	return str;
}
function trim(str) {
	return str.replace(/^\s+/, '').replace(/\s+$/, '');
}

function activateFirstCtrl()
{
	if (document.forms.length == 0)
		return;
	//Find first control that has tabIndex = 1 and can receive focus
	var frm = document.forms[0];
	var ctlFocus;
	for (var i = 0; i < frm.elements.length; i++)
	{
		var ctl = frm.elements[i];
		if (!ctl.disabled && ctl.type != 'hidden')
		{
		    //Navigate parent tree to verify visibility
		    var isHidden = false;
		    for(var elm = ctl; elm && !isHidden; elm = elm.parentNode)
		        isHidden = (elm.style && elm.style.visibility == 'hidden');

		    if (!isHidden)
		    {
		        if (ctl.tabIndex == 1)
		        {
		            ctlFocus = ctl;
		            break;
		        }
		    }
		}
	}
	if (ctlFocus)
	{
        //Activate the control
        ctlFocus.focus();
        //Select text if control is textbox
        if (ctlFocus.type == 'text' && !ctlFocus.readOnly)
	        ctlFocus.select();
    }
}

function ValidateInt(field, allowBlank)
{
    var msg = '';
    var value = field.value = trim(field.value);
    if (value == '')
    {
        //No data entered
        if (allowBlank)
            return true;
        else
            return ValidateRequired(field);
    }
    else
    {
        var regEx = /^\d+$/;
        if (!regEx.test(value))
            msg = 'An integer value is expected.';
    }
    if (msg != '')
    {
        field.focus();
        field.select();
        alert(msg);
        return false;
    }
    else
        return true;
}

function ValidateRequired(field, errMsg)
{
    var msg = '';
    var value = trimString(field.value);
    if (value == '')
    {
        field.focus();
        if (errMsg == undefined)
            errMsg = 'An input is required for this field.';
        alert(errMsg);
        return false;
    }
    else
        return true;
}
