
var flagfirst = true;

/*
	Get Listh of currs, which available to change from chosen one

*/
var ua = navigator.userAgent.toLowerCase();
var isIE = (navigator.appName == "Microsoft Internet Explorer") && (ua.indexOf("opera") == -1);

function SetOldValue (val) {
	document.getElementById("curr_to").value = val;
}

function GetExchangeList(recount)
{
	
	var curr_from = document.getElementById("curr_from");
	var curr_to = document.getElementById("curr_to");
	var id = curr_from.value;
	
	var old_val = document.getElementById("curr_to").value;
	

		JsHttpRequest.query(
            './ajax/ajax_getlist.php', // backend
            {
                'id': id
            },
            // Function is called when an answer arrives. 
            function(result, errors) {
					
					
					//var old_val = curr_to.value;
					
					var old_index = curr_to.selectedIndex;
					
					curr_to.options.length = 0;
					
					len = result.length;
					for (i = 0; i < len; i++) {
						//addOption(curr_to, result[i]['name'], result[i]['id'], false);
						curr_to.add(new Option(result[i]['name'], result[i]['id'], false, false), isIE ? curr_to.options.length : null);
						}
											
					if (curr_to.value != curr_from.value) {
						//curr_to.value = old_val;	
						SetOldValue	(old_val);				
						}
						
						
										
					if (curr_to.selectedIndex < 0) {
							curr_to.selectedIndex = 0;							
						}							
					
					GetExchangeWay (recount);
					
					
					out_in_change ('curr_form');		
					
            },	
            false  // do not disable caching
        );
		
		

	
	}


/*
	
	Add an option to SELECT box

*/
function addOption (oListbox, text, value, isDefaultSelected, isSelected)
{
  var oOption = document.createElement("option");
  oOption.appendChild(document.createTextNode(text));
  oOption.setAttribute("value", value);

  if (isDefaultSelected) oOption.defaultSelected = true;
  else if (isSelected) oOption.selected = true;

  oListbox.appendChild(oOption);
}

/*

	Get information 'bout choosen change 

*/		
function GetExchangeWay (recount) {

	var from = document.getElementById("curr_from").value;
	var to = document.getElementById("curr_to").value;

	JsHttpRequest.query(
            './ajax/ajax_backend.php', // backend
            {
                'from': from,  
                'to': to
            },
            // Function is called when an answer arrives. 
            function(result, errors) {
			
					
					exchange_way['way'] 		= result["way"];
					exchange_way['from'] 		= result["from"];
					exchange_way['to'] 			= result["to"];
					exchange_way['course'] 		= result["course"];
					exchange_way['course_back']	= result["course_back"];
					exchange_way['fromname'] 	= result["fromname"];
					exchange_way['toname'] 		= result["toname"];
					exchange_way['frompic'] 	= result["frompic"];
					exchange_way['topic'] 		= result["topic"];	
					exchange_way['type'] 		= result["type"];		
					exchange_way['komiss'] 		= result["komiss"];	
					
					total_amount = parseFloat(result["amount"]);				
					min_amount = parseFloat(result["min_amount"]);
					max_amount = parseFloat(result["max_amount"]);
					komiss = parseFloat(result["komiss"]);
					course = parseFloat(result["course"]);
					course_back = parseFloat(result["course_back"]);
					
					var elem = document.getElementById("div_kurs");	
					
					var str = '';
					if (exchange_way['course'] / exchange_way['course_back'] >= 0.5) {
						str = '<b>1</b> ' + exchange_way['fromname'] + ' = <b>' + (Math.round(exchange_way['course'] * 100) / 100) + '</b> ' + exchange_way['toname'];
						//document.getElementById('curr_from').value = 1;
						//document.getElementById('curr_to').value = course;
						}
					else {
						str = '<b>' + (Math.round(exchange_way['course_back'] * 100) / 100) + '</b> ' + exchange_way['fromname'] + ' = <b>1</b> ' + exchange_way['toname'] ;
						//document.getElementById('curr_from').value = course_back;
						//document.getElementById('curr_to').value = 1;
						}				
					elem.innerHTML = 'Rate: ' + str;	

					var elem2 = document.getElementById("div_amount");
					str = total_amount;
					elem2.innerHTML = 'Остаток средств: <b>' + str + '</b> ' + exchange_way['toname'];
					
					document.getElementById("img_curr_from").src = exchange_way['frompic'];
					document.getElementById("img_curr_to").src = exchange_way['topic'];
					
					//document.getElementById("div_from_name").innerHTML = exchange_way['fromname'];
					//document.getElementById("div_to_name").innerHTML = exchange_way['toname'];					
					
					if (flagfirst) {
						if (exchange_way['course_back'] != 0){
							
							if (exchange_way['course'] / exchange_way['course_back'] >= 0.5) {
								document.getElementById('amount_from').value = 1;
								document.getElementById('amount_to').value = exchange_way['course'];
								}
							else {								
								document.getElementById('amount_from').value = exchange_way['course_back'];
								document.getElementById('amount_to').value = 1;
							}	
							}
						flagfirst = false;
						}
						
						
					/*if (recount) {*/
						in_out_change ('curr_form');
						/*}*/
					
					//ProcessAjaxAnswer (parseInt (result["type"]));	
            },	
            false  // do not disable caching
        );

	}

/*
	From In to Out OnChange handler
*/
function in_out_change(fid, id, code, bForce) {

//alert ('in_out_change');
  if (document.forms[fid].amount_from == null) return;
  
  if (document.forms[fid].curr_from.value == document.forms[fid].curr_to.value)
	return;
  
  in_val = document.forms[fid].amount_from.value.replace(",", ".");
  if (isNaN(in_val)) { in_val = "0"; }
	
  amount = Math.round(in_val * 100) / 100;
	
	// added 23.05.07 - change amount_from according to precision
	if ((in_val != '') && (parseFloat(in_val) != amount))
		document.forms[fid].amount_from.value = amount;

  if (isNaN(document.forms[fid].amount_from.value)) {
		amount = min_amount[fid];
		document.forms[fid].amount_from.value = amount;
		out_in_change(fid, id, code, bForce);
		return;
	}	
	
  if (amount < min_amount)
	{
		min_amount = Math.round (100*min_amount) / 100;
		amount = min_amount;
		document.forms[fid].amount_from.value = min_amount;
		
		in_out_change(fid, id, code, bForce);
	/*	alert('The minimum amount of exchange ' + min_amount); */
		return;
  }
  if (amount > max_amount)
	{
		max_amount = Math.round (100*max_amount) / 100;
		amount = max_amount;
		document.forms[fid].amount_from.value = amount;
		
		in_out_change(fid, id, code, bForce);
		alert('The maximum amount of exchange ' + max_amount);
		return;
  }
	
  if (document.forms[fid].amount_from.value < 0) {
	amount = -amount;
	document.forms[fid].amount_from.value = amount;
	out_in_change(fid, id, code, bForce);
	return;
	}
	
  document.forms[fid].amount_to.value = Math.round(amount * course * 100) / 100;
  
  ExternalSetPaymentSystemsFee ();
	
  //if (bForce) { out_in_change(fid, code); }
}

/*
	From Out to In OnChange handler
*/
function out_in_change(fid, id, code, bForce) {

//alert ('out_in_change');
if (document.forms[fid].curr_from.value == document.forms[fid].curr_to.value)
	return;
	
  if (document.forms[fid].amount_to == null) return;
  if (id == null) return;
	
  out_val = document.forms[fid].amount_to.value.replace(",", ".");
  if (isNaN(out_val)) { out_val = "0"; }

  amount = Math.round(out_val * 100) / 100;
	
	// added 23.05.07 - change amount_out according to precision
	if ((out_val != '') && (parseFloat(out_val) != amount))
		document.forms[fid].amount_to.value = amount;
  
  if (isNaN(document.forms[fid].amount_to.value)) {
	amount = 0;
	document.forms[fid].amount_to.value = amount;
	in_out_change(fid, id, code, bForce);
	return;
	}
	
	
  if (document.forms[fid].amount_to.value < 0) {
	amount = -amount;
	document.forms[fid].amount_to.value = amount;
	in_out_change(fid, id, code, bForce);
	return;
	}
	
 
	
  if (course == 0)	return;
    
  document.forms[fid].amount_from.value = Math.round((out_val / course) * 100) / 100;
  ExternalSetPaymentSystemsFee ();
	

  if (document.forms[fid].amount_from.value > max_amount) {
		//in_out(fid, id, code, bForce);
		
		document.forms[fid].amount_from.value = max_amount;
		document.forms[fid].amount_to.value = Math.round( max_amount * course * 100) / 100;
		alert('The maximum amount of exchange ' + max_amount);
		return;
	}

  if (document.forms[fid].amount_from.value < min_amount) {
		//in_out(fid, id, code, bForce);
		
		document.forms[fid].amount_from.value = min_amount;
		document.forms[fid].amount_to.value = Math.round( min_amount * course * 100) / 100;
	/*	alert('The minimum amount of exchange ' + min_amount); */
		return;
	}
	
  //if (bForce) { in_out_change(fid, code); }

}

/*
	From In to Out OnKeyUp handler
*/
function in_out_keyup(fid, id, code, bForce) {

//alert ('in_out_keyup');

if (document.forms[fid].amount_from == null) return;
  
  in_val = document.forms[fid].amount_from.value.replace(",", ".");
  if (isNaN(in_val)) { in_val = "0"; }
	
  amount = Math.round(in_val * 100) / 100;
	
	// added 23.05.07 - change amount_from according to precision
	if ((in_val != '') && (parseFloat(in_val) != amount))
		document.forms[fid].amount_from.value = amount;

  if (isNaN(document.forms[fid].amount_from.value)) {
		amount = min_amount;
		document.forms[fid].amount_from.value = amount;
		out_in_keyup(fid, id, code, bForce);
		return;
	}	

	
  if (document.forms[fid].amount_from.value < 0) {
	amount = -amount;
	document.forms[fid].amount_from.value = amount;
	out_in_keyup(fid, id, code, bForce);
	return;
	}
	
  document.forms[fid].amount_to.value = Math.round(amount * course * 100) / 100;
  
  ExternalSetPaymentSystemsFee ();
	
  //if (bForce) { out_in_keyup(fid, code); }

}

/*
	From Out to In OnKeyUp handler
*/
function out_in_keyup(fid, id, code, bForce) {
//alert ('out_in_keyup');
  if (document.forms[fid].amount_to == null) return;
 // if (id == null) return;
	
  out_val = document.forms[fid].amount_to.value.replace(",", ".");
  if (isNaN(out_val)) { out_val = "0"; }

  amount = Math.round(out_val * 100) / 100;
	
	// added 23.05.07 - change amount_out according to precision
  if ((out_val != '') && (parseFloat(out_val) != amount))
		document.forms[fid].amount_to.value = amount;
		
  if (isNaN(document.forms[fid].amount_to.value)) {
	amount = 0;
	document.forms[fid].amount_to.value = amount;
	in_out_keyup(fid, id, code, bForce);
	return;
	}
  
  if (course == 0)	return;
    
  document.forms[fid].amount_from.value = Math.round((out_val / course) * 100) / 100;
  
  ExternalSetPaymentSystemsFee ();
  
  if (document.forms[fid].amount_from.value > max_amount) {
		//in_out(fid, id, code, bForce);
		document.forms[fid].amount_from.value = max_amount;
		document.forms[fid].amount_to.value = Math.round( max_amount * course * 100) / 100;
		alert('The maximum amount of exchange ' + max_amount);
		
		ExternalSetPaymentSystemsFee ();
		return;
	}
	
  //if (bForce) { in_out_keyup(fid, code); }

}	


function SetCourses () {

		
	if (course / course_back >= 0.5) {
		document.getElementById('curr_from').value = 1;
		document.getElementById('curr_from').value = course;
		}
	else {
		document.getElementById('curr_from').value = course_back;
		document.getElementById('curr_from').value = 1;
		}	

	}

