/*
form validation functions

@author S.Payne

used by:
literature.html
samples.html
 
07-05-08
*/

function checkMaxswatches(obj)
{

/*
 
Contrasts + Reflections + Modular <= 5

Laminate + Worktop <= 5

Classic + Statement <= 3
 
*/
total = 0;

f = document.forms['samples'];

currentItem   = obj.name;

currentItem   = currentItem.substring(3,currentItem.length)

underScorePos = currentItem.indexOf('_');

currentItem   = currentItem.substring(0,underScorePos);

//alert(currentItem);

if(currentItem == 'classic' || currentItem == 'statement' || currentItem == 'timberclassicsolidsurfaces' || currentItem == 'timberstatementsolidsurfaces')
{
maxTotal      = 3
pattern       = '(classic)|(statement)|(timberstatementsolidsurfaces)|(timberstatementsolidsurfaces)';
}

else if(currentItem == 'laminate' || currentItem == 'worktop' || currentItem == 'timberlaminate')
{
maxTotal      = 5
pattern       = '(laminate)|(worktop)|(timberlaminate)';
}

else
{
maxTotal      = 5
pattern       = '(contrasts)|(reflections)|(modular)';
}

//alert(pattern);

for(i=0;i<f.length;i++)
{
if(f.elements[i].type != 'checkbox') continue;

if(f.elements[i].name.match(pattern))
{
if(f.elements[i].checked)
{
total++;    
}
}
}

if(total > maxTotal)
{
sectionList = pattern.replace(/[\(\)\|]/g,' ');
alert('Choosing this option will mean you have selected ' + total + ' items. \nYou may only choose up to ' + maxTotal + ' options from the following sections : \n\n' + sectionList);
obj.checked = false;
return false;
}
return true;
}

function validateForm(id)
{

f = document.forms[id];

for(i=0;i<f.length;i++)
{
    
//alert(f.elements[i].name + ' : ' + f.elements[i].value);

// optional fields
if(f.elements[i].name == 'telephone') continue;
if(f.elements[i].name == 'title_explanation') continue;
if(f.elements[i].name == 'referrer_explanation') continue;

// check for empty boxes
if(f.elements[i].value == '')
{
alert('Please fill in the ' + f.elements[i].name + ' field.');
f.elements[i].focus();
return false;
} 

// check that a title of 'other' has a value in title_explanation
if(f.elements[i].name == 'title')
{
if(f.elements[i].value == 'Other')
{
if(f.title_explanation.value.search(/[\w]+/) == -1)
{
alert('Please specify your title.');
f.title_explanation.focus();
return false;    
}
}
}

// check that 'search engine - other' and 'other' options have a value in referrer_explanation
if(f.elements[i].name == 'Where_did_you_find_out_about_us')
{

if(f.elements[i].value == 'Other'  || f.elements[i].value == 'Search Engine - Other')
{
if(f.referrer_explanation && f.referrer_explanation.value.search(/[\w]+/) == -1)
{
alert('Please specify how you found out about us.');
f.referrer_explanation.focus();
return false;    
}
}
}
}

if (f.email && !validEmail(f.email.value)) 
{
alert("This is not a valid email address")
f.email.focus();
f.email.select();
return false;
}

if (f.postcode && !validPostcode(f.postcode.value)) 
{
alert("This is not a valid postcode");
f.postcode.focus();
f.postcode.select();
return false;
}

if (f.referrer_explanation && !f.Utopia_retail_brochure && !f.Utopia_retail_price_list && !f.Timber_Collection_brochure && !f.Timber_Collection_price_list  ) 
{
alert("Please select at least one option");
f.address.focus();
f.address.select();
return false;
}

if (f.address && f.address.value.search(/[\w]+/) == -1) 
{
alert("Please enter an address");
f.address.focus();
f.address.select();
return false;
}

// visitors must select at least one brochure
if(f.Utopia_retail_brochure.checked == false && f.Utopia_retail_price_list.checked == false &&
f.Timber_Collection_brochure.checked == false &&
f.Timber_Collection_price_list.checked == false)
{
alert('Please select at least one brochure or price list.');
f.Utopia_retail_brochure.focus();
return false;  
}

return true;

}

function validEmail(email)
{
	
if(email == "") {return false;}

if(email.search(/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i) === -1)
{
return false;
}
return true;
}

function validPostcode(postcode)
{
	
if (postcode == "") {return false;}

if(postcode.search(/^[A-Z]{1,2}\d[A-Z\d]?\s?\d[ABD-HJLNP-UW-Z]{2}$/i) === -1)
{
return false;
}
return true;
}



function checkDisplay(selectBox)
{

option_id = document.forms[0].elements[selectBox].selectedIndex;

myStatus = 'none';

if((selectBox== 'Where_did_you_find_out_about_us' && (option_id == 4 || option_id == 35)) ||  selectBox== 'title' && option_id == 5)
{

myStatus = 'inline';
    
}

id = selectBox == 'Where_did_you_find_out_about_us' ? 'explanation' : 'explanation_of_other_title';

document.getElementById(id).style.display = myStatus;

}



