// array_prices
// array_lettes

max_limit = 80; // maximum characters limit

// FORMATNUM: 	functie da formatare a numerelor intregi
// Parametrii: 	expr - numarul care urmeaza a fi formatat
// 				decplaces - numarul de zerouri care vor fi adaugate numarului intreg
// Returneaza:	noul numar formatat
// Uz:			ptr formatarea unui numar
// ------------------------------------------------------------------------------------------------------
function formatnum(expr, decplaces) {
	var str = (Math.round(parseFloat(expr) * Math.pow(10,decplaces))).toString()
	while (str.length <= decplaces) {
		str = "0" + str
	}
	var decpoint = str.length - decplaces
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length)
}
// ------------------------------------------------------------------------------------------------------


// str_replace:	functie gen str_replace dion PHP - inlocuieste un substring cu un altul dintr-un text
// Parametrii: 	
// Returneaza:	TRUE - in caz de succes, FLASE
// Uz:			uneori
// ------------------------------------------------------------------------------------------------------
function str_replace(search, replace, subject) {
	var split_string = subject.split(search);
	var new_string = '';
	
	for(var i = 0; i < split_string.length; i++) {
		new_string += split_string[i] + replace;
	}
	
	return new_string.substr(0, (new_string.length - replace.length));
}
// ------------------------------------------------------------------------------------------------------


// functie de determinare a grupului in care se situeaza numarul de caractere :
// ------------------------------------------------------------------------------------------------------
function verify_margins(nr_chars) {
	var split;
	
	for(i = 0; i < array_letters.length; i++) {
		split = array_letters[i].split("-");
		
		if(nr_chars >= split[0] && nr_chars <= split[1]) {
			return i;
		}
	}
}
// ------------------------------------------------------------------------------------------------------


// count spaces from a string :
// ------------------------------------------------------------------------------------------------------
function count_spaces(string) {
	var nrSpaces = 0;
	
	for(i = 0; i < string.length; i++) {
		if(string.charAt(i) == ' ') {
			nrSpaces++;
		}
	}
	
	return nrSpaces;
}
// ------------------------------------------------------------------------------------------------------


// verify if ctrl + v was pressed :
// ------------------------------------------------------------------------------------------------------
var ctr_v_pressed = '';
var nn = (document.layers) ? true : false;
var ie = (document.all) ? true : false;
if(nn) document.captureEvents(Event.KEYUP);
document.onkeydown = onKeyUp;

function onKeyUp(e) {
	var evt=(e)?e:(window.event)?window.event:null;
	
	// current pressed key
	var pressedKey = String.fromCharCode(evt.keyCode).toLowerCase();

	if (evt.ctrlKey && pressedKey == "v") {
		ctr_v_pressed = 'yes';
	}
}
// ------------------------------------------------------------------------------------------------------


// calculate_engraving - calculeaza pretul si numarul de caractere :
// Parametrii: 		form_name - numele formularului ce contine campul din care se numara caracterele
// 					field_text - numele campului din care se numara caracterele
//					field_material - tipul de material al produsului
//					div_price, div_chars, div_vat, div_total - div-urile caracteristice calculului
// Returneaza:		un alert cau modifica un div
// Uz:				-
// ------------------------------------------------------------------------------------------------------
function calculate_engraving(form_name, field_text, field_material, div_price, div_chars, div_vat, div_total) {
	var pound = "&pound;";
	var nr_chars, text_without_spaces, group_letters;
	var price = 0;
	var vat = 0;
	var price_total = 0;
	var nr_spaces = 0;
	
	eval("var form = document."+form_name);
	// verify the maximum limit :
	if(eval("str_replace(' ', '', form."+field_text+".value).length") > max_limit) {
		if(ctr_v_pressed == "yes") {
			nr_spaces = eval("count_spaces(form."+field_text+".value.substr(0, "+max_limit+"))");
			ctr_v_pressed = '';
		} else {
			nr_spaces = eval("count_spaces(form."+field_text+".value)");
		}
		
		eval("form."+field_text+".value = form."+field_text+".value.substr(0, "+(parseInt(max_limit) + parseInt(nr_spaces))+");");
		alert('The maximum characters limit is '+max_limit+'!');
	}
	
	eval("var form_text = form."+field_text);
	eval("var form_material = form."+field_material);
	
	text_without_spaces = str_replace(' ', '', form_text.value);
	nr_chars = text_without_spaces.length;
	
	if(form_material.value != 0) {
		group_letters = verify_margins(nr_chars);
		
		if(group_letters < 8) {
			price = parseFloat(array_prices[group_letters][form_material.value - 1]);
		} else {
			if(group_letters) {
				price = parseFloat(array_prices[group_letters - 1][form_material.value - 1]) + parseFloat(array_prices[group_letters][form_material.value - 1]) * (nr_chars - 80);
			}
		}
		
		//vat = vat_value*parseFloat(price)/100;
		vat = 0;
		price_total = parseFloat(price) + vat;
	}

	document.getElementById(div_price).innerHTML = pound+""+formatnum(price, 2);
	document.getElementById(div_chars).innerHTML = nr_chars;
	//document.getElementById(div_vat).innerHTML = pound+""+formatnum(vat, 2);
	document.getElementById(div_total).innerHTML = pound+""+formatnum(price_total, 2);
}
// ------------------------------------------------------------------------------------------------------


// calculate_total - calulate the total prices for order :
function calculate_total(div_id, form_name, quantity) {
	// prod_total_{div_id}
	// total_order, total_vat, total_checkout
	var total_prod_price = 0;
	
	// calculate total product price :
	var price_engrave = parseFloat(document.getElementById('divPriceTotal_t_'+div_id).innerHTML.substr(1)) + parseFloat(document.getElementById('divPriceTotal_s_'+div_id).innerHTML.substr(1));
	total_prod_price = parseFloat(str_replace(',', '.', array_order_prices[div_id])) + parseFloat(price_engrave)*parseInt(quantity);
	document.getElementById('prod_total_'+div_id).innerHTML = formatnum(total_prod_price, 2);
	
	// calculate total_order price :
	calculate_total_products(div_id, form_name, quantity);
}

// calculate_total_products - calculate the total products price :
function calculate_total_products(div_id, form_name, quantity) {
	var total = vat = price = 0;
	
	eval("var form = document."+form_name);
	
	// calculate total checkout price :
	for(var i = 0; i < nr_products; i++) {
		total += parseFloat(document.getElementById('prod_total_'+i).innerHTML);
	}
	
	// calculate total vat price :
	for(var i = 0; i < array_engrave_prods.length; i++) {
		if(array_engrave_prods[i] != 0) {
			//vat += (parseFloat(document.getElementById('divVat_t_'+i).innerHTML.substr(1)) + parseFloat(document.getElementById('divVat_s_'+i).innerHTML.substr(1)));
			vat += 0;
			price += (parseFloat(document.getElementById('divPrice_t_'+i).innerHTML.substr(1)) + parseFloat(document.getElementById('divPrice_s_'+i).innerHTML.substr(1)));
		}
	}
	total_vat = 0;
	vat += parseFloat(total_vat);
	price += parseFloat(total_order);
	
	document.getElementById('total_order').innerHTML = formatnum(price, 2);
	document.getElementById('total_checkout').innerHTML = formatnum(total, 2);
	document.getElementById('total_vat').innerHTML = formatnum(vat, 2);
}