/* globale Shop-Funktionen

*/

//Versandkosten
var addKissen = 5;
var addDuvet = 5;

function checkNumber(field){
	if( isNaN( field.value ) || field.value == "" || field.value == "0" ){
		field.value="";
		}
	else{
		field.value = Math.abs(field.value);
		}
	}
function refresh(field){
	var thisName = thisPriceField = "";
	var thisPrice = 0;
	checkNumber(field);
	
	if(field.value != ""){
		thisName = field.name.split("_");
		var thisType = thisName[thisName.length-1].toLowerCase();
		
		for(var i = 0; i<thisName.length-1;i++){
			if(thisPriceField==""){
				thisPriceField = thisName[i];
			}
			else{
				thisPriceField += "_" + thisName[i];
			}
		}
			
		thisPriceField += "_preis";
		thisPrice = getV(thisPriceField);
		artPrice = field.value * parseInt(thisPrice);
		setV(field.name+"_total",artPrice);
		}
	else{setV(field.name+"_total","");}
	updateFields();
	}
function updateFields(){
	var artPrice = totVK = total = 0;
	var thisField = thisCount = thisType = "";
	//total neu berechnen
	for(var i = 0; i<document.forms[0].elements.length;i++){
		if( document.forms[0].elements[i].name && document.forms[0].elements[i].name.indexOf("_total") != -1 ){
			if(document.forms[0].elements[i].value!="" && !isNaN(document.forms[0].elements[i].value)){
				total += parseInt(document.forms[0].elements[i].value);
				//anzahl und typ ermitteln
				thisName = document.forms[0].elements[i].name.split("_");
				thisField = "";thisType = 0;
				for(var k = 0; k<thisName.length-1;k++){
					if(thisField==""){
						thisField = thisName[k];
						}
					else{
						thisField += "_" + thisName[k];
						}
					}
				thisCount = getV(thisField);
				thisType = getPricePerUnit(thisName[thisName.length-2].toLowerCase());
				if(!isNaN(thisType) && !isNaN(thisCount)){
					totVK += parseInt(thisCount) * parseInt(thisType);
					}
				}
			}
		}
	setV("Versandkosten",totVK);
	setV("Gesamttotal",total + totVK);
	}
	
function getPricePerUnit(type){
	switch (type) {
		case "duvet":
			return addDuvet;
			break;	
		case "kissen":
			return addKissen;
		}
	}

