

function validateFieldSet(arr_fields, validate_type)
{
	if(validate_type == 'ALL')
	{
		var errors = '';
		var error_count = 0;
		for(var i=0; i < arr_fields.length; i++)
		{
			field_pair = arr_fields[i]; // field pair is first html name, then visible label that the user sees.
			if(document.getElementById(field_pair[0]).value == '')
			{errors += '<br/>'+field_pair[1];error_count++;}
		}
		if(error_count > 0)
		{
			return '<br/><br/>Pleast fill out the following fields:<br>'+errors;
		}
		else
		{return '';}
	}
	else if(validate_type == 'ATLEAST1')
	{
		var field_list = '';
		for(var i=0; i < arr_fields.length; i++)
		{
			field_pair = arr_fields[i];
			field_list += '<br/>'+field_pair[1];
			if(document.getElementById(field_pair[0]).value != '')
			{return '';} // success, at least one entered.
		}
		return '<br/><br/>Please fill out AT LEAST ONE of the following fields:<br/>'+field_list;
	}
}

var results = '';








// Text Field Validation Functions
// copyright Stephen Chapman, 26th Dec 2004
// you may copy this function but please keep the copyright notice with it
function stripBlanks(fld) {
var result = "";
var c = 0;
for (i=0; i<fld.length; i++) {
  if (fld.charAt(i) != " " || c > 0) {
    result += fld.charAt(i);
    if (fld.charAt(i) != " ") c = result.length;
    }
  }
return result.substr(0,c);
}

var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm,val) {
  if (parm == "") return true;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}

function isNum(parm) {return isValid(parm,numb);}
function isLower(parm) {return isValid(parm,lwr);}
function isUpper(parm) {return isValid(parm,upr);}
function isAlpha(parm) {return isValid(parm,lwr+upr);}
function isAlphanum(parm) {return isValid(parm,lwr+upr+numb);}
function isDecimal(parm) {return isValid(parm,numb+'.');}

function oneOnly(parm,chr,must) {
var atPos = parm.indexOf(chr,0);
if (atPos == -1) {return !must;}
if (parm.indexOf(chr, atPos + 1) > - 1) {return false;}
return true; }

function adjacent(parm,chrs) {
return(parm.indexOf(chrs,0) != -1);
}

function onlyAdjacent(parm,comb,chrs) {
var a = parm.split(comb);
var b = a.join('');
for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) != -1) return false;
  }
return true; }

function setOrder(parm,first,second) {
var pos1 = parmField.indexOf(first,0);
if (pos1 == -1) return false; // first char not found
var pos2 = parmField.indexOf(second,pos1+1);
if (pos2 == -1) return false; // second char doesn't follow first
return true; }

function setDistance(parm,first,last,min,max) {
var pos1 = first == '' ? 0 :
parmField.indexOf(first,0);
var pos2 = last == '' ? parmField.length - pos1 - 1 :
parmField.indexOf(second,pos1+1);
if (pos1 == -1) return false;
if (pos2 < min || pos2 > max) return false;
return true; }

function endOption(fld,val) {
return fld.substring(fld.lastIndexOf(val)) == val;
}
                
	
	
	
var arrTextInCells = new Array(2);
arrTextInCells[0] = '<input type="text" name="stone_number[]"  id="stone_number[]"  size="10" >';
arrTextInCells[1] = '<textarea name="stone_inscription[]"  id="stone_inscription[]" cols="37" rows="2"></textarea>';


function add_row(objTable, arrTextInCells2)
{
	var num_rows = objTable.rows.length;
	var aRow = objTable.insertRow(num_rows - 1);
	var numCells = arrTextInCells2.length;
	var arrCells = new Array(numCells);

	for (var i=0; i < numCells; i++ )
	{
		arrCells[i] = aRow.insertCell(-1);
		arrCells[i].setAttribute('align', 'left');
		arrCells[i].className = 'orderReg';
		arrCells[i].innerHTML = arrTextInCells2[i];
	}
}

function delete_row(objTable)
{
	var num_rows = objTable.rows.length;
	if(num_rows > 3)
		objTable.deleteRow(num_rows - 2);
}


function validatePrice(elem)
{
	if(!isDecimal(elem.value)) {alert('The price must be\n a decimal number.');}
}
	
function validateQty(elem)
{
	if(!isNum(elem.value)) {alert('The Quantity must be\n an integer.');}
}
	
	
function subtotal_cart()
{
	var arr_prod_desc = document.getElementsByName('prod_desc[]');
	var arr_prod_price = document.getElementsByName('prod_price[]');
	var arr_prod_qty = document.getElementsByName('prod_qty[]');
	var arr_prod_total = document.getElementsByName('prod_total[]');
	var ult_total = document.getElementById('ult_total');
	is_empty = true;
	for(var i=0; i < arr_prod_price.length; i++)
	{
		var prod_desc = stripBlanks(arr_prod_desc[i].value);
		var prod_price = stripBlanks(arr_prod_price[i].value);
		var prod_qty = stripBlanks(arr_prod_qty[i].value);
		if(prod_desc != '' || prod_price != '' || prod_qty != '' ) {is_empty = false; break;}
	}
	if(is_empty) return true;
	var errors_desc = 0;
	var errors_price = 0;
	var errors_qty = 0;
	for(var i=0; i < arr_prod_price.length; i++)
	{
		var prod_desc = arr_prod_desc[i].value;
		var prod_price = arr_prod_price[i].value;
		var prod_qty = arr_prod_qty[i].value;
		if(stripBlanks(prod_desc) == '') errors_desc++;
		if(!isDecimal(stripBlanks(prod_price))) errors_price++;
		if(!isNum(stripBlanks(prod_qty))) errors_qty++;
	}
	var errors = '';
	if(errors_desc > 0) errors += errors_desc + ' product description field(s) are empty. \nPlease specify the associated products for those rows, \nor delete those rows if you don\'t need them.\n\n';
	if(errors_price > 0) errors += errors_price + ' price field(s) are not decimal numbers. \nPlease correct them.\n\n';
	if(errors_qty > 0) errors += errors_qty + ' quantity field(s) are not integers.  \nPlease correct them.';

	if(errors != '') {alert(errors);return false;}
	result_total = 0;
	for(var i=0; i < arr_prod_price.length; i++)
	{
		var prod_price = arr_prod_price[i].value;
		var prod_qty = arr_prod_qty[i].value;
		arr_prod_total[i].value = Math.round((prod_price * prod_qty)*100)/100;
		result_total += prod_price * prod_qty;
	}
	ult_total.value = Math.round((result_total)*100)/100;
	return true;
}

function validate_cart()
{
	var arr_stone_number = document.getElementsByName('stone_number[]');
	var arr_stone_inscription = document.getElementsByName('stone_inscription[]');
	is_empty = true;
	for(var i=0; i < arr_stone_inscription.length; i++)
	{
		var stone_number = stripBlanks(arr_stone_number[i].value);
		var stone_inscription = stripBlanks(arr_stone_inscription[i].value);
		if(stone_number != '' || stone_inscription != '') {is_empty = false; break;}
	}
	if(is_empty) return true;
	var errors_stone_number = 0;
	var errors_stone_inscription = 0;
	for(var i=0; i < arr_stone_inscription.length; i++)
	{
		var stone_number = arr_stone_number[i].value;
		var stone_inscription = arr_stone_inscription[i].value;
		if(stripBlanks(stone_number) == '') errors_stone_number++;
		if(stripBlanks(stone_inscription) == '') errors_stone_inscription++;
	}
	var errors = '';
	if(errors_stone_number > 0) errors += errors_stone_number + ' memorial stone number field(s) are empty. \nPlease specify the associated memorial stone numbers for those rows, \nor delete those rows if you don\'t need them.\n\n';
	if(errors_stone_inscription > 0) errors += errors_stone_inscription + ' memorial stone inscription field(s) are empty. \nPlease specify the associated memorial stone inscriptions for those rows, \nor delete those rows if you don\'t need them.\n\n';

	if(errors != '') {alert(errors);return false;}
	return true;
}


function set_order(stone_number_in)
{
	var arr_stone_number = document.getElementsByName('stone_number[]');
	var arr_stone_inscription = document.getElementsByName('stone_inscription[]');
	var i=arr_stone_number.length - 1;
	var stone_number = stripBlanks(arr_stone_number[i].value);
	var stone_inscription = stripBlanks(arr_stone_inscription[i].value);
	if(stone_number != '' || stone_inscription != '')
	{
		add_row(document.getElementById('order_table'), arrTextInCells);
		i++;
	}
	arr_stone_number[i].value = stone_number_in;
	arr_stone_inscription[i].focus();
}


