/********************************************************************************************************
* POPUP FUNCTIONS
********************************************************************************************************/

//Open PopUp Window
function op(url,w,h,s) {
   var param = 'toolbar=no,location=no,directories=no,status=yes,copyhistory=no,scrollbars=yes,resizable=yes,width='+w+',height='+h;
   var wnd = window.open(url, Math.round(Math.random()*10000000), param);
   if(wnd.opener==null){
      wnd.opener = window;
   }
   wnd.focus ();
}

//Open New PopUp Window
function opw(url,w,h,s){
   if(s==1){
      var param = 'toolbar=no,location=no,directories=no,status=yes,copyhistory=no,scrollbars=yes,resizable=no,width='+w+',height='+h;
   }else{
      var param = 'toolbar=no,location=no,directories=no,status=yes,copyhistory=no,scrollbars=no,resizable=no,width='+w+',height='+h;
   }
   var wnd = window.open(url, Math.round(Math.random()*10000000), param);
   wnd.focus ();
}



/********************************************************************************************************
* USEFUL FUNCTIONS
********************************************************************************************************/

/** JS 'in_array' like PHP => Call: if(in_array(Element,ArrayName,))... **
*************************************************************************/
function in_array(p,a){
   for (i=0;i<a.length;i++)
      if (a[i] == p) return true
   return false
}

/** get x,y pos of 'this' element **
***********************************/
function getPosition(element){

  var elem=element,tagname='',x=0,y=0,position=0;
   
  while ((typeof(elem)=='object')&&(typeof(elem.tagName)!='undefined')){

     y+=elem.offsetTop;
     x+=elem.offsetLeft;
     tagname=elem.tagName.toUpperCase();

     if (tagname=='BODY'){
        elem=0;
     }

     if (typeof(elem)=='object'){
        if (typeof(elem.offsetParent)=='object'){
           elem=elem.offsetParent;
        }
     }  
  }

  position=new Object();
  position.x=x;
  position.y=y;
  return position;
}

/** set a cookie **
******************/
function setCookie(name, value, expires){
   if (!expires){
      expires = new Date();
   }
   document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

/** get cookie vars **
*********************/
function getCookie(name){
   cookie_name = name + "=";
   cookie_length = document.cookie.length;
   cookie_begin = 0;
   while (cookie_begin < cookie_length){

      value_begin = cookie_begin + cookie_name.length;

      if (document.cookie.substring(cookie_begin, value_begin) == cookie_name){
         var value_end = document.cookie.indexOf (";", value_begin);
         if (value_end == -1){
            value_end = cookie_length;
         }
         return unescape(document.cookie.substring(value_begin, value_end));
      }

      cookie_begin = document.cookie.indexOf(" ", cookie_begin) + 1;
      if (cookie_begin == 0){
         break;
      }
   }
   return null;
}

/** delete a cookie **
*********************/
function delCookie(name){
   var expireNow = new Date();
   document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
}


/** js countdown **
*********************/
function jsCountDown(countVal,countId,countSpan){

   if(countVal>=1){
      if(countSpan!='1')
         document.write("<span id='"+countId+"'>"+countVal+"</span>");
      else
         document.getElementById(countId).innerHTML=countVal;
      countVal--;
      window.setTimeout("jsCountDown("+countVal+",'"+countId+"','1')", 1000);
   }else{
      document.getElementById(countId).innerHTML=countVal;
   }
}


/********************************************************************************************************
* SHOW OR HIDE A SPECIFIC ZONE FUNCTIONS
********************************************************************************************************/

function flipBox(who) {
	var tmp; 
	if (document.images['b_' + who].src.indexOf('_on') == -1) { 
		tmp = document.images['b_' + who].src.replace('_off', '_on');
		document.getElementById('box_' + who).style.display = 'none';
		document.images['b_' + who].src = tmp;
	} else { 
		tmp = document.images['b_' + who].src.replace('_on', '_off');
		document.getElementById('box_' + who).style.display = 'block';
		document.images['b_' + who].src = tmp;
	} 
}

/** show or hide "this" zone and save status to cookie **
********************************************************/
function switchZone(objid){

   var obj=document.getElementById('zoneobj_'+objid);
   var img=document.getElementById('zoneimg_'+objid);

   if(!obj) return false;

   if(obj.style.display == "none"){
      if(img){
         img.src = img.src.replace('_on', '_off');
      }
      obj.style.display='';
      updateZoneCookie(objid, false);
   }else{
      if(img){
         img.src = img.src.replace('_off', '_on');
      }
      obj.style.display='none';
      updateZoneCookie(objid, true);
   }

   return false; //always false
}

/** update zone status cookie **
*******************************/
function updateZoneCookie(objid, addHidden)
{
   var hidden = getCookie("zone_hidden");
   var tmp = new Array();

   if(hidden != null){

      hidden = hidden.split("\n");

      for(i in hidden){

         if(hidden[i] != objid && hidden[i] != ""){
            tmp[tmp.length] = hidden[i];
         }
      }
   }

   if(addHidden){
      tmp[tmp.length] = objid;
   }

   expires = new Date();
   expires.setTime(expires.getTime() + (1000 * 86400 * 365));
   setCookie("zone_hidden", tmp.join("\n"), expires);
}