/* IE FIX FOR BUG IN CACHING BACKGROUND IMAGES */
/*@cc_on
   /*@if (@_win32)
     {document.execCommand("BackgroundImageCache",false,true);}
   /*@end 
@*/

function stripHTML(oldString){
	newString = oldString.replace(/<\S[^><]*>/g, "");
  return newString;
}

function trim(val){
	return val.replace(/^\s*/, "").replace(/\s*$/, "");
}

function confirmAction(msg,action) {
   if (confirm(msg)){       
		document.location = action;
   }
}

function displayWindow(name,url, width, height) {
  var top = screen.height/2 - height/2;
  var left = screen.width/2 - width/2;
  var Win = window.open(url, name, 'top=' + top + ', left=' + left + ', width=' + width + ', height=' + height + ', resizable=0, scrollbars=0, menubar=0, status=0' );
}

function displayWindowNoScroll(name,url, width, height) {
  var top = screen.height/2 - height/2;
  var left = screen.width/2 - width/2;
  var Win = window.open(url, name, 'top=' + top + ', left=' + left + ', width=' + width + ', height=' + height + ', resizable=1, scrollbars=0, menubar=0, status=0' );
}
function isUndefined( f ) {
	return (typeof(f)=='undefined');
}

function checkAll( field, check ) {
var i;
	if (isUndefined(field))
		return;
	if (isUndefined(field.length)) {
		field.checked = check;
	} else {
		for(i=0; i<field.length; i++)
			field[i].checked = check;
	}
}

function isChecked( field ) {
var i;
	if (isUndefined(field.length)) {
		return field.checked;
	} else {
		for(i=0; i<field.length; i++) {
			if (field[i].checked)
				return true;
		}
		return false;
	}
}

function isSelected( f, i ) {
	if (isUndefined(i))
		i = 0;
	try {
		return f.options[i].selected;
	} catch(e) {
	}
	return false;
}

//---------------------------------------------------------------------------
/**
 * Left menu
 */
function setCatIcon(id, new_src){
	$(id).src = new_src;
}

//---------------------------------------------------------------------------
/**
 * Functions to cookie management
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

//---------------------------------------------------------------------------
/**
 * Functions to manage option panels Open/Close/SetPanelStatus/GetPanelStatus
 */
function showPanel(panel_id,panel_img,panel_status,lbl_open,lbl_close,img_scr_down,img_src_up){
  element = document.getElementById(panel_id);
  if(panel_status==0){
		new Effect.BlindUp(element, {duration: 0.4});
		document.getElementById(panel_img).src=img_scr_down;
		document.getElementById(panel_img).alt=lbl_open;
		document.getElementById(panel_img).title=lbl_open;
		if(document.getElementById(panel_id+'_close')){
			document.getElementById(panel_id+'_close').style.display = 'none';
		}
		if(document.getElementById(panel_id+'_open')){
			document.getElementById(panel_id+'_open').style.display = 'inline';
	 	}
		if(document.getElementById(panel_id+'_top_close')){
			document.getElementById(panel_id+'_top_close').style.display = 'none';
		}
		if(document.getElementById(panel_id+'_top_open')){
			document.getElementById(panel_id+'_top_open').style.display = 'inline';
	 	}
		panel_status = 1;
		setPanelStatus(panel_id,'close');
	} else {
		new Effect.BlindDown(element, {duration: 0.4});
		document.getElementById(panel_img).src=img_src_up;
		document.getElementById(panel_img).alt=lbl_close;
		document.getElementById(panel_img).title=lbl_close;
		if(document.getElementById(panel_id+'_close')){
			document.getElementById(panel_id+'_close').style.display = 'inline';
		}
		if(document.getElementById(panel_id+'_open')){
			document.getElementById(panel_id+'_open').style.display = 'none';
	 	}
		if(document.getElementById(panel_id+'_top_close')){
			document.getElementById(panel_id+'_top_close').style.display = 'inline';
		}
		if(document.getElementById(panel_id+'_top_open')){
			document.getElementById(panel_id+'_top_open').style.display = 'none';
	 	}
		panel_status = 0;
		setPanelStatus(panel_id,'open');
	}
	return panel_status;
}

function initPanel(panel_id,panel_img,panel_status,lbl_open,lbl_close,img_scr_down,img_src_up,remember_state){
	if(remember_state==1){
		if(readCookie(panel_id+'_st')=='open'){
			document.getElementById(panel_id).style.display = 'inline';
			document.getElementById(panel_img).src=img_src_up;
			document.getElementById(panel_img).alt=lbl_close;
			document.getElementById(panel_img).title=lbl_close;
			if(document.getElementById(panel_id+'_close')){
				document.getElementById(panel_id+'_close').style.display = 'inline';
			}
			if(document.getElementById(panel_id+'_top_close')){
				document.getElementById(panel_id+'_top_close').style.display = 'inline';
			}
			return 0;
		} else {
			document.getElementById(panel_id).style.display = 'none';
			document.getElementById(panel_img).src=img_scr_down;
			document.getElementById(panel_img).alt=lbl_open;
			document.getElementById(panel_img).title=lbl_open;
			if(document.getElementById(panel_id+'_open')){
				document.getElementById(panel_id+'_open').style.display = 'inline';
			}
			if(document.getElementById(panel_id+'_top_open')){
				document.getElementById(panel_id+'_top_open').style.display = 'inline';
			}
			return 1;
		}
	} else {
		if(document.getElementById(panel_id)!=null)
			document.getElementById(panel_id).style.display = 'none';
		if(document.getElementById(panel_img)!=null)
		{
	    	document.getElementById(panel_img).src=img_scr_down;
			document.getElementById(panel_img).alt=lbl_open;
			document.getElementById(panel_img).title=lbl_open;
		}
		if(document.getElementById(panel_id+'_open')){
			document.getElementById(panel_id+'_open').style.display = 'inline';
	 	}
		if(document.getElementById(panel_id+'_top_open')){
			document.getElementById(panel_id+'_top_open').style.display = 'inline';
	 	}
		return 1;
	}
}

function setPanelStatus(panel_id,panel_status){
	createCookie(panel_id+'_st',panel_status,31);
}

var switch_panel_mode = 0;
function switchPanel(panel_id_1,panel_id_2,panel_id_3){
	p1 = document.getElementById(panel_id_1);
	a1 = document.getElementById(panel_id_2);
	d1 = document.getElementById(panel_id_3);

	if(switch_panel_mode==0){
		new Effect.DropOut(p1, {duration: 0.5});
		switch_panel_mode = 1;
		a1.innerHTML = '&raquo;';
		d1.style.display = 'inline';
	} else {
		new Effect.Appear(p1, {duration: 0.5});
		switch_panel_mode = 0;
		a1.innerHTML = '&laquo;';
		d1.style.display = 'none';
	}
}

//---------------------------------------------------------------------------
/**
 * Functions to manage one active tab panels
 */

var menu_home_active_tab;
var src_img_up = '/images/home/arrow_up3.gif'
var src_img_down = '/images/home/arrow_down3.gif'
var demo=0;

function menuPanel_init(menu_home_name,demo){
	menu_home_active_tab = readCookie(menu_home_name);
	if(isNaN(parseInt(menu_home_active_tab))){
		if(demo)
			menu_home_active_tab = '8';
		else	
			menu_home_active_tab = '1';
	}
	if($(menu_home_name+'_'+menu_home_active_tab)==null){
		for(i=0;i<=3;i++){
			if($(menu_home_name+'_'+i)!=null){
				menu_home_active_tab = i;
				break;
			}
		}
	}
	if($(menu_home_name+'_'+menu_home_active_tab)==null){
		menu_home_active_tab = '-1';
	}
	// Show active panel
	if($(menu_home_name+'_'+menu_home_active_tab)!=null){
		$(menu_home_name+'_'+menu_home_active_tab).style.display = 'block';
		$(menu_home_name+'_img_'+menu_home_active_tab).src = src_img_up;
		$('menu_home_title_'+menu_home_active_tab).className = 'tableGreen';
	}
}

function menuPanel_switch(menu_home_name,id){
	if(parseInt(menu_home_active_tab)!=parseInt(id)){
		if(!isNaN(parseInt(menu_home_active_tab))){
			oldclass = $('menu_home_title_'+menu_home_active_tab).className;
			// Hide old active panel
			if($(menu_home_name+'_img_'+menu_home_active_tab)){
				$(menu_home_name+'_img_'+menu_home_active_tab).src = src_img_down;
				if(menu_home_active_tab<0){
					$('menu_home_title_'+menu_home_active_tab).className = 'tableOrange';
				} else {					
					$('menu_home_title_'+menu_home_active_tab).className = 'tableBlue';
				}
				new Effect.SlideUp($(menu_home_name+'_'+menu_home_active_tab), {queue: {position: 'front', scope: 'leftmenuscope',limit: 2}, duration: 0.5});
			}
			if (oldclass.indexOf('categoriesOrange') > -1) {
				$('menu_home_title_'+menu_home_active_tab).className += ' categoriesOrange';
			}
		}
		// Show new active panel
		menu_home_active_tab = id;
		createCookie(menu_home_name,menu_home_active_tab,31);
		$(menu_home_name+'_img_'+menu_home_active_tab).src = src_img_up;
		if ($('menu_home_title_'+menu_home_active_tab).className.indexOf('categoriesOrange') > -1)
			$('menu_home_title_'+menu_home_active_tab).className = 'tableGreen categoriesOrange';
		else
			$('menu_home_title_'+menu_home_active_tab).className = 'tableGreen';
		new Effect.SlideDown($(menu_home_name+'_'+menu_home_active_tab), {queue: {position: 'end', scope: 'leftmenuscope',limit: 2}, duration: 0.5});
	} else {
		// Hide old active panel
		$(menu_home_name+'_img_'+menu_home_active_tab).src = src_img_down;
		oldclass = $('menu_home_title_'+menu_home_active_tab).className;
		if(menu_home_active_tab<0){
			$('menu_home_title_'+menu_home_active_tab).className = 'tableOrange';
		} else {
			$('menu_home_title_'+menu_home_active_tab).className = 'tableBlue';
		}
		if (oldclass.indexOf('categoriesOrange') > -1) {
			$('menu_home_title_'+menu_home_active_tab).className += ' categoriesOrange';
		}
		new Effect.SlideUp($(menu_home_name+'_'+menu_home_active_tab), {queue: {position: 'front', scope: 'leftmenuscope',limit: 1}, duration: 0.5});
		menu_home_active_tab = undefined;
		createCookie(menu_home_name,menu_home_active_tab,31);
	}
}

//---------------------------------------------------------------------------
/**
 * Form functions
 */

function set_all_checkboxes(FormName, FieldName, CheckValue){
	if(!document.forms[FormName])	return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes) return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes){
		objCheckBoxes.checked = CheckValue;
	} else {
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++){
			objCheckBoxes[i].checked = CheckValue;
		}
	}
}

function submit_rate_form(Rate,CanRate,RateMsg){
	if(CanRate==''){
		alert(RateMsg);
		return;
	}
	if(!document.forms['rate_form']) return;
	document.forms['rate_form'].rate.value = Rate;
	document.forms['rate_form'].submit();
}

function isEmail(value) {
	var apos = value.indexOf('@');
	var dpos = value.lastIndexOf('.');
	
	if (value.indexOf(' ') >= 0 || apos < 1 || dpos - apos < 2) {
		return false;
	}
	return true;
}

//---------------------------------------------------------------------------
/**
 * SCORM
 */
function exportScorm() {
	document.getElementById('export_form').submit();
}

//---------------------------------------------------------------------------
/**
 * OFFLINE
 */
function downloadOffline() {
	document.getElementById('offline_form').submit();
}

//---------------------------------------------------------------------------

function setBg(el,val){
	if(val=='on'){
		//el.setStyle({backgroundColor:'#d6edf8'}); //doesn't work on MSIE
		el.style.backgroundColor = '#d6edf8';
	} else {
		//el.setStyle({backgroundColor:'#ffffff'});
		el.style.backgroundColor = '#ffffff';
	}
}

// central banner

// makes els array shifted by shift:
// els=5 : [0,1,2,3,4]
// shift=2 : [2,3,4,0,1]
function picMakeArray (els,shift) {
	if (shift>els) shift = 0;
	var t2 = new Array();
	for (i=shift; i<els; i++)
		t2.push(i);
	for (i=0; i<shift; i++)
		t2.push(i);
	return t2;
}
var changepic_ineffect = false;
var overall_shift = 3; // how many images are switched at the same time (default 1)
function changePic (direction) {
	if (changepic_ineffect) return;
	if (nchangepic > 0) clearInterval (nchangepic);

	if (images_count <= 3) return;
	changepic_ineffect = true;
	sr2width = $('sr2_inner').getDimensions().width;
	mgvisible = Math.floor(sr2width/202); //how many images are visible
	//imgleft = images_count - imgvisible; //how many images left (invisible)
	var shift = current_image + direction*overall_shift;
	if (shift > images_count) shift = 1;
	if (shift < 0) shift = images_count-1;
	
	current_image = shift;
	var shifted = picMakeArray(images_count,shift);

	Effect.Fade('sr_img_0', {from: 1.0, to: 0.01, duration: 0.3, afterFinish: function() {
	}});
	setTimeout (function() {
		Effect.Fade('sr_img_1', {from: 1.0, to: 0.01, duration: 0.3, afterFinish: function() {
		}});
	},150);
	setTimeout (function() {
		Effect.Fade('sr_img_2', {from: 1.0, to: 0.01, duration: 0.3, afterFinish: function() {
		}});
	},300);
	setTimeout (function() {
		Effect.Fade('sr_img_3', {from: 1.0, to: 0.01, duration: 0.3, afterFinish: function() {
			for (i=0; i<images_count; i++) {
				img = 'sr_img_'+i;
				url = 'sr_url_'+i;
				current_img = aimages[shifted[i]];
				$(img).src = current_img.img.replace('.jpg','_thumb.jpg');
				$(url).href = current_img.url;
				$(url).title = current_img.title;
			}
			Effect.Appear('sr_img_0', {from: 0.01, to: 1, duration: 0.3, afterFinish: function() {
			}});
			setTimeout (function() {
				Effect.Appear('sr_img_1', {from: 0.01, to: 1, duration: 0.3, afterFinish: function() {
				}});
			},150);
			setTimeout (function() {
				Effect.Appear('sr_img_2', {from: 0.01, to: 1, duration: 0.3, afterFinish: function() {
				}});
			},300);
			setTimeout (function() {
				Effect.Appear('sr_img_3', {from: 0.01, to: 1, duration: 0.3, afterFinish: function() {
					changepic_ineffect = false;
				}});
			},450);			
		}});
	},450);

	autoChangePic();
}
var nchangepic = 0;
function autoChangePic () {
	nchangepic = setInterval ("changePic(1)", sample_resource_time*1000);
}
var nchangetext = 0;
var current_text = -1;
function autoChangeText () {
	if (nchangetext > 0) clearInterval (nchangetext);
	var next = current_text + 1;
	if (next > banner_texts.length-1)
		next = 0;
	current_text = next;
	Effect.Fade('text_t1', {duration: 0.2, afterFinish: function() {
		with ($('text_t1')) {
			setOpacity(1);
			style.display = 'block';
			innerHTML = banner_texts[current_text].t1;
			if (navigator.appName.indexOf('Explorer') != -1) {
				if (banner_urls[current_text]) {
					onclick = function () {
						document.location.href = banner_urls[current_text];
					}
				}
			}
		}			
	}});
	
	if ($('text_t2') != undefined) {
		Effect.Fade('text_t2', {duration: 0.2, afterFinish: function() {
			with ($('text_t2')) {
				setOpacity(1);
				style.display = 'block';
				innerHTML = banner_texts[current_text].t2;
				if (navigator.appName.indexOf('Explorer') != -1) {
					if (banner_urls[current_text]) {
						onclick = function () {
							document.location.href = banner_urls[current_text];
						}
					}
				}
			}			
		}});
	}

	if (banner_urls[current_text])
		$('banner_url').href = banner_urls[current_text];
	nchangetext = setInterval ("autoChangeText()", banner_text_time*1000);
}

	function showHint (event,el, wx, wy) {
		var obj = $('hint_'+el);
		var x = Event.pointerX(event) + wx/2;
		var y = Event.pointerY(event) + wy;


		if (el == 'cheque') {
			$('hint_transfer').style.display = 'none';
			$('hint_card').style.display = 'none';
		} else if (el == 'transfer') {
			$('hint_cheque').style.display = 'none';
			$('hint_card').style.display = 'none';
		} else if (el == 'card') {
			$('hint_transfer').style.display = 'none';
			$('hint_cheque').style.display = 'none';			
		}
		if (!obj.style.display || obj.style.display=='none') {
			with (obj.style) {
				left = x+'px';
				top = y+'px';
				display = 'block';
			}
			obj.setOpacity(0);
			Effect.Appear('hint_'+el,{duration: 0.5, from: 0, to: 0.95});			
		} else {
			obj.style.display = 'none';
		}
	}

	// loads localization texts
	// first checks if section is already loaded and if it is returns this section
	// otherwise it is loaded via ajax as json array
	var global_texts = {};
	function $txt (txt) {
		var url = '/ajax.php/misc/ajax_load_texts';
		txt_ = txt.split('.');
		section = txt_[0];
		val = txt_[1];
		if (global_texts[section]) {
			return global_texts[section][val];
		}
		new Ajax.Request(url, {
			method: 'post',
			postBody: 'txt='+txt,
			onComplete: function (request) {
				eval ("var vals = "+request.responseText);
				global_texts[section] = vals;
				if (vals[val]) {
					return vals[val];
				} else {
					return null;
				}
			}
		});
		return null;
	}

	// validator class for client-side form validation
	// if validation is set matches defined attributes with form's fields values
	// e.g. Validator.check()
	var Validator = {

		forma: null,
		fields: null,
		messages: null,
		current: null, //first element to focus()
		errorfields: null, //keep references to fields with errors
		
		check: function(forma) {
			this.forma = forma;
			this.fields = this.checkFields();
		},
		// handles form fields validation if validator attribute is defined
		checkFields: function () {
			els = this.forma.elements;
			this.messages = new Array();
			this.errorfields = new Array();
			for (i=0; i<els.length; i++) {
				if (els[i].getAttribute('validate')) {
					try {
						val = els[i].value; //element value
						val = val.replace(/^\s*(.*?)\s*$/,'$1'); // make trim
						validate = els[i].getAttribute('validate');
						validate = "["+validate+"]";
						// prepare array
						txt = validate.replace(/:(.)/g,":'$1",validate); //add ''
						txt = txt.replace(/(.),/g,"$1',",txt); // add ''
						txt = txt.replace(/(.)\]/,"$1'}",txt); // add ''
						txt = txt.replace(/\[/,'{',txt);
						txt = txt.replace('match:','match_:');
						txt = txt.replace('size:','size_:');
						eval ("var attrib = "+txt);
						//alert ('msg='+attrib.msg)

						// check var type
						error = false;
						if (attrib.match_) {
							switch (attrib.match_) {
								case 'alnum': pattern = /^[\w\s]+$/; break;
								case 'integer': case 'int':pattern = /^-?\d+$/; break;
								case 'number': pattern = /^-?(\d*\.)?\d+$/;break;
								case 'url': pattern = /^(https?|ftp):\/\/([-\w]+\.)+[A-Za-z]{2,}(:\ d+)?([\\\/]\S+)*?[\\\/]?(\?\S*)?$/i; break;
								case 'email': pattern = /(^[\w\.!#$%"*+\/=?`{}|~^-]+)@(([-\w]+\.)+[A-Za-z] {2,})$/i; break;
								default: pattern = /.*/;
							}
							if (!val.match(pattern)) {
								error = true;
							}
						}
						// check size if defined
						if (attrib.size_) {
							size_ = attrib.size_.split('-');
							if (size_.length > 1) { // range of size
								if (val.length>size_[1] || val.length<size_[0])
									error = true;
							} else { //single size
								if (val.length > size_[0])
									error = true;
							}
						} else {
							if (val.length < 1)
								error = true;
						}

						if (error) {
							if (!this.current) this.current = els[i];
							if (attrib.msg) {
								// prevent clonning the same messages
								if (this.messages.indexOf(attrib.msg) == -1)
									this.messages.push(attrib.msg);
							}
							this.errorfields.push(els[i]);
							if (attrib.highlight!='false')
								els[i].style.backgroundColor = '#FFEFEF';
							if (attrib.clear)
								els[i].value = '';
						}

					} catch (e) {						
					}
				}
			}
			// display messages
			if (this.messages.length > 0) {
				this.displayError(this.messages.join("\n"));
				this.current.focus();
				return false;
			}
			return true;

		},

		displayError: function (msg) {
			alert (msg);
		}

	};

	/**
	 * make form validation without setting validate attribute in form's field
	 * e.g.
	 * $('forma').validate ({
	 *   field1_name: "match:email,msg:message1",
	 *	 field2_name: "match:number,msg:message2"
	 * });
	 * you can set:
	 * match: [email|url|integer|number|alnum] - data type validation
	 * msg: message to display if error occurs
	 * size: field length or range of length
	 * clear: default false - clear field if error occurs
	 * highlight: default true - highlight input with error
	 */

	/**
	 * @todo make it work in IE!
	Element.prototype.validate = function(val) {
		el = this;
		els = this.elements;
		for (i=0; i<els.length; i++) {
			//alert ('els='+els[i]+' => val='+val[els[i].name]+' => els.name='+els[i].name+' =>> typeof='+(typeof val[els[i].name]))
			if (typeof val[els[i].name] == 'object') {
				v = val[els[i].name];
				s = new Array();
				for (var o in v) {
					s.push(o+':'+v[o]);
				}
				v2 = s.join(',');
			    els[i].setAttribute('validate',v2);
			}
		}
	}
	*/
	