// JavaScript Document

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function checkMail(x) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,4})+$/;
	if (!filter.test(x))
		return false;
	return true;
}

function checkPhone(x) {
	var filter  = /^([0-9\+\-])+$/;
	if (!filter.test(x))
		return false;
	return true;
}

function validate(which) {
	var name = which.name.value;
	var email = which.email.value;
	var phonenumber = which.phonenumber.value;
	var ebayid = which.ebayid.value;
	var theme = which.theme.value;
	var products = which.products.value;
	var tagline = which.tagline.value;
	var colourscheme = which.colourscheme.value;
	var company = which.company.value;
	var additional = which.additional.value;
	
	
	name = trim(name,' ');
	email = trim(email,' ');
	phonenumber = trim(phonenumber,' ');

	if ( name == "" || name == "Name..." ) {
        alert ( "Please fill in the 'Name' box." );
        return false;
    }

	if ( email == "" || email == "E-Mail..." || !checkMail(email)) {
        alert ( "Please fill in the 'E-Mail' box correctly." );
        return false;
    }

	if ( phonenumber == "" || phonenumber == "Tel. Number..." || !checkPhone(phonenumber)) {
        alert ( "Please fill in the 'Tel. Number' box correctly." );
        return false;
    }

	if (!which.website.checked && !which.store.checked && !which.listing.checked && !which.branding.checked && !which.printwork.checked) {
        alert ( "Please select at least one of the services you might be interested in." );
        return false;
	}
	
	/**/
	if (ebayid == "" && products == "" && tagline == "" && colourscheme == "" && company == ""  && (additional == "" || additional == "(e.g. particular websites or eBay Stores/Listings liked)"))
		return confirm('Are you sure you want to submit without adding extra information?');
	else
		return true;
}
