var dDate = new Date();
	dDate.setHours(0,0,0);
var dCurMonth = dDate.getMonth();
var dCurYear = dDate.getFullYear();
var app, IEDoc, v, g, sD, HD, ho, so;
var purl,adatan;
var startDate, vendor, resIDs=new Array(), classSlotIDs=new Array(), vendorMessage, resIDToPos, eventID, service;
var call='Please, call to make appointment for this time.';
var changeapptsID=0, realTR;
var dateField;

if(type == typeNETSCAPE && version <= 4) {
	app='document.layers[\'cDiv\'].';
	IEDoc="";
	v=".top=";
	g=".left=";
	sD="";
	HD="visibility=";
	ho='"hide"';
	so='"show"';
} else {
	app = "";
	IEDoc = "document.getElementById(\'cDiv\')";
	v=".pixelTop=";
	g=".pixelLeft=";
	sD=".style";
	HD=".display=";
	ho='"none"';
	so='"block"';
}

function initResIDs(user,rs) {
	resIDs[user]=new Array(rs);
    classSlotIDs[user]=new Array(rs);
}
function addResID(user,index,resID, classSlotID) {
	resIDs[user][index] = resID;
    classSlotIDs[user][index] = classSlotID;
}
function setGlobs(sd,v,vm, ev, sr) {
	startDate = sd;
	vendor = v;
	vendorMessage = vm;
	eventID=ev;
    service=sr;
}
function setUserJSItems(a,p) {
	adatan = a;
	purl = p;
}

function SetSel(dCurDate) {
	var sel;
	if(type == typeNETSCAPE && version > 4) {
		document.forms.SelCal.elements[0].selectedIndex = dCurDate.getMonth();
		document.forms.SelCal.elements[1].selectedIndex = dCurDate.getFullYear() - 2000;
	} else {
		eval(app+'document.SelCal.elements[0].selectedIndex = dCurDate.getMonth()');
		eval(app+'document.SelCal.elements[1].selectedIndex=dCurDate.getFullYear()-2000');
	}
}
function getCellInfo(userID, lastEvent){
    var objCell;
	if(type == typeNETSCAPE && version >= 5)
		objCell=lastEvent.target;
	else
		objCell=lastEvent.srcElement;

	if (objCell.tagName == "IMG")
		objCell=objCell.parentNode.parentNode;
	if (objCell.tagName == "A")
		objCell=objCell.parentNode;
	if ( objCell.tagName != "TD" )
		return;
	var form=document.getElementsByName("TheForm"+userID).item(0);
	var table=document.getElementById("tableid"+userID);
	var num_resources = form.resourcesize.value;
	var rowspan = objCell.getAttribute("rowspan", false);   
    if (rowspan=='' || rowspan==null)
	   rowspan=1;
	else
	   rowspan=parseInt(rowspan);
	var row=table.rows.item(0);
	var j = 0;
	for (var _tmp = 0; j<row.cells.length && _tmp + row.cells.item(j).colSpan < objCell.cellIndex+1; j++)
        _tmp += row.cells.item(j).colSpan;
    var month_year = getInner(row.cells.item(j));
	var comma = month_year.indexOf(",");
	var month=month_year.substring(0, comma);
	var year = month_year.substring(comma+2, month_year.length);
	var inc =  parseInt(form.inc_time.value);
	var col = objCell.getAttribute("index", false);
    if (col==null)
	 return
    var sp=objCell.getAttribute("special", false);
	if(sp!=null && sp!='')
		col=sp;
	col= parseInt(col);
	var intCellIndex = objCell.cellIndex;
	var intRowIndex  = objCell.parentNode.rowIndex;
    var backColor= objCell.style.backgroundColor;
    var text;
    if (backColor == null || backColor=="")
		text = "Appointment is unavailable";
    else if(col > 0){
        inc = rowspan * inc;
        var resource = new String(getResName(getResID(col,userID)+getclassSlotID(col,userID)));
		var timeval = new String( getTime(table, intRowIndex ));
		var fcol = Math.floor((col-1)/num_resources);
		var dayval = new String( getDay(table, fcol) );
		dateval =  new String(getDate(table, fcol));		
        var isOwnAppt=backColor == "#000000" || backColor == "rgb(0,0,0)"|| backColor == "rgb(0, 0, 0)"
        text = getTitleOverBox(isOwnAppt? titleOverOwnAppt : titleOverBox, resource, dayval + ", " + month + " " + dateval + ", " + year ,timeval, inc );
    }
	else if(col==-1)
		text=call;
	else if(col==-2)
		text =vendorMessage;
    
    if(intRowIndex >1 && intCellIndex >0){
        window.status=text;
        objCell.title=text;
	}
}
function getTitleOverBox(pattern, resource, date, timeval, duration) {
    macros['r_name']=resource;
    macros['time']=timeval;
    macros['dur']=duration;
    macros['d_long'] = date;
    var d= new Date(date);
    macros['d_medium'] = d.toDateString();    
    macros['d_short'] = (d.getMonth() + 1) + "/"+d.getDate() + "/"+d.getYear();
    var re =/\$\(\w*\)/g;
    return pattern.replace(re, function($0){
        $0=$0.substring(2,$0.length-1).toLowerCase();
        return macros[$0];        
    });
}
function getResName(resID) {
	return getInner(document.getElementById("res"+resID));
}
function getDay(table, fcol){
	return getInner(table.rows.item(1).cells.item(fcol+1));
}
function getDate(table, fcol) {
	return getInner(table.rows.item(2).cells.item(fcol));
}
function getStartTime(table,row) {
	return table.rows.item(row).cells.item(0).id;
}
function getTime(table, row){
	return	getInner(table.rows.item(row).cells.item(0));
}
function getInner(cell){
	return (type == typeNETSCAPE)? cell.innerHTML:cell.innerText;
}
var colorArray=new Array(8);

colorArray["#00CED1"]="#CAFFFF";
colorArray["#00FF00"]="#E0FFE0";
colorArray["#9370DB"]="#E2D9FF";
colorArray["#FF4500"]="#FFACAC";
colorArray["#FFD700"]="#FFFBC1";
colorArray["#B0C4DE"]="#ECF6F0";
colorArray["#9ACD32"]="#C4FFC4";
colorArray["#A0A0A0"]="#E0E0E0";

var array, isHL=false;
function highlightRes(selRes){    
    var style, Index, cell, isSlot, curColor;
	var resID, userID, table, totalRes, pat;
	isHL=selRes!=-1;
	for(userID in resIDs){
		table=document.getElementById("tableid"+userID);
		if (table == null)
			continue;
		totalRes=document.getElementsByName("TheForm"+userID).item(0).resourcesize.value;
		for (i=2; i<table.rows.length; i++) {
			for (j=1; j<table.rows[i].cells.length; j++){
				cell=table.rows[i].cells[j];
				style=cell.style;
				isSlot=cell.getElementsByTagName("A").length>0;
				if(isSlot && style.backgroundColor!="#000000" && cell.style.backgroundColor!="rgb(0,0,0)" && cell.style.backgroundColor!="rgb(0, 0, 0)"){
                    Index = cell.getAttribute("index", false)
                    resID=getResID(Index, userID)+getclassSlotID(Index,userID);
                    if(selRes==-1){ //restore All
						style.backgroundColor=getColor(resID, 0);
						style.borderBottom="solid 1px #202020";
						pat =getPattern(resID);
						if (pat!=null)							
                            style.backgroundImage=pat;
                    }
					else if(resID==selRes){
						style.backgroundColor=getColor(resID, 0);
						style.borderColor = "#000000";
						pat =getPattern(resID);
						if (pat!=null)
                            style.backgroundImage=pat;
                    }
					else{
						style.backgroundColor=getColor(resID, 1);
						style.borderBottom="solid 1px #aaaaaa";
						style.backgroundImage='';
					}
				}
			}
		}
	}
}
function resetHighLigting(lastEvent){
//	if(!isHL &&  changeapptsID==null)
//		return;
	var src;
	if(type == typeNETSCAPE && version >= 5)
		src=lastEvent.target;
	else if(type == typeMICROSOFT)
		src=lastEvent.srcElement;
	if(src.nodeType==3)// for netscape
	   src=src.parentNode;
	var tag=src.tagName.toLowerCase();

	if(tag=="img"){// for netscape
	   src=src.parentNode;
	   tag=src.tagName.toLowerCase();
	}
	if(isHL)
		highlightRes(-1);
}

function getFirstApptRow(table) {
	var i;
	for (i=0;i < 10 && (table.rows.item(i).tagName.search(/^TR$/i) != 0 || table.rows.item(i).cells.length == 0 ||
			table.rows.item(i).cells.item(0).className.search(/^TM$/i) != 0);i++) ;
			// the mac seems to have a different first starting row.
	return i;
}

function addLinks(userID,url, needRightColumn, isSchool){
var table=document.getElementById("tableid"+userID);
if (table == null)
   return;
var slot, resInd, resID, href, text, links, Index, classSlotID;
var totalRes=document.getElementsByName("TheForm"+userID).item(0).resourcesize.value;
array = new Array(table.rows.length);
var nrows = table.rows.length;
var firstApptRow = getFirstApptRow(table);
var ncols=table.rows.item(firstApptRow).cells.length-1;
	for (i=firstApptRow; i<nrows; i++)
		array[i]=new Array();
	for (i=firstApptRow; i<nrows; i++){
		var row = table.rows[i];
        var len=needRightColumn ? row.cells.length-1 : row.cells.length;
        row.cells[0].noWrap=true;
        for (j=1, Index=1; j<len; j++, Index++) {

            cell=row.cells[j];
			for(; array[i][Index]=="slot"; Index++);

            slot=cell.id;            
            cell.setAttribute('index', Index);

            for(k=1; k<cell.rowSpan; k++)
				array[i+k][Index]="slot";
			resInd=(Index-1)%totalRes;
			if(slot==null || slot=="")
				cell.style.borderWidth='0';
			if(resInd==0 && Index>1)
				cell.style.borderLeft='solid 1px #000000';
			if(slot==null || slot=="")
				continue;

            resID=resIDs[userID][resInd];
            classSlotID=classSlotIDs[userID][resInd];
            if(classSlotID==null)
                classSlotID=''
            if(cell.style.backgroundColor!="#000000" && cell.style.backgroundColor!="rgb(0,0,0)"  && cell.style.backgroundColor!="rgb(0, 0, 0)"){
				cell.style.backgroundColor=getColor(resID+classSlotID);
				var pat =getPattern(resID+classSlotID);
				if (pat!=null && pat != ''){
                    cell.style.backgroundImage=pat;
                    cell.style.borderBottomWidth=2;
                    cell.style.borderBottomColor='#303030';
                }
            }
			switch(parseInt(slot)){
			case -1:
				href='javascript:alert(call)';
				cell.setAttribute('special', -1);
				break;
			case -2://too close appointment
				href='javascript:alert(\"'+vendorMessage+'\")';
				cell.setAttribute('special', -2);
				break;
			default:
				href=url+"?slot="+slot+"&ven=" + vendor+"&event=" + eventID+"&service=" + service;                                        
                if (isSchool) href += "&classSlotID=" + classSlotID+"&studentID="+userID;
			}
			var anc=cell.childNodes.item(0);
			anc.href=href;
			var rowSpan=Math.abs(cell.rowSpan);
			if(rowSpan>1)
			   anc.style.height=cell.rowSpan*16;
			if (rowSpan == 0) rowSpan = 1;
			anc.innerHTML='<img border=0 width=100% height='+(13*rowSpan)+'px src=images/dot.gif>';
            anc.style.width='100';
        }
	}
}
function costHasChanged(aChoice) {
	var newCost = aChoice[aChoice.selectedIndex].getAttribute("amnt");
	document.getElementById("x_Amount").value = newCost;
	document.getElementById("x_AmountLabel").innerHTML = newCost;
}
function fillPosition(){
	resIDToPos=new Array();
	var table=document.getElementById("resource_table");
	var id;
	var wid = table.rows[1].cells.length / 2;
    var top=0;
    for (; top<table.rows.length; top++) {
         if (table.rows[top].id == 'lastRow') {
             top++;
             break;
         }
    }

	for (var i = top; i<table.rows.length; i++) {
		var row = table.rows[i];
		for (var j=1; j<row.cells.length; j+=2){
			id=row.cells[j].id;
			id=id.substring(3, id.length);
			resIDToPos[id]=((i-top)*wid+(j+1)/2-1);
		}
	}
}
function getColor(resID, type){
    var color=document.getElementById('resbox'+resID).style.backgroundColor;
    return type==1 ? colorArray[color.toUpperCase()] : color;
}
function getPattern(resID){
    return document.getElementById('resbox'+resID).style.backgroundImage;
}
function getResID(pos, userID){
	return resIDs[userID][pos2resIndex(pos, userID)];
}
function getclassSlotID(pos, userID){
    var cn=classSlotIDs[userID][pos2resIndex(pos, userID)]
    return cn==null? '': cn;
}
function pos2resIndex(pos, userID){
    var num_resources = document.getElementsByName("TheForm"+userID).item(0).resourcesize.value;
    return (pos-1)%num_resources;
}
function showTables(eventType){
	var el=document.getElementById("stub");
	if(el!=null)
		el.style.display="none";
	el=document.getElementById("blueMess");
	if(el!=null)
		el.style.display="";
	var schtab=document.getElementById("schooltab");
	if (schtab != null) { // this is because the mac needs a kick to relay out the table.
		var row = schtab.rows[0];
		if (row != null && row.cells.length > 1) {
			row.cells[1].style.display="none";
		}
	}
	for(userID in resIDs){
		var table=document.getElementById("tableid"+userID);
		if(table!=null)
			table.style.display="";
	}
	if (schtab != null) { // this is because the mac needs a kick to relay out the table.
		var row = schtab.rows[0];
		if (row != null && row.cells.length > 1) {
			row.cells[1].style.display="";
		}
	}
	window.setTimeout('blackenAppts('+eventType+')', 100);
}
function resizeTable(){
	var buttons = new Array("hideshowImg", "hideshowImg2");
	var i, img;
	for(i in buttons){
		img =document.getElementById(buttons[i]);
		if(img!=null && img.style.display=="none")
			img.style.display="";
	}
	var base=document.getElementById("base");
	if(base==null)
		return;
	var pickad=document.getElementById("pickad");
	var newHeight=document.body.clientHeight-base.offsetTop;
	if (pickad!=null)
		newHeight -= pickad.offsetHeight;
	if (newHeight>0)
		base.style.height=newHeight;
}
function hideShow(){
	if(type == typeMICROSOFT && document.readyState != "complete")
		return;

	var table=document.getElementById("schedule");
	var img =document.getElementById("hideshowImg");
	if(table==null)
		return;
	if(table.style.display==""){
		table.style.display="none";
		img.src="images/show.gif";
		img.title="show the appointments scheduled by you";
	}
	else{
		table.style.display="";
		img.src="images/hide.gif";
		img.title="hide the appointments scheduled by you";
	}
	resizeTable();
}
function hideTeach(left){
	if(type == typeMICROSOFT && document.readyState != "complete")
		return;
	var table=document.getElementById("resource_table");
	var img =document.getElementById("hideshowImg2");
	var colorid = document.getElementById("clickbelow");
	var blueMess = document.getElementById("blueMess");
    var scheduledAppt = document.getElementById("scheduledAppt");
    var unavailableAppt = document.getElementById("unavailableAppt");
    if(table==null)
		return;
	if(table.style.display==""){
		table.style.display="none";
		img.src="images/show.gif";
		img.title="show the legend";
        if (left){
             colorid.style.display="none";
		     if (blueMess != null)
				blueMess.style.display="";
        }
        else{
             colorid.style.display = document.getElementById("schooltab")? "": "none";
             if (blueMess != null)
				blueMess.style.display="none";
             if (scheduledAppt != null)
				scheduledAppt.style.display="none";
             if (unavailableAppt != null)
				unavailableAppt.style.display="none";
         }
    }
	else{
		table.style.display="";
		img.src="images/hide.gif";
		img.title="hide the legend";
		colorid.style.display="none";
		if (blueMess != null) blueMess.style.display="";
        if (scheduledAppt != null) scheduledAppt.style.display="";
        if (unavailableAppt != null) unavailableAppt.style.display="";
    }

	resizeTable();
}

function blackenAppts(eventType){
   var apptsTable=document.getElementById("schedule");
   if(apptsTable==null)
	  return;
   var nrows = apptsTable.rows.length;
   var row;   
   for (i=0; i<nrows; i++){
	   row=apptsTable.rows.item(i);
	   var data= new dataParser(row.id);
	   var table=document.getElementById("tableid"+data.peopleID);
	   if(table==null )
		   continue;
		var firstApptRow = getFirstApptRow(table);
		var ncols=table.rows.item(firstApptRow).cells.length-1;
	   var form=document.getElementsByName("TheForm"+data.peopleID).item(0);
	   var start_time = form.start_time.value;
	   var inc_time = parseInt(form.inc_time.value);
	   var num_resources = form.resourcesize.value;
	   if((data.duration%inc_time)>0)
		   continue;
	   row=table.rows.item(1);
	   for(j=1; j<row.cells.length; j++){
		 if(row.cells.item(j).id==data.apptDate){
			 var ny=(data.startTime-start_time)/inc_time;
			 var k;

             for(k=0; k<resIDs[data.peopleID].length && (resIDs[data.peopleID][k]!=data.remID  || (classSlotIDs[data.peopleID][k]!=null && data.classSlotID>0 && classSlotIDs[data.peopleID][k]!=data.classSlotID)); k++);
			 if(k==resIDs[data.peopleID].length)
				break;
             var  Index=num_resources*(j-1)+k+1;
             var apptrow = table.rows.item(ny+firstApptRow);
             var o =findCell(apptrow, Index);
             if(o==null || data.apptTime.indexOf(getStartTime(table, o.parentNode.rowIndex))==-1)
				break;
            o.rowSpan= data.duration/inc_time;

            setBlack(o, eventType);

            for(var t=inc_time, l=1; t<data.duration; t+=inc_time, l++){
                   apptrow = table.rows.item(ny+firstApptRow+l);
                    o =findCell(apptrow, Index);
                    if (o != null)
                        o.parentNode.removeChild(o);
                }
            break;
		 }
	   }
   }
}
function findCell(apptrow, index){
    for (l=1;l<apptrow.cells.length; l++ ){
         if (apptrow.cells.item(l).getAttribute('Index')==index)
            return apptrow.cells.item(l);
     }
}
function setBlack(cell, eventType){
	if(type == typeNETSCAPE && eventType!=1)
		cell.innerHTML='&nbsp;';
	cell.style.backgroundColor='#000000';
}
function dataParser(id){
	var i=id.indexOf('_');
	this.peopleID=parseInt(id.substring(0, i));
	var j=id.indexOf('_', i+1);
	this.remID=parseInt(id.substring(i+1, j));
	i=id.indexOf('_', j+1);
	this.duration=parseInt(id.substring(j+1, i));
	j=id.indexOf('_',i+1);
    this.classSlotID=parseInt(id.substring(i+1, j));
    i=id.indexOf('_',j+1);
    this.apptDate=id.substring(j+1,i);
	j=id.indexOf(':',i+1);
	this.apptTime=id.substring(i+1);
	this.startTime= parseInt(id.substring(i+1,j))*60;
	if (id.indexOf('0',j+1) == j+1)
		j += 1;
	this.startTime += parseInt(id.substring(j+1));
}
function removeFltr() {
	document.getElementById("actid").value = "-1";
	document.getElementById("MainForm").submit();
}
function askValue(evt) {
    var secField = document.getElementById("secret"), secCheck;
    var secValue = secField.value;
    if(secValue == null || secValue == "")
       return false;
    if(secValue.length != 2) {
	   alert("Please, enter a 2 digit number.");
	   return false;
    }
	secCheck = (new Number(secValue)).toString();
	if(secCheck.length == 1)
	   secCheck = "0"+secCheck;
    if(secCheck != secValue){
	   alert("The text you entered doesn't seem to be valid number!");
	   return false;
    }
    secField.value = secValue;
    return true;
}

function finishSepateLegend(){
    var tds = document.getElementsByName('legendHolder');        
    for(var i=0; i<tds.length; i++){
        var childNodes =tds.item(i).childNodes;
        var maxWidth=0, currWidth;
        for(var j=0; j<childNodes.length; j++ ){
            if(childNodes.item(j).tagName.toLowerCase()!='div'){
                currWidth=childNodes.item(j).offsetWidth;
                if(currWidth>maxWidth)
                    maxWidth=currWidth;
            }
        }
        for(var j=0; j<childNodes.length; j++ ){
            if(childNodes.item(j).tagName.toLowerCase()!='div')
                childNodes.item(j).style.width=maxWidth;

        }
    }
}

function notifyParent() {
	var bod = document.body;
	var el = parent.window.document.getElementById("theiframe");
	if (el != null) {
		el.height = bod.scrollHeight+20;
	}
}

function setRptLinks(){
    var obj = document.getElementById("tdBookAppt");
    
    if(obj == null)
        return;

    try{
        rptVenID;
    }catch(e){
        return;
    }

    if(obj.style.display == "none" && rptVenID > 0){
        var a = document.getElementById("aBookAppt");
        a.href = "client?ven=" + rptVenID + "&event="+ rptEventID;
        obj.style.display = "";
    }
}
var divBase = null;
var divLegend = null;
var LastPosY = 0;
function scrollLegend() {
	
	try {
		if (divBase == null)
			divBase = document.getElementById("base");

		if (divLegend == null)
			divLegend = document.getElementById('divLegend');

		if (getHeight(divLegend) > getHeight(divBase)-20) {
			LastPosY = 0;
			return;
		}

		var y = divBase.scrollTop;
		if (y != LastPosY) {
			if (window.opera) 
				divLegend.style.pixelTop = y;
			else if (document.layers) 
				divLegend.top = y;
			else if (document.getElementById) 
				divLegend.style.top = y + 'px';
			else if (document.all) 
				divLegend.style.pixelTop = y;

			LastPosY = y;
		}
	} catch (e) { ; }
}

function getHeight(obj) {
	return obj.offsetHeight ? obj.offsetHeight : obj.style.height;
}

function hideStudentTeachers(id){
	
	var arr = document.getElementsByName(id);
	var img = document.getElementById("hideshow" + id);
	var hidden = img.getAttribute("hidden")==true || img.getAttribute("hidden")=='true';

	img.setAttribute("hidden", !hidden);
	img.src = hidden? "images/hide.gif": "images/show.gif";
	img.title = hidden? "hide student's teachers": "show student's teachers";

	for(var i = 0; i < arr.length; i++)
		arr[i].style.display = hidden? "": "none";
}