﻿//JavaScripts in DCC website

function GetValue(e) {
    return document.getElementById(e).value;
}
/////////////////////////////////////////////////////////////////////////////////////////////
function GetXmlHttpObject() {
    var XmlHttpNew = null;
    try {
        XmlHttpNew = new XMLHttpRequest();
    }
    catch (e) {
        try {
            XmlHttpNew = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            XmlHttpNew = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return XmlHttpNew;
}
///////////////////////////////////////////////////////////////////////////////////////////
function AddUser()
 {
    var XmlHttp;
    XmlHttp = GetXmlHttpObject();
    if (XmlHttp == null) {
        alert("浏览器不支持 AJAX!");
        return;
    }
 ////////////////////////////////////////////////////////////////////////////////////////
    var PostPara = "";
    PostPara = PostPara + "&Name=" + GetValue("Name");
    PostPara = PostPara + "&Title=" + GetValue("Title");
    PostPara = PostPara + "&Email=" + GetValue("Email");
    PostPara = PostPara + "&LectureTitle=" + GetValue("LectureTitle");
    PostPara = PostPara + "&DateArrival=" + GetValue("DateArrival");
    PostPara = PostPara + "&DateLeave=" + GetValue("DateLeave");
    PostPara = PostPara + "&PreConferenceTour=" + GetValue("PreConferenceTour");
    PostPara = PostPara + "&PostConferenceTour=" + GetValue("PostConferenceTour");
    PostPara = PostPara + "&Number=" + GetValue("Number");
    PostPara = PostPara + "&Institution" + GetValue("Institution");
    PostPara = PostPara + "&Country=" + GetValue("Country");
    PostPara = PostPara + "&Telephone=" + GetValue("Telephone");
    PostPara = PostPara + "&Address=" + GetValue("Address");
    PostPara = PostPara + "&Remarks=" + GetValue("Remark");
    PostPara = PostPara + "&Abstract=" + GetValue("Abstract");
 ///////////////////////////////////////////////////////////////////////////////////////
    var Url = "mail.aspx";
    Url = Url + "?SID=" + Math.random();
    XmlHttp.open("POST", Url, true);
    XmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    XmlHttp.send(PostPara);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

      /**************************************************************************************
      htmlDatePicker v0.1
  
  Copyright (c) 2005, Jason Powell
      All Rights Reserved

  Redistribution and use in source and binary forms, with or without modification, are 
      permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of 
      conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above copyright notice, this list 
      of conditions and the following disclaimer in the documentation and/or other materials 
      provided with the distribution.
      * Neither the name of the product nor the names of its contributors may be used to 
      endorse or promote products derived from this software without specific prior 
      written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
      OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
      MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
      THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
      EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
      GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
      AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
      NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
      OF THE POSSIBILITY OF SUCH DAMAGE.
  
  -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

  
***************************************************************************************/
      // User Changeable Vars
      var HighlightToday = true;    // use true or false to have the current day highlighted
      var DisablePast = true;    // use true or false to allow past dates to be selectable
      // The month names in your native language can be substituted below
      var MonthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

      // Global Vars
      var now = new Date();
      var dest = null;
      var ny = now.getFullYear(); // Today's Date
      var nm = now.getMonth();
      var nd = now.getDate();
      var sy = 0; // currently Selected date
      var sm = 0;
      var sd = 0;
      var y = now.getFullYear(); // Working Date
      var m = now.getMonth();
      var d = now.getDate();
      var l = 0;
      var t = 0;
      var MonthLengths = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

      /*
      Function: GetDate(control)

  Arguments:
      control = ID of destination control
      */
      function GetDate() {
          EnsureCalendarExists();
          DestroyCalendar();
          // One arguments is required, the rest are optional
          // First arguments must be the ID of the destination control
          if (arguments[0] == null || arguments[0] == "") {
              // arguments not defined, so display error and quit
              alert("ERROR: Destination control required in funciton call GetDate()");
              return;
          } else {
              // copy argument
              dest = arguments[0];
          }
          y = now.getFullYear();
          m = now.getMonth();
          d = now.getDate();
          sm = 0;
          sd = 0;
          sy = 0;
          var cdval = dest.value;
          if (/\d{1,2}.\d{1,2}.\d{4}/.test(dest.value)) {
              // element contains a date, so set the shown date
              var vParts = cdval.split("/"); // assume mm/dd/yyyy
              sm = vParts[0] - 1;
              sd = vParts[1];
              sy = vParts[2];
              m = sm;
              d = sd;
              y = sy;
          }

          //  l = dest.offsetLeft; // + dest.offsetWidth;
          //  t = dest.offsetTop - 125;   // Calendar is displayed 125 pixels above the destination element
          //  if(t<0) { t=0; }      // or (somewhat) over top of it. ;)

          /* Calendar is displayed 125 pixels above the destination element
          or (somewhat) over top of it. ;)*/
          l = dest.offsetLeft + dest.offsetParent.offsetLeft+170;
          t = dest.offsetTop+277 ;
          if (t < 0) t = 0; // >
          DrawCalendar();
      }

      /*
      function DestoryCalendar()
  
  Purpose: Destory any already drawn calendar so a new one can be drawn
      */
      function DestroyCalendar() {
          var cal = document.getElementById("dpCalendar");
          if (cal != null) {
              cal.innerHTML = null;
              cal.style.display = "none";
          }
          return
      }

      function DrawCalendar() {
          DestroyCalendar();
          cal = document.getElementById("dpCalendar");
          cal.style.left = l + "px";
          cal.style.top = t + "px";

          var sCal = "<table><tr><td class=\"cellButton\"><a href=\"javascript: PrevMonth();\" title=\"Previous Month\">&lt;&lt;</a></td>" +
    "<td class=\"cellMonth\" width=\"80%\" colspan=\"5\">" + MonthNames[m] + " " + y + "</td>" +
    "<td class=\"cellButton\"><a href=\"javascript: NextMonth();\" title=\"Next Month\">&gt;&gt;</a></td></tr>" +
    "<tr><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>";
          var wDay = 1;
          var wDate = new Date(y, m, wDay);
          if (isLeapYear(wDate)) {
              MonthLengths[1] = 29;
          } else {
              MonthLengths[1] = 28;
          }
          var dayclass = "";
          var isToday = false;
          for (var r = 1; r < 7; r++) {
              sCal = sCal + "<tr>";
              for (var c = 0; c < 7; c++) {
                  var wDate = new Date(y, m, wDay);
                  if (wDate.getDay() == c && wDay <= MonthLengths[m]) {
                      if (wDate.getDate() == sd && wDate.getMonth() == sm && wDate.getFullYear() == sy) {
                          dayclass = "cellSelected";
                          isToday = true;  // only matters if the selected day IS today, otherwise ignored.
                      } else if (wDate.getDate() == nd && wDate.getMonth() == nm && wDate.getFullYear() == ny && HighlightToday) {
                          dayclass = "cellToday";
                          isToday = true;
                      } else {
                          dayclass = "cellDay";
                          isToday = false;
                      }
                      if (((now > wDate) && !DisablePast) || (now <= wDate) || isToday) { // >
                          // user wants past dates selectable
                          sCal = sCal + "<td class=\"" + dayclass + "\"><a href=\"javascript: ReturnDay(" + wDay + ");\">" + wDay + "</a></td>";
                      } else {
                          // user wants past dates to be read only
                          sCal = sCal + "<td class=\"" + dayclass + "\">" + wDay + "</td>";
                      }
                      wDay++;
                  } else {
                      sCal = sCal + "<td class=\"unused\"></td>";
                  }
              }
              sCal = sCal + "</tr>";
          }
          sCal = sCal + "<tr><td colspan=\"4\" class=\"unused\"></td><td colspan=\"3\" class=\"cellCancel\"><a href=\"javascript: DestroyCalendar();\">Cancel</a></td></tr></table>"
          cal.innerHTML = sCal; // works in FireFox, opera
          cal.style.display = "inline";
      }

      function PrevMonth() {
          m--;
          if (m == -1) {
              m = 11;
              y--;
          }
          DrawCalendar();
      }

      function NextMonth() {
          m++;
          if (m == 12) {
              m = 0;
              y++;
          }
          DrawCalendar();
      }

      function ReturnDay(day) {
          cDest = document.getElementById(dest);
          dest.value = (m + 1) + "/" + day + "/" + y;
          DestroyCalendar();
      }

      function EnsureCalendarExists() {
          if (document.getElementById("dpCalendar") == null) {
              var eCalendar = document.createElement("div");
              eCalendar.setAttribute("id", "dpCalendar");
              document.body.appendChild(eCalendar);
          }
      }

      function isLeapYear(dTest) {
          var y = dTest.getYear();
          var bReturn = false;

          if (y % 4 == 0) {
              if (y % 100 != 0) {
                  bReturn = true;
              } else {
                  if (y % 400 == 0) {
                      bReturn = true;
                  }
              }
          }
          return bReturn;
      }  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      String.prototype.trim = function () {return this.replace(/^\s+|\s+$/g, "");}
      function isValidEmail(str) {
          var reEmail = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/);
          //              /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/
          //              /^(?:\w+\.?)*\w+@(?:\w+\.?)*w+$/
          //           return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
          //              alert(reEmail.test("z@s.com"));
          return reEmail.test(str);
      }

function Check() {
    if (GetValue("Name").toString().trim() == "") { alert("Name can not be left blank!");return false;}
    if (GetValue("Email").trim() == "") { alert("Email can not be left blank!"); return false; }
    if (!isValidEmail(GetValue("Email").trim())) {alert("Your Email is incorrect!");return false;} 
    if (GetValue("LectureTitle").trim() == "") { alert("Lecture/Abstract Title can not be left blank!"); return false; }
    if (GetValue("DateArrival").trim() == "") { alert("Estimate date of arrival can not be left blank!"); return false; }
    if (GetValue("DateLeave").trim() == "") { alert("Estimated date of leave can not be left blank!"); return false; }
    if(!CheckingDate(GetValue("DateArrival"),GetValue("DateLeave"))){alert("You can not leave before arrive!"); return false;}
    if (GetValue("Number").trim() == "") { alert("Number of accomanying person can not be left blank!"); return false; }
    return true;
}

function CheckingDate(arrival, leave) {
    var monthsepA = arrival.indexOf("/");
    var yearsepA = arrival.lastIndexOf("/");
    var monthsepL = leave.indexOf("/");
    var yearsepL = leave.lastIndexOf("/");
    ///////////////////////////////////////////////////////
    var monthA = arrival.substring(0,monthsepA);
    var dateA = arrival.slice(monthsepA+1, yearsepA);
    var yearA = arrival.slice(yearsepA + 1);
    /////////////////////////////////////////////////////// 
    var monthL = leave.substring(0,monthsepL);
    var dateL = leave.slice(monthsepL+1, yearsepL);
    var yearL = leave.slice(yearsepL + 1);
    ///////////////////////////////////////////////////////
    if (yearA <= yearL) {
        if (monthA <= monthL) {
            if (dateA <= dateL) {
                return true;
            }
            else { return false; }
        }
        else { return false; }
    }
    else {return false;}
 }




  
