﻿// Validation function for the calculator
function ValidateFields()
{    
    if(document.getElementById('ctl00_Main_Content_ddlOption').selectedIndex == 0)
    {
        alert("Please select a \"Buy / Write a option \" !");
        document.getElementById('ctl00_Main_Content_ddlOption').focus();        
        return false;
    }
    if(document.getElementById('ctl00_Main_Content_txtStrikePrice').value == "")
    {
        alert("Please enter the \"Strike Price \" !");
        document.getElementById('ctl00_Main_Content_txtStrikePrice').focus();        
        return false;
    }
    if(document.getElementById('ctl00_Main_Content_txtLotSize').value == "")
    {
        alert("Please enter the \"Lot Size \" !");
        document.getElementById('ctl00_Main_Content_txtLotSize').focus();        
        return false;
    }
    if(document.getElementById('ctl00_Main_Content_txtPremium').value == "")
    {
        alert("Please enter the \"Premium \" !");
        document.getElementById('ctl00_Main_Content_txtPremium').focus();        
        return false;
    }
    if(document.getElementById('ctl00_Main_Content_txtExpectedStock').value == "")
    {
        alert("Please enter the \"Expected Stock Movement at Expiry \" !");
        document.getElementById('ctl00_Main_Content_txtExpectedStock').focus();        
        return false;
    }
    callAjaxCalculation(document.getElementById('ctl00_Main_Content_txtStrikePrice').value,document.getElementById('ctl00_Main_Content_txtLotSize').value,document.getElementById('ctl00_Main_Content_txtPremium').value,document.getElementById('ctl00_Main_Content_txtExpectedStock').value);
    return false;
}

function Toogle()
{    
    if(document.getElementById("ctl00_Main_Content_hfPage").value == 1)
    {       
        document.getElementById("divResult").style.visibility = "visible";       
    }
}
// AJAX Function Sending  xmlhttp request

var xmlHttp;
function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
//--------------------------

function callAjaxCalculation(StrikePrice,LotSize,Premium,ExpectedStock) 
{  
    xmlHttp = GetXmlHttpObject(); 
    if (xmlHttp==null) 
    { 
        alert ("Browser does not support HTTP Request");
         return; 
    }                       
    //you can provide your page URL which will fetch you the values from your database
    var url = "DerivativeCalculator.aspx"; 
    url = url + "?Page=Calculate&Option=" + document.getElementById("ctl00_Main_Content_ddlOption").value +"&Parameters=" + StrikePrice + ";" + LotSize + ";" + Premium + ";" + ExpectedStock;
    //state change event-this will occur ass soon as response comes from the url
    xmlHttp.onreadystatechange = stateChanged; 
    xmlHttp.open("GET",url,true); 
    xmlHttp.send(null);          
}
//--------------------------

//event
function stateChanged() 
{ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
    { 
        //Display contents 
        var xmlResponse = xmlHttp.responseText; 
        if (xmlResponse!='') 
        {         
            var Output = xmlResponse.split('##');               
            document.getElementById("divResult").style.visibility = "visible";
            document.getElementById("ctl00_Main_Content_imgResult").src = Output[0];
            document.getElementById("ctl00_Main_Content_lblResult").innerHTML = Output[1];
            document.getElementById("ctl00_Main_Content_lblResult").style.color = "#" + Output[2].substring(2);
            document.getElementById("ctl00_Main_Content_lblAmountofProfilt").innerHTML = Output[3];
            document.getElementById("ctl00_Main_Content_lblAmountofProfilt").style.color = "#" + Output[4].substring(2);         
            if(Output[5] == "True")
            {
                 document.getElementById("ctl00_Main_Content_lblBreak").innerHTML = "at";
            } 
            else
            {
                document.getElementById("ctl00_Main_Content_lblBreak").innerHTML = "of";
            }           
            //alert(xmlHttp.getAllResponseHeaders());                  
        } 
        else 
        { 
            alert("No Responce from the Server!");
        } 
    } 
}
//--------------------------
function Validatefuture()
{
    var Option;
    if(document.getElementById('ctl00_Main_Content_ddlCompanyName').selectedIndex == 0)
    {
        alert("Please select a Option !");
        document.getElementById('ctl00_Main_Content_ddlCompanyName').focus();        
        return false;
    }
    if(document.getElementById('ctl00_Main_Content_ddlOption').selectedIndex == 0)
    {
        alert("Please select \" Buy / Sell Future option \" !");
        document.getElementById('ctl00_Main_Content_ddlOption').focus();        
        return false;
    }
    if(document.getElementById('ctl00_Main_Content_txtButSellPrice').value == "")
    {
        alert("Please enter the \" Buy / Sell Price \" !");
        document.getElementById('ctl00_Main_Content_txtButSellPrice').focus();        
        return false;
    }
    if(document.getElementById('ctl00_Main_Content_txtLotSize').value == "")
    {
        alert("Please enter the \" Lot Size \" !");
        document.getElementById('ctl00_Main_Content_txtLotSize').focus();        
        return false;
    }   
    if(document.getElementById('ctl00_Main_Content_txtExpectedStock').value == "")
    {
        alert("Please enter the \" Expected Stock Movement at Expiry \" !");
        document.getElementById('ctl00_Main_Content_txtExpectedStock').focus();        
        return false;
    }    
    if(document.getElementById("ctl00_Main_Content_rbtnStock").checked == true)
    {
        Option = "Stock";
    }
    if(document.getElementById("ctl00_Main_Content_rbtnIndex").checked == true)
    {
        Option = "Index";
    }
    callAjaxCalculationforFuture(Option,document.getElementById('ctl00_Main_Content_ddlCompanyName').value,document.getElementById('ctl00_Main_Content_txtButSellPrice').value,document.getElementById('ctl00_Main_Content_txtLotSize').value,document.getElementById('ctl00_Main_Content_txtExpectedStock').value);
    return false;
}
//--------------------------
function callAjaxCalculationforFuture(Option,CompanyName,BuySellPrice,LotSize,ExpectedStock) 
{     
  debugger;
    xmlHttp = GetXmlHttpObject(); 
    if (xmlHttp==null) 
    { 
        alert ("Browser does not support HTTP Request");
         return; 
    }                         
    //you can provide your page URL which will fetch you the values from your database    
    var url = "FutureCalculator.aspx"; 
    url = url + "?Page=" + Option + "&Option=" + document.getElementById("ctl00_Main_Content_ddlOption").value +"&Parameters=" + CompanyName + ";" + BuySellPrice + ";" + LotSize + ";" + ExpectedStock;
    //state change event-this will occur ass soon as response comes from the url
    xmlHttp.onreadystatechange = futureChanged; 
    xmlHttp.open("GET",url,true); 
    xmlHttp.send(null);          
}
//--------------------------

//event
function futureChanged() 
{ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
    { 
        //Display contents 
        var xmlResponse = xmlHttp.responseText; 
        if (xmlResponse!='') 
        {         
            var Output = xmlResponse.split('##');               
            document.getElementById("divResult").style.visibility = "visible";
            document.getElementById("ctl00_Main_Content_imgResult").src = Output[0];
            document.getElementById("ctl00_Main_Content_lblResult").innerHTML = Output[1];
            document.getElementById("ctl00_Main_Content_lblResult").style.color = "#" + Output[2].substring(2);
            document.getElementById("ctl00_Main_Content_lblAmountofProfilt").innerHTML = Output[3];
            document.getElementById("ctl00_Main_Content_lblAmountofProfilt").style.color = "#" + Output[4].substring(2);         
            if(Output[5] == "True")
            {
                 document.getElementById("ctl00_Main_Content_lblBreak").innerHTML = "at";
            } 
            else
            {
                document.getElementById("ctl00_Main_Content_lblBreak").innerHTML = "of";
            }                                     
        } 
        else 
        { 
            alert("No Responce from the Server!");
        } 
    } 
}
//--------------------------
function GetNumber(obj, decimalPlaces, allowNegative)
{
	var temp = obj.value;
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');
	if (allowNegative) {

		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	obj.value = temp;
}
function AvoidDot(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	if (isNaN(key)) return true;
	keychar = String.fromCharCode(key);
	if (key == 8 || isCtrl)
	{
		return true;
	}
	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}

//Allows Only Numbers
var range = { 'onlynumbers':/[^\d]/g }
function valid(o,w)
{
  o.value = o.value.replace(range[w],'');
}

