/**
* Execute popup ans setup return_to variable.
*/

function ExecSubmitDialog(str1,str2,x,y,agn){
	if (agn != ''){
		eval ('agv=document.forms[0].'+agn+'.value;');
	}
	msgWindow = open ("","dialog",'resizable=no,width='+x+',height='+y+',menubar=no, scrollbars=1');
	if (msgWindow.opener == null) msgWindow.opener = self;
	msgWindow.opener.return_to = agn;
	msgWindow.opener.display_to = agn + '_display';

	Tform = document.forms[0];
	Tform._func_.value=str2;
	Tform._mod_.value=str1;
	//Tform._fc_post_='XXXX';
	Tform.target = 'dialog';
	Tform.submit();
}

function ExecSubmitConfirm(str1,str2,message){
	if (window.confirm (message)){
		ExecSubmit(str1,str2);
	}
}

function ExecSubmit(str1,str2){
	Tform = document.forms[0];
	Tform._func_.value=str2;
	Tform._mod_.value=str1;
	//Tform._fc_post_=1;
	Tform.target = '_self';
	Tform.submit();
}

function ExecParent(str1,str2,opener){
	Tform = opener;
	Tform._func_.value=str2;
	Tform._mod_.value=str1;
	Tform.target = '_self';
	//Tform._fc_post_=1;
	Tform.submit();
}



function ValReturn(varname,value){
	eval('opener.document.forms[0].'+varname+'.value="'+value+'"');
	window.close();
}

function Change(vname, sw){
	Tform = document.forms[0]
	for (i=0,n=Tform.elements.length; i < n ;i++){
	  if (Tform.elements[i].name.indexOf(vname) !=-1){
			Tform.elements[i].checked = sw;
		}
	}
}

function SelectAll(vname){ Change(vname, 1) }

function UnselectAll(vname){ Change(vname, 0) }

function no(){}

function _func(str1,str2){
	Tform = document.forms[0];
	Tform._func_.value=str2;
	Tform._mod_.value=str1;
	Tform.target = '_self';
	Tform.submit();
}

function _func_ext (str1, str2, ext){
	vix = ext.length;
	Tform = document.forms[0];
	Tform._LH_recall_.value = ext;
	_func(str1,str2);
}


function _popup_func(str1,str2,w,y,agn){
	Tform = document.forms[0];
	//alert (agn);
	xsize = w;
	ysize = y;
	eval ('agv=document.forms[0].'+agn+'.value;');

	x = (screen.width - xsize)/2;
	y = (screen.height- ysize)/2;

	msgWindow=open('popup.php?m_var='+agn+'&m_val='+agv+'&_mod_='+str1+'&re_mod='+str2,'dialog','resizable=no,width='+xsize+',height='+ysize+',menubar=no');
	msgWindow.moveTo(x,y)
	if (msgWindow.opener == null) msgWindow.opener = self;

}

function _popup_func(str1,str2,w,y,agn){
	Tform = document.forms[0];
	//alert (agn);
	xsize = w;
	ysize = y;
	eval ('agv=document.forms[0].'+agn+'.value;');

	x = (screen.width - xsize)/2;
	y = (screen.height- ysize)/2;

	msgWindow=open('index.php','dialog','resizable=no,width='+xsize+',height='+ysize+',menubar=no');
	msgWindow.moveTo(x,y)
	if (msgWindow.opener == null) msgWindow.opener = self;



}


function DateDialog(varname){
	Tform = document.forms[0];
	elm = Tform.elements[varname];

	xsize = 250
	ysize = 180

	x = (screen.width - xsize)/2;
	y = (screen.height- ysize)/2;

	msgWindow=open('lib/dialog/calendar.php?retval='+varname,'dialog','resizable=no,width='+xsize+',height='+ysize+',menubar=no');
	msgWindow.moveTo(x,y)
	if (msgWindow.opener == null) msgWindow.opener = self;

}

function _ch_func(str1,str2){
   Tform = document.forms[0];
   Tform._func_.value=str2;
   Tform._mod_.value=str1;
   //Tform.action="main.php";
	//Tforn.target="main";
	//if (err = _func_check()){
	/*
	if (err = verify()){
		alert ("Error:\n"+err);
	}else{
		Tform.submit();
	}
	*/
}

function _func_check (){
	Tform = document.forms[0];
	v = Tform.length;
	err=''
	for (a=2;a<v;a++){
		e = Tform.elements[a];
		if (e.type=="text" && e.value=='' ){
			err = err + "field "+ e.name+ " empty\n";
		}
	}
	return err
}


// verify table;
fvn = new Array();
fvl = new Array();


function verify(name,label){
	vix = fvn.length;
	Tform = document.forms[0];
	//alert('verification start 7');

	err=''
	for (n=0; n < vix ;n++){
		eval ("e = Tform."+fvn[n]);
		if (e.type=="text" && e.value=='' ){
			err = err + "field "+ fvl[n] + " empty\n";
		}
	}
	return err;
}


// auto corrections


function auto_correct(obj,crtype){
	eval('ob = document.forms[0].'+obj)

	str = ob.value
	err_flag = false
	six = (str.length -1)

	switch (crtype){
		case 'numeric':
			var valid="0123456789,";
			fst = str.search('[,]');
			if ( str.charAt(six) == ',' && (six) > fst) err_flag = true;

			break;
		case 'date':
			if (six >= 2 && str.charAt(2) != '/') {
				ob.value = str.substr(0,2) +'/';
				return false;
			}
			if (six >= 5 && str.charAt(5) != '/') {
				ob.value = str.substr(0,5) +'/';
				return false;
			}
			if (six > 9){
				ob.value = str.substr(0,10)
				return false
			}
			var valid="0123456789/";

			break;
		case 'real':      var valid="0123456789"; break;
		case 'hex':       var valid="0123456789ABCDEFabcdef"; break;
		case 'bin':       var valid="01"; break;

	}

	if (err_flag || valid.indexOf(str.charAt(six)) < 0) {
		ob.value=str.substr(0,six)
		return false;
	}
}

function insTag(wg,tag){
	eval ('ta = document.forms[0].'+wg);
	switch (tag){
		case 'i': utag='<i></i>'; break;
		case 'b': utag='<b></b>'; break;
		case 'u': utag='<u></u>'; break;
		case 'tt': utag='<tt></tt>'; break;
		case 'p': utag='<p></p>'; break;
		case 'br': utag='<br />'; break;
	}
	ta.value = ta.value+utag;
}

