/*******************
Author: Patrick Ryan
URL: http://www.agavegroup.com

Feel free to use this however you like.  Credit is always appreciated.
*******************/

	//need to set a couple of images here:
	imageRadioChecked="/assets/site/images/radio-checked.gif";
	imageRadioUnchecked="/assets/site/images/radio-unchecked.gif";
	
	//the rest of the images are in the CSS


	function prettyForms(){
		fixRadios();
	}
	
	//apply look to radio buttons
	function fixRadios(){
		inputs = document.getElementsByTagName("input");
		for(i=0;i<inputs.length;i++){
			if(inputs[i].type=="radio"){
				lnk = document.createElement("a");
				lnk.className="fake_radio";
				img = document.createElement("img");
				if(inputs[i].checked==true){
					img.src = imageRadioChecked;
				}else{
					img.src = imageRadioUnchecked;
				}
				
				//elements created, now pass functionality
				//give the checkbox an id if it doesn't have one
				if(inputs[i].id){
					realId = inputs[i].id;
				}else{
					realId = "radio"+i;
					inputs[i].id = realId;
				}
				
				//give the fake check an id
				fakeId = "fake"+realId;
				img.id=fakeId
				
				lnk.href="javascript:toggleRadio('"+realId+"','"+fakeId+"')";
			
				//insert the new image into the document
				if(document.all){				//IE
					lnk = inputs[i].insertAdjacentElement("BeforeBegin",lnk)
				}else{
					inputParent = inputs[i].parentNode;
					lnk = inputParent.insertBefore(lnk,inputs[i]);
				}
				lnk.appendChild(img);
				
				//remove the actual checkbox
				inputs[i].style.display="none";
			}
		}
	}
	
	//function runs when a radio button is clicked
	function toggleRadio(realRadioId, fakeRadioId){
		realRadio = document.getElementById(realRadioId);
		fakeRadio = document.getElementById(fakeRadioId);
		//want to ONLY look in the correct form, so get this radio button's parent form (supports multiple forms)
		radioForm = realRadio.parentNode;
		tmpCnt=1;
		while(radioForm.tagName!="FORM"){
			radioForm = radioForm.parentNode;
			tmpCnt++;
			if(tmpCnt>50){
				window.alert("encountered javascript error\n[parentNode]")
				break;
			}
		}	
		inputs=radioForm.getElementsByTagName("input");
		for(i=0;i<inputs.length;i++){
			if(inputs[i].type=="radio"){		
				//IDs look like this:  realId: blah    fakeId: fakeblah
				if(inputs[i].name==realRadio.name){	//is part of the same radio group, uncheck it.
					inputs[i].checked=false;	//uncheck the actual button
					document.getElementById("fake"+inputs[i].id).src=imageRadioUnchecked;
					if(inputs[i].id==realRadioId){
						inputs[i].checked=true;	//check the actual button
						document.getElementById("fake"+inputs[i].id).src=imageRadioChecked;
					}
				}
			}
		}
		
		//**** EVENT HANDLING
		// Clicking the radiobutton equivalent to the button's onClick event and onChange event . fire it.
		triggerEvent(realRadio,"change");
		triggerEvent(realRadio,"click");
	}
		
	//function to trigger events that are built into the form elements that have been hidden
	function triggerEvent(obj, evt){
		if(document.all){
			if(evt=="click"){
				res = obj.fireEvent("onclick");
			}else if(evt=="change"){
				res = obj.fireEvent("onchange");
			}
		}else{
			//NOTE - in the mozilla event model, I am cancelling the bubbleUp!  (1st false)
			// this is needed to prevent odd interaction, but could cause other issues!
			if(evt=="click"){
				mouseEvent = document.createEvent('MouseEvents');
				mouseEvent.initMouseEvent('click',true,true,window,1,0,0,0,0,false,false,false,false,0,null);
				obj.dispatchEvent(mouseEvent); 
			}else if(evt=="change"){
				mouseEvent = document.createEvent('HTMLEvents');
				mouseEvent.initEvent('change',true,true,window,1,0,0,0,0,false,false,false,false,0,null);
				obj.dispatchEvent(mouseEvent);
			}
		}
	}
	
