Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Choosing date via calendar


Message #1 by aoadeyemo@h... on Thu, 14 Jun 2001 14:00:09
Hi, all,
Can anyone pls help. I've picked up a calendar and i'm trying to make it 
suit the purpose of my project. It's working fine but i don't want the 
users to be able to select a weekend. the code is as shown below. 
The page is an include file in an asp app. 

ignore the backdatevalue below, thats from the main asp app.

Thanx
Ade'

<HTML>
<HEAD>
<!--
Trying to prevent weekends (sat & sun) being chosen from this calendar. 
Thanx. -->
<TITLE>Select a Date</TITLE>
<LINK rel="stylesheet" type="text/css" href="css/etc.css"/>
<SCRIPT language=SERVERASP>
<!-- Language=VBScript -->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">

<!--

 // Set this variabel to 1 if you want to be able to click through the 
month
 // If the variabel is anything else than 1 (e.g. 0), you will only be 
able to see the current month
var usesaved = 1; // valid variables = 1 or 0 

// Change the day- and month abbreviation for your own language
var dn = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); 
var ma = new Array
('January','February','March','April','May','June','July','August','Septemb
er','October','November','December');

// If daylink is anyting else than 1, you're missing the link for each day
// The format for the links in a month explains best by example:
// 19980104.html = Year:1998, month:01, day:4
// 20011202.html = Year:2001, month:12, day:2
var daylink = 1; // valid variables = 1 or 0 

// If linktarget not is an empty string, you'll get target on each daylink
// Example: var linktarget = '_blank'; opens a new window when clicking on 
a day
var linktarget = 'content';

// if the variable 'daylink' is set to 1, this one let you specify if you
// want to show links only up to the current date
//var linkstoptoday = 2;  // valid variables = 0, 1 and 2

// Change this to your favourite font types and size for all the text
//var fontface = 'arial, helvetica';
//var fontsize = '-2'; 

// Special characters for left and right arrow
var leftarr = '<IMG SRC="images/leftarrow.gif" WIDTH=9 HEIGHT=8 
BORDER="0">';
var rightarr = '<IMG SRC="images/rightarrow.gif" WIDTH=9 HEIGHT=8 
BORDER="0">';



// *******************************************
// Don't change the code below this point
// *******************************************



var nyDato = new Date();
var currmonth = new Date();
var calcook;
var globyear, globmonth, globday;

function setFormDate(ddmmyy)
{
//should be able to make this dynamic via ASP
//Obviously we want some kind of formbyname and controlbyname methods
//to give us search.date in the following;
//    opener.document.Search.Date.value = ddmmyy;
//but this dynamic asp will suffice for now.
<%
    dim strControl
	strControl = Request.QueryString("Control") + ""
	
	if strControl = "" then strControl = "txtFrom"
	  
	Response.write("opener.document." + strControl + ".value = 
ddmmyy;")
%>	
	opener.focus();
	close();
}

function makeCalender(year,month,day,linkstoptoday){
var il = makeCalender.arguments.length;
var spart;
if(usesaved==1){
   spart=GetCookie('calender');
	 if(spart!=null&&spart!='0'){
		 var rekke = spart.split('&');
		 //CB 24/2/00: added Y2K fix for Netscape
		 if (rekke[0]<1000) nyDato.setYear((rekke[0]*1)+1900);
		  else nyDato.setYear(rekke[0]);
		 nyDato.setMonth(rekke[1]);
		 nyDato.setDate(rekke[2]);
    }
	else{
	if(il==3){
		 nyDato.setYear(year)
		 nyDato.setMonth(month);
		 nyDato.setDate(day);
		}
	}
}
	year = nyDato.getYear(); //getYear  =  76 hvis årstallet er før 
2000 etter år 2000 returneres 2001
	month = nyDato.getMonth();
	day = nyDato.getDate();
	var diw = nyDato.getDay(); //getDay   = day of week 0-6
	
globyear = year;
globmonth = month;
globday = day;
var calender, cal;
var as1, as2, ae, bs1;
if(daylink==1){
//  var as1 = '<A HREF="search_advanced.asp?Date=';

  //CB var as1 = '<A ONCLICK="selectDate()" HREF="search_advanced.asp?
Date=';
	as1 = '<A class=Calendar HREF="javascript:setFormDate(\'';
	bs1 = '<A class=CalendarToday HREF="javascript:setFormDate(\'';
	
  if(linktarget != ''){
	  //CB var as2 = '" TARGET="' + linktarget + '">';
	  var as2 = '\')">';
  }
  else{
	  var as2 = '\')">';
  }
  var ae = '</A>';
}

if(year<2000)year+=1900;  // JavaScritt has it own year 2000 "problem"

cal = dn[diw+1] + ' ' + year + ' ' +month+' '+day;
var ts = '<TABLE WIDTH="160" BORDER="0" CELLSPACING="1" CELLPADDING="0" 
class=CalendarGrid>\n';
var tq = '<TABLE WIDTH="160" BORDER="0" CELLSPACING="0" 
CELLPADDING="0">\n';
var te = '</TABLE>';
var tf = '<TABLE WIDTH="160" BORDER="0" CELLSPACING="0" CELLPADDING="0" 
class=CalendarGrid>\n<TR><TD ALIGN="left" VALIGN="bottom"><img 
src="images/lower_left_corner.gif" width=9 height=9 border=0 
alt=""></TD><td><font class=CalendarFont> </td></TR>\n</TABLE>';
var rs = '<TR>';
var re = '</TR>\n';
var cs = '<TD ALIGN="CENTER" WIDTH="14%" class=CalendarBackground><FONT 
class=CalendarFont>';
var cq = '<TD ALIGN="CENTER" WIDTH="14%" class=CalendarHeading><FONT 
class=CalendarFont>';
var ce = '</FONT></TD>\n';

cal = tq + rs;
if(usesaved==1)cal += '<TD ALIGN="LEFT" class=CalendarGrid><img 
src="../images/sp.gif" width=9 height=9 border=0 alt=""></TD><TD 
ALIGN="CENTER" class=CalendarGrid> <FONT class=CalendarFont';
if(usesaved==1)cal += '><A HREF="javascript:go
(1);">'+leftarr+'</A></FONT></TD>';
if(usesaved==1)cal += '<TD COLSPAN="5"';
else cal+='<TD COLSPAN="7"';
cal += ' ALIGN="CENTER" class=CalendarGrid><FONT class=CalendarTitleFont 
>';
cal += ma[month] +', ' + year + ce;
if(usesaved==1)cal += '<TD ALIGN="CENTER" class=CalendarGrid><A 
HREF="javascript:go(2);">'+rightarr+'</A></TD><TD ALIGN="RIGHT" 
class=CalendarGrid valign=top><img src="images/top_right_corner.gif" 
width=9 height=9 border=0 alt=""></TD>';
cal += re+te;
cal += ts+rs+cq;
for(i=0;i<6;i++)cal += dn[i] + ce + cq;
cal += dn[6] + ce + re;

var tmp;
nyDato.setDate(1);
nyDato.setMonth(month);
var midl;
var diu = nyDato.getDay();
t = nyDato.getMonth();

for(i=0;i<diu;i++){cal += cs +' ' + ce;}
var fresh = new Date();
var nu = new Date();

var bs = '<FONT class=CalendarToday>';
var be = '</FONT>';

fresh = fresh.getDate();
for(i=1;i<32;i++){
  gd=nyDato.getDate();
  gm=nyDato.getMonth();
  gy=nyDato.getYear();
  var ngy = new String(gy)
//  ngy=ngy.substring(ngy.length-2,ngy.length)
//  gy=gy.substring(gy.length-2,gy.length)
//  if(gy<2000)gy+=1900;

  if((gd>1||(gd==1&&i==1))&&gm==month){
      diw = nyDato.getDay();
	  if(diw==0)cal+=rs;  // Hvis første day i uken
	  if(diw!=7){ 			 // Hvis ikke siste day i uken
		tmp1 = gm;
		tmp2 = gd;
		var tmpqs1;
		var tmpqs2;
	    if(tmp1<9){tmpqs1 = '0' + (tmp1 + 1)} else {tmpqs1 = tmp1 + 1};
	    if(tmp2<10){tmpqs2 = '0' + tmp2} else {tmpqs2 = tmp2};
	var strlen;
	    if(daylink==1){	//this bit automatically generates the URL 
for the dates
		  if(linkstoptoday==1&&nu.valueOf()<=nyDato.valueOf() || 
linkstoptoday==2){
			if(fresh==gd&&globmonth==currmonth.getMonth())cal 
+= cs + bs + bs1 + tmpqs2 + '/' + tmpqs1 + '/' + ngy + as2 + gd + ae + be 
+ ce;
			  else cal += cs + as1 + tmpqs2 + '/' + tmpqs1 
+ '/' + ngy + as2 + gd + ae + ce;
			}
		 else{
            if(fresh==gd&&globmonth==currmonth.getMonth())cal += cs + bs + 
tmp2 + be + ce;
       	          else cal += cs + tmp2 + ce;
		  }
		}
		else{
		  if(fresh==gd&&globmonth==currmonth.getMonth())cal += cs 
+ bs + tmp2 + be + ce;
		  else cal += cs + tmp2 + ce;
		}
	  }
	  if(diw==7)cal+=re;  // Hvis siste day i uken
  }
  nyDato.setDate(gd+1);
}

if(diw<6){
  for(i=diw;i<6;i++){
    if(i<7)cal += cs +' '+ce;
	if(i==6)cal += re;
  }
}

cal += te + tf;
calender = cal;
return calender;

}

function go(hva){
  if(hva==1)SaveCookie(globyear,globmonth-1,globday);
  else SaveCookie(globyear,globmonth+1,globday);
  document.location=document.location;
}



// *************************************** Start cookie  
******************************
// *************************************** Start cookie  
******************************
// If visit before: get saved data


function getCookieVal(offset) 
   {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
   }

function GetCookie(name){
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;
   while (i < clen) 
      {
      var j = i + alen;
      if (document.cookie.substring(i, j) == arg)
         return getCookieVal (j);
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) 
         break; 
      }
  return null;
}

function SetCookie(name, value){
   var argv = SetCookie.arguments;
   var argc = SetCookie.arguments.length;
   var expires = (2 < argc) ? argv[2] : null;
   var path = (3 < argc) ? argv[3] : null;
   var domain = (4 < argc) ? argv[4] : null;
   var secure = (5 < argc) ? argv[5] : false;
   document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires="+expires.toGMTString())) +
     ((path == null) ? "" : ("; path=" + path)) +
     ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
   }


function SaveCookie(yearvar,monthvar,dayvar){
   var expdate = new Date();
   expdate.setTime(expdate.getTime() +  (60 * 60*1000*3)); 
   SetCookie("calender", yearvar+'&'+monthvar+'&'+dayvar, expdate, "/", 
null, false);
}


function clearCookie(){
  var tid = new Date();
  tid.setTime(tid.getTime()+(60*60))
   SetCookie("calender", 0, tid, "/", null, false);
   history.go(0);
}

function selectDate()
{
  window.close()
}

//-->
</SCRIPT>

</HEAD>
<BODY BGCOLOR="#FFFFff" LINK="#OOOOOO" ALINK="#OOOOOO" VLINK="#OOOOOO">

<SCRIPT LANGAUGE="JavaScript">
<%
  dim strYear, strMonth, strDay, strBackValue
  dim Today 
  
  'Prevent user from entering back valued dates by setting strBackValue to 
1, i.e. only dates forward from this point in time are accepted  
  strBackValue = Request.QueryString("BackValue")
  if strBackValue = "" or strBackValue = "2" then 
    strBackValue = "2"
  else
    strBackValue = "1"	
  end if
  
  Today = Date()
  strYear  = Cstr(DatePart("yyyy", Today))
  strMonth = Cstr(DatePart("M", Today))
  strDay   = Cstr(DatePart("d", Today))  
  Response.write("document.write(makeCalender(" + strYear + "," + strMonth 
+ "," + strDay + "," + strBackValue + "));")

%>	
</SCRIPT>

<A class=calendar HREF="javascript:clearCookie()"><FONT 
class=CalendarFont>Today</FONT></A>
<LAYER width=468 height=60 visibility="hidden" onLoad="moveToAbsolute
(layer1.pageX,layer1.pageY);clip.height=60;clip.width=468; 
visibility='show';"></LAYER>
</BODY>
</HTML>


  Return to Index