
var COUNTRY_CODE_GERMANY = "DE";
var COUNTRY_CODE_SWITZERLAND = "CH";

function listSubmit(formName, orderColumnName, start, count, orderAsc) {
	var form = document.forms[formName];
	form[0].value = start;
	form[1].value = count;
	form[2].value = orderColumnName;
	form[3].value = orderAsc;
   	form.submit();
}



function deleteOfferDocument(newAction, field, imageType) {
	var form = document.forms[0];
	form[field].value = imageType;
	form.action = newAction;
	form.submit();
}

var focussed = false;

function calculatePrices(countryCode) {
	focussed = false;
	var form = document.forms[0];
	var total = 0;
	var productChosen = false;
	// fieldsArr is defined in create.jsp (createofferform.do)
	for (var i=0; i<fieldsArr.length; i++) {
		var j = 0;
		var p, c, pf;
		for (var key in fieldsArr[i]) {
      		var value = fieldsArr[i][key];
			if (key == "price") p = parseFloat(value);
			if (key == "count") c = value;
			if (key == "pricefield") pf = value;
		}
		var newprice = 0;
		newprice = (p * form[c].value);
		form[pf].value = formatNumber(newprice, countryCode);
		total += newprice;
		productCount = parseInt(form[c].value, 10);
		if (productCount > 0)
			productChosen = true;
	}
	if (form[pricetotal] != null)
		form[pricetotal].value = formatNumber(total, countryCode);
	if (form["sum"] != null)
		form["sum"].value = formatNumber(total, countryCode);
	// set ordercheck (check if at least one of the products is chosen)
	if (productChosen)
		form["ordercheck"].value = "1";
	else
		form["ordercheck"].value = "";
}

function checkOfferCountChanging(countryCode) {
	focussed = true;
	id = window.setTimeout("checkOfferCountChanging2('" + countryCode + "');",1200);
}

function checkOfferCountChanging2(countryCode) {
	if (focussed)
		calculatePrices(countryCode);
	checkOfferCountChanging(countryCode);
}

function formatNumber(price, locale) {;
	var strPrice = "" + price;
	var ret = strPrice;
	if (locale == COUNTRY_CODE_GERMANY) {
		// if there are no dots and no commas then append ",00"
		if (strPrice.indexOf(".") == -1 && strPrice.indexOf(",") == -1) {
			ret = strPrice + ",00";
		}
		// search for patterns like xxx.xx
		else if ((strPrice.lastIndexOf(".") + 3) == strPrice.length) {
			ret = strPrice.replace(".", ",");
		}
		// add dots to all triples
		numberCount = ret.lastIndexOf(",");
		count = 0;
		index = numberCount;
		for (i = 0; i < numberCount; i++) {
		   count++;
		   isNumberBeforeTriplet = (count == 4) ? true : false;
		   if (isNumberBeforeTriplet) {
		   		count = 1;
				strStart = ret.slice(0,index);
				strEnd = ret.slice(index, ret.length);
				ret = strStart + "." + strEnd;
		   }
		   index--;
		}
	}
	else if (locale == COUNTRY_CODE_SWITZERLAND) {
		// if there are no dots and no commas then append ".00"
		if (strPrice.indexOf(".") == -1 && strPrice.indexOf(",") == -1) {
			ret = strPrice + ".00";
		}
		// search for patterns like xxx,xx
		else if ((strPrice.lastIndexOf(",") + 3) == strPrice.length) {
			ret = strPrice.replace(",", ".");
		}
		// add apstrophes to all triples
		numberCount = ret.lastIndexOf(".");
		count = 0;
		index = numberCount;
		for (i = 0; i < numberCount; i++) {
		   count++;
		   isNumberBeforeTriplet = (count == 4) ? true : false;
		   if (isNumberBeforeTriplet) {
		   		count = 1;
				strStart = ret.slice(0,index);
				strEnd = ret.slice(index, ret.length);
				ret = strStart + "'" + strEnd;
		   }
		   index--;
		}
	}
	return ret;
}

var msg = null;
function loadWin() {
    var url = arguments[0];
    var typ;
    if (arguments.length>1) {
        var xwidth = arguments[1];
    }
    if (arguments.length>2) {
        var ywidth = arguments[2];
    }
 	params = "resizable=1,status=0,scrollbars=1,toolbar=0,location=0,directories=0,menubar=0,width="+
				xwidth+",height="+ywidth;
    if (msg != null) {
        if (!msg.closed) {
            msg.location.replace(url);
            msg.focus();
        } else {
            msg = window.open(url,"msg",params);
            msg.focus();
        }
    } else {
        msg = window.open(url,"msg",params);
        msg.focus();
    }
}
