var code_type;

function formatCurrency(num) {
num = num.toString().replace(/,/,'');
if(isNaN(num))
num = "0.00";
if(num.indexOf('-')!=-1)
var minus='-';
else
var minus='';
num=Math.abs(num);
num = Math.floor(num*100+0.50000000001);
thous =num%100;
num = Math.floor(num/100).toString();
if(thous<10)
thous = "0" + thous;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (minus+num + '.' + thous);
}

function checkShippingCosts(id, subtotal_id) {
var selectField = $(id);
var selectedOption = selectField.options[selectField.selectedIndex].value;
var subtotal = $(subtotal_id).innerHTML.toString().replace(/,/,'');
subtotal = subtotal*1;
var url = '/includes/shipping_costs.php';
var params = 'country=' + selectedOption + '&subtotal=' + subtotal;
var ajax = new Ajax.Updater($('shp_costs'),
url,
{method: 'post', parameters: params, onComplete:function() {calculateTotal()}});
}

function checkShippingCosts3(id, subtotal_id) {
var selectField = $(id);
var selectedOption = selectField.options[selectField.selectedIndex].value;
var subtotal = $(subtotal_id).innerHTML.toString().replace(/,/,'');
subtotal = subtotal*1;
var url = 'https://secure92.nocdirect.com/~thewoode/includes/shipping_costs.php';
var params = 'country=' + selectedOption + '&subtotal=' + subtotal;
var ajax = new Ajax.Updater($('shpp_costs'),
url,
{method: 'post', parameters: params, onComplete:function() {finalize3()}});
}

function finalize3() {
if($('shpp_costs').innerHTML * 1 == 0) { 
$('shpp_cntr_txt').innerHTML = '<b>Please contact us regarding shipping to your country</b>';
$('place_order').disabled = true;
} 
else {
$('shpp_cntr_txt').innerHTML = '<b>Having selected your delivery country here, shipping costs will be calculated above.</b>'
$('place_order').disabled = false;
}
$('shpp_cntr_txt').style.display='block';
var total_p = $('sub_total').innerHTML * 1 + $('shpp_costs').innerHTML * 1;

$('total').innerHTML = formatCurrency( total_p );
$('total_price_val').value = total_p;
$('shpp_costs_val').value = $('shpp_costs').innerHTML * 1;
$('sub_total_val').value = $('sub_total').innerHTML * 1;
Total2();
}

function calculateTotal() {
RibbonsPrice = AccessoryPrice('ribbon_price', 'ribbons_qty');
$('total_ribbon_price').innerHTML = formatCurrency(RibbonsPrice);  
var total_price = 0;
var item_price = $$('span.item_price');
var item_price_ln = item_price.length;

	for(var i=0; i<item_price_ln; i++) {
	total_price+=item_price[i].innerHTML*1;
	}
var subtotal = RibbonsPrice*1 + total_price*1;

$('sub_total').innerHTML = formatCurrency(subtotal);

$('subtotal').value = subtotal;
$('totalamount').value = subtotal;

}

function AccessoryPrice(id, id1) {
var AccessoryPrice = $(id).innerHTML*1;
if( isNaN(AccessoryPrice) )
AccessoryPrice=0;
var AccessoryQty = $(id1).value*1;
if( isNaN(AccessoryQty) )
AccessoryQty=0;
return (AccessoryPrice*AccessoryQty);
}

function copyDetails(id) {
var chkbox = $(id);
	if(chkbox.checked == true) {
	$('shipp_name').value = $('name').value;
	$('address4').value = $('address').value;
	$('address5').value = $('address2').value;
	$('address6').value = $('address3').value;
	$('postcode2').value = $('postcode').value;
	$('county2').value = $('county').value;
	var country = $('country').options[$('country').selectedIndex].value; 
	var shipp_c = $('shipping_country');
	
	for(var i=0; i<shipp_c.options.length; i++) {
		if(shipp_c.options[i].value == country) {
		shipp_c.options[i].selected = true;
		}
		else {
		shipp_c.options[i].selected = false;
		}	
	}
	checkShippingCosts3('shipping_country', 'sub_total');
	}
}

function checkCheckout() {
var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var name = $('name');
	if( name.value == '' ) {
	alert("Please fill in your name");
	name.focus();
	return false;
	}
var email = $('email');
	if( !filter.test(email.value) ) {
	alert("Your email seems to be invalid");
	email.focus();
	return false;	
	}
var telephone = $('telephone');
	if( telephone.value == '' ) {
	alert("Please fill in your telephone number");
	telephone.focus();
	return false;	
	}
var address = $('address');
	if( address.value == '' ) {
	alert("Please fill in address details");
	address.focus();
	return false;
	}
var town = $('address3');
	if( town.value == '' ) {
	alert("Please fill in town");
	town.focus();
	return false;
	}
var postcode = $('postcode');
	if( postcode.value == '' ) {
	alert("Please fill in post code");
	postcode.focus();
	return false;
	}
var shipp_name = $('shipp_name');
	if( shipp_name.value == '' ) {
	alert("Please fill in shipping name");
	shipp_name.focus();
	return false;
	}
var shipp_address = $('address4');
	if( shipp_address.value == '' ) {
	alert("Please fill in shipping address details");
	shipp_address.focus();
	return false;
	}
var town2 = $('address6');
	if( town2.value == '' ) {
	alert("Please fill in shipping town");
	town2.focus();
	return false;
	}
var postcode2 = $('postcode2');
	if( postcode2.value == '' ) {
	alert("Please fill in shipping post code");
	postcode2.focus();
	return false;
	}
}

function AddMore() {

var section = $('order_section');
var section_number = $$('select.letters_select').length;


var tex = '<div class="section_cell">';
tex +=    '<select size="1" id="letter_'+section_number+'" name="letter_'+section_number+'" style="font-weight: 700" class="letters_select">';
tex +=    '<option selected>Letter/number</option>';
tex +=    '<option>a</option>';
tex +=    '<option>b</option>';
tex +=    '<option>c</option>';
tex +=    '<option>d</option>';
tex +=    '<option>e</option>';
tex +=    '<option>f</option>';
tex +=    '<option>g</option>';
tex +=    '<option>h</option>';
tex +=    '<option>i</option>';
tex +=    '<option>j</option>';
tex +=    '<option>k</option>';
tex +=    '<option>l</option>';
tex +=    '<option>m</option>';
tex +=    '<option>n</option>';
tex +=    '<option>o</option>';
tex +=    '<option>p</option>';
tex +=    '<option>q</option>';
tex +=    '<option>r</option>';
tex +=    '<option>s</option>';
tex +=    '<option>t</option>';
tex +=    '<option>u</option>';
tex +=    '<option>v</option>';
tex +=    '<option>w</option>';
tex +=    '<option>x</option>';
tex +=    '<option>y</option>';
tex +=    '<option>z</option>';
tex +=    '<option>0</option>';
tex +=    '<option>1</option>';
tex +=    '<option>2</option>';
tex +=    '<option>3</option>';
tex +=    '<option>4</option>';
tex +=    '<option>5</option>';
tex +=    '<option>6</option>';
tex +=    '<option>7</option>';
tex +=    '<option>8</option>';
tex +=    '<option>9</option>';
tex +=    '<option>&amp;</option>';
tex +=    '</select> ';
tex +=    '<select size="1" name="case_'+section_number+'" id="case_'+section_number+'" style="font-weight: 700">';
tex +=    '<option selected>Capital - NO</option>';
tex +=    '<option>Capital - YES</option>';
tex +=    '</select><select size="1" name="font_'+section_number+'" id="font_'+section_number+'" style="font-weight: 700">';
tex +=    '<option selected>Font</option>';
tex +=    '<option>Modern</option>';
tex +=    '<option>Classic</option>';
tex +=    '</select> ';
tex +=    '<select size="1" name="size_'+section_number+'" id="size_'+section_number+'" style="font-weight: 700" onChange="SetPrice(\'size_'+section_number+'\', \'span_item_price_'+section_number+'\');">';
tex +=    '<option selected>Size</option>';
tex +=    '<option>5 inches</option>';
tex +=    '<option>8 inches</option>';
tex +=    '</select> ';
tex +=    '<select size="1" name="colour_'+section_number+'" id="colour_'+section_number+'" style="font-weight: 700; width:120px;">';
tex +=    '<option selected>Colour</option>';
tex +=    '<option>Lemon</option>';
tex +=    '<option>Yellow</option>';
tex +=    '<option>Brightorange</option>';
tex +=    '<option>Burntorange</option>';
tex +=    '<option>Cream</option>';
tex +=    '<option>Coffee</option>';
tex +=    '<option>Chocolate</option>';
tex +=    '<option>Metallicgold</option>';
tex +=    '<option>Paleblue</option>';
tex +=    '<option>Warmblue</option>';
tex +=    '<option>Royalblue</option>';
tex +=    '<option>Navyblue</option>';
tex +=    '<option>Turquoise</option>';
tex +=    '<option>Teal</option>';
tex +=    '<option>Metallicsilver</option>';
tex +=    '<option>Softsagegreen</option>';
tex +=    '<option>Springgreen</option>';
tex +=    '<option>Sagegreen</option>';
tex +=    '<option>Palepink</option>';
tex +=    '<option>Candypink</option>';
tex +=    '<option>Cerisepink</option>';
tex +=    '<option>Softlilac</option>';
tex +=    '<option>Plum</option>';
tex +=    '<option>Purple</option>';
tex +=    '<option>Raspberry</option>';
tex +=    '<option>Cranberry</option>';
tex +=    '<option>Red</option>';
tex +=    '<option>White</option>';
tex +=    '<option>Black</option>';
tex +=    '</select></div>';
tex +=    '<div class="price_cell">';
tex +=    '<b>&pound;<span id="span_item_price_'+section_number+'">0.00</span></b></div>';
tex +=    '<div class="qty_cell">';
tex +=    '<input name="item_price_'+section_number+'" size="4" id="item_price_'+section_number+'" onblur="ItemTotal(\'item_price_'+section_number+'\');" style="font-weight: 700" value="1">';
tex +=    '<input type="hidden" name="hd_total_item_price_'+section_number+'" id="hd_total_item_price_'+section_number+'" /></div>';
tex +=    '<div class="amount_cell"><b>&pound;<span id="total_item_price_'+section_number+'">0.00</span></b></div>';
tex +=    '<div class="clear"></div>';

var new_tag = document.createElement('div');
new_tag.innerHTML =  tex;
section.appendChild(new_tag);
return false;
}

function AddMore1() {

var section = $('order_section');
var section_number = $$('select.letters_select').length;


var tex = '<div class="section_cell">';
tex +=    '<select size="1" id="letter_'+section_number+'" name="letter_'+section_number+'" style="font-weight: 700" class="letters_select">';
tex +=    '<option selected>Letter/number</option>';
tex +=    '<option>A</option>';
tex +=    '<option>B</option>';
tex +=    '<option>C</option>';
tex +=    '<option>D</option>';
tex +=    '<option>E</option>';
tex +=    '<option>F</option>';
tex +=    '<option>G</option>';
tex +=    '<option>H</option>';
tex +=    '<option>I</option>';
tex +=    '<option>J</option>';
tex +=    '<option>K</option>';
tex +=    '<option>L</option>';
tex +=    '<option>M</option>';
tex +=    '<option>N</option>';
tex +=    '<option>O</option>';
tex +=    '<option>P</option>';
tex +=    '<option>Q</option>';
tex +=    '<option>R</option>';
tex +=    '<option>S</option>';
tex +=    '<option>T</option>';
tex +=    '<option>U</option>';
tex +=    '<option>V</option>';
tex +=    '<option>W</option>';
tex +=    '<option>X</option>';
tex +=    '<option>Y</option>';
tex +=    '<option>Z</option>';
tex +=    '<option>a</option>';
tex +=    '<option>b</option>';
tex +=    '<option>c</option>';
tex +=    '<option>d</option>';
tex +=    '<option>e</option>';
tex +=    '<option>f</option>';
tex +=    '<option>g</option>';
tex +=    '<option>h</option>';
tex +=    '<option>i</option>';
tex +=    '<option>j</option>';
tex +=    '<option>k</option>';
tex +=    '<option>l</option>';
tex +=    '<option>m</option>';
tex +=    '<option>n</option>';
tex +=    '<option>o</option>';
tex +=    '<option>p</option>';
tex +=    '<option>q</option>';
tex +=    '<option>r</option>';
tex +=    '<option>s</option>';
tex +=    '<option>t</option>';
tex +=    '<option>u</option>';
tex +=    '<option>v</option>';
tex +=    '<option>w</option>';
tex +=    '<option>x</option>';
tex +=    '<option>y</option>';
tex +=    '<option>z</option>';
tex +=    '<option>0</option>';
tex +=    '<option>1</option>';
tex +=    '<option>2</option>';
tex +=    '<option>3</option>';
tex +=    '<option>4</option>';
tex +=    '<option>5</option>';
tex +=    '<option>6</option>';
tex +=    '<option>7</option>';
tex +=    '<option>8</option>';
tex +=    '<option>9</option>';
tex +=    '<option>!</option>';
tex +=    '<option>&pound;</option>';
tex +=    '<option>?</option>';
tex +=    '<option>&amp;</option>';
tex +=    '</select> ';
tex +=    '<select size="1" name="font_'+section_number+'" id="font_'+section_number+'" style="font-weight: 700">';
tex +=    '<option selected>Font</option>';
tex +=    '<option>Classic</option>';
tex +=    '<option>Princess</option>';
tex +=    '<option>Funky</option>';
tex +=    '<option>Plain</option>';
tex +=    '<option>Cowboy</option>';
tex +=    '<option>Fairytale</option>';
tex +=    '<option>Dohnut</option>';
tex +=    '<option>Whimsical</option>';
tex +=    '<option>Fairytale</option>';
tex +=    '<option>School</option>';
tex +=    '<option>Bubble</option>';
tex +=    '</select> ';
tex +=    '<select size="1" name="size_'+section_number+'" id="size_'+section_number+'" style="font-weight: 700" onChange="SetPrice1(\'size_'+section_number+'\', \'span_item_price_'+section_number+'\');">';
tex +=    '<option selected>Size</option>';
tex +=	  '<option>6 inches</option>';
tex +=    '<option>8 inches</option>';
tex +=    '<option>10 inches</option>';
tex +=    '<option>12 inches</option>';
tex +=    '</select> ';
tex +=    '</div>';
tex +=    '<div class="price_cell">';
tex +=    '<b>&pound;<span id="span_item_price_'+section_number+'">0.00</span></b></div>';
tex +=    '<div class="qty_cell">';
tex +=    '<input name="item_price_'+section_number+'" size="4" id="item_price_'+section_number+'" onblur="ItemTotal(\'item_price_'+section_number+'\');" style="font-weight: 700" value="1">';
tex +=    '<input type="hidden" name="hd_total_item_price_'+section_number+'" id="hd_total_item_price_'+section_number+'" /></div>';
tex +=    '<div class="amount_cell"><b>&pound;<span id="total_item_price_'+section_number+'">0.00</span></b></div>';
tex +=    '<div class="clear"></div>';

var new_tag = document.createElement('div');
new_tag.innerHTML =  tex;
section.appendChild(new_tag);
return false;
}

function ItemTotal(id) {
var price = $('span_'+id).innerHTML;
price = price*1;

var qty = $(id).value*1;

if(isNaN(qty)){
qty = 1;
$(id).value = qty;
}

var total = qty * price;
$('hd_total_'+id).value = total;
$('total_'+id).innerHTML = formatCurrency(total);

checkShippingCosts1('shipping_country', 'subtotal');
}

function Sub_total() {
var section_number = $$('select.letters_select').length;
//var edges = $('bev_edg');
//var edges_sel = edges.options[edges.selectedIndex].text; 
var subtotal = 0;
//var edges_price=0;

for(var i=0; i<section_number; i++) {
	if( $('item_price_'+i).value!='') {
	var price = $('total_item_price_'+i).innerHTML.toString().replace(/,/,'');
	subtotal += price*1
		/*if(edges_sel=="Yes") {
		subtotal += 1 * $('item_price_'+i).value*1;
		edges_price += 1 * $('item_price_'+i).value*1;
		}*/
	}
}
var discount = $('discount_procent').value*1;

if( code_type == '0' ) {
subtotal = subtotal - discount;	
}
else if ( code_type == '1' ) {
subtotal = subtotal - (subtotal *discount / 100);
}

//$('edge').innerHTML = formatCurrency(edges_price);
$('subtotal').innerHTML = formatCurrency(subtotal);
return subtotal;
}

function Total() {
var subtotal = Sub_total();
$('subtotal_hidden').value = subtotal;
$('total_price').value = subtotal;
}

function Total2() {

var subtotal = $('sub_total_val').value*1
var shpcosts = $('shpp_costs_val').value*1
var discount = $('discount_procent').value*1;

if( code_type == '0' ) {
subtotal = subtotal - discount;	
}
else if ( code_type == '1' ) {
subtotal = subtotal - (subtotal *discount / 100);
}

$('total').innerHTML = formatCurrency(subtotal + shpcosts);
$('total_price_val').value = subtotal + shpcosts;
}

function checkFFOrderForm() {
var section_number = $$('select.letters_select').length;

for(var i=0; i<section_number; i++) {
	if( $('item_price_'+i).value!='' ) {
		if( $('letter_'+i).options[$('letter_'+i).selectedIndex].text == "Letter/number" ) {
		alert("Please fill in required field");
		$('letter_'+i).focus();
		return false;
		}
		if( $('font_'+i).options[$('font_'+i).selectedIndex].text == "Font" ) {
		alert("Please fill in required field");
		$('font_'+i).focus();
		return false;
		}
		if( $('size_'+i).options[$('size_'+i).selectedIndex].text == "Size" ) {
		alert("Please fill in required field");
		$('size_'+i).focus();
		return false;
		}
		if( $('colour_'+i).options[$('colour_'+i).selectedIndex].text == "Colour" ) {
		alert("Please fill in required field");
		$('colour_'+i).focus();
		return false;
		}
	}
}

var num_of_letters = $$('select.letters_select').length;
$('num_of_letters').value = num_of_letters;

return true;
}

function checkPrOrderForm() {
var section_number = $$('select.letters_select').length;

if( $('item_price_0').value!='' ) {
	if( $('letter_0').options[$('letter_0').selectedIndex].text == "Letter/number" ) {
	alert("Please fill in required field");
	$('letter_0').focus();
	return false;
	}
}

var num_of_letters = $$('select.letters_select').length;

$('num_of_letters').value = num_of_letters;

return true;
}

function checkShippingCosts1(id, subtotal_id) {
Total();
}


function SetPrice(id, item_price_id) {
var selected_size = $(id).options[$(id).selectedIndex].text;
var price='';
if( selected_size == '5 inches') {
price = '7.49';
}
else if( selected_size == '8 inches' ) {
price = '9.99';
}
else {
price = '0.00';	
}

$(item_price_id).innerHTML = formatCurrency(price);

var item_price = item_price_id.replace(/span_/,"");
ItemTotal(item_price);
checkShippingCosts1('shipping_country', 'subtotal');
}

function SetPrice1(id, item_price_id) {
var selected_size = $(id).options[$(id).selectedIndex].text;
var price='';
if( selected_size == '6 inches') {
price = '2.10';
}
else if( selected_size == '8 inches' ) {
price = '2.80';
}
else if( selected_size == '10 inches' ) {
price = '3.70';
}
else if( selected_size == '12 inches' ) {
price = '4.50';
}
else {
price = '0.00';	
}

$(item_price_id).innerHTML = formatCurrency(price);

var item_price = item_price_id.replace(/span_/,"");
ItemTotal(item_price);
checkShippingCosts1('shipping_country', 'subtotal');
}

function addTicket(code) {
var url = '/process.php';
			var ajax = new Ajax.Request(
			url,
			{method: 'post', parameters: {code: code},
			onSuccess: function(transport) {
    		var msg = transport.responseText.split('|');
				if(msg[0] != 'ok') {
				$('error').update(msg[1]).setStyle({ color: '#ef0000', display: 'block' });	
				}
				else {
				$('discount_procent').value = msg[1];
				$('discount_hidden').value = code;
				code_type = msg[2];
				calculateTotal();
				$('error').update('Discount code applied. Please check total costs above.').setStyle({ color: '#0C0', display: 'block' });	
				}
  			}
			});	
}

function addTicketFs(code) {
var url = 'https://secure92.nocdirect.com/~thewoode/process.php';
			var ajax = new Ajax.Request(
			url,
			{method: 'post', parameters: {code: code},
			onSuccess: function(transport) {
    		var msg = transport.responseText.split('|');
				if(msg[0] != 'ok') {
				$('error').update(msg[1]).setStyle({ color: '#ef0000', display: 'block' });	
				}
				else {
				$('discount_hidden').value = code;
				$('discount_procent').value = msg[1];
				code_type = msg[2];
				Total2();
				$('error').update('Discount code applied. Please check total costs above.').setStyle({ color: '#0C0', display: 'block' });	
				}
  			}
			});	
}

function CheckMax(ent_val, max_num) {
	if( ent_val * 1 > max_num * 1 ) {
	alert("You want to order too many items of a product!");
	}
	return false;
}