/* globals */
var errorMsgList = new ErrorMsgList();
var errorinfo = new error();

/* global array for receive all errors found from the call validateAdxx method */
function error() { 
	this.errorList = new Array();
	this.errorObjRefList = new Array();
}

/* ErrorMsgList class */
function ErrorMsgList()
{
	this.msgList = new Array();

	this.getMsg = getMsg;
	this.setErrorMessages = setErrorMessages;
	
	this.setErrorMessages();
	
	function getMsg(id)
	{	
		if(this.msgList[id]==null)
			return "Errormeldingen med id='"+id+"' er ikke satt i ErrorMsgList.";
			
		return this.msgList[id];
	}
	
	function setErrorMessages()
	{			
		this.msgList['id_email'] =  "Epostadressen ser ikke gyldig ut. Du kan oppgi kun én epostadresse, og den m&aring; v&aelig;re p&aring; formen xx@xx.xx.";		
		this.msgList['email'] =  "Ikke gyldig.";
		this.msgList['password'] = "Passord m&aring; fylles ut.";
	}
}

/* VALIDATION FIELDS */
/** validation input tag and textarea for string*/
function validateString(elmt, errorMsg)
{
	var obj = document.getElementById(elmt);
	if ( trim(obj.value) == null || trim(obj.value) == "" )
	{
		addError(elmt, errorMsg);
		markErrorField(obj);
	}
	else
		clearErrorField(obj);
}

function validateemail(elmt, errorMsg)
{
	var obj = document.getElementById(elmt);
	if( trim(obj.value)==null || !(new RegExp("^[^@]+@[^@]+\\..+")).test(trim(obj.value)))
	{
		addError(elmt, errorMsg);
		markErrorField(obj);
	}
	else
		clearErrorField(obj);
}

/** remove space from input tag or text area tag*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,"");
}


/** GET METODS */
function getValidationErrors()
{
	var errorText = "";
	for (var i=0;i<errorinfo.errorList.length;i++)
		errorText +='<li><a href='+getCurrentLink()+errorinfo.errorObjRefList[i]+'">'+errorinfo.errorList[i]+'</a></li>';	

	return errorText;
}

function getCurrentLink() 
{
  	var temp = location.href;
 	if ( temp.indexOf('#') == -1 ) {
 		var url = temp.substring(temp.lastIndexOf("/")+1);
 	} else {
 		var url = temp.substring(temp.lastIndexOf("/")+1,temp.indexOf('#'));
 	}
 	if ( url == "min_zett.html?lp=nt" ) {
 		var link = "\""+url+"#";
 	} else {
 		var link = "\""+"min_zett/"+url+"#";
 	}
 	return link;
}

function getErrorContainer(title, content)
{
	var text = "";
	text += '<div class=\"error\">';
	text += '<img class=\"icon\" id=\"img\" src=\"images/error_icon.gif\" />';
	text += '<span class=\"errortitle\">'+title+'</span>';
	text += '<ul class=\"errortext\">'+content+'</ul></div>';
	
	return text;
}



/** SHOW, ADD, HIDE AND MARK  HANDLER ERRORS */
function showErrorsLogin()
{
	if (errorinfo.errorList.length > 0) {	
		showvalidatejsplogin();
		return false; 
	} 
	else {
		document.logform.submit();
		return true;
	} 
}


function showvalidatejsplogin()
{
	cleanshowlist();
	var title = "Feil under innlogging:";

	if (errorinfo.errorList.length > 0) {	
		showError(title, getValidationErrors());
		window.scrollTo(0,0);
		return false; 
	} 
	else {
		hideError();
		return true;
	} 
}


function showError(title,text)
{	
	var showerror = document.getElementById('errorinformation');
	if(showerror!=null)
	{
		showerror.innerHTML = getErrorContainer(title, text);
		showerror.style.display = 'block';
	}
}

function addError(elmt, errorMsg)
{
	var obj = document.getElementById(elmt);
	errorinfo.errorList[errorinfo.errorList.length] = errorMsg;
	errorinfo.errorObjRefList[errorinfo.errorObjRefList.length] = elmt;
}

function hideError()
{
	var showerror = document.getElementById('errorinformation');
	if(showerror!=null)
	{
		showerror.innerHTML = "";
		showerror.style.display = 'none';
	}
}

function markErrorField(obj)
{
	obj = obj.parentNode;
	obj.style.backgroundColor = "yellow";
}

/** CLEAR ERRORS */
function clearerrorslogin() 
{
	clearErrors();
	clearErrorField(document.getElementById('id_email'));
	clearErrorField(document.getElementById('id_password'));
	hideError();
}


function clearErrors()
{
	errorinfo.errorList = new Array();
	errorinfo.errorObjRefList = new Array();
}

function clearErrorField(obj)
{	
	obj = obj.parentNode;
	obj.style.backgroundColor = "#fff";
}

function cleanshowlist()
{
	var cleanobj = "";
	
	for (i = 0; i < 3; i++)
	{
		cleanobj = document.getElementById('list'+i);
		if ( cleanobj != null )
			cleanobj.style.display = 'none';
	}
}


/** LOGIN, REGISTER, FORGOT PASSWORD */

function validationLogin() 
{ 
	clearErrors();
	validateemail("id_email", errorMsgList.getMsg("id_email"));
	validateString("id_password", errorMsgList.getMsg("password"));
 	document.logform.action='my/loginuser.action';

	return showErrorsLogin();
}

function forgotpassword()
{
	var value = document.getElementById('id_email').value;
	var parameter = "?"+value.replace(/^\s+|\s+$/g,"");
	var obja = document.getElementById('sendemail');
	obja.href ="min_zett/glemt_passord.html"+parameter;
}

function setemail()
{
	var temp = location.href;
	var email = temp.substring(temp.lastIndexOf("?")+1);
	email = email.replace(/%C3%A5/g,'å');
	email = email.replace(/%C3%B8/g,'ø');
	email = email.replace(/%C3%A6/g,'æ');
	if (email.match(/\//g)) 
		email = "";

 	document.getElementById('email').value = email;
}

/** SOLUTION FOR ENTER KEY PRESS LOGIN */

//global variable for save lastkeypress
var lastkeypress = new whichkeypress();

function whichkeypress() {
	this.login = 0;
	this.register = 0;
	this.idfield = "";
	this.idfieldnext = "";
}

function loginfocus(e)
{	
	clearerrorslogin();
	var keypress = 0;
	if(window.event) // IE/Google
  	{
  		keypress = event.keyCode;
  	}
	else if(e.which) // Netscape/Firefox/Opera
  	{
  		keypress = e.which;
  	}
	if ( keypress == 13 ) {
		if ( lastkeypress.login == 40 || lastkeypress.login == 38 ) {
			/*no submit, but get history*/
		} else {
			var obj = document.getElementById(getidfield());
			if (obj.value == "") {
				obj.focus();
			} else {
				var objnext = document.getElementById(getidfieldnext());
				objnext.focus();
			}
		}
	}
	lastkeypress.login = keypress;
}

function edituserfocus(e)
{	
	var keypress = 0;
	if(window.event) // IE/Google
  	{
  		keypress = event.keyCode;
  	} 
  	else if(e.which) // Netscape/Firefox/Opera
  	{
  		keypress = e.which;
  	}
	if ( keypress == 13) {
		if ( lastkeypress.lastkey == 40 || lastkeypress.lastkey == 38 )  {
			/*no submit, but get history*/
		} else {
			var obj = document.getElementById(getidfield());
			if (obj.value == "") {
				obj.focus();
			} else {
				var objnext = document.getElementById(getidfieldnext());
				objnext.focus();
			}
		}
	}
	lastkeypress.lastkey = keypress;	
}

function registerfocus(e)
{	
	clearerrorslogin();
	var keypress = 0;
	if(window.event) // IE/Google
  	{
  		keypress = event.keyCode;
  	} 
  	else if(e.which) // Netscape/Firefox/Opera
  	{
  		keypress = e.which;
  	}
	if ( keypress == 13) {
		if ( lastkeypress.register == 40 || lastkeypress.register == 38 )  {
			/*no submit, but get history*/
		} else {
			var obj = document.getElementById(getidfield());
			if (obj.value == "") {
				obj.focus();
			} else {
				var objnext = document.getElementById(getidfieldnext());
				objnext.focus();
			}
		}
	}
	lastkeypress.register = keypress;	
}

function setidfield(id,idnext)
{
	lastkeypress.idfield = id;
	lastkeypress.idfieldnext = idnext;
}
function getidfield()
{	
	return lastkeypress.idfield;
}
function getidfieldnext()
{	
	return lastkeypress.idfieldnext;
}

/** END SOLUTION FOR ENTER KEY PRESS LOGIN */