function trim(s) {
    while ((s.length > 0) && "\n\r\t\v ".indexOf(s.substring(0,1)) != -1) s = s.substring(1, s.length);
    while ((s.length > 0) && "\n\r\t\v ".indexOf(s.substring(s.length-1, s.length)) != -1) s = s.substring(0,s.length-1);
    return s;
}

function valid_email(s) {
    // trim string:
    s = trim(s);     
    
    // test with a regular expression    
    var objRegExp  = /(^[A-z]([A-z0-9_\.\-]*)@([A-z0-9_\.]*)\.([A-z]*)$)/;    
    return objRegExp.test(s);
}

function valid_email_list(s) {
    s = s.split('\n');
    valid = false;
    for(i=0;i<s.length;i++) {
        line = trim(s[i]);
        if (line.length >0) {
            if (valid_email(line)) valid = true;
            else return false;
        }
    }
    return valid;    
}


function validate_form(form) {

    if (form.firstname && form.firstname.value == ""){
	    alert('Please enter your first name.');
	    form.firstname.focus();
	    return false;
    }

    if (form.lastname && form.lastname.value == ""){
	    alert('Please enter your last name.');
	    form.lastname.focus();
	    return false;
    }
    
    if (form.contactno && form.contactno.value == ""){
        alert('Please enter your contact number.');
        form.contactno.focus();
        return false;
    }

    if (form.email && form.email.value == ""){
	    alert('Please enter your email address.');
	    form.email.focus();
	    return false;
    }
        
    if (form.email && !valid_email(form.email.value)) {
        alert('Please enter a valid email address.');
	    form.email.focus();
	    return false;
    }

    if (form.country && form.country.value == ""){
	    alert('Please select your country name.');
	    form.country.focus();
	    return false;
    }
	    
    if (form.organisation && form.organisation.value == ""){
	    alert('Please enter your organisation name.');
	    form.organisation.focus();
	    return false;
    }
        
    if (form.industry && form.industry.value == ""){
	    alert('Please select your industry.');
	    form.industry.focus();
	    return false;
    }

    if (form.level && form.level.value == ""){
	    alert('Please select your level.');
	    form.level.focus();
	    return false;
    }

    if (form.relationship && form.relationship.value == ""){
	    alert('Please select your relationship with integro.');
	    form.relationship.focus();
	    return false;
    }

    if (form.relationship && form.relationship_other && form.relationship.value == "other" && form.relationship_other.value == "") {
	    alert('Please enter your relationship with integro.');
	    form.relationship_other.focus();
	    return false;
    }

    if (form.comments && form.comments.value == ""){
	    alert('Please enter your comments.');
	    form.comments.focus();
	    return false;
    }
        
    if ( form.smartlender_general &&
         form.smartlender_retail && 
         form.smartlender_corporate && 
         form.smartlender_clms &&
         form.ib_retail &&
         form.ib_corporate &&
         !(form.smartlender_general.checked ||
           form.smartlender_retail.checked ||
           form.smartlender_corporate.checked ||
           form.smartlender_clms.checked ||
           form.ib_retail.checked ||
           form.ib_corporate.checked  )) {
        alert('Please select a product.');
	    form.smartlender_general.focus();
	    return false;       
    }

    if (form.email_list && trim(form.email_list.value).length == 0) {
        alert("Please enter at least one email address.");
        return false;
    }

    if (form.email_list && !valid_email_list(form.email_list.value)) {
        alert("Please enter one valid email address per line.");
        return false;
    }
	
    return true;
}
