/* Scripts for the Bli Medlem page */

/** Parses the HTML entities in string and returns the result. */
function parseEntities(string) {
	var el=document.createElement('span');
	el.innerHTML=string;
	return el.innerHTML;
}

/** Put focus onto the given field and blink it red. */
function gotoField(fieldName) {
	document.forms.nameform[fieldName].focus();
	document.forms.nameform[fieldName].style.backgroundColor="#FF8080";
	setTimeout("document.forms.nameform['"+fieldName+"'].style.backgroundColor='';",500);
}

/** Put focus onto the last of the given fields and blink them red. */
function gotoFields(fieldNames) {
	var styleCode='';
	var timeoutCode='';
	var fieldName='';
	while(fieldNames.length>0) {
		fieldName=fieldNames.shift();
		styleCode+="document.forms.nameform['"+fieldName+"'].style.backgroundColor='#FF8080';";
		timeoutCode+="document.forms.nameform['"+fieldName+"'].style.backgroundColor='';";
	}
	if (fieldName!='') {
		document.forms.nameform[fieldName].focus();
	}
	if (styleCode!='') {
		eval(styleCode);
	}
	if (timeoutCode!='') {
		setTimeout(timeoutCode,500);
	}
}

/** Change the message field when the family type is selected. */
function onchangeMedlemstype() {
	var form=document.forms.nameform;
	var classes=form.className.split(/\s+/);
	if (form.medlemstype.value=='familie') {
		var add=true;
		for(var i=0;i<classes.length;i++) {
			if (classes[i]=='type-familie') {
				add=false;
				break;
			}
		}
		if (add) {
			classes.push('type-familie');
			form.className=classes.join(' ');
		}
	} else {
		var remove=false;
		var i;
		for(i=0;i<classes.length;i++) {
			if (classes[i]=='type-familie') {
				remove=true
				break;
			}
		}
		if (remove && classes[i]=='type-familie') {
			classes.splice(i,1);
			form.className=classes.join(' ');
		}
	}
}

function onchangeAntallHusstandsmedlemmer() {
	var antall=document.forms.nameform.antallHusstandsmedlemmer;
	if (antall) {
		antall=antall.options[antall.selectedIndex].value;
		var mdl=document.getElementById('fam-mdl');
		if (mdl) {
			var str='';
			var i;
			for(i=1;i<=antall;i++) {
				str+=' show-'+i;
			}
			mdl.className=str;
		}
	}
}

/** Check that the contents of the form is not obviously invalid. */
function checkForm() {
	var agreement = document.getElementById('agreement');
	if (!agreement.checked) {
	    alert('Har du lest avtalen?');
	    return false;
	}

	var form=document.forms.nameform;
	var checkEmpty=Array("fornavn","etternavn","adresse","postnr","poststed","epost");
	var i;
	for(i=0;i<form.elements.length;i++) {
		form.elements[i].value=form.elements[i].value.replace(/^\s+|\s+$/g,'');
	}
	for(i=0;i<checkEmpty.length;i++) {
		if (form[checkEmpty[i]].value=="") {
			alert(parseEntities("Du m&aring; oppgi "+checkEmpty[i]+"."));
			gotoField(checkEmpty[i]);
			return false;
		}
	}
	if (form.epost.value.match(/^.+\@.+\.[^.]+$/)==null) {
		alert(parseEntities("Du m&aring; oppgi en gyldig epost-adresse."));
		gotoField('epost');
		return false;
	}
	if (form.birthdate_day.value.match(/^\d+$/)==null) {
		alert(parseEntities("Du m&aring; oppgi en gyldig f&oslash;dselsdag."));
		gotoField('birthdate_day');
		return false;
	}
	if (form.birthdate_month.value.match(/^\d+$/)==null) {
		alert(parseEntities("Du m&aring; oppgi en gyldig f&oslash;dselsm&aring;ned."));
		gotoField('birthdate_month');
		return false;
	}
	if (form.birthdate_year.value.match(/^\d+$/)==null) {
		alert(parseEntities("Du m&aring; oppgi et gyldig f&oslash;dsels&aring;r."));
		gotoField('birthdate_year');
		return false;
	}
	var birthdate=new Date(form.birthdate_year.value,form.birthdate_month.value-1,form.birthdate_day.value);
	if (birthdate.getFullYear()!=form.birthdate_year.value
	|| birthdate.getMonth()+1!=form.birthdate_month.value
	|| birthdate.getDate()!=form.birthdate_day.value) {
		alert(parseEntities("Du m&aring; oppgi en gyldig f&oslash;dselsdato."));
		gotoFields(new Array('birthdate_year','birthdate_month','birthdate_day'));
		return false;
	}
	if (form.medlemstype.value=='familie') {
		// TODO: check the family member fields
	}
	if (form.er_bruker && form.er_bruker.checked) {
		if (!form.passord) {
			alert("Feil: passordfeltet mangler samtidig som er-medlem boksen er avmerket.");
		} else {
			if (form.passord.value=="") {
				alert(parseEntities("Du m&aring; oppgi passord."));
				gotoField('passord');
				return false;
			}
		}
	}
	
	return true;
}

