

function launchExplainWindow(filename){
	var win = window.open(filename,'explanation','toolbar=no,location=no,width=500,height=500,status=no,scrollbars=1,resizable=1');
	win.focus();
}

function lockDate(){
	alert("Sorry you are not allowed to change the expiration date!");
	setDefaultDate();
}


function setDefaultDate(){
	var todayDate = new Date();
	var defExp = new Date();

	defExp.setDate(1);

	var dayOfWeek = defExp.getDay();
	var diff = 0;
	if(dayOfWeek==6){
		diff = 6;
	}else{
		diff = 5 - dayOfWeek;
	}

	defExp.setTime(defExp.getTime() + (diff *1000*60*60*24));
	defExp.setTime(defExp.getTime() + (14 * 1000*60*60*24));

	var rdFri = defExp.getDate();
	var cur = todayDate.getDate();
	var curMonth = todayDate.getMonth() + 1;
	var nextMonth = curMonth + 1;
	var month = curMonth;
	var year = todayDate.getYear();
	var defYear = year;
	var dateZero = new Date(2001,1,1);
	var yearZero = dateZero.getYear();	
 

	if(curMonth == 12){
		nextMonth = 1;
	}

	if(cur >= rdFri){
		month = nextMonth;
		if(curMonth ==12){
			defYear = year + 1; 
		}
	}

	document.NearbyForm.month.options[month -1].selected = true;
	document.NearbyForm.year.options[defYear-yearZero].selected = true;

	//set date to 3rd Fri
	document.NearbyForm.day.options[0].selected = true;

	date2days();
	document.NearbyForm.month2.options[month -1].selected = true;
	document.NearbyForm.year2.options[defYear-yearZero].selected = true;

	//set date to 3rd Fri
	document.NearbyForm.day2.options[0].selected = true;
	date2days2();


}



function launchWindow(){
	var win = window.open('terms.html','glossary','width=300,height=200,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no');
	win.focus();
}

function launchError(errMessage){
	var win = window.open('error.php?msg='+errMessage,'errorWin','width=239,height=300,toolbar=no,menubar=no,scrollbar=no,resizable=yes,location=no');
	win.focus();
}

function checkNearby() {
	var S=parseFloat(document.NearbyForm.stockPrice.value);
	var i=parseFloat(document.NearbyForm.rate.value);

	var K1=parseFloat(document.NearbyForm.strikePrice.value);
	var T1=parseFloat(document.NearbyForm.term.value);
//	var inpType = document.NearbyForm.inpType[0].checked;
	var inp1call=parseFloat(document.NearbyForm.callPrice1.value);
	var inp1put=parseFloat(document.NearbyForm.putPrice1.value);	


	var K2 =parseFloat(document.NearbyForm.strikePrice2.value);
	var T2 =parseFloat(document.NearbyForm.term2.value);
//	var inpType2 = document.NearbyForm.inpType2[0].checked;
	
	var inp2call =parseFloat(document.NearbyForm.callPrice2.value);	
	var inp2put =parseFloat(document.NearbyForm.putPrice2.value);	

	//check for missing inputs
	if( isNaN(S) || (S < 0.0)) {
		launchError("Please enter a stock price");
		return false;	
	} else if (isNaN(i) || ((i > 0.0) && (i <= 0.5))) {
		launchError("Please enter interest rate in APR form. For example, 4.5 percent should be entered as 4.5");
		return false;	
	}

	if (isNaN(K1) || (K1 < 0.0)) {
		launchError("Please enter a strike price for Option 1");
		return false;	
	}
	if (isNaN(T1) || (T1 < 0.0)) {
		launchError("Please enter a time to expiration for Option 1");
		return false;	
	} 
	 if (isNaN(inp1call) || (inp1call < 0.0)) {
	 	launchError("Please enter an call price for Strike 1");
	 	return false;	
	}	
	if (isNaN(inp1put) || (inp1put < 0.0)) {
		launchError("Please enter an put price for Strike 1");
		return false;	
	}	

	if (isNaN(K2) || (K2 < 0.0)) {
		launchError("Please enter a strike price for Option 2");
		return false;
	}
	if (isNaN(T2) || (T2 < 0.0)) {
		launchError("Please enter a time to expiration for Option 2");
		return false;	
	} 
	
	if (isNaN(inp2call) || (inp2call < 0.0)) {
		launchError("Please enter an call price for Strike 2");
		return false;	
	}
	if (isNaN(inp2put) || (inp2put < 0.0)) {
		launchError("Please enter an put price for Strike 2");
		return false;	
	}	

//	if(inpType != inpType2){
//		launchError("The option type must be the same. Please enter the correct option type.");
//		return false;
//	}

//if(inpType){
//	alert("call inpType="+inpType1);
	//step 1
	if(S == 0){
		launchError("Stock price cannot be zero. Please enter a valid price.");
		return false;	
	}
	//step 2
	if(inp1call == 0 || inp2call == 0||inp1put==0||inp2put==0){
		launchError("Option price cannot be zero. Please enter a valid price.");
		return false;	
	}
	//step 3
	if(K1 == 0 || K2 == 0){
		launchError("Strike price cannot be zero. Please enter a valid price.");
		return false;	
	}
	//step 4
	if(K1 >= 1.6*S || K2 >= 1.6*S){
		launchError("Option is deeply out of the money. The trial system cannot handle these options. Please enter a call with a strike price closer to the current stock price.");
		return false;	
	}

	if(K1 <= 0.35*S || K2 <= 0.35*S){
		launchError("The option is deeply in the money. The trial system cannot handle these options. Please enter a call with a strike price closer to the current stock price.");
		return false;	
	}

	if((K1<S && inp1call < S-K1)||
		(K2<S && inp2call < S-K2 )  ){
		launchError("Call price should be at least equal to the option's intrinsic value. Please enter a correct price.");
		return false;
	}

//Comparative between Option 1 and Option 2

//set closest strike to strike1

	if(T1!=T2){
		launchError("The expiration date for options must be equal. Please enter a new call with the same expiration as Option 1.");
		return false;
	}
	if(K1 == K2){
		launchError("Options are identical. Please enter a call with a different strike price.");
		return false;
	}
	if((K1 > K2) &&(inp1call >= inp2call)){
		launchError("The call with higher strike must have a lower price.Please enter correct price.");
		return false;
	}

	var strike1 = K1;
	var strike2 = K2;
	
	if(Math.abs(S-K2)<Math.abs(S-K1)){
		strike1 = K2;
		strike2 = K1;
	}
/*	
	if(strike1<=S && S<=strike2){
		if( (Math.abs(strike1-strike2)/2 <= Math.abs(strike1-S)) ||
			(Math.abs(strike1-strike2)/2 <= Math.abs(strike2-S)) ){
			launchError("There is an option with a strike price closer to the stock price. Please enter that option for better results.");
			return false;
		}
	}
*/
	if(S<=strike1 && strike1<strike2){
		if( (Math.abs(strike1-strike2) <= Math.abs(strike1-S)) ){
			launchError("There is an option with a strike price closer to the stock price. Please enter that option for better results.");
			return false;
		}
	}
	

	if(S>=strike1 && strike1>strike2){
		if( (Math.abs(strike1-strike2) <= Math.abs(strike1-S)) ){
			launchError("There is an option with a strike price closer to the stock price. Please enter that option for better results.");
			return false;
		}
	}

//}else{
	//PUT OPTION
//alert("put inpType="+inpType1);
	//step 1
//	if(S == 0){
//		launchError("Stock price cannot be zero. Please enter a valid price.");
//		return false;	
//	}
//	//step 2
//	if(optPrice1 == 0 || optPrice2 == 0){
//		launchError("Option price cannot be zero. Please enter a valid price.");
//		return false;	
//	}
//	//step 3
//	if(K1 == 0 || K2 == 0){
//		launchError("Strike price cannot be zero. Please enter a valid price.");
//		return false;	
//	}
//	//step 4
//	if(K1 >= 1.6*S || K2 >= 1.6*S){
//		launchError("Option is deeply in the money. The trial system cannot handle these options. Please enter a put with a strike price closer to the current stock price.");
//		return false;	
//	}
//
//	if(K1 <= 0.35*S || K2 <= 0.35*S){
//		launchError("The option is deeply out of the money. The trial system cannot handle these options. Please enter a put with a strike price closer to the current stock price.");
//		return false;	
//	}
//
	if((S<K1 && inp1put < K1-S)||
		(S<K2 && inp2put < K2-S )  ){
		launchError("Put price should be at least equal to the option's intrinsic value. Please enter a correct price.");
		return false;
	}

//Comparative between Option 1 and Option 2

//set closest strike to strike1

	if(T1!=T2){
		launchError("The expiration date for options must be equal. Please enter a new put with the same expiration as Option 1.");
		return false;
	}
	if(K1 == K2){
		launchError("Options are identical. Please enter a put with a different strike price.");
		return false;
	}
	if((K1 > K2) &&(inp2put >= inp1put)){
		launchError("The put with higher strike must have a lower price.Please enter correct price.");
		return false;
	}

	var strike1 = K1;
	var strike2 = K2;
	
	if(Math.abs(S-K2)<Math.abs(S-K1)){
		strike1 = K2;
		strike2 = K1;
	}
/*	
	if(strike1<=S && S<=strike2){
		if( (Math.abs(strike1-strike2)/2 <= Math.abs(strike1-S)) ||
			(Math.abs(strike1-strike2)/2 <= Math.abs(strike2-S)) ){
			launchError("There is an option with a strike price closer to the stock price. Please enter that option for better results.");
			return false;
		}
	}
*/
	if(S<=strike1 && strike1<strike2){
		if( (Math.abs(strike1-strike2) <= Math.abs(strike1-S)) ){
			launchError("There is an option with a strike price closer to the stock price. Please enter that option for better results.");
			return false;
		}
	}
	

	if(S>=strike1 && strike1>strike2){
		if( (Math.abs(strike1-strike2) <= Math.abs(strike1-S)) ){
			launchError("There is an option with a strike price closer to the stock price. Please enter that option for better results.");
			return false;
		}
	}


//}



}



function days2date()
{
  var term=parseFloat(document.NearbyForm.term.value); if( isNaN( term )) { term = 0; }
  var today = new Date();
  var exp = new Date();
  exp.setTime( today.getTime() + ( term * 1000 * 60 * 60 * 24 ));

  var day =  exp.getDate(); 
	if( isNaN( day )) { day = 1; }
  var month = exp.getMonth() + 1; 
	if( isNaN( month )) { month = 1; }
  var year = exp.getYear(); 
	if( isNaN( year )) { year = 2000; }
  if( year < 1900 ) { year += 1900 };

  document.NearbyForm.month.value = month;
  document.NearbyForm.day.value = day ;
  document.NearbyForm.year.value = year;
  
  document.NearbyForm.month.options[month-1].selected = true;
 document.NearbyForm.day.options[day].selected=true;
	document.NearbyForm.year.options[year-2001].selected = true;	

  return(true);
}


function days2date2()
{
  var term=parseFloat(document.NearbyForm.term2.value); if( isNaN( term )) { term = 0; }
  var today = new Date();
  var exp = new Date();
  exp.setTime( today.getTime() + ( term * 1000 * 60 * 60 * 24 ));

  var day =  exp.getDate(); 
	if( isNaN( day )) { day = 1; }
  var month = exp.getMonth() + 1; 
	if( isNaN( month )) { month = 1; }
  var year = exp.getYear(); 
	if( isNaN( year )) { year = 2000; }
  if( year < 1900 ) { year += 1900 };

  document.NearbyForm.month2.value = month;
  document.NearbyForm.day2.value = day ;
  document.NearbyForm.year2.value = year;
  
  document.NearbyForm.month2.options[month-1].selected = true;
 document.NearbyForm.day2.options[day].selected=true;
	document.NearbyForm.year2.options[year-2001].selected = true;	

  return(true);
}



function date2days()
{
  
  var day = parseFloat(document.NearbyForm.day.options[document.NearbyForm.day.selectedIndex].value);
  var month = parseFloat(document.NearbyForm.month.options[document.NearbyForm.month.selectedIndex].value) -1;
  var year = parseFloat(document.NearbyForm.year.options[document.NearbyForm.year.selectedIndex].value);

  var ptoday = new Date();
  var ttyear = ptoday.getFullYear();
  var ttmonth = ptoday.getMonth();
  var ttday = ptoday.getDate();
  var today = new Date(ttyear,ttmonth,ttday);
  
  var exp;
  var oneDay = (1000 * 60 * 60 * 24);

  if( day == -1 ) {
	
    exp = new Date(year, month, 1);
    while( exp.getDay() != 5 ) { //is is Friday 
	exp.setTime(exp.getTime()+ oneDay);
    }

//    exp.setTime(exp.getTime()+(oneDay * 15));  WHY 15???
    exp.setTime(exp.getTime()+(oneDay * 14));
  } else {  
    exp = new Date(year, month, day);
      }
      
  var BigCheck = exp.getTime() / (oneDay);
  
  var term = (exp.getTime() - today.getTime()) / ( oneDay );
  var testtoday = today.getTime()/ (oneDay);

  
   testtoday = Math.round(testtoday);
  BigCheck = Math.round(BigCheck);

  if( isNaN( term ) || term < 0.0 ) { 
	term = 0; 
	}
  term = Math.round(term);
  document.NearbyForm.term.value = term;
 

  return(true);
}


function date2days2()
{
  
  var day = parseFloat(document.NearbyForm.day2.options[document.NearbyForm.day2.selectedIndex].value);
  var month = parseFloat(document.NearbyForm.month2.options[document.NearbyForm.month2.selectedIndex].value) -1;
  var year = parseFloat(document.NearbyForm.year2.options[document.NearbyForm.year2.selectedIndex].value);

   var ptoday = new Date();
   var ttyear = ptoday.getFullYear();
   var ttmonth = ptoday.getMonth();
   var ttday = ptoday.getDate();
   var today = new Date(ttyear,ttmonth,ttday);
  
  var exp;
  var oneDay = (1000 * 60 * 60 * 24);

  if( day == -1 ) {
	
    exp = new Date(year, month, 1);
    while( exp.getDay() != 5 ) { //is is Friday 
	exp.setTime(exp.getTime()+ oneDay);
    }
//    exp.setTime(exp.getTime()+(oneDay * 15));  WHY 15???
     exp.setTime(exp.getTime()+(oneDay * 14));
  } else {  
    exp = new Date(year, month, day);
  }

  var term = (exp.getTime() - today.getTime()) / ( oneDay );

  if( isNaN( term ) || term < 0.0 ) { 
	term = 0; 
	}
  term = Math.round(term);
  document.NearbyForm.term2.value = term;
	
  return(true);
}



function imgSwap(imgName,swapSrc){
	document[imgName].src=swapSrc
}

function setStockSymbol(){
	document.NearbyForm.symbol.value = document.NearbyForm.symbolList[document.NearbyForm.symbolList.selectedIndex].value;
	//set default stock Price
}

// DEFAULT VALUE CHECKER

function checkDefStock(defStockPrice, guest){
	//if the symbol,default date, price,interest rate, inpType
	if(guest && document.NearbyForm.stockPrice.value != defStockPrice){
	
		alert("Sorry. You are only permitted to use the default values. You must login to enter a different value.");
		document.NearbyForm.stockPrice.value = defStockPrice;
	}
}

function checkDefInterest(defInterest, guest){
	if(guest && document.NearbyForm.rate.value != defInterest){
	
		alert("Sorry. You are only permitted to use the default values. You must login to enter a different value.");
		document.NearbyForm.rate.value = defInterest;
	}
}

function checkDefStrike(defStrike, guest){
	if(guest && document.NearbyForm.strikePrice.value != defStrike){
	
		alert("Sorry. You are only permitted to use the default values. You must login to enter a different value.");
		document.NearbyForm.strikePrice.value = defStrike;
	}
}

function checkDefOptionPrice(defOptionPrice, guest){
	if(guest && document.NearbyForm.optPrice.value != defOptionPrice){
	
		alert("Sorry. You are only permitted to use the default values. You must login to enter a different value.");
		document.NearbyForm.optPrice.value = defOptionPrice;
	}
}

function checkDefInpType(defInpType, guest){
	if(guest && document.NearbyForm.inpType.value != defInpType){
		alert("Sorry. You are only permitted to use the default values. You must login to select a different value.");
		document.NearbyForm.inpType[0].checked = true;
	}
}


function checkSubPlan(){
	if(document.SubPlanForm.subPlan.value){
		alert("Please select a plan");
		return (false);
	}
	return true;
}