var VALUES=new Array();
var REPLACE=new Array();

function GetFormValue(_formname, _name) {
	var f=document.forms[_formname] ;

	//var inp=eval("document."+_formname+"."+_name);
	var inp = f[_name];

	var Val;
	if (!inp) { return ""; }

	
	switch (true) {
		case ( inp.type == "text" ):
		case ( inp.type == "hidden" ):
			Val = inp.value ;
		break ;
		
		case ( inp.type == "select-one" ):
			if (inp.selectedIndex>-1)
				Val = inp.options[inp.selectedIndex].value ;
			else 
				Val = 0;
		break ;    
		
		case ( ( inp.length > 0 ) && ( inp[0].type == "radio" ) ): 
		case ( ( inp.length > 0 ) && ( inp[0].type == "checkbox" ) ):
			Val = new Array() ;
			for (var i=0; i<inp.length; i++ ) {
				if ( inp[i].checked ) {
					Val[Val.length] = inp[i].value ;
					if (inp.type == "checkbox")
					{
						//inp.disabled = true;
					}
				} 
			}
			Val = Val.join("_") ;
		break ;
		
		case ( inp.type == "radio" ): 
		case ( inp.type == "checkbox" ):
			Val = ( inp.checked ) ? inp.value : "" ;
			if (inp.type == "checkbox")
			{
				//inp.disabled = true;
			}
		break ;  
		
		default:
			Val = "";
		break ;  
	}
	
	return Val ;
}


function GetQS(fqs, myform) {
	var QS=new Array();
	// параметры формы

	if(myform){
		FORM = myform;
	}

	for (var i=0; i<=VALUES.length-1; i++) {
		var U=GetFormValue(FORM, VALUES[i]);

		if (U!="" && U!=0) {
			if(REPLACE[VALUES[i]])
			QS[QS.length]=replaceString(REPLACE[VALUES[i]],'',VALUES[i])+"="+U; 
			else
			QS[QS.length]=VALUES[i]+"="+U; 
		} else {
			var re = new RegExp("([^a-zA-Z0-9_])"+VALUES[i]+"\=[^&]+&?", "g");
			fqs=fqs.replace(re,"$1");
		}
		
	}

	QS = fqs + QS.join("&");

	return QS;
}



function inst_submit_all(URL, fqs, Mode, myform) { 
	var QS=new Array();
	 QS=GetQS(fqs, myform);

	if (Mode=="popup") { window.opener.document.location=URL+QS; self.close(); }
	else if (Mode=="new") { window.open(URL+QS,""); }
	else if (Mode=="_top" || Mode=="_blank" || Mode=="_parent") { window.open(URL+QS,Mode); }
	else { document.location=URL+QS; }
	
}


function inst_add_qs_name(Name, Replace) {
	VALUES[VALUES.length]=Name;
	REPLACE[Name]=Replace;
}

function replaceString(oldS, newS, fullS) {
	 for (var i = 0; i < fullS.length; i++) {
		if (fullS.substring(i, i + oldS.length) == oldS) {
			fullS = fullS.substring(0, i) + newS + fullS.substring(i + oldS.length, fullS.length);
		}
	 }
  
   return fullS;
}
