/*	replace submit button */
function replacesubmitbutton(){
   var allforms = document.getElementsByTagName('form');
   for (var i=0; i < allforms.length; i++) {
      var formid = allforms[i].id;
      forminputs = allforms[i].getElementsByTagName('input');
      for (var j=0; j < forminputs.length; j++){
         if(forminputs[j].type == 'submit' && forminputs[j].id.indexOf('_replace') != -1){
            var inputid = forminputs[j].id;
            var sublink = document.createElement('a');
            var sublinkem = document.createElement('em');
            var sublinktext = document.createTextNode(forminputs[j].value);
            sublink.appendChild(sublinkem);
            sublink.appendChild(sublinktext);
			sublink.setAttribute('href', 'javascript:document.forms.'+formid+'.submit();');
            sublink.setAttribute('id', inputid);
			sublink.className = 'replace '+inputid;
            
            parentObject = forminputs[j].parentNode;
            var replaced = parentObject.replaceChild(sublink,forminputs[j]);
         }
      }
   }
};

// Simon Willison's onload Magic
// http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
   var oldonload = window.onload;
   if (typeof window.onload != 'function') {
       window.onload = func;
   } else {
       window.onload = function() {
           oldonload();
           func();
       }
   }
}
  
// add onload event
addLoadEvent(function() { 
	replacesubmitbutton();
});



function voteFor(id,name) {
	var icon = document.getElementById("nomineeIcon"+id.toString());
	var checkBox = document.getElementById("nominee_"+id.toString());
	var image = document.getElementById("nominee_"+id.toString());
	var nameTag = document.getElementById("currentChoice");
	//var urlTag = document.getElementById("currentChoiceURL");

	// set active choice name
	nameTag.innerHTML = name + " is your current choice " + "<a href=\"/nominees/show/" + id + "\">more info on " + name + "</a>";
		
	var imageBase = document.getElementById("voteimage_"+id).src.substr(0, document.getElementById("voteimage_"+id).src.lastIndexOf("/")+1);
	
	
	for (var i=1; i < 6; i++) {
		var img = document.getElementById("voteimage_"+i);
		// get filname
		var filename = img.src.substr(img.src.lastIndexOf("/")+1);
		if(filename.substr(0, 3) == "Ovr"){
			img.src = imageBase + filename.substr(3);
		}
	}
	
	var clickedImg = document.getElementById("voteimage_"+id);
	var clickedFilename= clickedImg.src.substr(clickedImg.src.lastIndexOf("/")+1);
	if(clickedFilename.substr(0, 4) == "vote"){
		clickedImg.src = imageBase+"Ovr"+clickedFilename;
	} //else if(clickedFilename.substr(0, 7) == "Outvote") { clickedImg.src = imageBase+"Ovr"+clickedFilename.substr(3); }
	
	for (var i=1; i < 6; i++) {
		var img = document.getElementById("voteimage_"+i);
		var filename = img.src.substr(img.src.lastIndexOf("/")+1);
		//if(filename.substr(0, 4) == "vote"){ img.src = imageBase + "Outvote" + filename.substr(4); }
	}
	
	replacesubmitbutton();
	// check the box
	checkBox.checked = "checked";
}


function popUp(mypage,myname,w,h,scroll){
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var settings ='height='+h+',';
		settings +='width='+w+',';
		settings +='top='+wint+',';
		settings +='left='+winl+',';
		settings +='scrollbars='+scroll+',';
		settings +='resizable=yes';
		win=window.open(mypage,myname,settings);
	if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
	return false;
}
