//
// common library script.
// Copyright (c) 2011 by ascii_t
//

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1) { endstr = document.cookie.length; }
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg) {
      return getCookieVal (j);
      }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

function SetCookie(name, value, expires, path, domain, secure) {

    var today = new Date();
    today.setTime(today.getTime());

    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" +escape( value ) +
        ((expires) ? ";expires=" + expires_date.toGMTString() : "" ) +
        ((path) ? ";path=" + path : "" ) +
        ((domain) ? ";domain=" + domain : "" ) +
        ((secure) ? ";secure" : "" );
}

function DeleteCookie(name, path, domain) {
    if (GetCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? ";path=" + path : "") +
            ((domain) ? ";domain=" + domain : "" ) +
            ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
}

function highlight(id) {
  var eid = document.getElementById(id);
  eid.style.backgroundColor = "#EFEfEF";
}

function unhighlight(id) {
  var eid = document.getElementById(id);
  eid.style.backgroundColor = "#FFFFFF";
}

function onblocker() {
	var bgblocker = document.createElement('div');
	bgblocker.setAttribute('id', 'dbgblocker');
	
	bgblocker.style.position = "absolute";
	bgblocker.style.zIndex = "0";
	bgblocker.style.background = "#eeeeee";
	bgblocker.style.display = "block";
	bgblocker.style.opacity = "0.5";
	bgblocker.style.top = "0";
	bgblocker.style.left = "0";
	bgblocker.style.width = "100%";
	bgblocker.style.height = "100%";
	document.body.appendChild(bgblocker);
}

function offblocker() {
	var bgblocker = document.getElementById('dbgblocker');
	if (bgblocker)
		document.body.removeChild(bgblocker);
}

function getRadioSelectedIndex(obj) {
    var re = -1;
    for (i=0; i < obj.length; i++){
        if (obj[i].checked) {
            re = i;
            break;
        }
    }
    return re;
}

function getOptionSelectedIndex(obj) {
    var re = new Array(); 
    for (var i=0; i < obj.options.length; i++) 
        if (obj.options[i].selected) 
            re.push(i);
    return re;        
 }
 
 function getOptionSelectedValue(obj) {
    var re = new Array(); 
    for (var i=0; i < obj.options.length; i++) 
        if (obj.options[i].selected) 
            re.push(obj.options[i].value);
    return re;        
 }
 
 function getCheckBoxValue(obj) {
    var re = '0';
    if (obj.checked)
        re = obj.value;
    return re;        
 }

 function addLoadEvent(fn) {
    var UNDEF = "undefined";
    var win=window;
    var doc = document;
    if (typeof win.addEventListener != UNDEF) {
        win.addEventListener("load", fn, false);
    }
    else if (typeof doc.addEventListener != UNDEF) {
        doc.addEventListener("load", fn, false);
    }
    else if (typeof win.attachEvent != UNDEF) {
        addListener(win, "onload", fn);
    }
    else if (typeof win.onload == "function") {
        var fnOld = win.onload;
        win.onload = function() {
            fnOld();
            fn();
        };
    }
    else {
        win.onload = fn;
    }
}

function dobrowse(ul) {
	var popupWidth = 766;
	var popupHeight = 550;

	var div;
	var frm;
	var ptitle;

	var cw = window.innerWidth;
	var ch = window.innerHeight;

	var popupLeft = (cw/2)-(popupWidth/2);
	var popupTop = (ch/2)-(popupHeight/2);

    var url = '../kcfinder/browse.php';
    var ctype = 'images';
	var params = '?type='+ctype;

	popwin=window.open(url+params,"popwin","scrollbars=1,toolbar=0,location=0,directories=0,status=0,menubar=0,width="+popupWidth+",height="+popupHeight+",left="+popupLeft+",top="+popupTop);
	popwin.focus();
    window.KCFinder = {
        callBack: function(url) {
            window.KCFinder = null;
            var eid = document.getElementById(ul);
            eid.value = url;
        }
    };
}    

function dobrowsefile(ul) {
	var popupWidth = 766;
	var popupHeight = 550;

	var div;
	var frm;
	var ptitle;

	var cw = window.innerWidth;
	var ch = window.innerHeight;

	var popupLeft = (cw/2)-(popupWidth/2);
	var popupTop = (ch/2)-(popupHeight/2);

    var url = '../kcfinder/browse.php';
    var ctype = 'files';
	var params = '?type='+ctype;

	popwin=window.open(url+params,"popwin","scrollbars=1,toolbar=0,location=0,directories=0,status=0,menubar=0,width="+popupWidth+",height="+popupHeight+",left="+popupLeft+",top="+popupTop);
	popwin.focus();
    window.KCFinder = {
        callBack: function(url) {
            window.KCFinder = null;
            var eid = document.getElementById(ul);
            eid.value = url;
        }
    };
}    

function doremove(ul) {
	var eid = document.getElementById(ul);
	eid.value = ''; 
}

