var firstFieldsetLeft;
var columnWidth;
var firstFieldWidth;
function fixFirefoxLabels(){
	// Hide form
//	$('form.edunetForm').hide();
	
	$('form.edunetForm fieldset').find('label.firstField').each(function(i){
		$(this).width(firstFieldWidth);
	});
	
	if (columnWidth != undefined) {
		$('form.edunetForm').find('.column').each(function(i){
			$(this).width(columnWidth);
		});
	}
	
	$('form.edunetForm').find('div.note').each(function(i){
		$(this).css("marginLeft", firstFieldWidth + 8 + "px");
	});
	
	$('form.edunetForm').find('li label.firstField, .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;
		$(this).wrapInner(labelSpan);
		this.style.display = '-moz-inline-box';
	});
	
	// Show form
	$('form.edunetForm').show();
}

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

	if (firstFieldWidth != undefined) {    
		$('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");
			});
		});
		
		$('form.edunetForm').find('div.note').each(function(i){
			$(this).css("marginLeft",firstFieldWidth + 8 + "px");
		});
	}
	
	if (!$.browser.mozilla) {
		$('form.edunetForm fieldset').find('label.firstField').each(function(i){
			$(this).width(firstFieldWidth);
		});
	}
	
	if (columnWidth != undefined) {
		$('form.edunetForm').find('.column').each(function(i){
			if ($(this).css("width") == "auto");
				$(this).width(columnWidth);
		});
	}
	
	$("form.edunetForm :input[type='text']").each(function(i){
		if ($(this).attr("class") == undefined && $(this).css("width").indexOf("px") == -1)
			$(this).css("width","110px");
	});
	
	$("form.edunetForm").find(":input[type='radio'], :input[type='checkbox']").each(function(i){
		$(this).css("border","none");
	});
	
	$("form.edunetForm :not(label)").find("> :input[type='text'], > select").each(function(i){
		$(this).css("marginRight","10px");
	});
	
	$('form.edunetForm :input:not(#submit)').each(function(i) {
		$(this).focus(function(e) {
			$(this).attr("backgroundColor",$(this).css("backgroundColor"));
			$(this).css("backgroundColor", "#FFC");
			$(this).css("borderColor", "#FC6");
		});
		$(this).blur(function(e) {
		$(this).css("backgroundColor", $(this).attr("backgroundColor"));
		$(this).css("borderColor", "#DDD");
		});
	});
	
	// Show form
	$('form.edunetForm').show();
}

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

function validate(sourceForm) {
	var errorFields = "";
	var errorFieldsRadioOptions = new Object();
	var firstErrorField = "";
	$("form.edunetForm :input").each(function(i) {
		var curField = this;
		var curFieldHasError = false;
		var temp;
		
		if (curField.getAttribute("req") != undefined) {
			if (curField.getAttribute("alt") == undefined || curField.getAttribute("alt") == "")
				temp = curField.name.substr(curField.name.indexOf("_")+1);
			else
				temp = curField.getAttribute("alt");
			if (curField.getAttribute("type") != "radio" && (curField.value == "" || curField.value.indexOf("-- ") != -1)) {
				curFieldHasError = true;
			}
			else if (curField.getAttribute("type") == "radio") {
				var allCurFieldRadioOptions = document.getElementsByName(curField.name);
				if (errorFieldsRadioOptions[temp] == undefined) {
					for (var k=0; k<allCurFieldRadioOptions.length; k++) {
						if (!allCurFieldRadioOptions[k].checked && errorFieldsRadioOptions[temp] != "checked") {
							  errorFieldsRadioOptions[temp] = "notChecked";
						}
						else {
							errorFieldsRadioOptions[temp] = "checked";
						}
					}
					if (errorFieldsRadioOptions[temp] == "notChecked") {
						curFieldHasError = true;
					}
				}
			}
			else if (curField.getAttribute("type") == "checkbox" && !curField.checked) {
				curFieldHasError = true;
			}
			if (curFieldHasError) {
				if (firstErrorField == "") {
					firstErrorField = curField;
				}
				errorFields += " - " + temp + "\n";
			}
		}
	});
  
	$("form.edunetForm [name='attachFrame']").each(function(i) {
		try {
			var attachFrame = this;
			var attachDoc = attachFrame.contentWindow || attachFrame.contentDocument;
			if (attachDoc.document)
				attachDoc = attachDoc.document;
			var numUploaded = attachDoc.variables.numItems.value;
			if (attachFrame.getAttribute("req") != undefined && numUploaded == 0) {
				if (firstErrorField == "") {
				  firstErrorField = attachFrame;
				}
				if (attachFrame.getAttribute("alt") != undefined)
					errorFields += " - " + attachFrame.getAttribute("alt") + "\n";
			}
		} catch (e) {};
	});

	if (errorFields == ""){
		return true;
	} else {
		focusField(firstErrorField);
		var erroFieldsArr = errorFields.split("\n");
		if (erroFieldsArr.length > 10) {
			errorFields = erroFieldsArr.splice(0,9).join("\n") + "\n\t...\n\t...\n\t...\n";
		}
		alert("Please fill in required fields:\n" + errorFields);
		try {
			tempObj.select();
		}
		catch(e) {};
		return false;
	}
}

function reqAttachment(srcInput, i) {
	if (i != 0 && $("[name='attachFrame']").get(i-1) != undefined) {
		$("[name='attachFrame']").eq(i-1).attr("req","true");
		$("[name='" + srcInput.name + "']").one("change", function() {
			if (this != srcInput && $("[name='attachFrame']").eq(i).attr("req") != "")
				$("[name='attachFrame']").eq(i-1).removeAttr("req");
		});
	}
}

function debugForm() {
	$("form.edunetForm :input:not([type='hidden'])").each(function(i) {
		if (this.type.toLowerCase() == "text" || this.tagName.toLowerCase() == "textarea")
			$(this).val($(this).attr("name"));
		if (this.type.toLowerCase() == "radio" || this.type.toLowerCase() == "checkbox")
			$(this).attr("checked","true");
		if (this.options)
			this.options[1].selected = true;
	});
}

$(document).ready(function(){
	if ($.browser.mozilla) {
		$('form.edunetForm').hide();
		setTimeout("fixFirefoxLabels()",50);
	}
	else {
		edunetForm2();
	}
});
