
function prepareOptionInputDate() {
	var formContent = document.forms['optionInput'];
	
	var JsonObject = new Object;
	JsonObject.optionType = new Array;
	JsonObject.amount = new Array;
	JsonObject.shortLong = new Array;
	JsonObject.strike = new Array;
	JsonObject.price = new Array;
	JsonObject.expDate = new Array;
	JsonObject.expYear = new Array;
	
	var rowCounter = 0;
	var optionCounter = 0;
	while (document.getElementById("inputRow"+rowCounter).style.display != "none") {
		if (formContent['checkBox'+rowCounter].checked) {
			// box checked --> store row contents
			JsonObject.optionType[optionCounter] = formContent['optionType'+rowCounter].value;
			JsonObject.amount[optionCounter] = formContent['amount'+rowCounter].value;
			JsonObject.shortLong[optionCounter] = formContent['shortLong'+rowCounter].value;
			JsonObject.strike[optionCounter] = formContent['strike'+rowCounter].value;
			JsonObject.price[optionCounter] = formContent['price'+rowCounter].value;
			JsonObject.expDate[optionCounter] = formContent['expiration'+rowCounter].value;
			JsonObject.expYear[optionCounter] = formContent['expYear'+rowCounter].value;
			rowCounter++;
			optionCounter++;
			}
		else {
			// only increase rowCounter
			rowCounter++;
			}
		}
	
	JsonObject.targetDate = formContent['targetDate'].value;
	
	JsonObject.currentUnderlyingValue = formContent['currentUnderlyingValueInput'].value;
	JsonObject.targetUnderlyingValue = formContent['targetUnderlyingValueInput'].value;
	
	jsonString = JSON.stringify(JsonObject);
	return jsonString;
	}

function checkDateDefined() {
	// check if a target date has been defined
	if($('targetDate').getValue() == "--/--/--") {alert("A target date must be defined!"); return false;}
	else {return true;}
}



///////////////////////////////////////////////////
// Function to find earliest expiration based on multiple
// options input through the form
///////////////////////////////////////////////////
function checkEarliestExpiration() {
	// get first expiration date (first row always present)
	var rowNumber = 0;
	var earliestFoundExpirationDate = calculateFridayDate($('expiration'+rowNumber).getValue(), $('expYear'+rowNumber).getValue());
	
	// now loop rows
	var test = document.getElementById("inputRow"+rowNumber).style.display;
	while (test != "none") {
		var expirationDate = calculateFridayDate($('expiration'+rowNumber).getValue(), $('expYear'+rowNumber).getValue());
		// check if earliest
		if (Date.parse(expirationDate) < Date.parse(earliestFoundExpirationDate)) {
			// apparently earlier
			earliestFoundExpirationDate = expirationDate;
			}
		rowNumber++;
		var test = document.getElementById("inputRow"+rowNumber).style.display;
		}
	
	// Check if target date is before earliest expiration
	if (Date.parse($('targetDate').getValue()) > Date.parse(earliestFoundExpirationDate)) {
		$('targetDate').value = "--/--/--"; 
		alert('The selected Target date is later than the earliest expiration. Target date reset ...');
		}
	return earliestFoundExpirationDate;
	}
		
function checkExpirationDate(optionNumber){
 	var expirationValue = $('expiration'+optionNumber).getValue();
	var expYearValue = $('expYear'+optionNumber).getValue();
	var dateTarget = Date.parse(calculateFridayDate(expirationValue ,expYearValue ));
	now = new Date();
	var dateNow = Date.parse(now);
	if (dateTarget < dateNow) {alert("Selected expiration date is in the past!"); $('expiration'+optionNumber).selectedIndex = 0;}
	
	
}

function calculateFridayDate(expiration, expYear) {
	// Calculate for upcoming Friday
	if (expiration.search(/Friday/)!=-1 && expiration.search(/week/)!=-1) {
		///////////////////////////////////////////////////
		// Calculation is for next week friday
		///////////////////////////////////////////////////
		// Create date object
		expDate = new Date()
		// Month and year is this month and year
		monthNumber=expDate.getMonth()
		yearNumber=expDate.getFullYear()
		// Set for Friday
		targetDay=5 // Which is a Friday
		// Determine earliest possible date next week Friday could happen
		// The earliest this would be is when the current day is Friday, then the date would we just 7 days later
		
		// Set currentDay and currentDate here. Note that the Java Date functions are based on the USR clock!!! If the computer clock is
		// screwed, than the date is also not in order... 
		//currentDay = 6 // For testing
		now = new Date()
		currentDay = now.getDay()
		//currentDate = now.getDate()-15 // For testing
		currentDate = now.getDate()
				
		if(currentDay<=targetDay) {
			// numberOfDaysUntillNextFriday is:
			// the time from now (now.getDay()) untill targetDay, which is ((targetDay-now.getDay()) plus
			// one whole week, which is 7 days
			numberOfDaysUntillNextFriday = 7+(targetDay-currentDay)
			dateNextWeekFriday = new Date(yearNumber,monthNumber,currentDate + numberOfDaysUntillNextFriday);
			}
		else {
			// numberOfDaysUntillNextFriday is:
			// the time from now (now.getDay()) untill Sunday( 0), which is (7-now.getDay()) plus
			// the time from Sunday (0) till the targetDay, which is targetDay, plus
			// one whole week, which is 7 days
			numberOfDaysUntillNextFriday = 7+targetDay+(7-currentDay)
			dateNextWeekFriday = new Date(yearNumber,monthNumber,currentDate + numberOfDaysUntillNextFriday);
			}
		
	
		return dateNextWeekFriday;
		}
	else if (expiration.search(/Friday/)!=-1) {
		///////////////////////////////////////////////////
		// Calculation is for this friday
		///////////////////////////////////////////////////
		// Create date object
		expDate = new Date()
		// Month and year is this month and year
		monthNumber=expDate.getMonth()
		yearNumber=expDate.getFullYear()
		// Set for Friday
		targetDay=5 // Which is a Friday
		// Determine earliest possible date next week Friday could happen
		// The earliest this would be is when the current day is Friday, then the date would be now
		
		// Set currentDay and currentDate here. Note that the Java Date functions are based on the USR clock!!! If the computer clock is
		// screwed, than the date is also not in order... 
		//currentDay = 6 // For testing
		now = new Date()
		currentDay = now.getDay()
		//currentDate = now.getDate()-15 // For testing
		currentDate = now.getDate()
				
		if(currentDay<=targetDay) {
			// numberOfDaysUntillNextFriday is:
			// the time from now (now.getDay()) untill targetDay, which is ((targetDay-now.getDay()) 
			numberOfDaysUntillNextFriday = (targetDay-currentDay)
			dateThisWeekFriday = new Date(yearNumber,monthNumber,currentDate + numberOfDaysUntillNextFriday);
			}
		else {
			// numberOfDaysUntillNextFriday is:
			// the time from now (now.getDay()) untill Sunday( 0), which is (7-now.getDay()) plus
			// the time from Sunday (0) till the targetDay, which is targetDay
			numberOfDaysUntillNextFriday = targetDay+(7-currentDay)
			dateThisWeekFriday = new Date(yearNumber,monthNumber,currentDate + numberOfDaysUntillNextFriday);
			}
		
	
		return dateThisWeekFriday;
		}
	else {
		///////////////////////////////////////////////////
		// Calculation is for third Friday in a particular month
		///////////////////////////////////////////////////
		expDate = new Date()
		// Identify correct month
		switch(expiration) {
			 case "January":
			 monthNumber=0
			 break
			 case "February":
			 monthNumber=1
			 break
			 case "March":
			 monthNumber=2
			 break
			 case "April":
			 monthNumber=3
			 break
			 case "May":
			 monthNumber=4
			 break
			 case "June":
			 monthNumber=5
			 break
			 case "July":
			 monthNumber=6
			 break
			 case "August":
			 monthNumber=7
			 break
			 case "September":
			 monthNumber=8
			 break
			 case "October":
			 monthNumber=9
			 break
			 case "November":
			 monthNumber=10
			 break
			 case "December":
			 monthNumber=11
			 break
			 }
		// From the input:
		yearNumber=expYear
		// Set for Friday
		targetDay=5 // Which is a Friday
		// Determine earliest possible date a third Friday could happen that month
		// The earliest this would be is when the first day is a Friday, and then two weeks later
		earliestDayThirdFriday = 1+2*7 // The target day is the third Friday: so + 2 weeks 
		
		// Determine what date the earliest possible day is
		d = new Date(yearNumber,monthNumber,earliestDayThirdFriday)
		actualWeekDay = d.getDay()
		
		// So if this is a Friday, then the 3rd Friday is equal to the earliest possible 3rd Friday. If not, we must search for the 
		// Friday which follows on the earliest possible day.
		if( targetDay==actualWeekDay ) offsetDays = 0;
		else {
			if( targetDay<actualWeekDay ) offsetDays = targetDay + (7 - actualWeekDay);
			else offsetDays = (targetDay + (7 - actualWeekDay)) - 7;
			}
		
		// Add the offset needed on the earliest possible date and calculate the actual date of the 3rd Friday
		dateThirdFriday = new Date(yearNumber,monthNumber,earliestDayThirdFriday + offsetDays);
	
		//return result
		return dateThirdFriday
		}
	}
	
function checkUSRDate(day,month,year) {
	var now = new Date();
	if( day>now.getDate()+1 || day<now.getDate()-1 || month != now.getMonth()+1 || year != now.getFullYear() ) {
		alert('Computer date is not set properly. Expiration date calculations for "Friday" and "Friday + 1 week" will not function correctly. Please update local computer clock date ...')
		}
	else {return true}
	}
	 
function createCalendar(dateFriday) {
	// Create new calendar object as the previous one might have had dates disabled ...
	inputDate = new CalendarPopup("calendarDiv"); 
	now = new Date();
	
	// Different offset
	inputDate.offsetX=-120;
	inputDate.offsetY = -200;
	// Disable dates from the past
	inputDate.addDisabledDates(null,formatDate(now,"yyyy-MM-dd"));
	// Disable dates after the expiration date
	// add 1 day to be able to select the expiration day
	dateFriday.setDate(dateFriday.getDate()+1);
	inputDate.addDisabledDates(formatDate(dateFriday,"yyyy-MM-dd"),null);
	}
	
	
function checkDateValidity(targetDate,expiration,expYear) {
	// calculate the expiration date first
	expirationDate =calculateFridayDate(expiration, expYear)
	// Sanity check: the targetDate shouldn't be after the expirationDate!
	if (formatDate(expirationDate,"MM/dd/yy")<targetDate) {
		//alert("The expiration date is before the defined date! Please correct the 'Define date:' form ...")
		return false
		}
	else {return true}
	}
