FormBuilder = function(){
}

FormBuilder.reloadTree = function(){
	window.top.innerframe.tree.location.reload();
}
FormBuilder.reloadParent = function(){
	parent.location.reload();
}
FormBuilder.reloadGrandParent = function(){
	parent.parent.location.reload();
}

FormBuilder.unHideTabs = function(){
	var buttonHider = document.getElementById('buttonHider');
	if(document.getElementById('buttonHider')!=null){
		if(document.all){
			//ie
			buttonHider.style.setAttribute('display','none');
		}else{
			//gecko
			buttonHider.style.display='none';
		}
	}
}

/****************************************************************************************************************
REDRAW FUNCTIONS
****************************************************************************************************************/

FormBuilder.addFrameToRedraw = function(frame,xOffset,yOffset){
	if(typeof(aFramesToRedraw) 	=='undefined' ){
	aFramesToRedraw				= new Array();
	}
	var iFrameToRedraw 			= aFramesToRedraw.length;

	if(arguments.length >= 4){
		var xMin=arguments[3]
	}else{
		var xMin=0;}
		
	if(arguments.length >= 5){
		var yMin=arguments[4]
	}else{
		var yMin=0;}

	aFramesToRedraw[iFrameToRedraw]				= new Array(3);
	aFramesToRedraw[iFrameToRedraw]["frame"] 	= frame;
	aFramesToRedraw[iFrameToRedraw]["xOffset"] 	= xOffset;
	aFramesToRedraw[iFrameToRedraw]["yOffset"] 	= yOffset;
	aFramesToRedraw[iFrameToRedraw]["xMin"] 	= xMin;
	aFramesToRedraw[iFrameToRedraw]["yMin"] 	= yMin;
}

FormBuilder.redrawFrames = function(){
	var fReturn =false;
	if(typeof(aFramesToRedraw) 	!='undefined' ){
		for(var iFrameToRedraw=0;iFrameToRedraw < aFramesToRedraw.length;iFrameToRedraw++){
			if(!document.all){
				var height 	= window.innerHeight;
				var width 	= window.innerWidth;
			}else{
				var height 	= document.body.clientHeight;
				var width 	= document.body.clientWidth;
			}
			
			var minWidth	= aFramesToRedraw[iFrameToRedraw]["xMin"];
			var minHeight	= aFramesToRedraw[iFrameToRedraw]["yMin"];
			
			height 			= height - aFramesToRedraw[iFrameToRedraw]["yOffset"];
			width 			= width - aFramesToRedraw[iFrameToRedraw]["xOffset"];
		
			if(width < minWidth)
				{width = minWidth;}
				
			if(height < minHeight)
				{height=minHeight;}
				
			aFramesToRedraw[iFrameToRedraw]["frame"].height 	= height;
			aFramesToRedraw[iFrameToRedraw]["frame"].width 		= width;
	
		}
		fReturn = true;
	}
	else{
	fReturn = false;
	}
	return fReturn;
}
/****************************************************************************************************************
END REDRAW FUNCTIONS
****************************************************************************************************************/

/****************************************************************************************************************
FORM ACTION FUNCTIONS
****************************************************************************************************************/

FormBuilder.assignShadoActionAndSubmit = function(myForm,action){
	//check that there is a value in name and label
	myForm.shadoaction.value = action;
	myForm.submit();
}

FormBuilder.changeFormActionAndSubmit = function(myForm,newaction){
	myForm.action = newaction;
	myForm.submit();
}
/****************************************************************************************************************
END FORM ACTION FUNCTIONS
****************************************************************************************************************/


/****************************************************************************************************************
PROMPTS
****************************************************************************************************************/

FormBuilder.deleteFormPrompt = function(){
	var myResult = prompt("If you are positive you want to delete this form, enter the term 'PROCEED' now.");
	if(myResult == 'PROCEED'){
		myResult = confirm("You are about to delete this form, it's datastore classes and submissions.\nThis is you last chance, are you sure you want to proceed?");
		if(myResult){
			return true;
		}
	}
	return false;
}


FormBuilder.deleteAllResultsPrompt = function(deleteAllURL){
	var myResult = prompt("If you are positive you want to delete all the submissions returned by the current filter, enter the term 'PROCEED' now.");
	if(myResult == 'PROCEED'){
		myResult = confirm("You are about to delete all the submissions that match the current filter.\nThis is you last chance, are you sure you want to proceed?");
		if(myResult){
			location.href = deleteAllURL;
			return true;
		}
	}
	return false;
}

FormBuilder.publishPrompt = function(){
	var myResult = prompt("If you are positive you want to publish this form, enter the term 'PROCEED' now.");
	if(myResult == 'PROCEED'){
		myResult = confirm("You are about to publish this form.\nThis is an irreversible process, are you sure you wish to proceed?");
		if(myResult){
			return true;
		}
	}
	return false;
}

FormBuilder.modifyFieldPrompt = function(params){
	var bModify = true;
	if(String(params.frame.location).indexOf(params.location) >=0){
		bModify = confirm('You are part way through modifying a field.\nAre you sure that you want to go to a blank form and lose your changes?.\nIf you want to submit the field changes use the button on the last wizard.');
	};
	if(bModify){
		clearAllListPointers(params.listpointers);
		params.frame.location=params.location;
	}
	return bModify;
}

/****************************************************************************************************************
END PROMPTS
****************************************************************************************************************/


/****************************************************************************************************************
VALIDATION
****************************************************************************************************************/
FormBuilder.isValidEmail = function(value){
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value);
}

FormBuilder.isNumericEnumeration = function(value){ 
	var fReturn 	= true;

	var iValue 		= 0;
	var sLabelValue = '';
	var sValue 		= '';

	var iPipeChar 	= 0;
	var iPipeCount 	= 0;
	var aPipeChars 	= new Array(1);
	//loop the list of values and labels
	var aValues 	= new Array(value);
	
	var iComma		= -1;
	fContinue 		= true;
	
	while (fContinue){
		iStart 	= iComma+1;
		iComma 	= value.indexOf(',',iComma+1);
		if(iComma > 0){
			iLen 		= iComma-iStart;
		}else{
			iLen 		= value.length - iStart;
			fContinue 	= false;
		}
		sLabelValue = value.substr(iStart,iLen);

		//right separate the label|value|selected values.
		if(FormBuilder.trim(sLabelValue).length){
			//loop all the | characters
			sValue	= sLabelValue;
			
			iPipeChar 	= sLabelValue.indexOf('|');
			iPipeCount 	= 0;
			aPipeChars 	= new Array();

			while(iPipeChar > 0){
				iPipeCount++;
				aPipeChars[iPipeCount]= iPipeChar;
				iPipeChar 	= sLabelValue.indexOf('|',iPipeChar+1);
			}

			//how may pipe characters are there?
			switch(iPipeCount){
				//if there are not pipechars, then the label and the value are the same and there is no selected var. just use the default values above.
				//if there is one pipe then we will assume that we have a label and a value
				case 0:
					break;
				case 1:{
					//is the label blank?
					sValue		= sLabelValue.substr(aPipeChars[1]+1);
					break;}
				//if there are two pipes then we can just split all the vars out :) this is the default option
				default :{
					sValue		= sLabelValue.substr(aPipeChars[1]+1,aPipeChars[2]-aPipeChars[1]-1);
					break;}
			}		

			//check if the value is numeric
			if(isNaN(sValue)){
				fReturn = false;
				break;
			}
		}
	}
	
	return fReturn
}

FormBuilder.CheckBoxNotEmpty = function(checkboxes) {
var fChecked = false;
for(var iCheck=0;iCheck < checkboxes.length;iCheck++){
	if(checkboxes[iCheck].checked){
		fChecked = true;
		break;
	}
}
return fChecked;}	

var aSubmitCheck = typeof aSubmitCheck != "undefined"?aSubmitCheck:new Array();
FormBuilder.addSubmitCheck = function(form,field,check,message){
	if(typeof(aSubmitCheck[form.id]) =='undefined' ){
	aSubmitCheck[form.id]					= new Array();
	}
	var iSubmitCheck 								= aSubmitCheck[form.id].length;
	aSubmitCheck[form.id][iSubmitCheck]				= new Array(3);
	aSubmitCheck[form.id][iSubmitCheck]["field"] 	= field;
	aSubmitCheck[form.id][iSubmitCheck]["check"] 	= check;
	aSubmitCheck[form.id][iSubmitCheck]["message"] 	= message;
}

FormBuilder.checkSubmit = function(form,suppressMore,setFocus){
	var fStatus = true;
	if(typeof(aSubmitCheck[form.id]) =='undefined' ){
		return true;
	}
	
	//does this form have a hidden2 element and does the saveObj function exist?
	//if it does, you need to saveObj.
	if(	typeof(SaveObj) != 'undefined' 
		&& typeof(document.getElementById('Hidden2')) != 'undefined'
 		&& typeof(htmlCode) != 'undefined'
 		){
		//save the HTML content
		SaveObj(form.id);
	}
	
	for(var iSubmitCheck=0;iSubmitCheck < aSubmitCheck[form.id].length;iSubmitCheck++){
		var field 	= aSubmitCheck[form.id][iSubmitCheck]["field"];
		var check 	= aSubmitCheck[form.id][iSubmitCheck]["check"];
		var message = aSubmitCheck[form.id][iSubmitCheck]["message"];

		if(eval(check)){
			alert(message);
			if(setFocus){
				//is this not a tag with a length? (select boxes have a lenght and you don't want to iterate them to set disabled)
				if(typeof field.tagName == 'undefined' && field.length){
				field[0].focus();
				}else{
				field.focus();
				}
			}
			
			if(suppressMore){
				return false;
			}else{
				fStatus = false;
			}
		}
	}
	return fStatus ;
}

FormBuilder.clearSubmitCheck = function(){
	aSubmitCheck = new Array();
}


/****************************************************************************************************************
END VALIDATION
****************************************************************************************************************/

/****************************************************************************************************************
MISC
****************************************************************************************************************/

FormBuilder.trim = function(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   obj = /^\s*$/;
   if (obj.test(temp)) { temp = temp.replace(obj, ''); }
   return temp;
}

FormBuilder.storeCaret = function(textEl) {
   if (textEl.createTextRange) 
	 textEl.caretPos = document.selection.createRange().duplicate();
}

FormBuilder.insertAtCaret = function(text) {
   text = '{' + text + '}';
   textEl = document.getElementById('messagesMessage');
   if (textEl.createTextRange && textEl.caretPos) {
		var caretPos 	= textEl.caretPos;
		caretPos.text 	= caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text;
	}
	else
		 textEl.value  	= textEl.value + text;
}
/****************************************************************************************************************
END MISC
****************************************************************************************************************/

 
 /*************************************************************************
 HELPHINT
 *************************************************************************/
 
 HelpHint = function(params){
	param_default = function (pname, def) { if (typeof params[pname] == "undefined") { params[pname] = def; } };		
	
	param_default("hint"		, 'no hint');
	
	this.x 			= 0;
	this.y 			= 0;
	this.hint		='helpHint';
	this.className 	='helpHint';
	this.name 		='helpHint';
	this.id 		='helpHint';

	this.helpHintId = params.helpHintId;
	
	if (document.createElementNS) {
		// use the XHTML namespace; IE won't normally get here unless
		// _they_ "fix" the DOM2 implementation.
		this.elem			= document.createElementNS("http://www.w3.org/1999/xhtml", 'div');
	} else {
		this.elem 			= document.createElement('div');
	}
	this.elem.innerHTML 	= params.hint;
	this.elem.className		= this.className;
	this.elem.name			= this.name;
	this.elem.id			= this.id;
	this.elem.style.display = 'none';
	this.elem.style.top 	= 5;
	this.elem.style.left 	= 5;
	this.elem.style.zIndex 	= 1000;
	
	this.link 				= params.link;
	this.link.appendChild(this.elem);
	
	//caching
	window.helpHints 		= window.helpHints?window.helpHints:new Array();
	window.helpHintTimeouts = window.helpHintTimeouts?window.helpHintTimeouts:new Array();
	window.helpHints[this.helpHintId]=this;
	var hh 						= window.helpHints[this.helpHintId];
	
	
	this.hideMe 				= function(){
									window.helpHintTimeouts[hh.helpHintId] 	= setTimeout('window.helpHints["'+hh.helpHintId+'"].hide("'+hh.helpHintId+'")',100); 
								}
	this.showMe 				= function(){
									clearTimeout(window.helpHintTimeouts[hh.helpHintId]);
									hh.show(hh.helpHintId);
								}

	HelpHint.addEvent(this.elem, 'mouseover', this.showMe);
	//HelpHint.addEvent(this.elem, 'mouseout', this.hideMe);

	HelpHint.addEvent(this.link, 'mouseover', this.showMe);
	HelpHint.addEvent(this.link, 'mouseout', this.hideMe);

	return this;
}

HelpHint.addEvent = function(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, true);
	} else {
		el["on" + evname] = func;
	}
};

HelpHint.getAbsolutePos = function(el) {
	var SL = 0, ST = 0;
	var is_div = /^div$/i.test(el.tagName);
	if (is_div && el.scrollLeft)
		SL = el.scrollLeft;
	if (is_div && el.scrollTop)
		ST = el.scrollTop;
	var r = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	//is this ie and is the parent 
	//a relatively positioned element?
	if(HelpHint.is_ie && el.offsetParent && /^relative$/i.test(el.offsetParent.currentStyle.position)){
		//use getComputedstyle()  for moz etc
		if(/^relative$/i.test(el.currentStyle.position)){
			//recurse ok		
			var tmp = this.getAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}else if(/^absolute$/i.test(el.currentStyle.position)){
			//this is broken as well as IE will append the body offset to this elements offset.. ffs.
			//and recurse
			var tmp = this.getAbsolutePos(el.offsetParent);
			r.x += tmp.x;
			r.y += tmp.y;
		}else{
			//do nothing more, just return this :)
		}
	}else if (el.offsetParent ) {
		var tmp = this.getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
};



HelpHint.prototype.hideShowCovered = function () {
	var self = this;
	HelpHint.continuation_for_the_khtml_browser = function() {
		function getVisib(obj){
			var value = obj.style.visibility;
			if (!value) {
				if (document.defaultView && typeof (document.defaultView.getComputedStyle) == "function") { // Gecko, W3C
					if (!HelpHint.is_khtml)
						value = document.defaultView.
							getComputedStyle(obj, "").getPropertyValue("visibility");
					else
						value = '';
				} else if (obj.currentStyle) { // IE
					value = obj.currentStyle.visibility;
				} else
					value = '';
			}
			return value;
		};

		var tags = new Array("applet", "iframe", "select", "textarea");
		var el 	= self.elem;

		var p 	= HelpHint.getAbsolutePos(el);
		var EX1 = p.x;
		var EX2 = el.offsetWidth + EX1;
		var EY1 = p.y;
		var EY2 = el.offsetHeight + EY1;

		for (var k = tags.length; k > 0; ) {
			var ar = document.getElementsByTagName(tags[--k]);
			var cc = null;

			for (var i = ar.length; i > 0;) {
				cc = ar[--i];
				p = HelpHint.getAbsolutePos(cc);
				var CX1 = p.x;
				var CX2 = cc.offsetWidth + CX1;
				var CY1 = p.y;
				var CY2 = cc.offsetHeight + CY1;
				
				//if this thing is even on this page...
				if(CX1>0&&CX2>0&&CY1>0&&CY1>0&&CY2>0){
					if ((CX1 > EX2) || (CX2 < EX1) || (CY1 > EY2) || (CY2 < EY1)) {
						if (!cc.__msh_save_visibility) {
							cc.__msh_save_visibility = getVisib(cc);
						}
						cc.style.visibility = cc.__msh_save_visibility;
					} else {
						if (!cc.__msh_save_visibility) {
							cc.__msh_save_visibility = getVisib(cc);
						}
						cc.style.visibility = "hidden";
					}
				}
			}
		}
	};
	if (HelpHint.is_khtml)
		setTimeout("HelpHint.continuation_for_the_khtml_browser()", 10);
	else
		HelpHint.continuation_for_the_khtml_browser();
};


/// detect a special case of "web browser"
HelpHint.is_ie = ( /msie/i.test(navigator.userAgent) &&
		   !/opera/i.test(navigator.userAgent) );

HelpHint.is_ie5 = ( HelpHint.is_ie && /msie 5\.0/i.test(navigator.userAgent) );

/// detect Opera browser
HelpHint.is_opera = /opera/i.test(navigator.userAgent);

/// detect KHTML-based browsers
HelpHint.is_khtml = /Konqueror|Safari|KHTML/i.test(navigator.userAgent);


HelpHint.prototype.hideAll = function(){
	
	for( var sHelpHint in window.helpHints ){
		window.helpHints[sHelpHint].hide();
	}
}

HelpHint.prototype.show = function(helpHintId){
	var myHelpHint 					= this.elem?this:window.helpHints[helpHintId];
	//get the scrollLeft and the window width
	//is this thing off the edge of the screen, if so move it left by that much.
	myHelpHint.elem.style.display 	= 'block';
	myHelpHint.hideShowCovered();
	return true;
}
HelpHint.prototype.hide = function(helpHintId){
	var myHelpHint 					= this.elem?this:window.helpHints[helpHintId];
	myHelpHint.elem.style.display 	= 'none';
	myHelpHint.hideShowCovered();
	return true;
}



