// JavaScript Document
var prev;

function frm_validate(FORM)
{
	var error=false;
	l=FORM.length;
	for(i=0;i<l;i++)
		if(FORM[i].name && (!FORM[i].value || FORM[i].value=='http://'))
		{
			FORM[i].style.border='1px solid #ff0000';
			error=true;
		}
		else FORM[i].style.border='';
	if(error) return false;
	else return true;
}

function redirect(url, el)
{
   var cTicks = parseInt(el.innerHTML);

   var timer = setInterval(function()
   {
      if( cTicks )
      {
         el.innerHTML = --cTicks;
      }
      else
      {
         clearInterval(timer);
         window.location = url;	  
      }

   }, 1000);
}

function checkAll(theForm)
{
	for(z=0; z<theForm.length;z++)
	{
		if(theForm[z].type == 'checkbox')
		{
			theForm[z].checked = true;
			if(theForm[z].onchange) theForm[z].onchange();
		}
	}
}

function uncheckAll(theForm)
{
	for(z=0; z<theForm.length;z++)
	{
		if(theForm[z].type == 'checkbox')
		{
			theForm[z].checked = false;
			if(theForm[z].onchange) theForm[z].onchange();
		}
	}
}

function checkInvert(theForm)
{
	for(z=0; z<theForm.length;z++)
	{
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
		{
			theForm[z].checked = (theForm[z].checked+1)%2;
		}
	}
}

//following two funcs don't work
function getElementsByClass (className) {
  var all = document.all ? document.all :
    document.getElementsByTagName('*');
  var elements = new Array();
  for (var e = 0; e < all.length; e++)
    if (all[e].className == className)
      elements[elements.length] = all[e];
  return elements;
}

function stripe_tables()
{
	var table = getElementsByClass("stats");
	var allTRs = table.getElementsByTagName("tr");
	for (i=1; i <= allTRs.length ; i++) {
		if (i % 2 == 1)
			allTRs[i].style.backgroundColor = "#eeeeee";
	}
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return new Array(curleft, curtop);
}

function verify_pass(obj_pass)
{
	if(obj_pass.value.length>=6)
	{
		obj_pass.style.border='';
		return true;
	}
	obj_pass.style.border='1px solid #ff0000';
	return false;
}

function hash_password(login,pass,passobj,tstampobj)
{
	var hash = hex_md5(pass+login);
	var tostamp = (S_TSTAMP-C_TSTAMP) + Math.round(new Date().getTime()/1000);
	hash = hex_md5(hash+tostamp);
	passobj.value = hash;
	tstampobj.value = tostamp;
	
	if(passobj.value) return true;
	else return false;
}

var newwindow;
function popitup(url,sirka,vyska,zlava,zhora) {
	if (newwindow && !newwindow.closed && newwindow.location)
	{
		newwindow.location.href = url;
	}
	else {
		newwindow=window.open(url,'name','height='+vyska+',width='+sirka+',left='+zlava+',top='+zhora+',scrolling=auto');
		newwindow.name='pop_window';
		if (!newwindow.opener) newwindow.opener = self;
	}
	if (window.focus) {newwindow.focus()}
	return false;
}

function addClass(element, value) {
	if(value===false) {
		element.className = '';
	} else if(!element.className) {
		element.className = value;
	} else {
		newClassName = element.className;
		newClassName+= " ";
		newClassName+= value;
		element.className = newClassName;
	}
}

function prepend_zeros(val,n)
{
	if(val.length>=n) return val.substr(-n);
	
	for(var i=0; i<n-val.length; i++) val='0'+val;
	return val;
}

function increment(obj,top,step)
{
	var val=parseFloat(obj.value.replace(',','.'));
	step=parseFloat(step);
	val+=step;
	if(val>top) return false;
	obj.value = val.toFixed(2)
	obj.onchange();
	return true;
}

function decrement(obj,bottom,step)
{
	var val=parseFloat(obj.value.replace(',','.'));
	step=parseFloat(step);
	bottom=parseFloat(bottom)-.001;     // accomodate for precision errors
	val-=step;
	if(val<bottom) return false;
	obj.value = val.toFixed(2);
	obj.onchange();
	return true;
}

function settle(obj,bottom,top,step)
{
	var val=parseFloat(obj.value.replace(',','.'));
	step=parseFloat(step);
	val=Math.ceil(val/step*100)*step/100;
	if(val<bottom) obj.value=bottom;
	else if(val>top) obj.value=top;
	//else obj.value=Math.round(val);
	return true;
}

function htmlentities(str)
{
	return str.split("&").join("&amp;").split( "<").join("&lt;").split(">").join("&gt;");
}

function imposeMaxLength(obj, maxlen)
{
  if(obj.value.length > maxlen) obj.value=obj.value.substr(0,maxlen);
}


function randomkeys(length,num,lowercase,uppercase)
{
	var pattern=''; var max=-1;
	if(!num && !lowercase && !uppercase) return '';
	if(num) { pattern+='1234567890'; max+=10; }
	if(lowercase) { pattern+='abcdefghijklmnopqrstuvwxyz'; max+=26; }
	if(uppercase) { pattern+='ABCDEFGHIJKLMNOPQRSTUVWXYZ'; max+=26; }
	var key=pattern.substr(Math.floor(Math.random()*max),1);
	for(var i=1;i<length;i++)
	{
        key+=pattern.substr(Math.floor(Math.random()*max),1);
	}
	return key;
}

//		******************************************************************************		PRELOAD FUNCTIONS		*********/

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];}}
}

//***********************************************************************************		END of PRELOAD FUNCTIONS	******/

//		******************************************************************************		ROLLOVER FUNCTIONS		*********/
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_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];}
}
//		******************************************************************************		END of ROLLOVER FUNCTIONS		*********/
//		******************************************************************************		CAMPAING  FUNCTIONS				*********/
		
		
		
		function is_in_array(needle,array)
			{
				var contains=false;
				for (var id in array)
				{
					if (needle==array[id])
					{
						contains=true;
						break;
					}
				}
				return contains;
			}
		function array_diff (arr1) {
		    // *     example 1: array_diff(['Kevin', 'van', 'Zonneveld'], ['van', 'Zonneveld']);
		    // *     returns 1: {0:'Kevin'}
		    var retArr = {},
		        argl = arguments.length,
		        k1 = '',
		        i = 1,
		        k = '',
		        arr = {};
		 
		    arr1keys: for (k1 in arr1) {
		        for (i = 1; i < argl; i++) {
		            arr = arguments[i];
		            for (k in arr) {
		                if (arr[k] === arr1[k1]) {
		                    // If it reaches here, it was found in at least one array, so try next value
		                    continue arr1keys;
		                }
		            }
		            retArr[k1] = arr1[k1];
		        }
		    }
		 
		    return retArr;
		}
