/**
 * Indicates if at least one answer is required
 * @var bool
 */
var forceOneAnswer=false;

/**
 * Indicates if we should check the input
 * @var bool
 */
var checkInput=true;

/**
 * disable radio/checkbox series
 * @param string name   Name of the form element
 */
function disable_series(name){
	elements=document.getElementsByName(name);
	for(x=0;x<elements.length;x++){
		elements[x].disabled=true;
	}
}

/**
 * enable radio/checkbox series
 * @param string name   Name of the form element
 */
function enable_series(name){
	elements=document.getElementsByName(name);
	for(x=0;x<elements.length;x++){
		elements[x].disabled=false;
	}
}

/**
 * Opens a window for spell checking
 * @param object obj   A form element (Text/Textarea)
 */
function openSpellChecker(obj) {
	var speller = new spellChecker(obj);
	var centerX=(screen.width-500)/2; 
	var centerY=(screen.height-400)/2; 
	speller.popUpProps+=',top='+centerY+',screenY='+centerY+',left='+centerX+',screenX='+centerX;;
	speller.openChecker();
}


/**
 * Open popup window
 * @param string url   The url of the content
 * @param int height   The height of the window
 * @param int width    The width of the window
 */
function ow(url,height,width){
	var centerX=(screen.width-width)/2; 
	var centerY=(screen.height-height)/2; 
	var prop='height='+height+',width='+width+',scrollbars=yes,resizable=yes,top='+centerY+',screenY='+centerY+',left='+centerX+',screenX='+centerX;
	window.open(url,'bs',prop);
}


/**
 * Returns the character from the decimal ascii character code pased
 * chr=charFromCharCode(97); will return the letter 'a'
 */
function charFromCharCode (charCode) {
	return unescape('%' + charCode.toString(16));
}


/**
 * Checks the form for blank questions
 */
function checkForm(){
	skipArray=new Array();
	return _checkForm(skipArray);
}

/**
 * Checks the form for blank questions
 * @param array skipArray    Array of form element names that ahould be
 *                           ignored
 */
function _checkForm(skipArray)
{
	var f=document.form;
	var e=f.elements;

	if(!checkInput){
		return true;
	}

 //GET ALL VARABLE NAMES
	allElements=new Array();
	for(var i=0;i<e.length;i++){
		if(e[i].name.substr(0,1) == "Q" && e[i].type != "hidden" && !e[i].disabled){
			allElements.push(e[i].name);
		}
	}
	allElements=usort(allElements);

 //GET ALL ANSWERED VARABLE NAMES
	answeredElements=new Array();
	for(var i=0;i<e.length;i++)
	{
		if(e[i].name.substr(0,1) == "Q")
		{
			if(e[i].type.substr(0,4) == "text")
			{
				e[i].value=e[i].value.trim();
				if(e[i].value.length != 0){
					answeredElements.push(e[i].name);
				}
			}
			if(e[i].type == "radio" || e[i].type == "checkbox")
			{
				if(e[i].checked){
					answeredElements.push(e[i].name);
				}
			}
			if(e[i].type == "select-one")
			{
				if(e[i].selectedIndex > 0){
					answeredElements.push(e[i].name);
				}
			}
		}
	}

	//IF NONE ANSWERED ERROR
	if(answeredElements.length == 0 && forceOneAnswer)
	{
		alert("You must answer something to continue");
		return false;
	}

	//ADD ANY VARABLES THAT SHOULD BE SKIPPED
	answeredElements=answeredElements.concat(skipArray);

	//SORT ARRAY UNIQUE
	answeredElements=usort(answeredElements);

 //REMOVE ANSWERD VARABLES FROM ALL LIST
	for(var x=0;x<answeredElements.length;x++)
	{
		i=0;
		while(answeredElements[x] != allElements[i] && i < allElements.length) i++;
		allElements.splice(i,1)
	}

	//IF ALL LIST IS NOT EMPTY ASK TO ANSWER QUESTIONS
	if(allElements.length > 0 && document.form.ask.value == 1)
	{
		//traverse the DOM tree to find the "main" question container
		//which is the parent with an id of ^.+_question$
		for(var x=0;x<allElements.length;x++){
			theElement=document.getElementsByName(allElements[x])[0];
			if(theElement){
				anode=theElement;
				while(anode && anode.nodeName != "BODY" && !anode.id.match(/^.+_question$/)){
					anode=anode.parentNode;
				}
				if(anode && anode.nodeName != "BODY"){
					anode.style.background="pink";
				}
			}
		}
		if(allElements.length > 1){
			alert("There are "+allElements.length+" questions that you have left blank.\n\nThese questions have been highlighted. Please review these\nquestions and answer them if you wish.\n\n\nYou will not be asked about these questions again.");
		}else{
			alert("There is "+allElements.length+" question that you have left blank.\n\nThis question has been highlighted. Please review this\nquestion and answer it if you wish.\n\n\nYou will not be asked about this question again.");
		}
		document.form.ask.value=0;
		return false;
	}
	return true;
}

function rmwsp(obj) { if(obj) { var s=obj.value; re1=new RegExp("^ +"); re2=new RegExp(" +$"); s=s.replace(re1,""); s=s.replace(re2,""); obj.value=s; } } 

/**
 * Add string trim function
 */
String.prototype.trim=function(){
	a=this.split(/\n/g),
	i=a.length;
	while(i-->0){
		a[i]=a[i].replace(/^\s+/,'');
		a[i]=a[i].replace(/\s+$/,'');
	}
	return a.join('\n');
}


function usort(theArray)
{
	tmpArray=new Array();
	theArray=theArray.sort();
	for(var i=0;i<theArray.length;i++){
		if(i > 0 && theArray[i-1] == theArray[i]) {} else tmpArray.push(theArray[i]);
	}
	return tmpArray;
}

