var capsLockOn = false;
var myLightWindow ;

function _cancel(_o){
	Try.these(
	 function(){ $(_o).style.display="none";},
	 function(){ $(_o).visibility="hidden";}
	);
	return false;
}
function capLock(e){
 kc = e.keyCode?e.keyCode:e.which;
 sk = e.shiftKey?e.shiftKey:((kc == 16)?true:false);
 if(((kc >= 65 && kc <= 90) && !sk)||((kc >= 97 && kc <= 122) && sk))
  capsLockOn = true;
 else
  capsLockOn = false;
}
function setOptions(_obj, _firstOption, _opts){
	/** set the options from an XML
	*<select>=><option>=><value><text><selected>
	*/
	try {
		var _options = _opts.split("<option>");
		var _values = Array();
		var _texts = Array();
		var _selecteds = Array();

		for (i=_obj.length-1; i>=0 ;  i--) {
			Try.these(function(){_obj.remove(i);});
    }

		if(_options.length == 0){
			return false;
		}
		else {
			//if first option exists
			// this is a problem: removed and there is more than 1, add it
			if (_firstOption !="" ){
				var oOption = document.createElement("OPTION");
				oOption.value = "0";
				oOption.text=_firstOption;
				if (document.all){
					_obj.add(oOption);
				}else {_obj.add(oOption,null);}
			}
			for(var i=1; i<_options.length; i++){
				var oOption = document.createElement("OPTION");
				oOption.value = _parseXML(_options[i],"value");
				oOption.text = _parseXML(_options[i],"text");
				oOption.selected = _parseXML(_options[i],"selected");
				if (document.all){
					_obj.add(oOption);
				}
				else {_obj.add(oOption,null);
				}
			}
			return true
		}
	}
	catch(e){
		alert (e.message+"\n"+_opts);
	}
}
function _parseXML(_o,_t){
	var _start = _o.search("<"+_t+">")+_t.length+2;
	var _end = _o.search("</"+_t+">");
	if ( _end >_start ){
		return _o.substr(_start, _end - _start);
	}
	return false;
}
function hideDivs(_divs){
	_divs.each( function(_toHide){
		Try.these(
			function(){$(_toHide).style.display='none';},
			function(){$(_toHide).visibility="hidden";}
		);
	});
	return false;
}
function showDivs(_divs){
	_divs.each(function(_toShow){
		Try.these(
			function(){$(_toShow).style.display='block';},
			function(){$(_toShow).visibility="visible";}
		);
	});
	return false;
}
function trim (s){
return s.replace(/^\s+/g,'').replace(/\s+$/g,'')
}
function toggleDiv(_toHide,_toShow){
	//	alert(_toHide+";"+_toShow);
	if (_toHide!=""){
		Try.these(
			function(){$(_toHide).style.display='none'},
			function(){$(_toHide).visibility="hidden";}
		);
	}
	if (_toShow!=""){
		Try.these(
			function(){$(_toShow).style.display='block'},
			function(){$(_toShow).visibility="visible";}
		);
	}
	return false;
}
function lwOrNot(el){
	if (getIframeElement("lightwindow_iframe",el ) != null) {
		return getIframeElement("lightwindow_iframe",$(el) );
	} 
	else {
		return $(el);
	}
}
// _flag<0: _open is closed
function toggleFadeScale(_close,_cdur,_open,_odur,_oHeight,_oWidth,_flag){
	closediv = $(_close);//lwOrNot(_close);
	opendiv = $(_open);//lwOrNot(_open);	
	if (_flag>0) {
		new Effect.Fade(closediv,{duration:_cdur});
		new Effect.Scale(opendiv, 10000, {queue:'end', duration:_odur,scaleX:false,scaleY:true,scaleContent:false,scaleMode: { originalHeight: _oHeight, originalWidth: _oWidth }});
		opendiv.style.overflow="auto";
	}
	else if (_flag<0){
		opendiv.style.overflow="hidden";
		new Effect.Scale(opendiv, 0, {duration:_odur,scaleX:false,scaleY:true,scaleContent:false});
		new Effect.Appear(closediv,{queue:'end',duration:_cdur});
	}
}
function openLW(_hr,_t, _w,_h){
	//close if one is opened;
	//closeLW();
	// open the login  window
	if (myLightWindow==null || typeof myLightWindow =="undefined" ){ 
		myLightWindow = new lightwindow(); 
	}
	_options={href: _hr,title: _t}
	if (_w>0) {_options['width']=_w;}
	if (_h>0) {_options['height']=_h;}

	
	myLightWindow.activateWindow(_options);
	return false;
}
function closeLW(){
	if (!(typeof myLightWindow =="undefined") ){
		myLightWindow.deactivate();
	}
	return false;
}

function findElement(_typ,  _value){
	_els = Array();
	_els = $$(_typ);
	_els.each( function(e){
		if (e.htmlFor == _value){
			return e;
		}
	});
}
function clearAnyBrowse(_o){
	$(_o).update($(_o).innerHTML);
}
function clearForm(_f){
	$(_f).getInputs().each(function (field){field.clear();});
	$(_f).getElements().each(function (e){
		if (e.type== "select-one") {
			e.selectedIndex=0;
		}
		if (e.type == "textarea"){
			e.value = "";
		}
	});

}
function setError(_id,_o,_str){
	Try.these(function(){$(_id).remove();});
	advice = "<span id='"+_id+"' class='reg-error red '>"+_str+"</span>";
	new Insertion.After(_o, advice);
	return false;
}
function resetError(_id){
	Try.these(function(){$(_id).remove();});
	return false;
}
function setValidMessObject(_o,_tag){
	var _ret = false;
	_li = _o.parentNode;
	_li.immediateDescendants().each( function(e){
		if (e.tagName == _tag.toUpperCase()){
			_ret =  e;
		}
	});
	return _ret;
}
function getIframeElement(_f,_e){
	if(navigator.appName=="Microsoft Internet Explorer"){
		_d = document.frames[_f].document;
	}
	else {
		_d = document.getElementById(_f).contentDocument;
	}
	return _d.getElementById(_e) ;
}
function setPage(_pn,_ctrl,_fp){
	var _params = new Array();
	_params['job']= "gp";

	_params['pn'] = _pn;
	_params['fp']= _fp;
	//return false;
	if (!($("currpage"))==null){
		$("currpage").value=_pn;
	}
	var newAJAX = new Ajax.Request(
		"index.php?p="+_ctrl, {
			method: "post",
			parameters: _params,
			onComplete: function(response) {
				if (response.responseText.substr(0,4) == "ERR:"){
					//redisplay the form
					alert("Error: "+response.responseText.substr(4));
					return false;
				}
				$("paginationTop").update(response.responseText);
				$("paginationBottom").update(response.responseText);
				return false;
			}
		}
	);
}
function nextBlock(_pn,_fp){
	getModels(_pn,_fp);
//	setPage(_pn,_fp);
	return false;
}
function sendData(_f){
	_frm = $(_f);
	_frm.submit();
}
function showWaitLarge(o){
	$(o).update("<img src='resources/ajax-loading.gif' height='30px'/>");
}
function showWait(o){
	$(o).update("<center><img src='resources/ajax-loading.gif' height='20px'/></center>");
}
function showWaitInLine(o){
	$(o).update("<img id='waitimg' src='resources/ajax-loading.gif' height='20px'/>");
}
function hideWait(){
	$("waitimg").remove();
}
function emailCheck(email) {
	var at="@"
	var dot="."
	var lat=email.indexOf(at)
	var lemail=email.length
	var ldot=email.indexOf(dot)
	if (email.indexOf(at)==-1){
	 return false
	}
	if (lat==-1 || lat==0 || lat>=lemail-4){
	 return false
	}
	if (ldot==-1 || ldot==0 || ldot>=lemail-2){
	  return false
	}
	if (email.indexOf(" ")!=-1){
	  return false
	}
	if (email.indexOf(at,(lat+1))!=-1){
	  return false
	}
	if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){
	  return false
	}
	if (email.indexOf(dot,(lat+2))==-1){
	}
	return true
}
function checkDate(day, month,year){
	var leap = 0;
	var err = 0;
	var i;

   err = 0;
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "1") || (month == "0") || (month == "5") || (month == "7") || (month == "8") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "4") || (month == "0") || (month == "9") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) || (month == 0) || (year == 00)) {
      err = 0; // error already caught
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */

   if (err == 0) {
      return true
   }
   /* Error-message if err != 0 */
   else {
   	return false
   }
}
function stripHTML(_s){
	var rx= /<\S[^><]*>/g
	return _s.replace(rx, "");
}
// STYLING FILE INPUTS 1.0 | Shaun Inman <http://www.shauninman.com/> | 2007-09-07
if (!window.SI) { var SI = {}; };
SI.Files =
{
	htmlClass : 'SI-FILES-STYLIZED',
	fileClass : 'upld-button',
	wrapClass : 'cabinet',

	fini : false,
	able : false,
	init : function()
	{
		this.fini = true;

		var ie = 0 //@cc_on + @_jscript_version
		if (window.opera || (ie && ie < 5.5) || !document.getElementsByTagName) { return; } // no support for opacity or the DOM
		this.able = true;

		var html = document.getElementsByTagName('html')[0];
		html.className += (html.className != '' ? ' ' : '') + this.htmlClass;
	},

	stylize : function(elem)
	{
		if (!this.fini) { this.init(); };
		if (!this.able) { return; };

		elem.parentNode.file = elem;
		elem.parentNode.onmousemove = function(e)
		{
			if (typeof e == 'undefined') e = window.event;
			if (typeof e.pageY == 'undefined' &&  typeof e.clientX == 'number' && document.documentElement)
			{
				e.pageX = e.clientX + document.documentElement.scrollLeft;
				e.pageY = e.clientY + document.documentElement.scrollTop;
			};

			var ox = oy = 0;
			var elem = this;
			if (elem.offsetParent)
			{
				ox = elem.offsetLeft;
				oy = elem.offsetTop;
				while (elem = elem.offsetParent)
				{
					ox += elem.offsetLeft;
					oy += elem.offsetTop;
				};
			};

			var x = e.pageX - ox;
			var y = e.pageY - oy;
			var w = this.file.offsetWidth;
			var h = this.file.offsetHeight;

			this.file.style.top		= y - (h / 2)  + 'px';
			this.file.style.left	= x - (w - 30) + 'px';
		};
	},

	stylizeById : function(id)
	{
		this.stylize(document.getElementById(id));
	},

	stylizeAll : function()
	{
		if (!this.fini) { this.init(); };
		if (!this.able) { return; };

		var inputs = document.getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++)
		{
			var input = inputs[i];
			if (input.type == 'file' && input.className.indexOf(this.fileClass) != -1 && input.parentNode.className.indexOf(this.wrapClass) != -1)
			{
				this.stylize(input);
			};
		};
	}
};
/******* styled selects ********/
function selectReplacement(obj) {
	var origClass = obj.className;
  // append a class to the select
  //obj.className += ' replaced';
  // create list for styling
  var ul = document.createElement('ul');
  ul.className = origClass+' selectReplacement';
  var opts = obj.options;
  for (var i=0; i<opts.length; i++) {
    var selectedOpt;
    if (opts[i].selected) {
      selectedOpt = i;
      break;
    } else {
      selectedOpt = 0;
    }
  }
  for (var i=0; i<opts.length; i++) {
    var li = document.createElement('li');
    var txt = document.createTextNode(opts[i].text);
    li.appendChild(txt);
    li.selIndex = opts[i].index;
    li.selectID = obj.id;
    li.onclick = function() {
      selectMe(this);
    }
    if (i == selectedOpt) {
      li.className += 'selected';
      li.onclick = function() {
        this.parentNode.className += ' selectOpen';
        this.onclick = function() {
          selectMe(this);
        }
      }
    }
    if (window.attachEvent) {
      li.onmouseover = function() {
        this.className += ' hover';
      }
      li.onmouseout = function() {
        this.className =
          this.className.replace(new RegExp(" hover\\b"), '');
      }
    }
    ul.appendChild(li);
  }
  // add the input and the ul
  obj.parentNode.appendChild(ul);
}
function selectMe(obj) {
  var lis = obj.parentNode.getElementsByTagName('li');
  for (var i=0; i<lis.length; i++) {
    if (lis[i] != obj) { // not the selected list item
      lis[i].className='';
      lis[i].onclick = function() {
        selectMe(this);
      }
   } else {
      setVal(obj.selectID, obj.selIndex);
      obj.className='selected';
      obj.parentNode.className =
        obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
      obj.onclick = function() {
        obj.parentNode.className += ' selectOpen';
        this.onclick = function() {
          selectMe(this);
        }
      }
    }
  }
}
function setVal(objID, selIndex) {
  var obj = document.getElementById(objID);
  obj.selectedIndex = selIndex;
}
function setForm() {
  var s = document.getElementsByTagName('select');
  for (var i=0; i<s.length; i++) {
    selectReplacement(s[i]);
  }
}
function closeSel(obj) {
  // close the ul
}
window.onload = function() {
//  (document.all && !window.print) ? null : setForm();
}
