﻿/*valid.js
 * by Eurotours V1.0
*/

appVer = navigator.appVersion;

function compareDates ( theElementV, theElementB, theElementNameV, theElementNameB ) {
  strv = theElementV.value;
  strvday = strv.substring(0,2); 
  strvmonth = strv.substring(3,5); 
  strvyear = strv.substring(6,10); 
  strb = theElementB.value;
  strbday = strb.substring(0,2); 
  strbmonth = strb.substring(3,5); 
  strbyear = strb.substring(6,10);
  if ( strbyear<strvyear || ((strvyear==strbyear) && (strbmonth<strvmonth)) || ((strvyear==strbyear) && (strvmonth==strbmonth) && (strbday<strvday)) ) { 
    if ( compareDates.arguments.length != 4 )
      alert(sDateBLowerV);
    else
      alert( theElementNameB + sLowerV + theElementNameV + "!" );
    theElementB.value=""; 
    theElementB.focus(); 
    return false; 
  }
  else return true;
} 
//End of compareDates

function compareWithToday ( theElement, theElementName, compDate ) {
  str = theElement.value;
  strday = str.substring(0,2); 
  strmonth = str.substring(3,5); 
  stryear = str.substring(6,10);

  day=compDate.substr(0,2);
  month = compDate.substring(3,5); 
  year = compDate.substring(6,10);

  if ( stryear<year || ((stryear==year) && (strmonth<month)) || ((stryear==year) && (strmonth==month) && (strday<day)) ) { 
    if ( compareWithToday.arguments.length  < 3 )
      alert(sDateLowerToday);
    else
      alert( theElementName + sLowerToday);
    theElement.value=""; 
    theElement.focus(); 
    return false; 
  }
  else return true;
} 
//End of compareWithToday

function compareWith720DaysInAdvance ( theElement, theElementName, compDate ) {
  str = theElement.value;
  strday = str.substring(0,2); 
  strmonth = str.substring(3,5); 
  stryear = str.substring(6,10);

  day =compDate.substr(0,2);
  month = compDate.substring(3,5); 
  year = compDate.substring(6,10);

  x = new Date(year, month - 1, day);
  //2 * 360 * 24 * 60 * 60 * 1000 = 720 Tage in Millisekunden
  y = new Date(x.getTime()+62388000000);
  x.setTime(y);
  month2 = x.getMonth() + 1;
  year2 = x.getFullYear();
  day2 = x.getDate();

  if ( stryear>year2 || ((stryear==year2) && (strmonth>month2)) || ((stryear==year2) && (strmonth==month2) && (strday>day2)) ) { 
    if ( compareWith720DaysInAdvance.arguments.length  < 3 )
      alert(sDateToFar);
    else
      alert( theElementName + sToFar);
    theElement.value=""; 
    theElement.focus(); 
    return false; 
  }
  else return true;
} 
//End of compareWith720DaysInAdvance 

function CCDate ( theElement, theElementName, compare, compDate ) {
  if (!completedate ( theElement, theElementName,compDate ))
    return false;
  if (!isDate ( theElement, theElementName ))
    return false;
  if (compare>=1 && !compareWithToday ( theElement, theElementName, compDate ))
    return false;
  if (compare==2 && !compareWith720DaysInAdvance ( theElement, theElementName, compDate ))
    return false;
  return true;
} 
//End of CCDate

function completedate (theElement, theElementName, compDate){
   
    var dCurYear = compDate.substring(6,10);
    var indate = (theElement.value) ? theElement.value : $("#"+theElement.selector).val();

    var s = indate;
    var filter=/^[a-zA-Z0-9]{1,}$/;
    if (s.length == 0 ) {
      if ( completedate.arguments.length  == 1 )
        alert(sEnterValDate);
      else
        alert( theElementName + sValidDate);
      theElement.focus(); 
      return false; 
    }
    if (filter.test(s))
        {
        if (indate.length == 4)
        {
        var thisday = indate.substr(0,2);
        var thismonth = indate.substr(2,2);
        var newdate = thisday + "." + thismonth + "." + dCurYear;
        theElement.value = newdate;
        return true;
        }
        if (indate.length == 6)
        {
        var thisday = indate.substr(0,2);
        var thismonth = indate.substr(2,2);
        var thisyear = indate.substr(4,2);
        var newdate = thisday + "." + thismonth + ".20" + thisyear;
        theElement.value = newdate;
        return true;
        }
        if (indate.length == 8)
        {
        var thisday = indate.substr(0,2);
        var thismonth = indate.substr(2,2);
        var thisyear = indate.substr(4,4);
        var newdate = thisday + "." + thismonth + "." + thisyear
        theElement.value = newdate;
        return true;
        }
        else
        {
        alert(sEnterValDate);
        theElement.focus();
        return false;
        }
        }
    if (!filter.test(s))
        {
        if (indate.length == 5)
        {
        var thisday = indate.substr(0,2);
        var thismonth = indate.substr(3,2);
        var newdate = thisday + "." + thismonth + "." + dCurYear;
        theElement.value = newdate;
        return true;
        }
        if (indate.length == 8)
        {
        var thisday = indate.substr(0,2);
        var thismonth = indate.substr(3,2);
        var thisyear = indate.substr(6,2);
        var newdate = thisday + "." + thismonth + ".20" + thisyear;
        theElement.value = newdate;
        return true;
        }
        if (indate.length == 10)
        {
        return true;
        }
        else
        {
        alert(sEnterValDate);
        theElement.focus();
        return false;
        }
        }

    else
        alert(sEnterValDate);
    theElement.focus();
    return false;
    }
//End of completedate

function isDate ( theElement, theElementName ) {
  str = theElement.value;
  len = str.length; 
  if (len != 10) { 
    if ( isDate.arguments.length  == 1 )
      alert(sEnterValDate);
    else
      alert( theElementName + sValidDate);
    theElement.value=""; 
    theElement.focus(); 
    return false; 
  } 
  if ((str.substring(1,2))=="."){
    alert(sEnterValDate);
    return false;}
  if ((str.substring(2,3))!="."){
    alert(sEnterValDate);
    return false;}
  if ((str.substring(5,6))!="."){
    alert(sEnterValDate);
    return false;}
  strday = str.substring(0,2); 
  strmonth = str.substring(3,5); 
  stryear = str.substring(6,10); 
  if ( isNaN(strday)||(strday < "01")||(strday > "31")||isNaN(strmonth)||(strmonth < 0)||isNaN(stryear)||(stryear < 0) ) { 
    if ( isDate.arguments.length  == 1 )
      alert(sEnterValDate);
    else
      alert( theElementName+sValidDate);
    theElement.value=""; 
    theElement.focus(); 
    return false; 
  }
  if ( (strmonth == "01") || (strmonth == "03") || (strmonth == "05") || 
       (strmonth == "07") || (strmonth == "08") || (strmonth == "10") || 
       (strmonth == "12") ) 
    monthdays = 31
  else if ( (strmonth == "04") || (strmonth == "06") || (strmonth == "09") || (strmonth == "11") ) 
    monthdays = 30
  else if (strmonth == "02") 
    monthdays = ((stryear % 4) == 0) ? 29 : 28; 
  else { 
    if ( isDate.arguments.length  == 1 )
      alert(sEnterValDate);
    else
      alert( theElementName+sValidDate);
    theElement.value=""; 
    theElement.focus(); 
    return false; 
  } 
  if (strday > monthdays) { 
    if ( isDate.arguments.length  == 1 )
      alert(sEnterValDate);
    else
      alert( theElementName+sValidDate);
    theElement.value=""; 
    theElement.focus(); 
    return false; 
  } 
  return true;
} 
//End of isDate

function isNull ( field, fieldName ) {
  selected = 0;
  fieldIsNull = 0;
  if ( field.type == "text" ||
       field.type == "password" ||
       field.type == "textarea" ) {
    if ( field.value == "" )
      fieldIsNull = 1;
  } else if ( field.type == "select-one" ) {
      if ( field.options[field.selectedIndex].value == "%null%")
        fieldIsNull = 1;
  } else if ( field.type == "select-multiple" ) {
      fieldIsNull = 1;
      for ( i = 0; i < field.length; i++ )
        if ( field.options[i].selected )
          fieldIsNull = 0;
  } else if ( field.type == "undefined" ||
              field.type == "checkbox"  ||
              field.type == "radio" ) {
      fieldIsNull = 1;
      for ( i = 0; i < field.length; i++ )
        if ( field[i].checked )
          fieldIsNull = 0;
  }
  if ( fieldIsNull ) {
      if ( isNull.arguments.length  == 1 )
         alert(sNotNull);
      else
         alert( fieldName + ": "+sNotNull);
      if ( field.type == "text" ||
           field.type == "textarea"  ||
           field.type == "password"  ||
           field.type == "select-one"  ||
           field.type == "select-multiple" )
        field.focus();
     return false;
  }
  return true;
}
//End of isNull

function isNumber ( theElement, theElementName ) {
  s = theElement.value;
  if (isNaN(Math.abs(theElement.value)) && (s.charAt(0) != '#')) {
    if ( isNumber.arguments.length  <= 1 ) {
      alert(sMustBeNumb);
      theElement.value="";
      theElement.focus(); }
    else {
      alert( theElementName +  ": "+sMustBeNumb);
      theElement.value="";
      theElement.focus(); }
    return false;
  } 
  return true; 
}
//End of isNumber

function urlencode ( in_str ) {
  var hex = "0123456789ABCDEF";
  var num,temp,out="";
  for(i=0;i<in_str.length;i++) {
    num	= in_str.charCodeAt(i);
    out = out + "%";
    temp = (Math.floor(num/16))%16;
    out = out + hex.substr(temp,1);
    temp = num%16;
    out = out + hex.substr(temp,1);
  }
  return out;
}
//End of urlencode

/*
 * Returns the string representation of a Date object
 * @TODO move this helper function to util.js
 * @param Date date object from which the date as string is needed
 * @return String in format dd.mm.yyyy
 */
function getDateAsString(date)
{
  var day,month,year;
  day = (date.getDate() < 10) ? "0"+date.getDate() : date.getDate();
  month = ((date.getMonth()+1) < 10) ? "0"+(date.getMonth()+1) : (date.getMonth()+1);
  year = date.getFullYear();
  return day + "." + month + "." + year;
}

function modifyDates(jDate1,jDate2)
{
  var date1 = $.datepicker.parseDate('dd.mm.yy', jDate1.val());
  var date2 = $.datepicker.parseDate('dd.mm.yy', jDate2.val());
  if(date1 > date2)
  {
    var sDate2 = getDateAsString(date1);
    jDate2.val(sDate2);
  }
}
