var itsFirstPwd = "";    

/* Validation Object constructor */  
function Validation( ){
  //methods
  this.validateForm = p_validateForm;
  this.validateField= p_validateField;
  this.append = p_append;
  this.reset = p_reset;
  
  //attributes
  this.form    = 0;
  this.continueValidation = true;
  this.alert = (typeof itsMessageBox != 'undefined') ? itsMessageBox.alert : alert;
  
  this.IGNORE  = new Array( "ignore" );
  this.NORMAL  = new Array( );
  this.PHONE   = new Array( "phone", "fax" );
  this.ZIP     = new Array( "zip" );
  this.EMAIL   = new Array( "email" );
  this.PASSWORD= new Array( "password", "pwd" );
  this.VOLUNTARY = new Array( "optional" );
  
  this.NORMAL_ERROR_STRING    = "Fältet måste ha mer än två tecken.";
  this.PHONE_ERROR_STRING     = "Numret är inte giltigt!";
  this.ZIP_ERROR_STRING       = "Postnumret är inte giltigt!";
  this.EMAIL_ERROR_STRING     = "Email adressen är inte giltig!";
  this.PASSWORD1_ERROR_STRING = "Lösenordet ska bestå av minst 6 tecken!";
  this.PASSWORD2_ERROR_STRING = "Lösenord överensstämmer inte, ange lösenord i båda fälten!";
  this.documentReference = document.forms[this.form];
  this.useReference = false;
}  
  
function p_validateField( theField ){

  if(theField.disabled) return;
  if(theField.type=="hidden") return; //don't validate hidden fields
  if(theField.type=="button") return; //obviously, ignore buttons
  if(theField.type=="image") return; //obviously, ignore images
  if(theField.options) return;
 
  var aFieldName = theField.name.toLowerCase();
  var aFieldType = new String();
  if(isType(this.IGNORE, aFieldName)){
    aFieldType = "IGNORE";
  }else if(isType(this.NORMAL, aFieldName)){
    aFieldType = "NORMAL";
  }else if(isType(this.PHONE, aFieldName)){
    aFieldType = "PHONE";
  }else if(isType(this.ZIP, aFieldName)){
    aFieldType = "ZIP";
  }else if(isType(this.EMAIL, aFieldName)){
    aFieldType = "EMAIL";
  }else if(isType(this.PASSWORD, aFieldName)){
    aFieldType = "PASSWORD";
  }else{
    aFieldType = "NORMAL";
  }
  var aFieldValue = theField.value;
  switch( aFieldType ){
    case "IGNORE" :
	  return true;
	  break;
    case "EMAIL" : //Must be a valid email address
	  if(isVolontary( this.VOLUNTARY,theField )) return true; 
      var aPattern = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
      if((aPattern.test( aFieldValue ) && aFieldValue.length < 70)){
        return true;
      }else{
        this.alert(this.EMAIL_ERROR_STRING, theField.focus);
        theField.focus();
        this.continueValidation = false;
        return false;
      }
      break;

    case "PASSWORD" : //password
	    if(isVolontary( this.VOLUNTARY,theField )) return true;
	  	if(aFieldValue.length > 5  && aFieldValue.length < 50){
		  if(itsFirstPwd != ""){
		    if(itsFirstPwd == aFieldValue){
			  return true;
			}else{
			  this.alert(this.PASSWORD2_ERROR_STRING)
			  theField.focus();
              this.continueValidation = false;
              return false;
			}
		  }else{
		    itsFirstPwd = aFieldValue;
		  }
		}else{
		  this.alert(this.PASSWORD1_ERROR_STRING );
          theField.focus();
          this.continueValidation = false;
          return false;
		}
	  break;

    case "PHONE" : //must only contain +, -, and numeric
	  if(isVolontary( this.VOLUNTARY,theField )) return true;
	  var aPattern = /^([0-9\s\+-\/])+$/;
      if((aPattern.test( aFieldValue ) && aFieldValue.length < 50) ){
        return true;
      }else{
        this.alert( this.PHONE_ERROR_STRING, theField.focus);
        theField.focus();
        this.continueValidation = false;
        return false;
      }
      break;

    case "NORMAL": 
	  if(isVolontary(this.VOLUNTARY,theField)) return true;
      if(aFieldValue.length > 1  && aFieldValue.length < 50 ){
        return true;
      }else{
        this.alert(this.NORMAL_ERROR_STRING, theField.focus);
	    
        theField.focus();

        this.continueValidation = false;
        return false;
      }
      break;
	  
	case "ZIP" : 
	  if(isVolontary( this.VOLUNTARY,theField )) return true;
	  while(theField.value.indexOf(" ") > -1)  theField.value = theField.value.replace(" ", "");
	  aFieldValue = theField.value;
  	  var aPattern = /^[0-9]+$/;
	  if(!aPattern.test( aFieldValue ) || aFieldValue.length < 1){
        this.alert(this.ZIP_ERROR_STRING , theField.focus);
        theField.focus();
        this.continueValidation = false;
        return false;
	  }else{
	    return true;
	  }
	  break;
	 default:
	   alert("OKÄND TYP: " + aFieldName)
  }
}



function p_validateForm( ){
  this.continueValidation = true;
  if(this.useReference) {
	  for(j=0; j<this.documentReference.elements.length; j++){
	    if(!this.continueValidation)
	      break;
	   this.validateField( this.documentReference.elements[j] );
	  }
  } else {
	  for(j=0; j<document.forms[this.form].elements.length; j++){
	    if(!this.continueValidation)
	      break;
	   this.validateField( document.forms[this.form].elements[j] );
	  }  
  }
  return this.continueValidation;
}

function p_append( theArray,  theValue ){
  theArr = eval( "this." + theArray );
  aWhat = (typeof theValue);
  if(aWhat == "object"){
    for(i=0; i<theValue.length; i++){
	  theArr[theArr.length] = theValue[i].toLowerCase();
	}
  }else if(aWhat == "string"){
    theArr[theArr.length] = theValue.toLowerCase();
  }
}

function p_reset( theArray ){
  if(theArray != "*"){
    theArr = eval( "this." + theArray + " =  new Array()" );
  }else{
    this.IGNORE  = new Array( );
    this.NORMAL  = new Array( );
    this.PHONE   = new Array( );
    this.ZIP     = new Array( );
    this.EMAIL   = new Array( );
    this.PASSWORD= new Array( );    
    this.OPTIONAL= new Array( );  	
  }
}

/* Methods not explicitly tied to the object */
function isType( aArr, sVal ){
  for(i=0; i<aArr.length; i++){
    if(sVal.indexOf(aArr[i])>-1)
	  return true;
  }
  return false;
} 
 
function isVolontary( aArr, theField ){
  for(i=0; i<aArr.length; i++){
    if( theField.name.toLowerCase().indexOf(aArr[i]) > -1 && theField.value.length==0)
      return true;
  }
  return false;
}

