var util = {

  W3CDOM : document.getElementById && document.getElementsByTagName && document.getElementsByName && document.createElement && document.createTextNode,
  myForm : null,
  required : null,
  alts: null,
  
 init: function() {
 	//get the form we want to check using the id
	util.myForm = document.getElementById('form1');
	util.addEvent(util.myForm, 'submit', util.checkForm);
 },
 
 //check form for required fields
  checkForm: function(e) {
  	var inputs = util.myForm.getElementsByTagName('input');
  	var numInputs  = inputs.length;
  	util.required = new Array();
  	util.alts = new Array();
  	var msg = "";
  	for (var i=0; i < numInputs; i++) {
  		//any form input that has req as a class name will each be checked
  		if (/req/.test(inputs[i].className)) {
  			util.required.push(inputs[i]);
  			if (inputs[i].value == "") {
  				var label = util.required[i].parentNode.firstChild.nodeValue.replace(/^\s*/, '').replace(/\s*$/, '').replace(new RegExp(":"),'');
  				msg += label + " is required. \n";
  			}
  		}
  		//any form input that has alt1 as a class name will be checked to see that at least one of them has data in it
   		if (/alt1/.test(inputs[i].className)) {
   			util.alts.push(inputs[i]);
  		}
  	}
  	var numAlts = util.alts.length;
  	var altComp = false;
  	for (var i=0; i < numAlts; i++) {
  		var value = util.alts[i].value.replace(/^\s*/, '').replace(/\s*$/, '');
  		if (value != "") {
  			altComp = true;
  		}
  	}
  	if (!altComp) {msg += "At least one contact method is required.\n";}
   	if (msg != "") { //there is an error message so popup the error and do not submit form
   		alert(msg);
   		util.stopDefault(e);
   	}
 },
 
 // utility function for setting cookies
  createCookie : function(name,value) {
    var expiration = new Date();
    var duration = expiration.getTime() + (30 * 24 * 60 * 60 * 1000);
    expiration.setTime(duration);    
    var data = name + "=" + escape(value) + "; expires=" + expiration.toGMTString();
    document.cookie = data;
  },  

  // utility functions for retrieving cookie values
  findCookie : function(name) {
    var query = name + "=";
    var queryLength = query.length;
    var cookieLength = document.cookie.length;
    var i=0;
    while (i < cookieLength) {
      var startValue = i + queryLength;
      if (document.cookie.substring(i,startValue) == query) {
         return this.findValue(startValue);
      }
      i = document.cookie.indexOf(" ", i) + 1;
      if (i == 0) {break;}
    }
    return null;
  },

  findValue : function(startValue) {
    var endValue = document.cookie.indexOf(";", startValue);
    if (endValue == -1) {
       endValue = document.cookie.length;
    }
    return unescape(document.cookie.substring(startValue,endValue));
  },
    
  // utility function for stopping structural markup default behavior; disables link hrefs and form submissions in this case
  stopDefault : function(e) {
     if (!e) {e = window.event;}
     if (!e.preventDefault) {
         e.preventDefault = function() { this.returnValue = false; }
     }
     e.preventDefault();
     return false;
  },

  // utility function for adding events
  addEvent : function(obj, type, func) {
    if (obj.addEventListener) {obj.addEventListener(type, func, false);}
    else if (obj.attachEvent) {
      obj["e" + type + func] = func;
      obj[type + func] = function() {obj["e" + type + func] (window.event);}
      obj.attachEvent("on" + type, obj[type + func]);
    }
    else {obj["on" + type] = func;}
  },

  // utility functions for AJAX calls
  sendRequest : function(url,func,postData) {
    var xhr = organizer_util.createXMLHttpObject();
    if (!xhr) { return; }
    var method = (postData) ? "POST" : "GET";
    xhr.open(method, url, true);
    xhr.setRequestHeader('User-Agent','XHR');
    if (postData) { xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); }
    xhr.onreadystatechange = function() {
      if (xhr.readyState != 4) { return; }
      if (xhr.status != 200 && xhr.status != 304) {
         alert('HTTP error ' + xhr.status);
         return;
      }
      func(xhr);
    }
    if (xhr.readyState == 4) { return; }
    xhr.send(postData);
  },

  XHRoptions : [
   function () {return new XMLHttpRequest()},
   function () {return new ActiveXObject("Msxml2.XMLHTTP")},
   function () {return new ActiveXObject("Msxml3.XMLHTTP")},
   function () {return new ActiveXObject("Microsoft.XMLHTTP")}
  ],

  createXMLHttpObject : function() {
    var xmlhttp = false;
    for (var i=0, allOptions=util.XHRoptions.length; i<allOptions; i++) {
      try { xmlhttp = util.XHRoptions[i](); }
      catch (e) { continue; }
      break;
    }
    return xmlhttp;
  }

}
// object detection and initializing functionality
if (util.W3CDOM) {
	util.addEvent(window, 'load', util.init);
}
