var ns = (document.layers)? true:false
var ie = (document.all)? true:false
var idRow = null;
var idRec = null;
var canDo = false;
var mouseX = 0;
var mouseY = 0;


//function patmanMouseEvent(e) {
//	if (canDo) {
//		// we want to keep the cookie alive on a click event, just refresh the userID
//		readMultiKeyCookie(the_info,patman)
//		updateKey(patman,the_info,'userID',the_info["userID"])
//	}
//}
document.onmousemove = sysMouse;
function sysMouse(e) {
	mouseX = event.clientX + document.body.scrollLeft
    mouseY = event.clientY + document.body.scrollTop
}

document.oncontextmenu = cMenu;
function cMenu() {
	// Remove context menu - IE6 only
	// return false;
}

// Image Rollover //
function rollover(target, source, msg) {
	if(canDo) {
		if (document.images) { target.src = source.src; self.status=msg; return true;}
	}
}

function fullScreen(url) {
    window.open(url,"fs","fullscreen,scrollbars=no");
}

function fullScreenWithFrame(url) {
	params =  'toolbar=no,location=0,directories=no,status=no,menubar=0,scrollbars=yes,resizable=0,width=' + screen.width + ',height=' + screen.height + ',left=0 ,top=0'
	// params += ', fullscreen=yes';
	newwin=window.open(url,'CIS_Popup', params);
	if (window.focus) {newwin.focus()}
	return false;
}

var popupName = "functionWin";
function openWindow(url,w,h) {
	var winX = 0;
	var winY = 0;
	if (parseInt(navigator.appVersion) >= 4) { winX = (screen.availWidth - w)*.5; winY = (screen.availHeight - h)*.5; }
	winOpts = "toolbar=no,location=0,directories=no,status=no,menubar=0,scrollbars=yes,resizable=0," + "width=" + w + ",height=" + h + ",left=" + winX + ",top=" + winY
	popupWin = window.open(url, popupName, winOpts);
	if (window.focus) {popupName.focus()}
}

function setCookie(NameOfCookie, value, expiredays) {
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}

// Form Handeling Scripts //
function singleRadio(theObj)
{
	if (!canDo) { return; }
	this.status = theObj.name + "!!!"
	// alert("Got Here !!!")
	if (theObj.value == "true") { theObj.value = "false"; theObj.checked = false; }
	else { theObj.value = "true"; theObj.checked = true; }
	return true;
}

//functions to highlight fields
function HighLite() {
	if (!canDo) { return; }
	if (!ie4){return;}
    src = event.srcElement;
	src.style.background = "#CC9933";
}

function UnHighLite() {
	if (!canDo) { return; }
	if (!ie4){return;}
    src=event.srcElement;
    src.style.background = "#ffffff";
}

function rusure(msg)
{
	if (!canDo) { return; }
	var doit = window.confirm(msg);
	if (doit) return true;
	return false;
}

/* ########## Check numeric && date Functions ########## */

function checkDate(theEl) {
	if (theEl.value.length == 0) return false;
	var checkstr = "0123456789";
	var DateField = theEl;
	var Datevalue = "";
	var DateTemp = "";
	var seperator = "/";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
	err = 0;
	DateValue = theEl.value;
	for (i = 0; i < DateValue.length; i++) { if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) { DateTemp = DateTemp + DateValue.substr(i,1); } }
	DateValue = DateTemp;
	if (DateValue.length == 6) { DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
	if (DateValue.length != 8) { err = 19; }
	year = DateValue.substr(4,4);
	if (year == 0) { err = 20; }
	month = DateValue.substr(2,2);
	if ((month < 1) || (month > 12)) { err = 21; }
	day = DateValue.substr(0,2);
	if (day < 1) { err = 22; }
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) { leap = 1; }
	if ((month == 2) && (leap == 1) && (day > 29)) { err = 23; }
	if ((month == 2) && (leap != 1) && (day > 28)) { err = 24; }
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) { err = 25; }
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) { err = 26; }
	if ((day == 0) && (month == 0) && (year == 00)) { err = 0; day = ""; month = ""; year = ""; seperator = ""; }
	if (err == 0) { DateField.value = day + seperator + month + seperator + year; return true;}
	else {
		alert("You must enter a valid date!\n(dd/mm/yy) or (dd/mm/yyyy)");
		DateField.select();
		DateField.focus();
		return false;
	}
}

function checkInteger(theEl) {
	var str = theEl.value;
	if (str.length == 0) { return false; }
	var objRegExp  = /(^-?\d\d*$)/;
	if (!objRegExp.test(str)) {
		alert("You must enter a valid integer!")
		theEl.focus()
		theEl.select()
		return false;
	}
	return true;
}

function checkReal(theEl) {
	var str = theEl.value;
	if (str.length == 0) { return false; }
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
	if (!objRegExp.test(str)) {
		alert("You must enter a valid number!")
		theEl.focus()
		theEl.select()
		return false;
	}
	return true;
}

/* ########## Format Numbers Functions ########## */

function roundit(Num, Places) {
	if (Places > 0) {
		if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
			var Rounder = Math.pow(10, Places);
			return Math.round(Num * Rounder) / Rounder;
		}
		else return Num;
	}
	else
	{ return Math.round(Num); }
}

function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
	if (isNaN(parseInt(num))) { return "NaN"; }
	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;
	var tmpNumStr = new String(tmpNum);
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
	{
		if (num > 0) { tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length); }
		else { tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length); }
	}
	if (bolCommas && (num >= 1000 || num <= -1000))
	{
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0) { iStart = tmpNumStr.length; }
		iStart -= 3;
		while (iStart >= 1)
		{
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length);
			iStart -= 3;
		}		
	}
	if (bolParens && num < 0) { tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")"; }
	return tmpNumStr;
}

function FormatPercent(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.										
 
	RETVAL:
		The formatted number!		
 **********************************************************************/
{
	var tmpStr = new String(FormatNumber(num*100,decimalNum,bolLeadingZero,bolParens,bolCommas));
	if (tmpStr.indexOf(")") != -1)
	{
		tmpStr = tmpStr.substring(0,tmpStr.length - 1) + "%)";
		return tmpStr;
	}
	else { return tmpStr + "%"; }
}

function FormatCurrency(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.										
 
	RETVAL:
		The formatted number!		
 **********************************************************************/
{
	var tmpStr = new String(FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas));
	if (tmpStr.indexOf("(") != -1 || tmpStr.indexOf("-") != -1)
	{
		if (tmpStr.charAt(0) == "(") { tmpStr = "($"  + tmpStr.substring(1,tmpStr.length); }
		else
		{ if (tmpStr.charAt(0) == "-") { tmpStr = "-$" + tmpStr.substring(1,tmpStr.length); } }
		return tmpStr;
	}
	else { return "$" + tmpStr; }
}

/* ########## String Trim Functions ########## */

function LTrim(str) {
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1)
	{
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
		{
			j++;
		}
		s = s.substring(j, i);
	}

	return s;
}

function RTrim(str) {
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1)
	{
		var i = s.length - 1;
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
		{
			i--;
		}
		s = s.substring(0, i+1);
	}
	return s;
}

function Trim(str) { return RTrim(LTrim(str)); }

function Len(str) { return String(str).length; }

function Left(str, n) {
	if (n <= 0) { return ""; }
	else
	{
		if (n > String(str).length) { return str; }
		else { return String(str).substring(0,n); }
	}
}

function Right(str, n) {
	if (n <= 0) { return ""; }
	else 
	{
		if (n > String(str).length) { return str; }
		else
		{
			var iLen = String(str).length;
			return String(str).substring(iLen, iLen - n);
		}
	}
}

function Mid(str, start, len) {
	if (start < 0 || len < 0) return "";
	var iEnd, iLen = String(str).length;
	if (start + len > iLen) { iEnd = iLen; }
	else { iEnd = start + len; }
	return String(str).substring(start,iEnd);
}

function InStr(strSearch, charSearchFor) {
	for (i=0; i < Len(strSearch); i++)
	{ if (charSearchFor == Mid(strSearch, i, 1)) { return i; } }
	return -1;
}

/* ########## Favourites Functions ########## */

function addFavourites() {
    window.external.AddFavorite(location.href, document.title);
}

/* ########## Print Functions ########## */

function printPage() {
    window.print();
}

/* ########## Font Size Functions ########## */
   
var minFontSize=8;
var maxFontSize=18;

function increaseFontSize() {      
    doFontChange("increase", "p");
    doFontChange("increase", "h1");
    doFontChange("increase", "h2");
    doFontChange("increase", "h3");
    doFontChange("increase", "ul");
}

function decreaseFontSize() {
    doFontChange("decrease", "p");
    doFontChange("decrease", "h1");
    doFontChange("decrease", "h2");
    doFontChange("decrease", "h3");
    doFontChange("decrease", "ul");
}

function doFontChange(theDirection, theElement)
{
   var targetElement = document.getElementsByTagName(theElement);
   for(i=0;i<targetElement.length;i++) {
      if(targetElement[i].style.fontSize) {
         var s = parseInt(targetElement[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      var tempMax = maxFontSize;
      var tempMin = minFontSize;
      if (theElement == "h1") { tempMax = maxFontSize + 3; tempMin = minFontSize + 3; }
      if (theElement == "h2") { tempMax = maxFontSize + 2; tempMin = minFontSize + 2; }
      if (theElement == "h3") { tempMax = maxFontSize + 1; tempMin = minFontSize + 1; }
      if(theDirection == "increase" && s!=tempMax)
      {
        s += 1;
      } else {
          if(s!=tempMin) {
             s -= 1;
          }
      }
      targetElement[i].style.fontSize = s+"px"
   }
}
