function bsStrCropAfter(where, what){
    var c=where.length;
    var v=what.length;
    for( i=0; 1<c; i++) {
        if( where.substring(i,i+v)==what ) {
            return where.substring(i+v);
        }
    }
}

function bsStrCropBefore(where, what){
    var c=where.length;
    var v=what.length;
    for( i=0; 1<c; i++) {
        if( where.substring(i,i+v)==what ) {
            return where.substr(0, i);
        }
    }
}

function bsImgFilter(file,filter){
    var x=bsStrSplitFileName(file);
    var f=x[0]+'&asp&'+filter+'&bsp&';
    return file.replace(x[0],f);

}

function bsStrSplitFileName(s){
    var sp = s.split('\/');
    var file = sp[sp.length-1];
    /*
    var tt="";
    for(key in sp){
    	tt=tt+"\n -"+key+" -"+sp[key];
    }
    alert(tt);
    */
    var s2=file.split('.');
    return {0:s2[0],1:s2[1]};
}

/* s="x=mn&w=32&h=32&m=c" */
function bsStrImgFilter(f,s){
	var t=bsStrSplitFileName(f);
	return t[0]+"&asp&"+s+"&bsp&."+t[1];
}

/* php strtotime analog */
function mysqlTimeStampToDate(timestamp) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}

function bsDateStrToTime(mysqldatetime){
    if( mysqldatetime.length == 10 ) {
        mysqldatetime=mysqldatetime+' 00:00:00';
    }
    var temp=mysqlTimeStampToDate(mysqldatetime);
    return temp.getDate();
}

function getLiveTime(){
    var today=new Date();
    var h=today.getHours();
    var i=today.getMinutes();
    var s=today.getSeconds();
    var y=today.getFullYear();
    var m=today.getMonth();
    var d=today.getDate();
    // add a zero in front of numbers<10
    m=m+1;
    if( m>12 ) {
        m=12;
    }

    m=checkMasterNulls(m);
    s=checkMasterNulls(s);
    i=checkMasterNulls(i);
    d=checkMasterNulls(d);
    return y+'-'+m+'-'+d+" "+h+":"+i+":"+s;
}

function checkMasterNulls(i){
    if (i<10){
        i="0" + i;
    }
    return i;
}


function bsDataIsEmail(v){
    var r=/[0-9a-z_]+@[0-9a-z_^.]+\.[a-z]{2,4}/i;                                
    if( v.length > 0  ) {
        if( r.test(v)){
            return true;
        }else{
            return false;
        }
    }
}

function goToUrl(sUrl){
    //goto specified URL
    location.href=sUrl;   
}

function createWindow(sText) {
//create new window with content

    myWin= open("", "displayWindow6","width=640,height=480,status=1,toolbar=0,menubar=0,scrollbars=1,resizeable=yes");
    myWin.document.open();
    //myWin.document.write(\"<HTML><HEAD><meta http-equiv='content-type'CONTENT='charset=windows-1251'>\");
    //myWin.document.write(\"<TITLE></TITLE></HEAD>\");
    myWin.document.write(sText);
    myWin.document.close();
}

function createWindowSrc(sSrc, width, height, title) {
    //create new window with content
    var min=1000000;
    var max=9999999;
    var rand=min+Math.floor((max-min+1)*Math.random());
    myWin= window.open(sSrc, "win"+rand,"dependent=yes,width="+width+",height="+height+",status=1,toolbar=0,menubar=0,scrollbars=yes,resizable=1");

    var windowwidth, windowheight;
    /*
    if (window.innerWidth) {
      windowwidth = myWin.outerWidth;
      windowheight = myWin.outerHeight;
    } else {
      windowwidth = myWin.document.body.clientWidth;
      windowheight = myWin.document.body.clientHeight;
    }
    */
    
    windowwidth = width;
    windowheight = height;
      
    var screenwidth = screen.availWidth;
    var screenheight = screen.availHeight;

    myWin.moveTo(
    Math.round((screenwidth - windowwidth) / 2),
    Math.round((screenheight - windowheight) / 2));

    myWin.focus();
}

function showElement(sElementId){
    //set visible style
    var messenger = document.getElementById(sElementId);
    if(messenger.style.visibility == "hidden"){
        messenger.style.visibility = "visible";
    }
}    

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;
    }

    var a={'width':myWidth, 'height':myHeight }
    return a;
}

function isset(varname) {
    if(typeof(  varname ) != "undefined"){
        return true;
    }else{
        return false;
    }
}

function isfunction(varname) {
    if(typeof(  varname ) == "function"){
        return true;
    }else{
        return false;
    }
}

function initImageSelector(form,field,selector,callback){
    if( form && field  && selector ) {
        $(selector).click(function(){
            createWindowSrc("/?module=file5&form=second&nodeid="+nodeid+"&provid="+provid+"&field="+field, 800, 600);            
        });
    }
}

/* rgb(185,12,12) -> 101212 */ 
function bsColorRGB2Hex(c){
    c=c.replace("rgb(", "");
    c=c.replace(")", "");
    var c2=c.split(",");
    return "#"+bsMathDec2Hex(c2[0])+bsMathDec2Hex(c2[1])+bsMathDec2Hex(c2[2]);

}



function toJSON(aData){
    var v={};
    for( key in aData ) {
        v[key]=(aData[key]);
    }
    var t=JSON.stringify(v)
    return t;
}

function bsArray2GetCount(r){
    var cnt=0;
    if( typeof(r)!='undefined' ) {    
        for( key in r ) {
            cnt++;
        }
        return cnt;
    }else{
        return false;
    }
}

function bsArray2NotNull(r){
    var cnt=0;
    if( typeof(r)!='undefined' ) {    
        for( key in r ) {
            cnt++;
        }
        if( cnt>0 ) {
            return true;
        }
        return false;
    }else{
        return false;
    }
}

function bsArrayDump(a){
	var tt="";
	for( key in a){
		if( typeof(a[key])=='object' ){
			tt=tt+"\n ";
			for(key2 in a[key]){
				tt=tt+"\n     "+key2+"="+a[key][key2];	
			}
		}else{
			tt=tt+"\n "+key+"="+a[key];
		}
	}
	alert(tt);
}




