var AJLgreen  = '<img src="img/ico/ajaxloaders/indicator_light_green.gif"/>';

/*-------------------------------------------------------------------------------------------
  Find [left,top] position of an element on page
-------------------------------------------------------------------------------------------*/
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function getParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function get_response_tag(tag,response)
{
	start_tag = "["+tag+"]";
	end_tag   = "[/"+tag+"]";
	spos=response.indexOf(start_tag);
	epos=response.indexOf(end_tag);
	
	if(spos!=-1 && epos!=-1)
	{
		return response.substr(spos+start_tag.length,epos-spos-start_tag.length);
	}
	return "";
}

function cntChars(Object,maxlength)
{
  counter = $("n"+Object.name);
  txt     = Object.value;
  if(txt.length>maxlength) {
	    txt = txt.substr(0,maxlength);
   		Object.value = txt;
   		Object.scrollTop = Object.scrollHeight;
	}
  counter.innerHTML = maxlength - txt.length;
}

objLoader=null;
function showLoader(obj){
 var pos = findPos(obj);
 var pX  = pos[0];
 var pY  = pos[1];
 objLoader  = document.createElement('div');
 objLoader.className    = 'loader';
 objLoader.style.top    = pY + obj.offsetHeight/2-objLoader.offsetHeight/2 + 'px';
 objLoader.style.left   = pX + obj.offsetWidth/2-objLoader.offsetWidth/2 + 'px';
 document.body.appendChild(objLoader);
}

function closeLoader(){
 if(objLoader) document.body.removeChild(objLoader);
 objLoader = null;
}

objReplicate = null;
function replicate_div(obj)
{
 var pos = findPos(obj);
 var pX  = pos[0];
 var pY  = pos[1];
 objReplicate  = document.createElement('div');
 objReplicate.className    = 'modal';
 objReplicate.style.top    = pY + 'px';
 objReplicate.style.left   = pX + 'px';
 objReplicate.style.width  = obj.offsetWidth + 'px';
 objReplicate.style.height = obj.offsetHeight + 'px';
 objReplicate.innerHTML="Aloha";
 document.body.appendChild(objReplicate);
}
function closeRelicate(){
 if(objReplicate) document.body.removeChild(objReplicate);
 objReplicate = null;
}
function showObject(Object)
{
  Object.style.display = 'block';
}
function hideObject(Object)
{
  Object.style.display = 'none';
}

function focusF(Object){ Object.className='focus'; }

function blurF(Object){ Object.className=''; }

modalObject  = null;
dialogObject = null; 
loaderObject = null;

function showModal(){

 modalObject              = document.createElement("div");
 modalObject.className    = 'modal';
 document.body.appendChild(modalObject);
 if(modalObject.offsetHeight<document.body.scrollHeight) modalObject.style.height = document.body.scrollHeight + 'px';
 modalObject.onclick           = closeDialog;
}

function showDialog(html)
{
  wsize                         = getWindowSize();
  dialogObject                  = document.createElement("div"); 
  dialogObject.className        = 'dialog';
  dialogObject.innerHTML        = html; 
  document.body.appendChild(dialogObject);
  dialogObject.style.left       = wsize[0]/2 - dialogObject.offsetWidth/2 + 'px';
  dialogObject.style.top        = wsize[1]/2 - dialogObject.offsetHeight/2 + 'px';
  dialogObject.style.visibility = 'visible';
}

function closeDialog()
{
  if(modalObject) { document.body.removeChild(modalObject); modalObject=null;}
  if(dialogObject){ document.body.removeChild(dialogObject); modalObject=null;}
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth, myHeight];
}

function init()
{
	
   //if(crt_action=='edit' && crt_section=='links') update_anchors();
}
window.onload   = init;
//window.onresize = repositionDialog;

loginWindow = false;

function login_flip(flag)
{
  e  = document.getElementById('dlglogin');
  lg = document.getElementById('lgW');
  
  if(flag){
    p = findPos(document.getElementById('marker'));
    e.style.left = p[0] - lg.offsetWidth+10 + 'px';
    e.style.top  = p[1] + 15 + 'px'
    e.style.visibility = 'visible';
    e.focus();
  }
  else { 
    if (!flag) e.style.visibility = 'hidden';   
  }
}

//php to js

function trim (str, charlist) {
   
    var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '\$1');
    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function status(target,msg){
    if($("#status").length>0){
        e = document.getElementById('status');
        document.body.removeChild(e);   
    }
    e = document.createElement('div');
    e.id = 'status';
    document.body.appendChild(e);
    e.innerHTML = msg;

    if (target!=''){
        
        pos = $("#"+target).position(); //get the target position
        xpos = parseInt($("#"+target).width()/2 - e.offsetWidth/2)+pos.left;
        ypos = parseInt($("#"+target).height()/2 - e.offsetHeight/2)+pos.top;
           
    }else{
        
        xpos = parseInt((document.body.offsetWidth/2)-(e.offsetWidth/2));
        ypos = parseInt((document.body.offsetHeight/2)-(e.offsetHeight/2));
        
    }

    e.style.top = ypos + 'px';
    e.style.left = xpos + 'px'; 
    e.style.visibility = 'visible'; 
    $("#status").fadeTo('slow',1,function(){
        $("#status").fadeTo('slow',0,function(){
           e = document.getElementById('status');
      document.body.removeChild(e);   
        });
      
    });
}

function ucfirst (str) {
    // Makes a string's first character uppercase  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/ucfirst
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: ucfirst('kevin van zonneveld');
    // *     returns 1: 'Kevin van zonneveld'
    str += '';
    var f = str.charAt(0).toUpperCase();
    return f + str.substr(1);
}


