// Info request initial checking

function holdsubmit() 
{	
    bcodes = document.getElementsByName('brochure_code[]');
    if (!bcodes.length) { 
        alert('no brochures found');
        return;
    }
        
    selected = 0;
    num = bcodes.length;

    for (i = 0; i < num; i++) { 
        if (bcodes[i].checked) { 
            selected++;
        }
    }

    if (selected == 0) { 
        alert('Please select at least one Brochure');
        return false;
    }
	
  
  	//added by Leigh Smith 12.10.06
	//Firstname - check for integers or empty
	//Surname - check that length is minimum of two, no integers, not empty
	
	function Trim(s){
    	return s.replace(/(^\s+)|(\s+$)/g, "");
	}

	function checkStringForInt(val){
		var result = val.match(/^[a-zA-Z]+$/);
		return (result != null);
	}

	//check title is selected here now because it appears comes before first and surname.
	title = document.getElementById("custtitle").value;	
	if(title==""){
		alert("Please select a title"); 
		return false;
	}
			
	//trim input to makes sure user hasn't just used a space
	firstname = Trim(document.getElementById("firstname").value);
	surname = Trim(document.getElementById("lastname").value);

	//check first name
	if (firstname.length < 1){
		alert("First name must contain at least one character"); 
		return false;
	}

  	if((firstname.length > 0) && (!checkStringForInt(firstname))){
		 alert("First name must not contain numerals"); 
		 return false;
	}
	
	//check last name
	if(surname.length < 2){
		alert("Surname must contain at least two characters"); 
		return false;
	}

    if((surname.length > 1) && (!checkStringForInt(surname))){
		 alert("Surname must not contain numerals"); 
		 return false;
	}
    //
  
  
  df = document.forms['cust_details'];
    requiredfield = new Array('custtitle', 'firstname', 'lastname', 'address1', 'address2', 
                              'address4', 'addresszip1', 'addresszip2','email');
    requiredname  = new Array('Title', 'First Name (or initial)', 'Surname', 'House number / name', 
                              'Street / Road', 'City / Town', 'Postcode', 'Postcode','Email Address');

    regex = /^([_~a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,4}))$/i;

    // Validate email address
    if (Trim(df.email.value) !='' && (! df.email.value.match(regex))) {
        alert("We could not verify your email address.\nPlease check you have typed it correctly");
        return false;
    }
	
    if (Trim(df.email.value) !='' && df.email_confirm.value != df.email.value) {
        alert("Your email address and email confirmation did not match :\nPlease check you have typed your email address correctly.");
        return false;
    }
	
    //check required field
	for (i = 0; i < requiredfield.length; i++) {
        if (Trim(df[requiredfield[i]].value) == '') {
           	alert("Please fill in the required field :\n" + requiredname[i]);
            return false;
        }
    }
	

    if (df.mediachoice.selectedIndex == 0 || df.mediacode.selectedIndex == 0) {
        alert("Please enter a choice in both\n'How you heard about us' boxes");
        return false;
    }	

    // Successful submit - disable button to prevent pressing twice	
    if (df.submitform.disabled) {
        return false;
    } else {
        df.submitform.disabled = true;
    }

    return true;
}

function brochures_toggle_colour(obj)
{
    tr = false;
    
    for (tr = obj; tr && tr.tagName.toLowerCase() != 'tr'; tr = tr.parentNode);

    if (tr) {
        if (obj.checked) {
            tr.className = 'checked';
        } else {
            tr.className = '';
        }
    }
}

// How many brochures are selected?

function limit_brochures(obj)
{
    bcodes = document.getElementsByName('brochure_code[]');
    if (!bcodes.length) { 
        alert('no brochures found');
        return;
    }
        
    selected = 0;
    num = bcodes.length;

    for (i = 0; i < num; i++) { 
        if (bcodes[i].checked) { 
            selected++;
        }
    }

    if (selected > 2) { 
        alert('You may only select 2 brochures');
        obj.checked = false;
        brochures_toggle_colour(obj);
    } else {
        brochures_toggle_colour(obj);
    }        
}

function loadmedia() 
{
    df = document.forms['cust_details'];
    m  = df.mediachoice;
    d  = df.mediacode;
    mc = document.getElementById('mediacode');

    if (m.selectedIndex > 0) {
        mc.style.display = '';
        d.options.length = 0;
        d.options[0] = new Option;
        d.options[0].text = 'Please Select -----';
        d.options[0].value = '';
        i = m[m.selectedIndex].value;
        for (j = 1; j <= media[i].length; j++) {
            optionvalue = media[i][j-1].split(',');
            d.options.length++;
            d.options[j] = new Option();
            d.options[j].text = optionvalue[0];
            d.options[j].value = optionvalue[1];
        }
        d.selectedIndex = 0;
    } else {
        mc.style.display = 'none';
    }
}

function checkcodes()
{
    // Allow only up to two brochures to be requested
    df = document.forms['cust_details'];
    result = 0;
    for (i = 1; i <= 6; i++) {
	if (df.elements['brochure['+i+']'].checked) {
            result++;
            if (result > 2) {
                alert('Please select up to two brochures only');	
                return false;
            }
	}
    }

    return true;
}












