
var oneMinute = 60 * 1000
var oneHour = oneMinute * 60
var oneDay = oneHour * 24
var oneWeek = oneDay * 7

function Inherit( base, derived )
{
	for ( var i in base.prototype )
	{
		eval( 'derived.prototype.' + i + '= base.prototype.' + i )
	}
}
function DoCancel()
{
	window.top.close();
}

function PopWindow( sURL, sQueryString, sTitle, iWidth, iHeight )
{
	var w = window.top.document.body.clientWidth - 10;
	if ( iWidth ) w = iWidth;
	var h = window.top.document.body.clientHeight - 10;
	if ( iHeight ) h = iHeight;
	var t = window.top.document.body.clientTop;
	var l = window.top.document.body.clientLeft;

	if ( !sTitle )
		sTitle = 'Details'
		
	var sString = '../frames/DialogFrame.aspx?PageTitle=' + sTitle + '&ContentPage=' + sURL + '&ContentQueryString=' + encodeURIComponent( sQueryString )
	

	window.open( sString, '', 'height='+h+',width='+w+',scroll=no,menubar=no,resizable=yes' );	
}

function PopHTMLWindow( sURL, sQueryString, sTitle, iWidth, iHeight, wind )
{
	var w = window.top.document.body.clientWidth - 10;
	if ( iWidth ) w = iWidth;
	var h = window.top.document.body.clientHeight - 10;
	if ( iHeight ) h = iHeight;
	var t = window.top.document.body.clientTop;
	var l = window.top.document.body.clientLeft;

	if ( !sTitle )
		sTitle = 'Details'
		
	var sString = sURL + '?' + sQueryString 
	
	if (!wind ) wind = window;
	
	wind.top.open( sString, '', 'height='+h+',width='+l+',menubar=yes,resizable=yes,scroll=yes,scrollbars=yes' );	
}

function PopDialog( sURL, sQueryString, sTitle, iWidth, iHeight )
{
	var w = window.top.document.body.clientWidth - 10;
	if ( iWidth ) w = iWidth;
	var h = window.top.document.body.clientHeight - 10;
	if ( iHeight ) h = iHeight;
	var t = window.top.document.body.clientTop;
	var l = window.top.document.body.clientLeft;

	if ( !sTitle )
		sTitle = 'Details'
		
	var sString = '../frames/DialogFrame.aspx?PageTitle=' + sTitle + '&ContentPage=' + sURL + '&ContentQueryString=' + encodeURIComponent( sQueryString )
	

	if ( event != null && event.ctrlKey )
		window.open( sString, '', 'height='+h+',width='+w+',scroll=yes,menubar=no,resizable=yes,status=no' );	
	else
		return window.showModalDialog( sString, window, 'dialogHeight:'+h+'px;dialogWidth:'+w+'px;scroll:no;center:yes;resizable:no;status:no;help:no' );	
}


function PopDialogResizable( sURL, sQueryString, sTitle, iWidth, iHeight )
{
	var w = window.top.document.body.clientWidth - 10;
	if ( iWidth ) w = iWidth;
	var h = window.top.document.body.clientHeight - 10;
	if ( iHeight ) h = iHeight;
	var t = window.top.document.body.clientTop;
	var l = window.top.document.body.clientLeft;

	if ( !sTitle )
		sTitle = 'Details'
		
	var sString = '../frames/DialogFrame.aspx?PageTitle=' + sTitle + '&ContentPage=' + sURL + '&ContentQueryString=' + encodeURIComponent( sQueryString )
	

	if ( true )
		return window.showModalDialog( sString, window, 'dialogHeight:'+h+'px;dialogWidth:'+w+'px;scroll:no;center:yes;resizable:yes;status:no;help:no' );	
	else
		window.open( sString, '', 'height='+h+',width='+l+',scroll=no,menubar=no,resizable=yes,status=no' );	
}
 	
function CallFunction( sFrame, strFunction )
{
	var f = FindFrame( sFrame, window.top )
	if ( f != null )
		eval( 'f.' + strFunction )
}

var dbWin;

function Debug( str )
{
	if (! dbWin )
	{
		dbWin = window.open( "../Debug/Default.asp", null, "toolbar:no,height=430,width=600,menubar:no,resizable:no") 
	}

	dbWin.Debug( str )

}

function SetFrameURL( sURL, sFrame )
{
	FindFrame(sFrame).document.location.replace( sURL )
}

function FindFrame( sFrame, wind )
{
	
	if ( wind == null ) 
	{
		wind = window.top
	}

	if ( wind.frames != null )
	{
		var f = null
		for (var i=0; i<wind.frames.length;i++)
		{
			if ( wind.frames(i).name == sFrame )
			{
				f = wind.frames(i);
				break;
			}
		}
		
		if ( f != null )
			return f;
		else
		{
			for (var i=0; i<wind.frames.length;i++)
			{
				var w = wind.frames(i)
				f = FindFrame( sFrame, w )
				
				if ( f != null )
				{
					break;
				}
			}
			return f;			
		}
	}
	else
		return null;
}

function GetPageOffsetLeft(elt) 
{
	var x;
	x = elt.offsetLeft;
	if (elt.offsetParent != null)
		x += GetPageOffsetLeft(elt.offsetParent);
	return x;
}

function GetPageOffsetTop(elt) 
{
	var y;
	y = elt.offsetTop;
	if (elt.offsetParent != null)
		y += GetPageOffsetTop(elt.offsetParent);
	return y;
}

function getLocation(sIn)
{
	// returns URL without Querystring
	
	if (sIn.indexOf('?') != -1)
		return sIn.substr(0,sIn.indexOf('?'))
	else
		return sIn
}

function AddQueryString( sQuery, sName, sValue )
{
	//alert( 'query=' + sQuery + 'name=' + sName + 'value=' + sValue )
	sRes = StripQuery( sQuery, sName )

	//alert( 'strip=' + sRes )


	if ( sRes =='' || sRes =='?' )
		return sName + '=' + sValue
	else
		return sRes + '&' + sName + '=' + sValue
}

function RemoveQueryString( sQuery, sName )
{
	sRes = StripQuery( sQuery, sName )
	return sRes 
}

function frameURL( strURL, strSearch ) 
{
	//return '../frames/contentframe.asp?ContentPage=' + encodeURI( strURL ).replace( /&/g, '%26') + '&ContentQueryString=' + encodeURI( strSearch ).replace( /&/g, '%26')
	//return '../frames/contentframe.asp?ContentPage=' + strURL.replace( /&/g, '%26') + '&ContentQueryString=' + strSearch.replace( /&/g, '%26')
	return strURL + '?' + strSearch;
}

function StripQuery( sIn, sName )
{
	// write your own because different browsers do different things with regular expressions...
	
	var sincopy = sIn
	
	if ( sIn.substr(0,1) == '?' )
		sincopy = sIn.substr(1)
	
	var itms = sincopy.split( '&' )
	var res = ''

	for( var i=0; i< itms.length; i++ )
	{
		//alert( itms[i] )
		if (itms[i].substr(0, sName.length ).toUpperCase() != sName.toUpperCase() )
		{
			if ( res.length > 0 )
				res += '&'

			res = res + itms[i]
		}
	}

	return res
}

function StripQuery2( sIn, sName )
{
	//var re= RegExp( '(&|^)?.*?' + sName + '=.*?(&|$)?' , "g" )
	//var re= RegExp( '(^|&)' + sName + '.*?(?=&|$)' , "g" )
	//var re= RegExp( '(?:^|&)' + sName + '.*?(&|$)' , "g" )
	var re= RegExp( '[^&]*?' + sName + '.*?(&|$)' , "g" )

	var ret = sIn.replace( re, '')
	
	//alert(ret.substr( ret.length - 1))
		
	if (ret.substr( ret.length - 1 ) == '&' )
		return ret.substr( 0, ret.length - 1)
	else
		return ret
}

function setCookie (name, value, expires) 
{
	if ( expires )
		document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
	else
		document.cookie = name + "=" + escape(value) + "; path=/";		
}

function toggleDebugCookie()
{
	var exp = new Date();
	
	exp = exp.Add( 'd',1);
	
	if ( getCookie('MadgexDebug') != 'Off' )
	{
		setCookie('MadgexDebug','Off', exp)	
	}
	else
	{
		setCookie('MadgexDebug','On',exp)
	}
}

function getCookie(name) {

  var search;

  search = name + "=";
  offset = document.cookie.indexOf(search) ;
  if (offset != -1) {
    offset += search.length;
    end = document.cookie.indexOf(";", offset);
    if (end == -1)
      end = document.cookie.length;
    return unescape(document.cookie.substring(offset, end));
  }
  else
    return "";
}
function deleteCookie(name) {

  var expdate = new Date();
  expdate.setTime(expdate.getTime() - (86400 * 1000 * 1));
  setCookie(name, "", expdate);
}

function escapeJchars( str )
{
	var strRes = str.replace( /\\/g, '\\\\')
	strRes = strRes.replace( /\'/g, '\\\'')
	return strRes;
}


function Logout()
{
	var wind;
	
	if ( window.top.dialogArguments )
		wind = window.top.dialogArguments;
	else
		wind = window.top.opener;
	
	if(wind!=null)
	{
		setTimeout('LogoutCW()',1);
		wind.Logout();
		
	} else {
		window.top.location = '../logon/logon.aspx';
	}
	
}

function Opener( wind )
{
	if ( wind.dialogArguments )
		return wind.dialogArguments;
	else
		return wind.opener;
}

function LogoutCW()
{
	window.top.close()
}

function Is () {
	var agt=navigator.userAgent.toLowerCase();
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
	this.nav2 = (this.nav && (this.major == 2));
	this.nav3 = (this.nav && (this.major == 3));
	this.nav4 = (this.nav && (this.major == 4));
	this.nav4up = (this.nav && (this.major >= 4));
	this.navonly = (this.nav && ((agt.indexOf(";nav") != -1) ||(agt.indexOf("; nav") != -1)) );
	this.nav6 = (this.nav && (this.major == 5));
	this.nav6up = (this.nav && (this.major >= 5));
	this.gecko = (agt.indexOf('gecko') != -1);
    this.ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    this.ie3    = (this.ie && (this.major < 4));
    this.ie4    = (this.ie && (this.major == 4) && (agt.indexOf("msie 4")!=-1) );
    this.ie4up  = (this.ie  && (this.major >= 4));
    this.ie5    = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    this.ie5_5  = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.5") !=-1));
    this.ie5up  = (this.ie  && !this.ie3 && !this.ie4);
    this.ie5_5up =(this.ie && !this.ie3 && !this.ie4 && !this.ie5);
    this.ie6    = (this.ie && (this.major == 4) && (agt.indexOf("msie 6.")!=-1) );
    this.ie6up  = (this.ie  && !this.ie3 && !this.ie4 && !this.ie5 && !this.ie5_5);
    this.win	= ((agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1));
    this.mac    = (agt.indexOf("mac")!=-1);
    this.menu = false;   
}

var is = new Is();

if (is.nav6up) sClassRef = "class" 
if (is.ie5up) sClassRef = "className" 

function Doc()
{
	this.m_Text = new String
}

function AutoResizePopup()
{

	// Different for dialogs and windows
	
	if ( window.top.dialogArguments )
	{
		
		
		var wind;
		if ( window.top.dialogArguments )
			wind = window.top.dialogArguments;
		else
			wind = window.top.opener;
				
		var h;
		var w;
		var bReset=false;
		
		if(document.all('PropertiesContent') != null)
		{
//			alert('i am here');
			h = document.all('PropertiesContent').clientHeight+100;
			w = document.all('PropertiesContent').clientWidth;
			bReset=true;
	
//			alert('prop h:' + h + '  w:' + w);	
		}
		
		if(document.all('StatusContent') != null)
		{
			h = document.all('StatusContent').clientHeight;
			w = document.all('StatusContent').clientWidth;
			
			h=330;
			w=400;
			bReset=true;
//			alert('status h:' + h + '  w:' + w);
		}
		
		if(bReset==true)
		{
//			alert('status h:' + h + '  w:' + w);
			// it's a dialog
			window.top.dialogHeight = h + 'px';
			window.top.dialogWidth = w + 'px';
		//	alert('autoresize');
		}
	}
	else
	{
		// it's a window
	}
}

Doc.prototype.writeln = function( strAdd )
{
	this.m_Text += strAdd + '\n';
}

Doc.prototype.write = function( strAdd )
{
	this.m_Text += strAdd ;
}

String.prototype.Trim = function()
{
	return this.TrimLeft().TrimRight();
}
