window.addEvent('domready', function(){
if($('spec')){
	placeSpec();
	//alert(document.anchors.length);
	//document.anchors["view-specification"].href="javascript:toggleSpec();";
	$('view-specification').href="javascript:toggleSpec();";
	specslide = new Fx.Style('spec', 'height', {duration: 1500,unit:'em',transition: Fx.Transitions.Cubic.easeInOut});
	window.addEvent('resize', function(){placeSpec()});
	addCookLinks();
	updateSpec();
}	
	if($('photoFade')){
		$('firstPhoto').setOpacity(0);
		photoFadeFx = new Fx.Style('firstPhoto', 'opacity', {duration:800});
		window.setTimeout('fadePhoto()', 3000);
	}
	
	if($('specUpdated')){
		specUpdatedFx = new Fx.Styles('specUpdated', {duration: 800,unit:'px',transition: Fx.Transitions.Cubic.easeInOut});
	}

});

function addCookLinks(){
	$each(document.links, function(el){
		if (el.rel && el.rel.test(/^cookie/i)){
		
			el.onclick = function(){
			
			//split the URL first take end after ? then split into each section - delimited by &
				Aref = el.href.split('?').getLast().split('&');
				Aref.each(function(val){
					tc=val.split('=');
					
					if(tc[0]!='sc'){
					//sc is used by server side scripting to initalise a cookie
					 	Cookie.set(tc[0], tc[1], {path:'/',duration: 10}); // save this for 10 day
					 	updateSpec();
						 
					}
   					
				});
				
				AlertSpecUpdate();
				return false;
				
			};
		}
	});
}


var ptN=new Array();
/*standard*/
ptN['P7001 SD'] = 'Standard timber door kit';
ptN['P7001 ID'] = 'Intermediate timber door kit';
ptN['P7001 WD'] = 'Wide timber door kit';
ptN['P7001 TD'] = 'Tall timber door kit';
ptN['P7001 ITD'] = 'Tall Intermediate timber door kit';
ptN['P7001 WTD'] = 'Tall Wide timber door kit';
ptN['P7001 DD'] = 'Timber double door kit';
ptN['P7001 TDD'] = 'Timber tall double door kit';
/*flush*/
ptN['P7101 SDF'] = 'Timber door kit with flush door brackets';
ptN['P7101 IDF'] = 'Intermediate timber door kit with flush door brackets';
ptN['P7101 WDF'] = 'Wide timber door kit with flush door brackets';
ptN['P7101 TDF'] = 'Tall timber door kit with flush door brackets';
ptN['P7101 ITDF'] = 'Tall Intermediate timber door kit with flush door brackets';
ptN['P7101 WTDF'] = 'Tall Wide timber door kit with flush door brackets';
ptN['P7101 DDF'] = 'Timber double door kit with flush door brackets';
ptN['P7101 TDDF'] = 'Timber tall double door kit with flush door brackets';
/*plasterboard*/
ptN['P7200 SDP'] = 'Architrave free door kit';
ptN['P7200 IDP'] = 'Intermediate Architrave free door kit';
ptN['P7200 WDP'] = 'Wide Architrave free door kit';
ptN['P7200 TDP'] = 'Tall Architrave free door kit';
ptN['P7200 ITDP'] = 'Tall Intermediate Architrave free door kit';
ptN['P7200 WTDP'] = 'Tall Wide Architrave free door kit';
ptN['P7200 DDP'] = 'Architrave free double door kit';
ptN['P7200 TDDP'] = 'Architrave free tall double door kit';
/*firedoors*/
ptN['PFD30 SD'] = 'FD30 Fire door kit';
ptN['PFD30 WD']= 'Wide FD30 Fire door kit';
ptN['PFD30 TD']= 'Tall FD30 Fire door kit';
ptN['PFD30 WTD']= 'Tall Wide FD30 Fire door kit';
/*STD KIT NO DOOR LINING for frameless glass*/
ptN['P7000 SD'] = 'Standard timber door kit without door jambs';
ptN['P7000 ID'] = 'Intermediate timber door kit without door jambs';
ptN['P7000 WD'] = 'Wide timber door kit without door jambs';
ptN['P7000 TD'] = 'Tall timber door kit without door jambs';
ptN['P7000 ITD'] = 'Tall Intermediate timber door kit without door jambs';
ptN['P7000 WTD'] = 'Tall Wide timber door kit without door jambs';
ptN['P7000 DD'] = 'Timber double door kit without door jambs';
ptN['P7000 TDD'] = 'Timber tall double door kit without door jambs';
/*frameless glass*/
ptN['P1702'] = 'Frameless glass clamps';
ptN['P880'] = 'Frameless glass clamps up to 880mm wide';
ptN['P1280'] = 'Frameless glass clamps up to 1280mm wide';


var cookNames=new Array('dwidth','dheight','ndoor','dtype');
var prodCookie=new Array();
prodCookie['dwidth'] = 'Door Width';
prodCookie['dheight'] = 'Door Height';
prodCookie['ndoor'] = 'Door Set Type';
prodCookie['dtype'] = 'Design Option';
	
var dgList=new Array();
dgList['P7910']= 'Single wheel timber door hanger';
dgList['P7911']= 'Double wheel timber door hanger';
dgList['P7912']= 'Single wheel glass door hanger';
dgList['P7913']= 'Double wheel glass door hanger';
dgList['P7921'] ='End stop - timber door';
dgList['P7922'] ='End stop - glass door';
dgList['P7923'] ='Rail stop';
dgList['P7930'] ='T-shaped floor guide';
dgList['P7931'] ='U-shaped floor guide';
dgList['P7950'] ='Glass leaf pull handle';
dgList['P7951'] ='Glass leaf pull handle';
dgList['P7952'] ='Glass leaf pull handle';
dgList['P7953'] ='Glass leaf pull handle';
dgList['P7954'] ='Glass leaf pull handle';
dgList['P7902'] ='2m rail track';
dgList['P7903'] ='3m rail track';
dgList['P7904'] ='4m rail track';
dgList['P7920'] ='Track end plug';
dgList['P7940'] ='Brickwork track mount';
dgList['P7942'] ='Track end fixing';
dgList['P7999'] ='Pig-nose fixing tool';
dgList['P1702'] = 'Frameless glass clamps';
dgList['P880'] = 'Frameless glass clamps up to 880mm wide';
dgList['P1280'] = 'Frameless glass clamps up to 1280mm wide';
dgList['P08802'] = 'Klose self closing mechanism';
dgList['P7502CU'] = 'Circular flush pull excluding push return plate';
dgList['P7502CF'] = 'Circular flush pull complete with push return plate';
dgList['P7503SU'] = 'Square flush pull excluding push return plate';
dgList['P7503SF'] = 'Square flush pull complete with push return plate';
dgList['P7500OU'] = 'Oval flush pull excluding push return plate';
dgList['P7500OF'] = 'Oval flush pull complete with push return plate';
dgList['P7501RU'] = 'Rectangular flush pull excluding push return plate';
dgList['P7501RF'] = 'Rectangular flush pull complete with push return plate';
dgList['P7510SN'] = 'Satin Nickel Bathroom door furniture set';
dgList['P7510PN'] = 'Polished Nickel Bathroom door furniture set';
dgList['P7510PB'] = 'Polished Brass Bathroom door furniture set';

	//dgList['P7922'] ='Rail mounted end stop';

var cookieValue=new Array();
cookieValue['std'] = 'Standard';
cookieValue['flush'] = 'Flush';
cookieValue['plaster']='Plasterboard';
cookieValue['fire']='Firedoor';
cookieValue['glass']='Glass(P1702)';
cookieValue['glass80']='Glass(P880)';
cookieValue['single']='Single';
cookieValue['double']='Double';
cookieValue['sim']='P5550 Simultaneous opening &amp; closing';
cookieValue['self']='P0880-2 Self Closing';
cookieValue['powered']='PWS 100 Powered opening';


function toPartNo(){
//widths s=standard (626-926) i=intermediate(927-1300) w=wide(1301-1500)
//heights n=normal (1500-2100) t=tall(2101-2700)
//remove the rply stuff for production
cw=Cookie.get('dwidth');
rply='';

switch(true){
case cw<926: rply +='smaller than 926';pw='S';break;    
case cw<1300: rply +='smaller than 1300';pw='I';break;
case cw<1500: rply +='smaller than 1500'; pw='W';break;
default:rply +=' def';pw='S';
}


ch=Cookie.get('dheight');

switch(true){
case ch<2101: rply +=' shorter than than 2100';ph='D';break;    
case ch<2701: rply +=' smaller than 2700';ph='TD';break;
default:rply +=' def';ph='D';
}


cd=Cookie.get('ndoor');

switch(cd){
case 'double': rply +=' double door';pd='D';pw='';break;    
case 'single': rply +=' single door';pd='';break;
default:rply +=' def';pd='';
}


ct=Cookie.get('dtype');

switch(ct){
case 'std': rply +=' timber';pt='P7001 ';px='';break;    
case 'flush': rply +=' flush';pt='P7101 ';px='F';break;
case 'plaster': rply +=' plaster';pt='P7200 ';px='P';break;
case 'fire': rply +=' fire';pt='PFD30 ';px='';
	switch(true){
	case cw<926: rply +='smaller than 926';pw='S';break;    
	case cw<1400: rply +='smaller than 1400';pw='W';break;
	default:rply +=' def';pw='S';
	}
break;  
case 'glass': rply +=' glass';pt='P7000 ';px='';break;
case 'glass80': rply +=' glass80';pt='P7000 ';px='';break;
default:rply +=' def';pt='P7001 ';px='';
}


if(pw=='S' && ph=='TD'){
pw='';
}
if(pt=='none'){
	return false;
	}else{
	return pt+pw+ph+pd+px;
	}

}



function fadePhoto(){
	if($('firstPhoto').getStyle('opacity') == '1'){
	 fxTo = '0';
	}else{
	 fxTo = '1';
	}
	photoFadeFx.start(fxTo);
	window.setTimeout('fadePhoto()', 6000);
}

function toggleSpec(){
//slide the specifiaction box in and out
//substring needs as some browsers report 4.50

	if ($('spec').style.height.substring(0,3) == '4.5'){
		numSpec=$$('#spec-list li').length;
		resize = 4.5+(numSpec*3.5);
		resize= ((resize > 4.5)? resize:4.5);
	}else{
		resize = 4.5;
	}
specslide.start(resize);

}

function placeSpec(){

	dhd = $('hd').getCoordinates();
	lhd = dhd["right"] - (12*13)-40;
	$('spec').style.left=lhd+'px';
}

function AlertSpecUpdate(){
//popup an alert that the specification has changed

wsl=window.getScrollLeft();
wst=window.getScrollTop();
ww=window.getWidth();
wh=window.getHeight();

boxsize=$('specUpdated').getCoordinates();

topmove=wst+(wh/2)-(boxsize['height']/2);
leftmove=wsl+(ww/2)-(boxsize['width']/2);


if(arguments[0] == 'hide'){

	specUpdatedFx.start({'top':-400,'left':-400});
}else{
	specUpdatedFx.start({'top':topmove,'left':leftmove});
}
	
}

function checkIncompatibles(){
//check the cookies for incompatible drT(door type) options, remove any incompatibles (glass clamps or closers)
drT = Cookie.get('dtype');
drN = Cookie.get('ndoor');

	if ((drT !='glass') && (Cookie.get('dg_P1702')) ){
	
	//only glass clamps with glass doors
	Cookie.remove('dg_P1702',{path:'/'});
	alert('P1702 glass clamps only with standard glass kits');
	}
	
	if ((drT !='glass80') && (Cookie.get('dg_P880')) ){
	Cookie.remove('dg_P880',{path:'/'});
	alert('P880 glass clamps only with standard glass kits');
	}
	
	if(drT !='std' && (Cookie.get('closing')) ){
	Cookie.remove('closing',{path:'/'});
	alert('Powered opening and closing is only available with standard timber kits');
	}
	
	if( (drT !='std') && (drN !='double') && (Cookie.get('closing')) ){
	//if its not a standard AND dounle door check to see if theres simultaneous and remove
	 Cc = Cookie.get('closing');
	 if(Cc == 'sim'){
	 	Cookie.remove('closing',{path:'/'});
	 	alert('Simultaneous closing is only available with standard timber double door kits.');
	 }
	}
	
}


function updateSpec(){
//updates the spec box dropdown
//check cookies to see if any incompatible products
checkIncompatibles();

//first empty the list
$('spec-list').empty();
//populate specifcation widget from cookies	with kit information & part number

partNo=toPartNo();
if(partNo){
	
	cw=Cookie.get('dwidth');
	ch=Cookie.get('dheight');
	
	if(cw && ch ){
	 pr_dims=' ('+cw+'mm wide x '+ch+'mm tall)';
	}else{
	 pr_dims='';
	}
	ptDel = ' <a href="javascript:ptdelete();" class="deleteSpec"><span>del</span></a>';
	
	$('spec-list').adopt(new Element('li').setHTML(ptDel+'<strong>'+partNo+'</strong>'+' '+ptN[partNo]+pr_dims));
}

//add closing option
cls=Cookie.get('closing');
if(cls != false){
	clsDel = ' <a href="javascript:spDelete(\'closing\');" class="deleteSpec"><span>del</span></a>';
	$('spec-list').adopt(new Element('li').setHTML(clsDel+'<strong>Closing</strong> '+cookieValue[cls]));

}
	//populate specifcation widget with door gear cookies - 'dg_'
	myregexp = new RegExp('s*dg_(P[0-9]{3,5}[A-Z]{0,2})=1','g');	

	if(dGr = document.cookie.match(myregexp) ){	
		dgRegEx = new RegExp('P[0-9]{3,5}[A-Z]{0,2}');

		if(dGr != null){
			for(i=0;i<dGr.length;i++){
			var thisDG = dGr[i].match(dgRegEx);
			var dgDel = ' <a href="javascript:spDelete(\'dg_'+thisDG+'\');" class="deleteSpec"><span>del</span></a>';
			
				if(i==0){
				$('spec-list').adopt(new Element('li').addClass('spec-divide').setHTML('<strong>'+thisDG+'</strong>'+' '+dgList[thisDG]+dgDel));
				}else{
				$('spec-list').adopt(new Element('li').setHTML('<strong>'+thisDG+'</strong>'+' '+dgList[thisDG]+dgDel));
				}
			}
		}
	}	
	
//count list and update number
	numSpec=$$('#spec-list li').length;
	pr_items = ((numSpec == 1)? ' ITEM':' ITEMS');

	if(numSpec != null){
		$('num-specs').setText(numSpec+pr_items);
	}
}

function printcookies(){
	thisCookie = document.cookie.split("; ");
	crly='';
    	for (i=0; i<thisCookie.length; i++) {
			cookieName = thisCookie[i].split("=")[0];
			crly += cookieName+' ';
		}
	alert(crly);
}

function deleteall(){
	thisCookie = document.cookie.split("; ")
    	for (i=0; i<thisCookie.length; i++) {
			cookieName = thisCookie[i].split("=")[0];
			Cookie.remove(cookieName,{path:'/'});
		}
}

function printDate(duration){
	duration = parseInt(duration);
	var date = new Date();
	date.setTime(date.getTime() + duration * 24 * 60 * 60 * 1000);
	var value = '; expires=' + date.toGMTString();
	alert(value);
}

function spDelete(ckName){
	Cookie.remove(ckName,{path:'/'});
	updateSpec();
}

function ptdelete(){
 var ptarr=new Array('dheight','dwidth','closing','ndoor','dtype','wheight','wwidth');
 ptarr.each(function(val){
	Cookie.remove(val,{path:'/'});
 });
 updateSpec();
}

function validateform(){
//check standard feilds arent empty
 if (document.enquiry.name.value == ""  || document.enquiry.company.value == ""  || document.enquiry.contact.value == ""  || document.enquiry.address.value == ""  || document.enquiry.message.value == ""){
 alert("Please complete all fields.\nThis information will help us provide you with useful responses.")
 return false;
 }
//check email address
  substremail = document.enquiry.email.value.split("@")
  if (substremail.length > 1) {
    index = substremail[1].indexOf(".")
    if (index == -1) {
      alert("Please enter a valid email address")
      document.enquiry.email.focus()
      return false
    }else{
      return true
    }
  }else{
    alert("Please enter a valid email address")
    document.enquiry.email.focus()
    return false
  }
}