var minus = '/images/admin.tree.minus.gif';
var plus = '/images/admin.tree.plus.gif';
var st = (document.getElementById)? true:false;	// Standard DOM Browser
var ie4 = (document.all && !st)? true:false;	// Internet Explorer 4
var ns4 = (document.layers)? true:false;		// Netscape 4

if (st || ie4 || ns4) {
	if (ie4) {
		document.getElementById = function (layerName) {return document.all[layerName]};
		document.getElementsByTagName = function (layerName) {return document.all.tags[layerName]};
	}
	else if (ns4) {
		var getElementById = new Array ();
		document.getElementById = function (layerName) {return getElementById[layerName]};
		document.getElementsByTagName = function (tagName) {if (tagName.toLowerCase() == 'div') return getElementById.length;};
		function makeStandard (currentLayer) {
			var i=0;
			for (;i<currentLayer.length;i++) {
				currentLayer[i].style = currentLayer[i];
				getElementById[currentLayer[i].id] = currentLayer[i];
				if (currentLayer[i].document.layers.length) makeStandard (currentLayer[i].document.layers);
			}
		}
	}
	window.onload=new Function ('init();');
}

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

function init() {
	if (st || ie4 || ns4) {
		if (ns4) {
			makeStandard (document.layers);
		}
		for (i=0;i<document.links.length;i++) document.links[i].onfocus = function() {this.blur()};
	}
}

var openNewWindow = null;					// New Window Object
	
function openNew(URL) {
	var height, width, left, top, windowname, resizable;

	if (arguments.length > 1)
		height = arguments[1];
	else
		height = 175;
		
	if (arguments.length > 2)
		width = arguments[2];
	else
		width = 300;

	if (arguments.length > 3)
		left = arguments[3];
	else
		left = 300;

	if (arguments.length > 4)
		top = arguments[4];
	else
		top = 160;

	if (arguments.length > 5)
		windowname = arguments[5];
	else
		windowname = 'ViewImageWindow';
	
	if (arguments.length > 6)
		resizable = arguments[6];
	else
		resizable = 1;
	
	if (openNewWindow==null || openNewWindow.closed) {
		//if (document.all&&window.print) //if ie5
			//openNewWindow=window.showModelessDialog(URL,'','help:0;resizable:'+resizable+'1;dialogWidth:'+width+'px;dialogHeight:'+height+'px');
		//else		
			openNewWindow=window.open(URL,windowname,'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable='+resizable+',copyhistory=no,height='+height+',width='+width+',left='+left+',top='+top);
		
		openNewWindow.resizeTo(width, height);
	} else {
		openNewWindow.resizeTo(width, height);
		openNewWindow.moveTo(left, top);
		openNewWindow.location.href = URL;
	}
	openNewWindow.focus();
}

function ExpandCollapse(exctID, className) {
	//PreviousSelected
	//var currPreviousDiv = document.getElementById('divPrevious' + (exctID));
	var currParentDiv = document.getElementById('divParent' + (exctID));
	var currChildDiv = document.getElementById('divChild' + (exctID));
	//var currImg = document.getElementById('img' + exctID);
	//currChildDiv.style.display = (currChildDiv.style.display == 'block')? 'none':'block';
	//currImg.src = (currChildDiv.style.display == 'block')? minus:plus;
	if (currParentDiv.className == className + 'expand') {
		currParentDiv.className = className + 'collapse';
		currChildDiv.className = className + 'hide';
	} else {
		currParentDiv.className = className + 'expand';
		currChildDiv.className = className + 'show';
	}
}

function ExpandParents(exctID, className) {
	var currParentDiv = document.getElementById('divParent' + exctID);
	var currChildDiv = currParentDiv.parentElement;
	while (currChildDiv.id.substr(0, 8) == 'divChild') {
		currParentDiv = document.getElementById('divParent' + currChildDiv.id.substr(8));
		currParentDiv.className = className + 'expand';
		currChildDiv.className = className + 'show';
		currChildDiv = currParentDiv.parentElement;
	}
}

function AddRemoveCategory(cntSelected, cntSelectedList, CatID) {
	var strCats = cntSelectedList.value;
	var iPos = 0;
		
	if (cntSelected.type == "radio") {
		cntSelectedList.value = CatID;
	}
	else {
		if (cntSelected.checked) {
			if (strCats.length > 0) {
				if (strCats == CatID ||
				strCats.indexOf(CatID + ',') == 0 ||
				strCats.indexOf(',' + CatID + ',') != -1 ||
				(strCats.lastIndexOf(',' + CatID) == strCats.length - CatID.length - 1 && strCats.length > CatID.length))
					cntSelectedList.value = strCats;
				else
					cntSelectedList.value = strCats + ',' + CatID;
			}
			else cntSelectedList.value = CatID;
		}
		else {
			if (strCats.length > 0) {
				if (CatID == strCats)
					strCats = "";
				if (strCats.indexOf(CatID + ',') == 0)
					strCats = strCats.substr(CatID.length + 1);
				else if ((iPos = strCats.indexOf(',' + CatID + ',')) != -1) {
					strCats = strCats.substr(0, iPos + 1) + strCats.substr(iPos + CatID.length + 2);
				}
				else if (strCats.lastIndexOf(',' + CatID) == strCats.length - CatID.length - 1 && strCats.length > CatID.length)
					strCats = strCats.substr(0, strCats.length - CatID.length - 1);
	
				cntSelectedList.value = strCats;
			}
		}
	}
	return false;
}

function y2k(number) {
	return (number < 1000) ? number + 1900 : number;
}

function isDate(year, month, day) {
	// checks if date passed is valid
	// will accept dates in following format:
	// isDate(dd,mm,ccyy), or
	// isDate(dd,mm) - which defaults to the current year, or
	// isDate(dd) - which defaults to the current month and year.
	// Note, if passed the month must be between 1 and 12, and the
	// year in ccyy format.
	var today = new Date();
	
	year = ((!year) ? y2k(today.getYear()):year);
	month = ((!month) ? today.getMonth():month-1);
	if (!day) return false;
	var test = new Date(year,month,day);
	if ((y2k(test.getYear()) == year) &&
	(month == test.getMonth()) &&
	(day == test.getDate()))
		return true;
	else
		return false;
}

function isTime(time) {
	var hour, minute, marker;
	var result = time.match('^([0-2]?[0-9]):([0-5][0-9])? ?([APap][Mm])?');
	
	hour = result[1];
	minute = result[2];
	marker = result[3];
	
	if (marker.length > 0) {
		if (hour > 12 || minute > 59) 
			return false;
	} else {
		if (hour > 23 || minute > 59) 
			return false; 
	}
	// time was validated! 
	return true; 
}

function preLoadImages() {  //Simple PreLoader
	var i=arguments.length,j=0;

	for (;j<i;j++) {
		preLoadImages[j]=new Image();
		preLoadImages[j].src = arguments[j];
	}
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this;
}
function isDateText(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false;
	}
return true;
}

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function IsValidTime(timeStr) {
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.
	
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
	alert("Time is not in a valid format.");
	return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	
	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }
	
	if (hour < 0  || hour > 23) {
	alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
	return false;
	}
	if (hour <= 12 && ampm == null) {
	if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
	alert("You must specify AM or PM.");
	return false;
	   }
	}
	if  (hour > 12 && ampm != null) {
	alert("You can't specify AM or PM for military time.");
	return false;
	}
	if (minute<0 || minute > 59) {
	alert ("Minute must be between 0 and 59.");
	return false;
	}
	if (second != null && (second < 0 || second > 59)) {
	alert ("Second must be between 0 and 59.");
	return false;
	}
return true;
}
