// last edited by Matthew McKenna 07 July 2010
// Re-wrote some functions to utilise best practice object reference
// Have left in some old code to be compatible with much older browsers, IE5 and below
//Added browser detect to the object
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function y2k(number)    { 
	return (number < 1000) ? number + 1900 : number; 
}

function padout(number) { 
	return (number < 10) ? '0' + number : number; 
}

var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = y2k(today.getYear());
var currentdate = padout(day) + '/' + padout(month+1) + '/' + year;
delete today;

var calwindow = null;

/*
Create a pop-pup calender that defaults to today's date.
Calling page must have function restart(strfld) defined, to get the calender output.
*/
function cal(strfld)
{
	var w = 220;
	var h = 210;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	var strurl = "/commonlib/utilities/calendar/cal.htm?field=" + strfld;
	// following open has both IE4 and NS4 commands
   	calwindow = window.open(strurl, 'cal', 'resizable=no,scrollbars=no,width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',dependent=yes,screenX='+TopPosition+',screenY='+LeftPosition);
    calwindow.location.href = strurl;
    if (calwindow.opener == null) calwindow.opener = self;
	calwindow.focus();
}

/*
Create a pop-pup calender that defaults to today's date only if the specified date field is empty.
Calling page must have function restart(strfld) defined, to get the calender output.
*/
function calField(strfld)
{
	var w = 220;
	var h = 210;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	var strurl = "/commonlib/utilities/calendar/cal.htm?field=" + strfld;
	ExtractDate(strfld);	
//	window.top.captureEvents(Event.CLICK|Event.FOCUS);
//	window.top.onclick = IgnoreEvents;
//	window.top.onfocus = HandleFocus;
	// following open has both IE4 and NS4 commands
   	calwindow=window.open(strurl, 'cal', 'resizable=no,scrollbars=no,width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',dependent=yes,screenX='+TopPosition+',screenY='+LeftPosition);
    calwindow.location.href = strurl;
    if (calwindow.opener == null) calwindow.opener = self;
	calwindow.focus();
}

/*
Extracts the date components from inside the named date field.
*/
function ExtractDate(strDateFld)
{
	var ofield = eval("document.fm1." + strDateFld);
	var calDate = new Date();
	if (Date.parse(ofield.value) > 0)
	{
		calDate.setTime(Date.parse(ofield.value));
//		calDate.setTime(Date.parse(year(ofield.value) + "-" + month(ofield.value) + "-" + day(ofield.value)));
		day   = calDate.getDate();
		month = calDate.getMonth();
		year  = y2k(calDate.getYear());
	}
	delete calDate;	// always clean up
}

/*
Used to make the pop-up calender modal.
ie the calling web-page cannot be accessed till pop-up is closed.
*/
function HandleFocus()
{
	if (calwindow)
	{
		if (!calwindow.closed)
		{
			calwindow.focus();
		}
		else
		{
			window.top.releaseEvents(Event.CLICK|Event.FOCUS);
			window.top.onclick="";
		}
	}
	return false;
}

/*
Used to make the pop-up calender modal.
ie the calling web-page cannot be accessed till pop-up is closed.
*/
function IgnoreEvents(e)
{
	return false;
}

 //--------------------------------------------------------------------------------
 function restart(objfld) 
{
//  Matthew McKenna edit.  Comletely re-wrote this and other functions to use best practice naming for JS elements, getElementByID
	var strMonths = new String("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
	strMnth = strMonths.split(" ");
	var ofield = eval(objfld);

	var objFieldA = objfld.split(".");
	objFieldA = objFieldA[1];

	
	if (BrowserDetect.browser == "Explorer") {
		document.getElementById(objFieldA).value = "" + day + "-" + strMnth[month] + "-" + year;
		calwindow.close();
		ofield.value = "" + day + "-" + strMnth[month] + "-" + year;
	} else {
		ofield.value = "" + day + "-" + strMnth[month] + "-" + year;
		calwindow.close();
	}
	calwindow.close();
	
//    
    
//	
	delete strMonths;
	delete strMnth;
}

//----------------------------------------------------------------------------------
//-- the actual calling function for the calendar
function DoCal(elTarget) {
  if (showModalDialog() ) {
    var sRtn;
    sRtn = window.showModalDialog("/commonlib/utilities/calendar/Calendar.htm","","center=yes;dialogWidth=300pt;dialogHeight=160pt");
    if (sRtn!="")
      	elTarget.value = sRtn;
  } else
    	alert("Internet Explorer 4.0 or later is required.");
 }


 //----------------------------------------------------------------------------------
// some poxy popup function
  function MM_popupMsg(msg) { //v1.0
  alert(msg);
}

