////////////////////////////////////////// Constants ////////////////////////////////////////////////////

var Hmin=700	// Min height
var Hmax=1800	// Max height
var IHmin=500	// Min Internal Height
var IHmax=1650	// Max Internal Height
var IWmin=500	// Min Internal Width
var IWmax=1650	// Max Internal Width
var MH=44.0	// Default Mantelpiece Height 
var MHmin=0.0	// Min Mantelpiece Height
var MHmax=88	// Max Mantelpiece Height
var ML=1320.0	// Default Mantelpiece length 
var MLmin=700	// Min Mantelpiece Length 
var MLmax=2200	// Max mantelpiece length
var MD=145	// Mantelpiece depth
var LSmin=700	// Min Leg Span
var LSmax=MLmax	// Max Leg Span, same as mantelpiece length
var LW=165.0	// Default leg width
var LWmin=100.0	// Min leg width

var TPH=213 	// Default Top-piece height
var TPHmin=100	// Min Top-piece height
var OH=65.0	// Overhang
var OHmax=200.0	// Max overhang
var RB=25.0	// Default Rebate
var RBmax=75	// Max rebate
var RBmin=0	// Min Rebate

//////////////////////////////////////// Global Variables ////////////////////////////////////////////

var loc = new Array()
var samplepic = new Array()
var qsParm = new Array()
var figuresOK = false
var submitPressed=false
var fieldErrMsg = "Please correct the following\n---------------------------------\n"
var fieldsOK = fieldErrMsg 
var msgBlank= "\n--- Empty Field!  " 
var msgRange= "\n--- Out of range!  "
var msgNaN= "\n--- Non Numeric!  "
var msgLength="Please enter a Mantelpiece Length of between " + MLmin + "mm and " + MLmax +"mm"
var msgHeight="Please enter a Height of between " + Hmin + "mm and " + Hmax + "mm"
var msgIntWidth="Please enter a Internal Width of between " + IWmin + "mm and " + IWmax + "mm"
var msgIntHeight="Please enter a Internal Height of between " + IHmin + "mm and " + IHmax + "mm"
var msgRebate ="Please enter a Rebate between " + RBmin + "mm and " + RBmax + "mm"
var msgLegSpan="Please enter a leg span between " + LSmin + "mm and " + LSmax + "mm"
var msgMheight="Please enter a Mantelpiece Height of between " + MHmin + "mm and " + MHmax + "mm"
var msgTopSection="Top Section will be too shallow, less than " + TPHmin + "mm!\nPlease increase Height or decrease Internal Height"
var msgMantelShort="Mantelpiece too short!\nEither increase Mantelpiece length or decrease Leg Span"
var msgMantelLong="Mantelpiece too long!\nCannot be more than " + 2*OHmax + "mm longer than Leg Span\n You could also increase the Leg Span"
var msgLegs="Legs will be too narrow!\nPlease increase Leg Span or reduce Internal Width"
var msgName ="Please enter a Name of between 3 & 35 characters"
var msgEmail="Please enter an eMail address!\n"
var msgEmailShort="Email address must be at least 10 characters in length!"
var msgEmailInvalid="\n--- Please enter a Valid eMail address!"
var msgStyle="\n--- Please Select a Surround Style!"
var msgFigures="\n--- Please check dimensions entered correctly!"
var emailFormatStr = "/^[\s]*\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+[\s]*$/";	
	

////////////////////////////////////////////  Functions  //////////////////////////////////////////////

function preload()   	//pre-load colour sample images into an array
{	loc[0]="http://www.apex-uk.com/surrounds/images/standard.jpg"
	loc[1]="http://www.apex-uk.com/surrounds/images/englight.jpg"
	loc[2]="http://www.apex-uk.com/surrounds/images/geomed.jpg"
	loc[3]="http://www.apex-uk.com/surrounds/images/deepmog.jpg"
	loc[4]="http://www.apex-uk.com/surrounds/images/unfin.jpg"
	
	for (var i=0; i<loc.length; i++)
	{	samplepic[i]=new Image(); 
	   	samplepic[i].src=loc[i]
	}
}	

//------------------------------------------------------------


function showsample()
{    
 	if (document.images)  
 	{
 		document.images["samplepic"].src=samplepic[document.custom.dropDownColour.options.selectedIndex].src;
	}
}
//---------------------------------------------------------------

function calcSurroundSize()
{ 	
	var iwidth=document.custom.intWidth.value;
	var iheight=document.custom.intHeight.value;	
	figuresOK = false;

	if (iheight=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgIntHeight;
	else
	if (isNaN(iheight))
		fieldErrMsg = fieldErrMsg + msgNaN + msgIntHeight;
	else
	if(iheight<IHmin || iheight>IHmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgIntHeight;

	if (iwidth=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgIntWidth;
	else
	if (isNaN(iwidth))
		fieldErrMsg = fieldErrMsg + msgNaN + msgIntWidth;
	else
	if(iwidth<IWmin || iwidth>IWmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgIntWidth;	

	if (fieldErrMsg == fieldsOK)
 	{ // update fields
		document.custom.height.value = iheight*1.0 + TPH + MH;	
		document.custom.mLength.value = iwidth*1.0 + 2.0*LW + 2.0*OH;
		document.custom.mHeight.value = MH;
		document.custom.legSpan.value = iwidth*1.0 + 2.0*LW;
		document.custom.rebate.value = RB;
		figuresOK=true
		return true;
	}
	else
	{	alert(fieldErrMsg);
		fieldErrMsg=fieldsOK;
		return false
	}
}

//------------------------------------------------------------------------------

function checkMyFiguresOK(fieldMsg, subPressed)
{	
	var intWidth=document.custom.intWidth.value;
	var intHeight=document.custom.intHeight.value;
	var mLength=document.custom.mLength.value;
	var height=document.custom.height.value;
	var rebate=document.custom.rebate.value;
	var custName=document.custom.custName.value;
	var mHeight=document.custom.mHeight.value;
	var legSpan=document.custom.legSpan.value;
	figuresOK = false

//-------------------------Internal Height-------------------------
	if (intHeight=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgIntHeight;
	else
	if (isNaN(intHeight))
		fieldErrMsg = fieldErrMsg + msgNaN + msgIntHeight;
	else
	if(intHeight<IHmin || intHeight>IHmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgIntHeight;

//--------------------------Internal Width--------------------------	
	if (intWidth=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgIntWidth;
	else
	if (isNaN(intWidth))
		fieldErrMsg = fieldErrMsg + msgNaN + msgIntWidth;
	else
	if(intWidth<IWmin || intWidth>IWmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgIntWidth;	

//---------------------------Mantepiece Length------------------------	
	if (mLength=="")
	 	fieldErrMsg = fieldErrMsg + msgBlank + msgLength;
	else
	if (isNaN(mLength))
		fieldErrMsg = fieldErrMsg + msgNaN + msgLength;
	else
	if(mLength<MLmin || mLength>MLmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgLength;

//----------------------------Overall Height--------------------------
	if (height=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgHeight;
	else
	if (isNaN(height))
		fieldErrMsg = fieldErrMsg + msgNaN + msgHeight;
	else
	if(height<Hmin || height>Hmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgHeight;	

//---------------------------LegSpan------------------------	
	if (legSpan=="")
	 	fieldErrMsg = fieldErrMsg + msgBlank + msgLegSpan;
	else
	if (isNaN(legSpan))
		fieldErrMsg = fieldErrMsg + msgNaN + msgLegSpan;
	else
	if(legSpan<LSmin || mLength>LSmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgLegSpan;

//----------------------------Mantelpiece Height--------------------
	if (mHeight=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgMheight;
	else
	if (isNaN(mHeight))
		fieldErrMsg = fieldErrMsg + msgNaN + msgMheight;
	else
	if(mHeight<MHmin || mHeight>MHmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgMheight;	

//------------------------------Rebate--------------------------------	

	if (rebate=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgRebate;
	else
	if (isNaN(rebate))
		fieldErrMsg = fieldErrMsg + msgNaN + msgRebate;
	else
	if (rebate > RBmax || rebate < RBmin) 
		fieldErrMsg = fieldErrMsg + msgRange + msgRebate;

//------------------ check users figures add up -----------------------------------

	if (fieldErrMsg == fieldsOK)
	{	
		if (height - MH - intHeight < TPHmin)
			fieldErrMsg = fieldErrMsg + msgTopSection;
		else
		if (mLength < legSpan)
			fieldErrMsg = fieldErrMsg + msgMantelShort;
		else
		if (mLength - legSpan > 2*OHmax)
			fieldErrMsg = fieldErrMsg + msgMantelLong;
		else
		if (legSpan - intWidth < 2*LWmin )
			fieldErrMsg = fieldErrMsg + msgLegs
	}

	if (fieldErrMsg == fieldsOK)
	    {
		alert ("        Thank You! \nYour Figures seem OK \n ")
		figuresOK = true
	    }
	else 
	    {	alert(fieldErrMsg)
		fieldErrMsg=fieldsOK
	    }
}
 
//----------------------------------------------------------------------------------

function validated()
{
// check rest of form, if ok, pass to php mail program
var sstyle=document.custom.surroundStyle
var intWidth=document.custom.intWidth.value
var intHeight=document.custom.intHeight.value
var mLength=document.custom.mLength.value
var height=document.custom.height.value
var rebate=document.custom.rebate.value
var custName=document.custom.custName.value
var mHeight=document.custom.mHeight.value
var legSpan=document.custom.legSpan.value
var custEmail=document.custom.custEmail.value
var custAdds=document.custom.custAdds.value
var custName=document.custom.custName.value
filter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
filter2=/^[\s]*\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+[\s]*$/
filter3=/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
var IHBG=document.custom.intHeight.style.background


//-----------------------------Surround Style ----------------------------
	
	if (sstyle.selectedIndex < 1)
		fieldErrMsg =  fieldErrMsg + msgStyle
//	else alert("selection was " + style.options[sstyle.selectedIndex].text)

//--------------------------------Dimensions------------------------------	
//------------------------------Internal Height-------------------------
	if (intHeight=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgIntHeight;if(document.all || document.getElementByID){IHBG = "yellow";}
	else
	if (isNaN(intHeight))
		fieldErrMsg = fieldErrMsg + msgNaN + msgIntHeight;
	else
	if(intHeight<IHmin || intHeight>IHmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgIntHeight;

//--------------------------Internal Width--------------------------	
	if (intWidth=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgIntWidth;
	else
	if (isNaN(intWidth))
		fieldErrMsg = fieldErrMsg + msgNaN + msgIntWidth;
	else
	if(intWidth<IWmin || intWidth>IWmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgIntWidth;	

//---------------------------Mantepiece Length------------------------	
	if (mLength=="")
	 	fieldErrMsg = fieldErrMsg + msgBlank + msgLength;
	else
	if (isNaN(mLength))
		fieldErrMsg = fieldErrMsg + msgNaN + msgLength;
	else
	if(mLength<MLmin || mLength>MLmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgLength;

//----------------------------Overall Height--------------------------
	if (height=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgHeight;
	else
	if (isNaN(height))
		fieldErrMsg = fieldErrMsg + msgNaN + msgHeight;
	else
	if(height<Hmin || height>Hmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgHeight;	

//---------------------------LegSpan------------------------	
	if (legSpan=="")
	 	fieldErrMsg = fieldErrMsg + msgBlank + msgLegSpan;
	else
	if (isNaN(legSpan))
		fieldErrMsg = fieldErrMsg + msgNaN + msgLegSpan;
	else
	if(legSpan<LSmin || mLength>LSmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgLegSpan;

//----------------------------Mantelpiece Height--------------------------
	if (mHeight=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgMheight;
	else
	if (isNaN(mHeight))
		fieldErrMsg = fieldErrMsg + msgNaN + msgMheight;
	else
	if(mHeight<MHmin || mHeight>MHmax)
		fieldErrMsg = fieldErrMsg +  msgRange + msgMheight;	

//------------------------------Rebate--------------------------------	

	if (rebate=="") 
		fieldErrMsg = fieldErrMsg + msgBlank + msgRebate;
	else
	if (isNaN(rebate))
		fieldErrMsg = fieldErrMsg + msgNaN + msgRebate;
	else
	if (rebate > RBmax || rebate < RBmin) 
		fieldErrMsg = fieldErrMsg + msgRange + msgRebate;

//------------------ check users figures add up -----------------------------------

	if (fieldErrMsg == fieldsOK)
	{	
		if (height - MH - intHeight < TPHmin)
			fieldErrMsg = fieldErrMsg + msgTopSection;
		else
		if (mLength < legSpan)
			fieldErrMsg = fieldErrMsg + msgMantelShort;
		else
		if (mLength - legSpan > 2*OHmax)
			fieldErrMsg = fieldErrMsg + msgMantelLong;
		else
		if (legSpan - intWidth < 2*LWmin )
			fieldErrMsg = fieldErrMsg + msgLegs
	}


//------------------------------Customer Name------------------------------	

	if (custName=="")
		fieldErrMsg =  fieldErrMsg + msgBlank + msgName;
	else
	if (custName.length<3) 
		fieldErrMsg =  fieldErrMsg + msgRange + msgName;

//-------------------------------Email-------------------------------------
	
	if (custEmail=="")
		fieldErrMsg = fieldErrMsg + msgBlank + msgEmail
	else
	if (custEmail.length<10 ) 
		fieldErrMsg = fieldErrMsg +  msgRange + msgEmailShort
	else
	if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(custEmail))
		fieldErrMsg = fieldErrMsg + msgEmailInvalid
	
//---------------------------Additional Comments------------------------

// no need to check as optional field, limited to 250 chars by maxlength property on html page

//--------------------------so finish as follows:------------------------
	
if (fieldErrMsg == fieldsOK)
	{
		return true
	}
else 
	{	alert(fieldErrMsg)
		fieldErrMsg=fieldsOK
		return false
	}
}

///////////////////////////////////////////////////  END  //////////////////////////////////////////////////////
