/*
 * WebCT95
 *
 * Copyright (C) 2003 Conseil Général du Val d'Oise. All Rights
 * Reserved.
 *
 * Developed by IDEALX S.A.S. and Business Decision
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

function Request(label,nameForm){
	this.label=label;
	this.elements=new Array();
	this.data=null;
	this.nameForm=nameForm;
	this.form=new Forms(this.label+'.form',nameForm,null,this);
	this.user=null;

	this.EXTERN='extern';

	this.setData=function(data){
		this.data=data;
		this.form.data=data.form;
	};


	this.createXml=function(){
		var dateCreated=(this.data.dateCreated!='')?this.data.dateCreated.getYMDHMS():'';
		var dateEnd=(this.data.dateEnd!='')?this.data.dateEnd.getYMDHMS():'';

		var xml='<request id="'+this.data.id+'" key="'+this.data.key+'" '
		+'state="'+this.getState()+'" dateCreated="'+dateCreated+'" dateEnd="'+dateEnd+'" '
		+'dateCreatedFormatted="'+this.data.dateCreatedFormatted+'" dateEndFormatted="'+this.data.dateEndFormatted+'" '
		+'topicId="'+this.data.topicId+'">'
		+this.getXmlReferer();
		var divisionId = this.data.form.divisionId;
		var choice=this.getChoice();
		if (choice == 'message')
			divisionId = this.getForm().newGroup.value;
		var att={
			xslPath:this.data.form.xslPath,
			id:this.data.form.id,
			versionId:this.data.form.versionId,
			divisionId:divisionId
		};
		xml+=this.form.createXml(att)
		+this.getXmlVersions()
		+this.getXmlMessages()
		+this.getXmlSMS()
		+this.getXmlComments()
		+'</request>';
		return xml;
	};

	this.getXmlReferer=function(){
		var content='';
		var referer=this.data.referer;
		if(referer=='' && !this.form.isInBO){
			referer=document.referrer;
		}
		if(referer!='') content='<referer>'+this.form.printCDATA(referer)+'</referer>';
		return content;
	};

	this.getVersionId=function(){
		return this.data.versions.length+1;
	};

	this.getState=function(){
		var form=this.getForm();
		var choice=this.getChoice();
		if(choice=='changeState') return this.getNewState();
		if(choice=='close') return 'D';
		//if(choice=='response'){
			//if(form.includeForm && form.includeForm.checked && this.isExtern(this.getTo())) return 'R';
			//else return 'D';
		//}
		return this.data.state;
	};

	this.getNewState=function(){
		var form=this.getForm();
		return (form.newState)?form.newState.value:this.data.state;
	}

	this.getChoice=function(){
		var form=this.getForm();
		return (form.choice)?form.choice.value:'';
	}

	this.getTo=function(){
		var form=this.getForm();
		var choice=this.getChoice();
		if(choice == "transferRequest")
		{
			return form.emailAgent.value;
		}
		
		var to=(choice=='response')?this.EXTERN:'intern';
		to=(to=='intern' && form.messageTo)?form.messageTo[form.messageTo.selectedIndex].value:to;
		return to;
	};

	this.getForm=function(){
		return document.forms[this.nameForm];
	};

	this.getXmlVersions=function(){
		var xml='<historic>';
		if(this.mustAddVersion()){
			var date=new MyDate();
			var id=this.getVersionId();
			var version={id:id,from:this.getUser(),date:date};
			this.data.versions[id-1]=version;
		}
		for(var i in this.data.versions){
			var version=this.data.versions[i];
			xml+='<version id="'+version.id+'" from="'+version.from+'" date="'+version.date.getYMDHMS()+'"/>';
		}
		xml+='</historic>';
		return xml;
	};

	this.mustAddVersion=function(){
		return (this.form.edited && !this.isExtern() || this.isSave() && this.isExtern());
	};

	this.getXmlMessages=function(){
		var xml='<comments>';
		var form=this.getForm();
		var choice=this.getChoice();
		if(choice!='comment' && ((form.message && form.message.value!='') || choice=='response' || choice=='message')){
			var message={
				from:this.getUser(),
				to:this.getTo(),
				date:new MyDate(),
				value:form.message.value,
				includeForm:(form.includeForm)?form.includeForm.checked:false
			};
			this.data.messages[this.data.messages.length]=message;
		}
		for(var i in this.data.messages){
			var message=this.data.messages[i];
			xml+='<message from="'+message.from+'" to="'+message.to+'" date="'+message.date.getYMDHMS()+'" '
			+'includeForm="'+message.includeForm+'"><![CDATA['+message.value+']]></message>';
		}
		xml+='</comments>';
		return xml;
	};

	this.getXmlComments=function(){
		var xml='<icomments>';
		var form=this.getForm();
		var choice=this.getChoice();
		if((form.message && form.message.value!='') && choice=='comment'){
			var comment={
				from:this.getUser(),
				to:this.getUser(),
				date:new MyDate(),
				value:form.message.value,
				includeForm:false
			};
			this.data.comments[this.data.comments.length]=comment;
		}
		for(var i in this.data.comments){
			var comment=this.data.comments[i];
			xml+='<message from="'+comment.from+'" to="'+comment.to+'" date="'+comment.date.getYMDHMS()+'" '
			+'includeForm="'+comment.includeForm+'"><![CDATA['+comment.value+']]></message>';
		}
		xml+='</icomments>';
		return xml;
	};

	this.getXmlSMS=function(){
		var xml='<sms>';
		var form=this.getForm();
		var choice=this.getChoice();
		if((form.smsmessage && form.smsmessage.value!='') && choice=='sendSMS'){
			var sms={
				from:this.getUser(),
				to:form.phone.value,
				date:new MyDate(),
				value:form.smsmessage.value,
				includeForm:false
			};
			this.data.sms[this.data.sms.length]=sms;
		}
		for(var i in this.data.sms){
			var sms=this.data.sms[i];
			xml+='<message from="'+sms.from+'" to="'+sms.to+'" date="'+sms.date.getYMDHMS()+'" '
			+'includeForm="'+sms.includeForm+'"><![CDATA['+sms.value+']]></message>';
		}
		xml+='</sms>';
		return xml;
	};

	this.mySubmit=function(form){
		form.xml.value=this.createXml();
		//alert(form.xml.value);
		return true;
	};

	this.next=function(form){
		if(this.form.validate()){
			this.form.setRenderNextSeparator();
			this.mySubmit(form);
			return true;
		}
		return false;
	};


	this.previous=function(form){
		this.form.setRenderPreviousSeparator();
		this.mySubmit(form);
		form.submit();
	};

	this.isSave=function(){
		for(var i in this.form.data.elements){
			var data=this.form.data.elements[i];
			if(data.type=='separator' && data.rendered) return false;
		}
		return true;
	};

	this.getUser=function(){
		return (this.user!=null)?this.user.id:this.EXTERN;
	};

	this.isExtern=function(user){
		if(!user) user=this.getUser();
		return (user==this.EXTERN);
	};

	this.validate=function(){
		return (this.validateMessage() && this.form.validate());
	};
	
	this.validateBO=function(){
		return (this.validateMessage() && this.form.validateBO());
	};

	this.validateMessage=function(){
		var choice=this.getChoice();
		if(choice=='message' && this.getTo()==0){
			showMessage('Vous devez sélectionner un agent pour envoyer un message interne.');
			return false;
		}
		return true;
	};

};
