/**************
FormControl classes
Developer: G Bradley
© Globespan PLC 2008

This file contains classes to control certain types of form inputs. The Abstract
class defines a blank class that is extended to SelectBox, RedioGroup
and SubmitBttn classes.

**************/

FLYG.FormControls={
	Abstract:function(id){
		this.id=id;
		this.control=FLYG.$(id);
		}
	};
	
var fc=FLYG.FormControls;
	
fc.Abstract.prototype={
	clear:function(){},
	getValue:function(){},
	setValue:function(){},
	populate:function(){}
	};
	
fc.SelectBox=function(id,header,skipHeader){
	this.constructor.baseClass.call(this,id);
	this.header=header||false;
	this.skipHeader=skipHeader||false;
	};
fc.RadioGroup=function(id){
	this.constructor.baseClass.call(this,id);
	};
fc.SubmitBttn=function(id){
	this.constructor.baseClass.call(this,id);
	};
fc.Calendar=function(id){
	this.constructor.baseClass.call(this,id);
	};
	
/* Make them subclasses of the abstract class  */
FLYG.extend(fc.SelectBox,fc.Abstract);
FLYG.extend(fc.RadioGroup,fc.Abstract);
FLYG.extend(fc.SubmitBttn,fc.Abstract);
FLYG.extend(fc.Calendar,fc.Abstract);

/* Override prototype methods for SelectBox class */
fc.SelectBox.prototype.clear=function(){
	this.control[0].options.length=this.header ? 1 : 0;
	};
fc.SelectBox.prototype.getValue=function(){
	return this.control[0].options[this.control[0].options.selectedIndex].value;
	};
fc.SelectBox.prototype.setValue=function(v,suffix){
	var o=this.control[0].options, l=o.length;
	for (var i=0;i<l;i++){
		if (o[i].value==v){
			o[i].selected=true;
			this.control[0].selectedIndex=i;
			if (suffix) this.rename(suffix);
			return true;
			}
		}
	return false;
	};
fc.SelectBox.prototype.populate=function(optList,h){
	this.clear(h);
	var self=this.control[0];
	var index=0;
	optList.forEach(function(x,i){
		self.options[self.options.length]=new Option(x.name,x.value,x.selected||false);
		if (x.selected) index=i;
		});
	if (this.skipHeader) this.control[0].selectedIndex=index+(this.header ? 1 : 0);
	return this;
	};
fc.SelectBox.prototype.rename=function(suffix,skip,h){
	if (!this.header) return;
	var o=this.control[0].options, si=o.selectedIndex;
	if (suffix) var re=new RegExp(" "+this.header+"$");
	else var re=new RegExp("^"+this.header+" ");
	var i=o[0].value ? 0 : 1, j=i;
	for (;i<o.length;i++) o[i].text=o[i].text.replace(re,'');
	if (((j && si) || !j) && !skip){
		if (suffix) o[si].text=o[si].text+' '+this.header;
		else o[si].text=this.header+' '+o[si].text;
		}
	};
	
/* Override prototype methods for RadioGroup class */
fc.RadioGroup.prototype.clear=function(header){
	header=false;
	var c=this.control.children();
	for (var x=header||0;x<c.length;x++){
		this.control[0].removeChild(c[x]);
		}
	return this;
	};
fc.RadioGroup.prototype.getValue=function(){
	var r=this.control.$byTag('input');
	for (var i=0;i<r.length;i++){
		if (r[i].checked) return r[i].value;
		}
	return false;
	};
fc.RadioGroup.prototype.setValue=function(v){
	var r=this.control.$byTag('input');
	for (var i=0;i<r.length;i++){
		if (r[i].value==v){
			r[i].checked=true;
			return true;
			}
		}
	return false;
	};
fc.RadioGroup.prototype.populate=function(optList){
	this.clear();
	var self=this;
	optList.forEach(function(x,i){
		var li=document.createElement("li");
		li.innerHTML="<input type='radio' name='"+self.id+"' value='"+x.value+"'"+(!i ? " checked='checked'" : "")+"> "+x.name;
		self.control[0].appendChild(li);
		});
	};

/* Override prototype methods for SubmitBttn class */
fc.SubmitBttn.prototype.clear=function(){
	this.control[0].disabled=true;
	};
	
/* Override prototype methods for Calendar class */

fc.Calendar.prototype.clear=function(){
	this.control.$byTag('td').removeClass('avail','current').forEach(function(x){
		x.innerHTML='';
		});
	};
	
fc.Calendar.prototype.getValue=function(){
	var cell=this.control.$byClass('current');
	if (cell.length && cell[0].innerHTML) return cell[0].innerHTML;
	else return false;
	};
	
fc.Calendar.prototype.setValue=function(v){
	var cell=this.control.$byClass('current');
	if (cell.length) cell[0].className='';
	var cells=this.control.$byTag('td');
	for (var i=0;i<=cells.length;i++){
		if (cells[i].innerHTML==v){
			cells[i].className='current';
			break;
			}
		}
	};
	
fc.Calendar.prototype.populate=function(d,avail,current){
	this.clear();
	d=d.split("/");
	d=new FLYG.Date("01/"+d[1]+"/"+d[2]);
	var offset=d.getDay()-1, days=d.daysInMonth();
	
	avail=avail ? avail.map(function(x){
		return x.split("/")[0]*1;
		}) : [];
	current=current ? current=current.split("/")[0]*1 : false;
	
	var cells=this.control.$byTag('td');
	for (var i=1;i<=days;i++){
		cells[i+offset].innerHTML=i;
		if (avail.indexOf(i)>=0) cells[i+offset].className='avail';
		if (i==current) cells[i+offset].className='current';
		}
	};
	
// The link class
	
fc.Link=function(formControl,userEvent,callbacks){
	this.formControl=formControl;
	this.successor=null;
	this.predecessor=null;
	var self=this;
	if (userEvent) this.formControl.control[userEvent](function(){											// set the listener to handleSelection()
		if (callbacks) callbacks.forEach(function(f){ f() });
		self.handleSelection();
		});
	};

fc.Link.prototype={
	clear:function(){
		this.formControl.clear();
		if (this.successor) this.successor.clear();
		return this;
		},
	collectValues:function(){
		var p=this.predecessor, v={};
		while (p){
			v[p.formControl.id]=p.formControl.getValue();
			p=p.predecessor;
			}
		return v;
		},
	setSuccessor:function(s){
		this.successor=s;
		s.predecessor=this;
		return this;
		},
	autoSelect:function(v,suffix){
		if (this.formControl.setValue(v,suffix)) this.handleSelection(true);
		},
	activate:function(){},
	handleSelection:function(){}
	};
	
function dropDown(menuform){
	selecteditem = menuform.last_minute_holidays_dep.selectedIndex;
	newvalue = menuform.last_minute_holidays_dep.options[ selecteditem ].value;
	location.href="http://www.flyglobespan.com/index.asp?lmh="+newvalue;
}	