/***********************************************************************
 *
 * Klasa {@link FormHelper}.  eQUEST Library, http://www.equest.pl
 * 
 * @copyright (C) 2001-2006 by DBX Sp. z o.o. All rights reserved.
 *            ul. Marczukowska 6 p.203, 15-724 Bialystok, POLAND.
 *            http://www.dbx.pl  e-mail: biuro@dbx.pl
 *
 * @license This product is commercial use only. You can`t redistribute
 *          it and/or modify without permission. Before the code can be
 *          downloaded or installed or used you must read and agree to
 *          the terms of eQUEST software license: 
 *
 * @author Robert Borys <robert@dbx.pl>
 *
 * @category eQUEST core
 * @package Globals
 *
 ***********************************************************************/


/**
 * Zbior funkcji pomocniczych do formularzy
 */
function FormHelper() {

	this.initialize = function() {
		this.activeTabs = new Array();
		this.activeTabGroup = 0;
	}
	this.initialize();
	
	
	/**
	 * Wyswietla popupa z zawartoscia z podanego adresu i z podanymi wymiarami.
	 */
	this.showPage = function(url, width, height) {
		var params = "toolbar=no,location=no,directories=no,menubar=no,";
		params += "dependent=yes,status=no,scrolling=no,scrollbars=no,";
		params += "resizable=no,width=" + width + ",height=" + height;
		window.open(url,"",params);
	}
	
	/**
	 * Wyswietla popupa z obrazkiem z podanego adresu i z podanymi wymiarami.
	 * Klikniecie gdzies w body popupa powoduje jego zamkniecie
	 */
	this.showImgPage = function(url, width, height) {
		var diff = 50;
		var scrollbar = "no";
		
		width = parseInt(width);
		height = parseInt(height);
		
		if (width > (screen.width - diff)) {
			width = screen.width - diff;
			height += 20;
			scrollbar = "yes";
		}
		if (height > (screen.height - diff - 100)) {
			height = screen.height - diff - 100;
			width += 20;
			scrollbar = "yes";
		}
		
		var params = "toolbar=no,location=no,directories=no,menubar=no,";
		params += "dependent=yes,status=no,scrolling="+ scrollbar +",";
		params += "scrollbars="+ scrollbar +",resizable=no,";
		params += "width=" + width + ",height=" + height;
		
		var winq = window.open("","",params);
		winq.document.open();
		winq.document.write('<html><body onclick="window.close()"><center>');
		winq.document.write('<img src="'+url+'">');
		winq.document.write("</center></body></html>");
		winq.document.close();
	}
	
	
	/**
	 * Formatuje pola typu timestamp. date typu YYYYMMDD lub YYMMDD zamienia na
	 * YYYY-MM-DD. Czas (o ile jest), w formacie HH:MM:SS, pozostaje bez zmian.
	 */
	this.formatTimestamp = function(object) {
		var value;
		var regDates = new Array();
		var regTimes = new Array();
		var i, dates, times, dateStr, timeStr;
		
		value = validator.trim(object.value);
		
		regDates.push(/^([1-2]\d\d\d)-([0-1]\d)-([0-3]\d)/);
		regDates.push(/^([1-2]\d\d\d)([0-1]\d)([0-3]\d)/);
		regDates.push(/^(\d\d)([0-1]\d)([0-3]\d)/);
		for(i=0; i<regDates.length; i++) {
			dates = value.match(regDates[i]);
			if(dates) break;
		}
		if(i == 2) {
			var year = dates[1];
			if(parseInt(year) < 38) year = "20" + year;
			else year = "19" + year;
			dates[1] = year;
		}
		
		regTimes.push(/([0-2]\d):([0-5]\d):([0-5]\d)$/);
		for(i=0; i<regTimes.length; i++) {
			times = value.match(regTimes[i]);
			if(times) break;
		}
		
		if(!dates) return;
		dateStr = "" + dates[1] + "-" + dates[2] + "-" + dates[3];
		
		if(times) timeStr = " " + times[1] + ":" + times[2] + ":" + times[3];
		else timeStr = "";
		
		object.value = dateStr + timeStr;
		return;
	}
		
	/**
	 * Ustawia aktualny div z zakladka
	 *
	 * @param int group Grupa zakladek
	 * @param int tab Numer zakladki
	 */
	this.divchange = function(group, tab) {
		var tab1, tab2, btn1, btn2, old, inputs, inputElems, i;
		
		this.activeTabGroup = group;
		old = this.activeTabs[group];
		tab1 = document.getElementById("tab" + group + "_" + old);
		tab2 = document.getElementById("tab" + group + "_" + tab);
		if(!tab1 || !tab2 || tab==old) return;
		tab1.style.position = "absolute";
		tab2.style.position = "static";
		tab1.style.visibility = "hidden";
		tab2.style.visibility = "visible";
		
		inputElems = ["input", "select", "textarea"];
		for(i in inputElems) {
			inputs = tab2.getElementsByTagName(inputElems[i]);
			if(inputs.length > 0) {
				inputs[0].focus();
				break;
			}
		}
		
		btn1 = document.getElementById("tab" + group + "_"+ old + "_tab");		
		btn2 = document.getElementById("tab" + group + "_"+ tab + "_tab");
		btn1.className = "tab_inactive";
		btn1.parentNode.className = "tab";
		btn2.className = "tab_active";
		btn2.parentNode.className = "tab tab_active";
				
		this.activeTabs[group] = tab;
		document.form1.b_activeDivs.value = this.activeTabs.toString();
	}
	
	/**
	 * Wywolana, gdy grupa zakladek zlapie fokus.
	 *
	 * @param int group
	 */
	this.focusTabs = function(group) {
		this.activeTabGroup = group;
	}
	
	/**
	 * Ustawia wartosc pola formularzowego o podanym id
	 *
	 * @param String fieldId
	 * @param String value
	 */
	this.setFieldValue = function(fieldId, value) {
		var field;
		field = document.getElementById(fieldId);
		if(!field) return;
		field.value = value;
	}
	
	/**
	 * pobiera wartosc pola formularzowego
	 * 
	 * @param String fieldId Id pola formularzowego
	 * @return String
	 */
	this.getFieldValue = function(fieldId) {
		var field;
		field = document.getElementById(fieldId);
		if(!field) return;
		return field.value ? field.value : "";
	}
	
	/**
	 * Wstawia wartosci do komboboksa o podanym id
	 * 
	 * @param String selectId Id komboboksa
	 * @param Object options Opcje do komboboksa. Indeksy sa traktowane jako id,
	 *                       a wartosci jako teksty
	 * @param String selectedId Id zaznaczonego elementu
	 */
	this.setSelectOptions = function(selectId, options, selectedId) {
		var i, id, select, selectedIndex;
		
		select = document.getElementById(selectId);
		if(!select) return;
		
		select.options.length = 0;
		selectedIndex = 0; i = 0;
		for(id in options) {
			select.options[i] = new Option(options[id], id);
			if(""+id == ""+selectedId) selectedIndex = i;
			i++;
		}
		select.selectedIndex = selectedIndex;
	}
	
	/**
	 * Wysyla hash hasla,login i liczbe losowa (token) z formularza od logowania
	 *
	 * @param formularz do logowania
	 */
	this.sendLogin = function(form, token) {
		if (""+form.login.value == "") { 
			form.login.focus();
			return false;
		}
		if (""+form.password.value == "") { 
			form.password.focus();
			return false;
		}
		var temp = hex_md5(hex_md5(form.password.value) + token);
		form.password.value = temp;
	    return true;
	}
}

if(!window["formHelper"]) formHelper = new FormHelper();
