// retrieve an array of the current set date in the date field
// returns Array(month,day,year)
function getCurrentDate(){
 return document.getElementById('birthdate').value.split("/");
}


// places the date in the "date" input field when calendar is clicked
// and highlights that day on the calendar
function s(month,day,year){
 currentDate = getCurrentDate();
 try{(document.getElementsByName(currentDate[1])[0]).className='day';}
 catch(e){}//incase new month has more days than last
 if (month < 10) { month = "0"+month;}
 document.getElementById(day).className='highlighted';
 if (day < 10) { day = "0"+day;}
 document.getElementById('birthdate').value = day+"-"+month+"-"+year;
 document.getElementById('calendar').style.display="none";
}

// navigate the calendar left/right (backward/forward)
function calendarNav(direction){
 switch (direction){
  case "left":{
  	//alert(newMonth);
   if(d.getMonth()=="0"){newMonth=11;newYear=d.getFullYear()-1;}
   else{newMonth=d.getMonth()-1;newYear=d.getFullYear()}
   break;
  }
  case "right":{
   if(d.getMonth()==11){newMonth=0;newYear=d.getFullYear()+1;}
   else{newMonth=d.getMonth()+1;newYear=d.getFullYear();}
   break;
  }
  default:{
   // do nothing. basically, if you manually set the date field
   // you can use calendarNav() with no args to refresh what day
   // is highlighted.
   // this however is only useful if you are in the same month.
   // to display a custom calendar you should really use
   // placeCalendar(month,year,day);
   break;
  }
 }

 // refresh the calendar
 placeCalendar(newMonth,newYear);

 // preserve date highlighting
 currentDate = getCurrentDate();
 if(typeof currentDate[1]!="undefined"){
  if ((currentDate[0]==newMonth+1)&&(currentDate[2]==newYear)){
   s(newMonth+1,currentDate[1],newYear);
  }
 }
}



// write/refresh calendar to the calendar div
// all args optnl:
//  month - sets month of calendar; 0=january
//  year - sets year
//  day - highlight a given day

function placeCalendar(month,year,day){
 // main date object
 d=new Date();

 // load the date object with provided vars if any
 if(typeof month!="undefined") 
 {
 	d.setFullYear(year,month,day);
 }
 else
 {
	 d.setFullYear(1985,0,1);
 }

 // use this copy of the date object for local manipulation
 dx=d;

 // bring date vars to local ones
 month=d.getMonth();
 year=d.getFullYear();
 day=d.getDate();

 // configure months and weeks
 days=new Array('Pz','Pzt','Sa','Çar','Per','Cu','Cts');
 months=new Array('Ocak','Şubat','Mart','Nisan','Mayıs','Haziran','Temmuz','Ağustos','Eylül','Ekim','Kasım','Aralık');
 mlen=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
 if(d.getFullYear()%4==0){mlen[1]=29;} //leapyear

 // calendar start html
 cal="<table class=\"calendar\" cellpadding=\"0\" cellspacing=\"0\"><tr>";
 cal+="<td class=\"nav\"><a href=\"javascript:{}\" onclick=\"calendarNav(\'left\')\">&laquo;</a></td>";
 cal+="<td colspan=\"5\" class=\"month\" align=\"center\">"+months[month]+" "+year+"</td>";
 cal+="<td class=\"nav\"><a href=\"javascript:{}\" onclick=\"calendarNav(\'right\')\">&raquo;</a></td></tr><tr>";
 for(i=0;i<=6;i++){cal+="<td class=\"weekday\" id=\""+days[i]+"\">"+days[i]+"</td>";}
 cal+="</tr><tr>";

 // iterate the days and insert them
 for(i=1;i<=mlen[dx.getMonth()];i++){
  dx.setDate(i);
  iday=dx.getDay();

  // starting at appropriate day of the week
  if(i==1){
   for(j=0;j<iday;j++){
    cal+="<td>&nbsp;</td>";
   }
  }
  if((iday==0)&&(i!=1))cal+="<tr>";
  cal+="<td id=\""+i+"\" class=\"day\"><a href=\"javascript:{}\" onclick=\"s("+(month+1)+","+i+","+year+");\">"+i+"</a></td>";
  if((iday==6)||(i==days[month]))cal+="</tr>";
 }

 // close the calendar table
 cal+="</table>";

 // write out the calendar to the div
 calObj = document.getElementById('calendar');
 calObj.style.display="block";
 calObj.innerHTML=cal;
 calObj.style.position = "absolute";
 calObj.style.zIndex = "1000";
 calObj.style.backgroundColor = "#000000";
 calObj.style.border = "1px solid #000000";
 calObj.style.opacity = .6;

 // if a day was specified as an argument, highlight it
 if (typeof highlightDay!="undefined")s(month+1,highlightDay,year);

 // and by default, if there currently is no selected date at all
 // then select the current day
 //currentDate = getCurrentDate();
 //if (typeof currentDate[1]=="undefined")s(month+1,day,year);
}