//util.js AG utilities


// 1. Finding Objects
//-----------------
function getObj(id) 	/* get element given id */	{var obj; if (document.getElementById){obj=document.getElementById(id);return obj;}else if (document.all){return document.all[id];}else if (document.layers){return document.layers[id];}}
function posY(obj)	/* Usage obj.style.top =(posY(obj) + 20) + "px" */ {var curtop = 0;	if (obj.offsetParent){	while (obj.offsetParent){curtop += obj.offsetTop;obj = obj.offsetParent;}}else if (obj.y)curtop += obj.y;return curtop;}
function posX(obj)	/* Usage obj.style.left=(posX(obj) + 40) + "px" */ {var curleft = 0;if (obj.offsetParent){while (obj.offsetParent){curleft += obj.offsetLeft;obj = obj.offsetParent;}}else if (obj.x)curleft += obj.x;return curleft;}
function getTarg(e)	/* get target object of event e & stop propergation */ {var targ;if (!e) var e= window.event;if (e.target) targ=e.target;else if (e.srcElement) targ=e.srcElement;if (targ.nodeType==3) targ=targ.parentNode;e.cancelBubble = true;if (e.stopPropagation) e.stopPropagation();return targ;}

// 2. sprintf()   eg sprintf("age=%d, cost=%5.2f Euros", 42,76.48)
//-------------------------------------------------------------

// 3. getJavascript code
//-----------------------

/* 4. AG Date functions:
------------------------
  constructors:
  
  	today=new CalDate()			//today's date
  	xmas=new CalDate(2005,11,25)		//date by year, month day
  	birthday=new CalDate("2 Apr 2006")	//date from string, 
  	enddate=new CalDate(millsecs)		//date from no of secs
  	
  properties:
  
  	today.year		//4 digit year
  	today.month		//0-11
  	today.day		//1-31
  	today.dayOfWeek		//0-6, 0==Sunday
  	
  methods:
  
  	xmas.toString()			//return date as string "25 Dec 2005"
  	birthdate.valueOf()		//return date as milisecs
  	nextweek=today.dayAdd(7)	//add or subtract days & retuen a CalDate
  	daysToXmas=xmas.dayDiff(today)	//return no of days between dates
  	
  	febDays=CalDate.monthDays(1)	//return no of days in month
  	mname=CalDate.monthNames(11)	//return a 3charater string for the month name(here="Dec")
*/

//------------------------------------------------------------
// 2. sprintf()   eg sprintf("age=%d, cost=%5.2f Euros", 42,76.48)
//-------------------------------------------------------------
		function sprintf()
		{
			if (!arguments || arguments.length < 1 || !RegExp)
			{
				return;
			}
			var str = arguments[0];
			var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
			var a = b = [], numSubstitutions = 0, numMatches = 0;
			while (a = re.exec(str))
			{
				var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
				var pPrecision = a[5], pType = a[6], rightPart = a[7];
				
				//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

				numMatches++;
				if (pType == '%')
				{
					subst = '%';
				}
				else
				{
					numSubstitutions++;
					if (numSubstitutions >= arguments.length)
					{
						alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
					}
					var param = arguments[numSubstitutions];
					var pad = '';
					       if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
					  else if (pPad) pad = pPad;
					var justifyRight = true;
					       if (pJustify && pJustify === "-") justifyRight = false;
					var minLength = -1;
					       if (pMinLength) minLength = parseInt(pMinLength);
					var precision = -1;
					       if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
					var subst = param;
					       if (pType == 'b') subst = parseInt(param).toString(2);
					  else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
					  else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
					  else if (pType == 'u') subst = Math.abs(param);
					  else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
					  else if (pType == 'o') subst = parseInt(param).toString(8);
					  else if (pType == 's') subst = param;
					  else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
					  else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
				}
				str = leftpart + subst + rightPart;
			}
			return str;
		}
// ---------------------
// 3. getJavascript code
// ---------------------
function getJavascript(src) { 	//loads an html document as an iframe with absolute possition to the left of the window(ie hidden).						
				//Runs the javascript which can set values in top
	if (typeof ( resultFrame ) == "undefined") {
		resultFrame=document.createElement("iframe");
		resultFrame.style.position="absolute";
		resultFrame.style.width="1";
		resultFrame.style.height="1";
		resultFrame.style.left="-10";
		resultFrame.style.top="0";
		document.body.appendChild(resultFrame);
		}							
	resultFrame.src=src;
	}
//----------------------
// 4. AGs Date functions
//----------------------
function CalDate(yd,month,day) {
	if (arguments.length==0) this.date = new Date();
	else if (arguments.length==3) this.date= new Date(yd,month,day);
	else if ( isNaN(yd) ) this.date=new Date( Date.parse(yd) );
	else this.date=new Date(yd);
	this.year=this.date.getFullYear();
	this.month=this.date.getMonth();
	this.day=this.date.getDate();
	this.dayOfWeek=this.date.getDay();
	}
CalDate.monthNames=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
CalDate.monthDays=function(y,m) {
	var md=[31,28,31,30,31,30,31,31,30,31,30,31];
	if (y%4==0 && m==1) { return 29; }
	else { return md[m]; } }

CalDate.prototype.dayAdd=function (d) {return ( new CalDate( ( this.date.valueOf() +86400000*d) ) ); }

CalDate.prototype.dayDiff=function (e) {return ((e.date.valueOf()-this.date.valueOf())/86400000);}
CalDate.prototype.toString=function() {return this.day + " " + CalDate.monthNames[this.month] + " " + this.year;}

