<!--
// calmgmt.js

var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = y2k(today.getYear());
var newpage = './calmgmt.php';
var output = '';
function verify() {
  vunit  = document.myform.Unit.options[document.myform.Unit.selectedIndex].value + '';
  if (vunit == 0) { window.defaultStatus="Select Unit"; return false; }   
  else			  { window.defaultStatus="";			return true;  }			  
}
function verifyUpd(pcks,x) {
  pcks="'" + pcks + "'";
  uid=document.myform.userId.value;
  pw =document.myform.pw.value;
  //alert ('verifyUid-'+uid+','+pw+' pcks='+pcks+' x='+ x );
  if (verify()) { changeUnit( 1,x,pcks,uid,pw ); return true;  }
  else			{						         return false; }
}
function Calendar(UNIT,MONTH,YEAR,Mgr,v,pck,pnote) {
	//alert("Calendar entry"+UNIT+ ",y"+YEAR +",m"+MONTH+",mgr"+Mgr+",v"+v+",pck"+pck+',pnote'+pnote);
	pcks  = "'" + pck   + "'";
	pnote = "'" + pnote + "'";
	//alert("adj pcks"+pcks+',pnote'+pnote);
	YearMin = 2004;
    YearMax = year + 2;
	if(YEAR==2004 && MONTH<9) MONTH=9; // Oct2004 data on db is incomplets so first good data is Nov2004
    var output = '';
    output += '<FORM name="myform" method=GET action="' + newpage + '" >';
    output += '<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH=20% HEIGHT=20% BORDER=0>';
	output += '<TR><TD height=30 ALIGN=CENTER>';
	output += '<SELECT NAME="Unit" onChange="changeUnit(' + Mgr + ', 0,' + pcks + ',0,0);">';
    for (nunit=0; nunit<=20; nunit++) {
        if (nunit == 5)  nunit++;
		if (nunit == 8)  nunit++;
		if (nunit == 18) nunit++;
        if (nunit == UNIT) output += '<OPTION VALUE="' + nunit + '" SELECTED>' + uname[nunit] + '<\/OPTION>';
		else               output += '<OPTION VALUE="' + nunit + '">'          + uname[nunit] + '<\/OPTION>';
    }
    output += '<\/SELECT>';
	 //display month selection pull-down box
	output += '&nbsp;&nbsp;<SELECT NAME="Month" onChange="changeUnit('+ Mgr + ', 0,' + pcks + ',0,0);">';
    for (month=0; month<12; month++) {
        if (month == MONTH) output += '<OPTION VALUE="' + month + '" SELECTED>' + names[month] + '<\/OPTION>';
        else                output += '<OPTION VALUE="' + month + '">'          + names[month] + '<\/OPTION>';
    }
    output += '<\/SELECT>';
	
	// display year selection pull-down box
	output += '&nbsp;&nbsp;<SELECT NAME="Year" onChange="changeUnit('+ Mgr + ', 0,' + pcks + ',0,0);">'; 
    for (lyear=YearMin; lyear<=YearMax; lyear++) {
        if (lyear == YEAR) output += '<OPTION VALUE="' + lyear + '" SELECTED>' + lyear + '<\/OPTION>';
        else               output += '<OPTION VALUE="'  + lyear + '">'          + lyear + '<\/OPTION>';
    }
    output += '<\/SELECT><\/TD><\/TR>';
	//return output;	// for testing, especially useful in locating syntax errors debugger cannot locate " 

	// display date as title to the calendar
    output += '<TR><TD class=bold HEIGHT=25 ALIGN=CENTER>';
    output += names[MONTH] + ' ' + YEAR + '<\/TD><\/TR>'
	output += '<TR><TD ALIGN=CENTER VALIGN=BOTTOM>';

    if (((YEAR % 4 == 0) && (YEAR % 100 != 0))   || (YEAR % 400 == 0))
         days[1] = 29; 
    else days[1] = 28;

    // display dow in calendar heading
	output += '<TABLE CELLSPACING=1 CELLPADDING=0 BORDER=0><TR>';
    for (i=0; i<7; i++) {
     output+='<TD BGCOLOR=#000000 HEIGHT=15 CLASS=dbtbl ALIGN=CENTER><font color=WHITE>'+dow[i]+'<\/font><\/TD>';
	}
	output += '<\/TR>';
	
	// display last days of prior month as required in first row of calendar
	output += '<TR ALIGN=CENTER>';
    var column = 0;
    var lastMonth = MONTH - 1;
    if (lastMonth == -1) lastMonth = 11;
    
    firstDay = new Date(YEAR,MONTH,1);
    startDay = firstDay.getDay();
	
	for (i=0; i<startDay; i++, column++) {
        output += '<TD BGCOLOR="#CCCCCC" height=40 width=40>' + (days[lastMonth]-startDay+i+1) + '<\/TD>'; }

// display calendar days with selection checkboxes
//------------------- PUBLIC DISPLAY VIEW --------------------------------
if (! Mgr) {
	for (i=1; i<=days[MONTH]; i++, column++) { 
		c = pcks.substr(i,1);  // position 0 is a quote (')
		//alert('calendar-VIEW i ' + i + ',' + pcks.length + ' c' + c);
		if (UNIT==0)       {color = 'WHITE';          fcolor="BLACK"; }
		else if (c=='Y')  {color = 'BGCOLOR=RED'    ; fcolor="WHITE"; }
		else { if (c=='T'){color = 'BGCOLOR=#E8c873'; fcolor="BLACK"; }
		       else		  {color = 'BGCOLOR=GREEN'  ; fcolor="WHITE"; } }
		output += '<TD '+ color + ' height=40 width=40><font color='+ fcolor +'>'+ i +'</font></TD>';
		if (column == 6) { output += '</TR><TR ALIGN=CENTER VALIGN=MIDDLE>';
						   column = -1; }
    }    
} else {
//-------------------------- MANAGER UPDATE VIEW -------------------------	
	for (i=1; i<=days[MONTH]; i++, column++) { 
		c = pcks.substr(i,1);  // position 0 is a quote (')
		//alert('calendar-UPDATE i' + i + ',' + pcks.length + ' c' + c);
		ck=''; ck2='';
		if (c=='Y')		   { color = 'BGCOLOR=RED'    ; ck  = 'CHECKED'; fcolor="WHITE"; }
		else { if (c=='T') { color = 'BGCOLOR=#E8c873'; ck2 = 'CHECKED'; fcolor="BLACK"; }
		       else		   { color = 'BGCOLOR=GREEN'  ;				     fcolor="WHITE"; } }
		if (UNIT==0) {ck +=' DISABLED'; ck2 +=' DISABLED'; }
		output += '<TD '+ color + ' height=40 width=40><font color='+ fcolor +'>' + i + '</font>';
		output += '<input type=checkbox name="cka" ' + ck + ' >\n';  //auto numbers cka array starting from 0 
		j=i-1;
		output += '<input type=radio name="ck'+j+'" '+ ck2 +' ondblclick="toggl('+j+');"></TD>'; 
		if (column == 6) { output += '</TR><TR ALIGN=CENTER VALIGN=MIDDLE>';
					       column = -1; }
	} // end for i
}    // end else (mgr)
//------------------------------------------------------------------------
	// display first days of next month as required in last row of calendar
	if (column > 0) { for (i=1; column<7; i++, column++)
					  output += '<TD BGCOLOR="#CCCCCC" height=40 width=40>' + i + '<\/TD>';	}
    output += '</TR></TABLE></TD></TR></TABLE>';  
	
	// conditionally display comments text box and UPDATE button
	if (Mgr) {
		var r2 = /(CR)(LF)/g;
		pnote = pnote.replace(r2,'\r\n');  
		pnote = unescape(pnote);
		var r1 =/'/g;
		output += '<br>Rental Contact Notes - ' + uname[UNIT] + " " + names[MONTH] + " " + YEAR + '<br>';
		output += '<textarea rows="8" cols="64" wrap="soft" name="note" >' + pnote.replace(r1,'');
		output += '</textarea><center>UserId <input type=text name="userId" maxlength="15" size="15">';		
		output += 'Password <input type=PASSWORD name="pw" maxlength="10" size="10">';		
		output += '&nbsp;<input type="Button" value="UPDATE" ';		
	    output += 'onClick="verifyUpd(pcks,1);">'; 
		output += '<br><\/center>'; }

	output += '<\/FORM>';
	//alert ('end calendar '+ output);
	return output; 
}
function changeUnit ( mgr,v,pck,uid,pw ) {
	//alert('changeUnit entry -' + mgr + ',' + v + ',' + pcks +','+uid+','+pw );
	ounit   = document.myform.Unit.options[document.myform.Unit.selectedIndex].value ;  
	oyear   = document.myform.Year.options[document.myform.Year.selectedIndex].value ; 
	omonth  = document.myform.Month.options[document.myform.Month.selectedIndex].value ;
	
	 // ----- code to change pck & note not needed unless updating (v=1) - db values control display
	  if (v == 1) { //alert('changeUnit update');
	   cks='';
	   for ( i=0; i<31; i++ ) { if (i >= days[omonth] )										  cks += 'N';
								else {tempObj=eval("document.myform.ck"+i);  
									  //alert ('i'+i+' ckbox='+ eval("document.myform.cka[" + i + "].checked"));
									  if (eval("document.myform.cka[" + i + "].checked") == true) cks += 'Y';
									  //alert ('v'+v+',i'+i+',radio='+eval("tempObj.checked"));     //diagnostics
									  else { if (eval("tempObj.checked") == true)			  cks += 'T';	
											 else											  cks += 'N'; }
								     } // end else
							    }     // end if i
	   //alert('changeUnit update cks='+cks);
	   pck="'" + cks + "'";
	   var r1 =/[\'\"+\+\\]/g;	  // define regular expression to eliminate all '"+\ [$'"<>&;,%\] special chars
	   var r2 =/\\/g;
 	   var r3 =/(\n)/g;		    //  regular expression for new line
	   var r4 =/(\r)/g;		   //   regular expression for carriage return
	   t = document.myform.note.value;
	   t = t.replace(r3,'LF');
	   t = t.replace(r4,'CR');  
	   t = t.replace(r1,'') ;	  // strip all single quote (apostrophes) in text
	   t = escape(t);
	   t = "'" + t + "'";   // re-wrap text in (single) quotes for storage on db  
	   t=t.replace(r2,'');		  // single quote wrapper gets escaped so eliminate backslash (invalid param)
	   pck=pck.replace(r2,'');
	   //alert('changeUnit update pck='+pck+',t='+t);
	  }
	  else { pck= "'" + pck   + "'";  
		     t  = "''";	}//alert('changeUnit inq');	  
	newpage  = newpage + "?ounit=" + ounit + "&omonth=" + omonth + "&oyear=" + oyear ;
	newpage += "&mgr="+ mgr +"&v="+ v +'&cks='+ pck +'&t='+ t +'&userId='+ uid +'&pw='+ pw;								   
	window.location.href = newpage;  
	return true; 
}
function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

//--------------------------------- RADIO BUTTON CONTROL ------------------------------------------
function toggl(buttonNb) {
   tempObj=eval("document.myform.ck"+buttonNb);  
   //alert(buttonNb+','+tempObj+','+eval("tempObj.value")); // diagnostic
   if (eval("tempObj.checked") == true) tempObj.checked = false; 	
}
//-------------------------------------------------------------------------------------------------
function makeArray0() {
    for (i = 0; i<makeArray0.arguments.length; i++) this[i] = makeArray0.arguments[i];
}

var uname     = new makeArray0('Select Unit'        ,'Unit 1 - 3br '    ,'UNIT 2 - 1br' ,'Unit 3 - 1br'      ,
							   'Unit 4 - 1br'       ,'Unit 5 - Lndry'   ,'Unit 6 - 1br' ,'Unit 7 - Studio'   ,
							   'Unit 8 - Lobby'     ,'Unit 9 - 1br'     ,'Unit 10 - 1br','Unit 11 - 3br (upper)',
							   'Unit 12 - 1br (upper)' ,'Unit 13 - 1br (upper)'    ,'Unit 14 - 1br (upper)'   ,
							   'Unit 15 - Efncy (upper)','Unit 16 - 1br (upper)'   ,'Unit 17 - Studio (upper)',
							   'UNIT 18 - 2br (upper)' ,'Unit 19 - Studio (upper)' ,'Unit 20 - 1br (upper)'   );
							   // units 2 & 18 not avail (never rented)
var names     = new makeArray0('January','February','March','April','May','June','July',
                               'August','September','October','November','December');
var days      = new makeArray0(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var dow       = new makeArray0('S','M','T','W','T','F','S');

// DO NOT LAUNCH WITHOUT NIGHTLY BACKUP!!!!
//-->