/*DOM support detection*/
ie = (document.all) ? true:false; // IE4+
dom = ((document.getElementById) && (!ie)) ? true:false; // Mozilla 

setEventById("bu", "click", formHandler);


/*Help function to set events by id*/
function setEventById(id, ev, fu) {

	if(dom) {
		document.getElementById(id).addEventListener(ev, fu, false);
	} 
	if(ie) {
		document.getElementById(id).attachEvent('on' + ev, fu);
	}
} 


//formHandler ( Get user input and and sends it to matchInput )
function formHandler() {

	matchInput(document.getElementById("adr").value, document.getElementById("psw").value);
}


//matchInput function( investigates if address and password are correct)
function matchInput(a, p) {

var patternAdr = /@dsv.su.se$|@kth.se$/;
var patternPsw = /^\d+$/;
var adr = a;
var psw = p;
var resultAdr;
var resultPsw;

if((resultAdr = patternAdr.exec(adr)) != null) 
	var aCorrect = true;
else
	var aCorrect = false;
	
if((resultPsw = patternPsw.exec(psw)) != null) 
	var pCorrect = true;
else 
	var pCorrect = false;

if(aCorrect && pCorrect)
	window.alert("Address and password correct")
else if(!aCorrect && !pCorrect)
	window.alert("Address does not end with @dsv.su.se or @kth.se and password is not a number");
else if(!aCorrect && pCorrect)
	window.alert("Address does not end with @dsv.su.se or @kth.se");
else if(!pCorrect&& aCorrect)
	window.alert("Password is not a number");
}