function Company(app){
	this.app = app
}

Company.prototype.open = function(parent){
	var conf = new XML.Document('xmldata', {})
	conf.xmldata.options = {title:'Administration', modale:"0", name:"administration"}
	conf.xmldata.button = [{image:'refresh.gif', action:'refresh'},{image:'close.gif', action:'close'}]
    conf.xmldata.style = {width:'700px', height:'330px', position: 'absolute'}	
    var wnd = new Window(application, conf)

	var doc = new XML.Document('packet', XML.DefaultPacket('company', 'list'))
	wnd.loadContent('company/list.xsl', doc)
}


Company.prototype._add = function(){
	var conf = new XML.Document('xmldata', {})
	conf.xmldata.options = {title:'Add new company', modale:"0", name:"newCompany"}
	conf.xmldata.button = [{image:'close.gif', action:'close'}]
    conf.xmldata.style = {width:'650px', height:'400px', position: 'absolute'}
	var win = new Window( this.app, conf)

	var doc = new XML.Document('packet', XML.DefaultPacket('company', '_add'))
	win.loadContent('company/add.xsl', doc, function(response){
			var box = new ComboBox($('countryList'), response.xmldata.country)
		}.bind(this)
	)
}

Company.prototype.add = function(form){
	var doc = new XML.Document('packet', XML.DefaultPacket('company', 'add'))
	doc.packet.xmldata = this.app.parseForm(form).form	
	if(form.warehouse && form.warehouse.checked){	
		doc.packet.xmldata.warehouse.cdata = '1'	
		doc.packet.xmldata.defaultPriceLevel.cdata = '1'			
	}
	doc.packet.xmldata.country.cdata = $('countryList').getAttribute('param')
	var req = new XML.Request( function(response){
		if(response.packet.type == "response"){
			this.app.getWindowByName('newCompany').close()
			this.app.getWindowByName('administration').refresh()
			this.app.status("new company has been added")
		} else {
			this.app.showError(response.packet.xmldata)
		}
	}.bind(this))
	req.send(this.app.server, doc)
}

Company.prototype.show = function(uid, name){
	uid = uid+''
	var conf = new XML.Document('xmldata', {})
	conf.xmldata.options = {title: name + ' - main info', modale:"0", name:'company_'+uid}
	conf.xmldata.button = [{image:'refresh.gif', action:'refresh'}, {image:'hide.gif', action:'hideContent'}, {image:'close.gif', action:'close'}]
    conf.xmldata.style = {width:'650px', height:'650px', position: 'absolute'}
	var win = new Window(this.app, conf)
	var doc = new XML.Document('packet', XML.DefaultPacket('company', 'get'))
	doc.packet.query.uid = uid
	win.loadContent('company/info.xsl', doc,function(response){
		$(win.id+'_title').innerHTML = this.app.customer.chengeTitle(response.xmldata.fname,response.xmldata.lname, response.xmldata.company_from)+"  General"
	}.bind(this))
}

Company.prototype._edit = function(uid){
	var win = this.app.getWindowByName('company_' + uid)
    $(win.id+'_title').innerHTML = 'Edit company'
	var doc = new XML.Document('packet', XML.DefaultPacket('company', 'get'))
	doc.packet.query.uid = uid
	win.loadContent('company/edit.xsl', doc, function(response){
	   	var a = new XML.Document('xmldata',{})
 		a.xmldata = this.app.parseForm($('company')).form
		this.app.saveState('company_' + uid,a.serialize())													  
			var box = new ComboBox($('countryList'), response.xmldata.country_list)
			$('countryList').setAttribute('param',response.xmldata.country)		
		}.bind(this)
	)
}

Company.prototype.edit = function(uid, form, callback){
	var doc = new JSON.Document('packet', JSON.DefaultPacket('company', 'update'))
    doc.packet.query.uid = uid.toString();	
	doc.packet.xmldata = this.app.parseForm(form);
	
	//todo put that into verification
	/*
	if(form.warehouse){
		if(form.warehouse.checked){	
			doc.packet.xmldata.warehouse.cdata = '1'	
			doc.packet.xmldata.defaultPriceLevel.cdata = '1'			
		}
	}
	*/
	
	var req = new JSON.Request( function(response){
		if(response.packet.type == "response"){
			callback();
		}
	}.bind(this))
	req.send(this.app.server, doc)
}

Company.prototype._remove = function(uid){
	var doc = new XML.Document('packet', XML.DefaultPacket('company', 'remove'))
	doc.packet.xmldata.uid = uid
	var req = new XML.Request( function(response){
		if(response.packet.type == "response"){
			this.app.getWindowByName('company_'+uid).close()
			this.app.getWindowByName('administration').refresh()
			this.app.status("company has been removed")
		} else {
			var conf = new XML.Document('xmldata', {})
			conf.xmldata.options = {title: 'Error', modale:"1"}
			conf.xmldata.button = [{image:'close.gif', action:'close'}]
		    conf.xmldata.style = {width:'300px', height:'150px', position: 'absolute'}

			var win = new Window(this.app, conf)
			var doc = new XML.Document('xmldata', response.packet.xmldata)
			win.setContent('error.xsl', doc)
		}
	}.bind(this))
	req.send(this.app.server, doc)
}


/*
Company.prototype._categories_ = function(uid, uname, action){
	var conf = new XML.Document('xmldata', {})
	conf.xmldata.options = {title:uname+' categories list', modale:"0", name:'categoriesList_'+uid}
	conf.xmldata.button = [{image:'refresh.gif', action:'refresh'}, {image:'hide.gif', action:'hideContent'}, {image:'close.gif', action:'close'}]
	conf.xmldata.style = {width:'300px', height:'250px', position:'absolute'}
    var win = new Window(application, conf)
	var doc = new XML.Document('packet', XML.DefaultPacket('customer_cats', 'list'))
	doc.packet.query.uid = uid
	if(action) doc.packet.query.act = action
	win.loadContent('company/categories.xsl', doc)
}
*/
/*
Company.prototype._payments = function(uid,name){
	var conf = new XML.Document('xmldata', {})
	conf.xmldata.options = {title:name+' categories list', modale:"0", name:'paymentsList_'+uid}
	conf.xmldata.button = [{image:'refresh.gif', action:'refresh'}, {image:'hide.gif', action:'hideContent'}, {image:'close.gif', action:'close'}]
	conf.xmldata.style = {width:'300px', height:'250px', position:'absolute'}
    var win = new Window(application, conf)
	var doc = new XML.Document('packet', XML.DefaultPacket('company', 'listPayments'))
	doc.packet.query.uid = uid
	win.loadContent('company/payments.xsl', doc)
}
*/
/*
Company.prototype.editCategory = function(uid, cid, cname){
  if(cid)
  {
    var action = 'edit'
    var w_name = 'category_'+cid
    var w_titl = action+' '+cname
    var c_act  = 'save'
  }
  else
  {
    var action = 'add'
    var w_name = 'new_category'
    var w_titl = action+' new category'
    var c_act  = 'add'
  }

  if(!cname)
    cname = 'new category'

  var conf = new XML.Document('xmldata', {})
  conf.xmldata.options = {title:w_titl, modale:"0", name:w_name}
  conf.xmldata.button  = [{image:'refresh.gif', action:'refresh'}, {image:'hide.gif', action:'hideContent'}, {image:'close.gif', action:'close'}]
  conf.xmldata.style   = {width:'300px', height:'140px', position:'absolute'}
  var win = new Window(application, conf)

  var doc = new XML.Document('packet', XML.DefaultPacket('company', 'getCategory'))
  if(cid)
    doc.packet.query.cid    = cid+''
  doc.packet.query.uid      = uid+''
  doc.packet.query.wname    = w_name
  doc.packet.query.c_action = c_act
  win.loadContent('company/editcategory.xsl', doc)
}

Company.prototype.addCategory = function(form){
	var doc = new XML.Document('packet', XML.DefaultPacket('company', 'addCategory'))
	doc.packet.xmldata = this.app.parseForm(form).form
	var req = new XML.Request( function(response){
		if(response.packet.type == "response"){
			this.app.getWindowByName('new_category').close()
			this.app.getWindowByName('categoriesList_'+response.packet.xmldata.uid.cdata).refresh()
			this.app.status("new category has been added")
		} else {
			this.app.showError(response.packet.xmldata)
		}
	}.bind(this))
	req.send(this.app.server, doc)
}

Company.prototype.saveCategory = function(form){
	var doc = new XML.Document('packet', XML.DefaultPacket('company', 'updateCategory'))
	doc.packet.xmldata = this.app.parseForm(form).form
	var req = new XML.Request( function(response){
		if(response.packet.type == "response"){
			this.app.getWindowByName('category_'+response.packet.xmldata.cid.cdata).close()
			this.app.getWindowByName('categoriesList_'+response.packet.xmldata.uid.cdata).refresh()
			this.app.status("category has been updated")
		} else {
			this.app.showError(response.packet.xmldata)
		}
	}.bind(this))
	req.send(this.app.server, doc)
}

Company.prototype.removeCategory = function(form){
	var doc = new XML.Document('packet', XML.DefaultPacket('company', 'removeCategory'))
	doc.packet.xmldata = this.app.parseForm(form).form
	var req = new XML.Request( function(response){
		if(response.packet.type == "response"){
			this.app.getWindowByName('category_'+response.packet.xmldata.cid.cdata).close()
			this.app.getWindowByName('categoriesList_'+response.packet.xmldata.uid.cdata).refresh()
			this.app.status("category has been updated")
		} else {
			this.app.showError(response.packet.xmldata)
		}
	}.bind(this))
	req.send(this.app.server, doc)
}
Company.prototype.cancel_view = function(windowname){
	this.app.getWindowByName(windowname).close() 		 
}
*/
/*
Company.prototype._copmany = function(windowname){
	var doc = new XML.Document('packet', XML.DefaultPacket('company', 'list'))
	win = this.app.getWindowByName('administration')
	win.loadContent('company/list.xsl', doc);
}
*/
/*
Company.prototype._categories = function(windowname){
	var doc = new XML.Document('packet', XML.DefaultPacket('customer_cats', 'list'))
	win = this.app.getWindowByName('administration')
	win.loadContent('customer_cats/list.xsl', doc);
}
Company.prototype._payment_methods = function(windowname){
	var doc = new XML.Document('packet', XML.DefaultPacket('payment_methods', 'list'))
	win = this.app.getWindowByName('administration')
	win.loadContent('payment_methods/list.xsl', doc);
}
*/
/*
Company.prototype._payment_types = function(windowname){
	var doc = new XML.Document('packet', XML.DefaultPacket('payment_types', 'list'))
	win = this.app.getWindowByName('administration')
	win.loadContent('payment_types/list.xsl', doc);
}
*/
/*
Company.prototype._country = function(windowname){
	var doc = new XML.Document('packet', XML.DefaultPacket('country', 'list'))
	win = this.app.getWindowByName('administration')
	win.loadContent('country/list.xsl', doc);
}
*/
/*
Company.prototype.close_edit = function(uid){
	var a = new XML.Document('xmldata',{})
	a.xmldata = this.app.parseForm($('company')).form			
	if(this.app.compareState('company_' + uid,a.serialize()))
		this.edit(uid,$('company'));
	else{
		var win = this.app.getWindowByName('company_' + uid)
		var doc = new XML.Document('packet', XML.DefaultPacket('company', 'get'))
		doc.packet.query.uid = uid
		win.loadContent('company/info.xsl', doc);		
	}
}
*/

Company.prototype.uploadPhoto = function(uid){
	var box = new uploadBox(this.app, [{name:'uid', value:uid}, {name:'module', value:'company'}], function(url){		
		$('company_logo_'+uid).src = url+'?'+random();
		this.app.getWindowByName('uploadBox').close()
	}.bind(this))
}

/*
Company.prototype.alertPriceOverwrite= function(obj){
	if(obj.checked==true){
		confirmBox = confirm("Warning, by selecting OK you allow the program to overwrite all prices related to this company by using the default ratios");
		if(confirmBox){
		
		}
		else{
			obj.checked=false;
		}
	}
}*/
