// JavaScript Document

// AJAX //
/**
 * ONLIE CHAT GET MSG'S
 */
function get_msg() {
	if($('chat_box')) {
		new Ajax.Request('ajax.php?opt=onlinechat&task=get_msg',{
			parameters: { 'up_id':$('up_id').value,'last':$('last_id').value },
			onSuccess : function(transport) {
				json = transport.responseText.evalJSON();
				// DO THE TASKS //
				// set last id //
				last = json.length - 1;
				lastID = json[last].order;
				if(lastID!=0) $('last_id').value = lastID;			
				
				for(i=1; i<json.length; i++) {
					window.item_id = json[i].id;
					
					chatbox = $('chat_box');
					li = document.createElement('li');
					if(json[i]._my) li.className = 'my';
					lab = document.createElement('label');
					span = document.createElement('span');
					p = document.createElement('p');
					
					lab.innerHTML = json[i].short_desc;
					span.innerHTML = json[i].sdate;
					p.innerHTML = json[i].desc;
					
					li.appendChild(span);
					li.appendChild(lab);
					li.appendChild(p);
					chatbox.appendChild(li);					
				}
				// CHAT DATA //
				$('chat_with').innerHTML = json[0]._with;
				
				// scroll to bottom //
				if(lastID && lastID==$('last_id').value) $('outer_chat_box').scrollTop = $('outer_chat_box').scrollHeight;
			}
		});
		

	} else window.LiveChat.stop();
}

function SendForm(formid,element) {	
	document.body.style.cursor = 'wait';
	$(formid).request({
		evalScripts: true,
		onError: function() { alert('error'); },
		onComplete: function(transport) {
			document.body.style.cursor = 'auto';
			$(element).innerHTML = transport.responseText;
		}
	})
}


// SCROLABLE CLASS //
function scroll_left(objid) {
	if(!window._offset) window._offset = $(objid).scrollLeft;
	window._offset += 3;
	$('login').value = window._offset;
	$(objid).scrollLeft = window._offset;
	
	if(window.timevar) clearTimeout(window.timevar);
	window.timevar = setTimeout('scroll_left("'+objid+'")',10);
}



// FROM VALIDATION //
/*
var FormValidation = Class.create();
FormValidation.prototype = {
	initialize: function(element) {
		this.element = $(element);
		this.formid = element;
		this.msg = Array();
	    this.options = arguments[1] || {};

		this.requireds = $$('#'+this.formid+' input[class=required]');
		this.requireds2 = $$('#'+this.formid+' input[class="required_marked"]').reduce();
		this.requireds3 = $$('#'+this.formid+' select[class=required]').reduce();
		this.requireds4 = $$('#'+this.formid+' select[class="required_marked"]').reduce();
		this.requireds5 = $$('#'+this.formid+' textarea[class=required]');
		this.requireds6 = $$('#'+this.formid+' textarea[class="required_marked"]').reduce();
		this.requireds = Array(this.requireds,this.requireds2,this.requireds3,this.requireds4,this.requireds5,this.requireds6);
		this.requireds = this.requireds.flatten();
		this.requireds = this.requireds.compact();
		
		button = $$('#'+this.formid+' input[class=button]');
		button.each( function(s) {
			s.style.width = '82px';
		});

		// default data //
		this.valid = true;
		this.element.onsubmit = function() { return false; }
  	 	this.checkForm = this.checkRequired.bind(this);
		
		Event.observe(this.element, 'submit', this.checkForm);
	},
	checkRequired: function() {
		// get required //
		this.valid = true;		
		this.msg = Array();
		
		for(var i=0; i<this.requireds.length; i++) {
			if(!this.requireds[i].value) { 
				this.valid = false;
				// mark required //
				this.requireds[i].className = 'required_marked';
				this.msg[0] = 'Pleas fill hightlited fields!';
				//namecol = $('name_'+this.requireds[i].name);
				//namecol.style.color = '#b20000';
			}
			else {
				// unmark required //
				this.requireds[i].className = 'required';
				//namecol = $('name_'+this.requireds[i].name);
				//namecol.style.color = '';
			}
		}
		if(this.options.compare) {
			for(var i=0; i<this.options.compare.length; i++) {
				s = this.options.compare[i];
				obj = $$('#'+this.formid+' input[name='+s+']').reduce();
				obj2 = $$('#'+this.formid+' input[name=confirm_'+s+']').reduce();
				
				if(obj.value!=obj2.value) { 
					this.msg[1] = 'Wrong confirmation'; 
					this.valid = false; 
					obj.className = 'required_marked'; 
					obj2.className = 'required_marked';
				}
				else if(obj.value && obj2.value) {
					obj.className = 'required'; 
					obj2.className = '';
				}
			};
		}
		
		if(this.options.accept) {
			for(var i=0; i<this.options.accept.length; i++) {
				s = this.options.accept[i];
				obj = $$('#'+this.formid+' input[name='+s+']').reduce();
				
				if(obj.checked==false) { this.msg[2] = 'Accept the terms of use.'; this.valid = false; }
			}
		}
		
		if(this.element._pin) this.element._pin.value = '12345';
		
		if(!this.valid) { alert(this.msg.join("\n")); return false; }
		else this.element.submit();
	}
}
*/

function clear_input(obj) {
	obj.value = '';
	obj.onfocus = function() { return false; }
}

// BASCET //
function removee(rid,opt) {
	new Ajax.Request('/ajax.php?opt='+opt+'&task=remove&ids[]='+rid,{
		onComplete: function(transport) {
			// update box //
			new Ajax.Updater('basceet_box','/ajax.php?opt=orders&task=box');
		}
	})
	
}
function add(id,opt) {
	new Ajax.Request('/ajax.php?opt='+opt+'&task=add_to_bascet&ids[]='+id,{
		onComplete: function(transport) {
			// update box //
			new Ajax.Updater('basceet_box','/ajax.php?opt=orders&task=box');
		}
	})
}


// SHOW FLASH //
function swf_insert(filename,w,h) {
			var oeTags = '<object type="application/x-shockwave-flash" data="'+filename+'" width="'+w+'" height="'+h+'" id="VideoPlayback" >'
			+ '<param name="movie" value="'+filename+'" />'
			+ '<param name="allowScriptAcess" value="sameDomain" />'
			+ '<param name="quality" value="best" />'
			+ '<param name="bgcolor" value="#FFFFFF" />'
			+ '<param name="scale" value="noScale" />'
			+ '<param name="salign" value="TL" />'
			+ '<param name="wmode" value="transparent" />'
			+ '<param name="FlashVars" value="playerMode=embedded" />'
			+ '</object>';
			var oeTagsIE = '<object type="application/x-shockwave-flash" data="'+filename+'" width="'+w+'" height="'+h+'" id="VideoPlayback"  codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0">'
			+ '<param name="movie" value="'+filename+'" />'
			+ '<param name="allowScriptAcess" value="sameDomain" />'
			+ '<param name="quality" value="best" />'
			+ '<param name="bgcolor" value="#FFFFFF" />'
			+ '<param name="scale" value="noScale" />'
			+ '<param name="salign" value="TL" />'
			+ '<param name="wmode" value="transparent" />'
			+ '<param name="FlashVars" value="playerMode=embedded" />'
			+ '</object>';
		majorVersion=8;
		minorVersion=8;
		installinfo='<div class="get_fla"><a href="https://www.macromedia.com/go/getflashplayer" class="black" target="_blank" ><img src="/images/get_flash.jpg" alt="pobierz Flash Playera" border="0" /><br/></a></div>'; // URL zawierajacy informacje o potrzebie instalacji
		
		if(navigator.product=='Gecko' || navigator.userAgent.indexOf('Opera')!=-1) {
		 if(plugin=navigator.plugins["Shockwave Flash"]) {
		  if(majorVersion > parseInt(plugin.description.substr(plugin.description.indexOf(".")-1, 1))
		  || minorVersion > parseInt(plugin.description.substr(plugin.description.length-2)) )
			document.write(installinfo);  // pobieranie flasha
		  else document.write(oeTags);
		 } else {
			document.write(installinfo);  // pobieranie flasha 
		 }
		}
		else document.write(oeTagsIE);
		//-->
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

