// form options processor
function updateTotal(form,option,label,radio) {
	var price = parseFloat(form.elements['baseprice'].value);
	var increment = parseFloat(form.elements['increment'].value);
	var optionvalue = parseFloat(form.elements[option].value);
	var productpr = form.elements['itemname'].value;
	var optionsNames = productpr;// this holds a string to indicate which options were added
	var addString = " +";
	if (radio) {
		addString = "/";
		for (i=0;i<form.elements[radio].length;i++) {
			if (form.elements[radio][i].checked) {
				optionvalue = form.elements[radio][i].value;
				increment = optionvalue;
				var parts = productpr.split(addString);
				optionsNames = parts[0].concat(addString,label);
			}
		}
	} else {
		if (form.elements[option].checked == true) {
			increment += optionvalue;
			if (optionsNames.indexOf(label) == -1) {
				optionsNames = optionsNames.concat(addString,label);
			}
		} else if (increment != 0) {
			increment -= optionvalue;
			if (optionsNames.indexOf(label) != -1) {
				optionsNames = optionsNames.replace(addString.concat(label),"");
			}
		}
	}
	form.elements['increment'].value = increment;
	form.elements['itemname'].value = optionsNames;
	return true;
}
function setPrice(form) {
	var increment = parseFloat(form.elements['increment'].value);
	var baseprice = parseFloat(form.elements['baseprice'].value);
	var productpr = form.elements['itemname'].value;
	form.elements['productpr'].value = productpr.concat(":",baseprice+increment);
	return true;
}
function setPassword(form,id) {
	var pass = GeneratePassword();
	form.elements[id+'info'].value = pass;
	form.elements[id+'input_password'].value = pass;
	form.elements[id+'input_repeatpassword'].value = pass;
}
function GeneratePassword() {
    var length=6;
    var sPassword = ""; 
    var noPunction = true;
    for (i=0; i < length; i++) {
        numI = getRandomNum();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }
        sPassword = sPassword + String.fromCharCode(numI);
    }
    return sPassword;
}

function getRandomNum() {
    
    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000    
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127        
    rndNum = (rndNum % 94) + 33;
            
    return rndNum;
}

function checkPunc(num) {
    
    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }    
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }
    
    return false;
}

