/**
* Javascript class to support dynamic form handling
*/

function FormSupport()
{
	this.DOM = false;
	this.MSIE4 = false;
	this.NS4 = false;

	if (document.getElementById) {
		this.DOM = true;
	} else {
		if (document.all) {
		    this.MSIE4 = true;
		} else {
			if (document.layers) {
				this.NS4 = true;
			}
		}
	}
}

/**
* Searches for Elements where the "Mode" has a the "Identifier" as value.
* Supported modes are "id", "name", "tagname"
* 
* Returns an Array of Elements
*/
FormSupport.prototype.getElementList  = function(Mode, Identifier) {
  var Element, ElementList;
  if (this.DOM) {
    if (Mode.toLowerCase() == "id") {
      Element = document.getElementById(Identifier);
      ElementList = new Array();
      if (Element) {
        return new Array(Element);
      }
    }
    if (Mode.toLowerCase() == "name") {
      return document.getElementsByName(Identifier);
    }
    if (Mode.toLowerCase() == "tagname") {
      return document.getElementsByTagName(Identifier);
    }
  }
  if (this.MSIE4) {
    if (Mode.toLowerCase() == "id" || Mode.toLowerCase() == "name") {
      Element = document.all(Identifier);
      if (Element) {
        return new Array(Element);
      }
    }
    if (Mode.toLowerCase() == "tagname") {
      return document.all.tags(Identifier);
    }
  }
  if (this.NS4) {
    if (Mode.toLowerCase() == "id" || Mode.toLowerCase() == "name") {
      Element = document[Identifier];
      if (!Element) {
        Element = document.anchors[Identifier];
      }
      if (Element) {
        return new Array(Element);
      }
    }
  }
  return new Array();
}

/**
* Returns the attribute value of an element as String
*/
FormSupport.prototype.getAttributeForElement = function(Element, AttributeName) {
  var Attribute;
  if (this.DOM || this.MSIE4) {
    Attribute = Element.getAttribute(AttributeName);
    return Attribute;
  }
  if (this.NS4) {
    Attribute = Element[AttributeName]
    if (!Attribute) {
       Attribute = false;
    }
    return Attribute;
  }
  return false;
}

/**
* Searches a form element where the given path is used as
* argument in a javascript function in the action attribute
*/
FormSupport.prototype.findForm = function(path)
{
	// find forms
	var aElements = this.getElementList("tagname","form");
	for (var a = 0; a < aElements.length; a++)
	{
		var action = this.getAttributeForElement(aElements[a], "action");
		if (action) {
			var pathPosition = action.indexOf("'" + path + "'");
			if (pathPosition > -1) {
				return aElements[a];
			}
		}
	}
}

/**
* Rewrites the action of a form to open a portlet
* which can display the given path and submits the form
*/
FormSupport.prototype.submit = function(path)
{
	// find form
	var theForm = this.findForm(path);
	if (theForm) {
		var action = this.getSelectedChildElementValue(theForm);
		if (action) {
			document.location.href = action;
		} else {
			theForm.action = path;
			theForm.submit();
		}
		return;
	}
}

/**
* searches for a selected child element (not only direct children) and returns
* the value of this child
*/
FormSupport.prototype.getSelectedChildElementValue = function(obj)
{
	for (var i = 0; i < obj.childNodes.length; i++) {
		var child = obj.childNodes[i];
		if (child.nodeName.toLowerCase() == "select") {
			for (var j = 0; j < child.options.length; i++) {
				if (child.options[i].selected) {
					return child.options[i].value;
				}
			}
		} else {
			var result = this.getSelectedChildElementValue(child);
			if (result) {
				return result;
			}
		}
	}
	return false;
}

/**
* Validates the flights search form
*/
FormSupport.prototype.validateHotels = function(form)
{
	return true;
}

/**
* Validates the flights search form
*/
FormSupport.prototype.validateFlights = function(form)
{
	return true;
}

/**
* checks if all fields are filled
*/
FormSupport.prototype.hasMissingEntries = function(form)
{
	for (i = 0; i < form.elements.length; ++i) {
		if (form.elements[i].value == "") {
			alert("Please fill out field " + this.fieldMapper(form.elements[i].name));
			form.elements[i].focus();
			return true;
		}
	}
	return false;
}

/**
* checks if all fields in the Array are filled
*/
FormSupport.prototype.hasMissingField = function(form, fields)
{
	for (i = 0; i < fields.length; ++i) {
		if (form[fields[i]]) {
			if (!form[fields[i]].value || form[fields[i]].value == "") {
				alert("Please fill out field " + this.fieldMapper(form[fields[i]].name));
				form[fields[i]].focus();
				return true;
			}
		}
	}
	return false;
}

/**
* checks if all fields in the Array are filled
*/
FormSupport.prototype.notChecked = function(form, fields)
{
	for (i = 0; i < fields.length; ++i) {
		if (form[fields[i]]) {
			if (!form[fields[i]].checked) {
				alert("Please check field " + this.fieldMapper(form[fields[i]].name));
				form[fields[i]].focus();
				return true;
			}
		}
	}
	return false;
}

/**
* checks if two fields are different
*/
FormSupport.prototype.differ = function(form, field1, field2)
{
	if (form[field1].value != form[field2].value) {
		alert("Password confirmation failed");
		form[field1].focus();
		return true;
	}
	return false;
}

FormSupport.prototype.fieldMapper = function(name) {
	if (name == "DEPPORT") return "Departing";
	if (name == "ARRPORT") return "Destination";
	return name;
}

FormSupport.prototype.toggleCheckbox = function(id) {
    var checkbox=document.getElementById(id);
    if (checkbox) {
    	checkbox.checked = !checkbox.checked;
    }
}

/**
* Validates the flights search form
*/
FormSupport.prototype.validateSignup = function(form)
{
	var requiredFields = new Array("email",
		"password",
		"confirmpassword",
		"title",
		"firstname",
		"lastname",
		"state");
	if (this.hasMissingField($(form), requiredFields)) {
		return false;
	}
	if (this.notChecked($(form), new Array("agreement"))) {
		return false;
	}
	if (this.differ($(form), "password", "confirmpassword")) {
		return false;
	}

	return true;
}
/**
* creates the FormSupport object
*/

var formSupport = new FormSupport();

