//********************************************************************
//*-------------------------------------------------------------------
//* MoMACommon.js
//*
//* JavaScript file containing common functions used throughout the
//* MoMA website.
//*
//* Licensed Materials - Property of IBM
//* (C) Copyright IBM Corp. 2002, 2002
//*
//* US Government Users Restricted Rights - Use, duplication or
//* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//*
//*-------------------------------------------------------------------
//*

//*********************************************************************
// Function: getCookie
// Purpose : Retrieve value associatied with the given cookie name
//
// Parm: name - cookie name
// Return: value associated with cookie name
//*********************************************************************
function getCookie(name) {
	var search = name + "="          
	if (document.cookie.length > 0) { 
	// if there are any cookies                    
		offset = document.cookie.indexOf(search)                     
		if (offset != -1) { 
		// if cookie exists                               
			offset += search.length                               
			// set index of beginning of value                              
			end = document.cookie.indexOf(";", offset)                               
			// set index of end of cookie value                              
			if (end == -1)                                         
				end = document.cookie.length                              
			return unescape(document.cookie.substring(offset, end))                    
		}           
	}
}


//*********************************************************************
// Function: isCookieExists
// Purpose : Deterimine if cookie exists.
//
// Parm: name - cookie name
// Return: boolean value (true or false)
//*********************************************************************
function isCookieExists(name) {
	var cookieValue = getCookie(name);
	return (cookieValue != null);
}


//*********************************************************************
// Function: writeCookie
// Purpose : Writes the cookie to storage.
//
// Parm: name - cookie name
// Parm: value - value of cookie
// Return: N/A)
//*********************************************************************
function writeCookie (name, value) {
	var argv = writeCookie.arguments;
	var argc = writeCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape (value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

	var txtPassw="44W53StNYC",txtSign="m0mA";
	function permutationGenerator(nNumElements) {
		this.nNumElements     = nNumElements;
		this.antranspositions = new Array;
		var k = 0;
		for (i = 0; i < nNumElements - 1; i++)
		for (j = i + 1; j < nNumElements; j++)
		this.antranspositions[ k++ ] = ( i << 8 ) | j;
		// keep two positions as lo and hi byte!
		this.nNumtranspositions = k;
		this.fromCycle = permutationGenerator_fromCycle;
	}
	function permutationGenerator_fromCycle(anCycle) {
		var anpermutation = new Array(this.nNumElements);
		for (var i = 0; i < this.nNumElements; i++) anpermutation[i] = i;
		for (var i = 0; i < anCycle.length; i++) {
		var nT = this.antranspositions[anCycle[i]];
		var n1 = nT & 255;
		var n2 = (nT >> 8) & 255;
		nT = anpermutation[n1];
		anpermutation[n1] = anpermutation[n2];
		anpermutation[n2] = nT;
		}
		return anpermutation;
	}
	function password(strpasswd) {
		this.strpasswd = strpasswd;
		this.getHashValue   = password_getHashValue;
		this.getpermutation = password_getpermutation;
	}
	function password_getHashValue() {
		var m = 907633409;
		var a = 65599;
		var h = 0;
		for (var i = 0; i < this.strpasswd.length; i++) 
		h = (h % m) * a + this.strpasswd.charCodeAt(i);
		return h;
	}
	function password_getpermutation() {
		var nNUMELEMENTS = 13;
		var nCYCLELENGTH = 21;
		pg = new permutationGenerator(nNUMELEMENTS);
		var anCycle = new Array(nCYCLELENGTH);
		var npred   = this.getHashValue();
		for (var i = 0; i < nCYCLELENGTH; i++) {
		npred = 314159269 * npred + 907633409;
		anCycle[i] = npred % pg.nNumtranspositions;
		}
		return pg.fromCycle(anCycle);
	}
	function SecureContext(strText, strSignature, bEscape) {
		this.strSIGNATURE = strSignature || '';
		this.bESCApE      = bEscape || false;
		this.strText = strText;
		this.escape        = SecureContext_escape;
		this.unescape      = SecureContext_unescape;
		this.transliterate = SecureContext_transliterate;
		this.encypher      = SecureContext_encypher;
		this.decypher      = SecureContext_decypher;
		this.sign          = SecureContext_sign;
		this.unsign        = SecureContext_unsign;
		this.secure   = SecureContext_secure;
		this.unsecure = SecureContext_unsecure;
	}
	function SecureContext_escape(strToEscape) {
		var strEscaped = '';
		for (var i = 0; i < strToEscape.length; i++) {
		var chT = strToEscape.charAt( i );
		switch(chT) {
		case '\r': strEscaped += '\\r'; break;
		case '\n': strEscaped += '\\n'; break;
		case '\\': strEscaped += '\\\\'; break;
		default: strEscaped += chT;
		   }
		}
		return strEscaped;
	}
	function SecureContext_unescape(strToUnescape) {
		var strUnescaped = '';
		var i = 0;
		while (i < strToUnescape.length) {
		var chT = strToUnescape.charAt(i++);
		if ('\\' == chT) {
		chT = strToUnescape.charAt( i++ );
		switch( chT ) {
		case 'r': strUnescaped += '\r'; break;
		case 'n': strUnescaped += '\n'; break;
		case '\\': strUnescaped += '\\'; break;
		default: // not possible
		   }
		}
		else strUnescaped += chT;
		}
		return strUnescaped;
	}
	function SecureContext_transliterate(btransliterate) {
		var strDest = '';
		
		var nTextIter  = 0;
		var nTexttrail = 0;
		
		while (nTextIter < this.strText.length) {
		var strRun = '';
		var cSkipped   = 0;
		while (cSkipped < 7 && nTextIter < this.strText.length) {
		var chT = this.strText.charAt(nTextIter++);
		if (-1 == strRun.indexOf(chT)) {
		strRun += chT;
		cSkipped = 0;
		}
		else cSkipped++;
		}
		while (nTexttrail < nTextIter) {
		var nRunIdx = strRun.indexOf(this.strText.charAt(nTexttrail++));
		if (btransliterate) {
		nRunIdx++
		if (nRunIdx == strRun.length) nRunIdx = 0;
		}
		else {
		nRunIdx--;
		if (nRunIdx == -1) nRunIdx += strRun.length;
		}
		strDest += strRun.charAt(nRunIdx);
		   }
		}
		this.strText = strDest;
	}
	function SecureContext_encypher(anperm) {
		var strEncyph = '';
		var nCols     = anperm.length;
		var nRows     = this.strText.length / nCols;
		for (var i = 0; i < nCols; i++) {
		var k = anperm[ i ];
		for (var j = 0; j < nRows; j++) {
		strEncyph += this.strText.charAt(k);
		k         += nCols;
	   }
	}
	this.strText = strEncyph;
	}
	function SecureContext_decypher(anperm) {
		var nRows    = anperm.length;
		var nCols    = this.strText.length / nRows;
		var anRowOfs = new Array;
		for (var i = 0 ; i < nRows; i++) anRowOfs[ anperm[ i ] ] = i * nCols;
		var strplain = '';
		for (var i = 0; i < nCols; i++) {
		for (var j = 0; j < nRows; j++)
		strplain += this.strText.charAt(anRowOfs[ j ] + i);
	}
	this.strText = strplain;
	}
	function SecureContext_sign(nCols) {
		if (this.bESCApE) {
		this.strText      = this.escape(this.strText);
		this.strSIGNATURE = this.escape(this.strSIGNATURE);
		}
		var nTextLen     = this.strText.length + this.strSIGNATURE.length;
		var nMissingCols = nCols - (nTextLen % nCols);
		var strpadding   = '';  
		if (nMissingCols < nCols)
		for (var i = 0; i < nMissingCols; i++) strpadding += ' ';
		var x = this.strText.length;
		this.strText +=  strpadding + this.strSIGNATURE;
	}
	function SecureContext_unsign(nCols) {
		if (this.bESCApE) {
		this.strText      = this.unescape(this.strText);
		this.strSIGNATURE = this.unescape(this.strSIGNATURE);
		}
		if ('' == this.strSIGNATURE) return true;
		var nTextLen = this.strText.lastIndexOf(this.strSIGNATURE);
		if (-1 == nTextLen) return false;
		this.strText = this.strText.substr(0, nTextLen);
		return true;
	}
	function SecureContext_secure(strpasswd) {
		var passwd = new password(strpasswd);
		var anperm   = passwd.getpermutation()
		this.sign(anperm.length);
		this.transliterate(true);
		this.encypher(anperm);
	}
	function SecureContext_unsecure(strpasswd) {
		var passwd = new password(strpasswd);
		var anperm = passwd.getpermutation()
		this.decypher(anperm);
		this.transliterate(false);
		return this.unsign(anperm.length);
	}
	function doSecure(unseckey) {
		var sc = new SecureContext(unseckey, txtSign,false);
		sc.secure(txtPassw);
		return sc.strText;
	}
	
	function doUnsecure(seckey) {
		var sc = new SecureContext(seckey, txtSign, false);
		if (!sc.unsecure(txtPassw)) 
		alert('Invalid password used.');
		return sc.strText;
	}
	
		
	function MM_findObj(n, d) { //v4.0
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	  if(!x && document.getElementById) x=document.getElementById(n); return x;
	}
	function MM_showHideLayers() { //v3.0
	  var i,p,v,obj,args=MM_showHideLayers.arguments;
	  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
	    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
	    obj.visibility=v; }
	}
	function MM_swapImgRestore() { //v3.0
	  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}

	function MM_reloadPage(init) {  //reloads the window if Nav4 resized
	  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
	    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
	  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
	}
	MM_reloadPage(true);
	function MM_swapImage() { //v3.0
	  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}
	
