/*
 * Determining browser, version and platform
 *
 */

var isNav = false;
var isNav5 = false;
var isNav4 = false;
var isIE = false;
var isIE5 = false;
var isIE4 = false;
var isWin = false;
var isMac = false;

var appName = navigator.appName;
var appVersion = navigator.appVersion;
if (appName == "Netscape") {
  isNav = true;
  if (parseInt(appVersion) == 5)
    isNav5 = true;  
  else if (parseInt(appVersion) == 4)
    isNav4 = true;
}
else if (appName == "Microsoft Internet Explorer") {
  isIE = true;
  if (appVersion.indexOf("MSIE 5") != -1 || appVersion.indexOf("MSIE 6") != -1)
    isIE5 = true;
  else if (appVersion.indexOf("MSIE 4") != -1)
    isIE4 = true;
}

var platform = navigator.platform;
if (platform.indexOf("Win") != -1)
  isWin = true;
else if (platform.indexOf("Mac") != -1)
  isMac = true;





// menuhandling --------------------------------------


topMenuOffColor = "8B001C";
topMenuOnColor = "AF0040";

function topMenuOn(tdElem) {
    tdElem.style.backgroundColor = topMenuOnColor;
}

function topMenuOff(tdElem) {
    tdElem.style.backgroundColor = topMenuOffColor;
}



// common functions ---------------------------------

function goTo(location) {
    document.location.href=location;
}

function openPrintWindow(url) {
    var printWin = window.open(url, "_blank", "width=640,height=500,scrollbars,resizable,menubar,toolbar");
    //if (isIE)
        //printWin.print();
}

function openPolyWindow(url) {
    var polyWin = window.open(url, "polopoly", "width=800,height=480,fullscreen=no,scrollbars=yes,resizable=yes,menubar=yes,toolbar=no,status=yes,directories=no,");
    polyWin.focus();
}


/*
-------------------------------------------------------------------------------------------------------------------------
function for constructing VALIDATION FIELDS:
  obj = createValidationField(String formname, String elementname, String elementtype, String description, Boolean isRequired)

String elementtype can be any of the following (others not implemented in this package):
  "alphanumeric", any content is ok unless field is empty or only whitespace
  "email", content must be of e-mail format.
  
createValidationField returns an object. You may set maxLength and minLength on this object:
  obj.minLength / obj.maxLength
If either or both is set, these conditions will automatically be validated.
*/

var formObjects = new Array();
// formObject
function formObject(formname) {
    this.validationObjects = new Array();
    this.obj = document.forms[formname];
}
formObject.prototype.addValidationObject = formObject_addValidationObject;
function formObject_addValidationObject(valObj) {
    this.validationObjects[this.validationObjects.length] = valObj;
    return(valObj);
}

// the form validation functions (calls each belonging fieldobjects isValid/testCondition-function)
function validate(formname) {
  var errors = "";
  for(var i=0;i<formObjects[formname].validationObjects.length;i++)
    errors += formObjects[formname].validationObjects[i].isValid();
  if (errors == "") // no errors
    return(true); 
  else { // errors found
    alert(errors);
    if(window.event) window.event.returnValue = false; 
    return(false);
  }
  return(false);
}


// constructor function for validation fields
function createValidationField(formname, elementname, elementtype, description, isRequired) {
    if (formObjects[formname] == null) formObjects[formname] = new formObject(formname);
    return(formObjects[formname].addValidationObject((eval("new "+elementtype+"Field((document.forms[formname].elements[elementname]),\""+ description+"\", isRequired)"))));
}

// constructor function for conditions
function createCondition() {
    var type = createCondition.arguments[0];
    var errormessage = createCondition.arguments[1];
    var args = new Array();
    for (i=0;i<createCondition.arguments.length-2;i++) {
        args[i] = createCondition.arguments[i+2];
    }
    return(formObjects[args[0].obj.form.name].addValidationObject(eval("new "+type+"Condition(errormessage, args)")));
}

/* the fieldclass , all validationobjects of type field inherit this class   */
function fieldClass (elementobj, description, isRequired) {
  this.obj = elementobj;
  this.description = description;
  this.isRequired = isRequired;
  this.maxLength = null;
  this.minLength = null;
}
function obj_isEmpty() {
  if (this.obj.value.leftTrim().length == 0) {
    if (this.isRequired)
      return("- Du har glömt att fylla i "+this.description+"\n");
    else
      return("");
  }
  return(-1);
}
fieldClass.prototype.isEmpty = obj_isEmpty;

function obj_isOfCorrectLenght() {
  if (this.maxLength != null) {
    if (this.obj.value.length > this.maxLength)
        return("- "+this.description+" får som mest vara "+ this.maxLength + " tecken långt. Du har skrivit " + this.obj.value.length + " tecken.\n");
  }
  else if (this.minLength != null) {
    if (this.obj.value.length < this.minLength)
        return("- "+this.description+" skall inte vara kortare än "+ this.minLength + " tecken. Du har skrivit " + this.obj.value.length + " tecken.\n");
  }
  return(-1);
}
fieldClass.prototype.isOfCorrectLenght = obj_isOfCorrectLenght;

/* --------------------------------------------------------- */

function emailField(e, d, r) { this.base = fieldClass; this.base(e, d, r); }
emailField.prototype = new fieldClass;
emailField.prototype.isValid = emailField_isValid;
function emailField_isValid() {
  if(this.isEmpty() != -1)
    return (this.isEmpty());
  if (this.obj.value.search(/(\w|\.|\-)+\@(\w|\.|\-)+\.\w\w+/))
     return("- "+this.obj.value+" är inte en giltig e-postadress\n");
  if(this.isOfCorrectLenght() != -1)
    return (this.isOfCorrectLenght());
  return("");
}

function alphanumericField(e, d, r) { this.base = fieldClass; this.base(e, d, r); }
alphanumericField.prototype = new fieldClass;
alphanumericField.prototype.isValid = alphanumericField_isValid;
function alphanumericField_isValid() {
  if(this.isEmpty() != -1)
    return (this.isEmpty());
  if(this.isOfCorrectLenght() != -1)
    return (this.isOfCorrectLenght());
  return("");
}

/************ stringfunktioner */

// append leftTrim to String object
function String_leftTrim() {
  var i = 0;
  while(i < this.length && this.charAt(i) == ' ') {
    i++;
  }
  return this.substr(i);
}
String.prototype.leftTrim = String_leftTrim;

// append rightTrim to String object
function String_rightTrim() {
  var i = this.length-1;
  while(i > 0 && this.charAt(i) == ' ') {
    i--;
  }
  return this.substr(0, i+1);
}
String.prototype.rightTrim = String_rightTrim;

// append trimAll to String object (trims all whitespace in string)
function String_trimAll() {
  var i = 0;
  var temp = "";
  while(i < this.length) {
    if (this.charAt(i) != ' ')
        temp += this.charAt(i);
    i++;
  }
  return temp;
}
String.prototype.trimAll = String_trimAll;




