/*********************************************************************************
' Title:        Script for SPS5Search
'
' Copyright:    (C) 2004-2005 Seneca Web Development
'
'               This script is distributed in the hope that it will be useful,
'               but WITHOUT ANY WARRANTY; without even the implied warranty of
'               MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
'
' History:
' 2004-01-10    jvz Version for Smartsite 5
' 2005-01-11    ws  Added extra date checks
*********************************************************************************/

/**********************************************************************
Function SPS5Search (to validate form input)
***********************************************************************/
function SPS5Search(oForm, sDateFormat){

  if(oForm.fldDateFrom != undefined){
    // Field exists        
    if(!CheckDateTime(oForm.fldDateFrom, sDateFormat)){
      return false; 
    }
  }

  if(oForm.fldDateUntil != undefined){
    // Field exists    
    if(!CheckDateTime(oForm.fldDateUntil, sDateFormat)) {
      return false;
    }
  }

 if ((oForm.fldDateFrom !=undefined) && (oForm.fldDateUntil != undefined))  {   
     if (!((oForm.fldDateFrom.value=='') && (oForm.fldDateUntil.value=='')))
     {     if ((oForm.fldDateFrom.value)==(oForm.fldDateUntil.value))
           {    ShowMsgDate(oForm.fldDateFrom);
                  return false;
            }
     }

    if ((oForm.fldDateFrom.value!='') && (oForm.fldDateUntil.value!=''))
    {
       if (DateDiff("d",oForm.fldDateFrom.value, oForm.fldDateUntil.value) <= 0)
       {   ShowMsgDateDifference(oForm.fldDateFrom);
           return false;
       }
     } 
}
   return true;
}

/**********************************************************************
Function CheckDateTime 

Note: 
- date is compulsory and must contain "YYYY", "MM" and "DD" 
- time is optional and (if set) must contain "HH", "NN" and "SS"
***********************************************************************/
function CheckDateTime(oField, sDateFormat){
  var i;
  var cDF,cV;
  var sValue;
  
  sValue = oField.value

  // Check date if set 
  if(sValue != ""){
    if(sDateFormat.length != sValue.length){  
      ShowMsgInvalidDate(oField,sValue,sDateFormat);	
      return false; 
    }

    // Check format char by char
    for (i=0; i < sDateFormat.length; i++){         
      cDF = sDateFormat.charAt(i);            

      switch(cDF){
        case "Y": // Part of year
        case "M": // Part of month
        case "D": // Part of day          
        case "H": // Part of hours          
        case "N": // Part of minutes          
        case "S": // Part of seconds          
          cV = sValue.charAt(i);

          if(cV<"0" || cV>"9"){
            ShowMsgInvalidDate(oField,sValue,sDateFormat);	        
	    return false;  
          }
          break;

        default: // Seperators
          cV = sValue.charAt(i);

          if(cV != cDF){
            ShowMsgInvalidDate(oField,sValue,sDateFormat);	        
            return false;  
          }  
          break;
      }  
    }
  }

  return true;
}

/**********************************************************************
Function ShowMsgInvalidDate
***********************************************************************/
function ShowMsgInvalidDate(oField, sDate, sDateFormat){
  alert("Datum '" + sDate + "' voldoet niet aan formaat '" + sDateFormat +"'!");
  oField.focus();
  oField.select();
  return true;
}

/**********************************************************************
Function ShowMsgDateDifference
***********************************************************************/
function ShowMsgDateDifference(oField){
  alert("'Datum tot' moet na 'Datum vanaf' liggen!");
  oField.focus();
  oField.select();
  return true;
}

/**********************************************************************
Function ShowMsgDate
***********************************************************************/
function ShowMsgDate(oField){
  alert("Beide datums mogen niet gelijk zijn!");
  oField.focus();
  oField.select();
  return true;
}


/**********************************************************************
Function DateDiff
***********************************************************************/
function DateDiff(strInterval,strFirstDate,strSecondDate) {

var cLan="NL";		// Language setting
var cSml=1;		// Small increments or decrements
var cLrg=10;		// Large increments or decrements
var oAct;
var vMon = new Array(12);
var sMon = new Array(12);

vMon[1]=31;
vMon[2]=28;
vMon[3]=31;
vMon[4]=30;
vMon[5]=31;
vMon[6]=30;
vMon[7]=31;
vMon[8]=31;
vMon[9]=30;
vMon[10]=31;
vMon[11]=30;
vMon[12]=31;

sMon[1]="Januari";
sMon[2]="Februari";
sMon[3]="Maart";
sMon[4]="April";
sMon[5]="Mei";
sMon[6]="Juni";
sMon[7]="Juli";
sMon[8]="Augustus";
sMon[9]="Sepember";
sMon[10]="Oktober";
sMon[11]="November";
sMon[12]="December";

	
	//Returns a positive integer if second date is greater then first date
	//Returns a negative integer if second date is smaller then first date
	//Returns zero if second date is the same as first date
	//
	//Attention: Leap years are not yet implemented. Each year has now 365 days.
	//

	var intDayDiff=0;
	var intMonthDiff=0;
	var intYearDiff=0;
	var intDayCount=0;
	var strSign="";

	// Add leading zeros when appropriate
	if (strFirstDate.substring(1,2)=="/") { strFirstDate="0"+strFirstDate; }
	if (strFirstDate.substring(4,5)=="/") { strFirstDate=strFirstDate.substring(0,3)+"0"+strFirstDate.substring(3,10); }
	if (strSecondDate.substring(1,2)=="/") { strSecondDate="0"+strSecondDate; }
	if (strSecondDate.substring(4,5)=="/") { strSecondDate=strSecondDate.substring(0,3)+"0"+strSecondDate.substring(3,10); }
			
	// Define day, month and year variables
	dd1=parseFloat(cDay(strFirstDate,cLan));
	mm1=parseFloat(cMonth(strFirstDate,cLan));
	yy1=parseFloat(cYear(strFirstDate,cLan));
	dd2=parseFloat(cDay(strSecondDate,cLan));
	mm2=parseFloat(cMonth(strSecondDate,cLan));
	yy2=parseFloat(cYear(strSecondDate,cLan));

	// Perform date action based on strInterval.
	switch (strInterval.toLowerCase()) {

		case "d":

			intYearDiff=yy2-yy1;
			intMonthDiff=mm2-mm1;
			intDayDiff=dd2-dd1;

			// Determine the difference in days
			if (intYearDiff == 0) {					//if both dates are in the same year
				if (intMonthDiff == 0) {
						intDayCount=Math.abs(intDayDiff);	//	if first and second date are in same moth the dayDiff is also the dateDiff

				} else if (intMonthDiff > 0) {		//	if second date is in the future
					intDayCount=vMon[mm1]-dd1;		//		calculate days left from first month in time (=FirstDate)
					intDayCount+=dd2;				//		plus days from second month in time (=SecondDate)
					if (intMonthDiff > 1) {
						for (i=mm1+1; i<mm2; i++) {
							intDayCount+=vMon[i];	//		plus days from months between
						}
					}

				} else if (intMonthDiff < 0) {		//	if second date is in the past
					intDayCount=vMon[mm2]-dd2;		//		calculate days left from first month in time (=SecondDate)
					intDayCount+=dd1;				//		plus days from second month in time (=FirstDate)
					if (intMonthDiff < 1) {
						for (i=mm2+1; i<mm1; i++) {
							intDayCount+=vMon[i];	//		plus days from months between
						}
					}
				}

			} else if (intYearDiff > 0) {			//if second date is in the next year
				intDayCount=365*intYearDiff;		//	start with the difference based on the difference in years
				if (intMonthDiff == 0) {
					intDayCount+=intDayDiff			//	if first and second date are in the same month add the dayDiff.

				} else if (intMonthDiff > 0) {		//  if second month is greater then first month
					intDayCount+=(vMon[mm1]-dd1);	//		add days left from first month in time (=FirstDate)
					intDayCount+=dd2;				//		plus days from second month in time (=SecondDate)
						for (i=mm1+1; i<mm2; i++) {
							intDayCount+=vMon[i];	//		plus days from months between
						}

				} else if (intMonthDiff < 0) {		//  if second month is smaller then first month
					intDayCount-=(vMon[mm2]-dd2);	//		substract days left from first month in time (=FirstDate)
					intDayCount-=dd1;				//		minus days from second month in time (=SecondDate)
						for (i=mm2+1; i<mm1; i++) {
							intDayCount-=vMon[i];	//		minus days from months between
						}

				}

			} else if (intYearDiff < 0) {			//if second date is in the previous year
				intDayCount=365*Math.abs(intYearDiff);	//	start with the difference based on the difference in years

				if (intMonthDiff == 0) {
					intDayCount-=intDayDiff			//	if first and second date are in the same month substract the dayDiff.

				} else if (intMonthDiff > 0) {		//  if second month is greater then first month
					intDayCount-=(vMon[mm1]-dd1);	//		add days left from first month in time (=FirstDate)
					intDayCount-=dd2;				//		plus days from second month in time (=SecondDate)
						for (i=mm1+1; i<mm2; i++) {
							intDayCount-=vMon[i];	//		plus days from months between
						}

				} else if (intMonthDiff < 0) {		//  if second month is smaller then first month
					intDayCount+=(vMon[mm2]-dd2);	//		substract days left from first month in time (=FirstDate)
					intDayCount+=dd1;				//		minus days from second month in time (=SecondDate)
						for (i=mm2+1; i<mm1; i++) {
							intDayCount+=vMon[i];	//		minus days from months between
						}
				}

			}


			// Determine if difference negative
			if (intYearDiff < 0) {
				strSign="-";
			} else if (intYearDiff == 0) {
				if (intMonthDiff < 0) {
					strSign="-";
				} else if (intMonthDiff == 0) {
					if (intDayDiff < 0) { strSign="-"; }
				}
			}
	
	}  // end switch

	return strSign+intDayCount;
	
} // end function

/**********************************************************************
Function cMonth
***********************************************************************/
function cMonth(str,lan) { 

// Position of the month part depends on the laguage
  if (lan=="NL") {
     return str.substring(3,5);
  } else {
     return str.substring(0,2); 
     }
  }


/**********************************************************************
Function cDay
***********************************************************************/
function cDay(str,lan) { 
   // Position of the day part depends on the laguage	
  if (lan=="NL") {
       return str.substring(0,2);
  } else {
               return str.substring(3,5); 
            }
  }


/**********************************************************************
Function cYear
***********************************************************************/
function cYear(str,lan) { 
    // Position of the year part is language independant
    return str.substring(6,10);
}



