/**
 * 
 */

function hide(id){
	document.getElementById(id).style.display = 'none';
}
function show(id){
	document.getElementById(id).style.display = 'block';
}
function hideShow(id){
	elmnt = document.getElementById(id);
	elmnt.style.display = (elmnt.style.display == 'none') ? 'block' : 'none';
}
function addInput(formName,inputName,inputValue){
	form = document.getElementById(formName);
	input = document.createElement('input');
	input.setAttribute('name',inputName);
	input.setAttribute('value',inputValue);
	input.setAttribute('type','hidden');
	form.appendChild(input);
}
function clearNode(name){
	node = document.getElementById(name);
	while(node.firstChild){
		node.removeChild(node.firstChild);
	}
}

function number_format(number,decimalplaces){
	num_str = number.toString();
	
	if(num_str.lastIndexOf('.')!=-1){
		decimal = num_str.substring(num_str.lastIndexOf('.'),num_str.length);
		num_str = num_str.substring(0,num_str.lastIndexOf('.'));
	}
	else{
		decimal = '.';
	}
	output = '';
	while(num_str.length > 3){
		output = ',' + num_str.substring(num_str.length-3,num_str.length) + output;
		num_str = num_str.substring(0,num_str.length-3);
	}
	output = num_str + output;
	if(!decimalplaces){
		return output;
	}
	else{
		while(decimal.length<=decimalplaces){
			decimal+='0';
		}
		return output + decimal;
	}
}


//Shop
function addCart(num){
	hide('addcart'+num);
	show('incart'+num);
	items[num]['count'] = 1;
	updatetotal();
}
function addOne(num){
	items[num]['count']++;
	document.getElementById('itemcount'+num).firstChild.nodeValue = items[num]['count'];
	updatetotal();
}
function removeOne(num){
	items[num]['count']--;
	if(items[num]['count']>0){
		document.getElementById('itemcount'+num).firstChild.nodeValue = items[num]['count'];
	}
	else{
		hide('incart'+num);
		show('addcart'+num);
	}
	updatetotal();
}

function updatetotal(){
	totalvalue = 0;
	cartitemcount = 0;
	itemnumber = 1;
	clearNode('paypal');
	addInput('paypal','cmd','_cart');
	addInput('paypal','upload','1');
	addInput('paypal','business','lintvedt@ionsense.com');
	addInput('paypal','cancel_return','http://www.IonSense.com/ordercancelled');

	
	for(x in items){
		totalvalue += items[x]['price']*items[x]['count'];
		cartitemcount += items[x]['count'];
		if(items[x]['count']>0){
			addInput('paypal','item_name_'+itemnumber,items[x]['name']);
			addInput('paypal','amount_'+itemnumber,number_format(items[x]['price'],2));
			addInput('paypal','quantity_'+itemnumber,items[x]['count']);
			addInput('paypal','custom_'+itemnumber,x);
			itemnumber++;
		}
	}
	
	shippingchoice = 0;
	if(cartitemcount>0){
		for(x=1;x<shipping.length;x++){
			if(document.getElementById('shippingchoice'+x).checked){
				shippingchoice = x;
				break;
			}
		}
		addInput('paypal','shipping_1',shipping[shippingchoice]['price']+'.00');
		totalvalue+= shipping[shippingchoice]['price'];
	}

	if(cartitemcount==1){
		document.getElementById('itemcount').firstChild.nodeValue = cartitemcount+' item';
	}
	else{
		document.getElementById('itemcount').firstChild.nodeValue = cartitemcount+' items';
	}
	document.getElementById('carttotal').firstChild.nodeValue = number_format(totalvalue,2);
	
	
	if(totalvalue>4000){
		show('paypalrequired');
	}
	else{
		hide('paypalrequired');
	}
	if(totalvalue>10000){
		hide('checkoutbutton');
		show('paymenttoohigh');
	}
	else{
		hide('paymenttoohigh');
		show('checkoutbutton');
	}
}

function checkIE(){
	if(navigator.appName == 'Microsoft Internet Explorer'){
		var ua = navigator.userAgent;
    	var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	    if (re.exec(ua) != null){
			rv = parseFloat( RegExp.$1 );
		}
		var iecss = document.createElement("link");
		iecss.setAttribute("rel","stylesheet");
		iecss.setAttribute("type","text/css");
		iecss.setAttribute("href","css/IEfix.css");
		document.getElementsByTagName("head")[0].appendChild(iecss);
	}
}
