/*
 * General
 */
function hideElementByName(form, element) {
	try {
		// document.forms['eccform'].elements['evaluating']
		document.forms[form].elements[element].className = 'hidden';
	} catch (e) {
		throw e;
	}
}

function showElementByName(form, element, new_clazz) {
	var new_clazz = (new_clazz == null) ? '' : new_clazz;
	try {
		document.forms[form].elements[element].className = new_clazz;
	} catch (e) {
		throw e;
	}
}

function hideElementById(id) {
	try {
		document.getElementById(id).className = 'hidden';
	} catch (e) {
		throw e;
	}
}

function showElementById(id, new_clazz) {
	var new_clazz = (new_clazz == null) ? '' : new_clazz;
	try {
		document.getElementById(id).className = new_clazz;
	} catch (e) {
		throw e;
	}
}

function isFormEnabled(name) {
	if(document.forms['eccform'].elements[name].value == 'true') {
		return true;
	}
	return false;
}

/*
 * Formular Checks
 */

function checkForm() {
	clearErrors();
	var return_value = true;

	if (!checkFormFields()) {
		return_value = false;
	}
	
	if(isFormEnabled('form_company')) {
		if(isEvaluatingForCompany()) {
			if(!checkFormFieldsCompany()) {
				return_value = false;
			}
		}
	}
	
	if(isFormEnabled('form_download')) {
		if(!checkFormFieldsDownload()) {
			return_value = false;
		}
	}
		
	postErrors();
	return return_value;
}

function checkFormFields() {
	var return_value = true;

	var firstname = document.eccform.firstname;
	if (firstname.value == '') {
		errorContainer.push(new formError(firstname, '- Firstname is missing',
				'col_firstname'));
		return_value = false;
	}

	var lastname = document.eccform.lastname;
	if (lastname.value == '') {
		errorContainer.push(new formError(lastname, '- Lastname is missing',
				'col_lastname'));
		return_value = false;
	}

	var country = document.eccform.country;
	if (country.value == '') {
		errorContainer.push(new formError(country, '- Country is missing',
				'col_country'));
		return_value = false;
	}

	var email1 = document.eccform.email1;
	if (email1.value == '') {
		errorContainer.push(new formError(email1, '- Email is missing',
				'col_email1'));
		return_value = false;
	}

	var email2 = document.eccform.email2;
	if (email2.value == '') {
		errorContainer.push(new formError(email2,
				'- Email confirmation is missing', 'col_email2'));
		return_value = false;
	}
	
	if(return_value == true) {
		if(!checkEmailValid()) {
			return_value = false;
		} else {
			if(!checkEmailEqual()) {
				return_value = false;
			}
		}
	}

	return return_value;
}

function checkFormFieldsCompany() {
	var return_value = true;
	
	var company = document.eccform.company;
	if(company.value == '') {
		errorContainer.push(new formError(company, '- Company is missing', 'col_company'));
		return_value = false;
	}
	var company_title = document.eccform.company_title;
	if(company_title.value == '') {
		errorContainer.push(new formError(company_title, '- Title is missing', 'col_company_title'));
		return_value = false;
	}
	
	var company_evaluating_for = document.eccform.company_evaluating_for;
	if(company_evaluating_for.value == '') {
		errorContainer.push(new formError(company_evaluating_for, '- Evaluation is missing', 'col_company_evaluating_for'));
		return_value = false;
	}
	
	var company_industry = document.eccform.company_industry;
	if(company_industry.value == '') {
		errorContainer.push(new formError(company_industry, '- Industry is missing', 'col_company_industry'));
		return_value = false;
	}
	
	var company_funding_status = document.eccform.company_funding_status;
	if(company_funding_status.value == '') {
		errorContainer.push(new formError(company_funding_status, '- Funding status is missing', 'col_company_funding_status'));
		return_value = false;
	}
	
	var company_purchase_timeframe = document.eccform.company_purchase_timeframe;
	if(company_purchase_timeframe.value == '') {
		errorContainer.push(new formError(company_purchase_timeframe, '- Purchase timeframe is missing', 'col_company_purchase_timeframe'));
		return_value = false;
	}
	
	var company_employees = document.eccform.company_employees;
	if(company_employees.value == '') {
		errorContainer.push(new formError(company_employees, '- Number of employees is missing', 'col_company_employees'));
		return_value = false;
	}
	
	return return_value;
}

function checkFormFieldsDownload() {
	var license = document.eccform.license;
	if(license.checked == false) {
		errorContainer.push(new formError(license, '- You must read and accept licence agreement', 'col_license'));
		return false;
	}
	return true;
}

function checkEmailEqual() {
	if(document.eccform.email1.value != document.eccform.email2.value) {
		errorContainer.push(new formError(document.eccform.email2, '- The e-mail addresses doesn\'t match', 'col_email2'));
		return false;
	}
	return true;
}

function checkEmailValid() {
	var email = document.eccform.email1;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
        errorContainer.push(new formError(email, '- The e-mail is not valid', 'col_email1', ''));
        return false;
    }
    return true;
}

/*
 * Error
 */
var errorContainer = new Array();

function formError(element, message, rowId) {
	this.element = element;
	this.message = message;
	this.rowId = rowId;
}

function postErrors() {
	if (errorContainer.length > 0) {
		showElementById('error-message');
		for (i = 0; i < errorContainer.length; i++) {
			document.getElementById(errorContainer[i].rowId).className = 'form-error';
			if(errorContainer[i].message != '') {
				document.getElementById('error-message').innerHTML += '<p>' + errorContainer[i].message + '</p>';
			}
		}
	}
}

function clearErrors() {
	if (errorContainer.length > 0) {
		for (i = 0; i < errorContainer.length; i++) {
			document.getElementById(errorContainer[i].rowId).className = 'colleft';
		}
	}
	document.getElementById('error-message').innerHTML = '<h3>Please correct the following errors</h3>';
	hideElementById('error-message');
	errorContainer = new Array();
}

/*
 * Formular Actions
 */

function showCompanyFields() {
	var company_field = new Array('row_company_title',
			'row_company_evaluation_for', 'row_company_industry',
			'row_company_funding_status', 'row_company_purchase_timeframe',
			'row_company_employees');
	for (i = 0; i < company_field.length; i++) {
		showElementById(company_field[i]);
	}
}

function hideCompanyFields() {
	var company_field = new Array('row_company_title',
			'row_company_evaluation_for', 'row_company_industry',
			'row_company_funding_status', 'row_company_purchase_timeframe',
			'row_company_employees');
	for (i = 0; i < company_field.length; i++) {
		hideElementById(company_field[i]);
	}
}

function isEvaluatingForCompany(){
	for ( var i = 0; i < document.forms['eccform'].elements['evaluating'].length; i++) {
		if (document.forms['eccform'].elements['evaluating'][i].checked == true) {
			if(document.forms['eccform'].elements['evaluating'][i].value == 'Yes') {
				return true;
			}
		}
	}
	return false;
}

function handleCompany() {
	if(isEvaluatingForCompany()) {
		showCompanyFields();
		setCompanyMandatory();
	} else {
		hideCompanyFields();
		unsetCompanyMandatory();
	}
}

function setCompanyMandatory() {
	showElementById('span-star-company', 'star');
}

function unsetCompanyMandatory() {
	hideElementById('span-star-company');
}
