/* PopUp Calendar v2.1
© PCI, Inc.,2000 • Freeware
webmaster@personal-connections.com
+1 (925) 955 1624
Permission granted  for unlimited use so far
as the copyright notice above remains intact. */

function dateDifference(depDate,returnDate){
	//depDate = "2005/05/10"
	//returnDate = "2006/11/10"
	var enteredDays = document.fmQuote.txtNumberDays.value
		
	//alert(enteredDays)
	//alert(depDate)
	if (depDate!="" && returnDate!=""){
	isDate(depDate)
	isDate(returnDate)
	}
	
	if (depDate!="" && returnDate!=""){
		var depDateArray = depDate.split("/")
		var returnDateArray = returnDate.split("/")
		
		var departureDate=new Date(depDateArray[2], depDateArray[1]-1, depDateArray[0]) //Month is 0-11 in JavaScript
		var returnDate=new Date(returnDateArray[2], returnDateArray[1]-1, returnDateArray[0]) //Month is 0-11 in JavaScript
		
		var one_day=((1000*60)*60)*24	
	
		var now = new Date
		now.getDate() + "/" + now.getMonth() + "/" + now.getYear()
		
		var currentDate = new Date()
				
		var dateDiff = Math.floor((returnDate-departureDate)/864e5);
						
		var dateDiff2 = Math.ceil((returnDate.getTime()-currentDate)/(one_day))
		var dateDiff3 = Math.ceil((departureDate.getTime()-currentDate)/(one_day))
		
		if (dateDiff<0){
			alert("The departure date specified is after the return date. Please amend this so that the departure date is before the return date")
			document.fmQuote.txtReturnDate.value="";
		}
		//else if(dateDiff>enteredDays-1 && enteredDays>0){
//			alert("The dates specified are greater than the number of days entered")
//			document.fmQuote.txtReturnDate.value="";
//		}
//		else if(dateDiff<enteredDays-1 && enteredDays>0){
//			alert("The dates specified are less than the number of days entered")
//			document.fmQuote.txtReturnDate.value="";
//		}
		else if (dateDiff2<0){
			alert("The Return date is in the past")
			document.fmQuote.txtReturnDate.value="";
		}else if (dateDiff3<0){
			alert("The Departure date is in the past")
			document.fmQuote.txtDepartureDate.value="";
		}else if (dateDiff>=45){
			alert("The maximum duration is 45 days")
			document.fmQuote.txtReturnDate.value="";
		}
		else{
			
			if(isNaN(dateDiff)){
					//alert(dateDiff)
			}else{
				document.fmQuote.txtNumberDays.value = dateDiff+1;			
				document.fmQuote.txtNumberDays.readonly = true;
			}
		}
		}
	
}

function dateDifferenceDoDays(depDate, returnDate, dayCount){
	//depDate = "2005/05/10"
	//returnDate = "2006/11/10"
	var enteredDays = document.fmQuote.txtNumberDays.value
	var one_day=((1000*60)*60)*24
	
	if (depDate!="")
	{
		isDate(depDate)
	}
	
	if (depDate!="")
	{
		
		var depDateArray = depDate.split("/")
		var returnDateArray = returnDate.split("/")
		
		var departureDate=new Date(depDateArray[2], depDateArray[1]-1, depDateArray[0]) //Month is 0-11 in JavaScript
		var returnDate=new Date(returnDateArray[2], returnDateArray[1]-1, returnDateArray[0]) //Month is 0-11 in JavaScript
		
		var dateDiff = Math.floor((returnDate-departureDate)/864e5);
		
		var now = new Date
		now.getDate() + "/" + now.getMonth() + "/" + now.getYear()
		
		var currentDate = new Date()				
		var dateDiff3 = Math.ceil((departureDate.getTime()-currentDate)/(one_day))
		
		if (dateDiff3<0)
		{
			alert("The Departure date is in the past")
			document.fmQuote.txtDepartureDate.value="";
		}
		else{
			
			if(isNaN(dateDiff)){
					//alert(dateDiff)
			}else{
				
				//document.fmQuote.txtReturnDate.value = departureDate.setTime(returnDate.getTime());
				if ((dateDiff + 1) != dayCount)
				{
					alert("The trip duration is different from previously selected")
					return false;
				}
				else
				{
					return true;	
				}
				
			}
		}
	}
	
}

function dateBeforeCheck(startDate){
	
	//alert(enteredDays)
	//alert(depDate)
	if (startDate!=""){
	isDate(startDate)
	}
	
	if (startDate!=""){
		var startDateArray = startDate.split("/")
		
		var startDate=new Date(startDateArray[2], startDateArray[1]-1, startDateArray[0]) //Month is 0-11 in JavaScript
		//Set 1 day in milliseconds
		var one_day=1000*60*60*24
	
	
	
		var now = new Date
		now.getDate() + "/" + now.getMonth() + "/" + now.getYear()
		
		//document.write(Math.ceil((returnDate.getTime()-departureDate.getTime())/(one_day))+" difference")
		var currentDate = new Date()
		var dateDiff = Math.ceil((startDate.getTime()-currentDate)/(one_day))
		
		if (dateDiff<0){
			alert("The Start date is in the past")
			document.fmQuote.txtStartDate.value="";
		}else{
			
			if(isNaN(dateDiff)){
					//alert(dateDiff)
			}
		}
		}
	//var endDate = document.getElementById("endDate")
	if (document.fmQuote.EndDate!= null){
		


		function makeArray() {
			for (i = 0; i<makeArray.arguments.length; i++)
				this[i + 1] = makeArray.arguments[i];
		}
		
		var months = new makeArray('01/','02/','03/','04/',
								   '05/','06/','07/','08/','09/',
								   '10/','11/','12/');
		
		function nths(day) {
			if (day == 1 || day == 21 || day == 31) return '/';
			if (day == 2 || day == 22) return '/';
			if (day == 3 || day == 23) return '/';
			return '/';
		}
		
		function y2k(number) { return (number < 1000) ? number + 1900 : number; }
		
		function monthsahead(noofmonths) {
			var today = new Date();
		var startDate=new Date(startDateArray[2], startDateArray[1]-1, startDateArray[0]) //Month is 0-11 in JavaScript


		var date = new Date(startDateArray[2],startDateArray[1]-1+noofmonths,startDateArray[0]-1,today.getHours(),today.getMinutes(),today.getSeconds());
			
			return date.getDate() + nths(date.getDate()) +  months[date.getMonth()+1] + y2k(date.getYear());
		}
		
		//document.write();

		
		// CONTROLS HOW MANY MONTHS THE POLICY LASTS (12 = 1 YEAR)
		document.fmQuote.EndDate.value = monthsahead(12)


	}
	
}


/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	var dateError = ""
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
		dateError = "true"
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
		dateError = "true"
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
		dateError = "true"
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
		dateError = "true"
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
		dateError = "true"
	}
return true
}




/* Settings. Please read readme.html file for instructions*/
var ppcDF = "d/m/Y";
var ppcMN = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var ppcWN = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var ppcER = new Array(4);
ppcER[0] = "Required DHTML functions are not supported in this browser.";
ppcER[1] = "Target form field is not assigned or not accessible.";
ppcER[2] = "Sorry, the chosen date is not acceptable. Please read instructions on the page.";
ppcER[3] = "Unknown error occured while executing this script.";
var ppcUC = false;
 var ppcUX = 4;
 var ppcUY = 4;

/* Do not edit below this line unless you are sure what are you doing! */

var ppcIE=(navigator.appName == "Microsoft Internet Explorer");
var ppcNN=((navigator.appName == "Netscape")&&(document.layers));
var ppcTT="<table width=\"200\" cellspacing=\"1\" cellpadding=\"2\" border=\"1\" bordercolorlight=\"#000000\" bordercolordark=\"#000000\">\n";
var ppcCD=ppcTT;var ppcFT="<font face=\"MS Sans Serif, sans-serif\" size=\"1\" color=\"#000000\">";var ppcFC=true;
var ppcTI=false;var ppcSV=null;var ppcRL=null;var ppcXC=null;var ppcYC=null;
var ppcML=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var ppcWE=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var ppcNow=new Date();var ppcPtr=new Date();
if (ppcNN) {
 window.captureEvents(Event.RESIZE);
 window.onresize = restoreLayers;
 document.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
 document.onmousedown = recordXY;
 document.onmouseup = confirmXY;}
function restoreLayers(e) {
 if (ppcNN) {
  with (window.document) {
   open("text/html");
   write("<html><head><title>Restoring the layer structure...</title></head>");
   write("<body bgcolor=\"#FFFFFF\" onLoad=\"history.go(-1)\">");
   write("</body></html>");
   close();}}}
function recordXY(e) {
 if (ppcNN) {
  ppcXC = e.x;
  ppcYC = e.y;
  document.routeEvent(e);}}
  
  
function confirmXY(e) {
 if (ppcNN) {
  ppcXC = (ppcXC == e.x) ? e.x : null;
  ppcYC = (ppcYC == e.y) ? e.y : null;
  document.routeEvent(e);}}
  
  
function getCalendarFor(target,days) {
 ppcSV = target;
 //ppcRL = rules;
  
 if (ppcFC) {setCalendar();ppcFC = false;}
 if ((ppcSV != null)&&(ppcSV)) {
	 	 
  if (ppcIE) {
   var obj = document.all['PopUpCalendar'];
   obj.style.left = document.body.scrollLeft+event.clientX;
   obj.style.top  = document.body.scrollTop+event.clientY;
   obj.style.visibility = "visible";
   }
  else if (ppcNN) {
   var obj = document.layers['PopUpCalendar'];
   obj.left = ppcXC
   obj.top  = ppcYC
   obj.visibility = "show";}
  else {showError(ppcER[0]);}}
 else {showError(ppcER[1]);}}
 
 
function switchMonth(param) {
 var tmp = param.split("|");
 setCalendar(tmp[0],tmp[1]);}
 
 
function moveMonth(dir) {
 var obj = null;
 var limit = false;
 var tmp,dptrYear,dptrMonth;
 if (ppcIE) {obj = document.ppcMonthList.sItem;}
 else if (ppcNN) {obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;}
 else {showError(ppcER[0]);}
 if (obj != null) {
  if ((dir.toLowerCase() == "back")&&(obj.selectedIndex > 0)) {obj.selectedIndex--;}
  else if ((dir.toLowerCase() == "forward")&&(obj.selectedIndex < 12)) {obj.selectedIndex++;}
  else {limit = true;}}
 if (!limit) {
  tmp = obj.options[obj.selectedIndex].value.split("|");
  dptrYear  = tmp[0];
  dptrMonth = tmp[1];
  setCalendar(dptrYear,dptrMonth);}
 else {
  if (ppcIE) {
   obj.style.backgroundColor = "#FF0000";
   window.setTimeout("document.ppcMonthList.sItem.style.backgroundColor = '#FFFFFF'",50);}}}
function selectDate(param) {
 var arr   = param.split("|");
 var year  = arr[0];
 var month = arr[1];
 var date  = arr[2];
 var ptr = parseInt(date);
 ppcPtr.setDate(ptr);
 if ((ppcSV != null)&&(ppcSV)) {
  if (validDate(date)) {ppcSV.value = dateFormat(year,month,date);hideCalendar();}
  else {showError(ppcER[2]);if (ppcTI) {clearTimeout(ppcTI);ppcTI = false;}}}
 else {
  showError(ppcER[1]);
  hideCalendar();}
 	if (document.fmQuote.txtDepartureDate!= null){
  		dateDifference(document.fmQuote.txtDepartureDate.value,document.fmQuote.txtReturnDate.value)
	}else{
		dateBeforeCheck(document.fmQuote.txtStartDate.value)	
	}
 
  }
function setCalendar(year,month) {
 if (year  == null) {year = getFullYear(ppcNow);}
 if (month == null) {month = ppcNow.getMonth();setSelectList(year,month);}
 if (month == 1) {ppcML[1]  = (isLeap(year)) ? 29 : 28;}
 ppcPtr.setYear(year);
 ppcPtr.setMonth(month);
 ppcPtr.setDate(1);
 updateContent();}
function updateContent() {
 generateContent();
 if (ppcIE) {document.all['monthDays'].innerHTML = ppcCD;}
 else if (ppcNN) {
  with (document.layers['PopUpCalendar'].document.layers['monthDays'].document) {
   open("text/html");
   write("<html>\n<head>\n<title>DynDoc</title>\n</head>\n<body bgcolor=\"#FFFFFF\">\n");
   write(ppcCD);
   write("</body>\n</html>");
   close();}}
 else {showError(ppcER[0]);}
 ppcCD = ppcTT;}

function generateContent() {
 var year  = getFullYear(ppcPtr);
 var month = ppcPtr.getMonth();
 var date  = 1;
 var day   = ppcPtr.getDay();
 var len   = ppcML[month];
 var bgr,cnt,tmp = "";
 var j,i = 0;
 for (j = 0; j < 7; ++j) {
  if (date > len) {break;}
  for (i = 0; i < 7; ++i) {
   bgr = ((i == 0)||(i == 6)) ? "#FFFFCC" : "#FFFFFF";
   if (((j == 0)&&(i < day))||(date > len)) {tmp  += makeCell(bgr,year,month,0);}
   else {tmp  += makeCell(bgr,year,month,date);++date;}}
  ppcCD += "<tr align=\"center\">\n" + tmp + "</tr>\n";tmp = "";}
 ppcCD += "</table>\n";}
 
 
 
function makeCell(bgr,year,month,date) {
 var param = "\'"+year+"|"+month+"|"+date+"\'";
 var td1 = "<td width=\"20\" bgcolor=\""+bgr+"\" ";
 var td2 = (ppcIE) ? "</font></span></td>\n" : "</font></a></td>\n";
 var evt = "onMouseOver=\"this.style.backgroundColor=\'#FF0000\'\" onMouseOut=\"this.style.backgroundColor=\'"+bgr+"\'\" onMouseUp=\"selectDate("+param+")\" ";
 var ext = "<span Style=\"cursor: hand\">";
 var lck = "<span Style=\"cursor: default\">";
 var lnk = "<a href=\"javascript:selectDate("+param+")\" onMouseOver=\"window.status=\' \';return true;\">";
 var cellValue = (date != 0) ? date+"" : "&nbsp;";
 if ((ppcNow.getDate() == date)&&(ppcNow.getMonth() == month)&&(getFullYear(ppcNow) == year)) {
  cellValue = "<b>"+cellValue+"</b>";}
 var cellCode = "";
 if (date == 0) {
  if (ppcIE) {cellCode = td1+"Style=\"cursor: default\">"+lck+ppcFT+cellValue+td2;}
  else {cellCode = td1+">"+ppcFT+cellValue+td2;}}
 else {
  if (ppcIE) {cellCode = td1+evt+"Style=\"cursor: hand\">"+ext+ppcFT+cellValue+td2;}
  else {
   if (date < 10) {cellValue = "&nbsp;" + cellValue + "&nbsp;";}
   cellCode = td1+">"+lnk+ppcFT+cellValue+td2;}}
 return cellCode;}
function setSelectList(year,month) {
 var i = 0;
 var obj = null;
 if (ppcIE) {obj = document.ppcMonthList.sItem;}
 else if (ppcNN) {obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;}
 else {/* NOP */}
 while (i < 13) {
  obj.options[i].value = year + "|" + month;
  obj.options[i].text  = year + " • " + ppcMN[month];
  i++;
  month++;
  if (month == 12) {year++;month = 0;}}}

function hideCalendar() {
 if (ppcIE) {document.all['PopUpCalendar'].style.visibility = "hidden";}
 else if (ppcNN) {document.layers['PopUpCalendar'].visibility = "hide";window.status = " ";}
 else {/* NOP */}
 ppcTI = false;
 setCalendar();
 ppcSV = null;
 if (ppcIE) {var obj = document.ppcMonthList.sItem;}
 else if (ppcNN) {var obj = document.layers['PopUpCalendar'].document.layers['monthSelector'].document.ppcMonthList.sItem;}
 else {/* NOP */}
 obj.selectedIndex = 0;
 
 //setTotalDays('txtDepartureDate','txtReturnDate','txtNumberDays','dates')
 }
 
 
function showError(message) {
 window.alert("[ PopUp Calendar ]\n\n" + message);}
function isLeap(year) {
 if ((year%400==0)||((year%4==0)&&(year%100!=0))) {return true;}
 else {return false;}}
function getFullYear(obj) {
 if (ppcNN) {return obj.getYear() + 1900;}
 else {return obj.getYear();}}

function validDate(date) {
 var reply = true;
 if (ppcRL == null) {/* NOP */}
 else {
  var arr = ppcRL.split(":");
  var mode = arr[0];
  var arg  = arr[1];
  var key  = arr[2].charAt(0).toLowerCase();
  if (key != "d") {
   var day = ppcPtr.getDay();
   var orn = isEvenOrOdd(date);
   reply = (mode == "[^]") ? !((day == arg)&&((orn == key)||(key == "a"))) : ((day == arg)&&((orn == key)||(key == "a")));}
  else {reply = (mode == "[^]") ? (date != arg) : (date == arg);}}
 return reply;}
function isEvenOrOdd(date) {
 if (date - 21 > 0) {return "e";}
 else if (date - 14 > 0) {return "o";}
 else if (date - 7 > 0) {return "e";}
 else {return "o";}}
function dateFormat(year,month,date) {
 if (ppcDF == null) {ppcDF = "m/d/Y";}
 var day = ppcPtr.getDay();
 var crt = "";
 var str = "";
 var chars = ppcDF.length;
 for (var i = 0; i < chars; ++i) {
  crt = ppcDF.charAt(i);
  switch (crt) {
   case "M": str += ppcMN[month]; break;
   case "m": str += (month<9) ? ("0"+(++month)) : ++month; break;
   case "Y": str += year; break;
   case "y": str += year.substring(2); break;
   case "d": str += ((ppcDF.indexOf("m")!=-1)&&(date<10)) ? ("0"+date) : date; break;
   case "W": str += ppcWN[day]; break;
    default: str += crt;}}
 return unescape(str);
 
 
 }