//////////////////////////////
function Create(){
	var frm=EI('frm');
	frm.id.value='0';
	frm.key.value='0';
	EI('titlelabel').display='inline';
	EI('attention').innerHTML='新規トピック作成';
	Move(EI('form'), {y:50,x:460}, {display:'block'});
	Followyou.Recreate();
}

function Reply(){
	var frm=EI('frm');
	var row=pvTopicsView.getCurrentRow();
	frm.id.value=row.id;
	frm.key.value='0';
	EI('titlelabel').display='none';
	EI('attention').innerHTML='このスレッドに返信';
	
	Move(EI('form'), {y:50,x:10}, {display:'block'});
	Followyou.Recreate();
}

function CloseForm(){
	var frm=EI('form');
	frm.style.display='none';
}

function Submit(frm){
    var postdata='';
    var url='index.cgi';
    if(frm.id.value==0){
        postdata='mode=create';
    }else{
        postdata='mode=respond';
    }
    postdata+='&id='+frm.id.value+'&key='+frm.key.value;
    postdata+='&title='+frm.title.value;
    postdata+='&uname='+frm.uname.value+'&mail='+frm.mail.value+'&url='+frm.url.value+'&password='+frm.password.value;
    postdata+='&message='+frm.message.value;
    SetCookie(frm);
    postdata=encodeURI(postdata);
    
    Spry.Utils.loadURL("POST", url, true, function(req)
        {
            SubmitFinished(req.xhRequest.responseXML, req.xhRequest.responseText);
        }, {postData:postdata, headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}}
    );
    return false;
}
function SubmitFinished(r,t){
	var mode;
    var id,name,title,date,time,mail,url,message;
	if(!!r){
		mode=r.getElementsByTagName('mode')[0].firstChild.nodeValue;
		if(mode == 'insert'){
			id=r.getElementsByTagName('id')[0].firstChild.nodeValue;
			name=r.getElementsByTagName('name')[0].firstChild.nodeValue;
			date=r.getElementsByTagName('date')[0].firstChild.nodeValue;
			title=r.getElementsByTagName('title')[0].firstChild.nodeValue;
		}else if(mode =='respond'){
			id=r.getElementsByTagName('id')[0].firstChild.nodeValue;
			name=r.getElementsByTagName('name')[0].firstChild.nodeValue;
			date=r.getElementsByTagName('date')[0].firstChild.nodeValue;
			mail=r.getElementsByTagName('mail')[0].firstChild.nodeValue;
			url=r.getElementsByTagName('url')[0].firstChild.nodeValue;
			message=r.getElementsByTagName('message')[0].firstChild.nodeValue;
		}
	}else if(!!t){
	//need to remove tags
		ar=t.split('\n');
		message=ar[1];
		ar=ar[0].split('\t');
		mode=ar[0];
		name=ar[2],title=ar[3],date=ar[4],time=ar[5],mail=ar[6],url=ar[7];
	}else{
		mode='NG';
		message='Some Error Occured';
	}
    
	if(mode=='NG'){alert('NG: '+message);return;}
    if(mode!='insert' && mode!='respond'){alert(message);return;}
    
	if(mode=='insert'){
		var newLine=dsTopics.data.length;
		dsTopics.dataHash[newLine]=new Object();
		dsTopics.setCurrentRow(newLine);
		var row=dsTopics.getCurrentRow();
		row.ds_RowID=newLine;
		row['id']=id;
		row['name']=name;
		row['date']=date;
		row['title']=title;
		dsTopics.data[newLine]=dsTopics.dataHash[newLine];
		dsTopics.sort(['date','time'], 'descending');
		pvTopicsView.updatePagerColumns();
	
	}else if(mode=='respond'){
		var dsT_row=pvTopicsView.getCurrentRow();
		var dsid=dsT_row['id'];
		
		if(dsid==id){
			var newLine=dsThread.data.length;
			dsThread.dataHash[newLine]=new Object();
			dsThread.setCurrentRow(newLine);
			var row=dsThread.getCurrentRow();
			row.ds_RowID=newLine;
			row['name']=name;
			row['date']=date;
			row['time']=time;
			row['message']=message;
			row['mail']=mail;
			row['url']=url;
			dsThread.data[newLine]=dsThread.dataHash[newLine];
			dsThread.sort(['date','time'], 'descending');
			Spry.Data.updateAllRegions();
		}else{
			dsThread.setURL("index.cgi",{method: 'POST', postData: "thread="+id, headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}});
			dsThread.loadDATA();
		}
		
	}else{alert('response error '+ar[0]);return;}
	var frm=EI('frm');
    frm.id.value='0';
	frm.key.value='0';
    frm.title.value='';
    frm.message.value='';
	
	ScrollToTop()
}

function LoadThread(id){
	dsThread.setURL("index.cgi",{method: 'POST', useCache: false, postData: "thread="+id, headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}});
	dsThread.loadData();
	ScrollToTop();
}//

function ScrollToTop(){
//	scrollTo(0,110);
	if(Blink.tim){clearTimeout(Blink.tim);}
	Blink.toX=Blink.scrollx=0;
	Blink.scrolly=MAX(window.scrollY,document.body.scrollTop,document.documentElement.scrollTop);
	Blink.toY=110;
	Blink._Scroll();
}//
/********/
function GetCookie(ds){
	var ck=document.cookie;
	ck+='; ';
	var cks=ck.split("; ");
	var frm=EI('frm');
	var cookie={uname:'',url:'',mail:'',password:''};
	for(var i=0;i<cks.length;i++){
		var a=cks[i].split('=');
		a[1]=decodeURI(a[1]);
		if(cookie[a[0]]!==undefined){
			cookie[a[0]]=a[1];
		}
	}
	var val='';
	for(var i in cookie){
//		if(!!val)val+=',';
//		val+=i+":'"+cookie[i]+"'";
		frm.elements[i].value=cookie[i];
	}
//	ds.loadDataIntoDataSet('{'+val+'}');
	return cookie;
}//

function SetCookie(frm, save_days){
	var s='';
	var d=new Date();
	var cookie={uname:'',url:'',mail:'',password:''};
	if(!save_days || isNaN(save_days)){
		save_days=1;
	}
	d.setDate(d.getDate()+save_days);
	cookie.uname=frm.uname.value;
	cookie.mail=frm.mail.value;
	cookie.password=frm.password.value;
	cookie.url=frm.url.value;
	var expday='expires='+d.toGMTString()+';';
	for(var i in cookie){
		s=i+'='+encodeURI(cookie[i])+';';
		document.cookie=s+expday;
	}
}//
/********/
function EI(n){return document.getElementById(n);}
function MAX(){var mx=0;for(var i=0;i<arguments.length;i++){if(isNaN(arguments[i]))continue;if(mx<arguments[i]){mx=arguments[i];}}return mx;}
function Move(el,pos,style){
	el.style.top  = pos.y + 'px';
	el.style.left = pos.x + 'px';
	if(!style) return;
	for(var i in style){
		el.style[i]=style[i];
	}
}

