
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false;}
else {return true}
}
}//function validate_required(field,alerttxt)

function validate_number(sText,alerttxt)
{
   var ValidChars = "0123456789.";
   //var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
		 //IsNumber = false;
		 alert(alerttxt);
         return false;
         }
      }
   return true;
 }

function validate_bankform(thisform)
{
	with (thisform)
	{
	if (validate_required(amount,"Please enter Amount deposited")==false)
	  {amount.focus();return false;}
	  
	if (validate_number(amount.value,"Please enter numbers(0123456789) only\n without comma(,) or naira(N)sign")==false)
	  {amount.focus();return false;}
	  
	 if (validate_required(depositDate,"Please Select a date")==false)
	  {depositDate.focus();return false;} 
	if (validate_required(depositor,"Enter the name of the depositor")==false)
	  {depositor.focus();return false;} 
	
		
		
	}//with (thisform)
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function get_buy_rate(qty,rate1,rate2,rate3){
	//$type is either BUY or SELL
	 rate = 0;
	
		if (qty < 1000){
		 //rate = 185;
		 rate = rate1;
		}
		else if(qty < 3000){
		 //rate = 183;
		 rate = rate2;
		}
		else if(qty > 2999){
		//rate = 181;
		rate = rate3;
		}
	
	return rate;
}

function buyBulkLR(chargePercent,fixedAmount){
	var dollarAmount = trim(document.frmBuyLR.dollarAmount.value); 
	dollarAmount = parseFloat(dollarAmount);
	if (dollarAmount > 0){
	percent = chargePercent * 0.01 * dollarAmount;
	totalFee =  percent + dollarAmount + fixedAmount;
	totalFee = Math.round(totalFee*Math.pow(10,2))/Math.pow(10,2);//Math.round(rnum*Math.pow(10,rlength))/Math.pow(10
	document.getElementById("bulkPay").innerHTML= 'You  pay <b>$'+addCommas(dollarAmount)+'</b> plus <b>'+chargePercent+'%</b> plus fixed fee <b>$'+fixedAmount+'</b>.Total is <b>$'+addCommas(totalFee)+'</b>';
 }//if (dollarAmount > 0)
 else{
 	document.getElementById("bulkPay").innerHTML='Please Enter Amount of Liberty Reserve Needed';
 }
}//function

function nairaBulkLR(rate){
	//var fee = 0.0 ;
		var dollarAmount = trim(document.frmBulkLR.dollarAmount.value); 
		var temp = 0;
		var total = 0;
		
		if (dollarAmount > 0){
			
			//rate = get_buy_rate(dollarAmount,rate1,rate2,rate3);
			temp = dollarAmount * rate;
			temp = Math.round(temp*Math.pow(10,2))/Math.pow(10,2);//Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength)
			// LR fee
			fee = dollarAmount * 0.01;
			if (fee > 2.99){
				fee = 2.99;
			}
			else if(fee < 0.01) {
				fee = 0.01;
			}
			total = dollarAmount - fee;
			
			//total = Math.round(total*Math.pow(10,2))/Math.pow(10,2);
			//document.LRpayment.lr_amnt.value = total;
			//document.getElementById("nairaAmount").innerHTML='Naira Value is: <b>'+addCommas(temp)+'</b>';
			document.getElementById("nairaAmount").innerHTML= 'Naira Value for <b>$'+dollarAmount+'</b> is <b>N'+addCommas(temp)+'</b>. Total of <b>$'+addCommas(total)+'</b> to your Liberty Reserve account. Calculation includes Liberty Reserve fee of 1% but not more than $2.99';
 }//if (dollarAmount > 0)
 else{
 	document.getElementById("nairaAmount").innerHTML='Please Enter Amount of Liberty Reserve Needed';
 }
}//function

function nairaAmount(rate1,rate2,rate3)
	{
		//var fee = 0.0 ;
		var dollarAmount = trim(document.frmBuyLR.dollarAmount.value); 
		var temp = 0;
		var total = 0;
		
		if (dollarAmount > 0){
			
			rate = get_buy_rate(dollarAmount,rate1,rate2,rate3);
			temp = dollarAmount * rate;
			temp = Math.round(temp*Math.pow(10,2))/Math.pow(10,2);//Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength)
			// LR fee
			fee = dollarAmount * 0.01;
			if (fee > 2.99){
				fee = 2.99;
			}
			else if(fee < 0.01) {
				fee = 0.01;
			}
			total = dollarAmount - fee;
			
			//total = Math.round(total*Math.pow(10,2))/Math.pow(10,2);
			//document.LRpayment.lr_amnt.value = total;
			//document.getElementById("nairaAmount").innerHTML='Naira Value is: <b>'+addCommas(temp)+'</b>';
			document.getElementById("nairaAmount").innerHTML= 'Naira Value for <b>$'+dollarAmount+'</b> is <b>N'+addCommas(temp)+'</b>. Total of <b>$'+addCommas(total)+'</b> to your Liberty Reserve account. Calculation includes Liberty Reserve fee of 1% but not more than $2.99';
 }//if (dollarAmount > 0)
 else{
 	document.getElementById("nairaAmount").innerHTML='Please Enter Amount of Liberty Reserve Needed';
 }
}//function 

function sellNairaAmount(rate)
	{
		//var fee = 0.0 ;
		var dollarAmount = trim(document.frmSellLR.dollarAmount.value); 
		var total = 0;
		
		if (dollarAmount > 0){
			total = dollarAmount * rate;
			total = Math.round(total*Math.pow(10,2))/Math.pow(10,2);//Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength)
			document.getElementById("sellNairaAmount").innerHTML= 'You will recieve a total of <b>N'+addCommas(total)+'</b> to your specified bank account';
 }//if (dollarAmount > 0)
 else{
 	document.getElementById("sellNairaAmount").innerHTML='Please Enter Amount of Liberty Reserve to Sell';
 }
}//function

function countR(txtarea){
	var txtRef = trim(txtarea.value,',');
	var temp_array = new Array();
	
	if (txtRef != ""){
		temp_array = txtRef.split(",");
		document.getElementById("recp").innerHTML=temp_array.length;
	}
	
}
function trim (str, charlist) {
    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}
