function checkLoginForm2(form) {
	// check the required fields
	if (form.usr_name != null && form.usr_name.value != '' && form.usr_password.value != '') 	{
		if (form.saveusername.checked) 
			setCookie('loginusername', form.usr_name.value, 1000*60*60*24*365);
		else
			setCookie('loginusername', '', 1000*60);  // delete the user cookie

		form.HiddenURI.value = form.HiddenURI.value.replace('%26usr_name%3D', '%26usr_name%3D' + form.usr_name.value) + '&usr_name=' + form.usr_name.value;
		form.RedirectOnFailure.value = form.RedirectOnFailure.value.replace('%26usr_name%3D', '%26usr_name%3D' + form.usr_name.value) + '&usr_name=' + form.usr_name.value;

		// set the service (shop checkout only)
		if (document.serviceForm != undefined) {
			for (i = 0; i < document.serviceForm.elements.length;i++) {
				if (document.serviceForm.elements[i].checked) {
					form.HiddenURI.value = form.HiddenURI.value.replace(escape('%26formelement%3D1801351'), escape('%26formelement%3D1801351%26formpartcode%3Dservice%26service%3D' + document.serviceForm.elements[i].value));
				}
			}
		}

		form.submit();
	} else {
		alert('Uw gebruikersnaam en/of wachtwoord is niet ingevuld. Probeer het nogmaals.');
		return false;
	}
}

function onFocusField(field, defaultValue) {
	if ((field != null) && (field.value != '') && (field.value == defaultValue)) 
		field.value = '';
	return true;
}

function initializeLoginForm() {
	if (document.login && document.login.usr_name && document.login.saveusername) {
		
		
		if (getCookie('loginusername') != '') {
			document.login.usr_name.value = getCookie('loginusername');
			document.login.saveusername.checked = true;
		}
	}
}


/**
 *  Apply personalization clientside using cookies.
 *
 *  Copyright (c) 2007 by <GX> creative online development B.V.
 *  All rights reserved.
 *
 *  <GX>: Open for business
 *  http://gx.nl/
 *
 *  Contains code by Jonathan Snook (http://www.snook.ca/jonathan),
 *  Robert Nyman (http://www.robertnyman.com) and Peter-Paul Koch
 *  (http://www.quirksmode.org/js/cookies.html), thanks for sharing!
 *
 *  @author   StijnW <Stijn.de.Witt@gx.nl>
 *  @date     2007/02/16
 *  @desc     Site20 - Restyle KPN.com
 *
 *  @author   StijnW <Stijn.de.Witt@gx.nl>
 *  @date     2007/06/04
 *  @desc     KPN-1357 [8742]
 *            Added field type to allow checking for user type
 *
 *  @author   StijnW <Stijn.de.Witt@gx.nl>
 *  @date     2007/06/29
 *  @desc     KPN-1368 [8842]
 *            Added calls to encode- / decodeURIComponent to
 *            encode/decode UTF-8 chars
 *
 *  @author   StijnW <Stijn.de.Witt@gx.nl>
 *  @date     2007/12/21
 *  @desc     KPN-1509 [9191]. Added field session and a check to
 *            verify that it matches the value in cookie JSESSIONID
 *            to synchronize the server session with the client session. 
 *
 *  USAGE
 *
 *  1. Create an instance of the WebUser javascript 'class' at the top of your
 *     document (in the head section):
 *
 *       <script type="text/javascript">
 *         var webUser = new WebUser();
 *       </script>
 *     </head>
 *
 *  2. Mark HTML elements that should be (in)visible only when the user is
 *     logged in with the WEBUSER_LOGGEDIN_CLASS or WEBUSER_NOTLOGGEDIN_CLASS:
 *
 *     <body>
 *       <form name="login" class="webUser-notLoggedIn">
 *         ...
 *       </form>
 *
 *  3. Use special WebUser attribute marker comments within your HTML to have
 *     certain attributes of the logged-in webuser filled in there. Only marker
 *     comments within elements with WEBUSER_LOGGEDIN_CLASS will be filled in.
 *
 *     <div class="webUser-loggedIn">
 *       Thanks for logging in, <!-- [webUser.name] -->.<br />
 *       You are registered as <!-- [webUser.initials] --> <!-- [webUser.surname] -->.
 *     </div>
 *
 *  4. Add a call to applyPersonalization at the bottom of your document
 *     (just before the close of the body tag):
 *
 *       <script type="text/javascript">
 *         webUser.applyPersonalization();
 *       </script>
 *     </body>
 *
 *  MARKER COMMENTS
 *
 *  The following marker comments are available:
 *
 *  <!-- [webUser.id] -->
 *  <!-- [webUser.name] -->
 *  <!-- [webUser.title] -->
 *  <!-- [webUser.initials] -->
 *  <!-- [webUser.middlename] -->
 *  <!-- [webUser.surname] -->
 *  <!-- [webUser.gender] -->
 *  <!-- [webUser.type] -->
 */

// Defaults may be overruled by passing arguments to WebUser constructor
var WEBUSER_COOKIENAME = "userinfo";
var WEBUSER_LOGGEDIN_CLASS = "webUser-loggedIn";
var WEBUSER_NOTLOGGEDIN_CLASS = "webUser-notLoggedIn";
var WEBUSER_SESSIONCOOKIENAME = "JSESSIONID";


/* The following arrays define which div ID's to switch on & off - for login/logout */
var WEBUSER_ARROFF = new Array("loginWelcome");
var WEBUSER_ARRON = new Array("loginWindow");
var WEBUSER_ARRLOGINOFF = new Array("loginWelcome", "loginWindow", "previouslyViewed", "loginPanel");
var WEBUSER_ARRLOGINON = new Array("loggedInPanel", "LoginTop", "loginBottom", "userName");
var WEBUSER_ARRLOGOUTOFF = new Array("loggedInPanel", "LoginTop", "loginBottom", "userName");
var WEBUSER_ARRLOGOUTON = new Array("loginWelcome");


// WebUser 'class' constructor
function WebUser(cookieName, sessionCookieName) {
  this.cookieName = cookieName != null ? cookieName : WEBUSER_COOKIENAME;
  this.sessionCookieName = sessionCookieName != null ? sessionCookieName : WEBUSER_SESSIONCOOKIENAME;
  
  // Cookie helper functions, thanks Peter-Paul Koch!
  this.loadCookie = function(cookieName) {
    var nameEQ = cookieName + "=";
    var cookies = document.cookie.split(';');
    for(var i=0; i<cookies.length; i++) {
      var c = cookies[i];
      while (c.charAt(0) == ' ')
        c = c.substring(1, c.length);
      if (c.indexOf(nameEQ) == 0) {
        var encoded = c.substring(nameEQ.length, c.length);
        encoded = encoded.replace(/\+/ig, "%20");
        encoded = decodeURIComponent(encoded);
        return encoded;
      }
    }
    return null;
  };

  this.saveCookie = function(name, value, days, path) {
    var expires = "";
    if (days) {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      expires = "; expires=" + date.toGMTString();
    }
    if (! path) path = "/";
    document.cookie = name + "=" + encodeURIComponent(value).replace(/%20/ig, "+") + expires + "; path=" + path;
  };
  

  this.removeCookie = function(name) {
    this.saveCookie(name, "", -1);
  };
  // /Cookie helper functions, thanks Peter-Paul Koch!

  this.clear = function() {
    this.id = -1;
    this.welcome_name = null;
    this.name = null;
    this.title = null;
    this.initials = null;
    this.middlename = null;
    this.surname = null;
    this.gender = -1;
    this.type = -1;
    this.session = null;
    this.prod = null;
  };

  this.isLoggedIn = function() {
    return ((this.name != null) && (this.id != -1));
  };
  
  this.isNotLoggedIn = function() {
    return ((this.name == null) || (this.id == -1));
  };
  
  this.load = function() {
    // cookieFormat:  user_id=<userId>#user_name=<userName>#title=<title>#initials=<initials>#surname=<surname>#gender=<gender>#session=<jsessionid>
    // eg:            "user_id=234#user_name=PietJ#title=ing#initials=P.W.H.#surname=Joosten#gender=0#session=7AE5F43B86CE59A76B4CE33A2"
    //                "user_id=345#user_name=RitaV#title=drs#initials=R.#surname=Verdonk#gender=1#session=9A8FE7B2BA34C6FE5FC324762"
    var cookieValue = this.loadCookie(this.cookieName);

    if (cookieValue != null) {
      var values = cookieValue.split("#");

      for (var i=0; i<values.length; i++) {
        var nameValuePair = values[i].split("=");
        if (nameValuePair.length == 2) {
          var attrName = nameValuePair[0];
          var attrValue = nameValuePair[1];

          // security measure
		  attrValue = attrValue.replace(/</g,"&lt;");
		  attrValue = attrValue.replace(/>/g,"&gt;");

          if (attrName == "user_id")
            this.id = attrValue != '' ? parseInt(attrValue) : -1;
          else if (attrName == "welcome_name")
            this.welcome_name = attrValue != '' ? attrValue : null;
          else if (attrName == "user_name")
            this.name = attrValue != '' ? attrValue : null;
          else if (attrName == "title")
            this.title = attrValue != '' ? attrValue : null;
          else if (attrName == "initials")
            this.initials = attrValue != '' ? attrValue : null;
          else if (attrName == "middlename")
            this.middlename = attrValue != '' ? attrValue : null;
          else if (attrName == "surname")
            this.surname = attrValue != '' ? attrValue : null;
          else if (attrName == "gender")
            this.gender = attrValue != '' ? parseInt(attrValue) : -1;
          else if (attrName == "user_type")
            this.type = attrValue != '' ? parseInt(attrValue) : -1;
          else if (attrName == "session")
            this.session = attrValue != '' ? attrValue : null;
          else if (attrName == "prod")
            this.prod = attrValue != '' ? attrValue : null;
        }
      }
    }
    
    // Check if server-side session is still valid.
    var sessionId = this.loadCookie(this.sessionCookieName);
    if ((sessionId == null) || ((this.session != null) && (sessionId.indexOf(this.session) == -1))) {
      // Session is not valid, clear this object's state and remove associated cookie
      //this.clear();
      //this.remove();
    }
  };
  
  this.save = function() {
    // cookieFormat:  user_id=<userId>#user_name=<userName>#title=<title>#initials=<initials>#surname=<surname>#gender=<gender>#session=<jsessionid>
    // eg:            "user_id=234#user_name=PietJ#title=ing#initials=P.W.H.#surname=Joosten#gender=0#session=7AE5F43B86CE59A76B4CE33A2"
    //                "user_id=345#user_name=RitaV#title=drs#initials=R.#surname=Verdonk#gender=1#session=9A8FE7B2BA34C6FE5FC324762"
    var payload = "";
    payload += "user_id=" + (this.id != -1 ? this.id : "");
    payload += "#user_name=" + (this.name != null ? this.name : "");
    payload += "#welcome_name=" + (this.welcome_name != null ? this.welcome_name : "");
    payload += "#title" + (this.title != null ? this.title : "");
    payload += "#initials=" + (this.initials != null ? this.initials : "");
    payload += "#surname=" + (this.surname != null ? this.surname : "");
    payload += "#gender=" + (this.gender != -1 ? this.gender : "");
    payload += "#user_type=" + (this.type != -1 ? this.type : "");
    payload += "#middlename=" + (this.middlename != null ? this.middlename : "");
    payload += "#session=" + (this.session != null ? this.session : "");
    payload += "#prod=" + (this.prod != null ? this.prod : "");

    this.saveCookie(this.cookieName, payload);
  };
  
  this.remove = function() {
    this.removeCookie(this.cookieName);
  };
  
  this.setElements = function(eleOff, eleOn) {
	  	/* This function turns div's ON/OFF by ID using the display CSS property
		- it requires an array of IDs to switch on & another array of IDs to switch off */
		
		for (var offI=0; offI<eleOff.length; offI++)
		{
			var theElement = document.getElementById(eleOff[offI]);
			if (theElement != null){
				theElement.style.display = "none";
			}
		}
		
		for (onI=0; onI<eleOn.length; onI++)
		{
			var theElement = document.getElementById(eleOn[onI]);
			if (theElement != null){
				theElement.style.display = "block";
			}	
		}
  };
  
  this.setloginwindow = function() {
	  this.setElements(WEBUSER_ARROFF, WEBUSER_ARRON);
  };
  
  this.setloginparams = function() { 		
	  this.setElements(WEBUSER_ARRLOGINOFF, WEBUSER_ARRLOGINON);	  
	  var theUserName = document.getElementById("loggedInPanel");
	  theUserName.innerHTML = theUserName.innerHTML.replace(/<!-- \[webUser\.welcome_name\] -->/g, this.welcome_name == null ? "" : this.welcome_name);
	  theUserName.innerHTML = theUserName.innerHTML.replace(/<!-- \[webUser\.name\] -->/g, this.name == null ? "" : this.name);
  };
  
  this.setmetanavparams = function(){
 	var theMetaNav = document.getElementById("metanav");
	  
	if (theMetaNav != null) {
	  theMetaNav.innerHTML = theMetaNav.innerHTML.replace(/<!-- \[webUser\.welcome_name\] -->/g,this.welcome_name == null ? "" : this.welcome_name);
	  theMetaNav.innerHTML = theMetaNav.innerHTML.replace(/<!-- \[webUser\.name\] -->/g,this.name == null ? "" : this.name);

	  if (this.prod != null ? this.prod : "") {
		  var values = this.prod.split(",");
		  for (var i=0; i<values.length; i++) {
	        var attrValue = values[i];
	        var iconId = document.getElementById(attrValue);
	        if (iconId != null){
	      	  var classSplits = iconId.className.split("DisplayNone");
	          var newClass = "";
	          for (var x=0; x<classSplits.length; x++) {
	        	if (classSplits[x] != "DisplayNone"){
	        	  newClass = newClass + classSplits[x];
	        	}
	          }
	          iconId.className = newClass;
	        }
         }
	  }
	}
  };
  
  this.setlogoffparams = function() {
	  this.setElements(WEBUSER_ARRLOGOUTOFF, WEBUSER_ARRLOGOUTON);
  }; 
  
  
  this.applyPersonalization = function() {
	this.setmetanavparams();
    if (this.isLoggedIn()) { this.setloginparams(); }
    else { this.setlogoffparams(); }
  };

  this.clear();
  this.load();
};





/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
    http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
*/
function getElementsByClassName(oElm, strTagName, strClassName) {
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++) {
        oElement = arrElements[i];
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements);
};

