
var widthMap='[ custom length mm ]...'
var heightMap='[ custom width mm ]...'
var preMap='[ size ]...'
var parMap='[ parameters ]...'
var surMap='[ surcharge ]...'
     
var polishMap='[ polished edges ]...'
var drilledMap='[ CNC cutting ]...'

function buyFunc(frm){; 

	if(frm[parMap]){
		var _w=0
		var _h=0
		if(frm[widthMap])_w=parseFloat(frm[widthMap].value)
		if(frm[heightMap])_h=parseFloat(frm[heightMap].value)
	 
		if(frm[parMap])var par=frm[parMap].value  
		if(frm[surMap])var sur=frm[surMap].value 
	
		if(frm[preMap])var pre=frm[preMap].value 
	
		if(frm['hid7'])var hidPav=frm.hid7.options[frm.hid7.selectedIndex].value

	

	

		if(!frm[heightMap]){ //must be a Roll and not a Sheet

			if(frm[preMap]){
				if(frm[preMap].value=='Custom Length'){
					if(frm[widthMap])frm[widthMap].alt=''
					if(frm[preMap])frm[preMap].alt='hidden'
				}else{
					if(frm[widthMap])frm[widthMap].alt='hidden'
					if(frm[preMap])frm[preMap].alt=''
				}
			}


			gPOver=null
			
			var surcharge=parseFloat(sur.split('Surcharge=')[1].split('%')[0])
			var minPrice=parseFloat(sur.split('£')[1])
			
			hidPav=unescape(hidPav)
			var hidPavArry=hidPav.split('|')
			var sqm=parseFloat(hidPavArry[2])
			var sqmm=sqm/1000

			var price=(parseFloat(_w)*sqmm)
	
			if(pre.indexOf('Custom')!=-1){		
				price=price+(price*surcharge/100)
			}
	
			var min=parseFloat(par.split('Min=')[1].split('mm')[0])
			var max=parseFloat(par.split('Max=')[1].split('mm')[0])
			var warn=false

			if(document.getElementById('info_min'))document.getElementById('info_min').innerHTML=min+'mm'
			if(document.getElementById('info_max'))document.getElementById('info_max').innerHTML=max+'mm'
			
			if(_w<min||_w>max||!_w){
			    warn=true
			    if(frm[widthMap])frm[widthMap].style.color='red'
			}else{
			    if(frm[widthMap])frm[widthMap].style.color='black'
			}
			
                                                if(frm[preMap]){
                                                        if(frm[preMap].value.indexOf('Custom')==-1){
                                                                warn=false
                                                        }
                                                }

			if(warn==true){
			    if(gAddToOrder==true)alert('Minimum cut length is '+min+'mm\nMaximum cut length is '+max+'mm\n\nPlease check your dimensions and try again')
			    return true
			}

			if(price<minPrice)price=minPrice
			gPOver=price	
		}else{
		
			if(frm[preMap]){
				if(frm[preMap].value=='Custom Dimensions'){
					if(frm[widthMap])frm[widthMap].alt=''
					if(frm[heightMap])frm[heightMap].alt=''
				}else{
					if(frm[widthMap])frm[widthMap].alt='hidden'
					if(frm[heightMap])frm[heightMap].alt='hidden'
				}
			}
			
			gPOver=null
			var surcharge=parseFloat(sur.split('Surcharge=')[1].split('%')[0])
			var minPrice=parseFloat(sur.split('£')[1])
			
			hidPav=unescape(hidPav)
			var hidPavArry=hidPav.split('|')
			var sqm=parseFloat(hidPavArry[2])
			var sqmm=sqm/1000
			var price=(parseFloat(_w)*parseFloat(_h)*sqmm)/1000
	
			if(pre.indexOf('Custom')!=-1){		
				price=price+(price*surcharge/100)
			}


                        if(par.indexOf('Max2')==-1){
                        //SHEET PRODUCTS - OLD STYLE TEMPLATES AND PRODUCTS FOR BACKWARDS COMPATABILITY
                    
                                                var min=parseFloat(par.split('Min=')[1].split('mm')[0])
                                                var max=parseFloat(par.split('Max=')[1].split('mm')[0])
                        
                                                var warn=false
                                                if(_w<min||_w>max||!_w){
                                                    warn=true
                                                    if(frm[widthMap])frm[widthMap].style.color='red'
                                                }else{
                                                    if(frm[widthMap])frm[widthMap].style.color='black'
                                                }
                                                
                                                if(_h<min||_h>max||!_h){
                                                    warn=true
                                                    if(frm[heightMap])frm[heightMap].style.color='red'
                                                }else{
                                                    if(frm[heightMap])frm[heightMap].style.color='black'
                                                }
                        
                                                if(frm[preMap]){
                                                        if(frm[preMap].value!='Custom Dimensions'){
                                                                warn=false
                                                        }
                                                }
                                
                                                
                                                if(warn==true){
                                                    if(gAddToOrder==true)alert('Custom cut dimensions must be a minimum of ['+min+'] and a maximum of ['+max+']')
                                                    return true
                                                }
                        
                                                if(price<minPrice)price=minPrice
                                                gPOver=price
                        
                        
                        }else{
                        //SHEET PRODUCTS - NEW STYLE
                        
                                                var min=parseFloat(par.split('Min=')[1].split('mm')[0])
                                                var max=parseFloat(par.split('Max=')[1].split('mm')[0])
                                                var max2=parseFloat(par.split('Max2=')[1].split('mm')[0])
                        
                        
                                                if(document.getElementById('info_min'))document.getElementById('info_min').innerHTML=min+'mm'
                                                if(document.getElementById('info_max'))document.getElementById('info_max').innerHTML=max+'mm x '+max2+'mm'
                        
                        
                                                var warn=false
                                                if(_w<min||_w>max||!_w||frm[widthMap].value.indexOf('.')!=-1){
                                                    warn=true
                                                    if(frm[widthMap])frm[widthMap].style.color='red'
                                                }else{
                                                    if(frm[widthMap])frm[widthMap].style.color='black'
                                                }
                                                
                                                if(_h<min||_h>max2||!_h||frm[heightMap].value.indexOf('.')!=-1){
                                                    warn=true
                                                    if(frm[heightMap])frm[heightMap].style.color='red'
                                                }else{
                                                    if(frm[heightMap])frm[heightMap].style.color='black'
                                                }
                        
                        
                        
                                                if(frm[preMap]){
                                                        if(frm[preMap].value!='Custom Dimensions'){
                                                                warn=false
                                                        }
                                                }
                                
	
                                                
                                                if(warn==true){
                                                    if(gAddToOrder==true)alert('Minimum cut length is '+min+'mm\nMaximum custom panel size is '+max+'mm x '+max2+'mm\n\nPlease check your dimensions and try again')
                                                    return true
                                                }
                        
                                                if(price<minPrice)price=minPrice
					
                                                if(frm[preMap]){ 
                                                        if(frm[preMap].value=='Custom Dimensions'){
								if(frm[polishMap]){
									if(frm[polishMap].checked){		
										var polish=parseFloat(frm[polishMap].id.split('Polished=')[1].split('%')[0])		
										if((price*polish/100)<5){
											price=price+5
										}else{
											price=price+(price*polish/100)
										}
									}
								}
				
								if(frm[drilledMap]){
									if(frm[drilledMap].checked){		
										var drilled=parseFloat(frm[drilledMap].id.split('Drilled=')[1].split('£')[1])		
										price=price+drilled
									}
								}
                                                        }
                                                }	

						if(frm[preMap].value!='Custom Dimensions'){
							if(frm[polishMap]){
								frm[polishMap].checked=false
							}
							if(frm[drilledMap]){
								frm[drilledMap].checked=false
							}
						}				
				
						
                                                gPOver=price
                        
                        }

		}

	}
	return false;
   
};



function modFunc(elem,frm){

	if(frm[parMap]){
		if(elem.name==preMap){

			if(elem.value.indexOf('select')!=-1){
	
				if(frm[widthMap])frm[widthMap].value=''
				if(frm[heightMap])frm[heightMap].value=''
				if(frm[parMap])frm[parMap].style.visibility='hidden'
				if(frm[surMap])frm[surMap].style.visibility='hidden'
				if(frm[widthMap])frm[widthMap].disabled=true
				if(frm[heightMap])frm[heightMap].disabled=true
				if(frm[widthMap])frm[widthMap].style.backgroundColor='buttonface'
				if(frm[heightMap])frm[heightMap].style.backgroundColor='buttonface'

	
			}else if(elem.value.indexOf('Custom')!=-1){
	
				if(frm[parMap])frm[parMap].style.visibility='hidden'
//				if(frm[surMap])frm[surMap].style.visibility='visible'
	
				if(frm[widthMap])frm[widthMap].value=''
				if(frm[heightMap])frm[heightMap].value=''
	
				if(frm[widthMap])frm[widthMap].disabled=false
				if(frm[heightMap])frm[heightMap].disabled=false
	
				if(frm[widthMap])frm[widthMap].style.backgroundColor='white'
				if(frm[heightMap])frm[heightMap].style.backgroundColor='white'
	
	
			}else{
	
				if(frm[parMap])frm[parMap].style.visibility='hidden'
				if(frm[surMap])frm[surMap].style.visibility='hidden'
	
				if(frm[widthMap])frm[widthMap].value=frm[preMap].value.split('x')[0]
				if(frm[heightMap])frm[heightMap].value=frm[preMap].value.split('x')[1]
	
				if(frm[widthMap])frm[widthMap].disabled=true
				if(frm[heightMap])frm[heightMap].disabled=true
	
				if(frm[widthMap])frm[widthMap].style.backgroundColor='buttonface'
				if(frm[heightMap])frm[heightMap].style.backgroundColor='buttonface'


			}
	
		}

                //NEW HIDE AND REVEAL PANEL FOR CUSTOM DIMENSIONS
                if(frm[preMap].value.indexOf('Custom')!=-1){
                        if(document.getElementById('info_panel'))document.getElementById('info_panel').style.display='block' 
                }else{
                        if(document.getElementById('info_panel'))document.getElementById('info_panel').style.display='none' 
                }

		buyFunc(frm)
	}

}