

function checkForm(changedElement) {
	e=changedElement;
	makeDependents(e);
	calculateTotal();
}


function makeDependents(changedElement) {
	if(!changedElement) return false;
	ce=changedElement;
	//alert(ce.name);
	//dependencies
	// elementos booleanos, si esta uno activo el otro no y viceversa
	bool=Array();
	bool[0]=Array();
	bool[0]['e6_1']='e4_1';
	
	//bool0(ce);
	
	// booleanos solo si uno esta activo, pero ambos pueden estar desactivos
	//bool[1]=Array();
	//bool[1]['e6_3']='e6_4';
	
	doIt(ce);

}

function changeDiv(checkboxname,divtext) {
	
	// componemos divname, se nos pasa realmente checkboxname
	elemento=getFormElem(checkboxname);
	name=elemento.name;
	divname="d"+name.substring(1,name.length);
	
	// options for text content, cojemos idioma
	switch(divtext) {
		case "notincluded":
		texto="Not Included";
		break;
		
		case "included":
		texto="Standard";
		break;
		
		case "optional":
		texto="Optional";
		break;
		default:
		texto=divtext;
		break;
	}

	if(el=document.getElementById(divname)) {
		el.innerHTML=texto;
		return true;
	}
	else return false;
}

function desactivateCheckbox(checkbox) {
	el=getFormElem(checkbox);
	if(el.disabled=true) return true;
	else return false;
}

function activateCheckbox(checkbox) {
	if(el=getFormElem(checkbox)) {
		el.disabled=false;
		return true;
	}
	else return false;
}

function changeCheckbox(boxname, state) {		
	el=getFormElem(boxname);
	if(el.checked=state) return true;
	else return false;
}

// si checked return true else return false
function comprobarCheckbox(checkbox) {
	el=getFormElem(checkbox);
	if(el.checked) return true;
	else return false;
}

function getFormElem(nombre) {
	if(el=document.forms.prices.elements[nombre]) return el;
	else return false;
}

function findDependant(name) {
	elem=getFormElem(name);
	for(i=0;i<=bool.length;i++) {
		for(key in bool[i]) {
			if(key==name) {
				elem.tipodependency=i;
				return bool[i][key];
			}
			if(bool[i][key]==name) {
				elem.tipodependency=i;
				return key;
			}
		}
	}
	return false;
}


/** constructor 
@param duration integer seconds
@param <optional> function to run while waiting.
	*/
function Pause(duration, busy) {
	this.duration= duration * 1000;
	this.busywork = null; // function to call while waiting.
	this.runner = 0;
	
	if (arguments.length == 2) {
		this.busywork = busy;
	}
		this.pause(this.duration);
} //Pause class
	
	
/** pause method 
@param duration: integer in seconds
*/
Pause.prototype.pause = function(duration) {
	if ( (duration == null) || (duration < 0)) {return;}
				
	var later = (new Date()).getTime() + duration;
				
	while(true){
		if ((new Date()).getTime() > later) {
			break;
		}
		this.runner++;
		if (this.busywork != null) {
		this.busywork(this.runner);
		}
		
	} // while
		
} // pause method
		
		
function doIt(elemento) {
	dependant=getFormElem(findDependant(elemento.name));
	tipo=elemento.tipodependency;
	
	if(tipo===0) {
		if(elemento.checked) {
			changeCheckbox(dependant.name,false);
			changeDiv(dependant.name,"notincluded");
		}
		else {
			changeCheckbox(dependant.name,true);
			changeDiv(dependant.name,"included");
		}
	}
	if(tipo===1) {
		if(elemento.checked) {
			changeCheckbox(dependant.name,false);
			changeDiv(dependant.name,"notincluded");
			
			changeDiv(dependant.name,dependant.value+" &euro;");
			changeDiv(elemento.name,elemento.value+" &euro;");
		}
	}
	return false;
}
		
		
function calculateTotal() {
	total=0.00;
	basic=0.00;
	for(i=0;i<document.forms.prices.elements.length;i++) {
		el=document.forms.prices.elements[i];
		if(el.type=='checkbox') {
			if(el.checked) {
				if(el.name.charAt(1)!='6') basic+=el.value*1;
				total+=el.value*1;
			}
		}
	}
			
	tdiv=document.getElementById('totaldiv');
	tdiv.innerHTML=total+" &euro;";
	
	bdiv=document.getElementById('basepricediv');
	bdiv.innerHTML=basic+" &euro;";
											
											
}
																			
																				
