// JavaScript Document
var displaycookie = {
	
	//Initialize and call other functions
	init : function () {
	  if(displaycookie.checkCookies() != -1) {return;}
		var fakeSubmit = document.getElementById('voteButton');
		displaycookie.addEvent(fakeSubmit, 'click', displaycookie.returnData);
		
	},
	
	checkCookies : function() {
  	if(!navigator.cookieEnabled) {
			var warning = document.createElement('div');
			warning.appendChild(document.createTextNode('Cookie Support is Required'));
			warning.id = 'warning';
			var theBody = document.getElementsByTagName('body')[0];
			var firstHeading = document.getElementsByTagName('h1')[0];
			displaycookie.insertAfter (theBody,warning,firstHeading);
			return 0;
		} else {return -1;}
	},
	
	returnData : function () {
		var theInputs = document.getElementsByTagName('input');
    var theSelects = document.getElementsByTagName('select');
    for (var i = 0; i<theInputs.length-1; i++) {
			displaycookie.createCookie (theInputs[i].name, theInputs[i].value);
		}
		for (i = 0; i<theSelects.length; i++) {
			displaycookie.createCookie (theSelects[i].name, theSelects[i].value);
		}
		window.open('lab5results.html','_blank','top=40,left=40,width=400,height=400,scrollbars=1,resizable=1,location=1');
	},
	
	
	insertAfter : function(parent, nodeToInsert, referenceNode) {
  	parent.insertBefore(nodeToInsert, referenceNode.nextSibling);
	},
	
	createCookie : function (name,value,expiration,path,domain,secure) {
  	var data = name + "=" + escape(value);
  	if (expiration) { data += "; expires=" + expiration.toGMTString(); }
  	if (path) { data += "; path=" + path; }
  	if (domain) { data += "; domain=" + domain; }
  	if (secure) { data += "; secure"; }
  	document.cookie = data;
	},
	
	// associating nodes with events and functions
   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;}
   }
}

displaycookie.addEvent(window, 'load', displaycookie.init);   