/* default scripts for all pages */

// set img src to filename
function SetImage( id, filename ) {
   if ( document.images[id] )
      document.images[id].src = filename;
}

// find object by id in a frame
// n = id , (optional) d = frame document object
function FindObject(n, d) {
	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 );
	}
	
	// find n in .all
	if (!(x=d[n]) && d.all ) 
	  	x = d.all[n];
	
	// find n in forms
	for ( i = 0; !x && i<d.forms.length;i++ )
	  	x = d.forms[i][n];
	
	// find n in layers
	for( i =0 ; !x && d.layers && i<d.layers.length ;i++ )
	  	x=FindObject( n, d.layers[i].document );
	
	// find n by getElementById
	if( !x && document.getElementById )
	  	x = document.getElementById( n );
	
	return x;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function GetCookie(name) 
{
   var dc = document.cookie;
   var prefix = name + "=";
   var begin = dc.indexOf("; " + prefix);
   if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return null;
   } else
      begin += 2;

   var end = document.cookie.indexOf(";", begin);
   if (end == -1)
      end = dc.length;

   return unescape(dc.substring(begin + prefix.length, end));
}

// Calculate the left position of the element, relative to the body.
function GetElementLeft( element ) {

	var left = 0; 
	while ( element ) {
		left	  += element.offsetLeft;
		element	= element.offsetParent;
	}
	return left;
}


// Calculate the top position of the element, relative to the body.
function GetElementTop( element ) {

	var top = 0; 
	while ( element ) {
		top	  += element.offsetTop;
		element	= element.offsetParent;
	}
	return top;	
}

// dumpv in javascript
function dumpv( object ) {
   var i;
   var s='';
   for ( i in object ) {
      s += i + ": " + object[i] + "\n";
   }
   alert(s);
}

// open a simple popup window
function openPopup( href, width, height ) {
   
   // Set default size
   if ( width == null || width == '' )
      width = 600;

   if ( height == null || height == '' )
      height = 400;

   window.open( href, '', 'statusbar=no,width=' + width + ',height=' + height + ',menubar=no,toolbar=no,scrollbars=yes' );
   return false;
}

// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function SetCookie(name, value, expires, path, domain, secure) 
{
   var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
   document.cookie = curCookie;
}

// UnpackCSV
function UnpackCSV ( csv ) {
   
   // initializing variables
   var i = 0;
   var data = new Array();
   var heading = new Array();
   if ( csv.charAt(csv.length-1) != ';') csv = csv + ';';

   while ((x = csv.indexOf(';'))>-1) {
      
      var row = csv.substr(0,x) + ',';
      var csv = csv.substr(x+1);
      var j = 0;
      data[i] = new Array();
      while ((y = row.indexOf(','))>-1) {

         var cell = row.substr(0,y);
         var row = row.substr(y+1);
         j++;
         if ( i == 0 ) {
            heading[j] = unescape(cell);
         } else {
            data[i][heading[j]] = unescape(cell);
         }
      }
      i++;
   }
   return data; 
}