
/*
 * General 
 */
function parseBodyContent(text){
	bodyStartTag=text.indexOf("<body ");
	start=text.indexOf(">", bodyStartTag)+1;
	end=text.indexOf("</body>",start);
	if (start < end && start >-1 && end > -1){
		return text.substring(start,end);
	}
	return "";
}

function scrolldown(pixels){
	if(pixels<=0)
			return;
	window.scrollBy(0,25);
	setTimeout('scrolldown('+(pixels-25)+')',20);
}

function scrollup(pixels){
	if(pixels<=0)
			return;
	window.scrollBy(0,-25);
	setTimeout('scrollup('+(pixels-25)+')',20);
}

function switchView(e){
	if (e != null){
		if ( e.style.display!='block' ) e.style.display='block';
		else e.style.display='none';
	}
}

function viewOn(e){
	if (e != null)
		e.style.display='block';
}

function viewOff(e){
	if (e != null)
		e.style.display='none';
}

function switchIcon(icon, a, b){
	if (icon != null){
		if ( icon.src==a ) icon.src=b;
		else icon.src=a;
	}
}

function switchCheckbox(c){
	if (c != null){
		c.checked = !c.checked;
	}
}

function toggleOpenCloseSection(section, icon, img_open, img_close, link){
	link.blur();
	switchIcon( icon, img_close, img_open);
	switchView( section );
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

//Browser Window Size and Position
//copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
//you may copy these functions but please keep the copyright notice as well
//ALT: http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function pageWidth() {return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;} function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;} function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;} function posTop() {return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;} function posRight() {return posLeft()+pageWidth();} function posBottom() {return posTop()+pageHeight();}

function traverseForm(formid, func){
	var the_form = document.getElementById(formid);
	for ( var x=0; x<the_form.elements.length; x++ )
		func(the_form.elements[x]);
}

function formToString(formid){
	var s = '';
	traverseForm(formid, function(input){
		if(input.type == 'checkbox'){
			if (input.checked )
				s+=(s ? '&' : '')+input.name+'='+escape(input.value);
		} else
			s+=(s ? '&' : '')+input.name+'='+escape(input.value);
	});
	return s;
}

function updateMasterCheckbox(formid,masteid,slave_name_regex){
	var count_checked = 0;
	traverseForm(formid, function(input){
		if(input.type == 'checkbox' && input.name.match(slave_name_regex) && input.checked)
			count_checked+=1;
	});
	master = document.getElementById(masteid);
	master.checked=(count_checked ? 'checked' : '');
}

function updateSlaveCheckboxes(formid,masteid,slave_name_regex){
	master = document.getElementById(masteid);
	if (!master.checked)
		traverseForm(formid, function(input){
			if(input.type == 'checkbox' && input.name.match(slave_name_regex))
				input.checked='';
		});
}

/*
 * Votes
 */
function switchVotesButtons(clicked){
	clicked.parentNode.style.display='none';
	all_items=clicked.parentNode.parentNode.childNodes;
	for (i=0;i<all_items.length;i++){
		if ( 
			(clicked.id == 'vote_p' && all_items[i].id == 'voting_block_p') ||
			(clicked.id == 'vote_n' && all_items[i].id == 'voting_block_n')
		)
			all_items[i].style.display='inline';
	}
}

function switchAnswerToNoAnswer(clicked){
	if (clicked.href.match(/revoke=1/)){
		clicked.innerHTML="Great Answer!";
		clicked.href=clicked.href.replace("revoke=1", "revoke=0");
	} else {
		clicked.innerHTML="Didn't Answer";
		clicked.href=clicked.href.replace("revoke=0", "revoke=1");
		clicked.onclick=function(e){ return true; } ;
	}
}


