//<script language="javascript">

var wf_form;
var wf_r = new Array(10);
var wf_allon = new Array(10);
var wf_client_text = true;

function wf_checkall(i) {
	var rdiv = document.all("wf_r_" + i);
	j = 1;
	if (wf_allon[i]) wf_allon[i] = false; else wf_allon[i] = true;
	while (rdiv.all("wf_r_" + i + "_" + j)) {
	  cb = rdiv.all("wf_r_" + i + "_" + j);
		cb.checked = wf_allon[i];
		cb.click();
		j++;
	}
}

function RTELoaded(w) {
	if (typeof(arLinks) != "undefined")
		w.setLinks(arLinks,arLinks2);
	w.setBGColor("#FFFFFF");
	w.setHTML(document.all.htmlsource.value);
	var tmpEl = document.createElement("SPAN");
	tmpEl.innerHTML = "a";
	w.setEditCss(rundir + "style.css");
	w.setPopupCss(sysdir + "global.css");
	w.setToolbar("tbbgcolor",false);
	w.setToolbar("tbemoticon",false);
	w.setToolbar("tbbar7",false);
}

function wf_field_str_value(i) {
	if (i) obj = this.obj(i); 
		else obj = this.obj;
	return obj.value;  
}

function wf_field_getvalue(i) {
  a = this.str_value(i);
	if (this.simpletype=="real" || this.simpletype=="int") {
		a = a.replace(/,/,".");
		return 1*a; // javascript recognises 0="" if "0" is converted to 0
	} 
	else 
		return a;
}

function wf_field_hasvalue(i) {
	if (i) obj = this.obj(i); 
		else obj = this.obj;
  if (this.input=="checkbox") {
    return (obj.checked);
  } else {
    return (obj.value>"");
  }
}

//fixme : fields to wf_form and wf_r and get() as default property.
function wf_field_constructor(name, type, check, frm) {
	var name;
	var obj;
	var simpletype;
	var check;
	var input;
	var oldvalue;
	var i;
	var parent;
	var id; // r only
	var getvalue;
	
	this.name = name;
	this.simpletype = type;
	this.check = check;
	frm.count = frm.count + 1;
	this.i = frm.count;
	frm.coll_nr[this.i] = this;
	frm.coll_str[this.name + "_"] = this;
	this.parent = frm;
	if (frm.i) { // =if (frm.r)
		this.obj = wf_r_get_object;
		tmp = this.obj(1);
		this.input = wf_field_input(tmp);
		this.str_value = wf_field_str_value;
		this.getvalue = wf_field_getvalue;
	} else {
		this.obj = frm.elements[this.name];
		//fixme - remove these 2 lines as in r constructor
		this.obj.wf_field = this;
		this.obj.i = 0;
		this.input = wf_field_input(this.obj);
		this.str_value = wf_field_str_value;
		this.getvalue = wf_field_getvalue;
		this.oldvalue = this.str_value();
	}
	this.hasvalue = wf_field_hasvalue;
}

function wf_fields_get(ident) {
	if (isNaN(ident)) return this.coll_str[ident + "_"]; // _ is added because fieldname can be "length" etc.. form property
		else return this.coll_nr[ident]; 
}

function wf_field_input(obj) {
//alert(obj.name);
	if (obj.tagName=="SELECT") return "select";
	  else if (obj.disabled) return "disabled";
		  else return obj.type;
}


function wf_fields_constructor(names_array, types_array, checks_array) {
  var me;
  me = document.forms.wf_form;
	me.changed = false;
	me.count = 0;
	me.coll_nr = new Array();
	me.coll_str = new Array();
	me.i = 0; // 0 for main, following for r
	for (i=1; i<names_array.length; i++) {
		fld = new wf_field_constructor(names_array[i], types_array[i], checks_array[i], me);
	}
	me.fields = wf_fields_get;
	me.get = wf_fields_get;
	return me;
}

function wf_r_get_object(i) {
//alert("wf_r_" + this.parent.i + "_" + i + "_" + this.name);
  var rqname;
  if (this.name == "wf_bind_id") rqname=""; 
    else rqname = "_" + this.name;
	return wf_form.elements["wf_r_" + this.parent.i + "_" + i + rqname];	
}

function wf_r_constructor(i) {
	var name;
	var i;
	var xtable;
	var rowcount;
	
	this.name = wf_r_names[i];
	this.xtable = wf_r_xtables[i];

	this.changed = false;
	this.count = 0;
	this.coll_nr = new Array();
	this.coll_str = new Array();
	this.i = i; // 0 for main, following for r
	for (j=1; j<(wf_r_fields[i]).length; j++) {
		fld = new wf_field_constructor(wf_r_fields[i][j], wf_r_types[i][j], wf_r_checks[i][j], this);
	}
	this.fields = wf_fields_get; 
	fld = new wf_field_constructor("wf_bind_id", "int", false, this);
	this.count -= 1;
	// fixme - this could be solved with arrays and scripts at the end of the page
	j = 1;
	while (this.fields(1).obj(j+1)) j++;
	this.rowcount = j;
}

function wf_r_clean(i, j) {
  r = wf_r[i];
  for(k=1; k<=r.count; k++) {
    f = r.fields(k);
    switch (f.input) {
      case "checkbox":
        f.obj(j).checked=false;
        break;
      case "select":
        f.obj(j).selectedIndex = 0;
        break;
      default:
        f.obj(j).value = "";
    }
  }
  if (typeof(wf_onclean)!="undefined") wf_onclean(i, j);  
}

function wf_r_xtable_checked(i, j, me) {
  if (!(me.checked)) {
    wf_r_clean(i,j);
  }
}

var wf_active_label = "";
function wf_show_pop_name(name) {
  top.status = name;
}
function wf_clear_pop_name() {
  if (wf_active_label == "") top.status = ""; else top.status = wf_active_label;
}

function wf_onblurbind(i) {
  top.status = "";
  wf_active_label = "";
  if (typeof(wf_onblur)!="undefined") wf_onblur(i);
}

function wf_onfocusbind(i) {
  wf_active_label = i.wf_label;
  top.status = wf_active_label;
  if (typeof(wf_onfocus)!="undefined") wf_onfocus(i);
}

function wf_onmouseoverbind(i) {
  top.status = i.wf_label;
  if (typeof(wf_onmouseover)!="undefined") wf_onmouseover(i);
}

function wf_onmouseoutbind(i) {
  wf_clear_pop_name();
  if (typeof(wf_onmouseout)!="undefined") wf_onmouseout(i);
}

function wf_ondblclickbind(i) {
  if (typeof(wf_ondblclick)!="undefined") wf_ondblclick(i);
}

function wf_onkeydownbind(i) {
  //fixme : skip disabled fields
  if (event.ctrlKey) {
    if (i.wf_r) {
      r = wf_r[i.wf_r];
      f = r.fields(i.wf_name);
      switch (event.keyCode) {
        case wr_keys.up:
          if (i.wf_i>1) r.fields(i.wf_name).obj(i.wf_i-1).focus();
          break;
        case wr_keys.left:
          if (f.i>1) r.fields(f.i-1).obj(i.wf_i).focus();
          break;
        case wr_keys.down:
          if (i.wf_i<r.rowcount) r.fields(i.wf_name).obj(1*i.wf_i+1).focus();
          break;
        case wr_keys.right:
          // fixme: move to next r first field
          if (f.i<r.count) r.fields(1*f.i+1).obj(i.wf_i).focus();
          break;
      }      
    } else {
      f = wf_form.fields(i.name);
      switch (event.keyCode) {
        case wr_keys.up:
        case wr_keys.left:
          if (f.i>1) wf_form.fields(f.i-1).obj.focus();
          break;
        case wr_keys.down:
        case wr_keys.right:
          // fixme: move to first r
          if (f.i<wf_form.count) wf_form.fields(f.i+1).obj.focus();
          break;
      }
    }
    

    if (event.keyCode==wr_keys.s) wf_form.elements('wf_save').click();
   
    if (event.keyCode==wr_keys.up || event.keyCode==wr_keys.down || event.keyCode==wr_keys.left || event.keyCode==wr_keys.right || event.keyCode==wr_keys.s) 
      event.returnValue = false;
  }
  if (typeof(wf_onkeydown)!="undefined") wf_onkeydown(i);
}

function wf_onloadbind() {
	wf_form = new wf_fields_constructor(wf_main_names, wf_main_types, wf_main_checks);
	//wf_fields = new wf_fields_constructor(wf_main_names, wf_main_types, wf_main_checks);
	for (i=1; i<=wf_rcount; i++) {
		wf_allon[i] = false;
		wf_r[i] = new wf_r_constructor(i);
	}
	if (typeof(wf_onload)!="undefined") wf_onload();
//alert(wf_r[1].fields.get("volume").getvalue(2));
}

function wf_onclickbind(i) { if (typeof(wf_onclick)!="undefined") wf_onclick(i); }

function wf_onchangebind(i) { 
	wf_form.changed = true;
	if (i.wf_r) {
	  r = wf_r[i.wf_r];
	  if (r.xtable > "") {
	    f = r.fields(1);
	    if (f.hasvalue(i.wf_i)) f.parent.fields(f.parent.count+1).obj(i.wf_i).checked = true;
	  }
	}
	if (typeof(wf_onchange)!="undefined") wf_onchange(i); 
}

function wf_onoverbind(i) { if (typeof(wf_onover)!="undefined") wf_onover(i); }

function wf_onoutbind(i) { if (typeof(wf_onout)!="undefined") wf_onout(i); }

function wf_show_object(link, select) {
  document.location = link + wf_form.fields(select).getvalue();
}

// move to webrock.js

var selectedInput;
if (wf_hasdate) {
  //var calObj = new CalendarPopup();
  // kui anda CalendarPopup funktsioonile ette nimi "calDiv", siis tehakse kalender layeriga, tühja stringi korral popupiga
  //calObj.setReturnFunction("showDate");
}

// Function to get input back from calendar popup
function showDate(y,m,d,anchor) {
  ipbind = ""+document.all(anchor).ipbind;
  document.all(ipbind).value = d + "." + m + "." + y;
}

function getDate(input,linkname) {
  selectedInput=input;
  calObj.showCalendar(linkname);
}

function wr_keys_constructor() {
  this.up = 38;
  this.left = 37;
  this.right = 39;
  this.down = 40;
  this.s = 83;
}
var wr_keys = new wr_keys_constructor();


	/* rg = /(&|\?)key=.+?(&|$)/; 
	*/
	
	//rg = new RegExp("(&|\?)key=.+?(&|$)", "i");
	//rg.source = "(&|\?)" + key + "=.+?(&|$)"
	//alert(rg.source);

function wr_url_parameter(path, key, value) {
  var newpath;
  newpath = path;
  rg = new RegExp("(&|[/?])"+key+"=.+?(&|$)");
	if ((ma = rg.exec(newpath)) != null) {
		if (ma.input.substr(ma.lastIndex-1,1) == "&") sufix = "&"; else sufix = "";
		newpath = newpath.replace(rg, ma.input.substr(ma.index,1)+key+"="+value+sufix);
	} else {
	  pos = newpath.indexOf("?");
	  if (pos==-1)
	  	newpath += "?";
	  else
	    if (pos < newpath.length-1) newpath += "&";
		newpath += key + "=" + value;
	}
	return newpath;
}

// *

function wf_check_fields() {
//return true;
	var i, fi, labelstr;
	for (i=1; i<=wf_form.count; i++) {
	  fi = wf_form.fields(i); 
	  //if ((fi.check) && fi.obj.contentEditable && !(fi.name=="sentdate" && wf_addnew) && (fi.input!="hidden")) { // fixme
	  if (fi.check) {
	    if (fi.str_value()=="") {
				labelstr = document.all("lbl_"+fi.name).innerHTML;
				alert(wf_pleasemark + " " + labelstr + "!");
				fi.obj.focus();
				return false;	      
	    }
	  }
	}
	for (i=1; i<=wf_rcount; i++) {
	  r = wf_r[i];
	  for (j=1; j<=r.rowcount; j++) {
  	  if (r.xtable>"") {
	      for (k=1; k<=r.count; k++) {
	        f = r.fields(k);
	        //alert(r.fields.get(k).name);
	        //alert(r.fields.get(k).obj(j).notnull);
	        if (f.check && f.obj(j).contentEditable!="false" && fi.input(j)!="hidden") {
	          if (f.str_value(j)=="" && r.fields("wf_bind_id").obj(j).checked) {
	            rname = document.all("lbl_r_"+r.name).innerText;
	            objname = document.all("lbl_r_"+r.i+"_"+j).innerText;
	            alert(wf_pleasemark + " '" + rname + "' field " + f.name + " on " + objname + " (" + j + ".)");
	            f.obj(j).focus();
	            return false;
	          }
	        }
	      }
	    } else {
	      alive = false;
	      for (k=1; k<=r.count; k++) {
	        if (r.fields(k).hasvalue(j)) {
	          alive=true;
	          break;
	        }
	      }
	      if (alive) {
	        for (k=1; k<=r.count; k++) {
	          f = r.fields(k);
	          if (f.check && f.obj(j).contentEditable!="false") {
	            if (f.str_value(j)=="") {
	              rname = document.all("lbl_r_"+r.name).innerText;
	              alert(wf_pleasemark + " '" + rname + "' field " + f.name + " on row " + j);
	              f.obj(j).focus();
	              return false;
	            }
	          }
	        }
	      }
	    }
    } 
	}
	return true;
}

function wf_complete_submit() {
	if (frames.richedit) {
		if (richedit.getText()!="" || (richedit.getHTML()).indexOf("IMG") != 0) {
			//document.all.htmlsource.value = convert_mailto_js(richedit.getHTML());
			document.all.htmlsource.value = richedit.getHTML();
		} else {
			document.all.htmlsource.value = "";
		}
		//if (document.all.textsource) document.all.textsource.value = convert_mailto_js(richedit.getText());
		if (wf_client_text && document.all.textsource) document.all.textsource.value = richedit.getText();
	}

	wf_comma = ""
	for(i in wf_carry) {
		if (wf_carry[i] > "") wf_form.elements.wf_carry.value += wf_comma + i + ":" + wf_carry[i];
		wf_comma = ",";
	}
}

function wf_onsubmit() {
  var val;
	if (!(wf_check_fields())) return false;
  if (typeof(wf_onsubmit_rev)!="undefined") {
    val = wf_onsubmit_rev();
	  if (val == false) return false;
	}
	wf_complete_submit();
	return true;
}

