//dynamic module by Basementia Design

function addEvent (object, userAction, func, useCapture){
			if(!document.getElementById || !document.createTextNode){return;}
			if(object.addEventListener){
				object.addEventListener(userAction, func, useCapture);
				return true;
			}else if(object.attachEvent){
					var result=object.attachEvent('on'+userAction, func);
					return result;
			}else{
					object['on'+userAction]=func;
			}

		}	

	
	function getTarget(e){
		var target;
		if(window.event){
			target = window.event.srcElement;
			}else if(e){
				target = e.target;
				
				}else{
					target = null;
					}
		return target;
		}
	
	function cancelClick(e){
		//alert('cancel that!');
		if(window.event && window.event.cancelBubble && window.event.returnValue){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
			return;
			}
		if(e && e.stopPropagation && e.preventDefault){
			e.stopPropagation();
			e.preventDefault();
			}
		}
	
	/*
	getTarget:function(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if(!target){return false;}
		return target;
	}
	*/
	addEvent(window,'load',initForm, false);
		
	function initForm(){
		if(!document.createElement){return;}
		
/*		
		var yesBtn = document.getElementById('yesBtn');

		addEvent(yesBtn,'click',addColorMenu, true);
		
		var noBtn = document.getElementById('noBtn');
		addEvent(noBtn, 'click', removeForm, false);

*/
	//	alert('initializing page...');
	try{
		var containerDiv = document.getElementById('activeContent');
		
		var optionstext = document.createElement('h3');
		optionstext.innerHTML = 'Would you like more options?';
		containerDiv.appendChild(optionstext);
		
		var containerForm = document.createElement('form');
		containerForm.setAttribute('name', 'activeContent');
		containerForm.setAttribute('method', 'get');
		containerForm.setAttribute('action', ''); //echo php self
		containerDiv.appendChild(containerForm);
		
	//	addEvent(containerForm, 'submit', alertSubmission, false);
		/**/
		var initialParagraph = document.createElement('p');
		containerForm.appendChild(initialParagraph);
		
		var yesBtn = new createFormElement( 'button','button', 'Yes', 'Yes', 'Yes');
		yesBtn.setAttribute('id', 'yesBtn');
		initialParagraph.appendChild(yesBtn);
		
		var noBtn = new createFormElement( 'button','button', 'No', 'No', 'No' );
		initialParagraph.appendChild(noBtn);
		noBtn.setAttribute('id', 'noBtn');

		addEvent(yesBtn,'click',addColorMenu, false);
		addEvent(noBtn, 'click', removeForm, false);
		
		//	alert('events added!');
			
	}catch(exception){
		alert(exception);
		}
			
		
	}
	
	function removeForm(e){
		var container = document.getElementById('activeContent');
		container.parentNode.removeChild(container);
		cancelClick(e);
		return true;
		}

	
			function addColorMenu(e){
				
				var containerForm = document.getElementById('activeContent').getElementsByTagName('form')[0];//trying to access this via the form name throws an error in IE
				if(!containerForm){
					alert('you may have to find a better way to do this.');
					}
			
				var nextBlock = document.createElement('p');
				var selectionText = document.createTextNode('Select a color:');
				var breakInTheCode = document.createElement('br');

				nextBlock.appendChild(selectionText);
				nextBlock.appendChild(breakInTheCode);

				try{
					var selectColorMenu = new createFormElement('select', 'select', 'color', null, false);
					optionsArray = new Array('Select one', 'blue', 'green', 'red', 'white', 'purple', 'yellow', 'orange', 'teal');
					
					for(var i=0; i<optionsArray.length; i++){
						//alert(optionsArray[i]);
						colorOption = new createFormElement('option', 'option', optionsArray[i], optionsArray[i], optionsArray[i]);
						selectColorMenu.appendChild(colorOption);
						}
					
					nextBlock.appendChild(selectColorMenu);	
					containerForm.appendChild(nextBlock);				
				
			//		alert('color menu: '+selectColorMenu);
//			return selectColorMenu;
				addEvent(selectColorMenu,'change', setBkgrndColor, true);
				   

			
			var additionalBlock = document.createElement('p');
			additionalBlock.innerHTML = "What would you like to be doing right now?<br />";
			containerForm.appendChild(additionalBlock);
			
			//alert(additionalBlock);
			
			var selectActivityMenu = new createFormElement('select', 'select', 'activity', null, null);
			activitiesArray = new Array("Select One", "Surfing the Internet", "Skiing or Snowboarding", "Filling Out Paperwork", "Swimming", "Reading a book", "Hiking, Camping or Fishing", "Working on engines", "Writing Code", "Eating", "Something else entirely");

			for(var i=0; i<activitiesArray.length; i++){
				
			var activityOptions = new createFormElement('option', 'option', activitiesArray[i], activitiesArray[i], activitiesArray[i]);
			selectActivityMenu.appendChild(activityOptions);		
				
				}
			additionalBlock.appendChild(selectActivityMenu);
			
			//alert(selectActivityMenu);
			
			
			
			//call this when you're ready for it:
			
			addEvent(selectActivityMenu, 'change', processAction, false);
/**/

				}catch(error){
					alert('addColorMenu failed: '+error.lineNumber);
				}
		//	alert('almost the end of addColor Menu.  what now?');
		
		//next we remove the event listener from the yes button:
		var yesBtn = document.getElementById('yesBtn');
		if(yesBtn.removeEventListener){
			yesBtn.removeEventListener('click', addColorMenu, false);//not supported by IE
		}
		
		
		//and finally we prevent formal form submission
				cancelClick(e);

			}

		function processAction(){
			
			//alert('oh no you have called an unwritten function.');
			
			alert("You are a winner!");
			
			//write a server-side script
			//send this form as post data
			
			}

		function createFormElement( formElementType, inputType, inputName, inputValue, innerContent ){
			
			var newElm = document.createElement(formElementType);
			
			//if(inputType.tolowercase == 'input'){} 
			
			if(formElementType){
				newElm.name = inputName;
				
				if(formElementType == 'input'){
						if(inputType){
							newElm.type = inputType;//it's telling me this is read-only????
							}	
					
					}
				
				}
			if(inputValue){
				newElm.value = inputValue;
				}
			if(innerContent){
				newElm.innerHTML = innerContent;
				}
			
			return newElm;
		
		}
		
		function setBkgrndColor(e){
			
			/*
			if(!e){
				//alert('what is e?');
				return;
				}
		//	alert(e.target);
			
			var selectedColor;
			
			if(e.target){
				selectedColor = e.target.value;
			}else{
				if(window.event.srcElement){
					selectedColor = window.event.srcElement.value;
					}else{
					selectedColor = null;	
					}
			}
			
			*/
			var selectedColor = getTarget(e).value;
			
		//	alert('selectedColr is '+ selectedColor);
			
			var containerDiv = document.getElementById('activeContent');
			containerDiv.style.backgroundColor = selectedColor;
			}
			
			function alertSubmission(e){
				//alert(getTarget(e));
				
				alert('the form has been submitted.');
				
					
					
				
				/*
				could test:
				if (e.target == yesBtn){ cancelBubble;}
				*/
				}
			
/*
	function submitForm(){
		var theForm = document.forms['activeContent'];
	//	theForm.submit;
		}
*/

//getParameter(s) by Marcus Andersson

function getParameter(name) {
   var url = window.location.href;
   var paramsStart = url.indexOf("?");

   if(paramsStart != -1){

      var paramString = url.substr(paramsStart + 1);
      var tokenStart = paramString.indexOf(name);

      if(tokenStart != -1){

         paramToEnd = paramString.substr(tokenStart + name.length + 1);
         var delimiterPos = paramToEnd.indexOf("&");

         if(delimiterPos == -1){
            return paramToEnd;
         }
         else {
            return paramToEnd.substr(0, delimiterPos);
         }
      }
   }
}

function getParameters() {

   var params = new Array();
   var url = window.location.href;
   var paramsStart = url.indexOf("?");
   var hasMoreParams = true;

   if(paramsStart != -1){

     var paramString = url.substr(paramsStart + 1);
     var params = paramString.split("&");
     for(var i = 0 ; i < params.length ; i++) {

       var pairArray = params[i].split("=");

       if(pairArray.length == 2){
         params[pairArray[0]] = pairArray[1];
       }

     }
     return params;
   }
   return null;
}

