window.addEvent('domready', function() {
	 
	/**
	 * Hijack form submission action
	 */
	// $('survey').onsubmit = validateFields;
	
	/**
	 * Fair use pop-up
	 */
	$('fairuse').addEvent('click', function () {
		mywindow = window.open("fairuse.html", "myWindow", "status = 1, height = 300, width = 300, resizable = 1" );
		mywindow.moveTo(300,200);
	});
	
	/**
	 * Form validation using Form.Check
	 */
	var formcheck = new FormCheck('survey', {
		display : {	
			errorsLocation : 1,
			indicateErrors : 2
		}
	});
	
	/**
	 * Commaify user values
	 */
	$$('input.userValue').addEvent('change', function() {
		var num = this.value.replace(/\,/g,'');
		if(!isNaN(num)) {
			this.value = num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
		}
	});
	
	/*
	* Delete estimate entry and disable value field if user selects "Bought In"
	*/
	$$('input.boughtIn').addEvent('click',function() {
		var qNum = (this.name.substring(2,3) == 'q') ? this.name.substring(1,2) : this.name.substring(1,3);
		var estimate = $('survey')['value'+qNum];
		estimate.value = "0";
		estimate.disabled = true;
		//formcheck.removeError(estimate);
	});
	
	/*
	* Enable value field if user clicks on estimate entry field
	*/
	$$('input.bought').addEvent('click',function() {
		var qNum = (this.name.substring(2,3) == 'q') ? this.name.substring(1,2) : this.name.substring(1,3);
		var estimate = $('survey')['value'+qNum];
		estimate.value = "";
		estimate.disabled = false;
	});

	
	/**
	 * Temporary function to block form submission
	 */
	function validateFields()
	{
		var valid = true;
		var msg = 'There were errors for the following items:\n';
		
		// Cycle through ratings and validate
		for (i = 1; i <= 10; i++) {
			var group = $('survey')['a'+i+'q2'];
			var selected = false;
			
			for (var j = 0; j < group.length; j++) {
				if (group[j].checked == true) {
					selected = true;
					break;
				}
			}
			
			if (!selected) {
				valid = false;
				msg += 'Artwork '+i+', no rating is selected\n';
				$$("."+group[0].name+"span").setStyle('background-color', "#BE5A5A");
			} else {
				$$("."+group[0].name+"span").setStyle('background-color', "#FFF");
			}
		}
		
		if (!valid) alert(msg);
		
		return valid;
	}
	
});

/**
 * Preload form validation images
 */
 
var i = 0;
imageObj = new Image();

// set image list
images = new Array();
images[0]="img/b.png";
images[1]="img/bl.png";
images[2]="img/br.png";
images[3]="img/c.png";
images[4]="img/close.png";
images[5]="img/l.png";
images[6]="img/r.png";
images[7]="img/t.png";
images[8]="img/tl.png";
images[9]="img/tr.png";

// start preloading
for(i=0; i<=9; i++) 
{
	imageObj.src=images[i];
}