// ---------------------------------------------------------------------
// Browser Detection 
// 
// ---------------------------------------------------------------------
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false; 
isDOM=(document.getElementById)?true:false
isOpera=isOpera5=window.opera && isDOM
isOpera6=isOpera && window.print
isOpera7=isOpera && navigator.userAgent.indexOf("Opera 7") > 0 || navigator.userAgent.indexOf("Opera/7") >= 0
isMSIE=isIE=document.all && document.all.item && !isOpera
isMSIE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false; 
isMSIEmac = ((document.all)&&(isMac)) ? true : false; 
isNC=navigator.appName=="Netscape"
isNC4=isNC && !isDOM
isNC6=isNC && isDOM



// ---------------------------------------------------------------------
// Safe loading into window.onload
//
// Body onload utility (supports multiple onload functions) 
// ---------------------------------------------------------------------
var gSafeOnload = new Array(); 
var gSafeOnloadDelay = new Array();

function SafeAddOnload(f) {
  SafeAddOnloadDelayed(f, 0);
}

function SafeAddOnloadDelayed(f, d) {
  if (isMSIEmac && isMSIE4)  // IE 4.5 blows out on testing window.onload 
  { 
    window.onload = SafeOnload; 
    gSafeOnload[gSafeOnload.length] = f;
    gSafeOnloadDelay[gSafeOnloadDelay.length] = d;
  } 
  else if (window.onload) 
  { 
    if (window.onload != SafeOnload) 
    { 
      gSafeOnload[0] = window.onload; 
      gSafeOnloadDelay[0] = 0;
      window.onload = SafeOnload; 
    } 
    gSafeOnload[gSafeOnload.length] = f; 
    gSafeOnloadDelay[gSafeOnloadDelay.length] = d;
  } 
  else {
    window.onload = f;
  }
} 

function SafeOnload() 
{ 
  var xyziilen = gSafeOnload.length;
  
  for (xyzii = 0; xyzii < xyziilen; xyzii++) {
    
    if (typeof(gSafeOnload[xyzii]) != 'function') {
      gSafeOnload[xyzii] = new Function(gSafeOnload[xyzii]);
    }
    
    if (gSafeOnloadDelay[xyzii] > 0) {
      setTimeout(gSafeOnload[xyzii], gSafeOnloadDelay[xyzii]); 
    }
    else {
      gSafeOnload[xyzii]();
    }  
  }
} 

// ---------------------------------------------------------------------
// Set a layer opacity
// 
// ---------------------------------------------------------------------
function SetLayerOpacity(layername, opacitylevel) {
  if (!isDOM) {
    return false;
  }
  if(isMSIE) {
    document.getElementById(layername).style.filter='alpha(opacity='+opacitylevel+')';
  }
	if(isNC6) {
    document.getElementById(layername).style.MozOpacity = opacitylevel/100;
  }
}



// ---------------------------------------------------------------------
// Set a cookie
// 
// ---------------------------------------------------------------------
function SetCookie(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 GetCookie(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 DeleteCookie(name) {
  SetCookie(name, "", -1);
}


// ---------------------------------------------------------------------
// Open new browser window
// 
// Params:
//
//   pWidth - popup width in px
//   pHeight - popup height in px
//   pXpos - x window position or one of: center, left, right
//   pYpos - y window position or one of: center, top, bottom
//   pURL - URL to open in window - optional
//   pWindowName - name of window - optional
//   pFocusAfterLoad - focus window after load
//   pAddon - turn on addons. give what addons you want to enable separated by ,
//            ie: scrollbars,hotkeys,location,menubar,resizable,titlebar,toolbar
// ---------------------------------------------------------------------
function PopupWindow(pWidth, pHeight, pXpos, pYpos, pURL, pWindowName, pFocusAfterLoad, pAddon) {

  // Position X
  if ('center' == pXpos) {
    pXpos = Math.round((screen.width -  pWidth) / 2);
  }
  else if('left' == pXpos) {
    pXpos = 0;
  }
  else if('right' == pXpos) {
    pXpos = screen.width -  pWidth;
  }
  
  // Position Y
  if ('center' == pYpos) {
    pYpos = Math.round((screen.height - pHeight) / 2);
  }
  else if('top' == pYpos) {
    pYpos = 0;
  }
  else if('bottom' == pYpos) {
    pYpos = screen.height - pHeight;
  }
  
  var addons = '' + 
    'hotkeys=no' + 
    ',location=no' + 
    ',menubar=no' + 
    ',resizable=no' + 
    ',scrollbars=no' + 
    ',status=no' + 
    ',titlebar=no' + 
    ',toolbar=no';
    
  // Replace addons to yes - ie. status=no -> status=yes
  if (pAddon) {
    splitaddons = pAddon.split(',');
    
    for(i = 0; i < splitaddons.length; i++) {
      regexp = new RegExp(splitaddons[i] + '=no', 'i');
      addons = addons.replace(regexp, splitaddons[i] + '=yes');
    }
  }
    
  windowhandle = window.open(
    pURL, pWindowName, 
    'height=' + pHeight +
    ',width=' + pWidth +
    ',screenX=' + pXpos + 
    ',screenY=' + pYpos + 
    ',left=' + pXpos +
    ',top=' + pYpos + 
    addons
  );
  
  // Raise window after window load
  if (true == pFocusAfterLoad) {
    windowhandle.focus();
  }
  
  //windowhandle.opener = window;
  
  return windowhandle;
}


function PopupWindowResize(pWidth, pHeight, pXpos, pYpos) {
  
  // Position X
  if ('center' == pXpos) {
    pXpos = Math.round((screen.width -  pWidth) / 2);
  }
  else if('left' == pXpos) {
    pXpos = 0;
  }
  else if('right' == pXpos) {
    pXpos = screen.width -  pWidth;
  }
  
  // Position Y
  if ('center' == pYpos) {
    pYpos = Math.round((screen.height - pHeight) / 2);
  }
  else if('top' == pYpos) {
    pYpos = 0;
  }
  else if('bottom' == pYpos) {
    pYpos = screen.height - pHeight;
  }
    
  window.resizeTo(pWidth, pHeight);
  
  if (pXpos > 0 && pYpos > 0) {
    window.moveTo(pXpos, pYpos);  
  }
}

// ---------------------------------------------------------------------
//  Loads dynamically script
// ---------------------------------------------------------------------
var gLoadedScripts = new Array();

function LoadScript(pScriptSrc) {
  
  // Check if script was'nt loaded already
  if (true == in_array(pScriptSrc, gLoadedScripts)) {
    return;  
  }
  
  gLoadedScripts.push(pScriptSrc);
  
  script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = pScriptSrc;
  
  document.getElementsByTagName("head")[0].appendChild(script);
  
  return script;
}

function in_array(pNeedle, pHaystack) {
  for(var i in pHaystack) {
    if (pNeedle == pHaystack[i]) {
      return true;
    }  
  }  
  return false;
}



function print_r(pMixed) {
  var tmp = new String;
  
  for(var i in pMixed) {
    tmp += i + ' = ' + pMixed[i] + '\n';
  }
  
  alert(tmp);
}


// ---------------------------------------------------------------------
// Add event to object
// 
// ---------------------------------------------------------------------
function AddEvent(pObject, pHandler, pFunction){

  if (!document.all && document.getElementById){
    pObject.setAttribute(pHandler, pFunction);
  }    
  
  //workaround for IE 5.x and IE 6
  if (document.all && document.getElementById){
    pObject[pHandler.toLowerCase()] = new Function(pFunction);
  }
}


function GetWindowWidth() {
  return parseInt(
    (document.body && !isOpera && document.body.clientWidth)? document.body.clientWidth : (window.innerWidth || 0)
  );
};

function GetWindowHeight() {
  return parseInt(
    (document.body && !isOpera && document.body.clientHeight)? document.body.clientHeight : (window.innerHeight || 0)
  );
};

function CenterLayerOnPage(pLayer, pWidth, pHeight) {
  
  if (document.documentElement.clientWidth < pWidth) {
    leftPom = Math.round((document.documentElement.scrollWidth - pWidth)/2);
  }
  else {
    leftPom = Math.round((document.documentElement.clientWidth - pWidth)/2) + document.documentElement.scrollLeft;
  }
  
  if (document.documentElement.clientHeight < pHeight) {
    topPom = Math.round((document.documentElement.scrollHeight - pHeight)/2);
  }
  else {
    topPom = Math.round((document.documentElement.clientHeight - pHeight)/2) + document.documentElement.scrollTop;
  }
  
  $(pLayer).style.top = topPom + 'px';
  $(pLayer).style.left = leftPom  + 'px';
}

function confrim_box(site, text) {
  var check = window.confirm(text);
  if (check == true) {
    document.location.href = site;
    return true;
  }
}
