var items=1;


function addmore() {
	target = document.getElementById("insert" + items);
	source = '<div class="row"><div class="col"><label for="product'+ items +'">Product:   </label> <select class="product" name="product[]" id="product'+ items +'"></select></div><div class="col"><label for="quantity'+ items +'">Quantity: </label> <input class="quantity" type="text" id="quantity'+ items +'" name="quantity[]" size="3" /></div><div class="col"><label for="price'+ items +'">Total Price: </label> <input class="price" type="text" id="price'+ items +'" name="price[]" size="10" /></div><div class="clear"></div></div>';
	product = "product"+ items; 
	items++;
	insert = source + '<div id="insert'+ items +'"></div>';
	target.innerHTML = insert;
	document.getElementById(product).innerHTML = document.getElementById('product0').innerHTML;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


function validate_email() {
	
	if( ! document.getElementById('email_address').value.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b/) ) 
	{
		return false;
	}
	
	else {
		return true;
	}
}

function capitalizeMe(val) {
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + ' ';
        }
        return newVal;
}

function check_form() {
	valid = true;
	document.getElementById("validation_errors").innerHTML = "";
	var quantityError = 0;
	var priceError = 0;
	var products = getElementsByClass("product");
	var quantities = getElementsByClass("quantity");
	var prices = getElementsByClass("price");
	for (i=0; i < products.length; i++) {
		if (products[i].value != "0-0-0") {
			if ((!quantities[i].value) || (quantities[i].value < 1)) {
				if (quantityError == 0) {
					document.getElementById("validation_errors").innerHTML = document.getElementById("validation_errors").innerHTML + "<li>Please ensure that all products have a valid quantity</li>";
				}
				quantityError = 1;
				quantities[i].parentNode.firstChild.style.color = "red";
				valid = false;
			} else {
				quantities[i].parentNode.firstChild.style.color = "#3A494F";
			}
			
			if ((!prices[i].value) || (prices[i].value < 1)) {
				if (priceError == 0) {
					document.getElementById("validation_errors").innerHTML = document.getElementById("validation_errors").innerHTML + "<li>Please ensure that all products have a valid price</li>";
				}
				priceError = 1;
				prices[i].parentNode.firstChild.style.color = "red";
				valid = false;
			} else {
				prices[i].parentNode.firstChild.style.color = "#3A494F";
			}
		} else {
			prices[i].parentNode.firstChild.style.color = "#3A494F";
			quantities[i].parentNode.firstChild.style.color = "#3A494F";
			products[i].parentNode.parentNode.innerHTML = "";
		}
	}






	if (!validate_email()) {
		document.getElementById("validation_errors").innerHTML = document.getElementById("validation_errors").innerHTML + "<li>Please enter a valid email address</li>";
		valid = false;
	}
	var fields = getElementsByClass("req");
	for (i=0; i < fields.length; i++ ) {
		if (!fields[i].value) {
			document.getElementById("validation_errors").innerHTML = document.getElementById("validation_errors").innerHTML + "<li>Please fill in your " + capitalizeMe(fields[i].name.replace("_", " ")) + "</li>";
			fields[i].parentNode.firstChild.style.color = "red";
			valid = false;
		} else {
			fields[i].parentNode.firstChild.style.color = "#3A494F";
		}
	}	
	return valid;
}
