var array = new Array();
function fill_form(a_array, b_array, doReset){	
	var frm=document.getElementById("basket");
	var fields = new Array('address_1', 'city', 'state', 'phone');
				
	if(document.getElementById('same').checked == true){
		document.getElementById('adresa_livrare').style.display="none";
		
		for(i=0; i<fields.length; i++)
		{
		    srcElem = frm['b_' + fields[i]];	
		    destElem = frm['s_' + fields[i]];
		    if (srcElem && destElem) destElem.value = srcElem.value;
		}		  		
	}else{
		document.getElementById('adresa_livrare').style.display="block";
	    if (doReset == 1)
	    {
		for(i=0; i<fields.length; i++)
		{
		    srcElem = frm['b_' + fields[i]];	
		    destElem = frm['s_' + fields[i]];
		    if (srcElem && destElem) destElem.value = ''; 
		}		  		
	  }
	}
}
function change_s(val, field, contor, a_array, b_array){
	if(document.getElementById('same').checked == false){
		if((a_array[contor] == 1)&&(b_array[contor] == 1)){
			document.getElementById(field).value = val;
		}
	}
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function number_format(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function calculate_total(value1, value2, mii, zecimi){
	var a_value = value1.split("_");
	var a = number_format(parseFloat(a_value[1]) + parseFloat(value2), 2).split(".");
	var contor = 0;
	var a_intors = new Array();
	if(a[0].length > 3){
		for(i=(a[0].length-1);i>=0;i--){
			if(contor == 3){
				a_intors = a_intors + mii ;
				contor = 0;
			}
			a_intors = a_intors + a[0].charAt(i);
			contor++;
		}
		var a_bun = "";
		for(i=(a_intors.length-1);i>=0;i--){
			a_bun = a_bun + a_intors.charAt(i);	
		}
		a[0] = a_bun;
	}
	var total_new = a[0] + zecimi +a[1];
	document.getElementById('total_order_value').style.width = total_new.length*6 + 'px';
	document.getElementById('total_order_value').value = a[0] + zecimi +a[1];
	
	var a = number_format(a_value[3], 2).split(".");
	var contor = 0;
	var a_intors = new Array();
	if(a[0].length > 3){
		for(i=(a[0].length-1);i>=0;i--){
			if(contor == 3){
				a_intors = a_intors + mii ;
				contor = 0;
			}
			a_intors = a_intors + a[0].charAt(i);
			contor++;
		}
		var a_bun = "";
		for(i=(a_intors.length-1);i>=0;i--){
			a_bun = a_bun + a_intors.charAt(i);	
		}
		a[0] = a_bun;
	}
	
	document.getElementById('shipping_vat_value').value = a[0] + zecimi +a[1];
}

showRow = (navigator.appName.indexOf("Internet Explorer") != -1) ? "block" :"table-row";
function hide_tr_(val, val_total){
	for(i = 1; i < val_total; i++){
		if(val == i){
			window.document.getElementById("tr" + val).style.display = showRow;
		}else{
			window.document.getElementById("tr" + i).style.display = "none";
		}
	}
}

//showRow = (navigator.appName.indexOf("Internet Explorer") != -1) ? "block" :"table-row";
function hide_tr(){
	if(document.getElementById('same').checked == true){
		window.document.getElementById("tr1").style.display = "none";
		window.document.getElementById("tr2").style.display = "none";
		window.document.getElementById("tr3").style.display = "none";
		window.document.getElementById("tr4").style.display = "none";
		window.document.getElementById("tr5").style.display = "none";
		window.document.getElementById("tr6").style.display = "none";
		window.document.getElementById("tr7").style.display = "none";
		window.document.getElementById("tr8").style.display = "none";
		window.document.getElementById("tr9").style.display = "none";
		window.document.getElementById("tr10").style.display = "none";
		window.document.getElementById("tr11").style.display = "none";
		window.document.getElementById("tr12").style.display = "none";
		//window.document.getElementById("tr13").style.display = "none";
	}else{
		window.document.getElementById("tr1").style.display = showRow;
		window.document.getElementById("tr2").style.display = showRow;
		window.document.getElementById("tr3").style.display = showRow;
		window.document.getElementById("tr4").style.display = showRow;
		window.document.getElementById("tr5").style.display = showRow;
		window.document.getElementById("tr6").style.display = showRow;
		window.document.getElementById("tr7").style.display = showRow;
		window.document.getElementById("tr8").style.display = showRow;
		window.document.getElementById("tr9").style.display = showRow;
		window.document.getElementById("tr10").style.display = showRow;
		window.document.getElementById("tr11").style.display = showRow;
		window.document.getElementById("tr12").style.display = showRow;
		//window.document.getElementById("tr13").style.display = showRow;
	}
}

function display_tr(){
	if(document.getElementById('gift').checked == true){
		document.getElementById('tr_gift').style.display = showRow;
	}else{
		document.getElementById('tr_gift').style.display = "none";
	}
}

//dummy function
function nd(){
}
