
/* Functions for the Portman website - specifiaction tool page */
window.addEvent('domready', function(){
	specValue = specCookies();
	// get the submit button
	var sbmtBtn = document.getElementById('submitButton');
	// remove the submit button
	sbmtBtn.parentNode.removeChild(sbmtBtn);
	//setup the spec alert message
	if($('spec-alert')){
		specAlertFx = new Fx.Style('spec-alert', 'opacity',{duration: 800,transition: Fx.Transitions.linear});
		$('spec-alert').style.position = 'absolute';
		$('spec-alert').style.top = '200px';
		$('spec-alert').style.left = '53em';
	}

	// dont do ondomready updates - they need to go in the body onload	
});

function specCookies(){
//get various values from cookies or set defaults
thisAr = new Array();
	if( !(thisAr['dtype'] = Cookie.get('dtype')) ){
		thisAr['dtype'] = 'std';
	}
	
	if( !(thisAr['ndoor'] = Cookie.get('ndoor')) ){
		thisAr['ndoor'] = 'single';
	}
	return thisAr;
}

function updateAll(){
doorCheck();
updateDoor();
calcWall();
calcDoor();
sendFlX();
sendFlY();
updateCooks();
updateSpec();
}

function doorCheck(){
//check door sizes are okay
drW = parseInt(document.tool.dwidth.value);
wlW = parseInt(document.tool.wwidth.value);
//addH = getH();
//nd = getNdoors();
//addW = 11+(5*(nd-1));

	constants = getConstantsSorted();

	//ddw_max=(926*4)+constants.addW;


	//if((nd==2) && ((drW>926)||(wlW>ddw_max))){
 	//AlertSpec('Each door for double door kits cannot be wider than 926mm\r\n(max wall aperture is '+ddw_max+'mm)\r\nPlease contact the sales office for wider doors.');
	//}
	

	switch(specValue['dtype']){
		case 'fire':
			if(drW>1400){
				document.tool.dwidth.value = 1400;
				AlertSpec('FD30 fire doors cannot be wider than 1400mm\r\n');
			}
		break;
		case 'tele':
			if(drW>1300){
				document.tool.dwidth.value = 1300;
				AlertSpec('Telescopic doors cannot be wider than 1300mm\r\n');
			}
			
			if(specValue['ndoor']=='double'){
				Cookie.set('ndoor', 'single', {path:'/',duration: 10});
				AlertSpec('Telescopic doorsets can only have single doors\r\n');
			}
		break;
		case 'conv':
			if(drW>1300){
				document.tool.dwidth.value = 1300;
				AlertSpec('Converging doors cannot be wider than 1300mm\r\n');
			}
			
			if(specValue['ndoor']=='single'){
				Cookie.set('ndoor', 'double', {path:'/',duration: 10});
				AlertSpec('Converging doorsets must have double doors\r\n');
			}
		break;
		
	}
	

		
}

/*
function calcDoorN(){
	constants = getConstantsSorted();
	
	console.log(constants)
	console.log(Cookie.get('dwidth'));
	console.log(Cookie.get('dheight'));
	
	wlW = parseInt(document.tool.wwidth.value);
	wlH = parseInt(document.tool.wheight.value);
	
	document.tool.dwidth.value = (wlW - constants.addWSingle) / constants.multW;
	document.tool.dheight.value = wlH - constants.addH;
	
}
*/

function calcWall(){
//update the Wall Apperture sizes
drW = parseInt(document.tool.dwidth.value);
drH = parseInt(document.tool.dheight.value);
constants = getConstantsSorted();
nd = getNdoors();


//set limits

if((nd==2) && (drW>926)){
//double doors can only be 926 wide
 AlertSpec('Door panels for double doors cannot be wider than 926mm wide');
 document.tool.dwidth.value = 926;
 drW = 926;

}

if(drW < 626){

 document.tool.dwidth.value = 626;
 drW = 626;
 AlertSpec('Door panels must be at least 626mm wide');
}else if(drW > 1500){
 document.tool.dwidth.value = 1500;
 drW = 1500;
 AlertSpec('Door panels cannot be wider than 1500mm');
}

if(drH < 1500){
 document.tool.dheight.value = 1500;
 drH = 1500;
 AlertSpec('Door panels must be at least 1500mm tall');
}else if(drH > 2700){

 document.tool.dheight.value = 2700;
 drH = 2700;
 AlertSpec('Door panels cannot be taller than 2700mm');
}

if( (specValue['dtype']=='fire') && (drW>1400) ){
	document.tool.dwidth.value = 1400;
	AlertSpec('FD30 fire doors cannot be wider than 1400mm\r\n');
}

document.tool.wwidth.value = (drW*(constants.multW))+constants.addW;
document.tool.wheight.value = drH+constants.addH;

}

function calcDoor(){
//update the Door sizes
wlW = parseInt(document.tool.wwidth.value);
wlH = parseInt(document.tool.wheight.value);

constants = getConstantsSorted();
nd = getNdoors();

w_min= (626*(constants.multW))+constants.addW;
ddw_max=(926*4)+constants.addW;
w_max= (1500*(constants.multW))+constants.addW;
h_min= 1500+constants.addH;
h_max= 2700+constants.addH;


if((nd==2) && (wlW>ddw_max)){
//double doors can only be 926 wide
 document.tool.wwidth.value = ddw_max;
 wlW = ddw_max;

}

//set limits for wall width
	if(wlW < w_min){
	 AlertSpec('Wall aperture must be at least '+w_min+'mm wide');
	 document.tool.wwidth.value = w_min;
	 wlW = w_min;
	}else if(wlW > w_max){
	 AlertSpec('Wall aperture must be less than '+w_max+'mm');
	 document.tool.wwidth.value = w_max;
	 wlW = w_max;
	}
//set limits for wall hight


if(wlH < h_min){
 AlertSpec('Wall aperture must be at least '+h_min+'mm high');
 document.tool.wheight.value = h_min;
 wlH = h_min;
}else if(wlH > h_max){
 AlertSpec('Wall aperture cannot be higher than '+h_max+'mm');
 document.tool.wheight.value = h_max;
 wlH = h_max;
}	


document.tool.dwidth.value = (wlW-constants.addW)/(constants.multW);
document.tool.dheight.value = wlH-constants.addH;

}

function sendFlX(){

	flashProxy.call('moveX', document.tool.wwidth.value);
}

function sendFlY(){

	flashProxy.call('moveY', document.tool.wheight.value);
}

function updateDoor(){
//create 2 strings one for single/doulble ane for type(stndard/flush/plasterboard/glass
	
	
	if (specValue['dtype']=='std'){
	 dtype = 'std';
	}else{
	 dtype = 'std';
	}
	
	if(specValue['ndoor']=='double'){
	 ndoor = 'dbl';
	}else{
	 ndoor = 'single';
	}
	
	if(specValue['dtype']=='conv'){
	// the double converging door looks like a single in profile
		ndoor = 'single';
	}
	//alert('click okay to view your specification');
	flashProxy.call('setDoor',dtype+ndoor);

}

/*
function getH(){

switch (specValue['dtype']){
	case 'std':
		addH=95;
		break;
	case 'flush':
		addH=72;
		break;
	case 'plaster':
		addH=72;
		break;
	case 'glass':
		addH=126;
		break;
	case 'glass80':
		addH=119;
		break;
	default:
		addH=95; 
}


return addH;
}
*/

function getNdoors(){
if(specValue['ndoor']=='double'){
	 ndoor = 2;
	}else{
	 ndoor = 1;
	}
return ndoor;
}

function oldupdateCooks(){

	Cookie.set(tc[0], tc[1], {path:'/',duration: 10}); // save this for 10 day
	updateSpec();

}

function updateCooks(){

var toolfields = new Array('wheight','wwidth','dheight','dwidth');
$each(toolfields, function(el){
	Cookie.set(el, document.tool[el].value, {path:'/',duration: 10});
});


}

function AlertSpec(){
if(arguments[0] == 'hide'){
	specAlertFx.start(0);
	}else{
	$('spec-alert-warning').setHTML(arguments[0]);
	specAlertFx.start(0,1);
	}
}

/*
function calcWallN(){
	constants = getV();
	
	console.log(constants)
	console.log(Cookie.get('dwidth'));
	console.log(Cookie.get('dheight'));
	
	drW = parseInt(document.tool.dwidth.value);
	drH = parseInt(document.tool.dheight.value);
	
	document.tool.wwidth.value = (drW*(constants.multW)) + constants.addWSingle;
	document.tool.wheight.value = drH + constants.addH;
	
}
*/



function getConstantsSorted(){
	nd = getNdoors();
//	console.log('numdoors:'+nd);
	if(nd == 2){
		switch (specValue['dtype']){
			case 'std':
				var sendback = 
				{"addH":95,
				"multW":2*nd,
				"addW":-16};
				break;
			case 'flush':
				var sendback =
				{"addH":72,
				"multW":2*nd,
				"addW":-16};
				break;
			case 'plaster':
				var sendback = 
				{"addH":70,
				"multW":2*nd,
				"addW":18};
				break;
			case 'glass':
				var sendback = 
				{"addH":126,
				"multW":2*nd,
				"addW":-16};
				break;
			case 'glass880':
				var sendback = 
				{"addH":119,
				"multW":2*nd,
				"addW":-16};
				break;
			case 'glass1280':
				var sendback = 
				{"addH":119,
				"multW":2*nd,
				"addW":-16};
				break;	
			case 'fire':
				var sendback = 
				{"addH":95,
				"multW":2*nd,
				"addW":false};		
				break;
			case 'curved':
				var sendback = 
				{"addH":95,
				"multW":2*nd,
				"addW":-11};
				break;
			case 'tele':
				var sendback = 
				{"addH":95,
				"multW":3,
				"addW":7};
				break;
			case 'conv':
				var sendback = 
				{"addH":95,
				"multW":2,
				"addW":65};
				break;

			default:
				 
		}	
	}else{
	
		switch (specValue['dtype']){
			case 'std':
				var sendback = 
				{"addH":95,
				"multW":2,
				"addW":11};
				break;
			case 'flush':
				var sendback =
				{"addH":72,
				"multW":2,
				"addW":11};
				break;
			case 'plaster':
				var sendback = 
				{"addH":70,
				"multW":2,
				"addW":12};
				break;
			case 'glass':
				var sendback = 
				{"addH":126,
				"multW":2,
				"addW":11};
				break;
			case 'glass880':
				var sendback = 
				{"addH":119,
				"multW":2,
				"addW":11};
				break;
			case 'glass1280':
				var sendback = 
				{"addH":119,
				"multW":2,
				"addW":11};
				break;	
			case 'fire':
				var sendback = 
				{"addH":95,
				"multW":2,
				"addW":-20};		
				break;
			case 'curved':
				var sendback = 
				{"addH":95,
				"multW":2,
				"addW":12};
				break;
			case 'tele':
				var sendback = 
				{"addH":95,
				"multW":3,
				"addW":7};
				break;
			case 'conv':
				var sendback = 
				{};
				break;
			
			default:
				 
		}
	}
//console.log(specValue['dtype']);
//console.log(sendback);
return sendback;
}

//console.log(Cookie);