//Copyright (C) 2003-2004 Richard Schmid, Infolox GmbH. All rights reserved.
function SF_findObj(n, d) {
	var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); return x;
}

//submit
function SF_submit(oDocument) {
	if(!oDocument)
		oDocument=document;
	oDocument.sf_propdefns.submit();
}
//selectValue
function SF_sV(oBtn, oDocument) {
	if(!oDocument)
		oDocument=document;
	if (oBtn && oBtn.id) {
		var sFormElID = "I" + String(oBtn.id);
		var oFormEl = SF_findObj(sFormElID, oDocument);
		if (oFormEl && (oFormEl.value)) {
			oFormEl.value = "1";
		}
		else {
			alert(sFormElID + " not found");			
		}
	}
}
function SF_click(sID_) {
	var oInp = SF_findObj(sID_, document);
	if (!oInp || !oInp.type) return false;
	switch( oInp.type.toLowerCase() ){
		case "radio": oInp.click(); break;
		case "checkbox": oInp.click(); break;
		case "text": oInp.focus(); break;
	}
}
function SF_setCBVal(oBtn, bValue, oDocument) {
	if(!oDocument)
		oDocument=document;
	if (oBtn && oBtn.id) {
		var sFormElID = "I" + String(oBtn.id);
		var oFormEl = SF_findObj(sFormElID, oDocument);
		if (oFormEl) {
			oFormEl.value = bValue;
			oFormEl.checked = bValue;
		}
	}
}
//de-selectValue
function SF_dsV(oBtn, oDocument) {
	if(!oDocument)
		oDocument=document;
	if (oBtn && oBtn.id) {
		var sFormElID = "I" + String(oBtn.id);
		var oFormEl = SF_findObj(sFormElID, oDocument);
		if (oFormEl && (oFormEl.value)) {
			oFormEl.value = "0";
		}
	}
}
function SF_kp(oEvent, oForm) {
	var key;
	var keychar;
	// IE
	if (window.event || !oEvent.which) {
		key = oEvent.keyCode; // for IE, same as window.event.keyCode
	}
	// netscape
	else if (oEvent) {
		key = oEvent.which;
	}
	
	if ((key == 13) && (oForm)) {
		oForm.submit();
	}
}

function SF_getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function ChoosingManager( obj_name ){
	this.objName = obj_name;
	this.is_ie = navigator.userAgent.indexOf("MSIE") != -1;
	this.is_opera = navigator.userAgent.indexOf("Opera") != -1;
	
	this.in_progress = false;
	this.uri_vars = [];
	
	this.click = function( input_id ){
		var input = document.getElementById( input_id );
		this.uri_vars = this.__getVars();
		
		if( ! input || ! input.type ){ return false; }
		switch( input.type.toLowerCase() ){
			case "radio": input.click(); break;
			case "checkbox": input.click(); break;
			case "text": input.focus(); break;
		}
		this.__handleAllText();	
		this.__goByVars( this.uri_vars );
	};
	
	this.choose = function( input_id ){
		showLoading();
		document.getElementById('lCont').style.display="";
		var input = document.getElementById( input_id );
		this.uri_vars = this.__getVars();
		
		if( !input || !input.type ){ return false; }
		
		switch( input.type ){
			case "radio":    this.__handleRadio( input );    break;
			case "checkbox": this.__handleCheckbox( input ); break;
			case "text":     this.__handleText( input );     break;
		}
		this.__handleAllText();
		
		if( this.is_opera && input.type.toLowerCase()=="checkbox" ){
			input.checked = ! input.checked;
		}
		this.__goByVars( this.uri_vars );
	};
	
	this.__handleAllText = function() {
		var elements = SF_getElementsByClass( 'SFText', null, 'input' );
		
		for ( i in elements ) {
			this.__handleText( elements[i] );
		}
	};
	
	this.__handleRadio = function( input ){
		var ids_to_remove = [];
		var founded_values = [];
		var aTextVars = [];
		var iActColumn = 0;
		while (iActColumn < 101) {
			var oTextInput = document.getElementById( "idc_"+iActColumn );
			if (!oTextInput || !oTextInput) {
				iActColumn++;
				continue;
			}
			for( i in this.uri_vars ){
				if( this.uri_vars[i]['key'] == oTextInput.name ){
					ids_to_remove[ids_to_remove.length] = i;
				}
			}
			if (oTextInput.value != "") {
				aTextVars[aTextVars.length] = {
					"key"   : oTextInput.name,
					"value" : escape( oTextInput.value )
				};
			}
			iActColumn++;
		}
		for( i in this.uri_vars ){
			if( this.uri_vars[i]['key'] == input.name ){
				ids_to_remove[ids_to_remove.length] = i;
				founded_values[founded_values.length] = this.uri_vars[i]['value'];
			}
			else if (this.uri_vars[i]['value'] == "") {
				ids_to_remove[ids_to_remove.length] = i;
			}
		}
		for( i in ids_to_remove ){
			delete( this.uri_vars[ids_to_remove[i]] );
		}
		var value_is_the_same = false;
		for( i in founded_values ){
			if( founded_values[i] == escape( input.value ) ){
				value_is_the_same = true;
			}
		}
		if( ! value_is_the_same ){
			this.uri_vars[this.uri_vars.length] = {
				"key"   : input.name,
				"value" : escape( input.value )
			};
		}
		for( var i in aTextVars ){
			this.uri_vars[this.uri_vars.length] = {
				"key"   : aTextVars[i]['key'],
				"value" : aTextVars[i]['value']
			};
		}

	};
	
	this.__handleCheckbox = function( input ){
		if( this.in_progress ){ return; }
		else { this.in_progress = true; }
		
		if( input.checked ){
			var value_is_present = false;
			for( i in this.uri_vars ){
				if( this.uri_vars[i]['key'] == input.name && this.uri_vars[i]['value'] == escape( input.value ) ){
					value_is_present = true;
					break;
				}
			}
			if( !value_is_present ){
				this.uri_vars[this.uri_vars.length] = {
					"key"   : input.name,
					"value" : escape( input.value )
				};
			}
		} else {
			//alert(input.name);
			var ids_to_remove = [];
			for( i in this.uri_vars ){
				//alert(this.uri_vars[i]['key']);
				if( this.uri_vars[i]['key'] == input.name && this.uri_vars[i]['value'] == escape( input.value ) ){
				    //alert("remove:"+i);	
				    ids_to_remove[ids_to_remove.length] = i;
				}
			}
			for( i in ids_to_remove ){
				delete( this.uri_vars[ids_to_remove[i]] );
			}
		}

	};
	
	this.__handleText = function( input ){
		var value = escape(input.value);
		var ids_to_remove = [];
		var aTextVars = [];
		var iActColumn = 0;
		while (iActColumn < 101) {
			var oTextInput = document.getElementById( "idc_"+iActColumn );
			if (!oTextInput || !oTextInput) {
				iActColumn++;
				continue;
			}
			for( i in this.uri_vars ){
				if( this.uri_vars[i]['key'] == oTextInput.name ){
					ids_to_remove[ids_to_remove.length] = i;
				}
			}
			if (oTextInput.value != "") {
				aTextVars[aTextVars.length] = {
					"key"   : oTextInput.name,
					"value" : escape( oTextInput.value )
				};
			}
			iActColumn++;
		}
		for( i in ids_to_remove ){
			delete( this.uri_vars[ids_to_remove[i]] );
		}
		for( var i in aTextVars ){
			this.uri_vars[this.uri_vars.length] = {
				"key"   : aTextVars[i]['key'],
				"value" : aTextVars[i]['value']
			};
		}
	};
	
	this.__getVars = function(){
		var els = [];
		var sHRef = document.location.href;
		sHRef = sHRef.replace("%5B", "[").replace("%5D", "]");
		if (sHRef.indexOf('#') >= 0) {
		    sHRef = sHRef.split("#")[0];
		}
		var uri_els = sHRef.split("?");
		if( uri_els[1] ){
			uri_els = uri_els[1].split("&");
			for( var i in uri_els ){
				var var_els = uri_els[i].split("=");
				if( typeof var_els[1] != "undefined" ){
					els[els.length] = {
						"key"   : var_els[0],
						"value" : var_els[1]
					};
				}
				else {
					alert(uri_els[i]);
				}
			}
		}
		
		return els;
	};
	
	this.__goByVars = function( vars ){
		var sHRef = document.location.href;
		var sAnchor = '';
		var uri = sHRef.split("?")[0]+"?";
		if (sHRef.indexOf('#') >= 0) {
		    sAnchor = sHRef.split("#")[1];
		}
		var bHasIDKey = false;
		for( var i in vars ){
			if (vars[i]['key'] == 'id') {
				bHasIDKey = true;
			}
			uri+= vars[i]['key']+"="+vars[i]['value']+"&";
		}
		if (!bHasIDKey) {
			uri += "id=17&";
		}
		uri = uri.substr( 0, uri.length-1 );
		if (sAnchor != '') {
		    uri = uri + '#' + sAnchor;
		}
		//Exista-special:
		else {
		    uri = uri + '#reslist';		
		}
		document.location.href = uri;
	};
};

var CM = new ChoosingManager("CM");