var helpText = MO.N + MO.N + "If you experience any difficulties with this form, please call us at +31-346-285144 (or toll-free 1-800-518-6001 in the U.S. and Canada).";

function testString(form, Ctrl, ErrText) {
	if (Ctrl.value == "") {
		validatePrompt (Ctrl, "Please enter " + ErrText + '.' + helpText);
		return (false);
	} else {
		return (true);
	}
}

// simple test for address format
function testMAIL(form, Ctrl) {
	if (Ctrl.value == "" || Ctrl.value.indexOf ('@', 0) == -1) {
		validatePrompt (Ctrl, "Please enter your email address." + helpText);
		return (false);
	} else {
		return (true);
	}
}

function testPlatform(form, Ctrl, ErrText) {
	var i=0;
	for(i=0;i<Ctrl.length;i++)
	{
		if (Ctrl[i].checked === true) {return true;}
	}
	validatePrompt (Ctrl, "Please select your " + ErrText + '.' + helpText);
	return (false);
	
}

function testSelect(form, Ctrl, ErrText) {
	
	var optionValue = Ctrl.options[Ctrl.selectedIndex].value;
	
	if (optionValue == "PleaseSelect") {
		validatePrompt (Ctrl, "Please indicate " + ErrText + '.' + helpText);
		return false;
	} 
	
	return true;	
}

function testAgreed(form, Ctrl) {
	if (!Ctrl.checked) {
		validatePrompt (Ctrl, "Please indicate that you agree to the terms of this download by checking the appropriate checkbox." + helpText);
		return false;
	}
	return true;	
}

function submitme(form){	
	if (!testString(form, form.FirstName,"your first name")) {return;}
	if (!testString(form, form.Surname,"your last name")) {return;}
	if (!testString(form, form.Company,"your company name")) {return;}
	if (!testMAIL(form, form.Email)) {return;}
	if (!testString(form, form.Telephone,"your telephone number")) {return;}
	if (!testString(form, form.Address1,"your address")) {return;}
	if (!testString(form, form.City,"your city")) {return;}
	if (!testString(form, form.Postcode,"your postal or zip code")) {return;}
	if (!testSelect(form, form.Country,"your country")) {return;}
	if (!testPlatform(form, form.Platform,"the primary platform you develop on")) {return;}
	if (!testString(form, form.HowDidYouHear,"how you heard about us")) {return;}
	if (!testString(form, form.Developers,"the number of Omnis developers who work for your company")) {return;}
	if (!testSelect(form, form.Version,"the Omnis version you currently use")) {return;}
	if (!testString(form, form.Databases,"the databases you use or plan to use")) {return;}
	if (!testSelect(form, form.Kind,"the kind of company you work for")) {return;}
	if (!testAgreed(form, form.Agreed)) {return;}
	
	form.action="http://omnis.masterobjects.com/masterstudio/download-trial.php";
	form.submit();
	
}

// support routine used during tests
function validatePrompt (Ctrl, PromptStr) {
	alert (PromptStr);
	Ctrl.focus();
	return;
}

function setFields() {	
	if (MO.env.lin)
	{
		document.EvalForm.Platform[2].checked=true;
	}
	else if (MO.env.mac)
	{
		document.EvalForm.Platform[1].checked=true;
	}
	else if (MO.env.win)
	{
		document.EvalForm.Platform[0].checked=true;
	}
}

function replace(string,text,by) {
	// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength === 0) || (txtLength === 0)) {return string;}
	
    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) { return string; }
    if (i == -1) { return string; }
	
    var newstr = string.substring(0,i) + by;
	
    if (i+txtLength < strLength) {
        newstr += replace(string.substring(i+txtLength,strLength),text,by);
	}
	
    return newstr;
}