﻿

function bmlUpdate( id ){
	var R = [];
	var F;
	var K;
	var V;
	var S;
	
	try{
		eval( "S = template_" + id + ";");
	}
	catch(e){
		return(false);
	}
	
	F = document.getElementById("form_"+id);
	for( i = 0 ; i < F.elements.length ; i++){
		if(! F.elements[i].id) continue;
		K =  F.elements[i].id.substr( id.length + 2 , 255 )
		try{
			eval(" V = get_" + F.elements[i].id + "( F.elements[i].value );") ;
		}
		catch(e){
			V = F.elements[i].value ? F.elements[i].value : '';
		}
		if( typeof( V ) == "object"){
			for ( j = 0 ; j < V.length ; j ++){
				Re = new RegExp("##" + K + "\\[" + j +"\\]##","g");
				S = S.replace( Re , V[j] );
			}
		}else{
			Re = new RegExp("##" + K + "##" ,"g");
			S = S.replace( Re , V);
		}
	}
	//alert(R.length);
	
	BML = document.getElementById( "bml_" + id );
	BML.innerHTML = V;
	BML.href = S.replace(/&quot;/g , '"');
	document.getElementById( "bmlstats_" + id ).innerHTML = "(" + S.length + " char)";
	
	document.getElementById( "code_" + id ).innerHTML = bmlSourcePublish(S);
};

function bmlSourcePublish(S){
	var i,ii,nst='',Ss,Rs='';
	S = S.replace( /&quot;/g, '"' );
	S = S.replace( /\{/g, " {\n" );
	
	S = S.replace( /\}/g, "\n}\n" );
	S = S.replace( /;([^'"])/g, "\;\n$1" );
	
	S = S.replace( /\n\n/g, "\n" );
	S = S.replace( /%20/g, " " );
	S = S.replace( /%5C/g, "\\" );
	
	S = S.replace( /^(for.*;)\n(.*;)\n/mg, "$1 $2 " );
	
	S = S.replace( /</g, "&lt " );
	S = S.replace( />/g, "&gt " );
	
	Ss = S.split('\n');
	
	for( i = 0; i < Ss.length ; i ++){
		if(Ss[i].match(/^}/)) nst --;
		for(ii=0;ii<nst;ii++) Rs += '\t';
		Rs +=  Ss[i] +"\n";
		if(Ss[i].match(/{$/)) nst ++;
	}
	Rs = Rs.replace(/^([^'"]+[^=])(=)([^=])/mg,'$1 $2 $3');	
	Rs = Rs.replace(/([\{\};\(\),.])/g,'<b>$1</b>');
	Rs = Rs.replace(/(function|var|with|for|if|else)/g,'<span style="color:#909">$1</span>');
	
	return Rs;
}


function bmlSourceViewerIni( id ){
	var i,Vw,Bt,Btp,Lb,textShow='[ 表示 ]',textHide='[ 隠す ]',textPop='[ ウインドウで表示 ]';
	
	if (! (Vw = document.getElementById('code_'+id) )){
		return( false);
	}else{
		Lb = document.getElementById('codeLabel_'+id );
		Lb.appendChild ( Bt = document.createElement('a'));
		Lb.appendChild ( Btp = document.createElement('a'));
	}
	with( Bt ){
		href='javascript:void(0)';
		style.cssText = 'font-size:11px;';
		innerHTML = textShow;
		onclick = function(){
			if( Vw.style.display=='none' ){
				toggleElement(Vw,300);
				this.innerHTML = textHide;
			}else{
				toggleElement(Vw,300);
				this.innerHTML = textShow;
			}
		}
	}
	with( Btp){
		href='javascript:void(0)';
		style.cssText = 'font-size:11px;';
		innerHTML = textPop;
		onclick = function(){
			var srcwin = open('about:blank','bmlSource','width=500,height=400,scrollbars=1');
			srcwin.document.write( '<pre style="font-size:12px;">'+ Vw.innerHTML + '</pre>');
			srcwin.document.getElementsByTagName('body')[0].appendChild( document.createElement('a'));

		}
	}
	
	with( Vw ) {
		style.display='none';
	}
	
	
};

function toggleElement(obj,h){
	var timer,i=0, step=8;
	with(obj.style){
		if(display == 'none'){
			height = '0px';
			display = 'block';
			timer = setInterval(function(){
				height = Math.round(h/step * i) +'px' ; i ++ ;
				if(i > step) clearInterval(timer);
			},1);
		
		}else{
			height = h+'px';
			timer = setInterval(function(){
				height = h - Math.round(h/step * i) +'px' ; i ++ ;
				if(i >= step){
					clearInterval(timer);
					display = 'none';
				}
			},1);
		
		}
	}
}
