//
//         FormManager v1.0
//
//  2004-08-14 Christopher Bergren, IT Management 121 AB
//  
//
var agent=navigator.userAgent;
var ObjForm,ObjImages,ObjManager;
var isOpera=agent.indexOf("Opera")>-1;
var isIE=agent.indexOf("MSIE")>-1&&!isOpera;
var isMozilla=agent.indexOf("Mozilla")>-1&&!isOpera&&!isIE;

if (!(isOpera||isMozilla||isIE)) {
	alert("Denna webbplats stöder inte den webbläsare du använder\n\nVissa formulärfunktioner kommer ej att fungera.");
}

// This function is not part of the module, only placed here for convenience.
function PostToPage(PageNo,FromIndex) {
	oFrm=document.getElementById("MyForm");
	switch (PageNo) {
		case 0:
			if (FromIndex)
				oFrm.action='usrIndex.asp?Save=1';
			else
				oFrm.action='usrIndex.asp?Save=X';
			break;
		case 1:
			if (FromIndex)
				oFrm.action='usrFormIndex.asp?Save=1';
			else
				oFrm.action='usrFormIndex.asp?Save=X';
			break;
		default:
			if (FromIndex)
				oFrm.action='usrForm.asp?Save=1&PageNo='+PageNo;
			else
				oFrm.action='usrForm.asp?Save=X&PageNo='+PageNo;
			break;
	}
	oFrm.submit();
}
function EndForm() {
	window.location="usrTryEndForm.asp";
}


function FormManager(formId) {
	FormManager.prototype.RadioGroupCheck=fncRadioGroupCheck;
	FormManager.prototype.AddRadioGroup=fncAddRadioGroup;
	FormManager.prototype.SetRadioButtonImages=fncSetRadioButtonImages;
	FormManager.prototype.CheckBoxToggle=fncCheckBoxToggle;
	FormManager.prototype.AddCheckBox=fncAddCheckBox;
	FormManager.prototype.SetCheckBoxImages=fncSetCheckBoxImages;
	FormManager.prototype.IsChecked=fncIsChecked;

	var arrObjects=new Array();
	var arrIds=new Array();
	var nCount=0;
	
	ObjForm=document.getElementById(formId);
	ObjImages=new Images();
	ObjManager=this;
	
	function fncIdToObject(sId) {
		for (i=0;i<nCount;i++) {
			if (arrIds[i]==sId){
				return arrObjects[i];
			}
		}
	}

	function fncAddRadioGroup(id_,count_,setindex_,userfunction_) {
		arrIds[nCount]=id_;
		arrObjects[nCount]=new RadioGroup(id_,count_,userfunction_);
		if (setindex_>0) {
			arrObjects[nCount].Check(setindex_);
		}
		arrObjects[nCount].IsInitializing=false;
		nCount++;
	}
	function fncAddCheckBox(id_,idx_,value_,check_,userfunction_) {
		arrIds[nCount]=id_+'_'+idx_;
		arrObjects[nCount]=new CheckBox(id_,idx_,value_,userfunction_);
		if (check_) 
			arrObjects[nCount].Toggle();
		arrObjects[nCount].IsInitializing=false;
		nCount++;
	}
	function fncIsChecked(sName) {
		o=fncIdToObject(sName);
		return o.blOn;
	}
	function fncRadioGroupCheck(oElement) {
		sId=oElement.id;
		idx=parseInt(sId.substr(1+sId.indexOf('_')));
		sIdName=sId.substr(0,sId.indexOf('_'));
		oObj=fncIdToObject(sIdName);
		oObj.Check(idx);
	}
	function fncCheckBoxToggle(oElement) {
		sId=oElement.id;
		oObj=fncIdToObject(sId);
		oObj.Toggle();
	}
	function fncSetRadioButtonImages(sImgOff,sImgOn) {
		ObjImages.SetRadio(sImgOff,sImgOn);
	}
	function fncSetCheckBoxImages(sImgOff,sImgOn) {
		ObjImages.SetCheckBox(sImgOff,sImgOn);
	}
}

function Images() {
	Images.prototype.SetRadio=fncSetRadio;
	Images.prototype.SetCheckBox=fncSetCheckBox;
	function fncSetRadio(sImgOff,sImgOn) {
		this.RadioOn=sImgOn;
		this.RadioOff=sImgOff;
	}
	function fncSetCheckBox(sImgOff,sImgOn) {
		this.CheckBoxOn=sImgOn;
		this.CheckBoxOff=sImgOff;
	}
}

function RadioGroup(id_,count_,userfunction_) {
	RadioGroup.prototype.Check=fncCheck;
	RadioGroup.prototype.AddRadioButton=fncAddRadioButton;
	this.IsInitializing=true;
	this.UserFunction=(userfunction_=='')?'UserVoid':userfunction_;
	this.arrRadios=new Array();
	this.nCount=0;
	this.nValue=0;
	this.oData;
	for (i=1;i<=count_;i++) {
		this.AddRadioButton(document.getElementById(id_+'_'+i));
	}
	if (isIE) {
		this.oData=document.createElement("<INPUT TYPE='hidden'>");
	} else {
		this.oData=document.createElement("input");
		this.oData.type='hidden';
	}
	this.oData.name=id_;
	this.oData.value=0;
	ObjForm.appendChild(this.oData);

	function fncCheck(idx) {
		for (i=0;i<this.nCount;i++) {
			this.arrRadios[i].src=ObjImages.RadioOff;
		}
		this.arrRadios[idx-1].src=ObjImages.RadioOn;
		this.oData.value=idx;
		if (!this.IsInitializing) 
			eval(this.UserFunction+'('+idx+')');	
	}
	
	function UserVoid() {}
	
	function fncAddRadioButton(oElement) {
		id=oElement.id;
		oElement.id='';
		if (isIE) {
			oRadio=document.createElement("<IMG id='"+id+"' onclick='ObjManager.RadioGroupCheck(this);'>");
		} else {
			oRadio=document.createElement("img");
			oRadio.id=id;
			if (isOpera)
				oRadio.onclick="ObjManager.RadioGroupCheck(this);";
			else if (isMozilla)
				oRadio.addEventListener("click",DoClick,true);
		}
		oElement.insertBefore(oRadio,oElement.childNodes[0]);
		this.arrRadios[this.nCount]=oRadio;
		oRadio.src=ObjImages.RadioOff;
		this.nCount++;
	}
}

function CheckBox(id_,idx_,value_,userfunction_) {
	CheckBox.prototype.Toggle=fncToggle;

//	var oData;
	this.oData=null;
	this.blOn=false;
	this.Value=value_;
	this.Idx=idx_;
	this.IsInitializing=true;
	this.UserFunction=(userfunction_=='')?'UserVoid':userfunction_;

	this.OCheckBox=fncAddCheckBox(document.getElementById(id_+'_'+idx_));

	eval('this.oData=document.CHK_'+id_+';');
	if (!this.oData){
		if (isIE) {
			this.oData=document.createElement("<INPUT TYPE='hidden'>");
		} else {
			this.oData=document.createElement("input");
			this.oData.type='hidden';
		}
		eval('document.CHK_'+id_+'=this.oData;');
		this.oData.name=id_;
		this.oData.value=0;
		ObjForm.appendChild(this.oData);
	}

	function fncToggle() {
		this.OCheckBox.src=(this.blOn)?ObjImages.CheckBoxOff:ObjImages.CheckBoxOn;
		//oData.value=(this.blOn)?0:this.Value;
		this.blOn=!this.blOn;
		this.oData.value^=this.Value;
		if (!this.IsInitializing) 
			eval(this.UserFunction+'('+this.Idx+')');	
	}
	function UserVoid() {}

	function fncAddCheckBox(oElement) {
		id=oElement.id;
		oElement.id='';
		if (isIE) {
			oCheckBox=document.createElement("<IMG id='"+id+"' onclick='ObjManager.CheckBoxToggle(this);'>");
		} else {
			oCheckBox=document.createElement("img");
			oCheckBox.id=id;
			if (isOpera)
				oCheckBox.onclick="ObjManager.CheckBoxToggle(this);";
			else if (isMozilla)
				oCheckBox.addEventListener("click",DoCheckBoxToggle,true);
		}
		oElement.insertBefore(oCheckBox,oElement.childNodes[0]);
		oCheckBox.src=ObjImages.CheckBoxOff;
		return (oCheckBox);
	}
}

function DoClick(e) {
	ObjManager.RadioGroupCheck(e.target);
}
function DoCheckBoxToggle(e) {
	ObjManager.CheckBoxToggle(e.target);
}

