function check_account_balance()
{
     SetCookie('cook_balance',$F('balance'),3);   
     param = "action=acct_bal&balance="+$F('balance');
     resultDiv = 'validate1';
     checkit(param,resultDiv);
}
function check_percent_risk()
{
     SetCookie('cook_risk',$F('risk'),3);        
     var param = "action=percent_risk&risk="+$F('risk');
     resultDiv = 'validate2';
     checkit(param,resultDiv);

}
function check_stop_loss()
{
     SetCookie('cook_stoploss',$F('stoploss'),3);             
     param = "action=stop_loss&stoploss="+$F('stoploss');
     resultDiv = 'validate3';
     checkit(param,resultDiv);

}

function check_extra_field1()
{
     SetCookie('cook_extra_price',$F('extra_price'),3);                  
     param = "action=extra1&extra_price="+$F('extra_price');
     resultDiv = 'validate4';
     checkit(param,resultDiv);

}
function check_extra_field2()
{
     SetCookie('cook_extra_price2',$F('extra_price2'),3);                       
     param = "action=extra2&extra_price2="+$F('extra_price2');
     resultDiv = 'validate5';
     checkit(param,resultDiv);

}

function set_currency_cookie()
{
     SetCookie('cook_currency',$F('currency'),3);
     get_cat();
}
function checkit(param,resultDiv)
{
     var url="validator.php";
     var ajax = new Ajax.Updater(
          {success: resultDiv},
          url,
          {method: 'get', parameters: param, onFailure: reportError});
}
function reportError(request)
{
     
}


function step2()
{
     error = 0;
     if($F('balance') == '' || $F('balance') <= 0 || isNaN($F('balance')))
     {
          check_account_balance();
          error = 1;
     }
     
     if($F('risk') == '' || $F('risk') <= 0 || $F('risk') > 100 || isNaN($F('risk')))
     {
          check_percent_risk();
          error = 1;
     }     

     if($F('stoploss') == '' || $F('stoploss') <= 0 || isNaN($F('stoploss')))
     {
          check_stop_loss();
          error = 1;
     } 
     

     if($F('cat') == 2 && ( $F('extra_price') == '' || $F('extra_price') <=0 || isNaN($F('extra_price'))))
     {
          check_extra_field1();
          error = 1;
          document.getElementById('extra1').style.display = "block";

          
     }


     if($F('cat') == 3)
     {
          if($F('extra_price') == '' || $F('extra_price') <=0  || isNaN($F('extra_price')))
          {
               check_extra_field1();
               error = 1;
          }
          if($F('extra_price2') == '' || $F('extra_price2') <=0  || isNaN($F('extra_price2')))
          {
               check_extra_field2();
               error = 1;
          }
          document.getElementById('extra1').style.display = "block";
          document.getElementById('extra2').style.display = "block";          
          
     }
     ext_opt1 = document.getElementById('extra1').style.display;
     ext_opt2 = document.getElementById('extra2').style.display;     

     if(error != 1)
     {
          final_calc();                         
     }
     else
     {
          document.getElementById('calcRes').style.display = 'none';          
          return false;
     }
}

function performAJAX(param)
{
     var url="validator.php";
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: param, 
			onComplete: showResponse
		});
}

function showResponse(Request)
{
     var result = Request.responseText;
     arr = result.split(":");

     if(arr[0] == 1)
     {
          document.getElementById('extra1').style.display = "none";
          document.getElementById('extra2').style.display = "none";          
     }
     else if(arr[0] == 2)
     {
          document.getElementById('lbl1').innerHTML = "Enter current rate of " + arr[1] + "/" + arr[2] + ": ";
          document.getElementById('extra1').style.display = "block";
          document.getElementById('extra2').style.display = "none";          
     }
     else if(arr[0] == 3)
     {
          document.getElementById('lbl1').innerHTML = "Enter current rate of " + arr[1] + "/" + arr[2] + ": ";
          document.getElementById('extra1').style.display = "block";

          document.getElementById('lbl2').innerHTML = "Enter current rate of " + arr[3] + ": ";
          document.getElementById('extra2').style.display = "block";
          
     }
}

function final_calc()
{
          var param = "action=calculate" +
                      "&currency=" + $F('currency') +
                      "&balance=" + $F('balance') +
                      "&risk=" + $F('risk') +
                      "&pair=" + $F('pair') +
                      "&stoploss=" + $F('stoploss');

          if($F('cat') == 2)
          {
               param = param + "&extraprice=" + $F('extra_price');
          }
          if($F('cat') == 3)
          {
               param = param + "&extraprice=" + $F('extra_price') + "&curprice=" + $F('extra_price2');
          }
          performCalc(param);
}

function performCalc(param)
{
     var url="validator.php";
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: param, 
			onComplete: showCalculation
		});
}

function showCalculation(Request)
{
     var result = Request.responseText;
     document.getElementById('calcRes').style.display = 'block';     
     document.getElementById('calcRes').innerHTML = result;
}

function get_cat()
{
     SetCookie('cook_pair',$F('pair'),3);           
     param = "action=getCase&currency="+$F('currency')+"&pair="+$F('pair');
     getCaseAJAX(param);
}

function getCaseAJAX(param)
{
     var url="validator.php";
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: param, 
			onComplete: updateCase
		});
}

function updateCase(Request)
{
     var result = Request.responseText;

     document.getElementById('cat').value = result;

     SetCookie('cook_cat',result,3);     

//     document.getElementById('calc').style.display = 'none';
     document.getElementById('calcRes').style.display = 'none';

     if(result == 1)
     {
          document.getElementById('extra1').style.display = 'none';
          document.getElementById('extra2').style.display = 'none';     
     }
     param = "action=step2&currency="+$F('currency')+"&pair="+$F('pair');
     performAJAX(param);
    
}

function getCookie(Name) {
   var search = Name + "="
   if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search)
      if (offset != -1) { // if cookie exists 
         offset += search.length 
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset) 
         // set index of end of cookie value
         if (end == -1) 
            end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))

      } 
   }
}

function SetCookie(cookieName,cookieValue,nDays) {
     var today = new Date();
     var expire = new Date();

     str = cookieName+"="+escape(cookieValue);
     if (nDays!=null && nDays!=0)
     {
      expire.setTime(today.getTime() + 3600000*24*nDays);
      str = str + ";expires="+expire.toGMTString();
     }
     document.cookie = str;
}
function load_cookies()
{
     if(getCookie('currency'))
          document.getElementById('currency').value = getCookie('currency');
     if(typeof(getCookie('balance')) != 'undefined')
          document.getElementById('balance').value = getCookie('balance');
     if(typeof(getCookie('risk')) != 'undefined')          
          document.getElementById('risk').value = getCookie('risk');
     if(typeof(getCookie('stoploss')) != 'undefined')                    
          document.getElementById('stoploss').value = getCookie('stoploss');
     if(getCookie('pair'))                              
          document.getElementById('pair').value = getCookie('pair');
     if(typeof(getCookie('extra_price')) != 'undefined')                              
          document.getElementById('extra_price').value = getCookie('extra_price');
     if(typeof(getCookie('extra_price2')) != 'undefined')                              
          document.getElementById('extra_price2').value = getCookie('extra_price2');
     if(getCookie('cat'))
          document.getElementById('cat').value = getCookie('cat');

     cat_val = getCookie('cat');
     if(cat_val == 2)
     {
          document.getElementById('extra1').style.display = 'block';
          param = "action=step2&currency="+$F('currency')+"&pair="+$F('pair');
          performAJAX(param);
          
     }
     if(cat_val == 3)
     {
          document.getElementById('extra1').style.display = 'block';
          document.getElementById('extra2').style.display = 'block';
          param = "action=step2&currency="+$F('currency')+"&pair="+$F('pair');
          performAJAX(param);
          
     }
          
     initial_check();
     
}

function initial_check()
{
     check_account_balance();
     check_percent_risk();
     check_stop_loss();
     check_extra_field1();
     check_extra_field2();
}
