
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 validateInputs() {
	
	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);	

	
	//	if ((!inpType && inpType2)||(inpType && !inpType2)) {
//		launchError( "In the trial version, the option type must be the same. Please make sure both options are call or put.");
//		return false;
//	}
	
	//stock validations
	if (S == 0){
		launchError("Please enter a stock price");
		return false;
	}if (isNaN(S)){
		launchError("Please enter a stock price");
		return false;
	}else if(S == ''){
		launchError( "Please enter a stock price");
		return false;
	}else if(S < 0){
		launchError( "Please enter a valid stock price");
		return false;	
	}else if(S > 1000){
		launchError( "At present SaberSystem does not handle stocks over $1000. Please select a stock with price below $1,000.");
		return false;
	}else if(S>0 && S<10){
		launchError( "There are no indicative options on this stock. Please select another stock.");
		return false;
	}
	
	//interest rate validations
	if(isNaN(i)){
		launchError( "Please enter interest rate. If the rate is zero, please enter 0.0");
		return false;
	}
	if(i/100 < 0.0005){
		launchError( "Interest rate input format is i percent per year. For example, a rate of 3 percent per year must be entered as 3.0");
		return false;
	}
	if(i/100 >0.15){
		launchError( "At present SaberSystem does not allow for interest rate greater than 15 percent. Please enter a valid rate");
		return false;
	}

	//expiration validation
	if(isNaN(T1)||isNaN(T2)){
		launchError( "Please enter the expiration date.");
		return false;
	}

	if(T1 != T2){
		launchError( "SaberSystem currently does not process options with different expiration dates. Please select options from the same expiration cycle.");
		return false;
	}
	if(T1 == 0 && T2 == 0){
		launchError( "On the expiration date the options prices contain little information about the behavior of the underlying stock.");
		return false;
	}
	if (T1 > 42){
 		launchError ("The system presently processes only current option.");
 		return false;
	}

//Valide strike prices	
	if(K1 == 0 || K2 == 0){
		launchError( "Please enter a strike price");
		return false;
	} else if(isNaN(K1) || isNaN(K2)){
		launchError( "Please enter a valid strike price.");
		return false;
	} else if(K1=='' || K2==''){
		launchError( "Please enter a strike price.");
		return false;
	} else if(K1 < 0 || K2 < 0){
		launchError( "Please enter a valid strike price.");
		return false;
	}else if(K1 > 1000 || K2 > 1000){
		launchError( "At present SaberSystem does not handle strikes over $1000. Please select a strike below $1,000.");
		return false;
	}

	
	//strike validation
	if (K1 < S && (S - K1) > S/2) {
		launchError("The option is deeply in the money. Such options do not have any indicative value. Please select a strike closer to the stock price.");
		return false;
	}
	if (K2 < S && (S -K2) > S/2) {
		launchError("The option is deeply in the money. Such options do not have any indicative value. Please select a strike closer to the stock price.");
		return false;
	}


//	if(inpType){
		if(inp1call == 0||inp2call==0){
			launchError( "Please enter a call price");
			return false;
		} else if (inp1call == '' || inp2call == '') {
			lunchError( "Please enter a call price");
			return false;
		}else if (inp1call < 0 || inp2call < 0){
			launchError( "Please enter a valid call price.");
			return false;
		} else if(isNaN(inp1call) || isNaN(inp2call)){
			launchError( "Please enter a valid call price.");
			return false;
		} else 	if(inp1call <= 0.1 || inp2call <= 0.1){
			launchError( "The call is deeply out of the money. Such options do not have any indicative value. Please select an option with the strike close to the stock price.");
			return false;
		}

		if(K1 == K2){
			launchError( "Both calls cannot have the same strike price. Please select a call with a different strike price.");
			return false;
		}

		if(K1 > K2 && inp1call >= inp2call){
			launchError( "The call with the lower strike must have higher price. Please enter a valid price.");
			return false;
		}


		if(K2 > K1 && inp2call >= inp1call){
			launchError( "The call with the lower strike must have higher price. Please enter a valid price.");
			return false;
		}
		if(K1 < S && inp1call < (S - K1)){
			launchError( "The price of an in-the-money call must at least be equal to its intrinsic value. Please enter a valid price.(If the prices are accurate, you could earn risk free profit by buying the option and exercising it.)");
			return false;
		}
		if(K2 < S && inp2call < (S - K2)){
			launchError( "The price of an in-the-money call must at least be equal to its intrinsic value. Please enter a valid price.(If the prices are accurate, you could earn risk free profit by buying the option and exercising it.)");
			return false;
		}

		if((K1<S && inp1call == (S - K1) && inp1call > 0.149*S)||(K2<S && inp2call == (S - K2) && inp2call > 0.149*S)){
			launchError( "The option is deeply in-the-money. Such options have no indicative value. Please select a different strike price. If the prices are correct, buy the call and short the stock.");
			return false;
		}

		if((K1<S && inp1call == S - K1 && inp1call <= 0.149*S)||(K2<S && inp2call == S - K2 && inp2call <= 0.149*S)){
			launchError( "The call you have entered has no optionality. Please enter a different strike. If the prices are correct, buy the call and short the stock. If the stock increases, you have no risk. If it decreases, you stand to make profit."); 
			return false;
		}
		
		var optdiff = inp1call-inp2call;
		var strikediff = K1 - K2;
		if (Math.abs(optdiff) > Math.abs(strikediff)){
			launchError( "The difference in option prices can not be greater than the difference in strike prices.");
			return false;
		}
//	}else{ //PUT
		if(inp1put == 0 || inp2put == 0){
			launchError( "Please enter a put price");
			return false;
		} else if (inp1put == '' || inp2put == ''){
			lunchError( "Please enter a put price");
			return false;
		}else if (inp1put < 0 || inp2put < 0){
			launchError( "Please enter a valid put price.");
			return false;
		} else if(isNaN(inp1put) || isNaN(inp2put)){
			launchError( "Please enter a valid put price.");
			return false;
		} else 	if(inp1put <= 0.1 || inp2put <= 0.1){
			launchError( "The put is deeply out of the money. Such options do not have any indicative value. Please select an option with the strike close to the stock price.");
			return false;
		}
	
		if(K1 == K2){
			launchError( "Both puts cannot have the same strike price. Please select a put with a different strike price.");
			return false;
		}

		if(K1 > K2 && inp2put >= inp1put){
			launchError( "The put with higher strike must have higher price. Please enter a valid price.");
			return false;
		}

		if(K2 > K1 && inp1put >= inp2put){
			launchError( "The put with higher strike must have higher price. Please enter a valid price.");
			return false;
		}
			
		if(S < K1 && inp1put < (K1-S)){
			launchError( "The price of an in-the-money put must at least be equal to its intrinsic value. Please enter a valid price.");
			return false;
		}
		if(S < K2 && inp2put < (K2 - S)){
			launchError( "PUT-- The price of an in-the-money put must at least be equal to its intrinsic value. Please enter a valid price.");
			return false;
		}

		if((S < K1 && inp1put == (K1- S)&&inp1put > 0.149*S)||(S < K2&& inp2put == (K2 -S)&&inp2put > 0.149*S)){
			launchError( "The option is deeply in-the-money. Such options have no indicative value. Please select a different strike price.");
			return false;
		}

		if((S < K1&& inp1put == (K1-S)&&inp1put <= 0.149*S)||(S<K2 && inp2put == (K2-S)&&inp2put <= 0.149*S)){
			launchError( "The put you have entered has no no optionality. Please enter a different strike price. If the prices are correct, buy the put and short the stock.");
			return false;
		}
		
		var optdiff = inp1put - inp2put;
		var strikediff = K1 - K2;
		if (Math.abs(optdiff) > Math.abs(strikediff)) {
			launchError( "The difference in option prices can not be greater than the difference in strike prices.");
			return false;
		}
//	}

}


