function $(x)
{
	if (x.charAt(0) == '#')
	{
		return window.document.getElementById(x.substr(1,x.length-1));
	}
	else
	{
		return window.document.getElementsByName(x);
	}
}

AddEventListener = function(object, action, func, prop)
{
    if (object.addEventListener) // standardizovany postup
    {
        object.addEventListener(action, func, prop);
        return true;
    }
    else if (object.attachEvent)  // microsoft postup
    {
        object.attachEvent("on"+action, func);
        return true;
    } else {
        // report problem
        return false;
    }
}

function email_require_sign_update()
{
	if ($("#replyrequired").checked) {
		$("#email-required-sign").style.display = "";
	}
	else {
		$("#email-required-sign").style.display = "none";
	}
}

function check_and_change(event)
{
	var form = $("#contact-us-form");
	var replyrequired = $("#replyrequired").checked;
	var testingtopic = $("#contact-topic").value == 5;
	
	var subjectfield = $("#subject-field");
	var messagefield = $("#message-field");
	
	if (testingtopic) {
		subjectfield.disabled = true;
		subjectfield.value = "Testing request";
		messagefield.innerHTML = "I would like to ask you to test the following products";
		$("#replyrequired").checked = false;
		$("#replyrequired").disabled = true;
	}
	else {
		subjectfield.disabled = false;
		if (subjectfield.value == "Testing request") {
			subjectfield.value = "";
		}
		messagefield.innerHTML = "Message";
	}

	if ($("#email-field").value.length > 0 && !testingtopic) {
		$("#replyrequired").disabled = false;
	}	

	email_require_sign_update();
}

function email_type(event)
{
	var field = $("#email-field");
	var checkbox = $("#replyrequired");
	var testingtopic = $("#contact-topic").value == 5;
	
	if (field.value.length > 0 && !testingtopic) {
		checkbox.disabled = false;
	}
	else if (field.value.length == 0) {
		checkbox.disabled = true;
		checkbox.checked = false;
	}
	
	email_require_sign_update();
}

function init_form(event)
{
	AddEventListener($("#contact-topic"), "change", check_and_change, false);
	AddEventListener($("#contact-topic"), "keypress", check_and_change, false);
	AddEventListener($("#replyrequired"), "change", check_and_change, false);
	AddEventListener($("#replyrequired"), "click", check_and_change, false);
	AddEventListener($("#email-field"), "keyup", email_type, false);
	AddEventListener($("#email-field"), "change", email_type, false);
	check_and_change(null);
	$("#replyrequired").disabled = $("#email-field").value == "";
}

if (!AddEventListener(window, "load", init_form, false))
{
    if (!AddEventListener(document, "load", init_form, false))
    {
        ; // report problem
    }
}

