var b = -1;
var s = -1;
var eb_b = -1;
var eb_s = -1;

Math.hold = function(x,n)
{
  var N = Math.pow(10,n);
  return Math.round(x * N)/N;
}

function get_cookie(name)
{   
  var strCookie=document.cookie;
  var arrCookie=strCookie.split("; ");
  
  for(var i=0;i<arrCookie.length;i++)
  {
    var arr=arrCookie[i].split("=");
    if(name==arr[0])
    {
      return unescape(arr[1]);
    }
  }
  
  return '';
}

function update_amount()
{
  if (b != -1 && s != -1)
  {
    if (eb_b != eb_s)
    {
      amount = ex.pay_amount.value*b/s;
      amount = Math.hold(amount,2);
      document.all('get_amount').innerHTML = amount;
    }
    else
    {
      document.all('get_amount').innerHTML = 'n/a';
    }
  }
}

function update_fee1(eb_name,fee_low,fee_high,fee_rate,t)
{
	document.all('fee1').innerHTML = 'The  transfer fee of '+eb_name+':<br>'+fee_rate+'% of the transfer amount, minimum '+fee_low+t+', maximum '+fee_high+t;
}

function update_fee2(eb_name,fee_low,fee_high,fee_rate,t)
{
	document.all('fee2').innerHTML = 'The  transfer fee of '+eb_name+':<br>'+fee_rate+'% of the transfer amount, minimum '+fee_low+t+', maximum '+fee_high+t;
}

function fromtochange(eb_id,eb_name,rate_buy,rate_sell)
{
  if (rate_buy != -1)
  {
    eb_b = eb_id;
    b = rate_buy;
    document.all('pay').innerHTML = eb_name;
  }
  
  if (rate_sell != -1)
  {
    eb_s = eb_id;
    s = rate_sell;
    document.all('get').innerHTML = eb_name;
  }
  
  if (eb_b == eb_s)
  {
    document.all('rate').innerHTML = 'N/A';
  }
  else if (b != -1 && s != -1)
  {
  	if (b == 1)
  	{
  	  var r = s;
  	  document.all('rate').innerHTML = r+':1';
  	}
  	else
  	{
  	  r = Math.hold(b/s,4);
      document.all('rate').innerHTML = '1:'+r;
    }
  }

  update_amount();
}

function ex_check()
{
  if (!ex.pay_amount.value)
  {
    alert('Input the amount to exchange out please');
    return false;
  }
  
  return true;
}

function ex_done_check()
{
  if (!ex.payto.value)
  {
    alert('Input '+document.all('payto_title').innerHTML+' please');
    ex.payto.focus();
    return false;
  }
  
  if (!ex.email.value)
  {
    alert('Input your E-mail please');
    ex.email.focus();
    return false;
  }
  
  if (!ex.vcode.value)
  {
    alert('Input the number showed in picture please');
    ex.vcode.focus();
    return false;
  }
  
  return true;
}

function order_done_check()
{
  if (!order.orderid.value)
  {
    alert('Input '+document.all('orderid_title').innerHTML+' please');
    order.orderid.focus();
    return false;
  }
  
  if (!order.amount.value)
  {
    alert('Input '+document.all('amount_title').innerHTML+' please');
    order.amount.focus();
    return false;
  }
  
  if (!order.vcode.value)
  {
    alert('Input the number showed in picture please');
    order.vcode.focus();
    return false;
  }
  
  return true;
}

function msg_check()
{
  if (!msg.message.value)
  {
    alert('Input a message please');
    msg.message.focus();
    return false;
  }
  
  if (!msg.email.value)
  {
    alert('Input your E-mail please');
    msg.email.focus();
    return false;
  }
  
  return true;
}
