want an urgent help(Calender)

Discussion in 'JavaScript' started by shinoy, Feb 19, 2007.

  1. #1
    Hi,

    I have to display fromDate and toDate from 2 calenders in to the text boxes,but issue is that when i click the fromDate's calender i want to display toDate text box with 7days more than the fromDate . it should happen at the point when i am clicking the FromDate's calender.How can i do it using javascript. I am using date pickers.Expecting your help soon

    its position is as follows:

    FromDateTextBox calender1 toDateTextBox calender2


    when i click the calender1 ,i want to display the selected date to FromDateTextBox and display toDateTextBox with 7 days greater than FromDateTextBox at a sametime.ie with 1 click it should happen using javascript. we are using DatePicker for calender in this project.Soon give me the code for this

    i am using the following calender. can i make any change in the following calender.js file to acieve the goel?

    // calendar.js

    document.write("<font color=white>");
    document.write(setDate());


    function setDate() {
    this.dateField = opener.dateField;
    this.inDate = dateField.value;
    var defaultToday = false;

    // REPLACE DASHES WITH SLASHES
    re = /-/g;
    inDate = inDate.replace(re, '/');

    // SET DAY MONTH AND YEAR TO TODAY'S DATE
    var now = new Date();
    var day = now.getDate();
    var month = now.getMonth();
    var year = now.getFullYear();

    // IF A DATE WAS PASSED IN THEN PARSE THAT DATE
    if (inDate.indexOf('/') != -1) {

    var inDay = inDate.substring(0,inDate.indexOf("/"));
    if(inDay.substring(0,1) == "0" && inDay.length > 1) inDay = inDay.substring(1,inDay.length);
    inDay = parseInt(inDay);

    var inMonth = inDate.substring(inDate.indexOf("/") + 1, inDate.lastIndexOf("/"));
    if (inMonth.substring(0,1) == "0" && inMonth.length > 1) inMonth = inMonth.substring(1,inMonth.length);
    inMonth = parseInt(inMonth);

    var inYear = parseInt(inDate.substring(inDate.lastIndexOf("/") + 1, inDate.length));


    if (inDay <= 0 || inMonth <= 0 || inYear <= 0) {
    alert(calenderJSAlert1);
    defaultToday = true;
    }

    if (inDay) {
    day = inDay;
    }
    if (inMonth) {
    month = inMonth-1;
    }
    if (inYear) {
    year = inYear;
    }
    }else{
    if (inDate != "")
    alert(calenderJSAlert1);
    defaultToday = true;
    }

    if (!checkDateFormat(year,month,day)){
    alert(calenderJSAlert1);
    defaultToday = true;
    }
    if (defaultToday){
    //If invalid format default the date to today's date
    day = now.getDate();
    month = now.getMonth();
    year = now.getFullYear();
    }

    this.focusDay = day;

    returnDate(day);//anurag
    document.calControl.month.selectedIndex = month;
    document.calControl.year.value = year;
    displayCalendar(day, month, year);
    }

    function checkDateFormat(inYear,inMonth,inDay){
    var year = inYear+"";
    var yearLen = year.length;
    if (!isNaN(inMonth)){
    inMonth = inMonth+1;
    }
    if ((yearLen != 4) || isNaN(inYear) || isNaN(inMonth) || isNaN(inDay) || (inMonth < 1) || (inMonth > 12) ||(inDay < 1) || (inDay >31)){
    return false;
    }
    var daysIn = days_in (inMonth,inYear)
    if (inDay > daysIn){
    return false;
    }
    return true;
    }


    function days_in(month, year){
    if(month == 4 || month == 6 || month == 9 || month == 11){
    return 30;
    } else if(!isLeapYear(year) && month == 2){
    return 28;
    } else if(isLeapYear(year) && month == 2){
    return 29;
    } else return 31;
    }

    function setToday(){
    // SET DAY MONTH AND YEAR TO TODAY'S DATE
    var now = new Date();
    var day = now.getDate();
    var month = now.getMonth();
    var year = now.getFullYear();

    this.focusDay = day;
    document.calControl.month.selectedIndex = month;
    document.calControl.year.value = year;
    displayCalendar(day, month, year);
    }


    function isFourDigitYear(year) {
    if (year.length != 4) {
    //Sorry, the year must be four-digits in length.
    alert(calenderJSAlert2);
    document.calControl.year.select();
    document.calControl.year.focus();
    }else {
    return true;
    }
    }


    function selectDate(){
    var year = document.calControl.year.value;
    if (isFourDigitYear(year)) {
    var day = 0;
    var month = document.calControl.month.selectedIndex;
    displayCalendar(day, month, year);
    }
    }


    function setPreviousYear()
    {
    var year = document.calControl.year.value;
    if (isFourDigitYear(year)) {
    var day = 0;
    var month = document.calControl.month.selectedIndex;
    year--;
    document.calControl.year.value = year;
    displayCalendar(day, month, year);
    }
    }


    function setPreviousMonth()
    {
    var year = document.calControl.year.value;
    if (isFourDigitYear(year)) {
    var day = 0;
    var month = document.calControl.month.selectedIndex;
    if (month == 0) {
    month = 11;
    if (year > 1000) {
    year--;
    document.calControl.year.value = year;
    }
    }
    else {
    month--;
    }
    document.calControl.month.selectedIndex = month;
    displayCalendar(day, month, year);
    }
    }


    function setNextMonth() {
    var year = document.calControl.year.value;
    if (isFourDigitYear(year)) {
    var day = 0;
    var month = document.calControl.month.selectedIndex;
    if (month == 11) {
    month = 0;
    year++;
    document.calControl.year.value = year;
    }
    else {
    month++;
    }
    document.calControl.month.selectedIndex = month;
    displayCalendar(day, month, year);
    }
    }


    function setNextYear() {
    var year = document.calControl.year.value;
    if (isFourDigitYear(year)) {
    var day = 0;
    var month = document.calControl.month.selectedIndex;
    year++;
    document.calControl.year.value = year;
    displayCalendar(day, month, year);
    }
    }


    function displayCalendar(day, month, year) {

    day = parseInt(day);
    month = parseInt(month);
    year = parseInt(year);
    var i = 0;
    var now = new Date();

    if (day == 0) {
    var nowDay = now.getDate();
    } else {
    var nowDay = day;
    }
    var days = getDaysInMonth(month+1,year);
    var firstOfMonth = new Date (year, month, 1);
    var startingPos = firstOfMonth.getDay();
    days += startingPos;

    // MAKE BEGINNING NON-DATE BUTTONS BLANK
    for (i = 0; i < startingPos; i++) {
    document.calButtons.elements.value = "__";
    }

    // SET VALUES FOR DAYS OF THE MONTH
    for (i = startingPos; i < days; i++) {
    // document.calButtons.elements.value = i-startingPos+1;
    // document.calButtons.elements.onClick = "returnDate"
    var val = i-startingPos+1;
    if (val == "1") document.calButtons.elements.value = "01";
    else if (val == "2") document.calButtons.elements.value = "02";
    else if (val == "3") document.calButtons.elements.value = "03";
    else if (val == "4") document.calButtons.elements.value = "04";
    else if (val == "5") document.calButtons.elements.value = "05";
    else if (val == "6") document.calButtons.elements.value = "06";
    else if (val == "7") document.calButtons.elements.value = "07";
    else if (val == "8") document.calButtons.elements.value = "08";
    else if (val == "9") document.calButtons.elements.value = "09";
    else document.calButtons.elements.value = i-startingPos+1;
    // document.calButtons.elements.onClick = "returnDate"
    }

    // MAKE REMAINING NON-DATE BUTTONS BLANK
    for (i=days; i<42; i++) {
    document.calButtons.elements.value = "__";
    }

    // GIVE FOCUS TO CORRECT DAY
    document.calButtons.elements[focusDay+startingPos-1].focus();
    document.calButtons.elements[focusDay+startingPos-1].style.color = "purple";
    }


    // GET NUMBER OF DAYS IN MONTH
    function getDaysInMonth(month,year){
    var days;
    if (month==1 || month==3 || month==5 || month==7 || month==8 ||
    month==10 || month==12) days=31;
    else if (month==4 || month==6 || month==9 || month==11) days=30;
    else if (month==2) {
    if (isLeapYear(year)) {
    days=29;
    } else {
    days=28;
    }
    }
    return (days);
    }


    // CHECK TO SEE IF YEAR IS A LEAP YEAR
    function isLeapYear (Year) {
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
    return (true);
    } else {
    return (false);
    }
    }


    // SET FORM FIELD VALUE TO THE DATE SELECTED

    function clear(){
    dateField.value ="";
    window.close();
    }


    function returnDate(inDay){
    selectedDay = inDay;
    }

    function modifyDate(){


    if (selectedDay == ""){
    var now = new Date();
    selectedDay = now.getDate();
    }
    var month = (document.calControl.month.selectedIndex)+1;
    var year = document.calControl.year.value;

    if ((""+month).length == 1){
    month="0"+month;
    }
    if ((""+selectedDay).length == 1) {
    selectedDay="0"+ selectedDay;
    }
    if (selectedDay != "CL" && selectedDay != "__") {

    dateField.value = selectedDay + "/" + month + "/" + year;
    //dateField.value = year + "-" + month + "-" + day ;
    window.close();
    }
    if (selectedDay == "CL") {
    dateField.value = "" ;
    window.close()
    }

    }


    function right(e) {
    //var msg = "Sorry, you don't have permission to right-click.";
    if (navigator.appName == 'Netscape' && e.which == 3) {
    //alert(msg);
    return false;
    } else {
    if (navigator.appName == 'Microsoft Internet Explorer' && event.button==2) {
    //alert(msg);
    return false;
    }
    return true;
    }
    }

    function checkForClose(){
    if (opener.document.setDateForm.parentVisible.value == 'false') {
    self.close();
    }
    }

    document.onmousedown = right;

    ---------------------------------------
    calenderpopup.js file is also using .butr i can't put here(file size will become high)

    by

    shinoy
     
    shinoy, Feb 19, 2007 IP