/*
 * 0 - IE
 * 1 - FFox
 * 2- Opera
 * 3 - Netscape
 */
var navigator_type = -1;


	function push(arr, val) {
		arr[arr.length] = val;
	}
	
	function setRadioValue(form, fieldname, value) {
		for (formindex = 0; formindex < form.length; formindex++) {
			if (form.elements[formindex].name == fieldname && form.elements[formindex].value == value) {
				if (form.elements[formindex].checked != true) {
					form.elements[formindex].checked = true;
				}
			}
		}
	}
	
	function disableControl(form, fieldname, disabled) {
		for (formindex = 0; formindex < form.length; formindex++)
			if (form.elements[formindex].name == fieldname)
				form.elements[formindex].disabled = (disabled == true) ? true : "";
	}

	function getCheckValues(form, fieldname) {
		a = new Array();
		for (formindex = 0; formindex < form.length; formindex++)
			if (form.elements[formindex].name == fieldname && form.elements[formindex].checked)
				push(a, form.elements[formindex].value);
		return a;
	}

	function getRadioValue(form, fieldname) {
		for (formindex = 0; formindex < form.length; formindex++)
			if (form.elements[formindex].name == fieldname && form.elements[formindex].checked)
				return form.elements[formindex].value;
		return null;
	}
	
	function getControl(form, fieldname) {
		for (formindex = 0; formindex < form.length; formindex++)
			if (form.elements[formindex].name == fieldname)
				return form.elements[formindex];
		return null;
	}

	
//		changeOptions(document.agform.testselect, );
//		table = getElementById("maintable", document);
//		if (state == 0) {
//			state = 1;
//			hideRows(table, [3,4,5,6,7,8]);
//		}
//		else {
//			state = 0;
//			showRows(table, [3,4,5,6,7,8]);
//		}

	function getCheckHTML(name, value, values) {
		s = '<input type="checkbox" name="';
		s = s + name;
		s = s + '" value="';
		s = s + value;
		s = s + '"';
		found = false;
		for (valindex = 0; valindex < values.length; valindex++)
			if (values[valindex] == value)
				found = true;
		if (found == true)
			s = s + ' checked="checked"';
		s = s + ">";
		return s;
	}
	
	function getElementById(name, doc) {
		//Detect IE5.5+
		if (isNavigatorMSIE()){
			if (doc.all)
				return doc.all[name];
			if (doc.getElementById)
				return doc.getElementById(name);
		}
		//firefox
		if(isNavigatorFirefox()){
			var i = 0;
			var j = 0;
			if (doc instanceof HTMLDocument){
				if(doc.getElementById(name)!= null){
					return doc.getElementById(name);
				}else{
					for(i = 0;i < doc.forms.length;i++){
						var elements = doc.forms[i].elements;
						for(j = 0;j < doc.forms[i].elements.length; j++){
							if(elements[j].name == name){
								return elements[j];
							}
						}
					}
				}
			}else{
				if(document.getElementById(name)!= null){
					return document.getElementById(name);
				}else
				var doc_els = doc.elements;
				for(j = 0; j < doc.elements.length; j++){
					if(doc_els[j].name == name || doc_els[j].id == name){
						return doc_els[j];
					}
				}
			}
		}
		//detect Netscape 4.7+
		if (isNavigatorNetscape()){
			//
		}
		//Opera
		if(isNavigatorOpera()){
			if (doc.all)
				return doc.all[name];
			if (doc.getElementById)
				return doc.getElementById(name);
		}
		
	}
	
	function isNavigatorMSIE() {
		if(navigator_type == -1){
			navigator_type = navigator.appVersion.indexOf("MSIE") != -1? 0: -1;
								
		}
		return navigator_type == 0;
	}

	function isNavigatorOpera() {
		if(navigator_type == -1){
			if(navigator.userAgent.indexOf("Opera") != -1){
				var versionindex=navigator.userAgent.indexOf("Opera") + 6;
				if (parseInt(navigator.userAgent.charAt(versionindex)) >= 8) {
					navigator_type = 2;
				}
			}	
		}
		return navigator_type == 2;
	}
	
	function isNavigatorFirefox() {
		if (navigator_type == -1){
			if(navigator.userAgent.indexOf("Firefox") != -1){
				var versionindex=navigator.userAgent.indexOf("Firefox") + 8;
				if (parseInt(navigator.userAgent.charAt(versionindex)) >= 1) {
					navigator_type = 1;
				}
			}	
		}
		return navigator_type == 1;
	}

	function isNavigatorNetscape() {
		if(navigator_type == -1){
			if (navigator.appName=="Netscape" && parseFloat(navigator.appVersion) >= 4.7){
				navigator_type = 3;
			}	
		}		
		return navigator_type == 3;
	}
	
	/* old
	function getElementById(name, doc) {
		if (doc.all)
			return doc.all[name];
		if (doc.getElementById)
			return doc.getElementById(name);
		return null;
	}
	*/
	
	function hideRows (table, rowIndices) {
	   if (table && table.rows) {
	     for (var rowindex = 0; rowindex < rowIndices.length; rowindex++)
	       table.rows[rowIndices[rowindex]].style.display = 'none';
	   }
	}

	function hideRow(table, rowIndex) {
       if (rowIndex!=null) {
	       table.rows[rowIndex].style.display = 'none';
	   }
	}

	function showRow(table, rowIndex) {
       table.rows[rowIndex].style.display = '';
	}

	function showRows (table, rowIndices) {
	   if (table && table.rows) {
	     for (var rowindex = 0; rowindex < rowIndices.length; rowindex++)
	       table.rows[rowIndices[rowindex]].style.display = '';
	   }
	}
	
	function selectArray(allopts, fk) {
		res_array = new Array();
		res_index = 0;
		for (alloptsindex = 0; alloptsindex < allopts.length; alloptsindex++) {
			foundopt = false;
			if (allopts[alloptsindex].length > 2) {
				optfk = allopts[alloptsindex][2];
				found = false;
				for (indexjsa = 0; indexjsa < optfk.length; indexjsa++)
					for (indexksa = 0; indexksa < fk.length; indexksa++)
						if (optfk[indexjsa] == fk[indexksa])
							foundopt = true;
			}
			else
				foundopt = true;
			if (foundopt == true) {
				res_array[res_index] = new Array(allopts[alloptsindex][0], allopts[alloptsindex][1]);
				res_index = res_index + 1;
			}
		}
		return res_array;
	}

	function selectArray2(allopts, fk) {
		res_array = new Array();
		res_index = 0;
		for (alloptsindex = 0; alloptsindex < allopts.length; alloptsindex++) {
			foundopt = false;
			if (allopts[alloptsindex].length > 2) {
				optfk = allopts[alloptsindex][2];
				found = false;
				for (indexjsa = 0; indexjsa < optfk.length; indexjsa++)
						if (optfk[indexjsa] == fk)
							foundopt = true;
			}
			else
				foundopt = true;
			if (foundopt == true) {
				res_array[res_index] = new Array(allopts[alloptsindex][0], allopts[alloptsindex][1]);
				res_index = res_index + 1;
			}
		}
		return res_array;
	}

	function selectArrayByString(allopts, fk) {
		res_array = new Array();
		res_index = 0;
		for (alloptsindex = 0; alloptsindex < allopts.length; alloptsindex++) {
			foundopt = false;
			if (allopts[alloptsindex].length > 2) {
				optfk = allopts[alloptsindex][2];
				found = false;
				if (optfk == fk)
					foundopt = true;
			}
			else
				foundopt = true;
			if (foundopt == true) {
				res_array[res_index] = new Array(allopts[alloptsindex][0], allopts[alloptsindex][1]);
				res_index = res_index + 1;
			}
		}
		return res_array;
	}
	
	function changeSelectOptions(select, array) {
		if ((select.options.length > 0) && (select.selectedIndex >= 0))
			selectvalue = select.options[select.selectedIndex].value;
		else
			selectvalue = "";
		select.length = 0;
		for (selectindex1 = 0; selectindex1 < array.length; selectindex1++) {
			valueseli = array[selectindex1];
			//alert(valueseli);
			bool_selected = false;
			if (valueseli[0] == selectvalue)
				bool_selected = true;
			select.options[select.length] = new Option(valueseli[1], valueseli[0], false, bool_selected);
		}
	}
	
	function getRowById(table, rowid) {
	   if (table && table.rows) {
	     for (var rowindex = 0; rowindex < table.rows.length; rowindex++)
	     	if (table.rows[rowindex].id == rowid)
	     		return rowindex;
	   }
	   return null;
	}

	function changeRadio(form, radio, array) {
		reset = false;
		for (i = 0; i < form.elements.length; i++){
			if (form.elements[i].name == radio){
				cont = true;
				for (j = 0; j < array.length; j++){
					if (form.elements[i].value == array[j][0]){
						cont = false;
						break;
					}
				}
				if (form.elements[i].checked && cont){
					reset = true;
				}
				form.elements[i].disabled = cont;
			}
		}
		if (reset){
			for (i = 0; i < form.elements.length; i++){
				if (form.elements[i].name == radio){
					if (!form.elements[i].disabled){
						form.elements[i].checked = true;
						break;
					}
				}
			}
		}
	}
	function updateTableView(table, condition, firstRowName, precedingRowCount, lastRowName, followingRowCount){
		var firstRowIndex = getRowById(table, firstRowName) - precedingRowCount;
		var lastRowIndex = getRowById(table, lastRowName) + followingRowCount;
		for (var i = firstRowIndex; i <= lastRowIndex; i++){
			if (condition) {
				table.rows[i].style.display = '';
			} else {
				table.rows[i].style.display = 'none';
			}
		}
	}
	function setAllCheckboxesStartWith(form, fieldName, status){
		for (i = 0; i < form.elements.length; i++){
			if (form.elements[i].type == "checkbox" && form.elements[i].name.substring(0, fieldName.length) == fieldName){
				form.elements[i].checked = status;
			}
		}
	}
	function setAllRadiosStartWith(form, fieldName, value){
		for (i = 0; i < form.elements.length; i++){
			if (form.elements[i].type == "radio" && form.elements[i].name.substring(0, fieldName.length) == fieldName && form.elements[i].value == value){
				form.elements[i].checked = true;
			}
		}
	}
	function setAllRadiosStartWithByCheckbox(form, nameCheckbox, nameRadio, value){
		for (i = 0; i < form.elements.length; i++){
			if (form.elements[i].type == "radio" && form.elements[i].name.substring(0, nameRadio.length) == nameRadio && form.elements[i].value == value){
				cb = getControl(form, nameCheckbox + form.elements[i].name.substring(nameRadio.length));
				if (cb != null && cb.type == "checkbox" && cb.checked){
					form.elements[i].checked = true;
				}
			}
		}
	}
	/**
	 * work when checkbox not exists
	 */
	function checkMultipleRadioByCheckbox(form, nameCheckbox, nameRadio, value){
		res = true;
		for (i = 0; i < form.elements.length; i++){
			if (form.elements[i].type == "radio" && form.elements[i].name.substring(0, nameRadio.length) == nameRadio && form.elements[i].checked && form.elements[i].value != value){
				cb = getControl(form, nameCheckbox + form.elements[i].name.substring(nameRadio.length));
				if (cb == null || cb.type == "checkbox" && cb.checked){
					res = false;
					break;
				}
			}
		}
		return res;
	}	

	function getSelectValue(form, fieldname) {
		selectctrl = getControl(form, fieldname);
		return selectctrl[selectctrl.selectedIndex].value;
	}
	
	function updateRowView(tableId, rowId, condition) {
		var table = getElementById(tableId, document);
		if (condition) {
			showRow(table, getRowById(table, rowId));
		} else {
			hideRow(table, getRowById(table, rowId));
		}
	}	