function trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

function set_focus_field(focus_field, field) {
        if (!focus_field) {     
                focus_field = field;
        }
}

function validate_tel(field) {
	var telname = trim(field.value).replace(/\.tel$/, "");
	
	if (telname.match(/^\-/)) {
		return false;
	}
	if (telname.match(/\-$/)) {
		return false;
	}
	if (telname.match(/^..\-\-/) && !(telname.match(/^xn--/))) {
		return false;
	}
	if (telname.length < 2) {
		return false;
	}
	if (telname.length > 63) {
		return false;
	}
	if (telname.length > 7) {
		if (!telname.match(/[a-z]/gi)) {
			return false;
		}
	}
	if (!telname.match(/^[a-z0-9\-]+$/)) {
		return false;
	}
	
	return true;
}

function validate_textarea(field,str) {
	field.value = trim(field.value);

	if (field.value == str) {
		return false;
	}

	if (field.value == "") {
		return false;
	}

	return true;
}

function validate_input(field) {
	field.value = trim(field.value);

	if (field.value == "") {
		return false;
	}

	return true;
}

function validate_email(field) {
	with (field) {
		value = trim(value);
		apos   = value.indexOf("@");
		dotpos = value.lastIndexOf(".");
		if (apos < 1 || dotpos - apos < 2) {
			return false;
		}
	}
	if (!field.value.match(/^[a-z0-9\-_.]+@[a-z0-9\-_.]+\.[a-z]{2,}$/)) {
		return false;
	}

	return true;
}

function validate_dropdown(field) {
	field.value = trim(field.value);

	if (field.value > 0) {
		return true;
	}

	return false;
}

function set_red(field) {
	field.style.backgroundColor = '#ffdfdf';
}

function clear_red(field) {
	field.style.backgroundColor = '';
}

function set_status(str) {
	document.getElementById("form_status").innerHtml = str;
}

function validate_form(thisform) {
        var has_errors = false;
        var focus_field = null;

	with (thisform) {
		clear_red(telname);
		clear_red(why);
		clear_red(how);
		clear_red(you);
		clear_red(yourself);
		clear_red(email);
		clear_red(domains);

                if (validate_tel(telname) == false) {
                        has_errors = true;
                        set_focus_field(telname);
			set_red(telname);
                }
                if (validate_textarea(why, "Enter your .tel story here") == false) {
                        has_errors = true;
                        set_focus_field(why);
			set_red(why);
                }
                if (validate_textarea(how, "Tell us how you use your .tel domain and how you have benefitted from it") == false) {
                        has_errors = true;
                        set_focus_field(how);
			set_red(how);
                }
                if (validate_input(you) == false) {
                        has_errors = true;
                        set_focus_field(you);
			set_red(you);
                }
                if (validate_textarea(yourself, "Provide a short bio and your relation to .tel") == false) {
                        has_errors = true;
                        set_focus_field(yourself);
			set_red(yourself);
                }
                if (validate_email(email) == false) {
                        has_errors = true;
                        set_focus_field(email);
			set_red(email);
                }
                if (validate_dropdown(domains) == false) {
                        has_errors = true;
                        set_focus_field(domains);
			set_red(domains);
                }
        }

	if (has_errors) {
		set_status("The fields highlighted in red are not filled correctly");
	}

        // Invert the flag. has_errors = true becomes false, so form doesn't send
        return !has_errors;
}


