//if( document.addEventListener )
//	document.addEventListener( 'DOMContentLoaded', edunetForm1, false);

var firstFieldsetLeft;
var columnWidth;
var firstFieldWidth;
function edunetForm1(){
	// Hide forms
	$( 'form.edunetForm' ).hide().end();
	
	$( 'form.edunetForm fieldset' ).find( 'label.firstField' ).each( function( i ){
		$(this).width(firstFieldWidth);
	} ).end();
	
	if (columnWidth != undefined) {
		$( 'form.edunetForm' ).find( 'label.column' ).each( function( i ){
			$(this).width(columnWidth);
		} ).end();
	}
	
	$( 'form.edunetForm' ).find( 'div.note' ).each( function( i ){
		$(this).css("marginLeft",firstFieldWidth + 8 + "px");
	} ).end();
	
	$( 'form.edunetForm li' ).find( 'label.firstField' ).each( function( i ){
		var labelContent = this.innerHTML;
		var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
		var labelSpan = document.createElement( 'span' );
			labelSpan.style.display = 'block';
			labelSpan.style.width = labelWidth;
			labelSpan.innerHTML = labelContent;
		this.style.display = '-moz-inline-box';
		this.innerHTML = null;
		this.appendChild( labelSpan );
	} ).end();
	
	$( 'form.edunetForm' ).find( 'label.column' ).each( function( i ){
		var labelContent = this.innerHTML;
		var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
		var labelSpan = document.createElement( 'span' );
			labelSpan.style.display = 'block';
			labelSpan.style.width = labelWidth;
			labelSpan.innerHTML = labelContent;
		this.style.display = '-moz-inline-box';
		this.innerHTML = null;
		this.appendChild( labelSpan );
	} ).end();
	
	// Show forms
	$( 'form.edunetForm' ).show().end();
}

function edunetForm2(){
	// Hide forms
	$( 'form.edunetForm' ).hide().end();

	if (firstFieldWidth != undefined) {
		if (!$.browser.mozilla) {
			$( 'form.edunetForm fieldset' ).find( 'label.firstField' ).each( function( i ){
				$(this).width(firstFieldWidth);
			} ).end();
		}
		
		$( 'form.edunetForm' ).find( 'fieldset fieldset' ).each( function( i ){
			$(this).css("paddingLeft",firstFieldsetLeft + 5 + "px");
			$(this).find( 'legend').each( function( j ){
				if ($.browser.msie)
					$(this).css("marginLeft",(firstFieldsetLeft + 12) * -1 + "px");
				else
					$(this).css("marginLeft",(firstFieldsetLeft + 6) * -1 + "px");
			} ).end();
		} ).end();
		
		$( 'form.edunetForm' ).find( 'div.note' ).each( function( i ){
			$(this).css("marginLeft",firstFieldWidth + 8 + "px");
		} ).end();
		
		$( 'form.edunetForm' ).find( 'input#submit' ).each( function( i ){
//			$(this).css("marginLeft",firstFieldsetLeft + 10 + "px");
		} ).end();
	}
	
	if (columnWidth != undefined) {
		$( 'form.edunetForm' ).find( 'label.column' ).each( function( i ){
			$(this).width(columnWidth);
		} ).end();
	}

	$( 'form.edunetForm' ).find( 'input' ).each( function( i ){
		if (this.type != "text")
			this.style.width = "auto";
		if (this.type == "radio" || this.type == "checkbox")
			this.style.border = "none";
		if (this.type == "text")
			this.style.marginRight = "10px";
	} ).end();

	// Show forms
	$( 'form.edunetForm' ).show().end();
}

var tempObj;
function focusField(obj) {
	tempObj = obj;
	tempObj.focus()
}

function validate(sourceForm) {
	var errorFields = "";
	var fields = Array( document.getElementsByTagName("input"), document.getElementsByTagName("textarea"), document.getElementsByTagName("select") );
	var errorFieldsRadioOptions = new Object();
	var firstErrorField = "";
	
	for (var i=0; i<fields.length; i++) {
		for (var j=0; j<fields[i].length; j++) {
			var curField = fields[i][j];
			var curFieldHasError = false;
			if (curField.getAttribute("req") != undefined ) {
				curField.id = curField.name.substr(curField.name.indexOf("_")+1);
				if (curField.getAttribute("type") != "radio" && (trimString(curField.value) == "" || trimString(curField.value) == "-- Please select from the following choices --" || trimString(curField.value) == "-- Please select one --")) {
					curFieldHasError = true;
				}
				else if (curField.getAttribute("type") == "radio") {
					var allCurFieldRadioOptions = document.getElementsByName(curField.name);
					if (errorFieldsRadioOptions[curField.id] == undefined) {
						for (var k=0; k<allCurFieldRadioOptions.length; k++) {
							if (!allCurFieldRadioOptions[k].checked && errorFieldsRadioOptions[curField.id] != "checked") {
								errorFieldsRadioOptions[curField.id] = "notChecked";
							}
							else {
								errorFieldsRadioOptions[curField.id] = "checked";
							}
						}
						if (errorFieldsRadioOptions[curField.id] == "notChecked") {
							curFieldHasError = true;
						}
					}
				}
				else if (curField.getAttribute("type") == "checkbox" && !curField.checked) {
					curFieldHasError = true;
				}
				if (curFieldHasError) {
					if (firstErrorField == "") {
						firstErrorField = curField;
					}
					errorFields += " - " + curField.id + "\n";
				}
			}
		}
	}

	if (errorFields == ""){
		return true;
	} else {
		focusField(firstErrorField);
		alert("Please fill in required fields:\n" + errorFields);
		try {
			tempObj.select();
		}
		catch(e) { };
		return false;
	}
}

$(document).ready(function(){
	if ($.browser.mozilla)
		setTimeout("edunetForm1()",10);
	//setTimeout("edunetForm2()",1);
	edunetForm2();
});
