var usd                    = false;
var divied                 = false;
var issue_fee              = 0;
var charge_amount          = 0;
var commission_fee         = 0;
var foreign_commission_fee = 0;
var currency_value         = 0;
var transfer_fee           = 0;

//Converts Plain number to Realdable Number
function convertMoney(money) {
	
    money = money + ''; 
    money = money.split(".");

    if (money[0] && money[1]) {
        temp = money[1] + '';
        return convertMoney(money[0])+"."+temp.charAt(0)+temp.charAt(1);
    } else {

        money = money + ''; 
        money = money.replace(/\D+/g, '');    
	
        var money_result = '';
        var i = money.length - 1;
        var j = 1;
        for(; i >= 0; i--, j++){
            money_result = money.charAt(i) + '' + money_result;
            if(j == 3 && money.charAt(i-1)){
                money_result = ',' + money_result;
                j = 0;
            }
        }
        return money_result;
    }
}
//Convert PLain PIN to dashed PIN
function dashPAN(pin) {
	pin = pin + '';
	pin = pin.replace(/\D+/g, '');
	var pin_result = '';
	var len = pin.length;
	var i = 0;
	var j = 1;
	for(; i < len && i < 16; i++, j++){
		pin_result = pin_result + '' + pin.charAt(i);
		if(j == 4 && pin.charAt(i+1) && i < 15){
			pin_result = pin_result + '-';
			j = 0;
		}
	}
	return pin_result;
}
//Convert back number to plain
function convert_back(input){
	input2 = document.getElementById(input).value;
	input2 = input2.replace(/,/g,'');
	document.getElementById(input).value = input2;
}
//Convert to readable and add Rial
function add_rial(y){
	y = convertMoney(y);
	y = y + ' ریال';
	return y;
}

function add_currency(y){
	y = convertMoney(y);
	y = (usd) ? (y + ' USD') : (y + ' ریال');
	return y;
}
//Used in product page
function update_price(from, to){
	var str = get_node('product_price');
  	str = str.replace(/USD/, '');
	str = str.replace(/ریال/, '');
	str = str.replace(/,/g, '');
	ca = from.value;
	ca = ca.replace(/,/g, '');
	total = (str * 1) + (ca * 1);
	total = add_currency(total)	
	update_node(to, total);
}
//Toggle element visibility
function toggle_display(object) {
	object = document.getElementById(object).style;
	object2 = object.display;
	if (object2 == "block") {
		object.display = "none";
	} else {
		object.display = "block";
	}
}
//changes some thing on load
function direct_change(object) 
{
    if (eval("typeof("+object.replace(/-/g, '_')+") == 'undefined'")) {
        value = get_numberic(get_node(object));         
    } else {
        eval("value = "+object.replace(/-/g, '_')+";");
    }
    
    if (usd) {
        value  = value / currency_value;
    }

	update_node(object, add_currency(value));
}
//Used on order: calculates the region price and add it to total !
function calculate(region) 
{
    transfer_fee = value2price(region) * 1; // convert it to float/int with "* 1"
  	total        = get_total();
    region_price = (usd) ? value2price(region) / currency_value : value2price(region);

    update_node('transfer-fee', add_currency(region_price));
	update_node('total-price', add_currency(total));
}
function change_foreign_currency() 
{ 
    usd                = true; 
    var fcfl           = document.getElementById('foreign-commission-fee-cell');    
    fcfl.style.display = 'table-row';
    update_node('commission-fee', add_currency(0));

    var cv           = document.getElementById('currency-value-cell');    
    cv.style.display = 'table-row';

    var ff           = document.getElementById('for-foreign');    
    ff.style.display = 'block';        

    update_node('total-price',add_currency(get_total()));    
}
function change_mellat_currency(xcommission_fee) 
{ 
    usd                = false; 
    var fcfl           = document.getElementById('foreign-commission-fee-cell');    
    fcfl.style.display = 'none';
    update_node('commission-fee', add_currency(xcommission_fee));    

    var cv           = document.getElementById('currency-value-cell');    
    cv.style.display = 'none';

    var ff           = document.getElementById('for-foreign');    
    ff.style.display = 'none';    

    update_node('total-price',add_currency(get_total()));    
}
function change_saman_currency(xcommission_fee) 
{ 
    usd                = false; 
    var fcfl           = document.getElementById('foreign-commission-fee-cell');    
    fcfl.style.display = 'none';
    update_node('commission-fee', add_currency(xcommission_fee));    

    var cv           = document.getElementById('currency-value-cell');    
    cv.style.display = 'none';

    var ff           = document.getElementById('for-foreign');    
    ff.style.display = 'none';        

    update_node('total-price',add_currency(get_total()));    
}
//Used on order page
function get_total()
{	
    total = issue_fee + charge_amount + commission_fee + transfer_fee;

    if (usd) {
        total = total - commission_fee;
        total = total / currency_value;        
        // foreign_commission_fee in in USD
        total = total + foreign_commission_fee;
    }

	return total;
}
//Common Functions
function update_node(node,value){
	document.getElementById(node).firstChild.nodeValue = value;
}
function get_node(node) {
	node = document.getElementById(node).firstChild.nodeValue;
	return node;
}
function get_numberic(x) {
	x = x.replace(/ریال/, '');
	x = x.replace(/USD/, '');    
	x = x.replace(/,/g, '');
	x = x*1;
	return x;
}
// Clear hidden inputs
function clearHidden(form){
	var frm = document.getElementById(form);
	inputs = frm.getElementsByTagName('input');
	for (var i=0; i < inputs.length; i++){
		if(inputs[i].type == "hidden"){
			inputs[i].value.replace(/USD/, '');        
			inputs[i].value.replace(/ریال/, '');
			inputs[i].value.replace(/,/g, '');
		}
	}
}
// Update deliver man name
function updateDeliver(object,node){
	y = object.options[object.value].firstChild.nodeValue;
	update_node(node,y);	
}
// my always runnig counter !
var counter = 0;
var count = -1;
function timeCounter(){
	count++;
}
// Check Receipt Status
function check_amount() {
	if(count >= counter) {
		counter = 0;
		count = -1;
		var amount = get_numberic(document.getElementById('amount').value);
		var total = get_numberic(get_node('total-price'));
		var msgDiv = 'receipt_message';
		if(!amount){
			document.getElementById(msgDiv).style.display = 'none';
		}else{
			document.getElementById(msgDiv).style.display = 'block';
			document.getElementById('submit').removeAttribute('disabled', 'disabled');
		}
		if(amount < limit) {
			update_node(msgDiv,msg[0]);
			document.getElementById(msgDiv).style.border = '1px solid #EF4A4A';
			document.getElementById(msgDiv).style.background = '#FFAFAF';
			document.getElementById('submit').setAttribute('disabled', 'disabled');
		} else if(amount == total) {
			update_node(msgDiv,msg[1]);
			document.getElementById(msgDiv).style.border = '1px solid #9CEF4A';
			document.getElementById(msgDiv).style.background = '#DFFFBF';
		} else if(amount < total) {
			update_node(msgDiv,msg[2]);
			document.getElementById(msgDiv).style.border = '1px solid #FFBF00';
			document.getElementById(msgDiv).style.background = '#FFE79F';
		} else {
			update_node(msgDiv,msg[3]);
			document.getElementById(msgDiv).style.border = '1px solid #FFBF00';
			document.getElementById(msgDiv).style.background = '#FFE79F';
		}
	}else{
		if(count >= 0) count = counter;
	}
}
// Sanjesh Calculate
function check_total() {
	var sum = 0;
	for ( var i = 1; i < fee1.length; i++ ) {
		if (document.getElementById(fee1[i]).checked) {
			sum = sum + get_numberic(fee1[0]);
		}
	}
	
	for ( var i = 1; i < fee2.length; i++ ) {
		if (document.getElementById(fee2[i]).checked) {
			sum = sum + get_numberic(fee2[0]);
			break;
		}
	}

	total = add_currency(convertMoney(sum));
	update_node('total_fee', total);
}

function initialized_amounts(init_issue_fee, init_charge_amount, init_commission, init_foreign_commission, init_currency_value)
{
    issue_fee              = init_issue_fee;
    charge_amount          = init_charge_amount;
    commission_fee         = init_commission;
    foreign_commission_fee = init_foreign_commission;
    currency_value         = init_currency_value;
}

