function incquantity(id) {
	quanin = document.getElementById('quantity_'+id);
	quanin.value++;
}

function decquantity(id) {
	quanin = document.getElementById('quantity_'+id);
	if (quanin.value != 1) {
		quanin.value--;
	}
}


function showit(which){
var submenu=new Array()
submenu[0]='<a href="/register/client">Register as a client</a> | <a href="/register/therapist">Register as a therapist</a>';
submenu[1]='<a href="/sectors/workplace">Work Place</a> | <a href="/sectors/hospitality">promotion & hospitality</a> | <a href="/sectors/healthservices">health services</a> | <a href="/sectors/veterinary">veterinary & animal health</a> | <a href="/sectors/sports">sports injuries</a>';
menuobj=document.getElementById("describe");
menuobj.style.display = 'block';
clear_delayhide();
thecontent=submenu[which];
menuobj.innerHTML=thecontent;
}

function resetit(e){
var delay_hide=500;
menuobj=document.getElementById("describe");
delayhide=setTimeout("menuobj.style.display='none';",delay_hide);
}

function clear_delayhide(){
if (window.delayhide)
clearTimeout(delayhide)
}

function contains_ns6(a, b) {
while (b.parentNode)
if ((b = b.parentNode) == a)
return true;
return false;
}


var DateTimePicker = Class.create();
DateTimePicker.prototype = {
	initialize: function(element) {
		this.element=$(element);
		this.button = $(Builder.node('img',{src: '/themes/default/graphics/calendar.gif',alt:'Choose Date',id:this.element.id+'_button'}));
		this.element.parentNode.appendChild(this.button);
		Calendar.setup(
			{
				inputField: this.element.id,
				ifFormat: "%d/%m/%Y %H:%M",
				showsTime: true,
				button: this.button.id,
				step:1
			}
		);
	}
}

Event.observe(window,'load',function() {

	setTimeout(function() { postInit(); },500);

},false);


function postInit() {
	if($('bookingForm')&&document.getElementsByClassName('datetimefield','bookingForm').length>0) {
		var dates = document.getElementsByClassName('datetimefield','bookingForm');
		dates.each(function(input) {
			new DateTimePicker(input);
		});
	}
	
	if($('bookingForm')) {
		new Validation('bookingForm');
	}	

	if($('save_form')) {
		new Validation('save_form');
	}	


}

function ajax (params, element) {
	
 	url = '/'+params+'?ajax';
	new Ajax.Request(url, {
			method: 'get',
			onComplete: function(xhr) {
				$(element).innerHTML = xhr.responseText;
			}
		}
	);

};

Ajax.JSONResponse = function(xhr) {
	var data=eval('('+xhr.responseText+')');
	Object.extend(data,Enumerable);
	Object.extend(data,Hash);
	return data;
};

//for attaching (and detaching) a spinning graphic to elements
/*
usage:
new Spinner(element);
element.spin(); - to start spinning
element.stopSpin(); - to stop spinning
*/

var Spinner = Class.create();
if(typeof(themeName)!='undefined') {
	Spinner.src='/themes/'+themeName+'/graphics/spinner.gif';
};

Spinner.prototype = {
	element: {},
	spinner: {},
	//makes an image and attaches it (hidden) to the element
	initialize: function(element) {
		this.element=$(element);
		var spinner= Builder.node('img',{src:Spinner.src});
		spinner=$(spinner);
		//absolute means it won't make things move
		spinner.style.position='absolute';
		spinner.hide();
		spinner.addClassName('spinner');
		element.parentNode.appendChild(spinner);
		this.spinner=spinner;
		//make it so that calling .spin() on the element will pass through
		this.element.spin=this.spin.bind(this);
		//likewise for stopSpin
		this.element.stopSpin=this.stopSpin.bind(this);
		//and for setSpinPosition
		this.element.setSpinPosition=this.setSpinPosition.bind(this);
	},
	//start spinning
	spin: function() {
		Effect.Appear(this.spinner);
	},
	//stop spinning
	stopSpin: function() {
		Effect.Fade(this.spinner);
	},
	//allows the position of the spinner to be changed
	setSpinPosition: function(position) {
		if(position=='before') {
		//	this.spinner.remove();
			this.element.parentNode.insertBefore(this.spinner,this.element);
		}
	}
};

/*
Used for causing a change in a select or text box to cascade to field,
e.g. selecting a company will limit the choice of people
usage:
new CascadeSelect(select_1,select_2);

Where select_1 is the id of the first select object
and select_2 is a complex structure containing an array of:-
        	{
            	container: '',
            	element: '',
            	mandatory: ,
	            url: {
    	            module: '',
        	        submodule: '',
            	    controller: '',
                	action: '',
                	extra_params: ['param_name','form_field',....]
            	}
        	}
container: is optional and refers to the id of the containing DIV; if specified
and the target select is empty, the DIV is hidden so as not to display an empty list
element: is the id of the target select list
mandatory: true or false, if false, the first element in the list is preserved as it is
assumed to be NONE or blank
url: defines the parameters supplied in the url call for the Ajax request; the selected value
of the select_1 list is passed as parameter 'id' and additional parameters can be passed if
required as a pair - the name of the parameter and the form field name that provides the value
for the parameter.
*/
var CascadeSelect= Class.create();
CascadeSelect.prototype = {
	from: {},
	targets: [],
	initialize: function(from,targets) {
		this.from=$(from);
		this.targets=targets;
		this.from.onchange=this.CascadeSelect.bind(this);
//		new Spinner(this.from);
		this.cache=[];
	},
	
	CascadeSelect: function() {
//		this.from.spin();
		this.targets.each(function(target) {
				target.address={
					module: target.url.module,
					submodule: target.url.submodule,
					controller: target.url.controller,
					action: target.url.action
				};
				this._ajaxRequest(target);
			}.bind(this));
//		this.from.stopSpin();
	},
	_ajaxRequest: function(target) {
		var to=$(target.element);
		to.setAttribute('disabled','disabled');
		var from_val = '';
		if (this.from.type=='text') {
			from_val = this.from.value;
		} else if (this.from.selectedIndex>=0) {
			from_val = this.from.options[this.from.selectedIndex].value;
		}
		var uparams=this._makeParams(target);
		var url = '/ajax?'+$H(target.address).toQueryString()+'&ajax&id='+from_val+uparams;
		if(this.cache[url]) {
			this._setOptions(target, url);
		}
		else {
			var options = {
				method: 'get'
			}
			options.onComplete=this._callback.bind(this,target,url);
			new Ajax.Request(url,options);
		}
	},
	
	_makeParams: function(target) {
// apart from the id of the selected index item
// the uparams parameter allows additional information
// to be passed from the form to the target procedure
		var uparams="";
		for (var idx = 0; idx < target.url.extra_params.length; idx++)
  		{
			if (document.getElementById(target.url.extra_params[idx+1])!= null) {
				uparams+="&"+target.url.extra_params[idx];
				idx++;
				uparams+=$(target.url.extra_params[idx]).value;
			} else {
				idx++;
			}
  		}
  		return uparams;
 
	},
	_callback: function(target,url,xhr) {
//		console.log(arguments);
		var opts = Ajax.JSONResponse(xhr);
		this.cache[url]=opts;
		this._setOptions(target,url);
	},
	
	_setOptions: function(target, address) {
		var opts = this.cache[address];
		var to_sel = $(target.element);
// Perhaps need something here to indicate whether mandatory or optional
// field; if optional, need to preserve first entry. 
		if (target.mandatory) {
			to_sel.length=0;
		} else {
			to_sel.length=1;
		}
		if (to_sel.type=='text') {
			to_sel.value=opts;
		}
		if (to_sel.type=='select-one' || to_sel.type=='select-multiple') {
// Only populate and enable the list if elements are returned
// Ideally, if no elements are returned, may want to hide
// the select box altogether
//alert('setoptions(2) opts='+opts.size());
//			if (opts.size()>0) {
				opts.each(function(opt) {
					var opt_el = Builder.node('option',{value: parseInt(opt[0])},opt[1]);
					to_sel.appendChild(opt_el);
				});
// by default, set the selected index to the first entry in the list
// then, if a selected value is provided, and this value exists in the select list
// set the index value accordingly
				to_sel.selectedIndex=0;
				if (target.selected) {
					var opts = to_sel;
					var optionCounter;
					for (optionCounter = 0; optionCounter < opts.length; optionCounter++) {
						if (opts.options[optionCounter].value==$(target.selected).value) {
								opts.selectedIndex=optionCounter;
								break;
						}
					}
				}
//			}
		}
		to_sel.removeAttribute('disabled');
		new Effect.Highlight(to_sel,{duration: 2});
		if (to_sel.onchange) {
			to_sel.onchange();
		}
	}
	
}

function CascadeSelect(from, to) {
	
	new CascadeSelect(from,to);

};

function getShipping (country, value, currency, to_location) {
	new CascadeSelect(country,
    	[
        	{
            	container: '',
            	element: to_location,
            	mandatory: true,
            	url: {
                	module: 'ecommerce',
    	            controller: 'Myaccount',
        	        action: 'getShipping',
            	    extra_params: ["country=",country
            	                            ,"value=",value
            	                            ,"currency_id=",currency]
	            }	
        	}
    	]
	);
}

function setCurrency (element) {
	
 	url = '/basket/setcurrency/'+element.value+'?ajax';
 	var newvalue = element.value;
	new Ajax.Request(url, {
			method: 'get',
			onComplete: function(xhr) {
					var opts = $(element);
					var optionCounter;
// By default, set the index to the first entry in the list
 					opts.selectedIndex=0;
					for (optionCounter = 0; optionCounter < opts.length; optionCounter++) {
						if (opts.options[optionCounter].value==newvalue) {
								opts.selectedIndex=optionCounter;
						}
					}
					window.location.reload();
				}
			}
	);

};

