// JavaScript Document


function run(){}

var last = 1;

function addInscription(){
	var div;
	if(div = $('inscription'+last)){
		next = last+1;
		div.insert({after: '<div id="inscription'+next+'"></div>'});
		var newDiv = $('inscription'+next);
		var divContent = $('inscription1').innerHTML;
		var pat = new RegExp('inscription1','g');
		newDiv.insert( {top:divContent.replace(pat,'inscription'+next)} );
		newDiv.insert({ bottom:'<a onclick="removeInscription('+next+');" class="removeButton" >Remove This Inscription</a>' });
		var inputs = newDiv.select('input');
		for(var i = 0; i<inputs.length; i++){
			if(inputs[i].name == 'inscription'+next+'_count'){
				inputs[i].value = 1;
			}else{
				inputs[i].value = '';
			}
		}
		last = next;
		runCount();
	}else if(last > 1){
		last--;
		addInscription();
	}
}

function runCount(){
	var counts = $$('input').findAll(function(i) { return i.name.match(/^inscription[0-9]+\[count\]$/); } );
	var thisCount = 0;
	for(var i=0;i<counts.length; i++){
		if(!counts[i].value.match(/^[0-9]+$/)){ counts[i].value = 1; }
		thisCount += parseInt(counts[i].value);
	}
	$('brickCountShow').innerHTML = thisCount;
	$('brick_count').value = thisCount;
	$('totalShow').innerHTML = thisCount*300;
	$('total_donation').value = thisCount*300;
}

function removeInscription(num){
	$('inscription'+num).remove();
	runCount();
}

function redAlert(text){
	$('redAlert').insert({bottom:'<p>'+text+'</p>'});
}

function checkForm(){
	$('redAlert').childElements().invoke('remove');
	var alertColor = '#FF0000';
	var normColor = '#333333';
	var pass = true;
	var fields = new Array();
	var names = new Array();
	var num = -1;
	var lblArr;
	x = $('form1');
	for(e=0;e<x.length;e++){
		if(!names.find( function(n){ return n == x.elements[e].name; }) ){
			num++;
			names[num]=x.elements[e].name.replace(/[\[\]]/g,'');
			lblArr = $$('#lbl-'+x.elements[e].id,'#lbl-'+x.elements[e].name);
			if(lblArr.length>0){
				fields[num] = new Array(false,lblArr[0]);
			}
		}
		if(fields[num]){
			switch(x.elements[e].type){
				case 'checkbox':
				case 'radio':
					if(x.elements[e].checked){ fields[num][0] = true; }
					break;
				default:
					if(x.elements[e].value != ''){ fields[num][0] = true; }
			}
		}
	}
	fields.each(function(f){
			if(f){
				if(!f[0]){
					f[1].style.color= alertColor;
					pass = false;
				}else{
					f[1].style.color= normColor;
				}
			}
		});
	
	if(!pass){
		redAlert('Please fill in the fields with red labels');
	}
	var phone;
	if(phone = $('phone').value){
		if(!phone.match(/^\(?[0-9]{3}\)?[-\s]?[0-9]{3}[-\s]?[0-9]{4}([^0-9].*)?$/)){/**/
			$('lbl-phone').style.color= alertColor;
			pass = false;
			redAlert("The phone number entered does not appear to be a phone number.");
		}
	}
	if($('email').value != $('email2').value){
		$('lbl-email').style.color= alertColor;
		$('email2_lbl').style.color= alertColor;
		pass = false;
		redAlert("Your E-mail address doesn't match the Confirm E-mail field.");
	}else{
		$('email2_lbl').style.color= normColor;
	}
		
	// if pass is still true submit the form
	if(pass){
		return true;
	}else{
		window.scrollTo(0,0);
		return false;
	}
}