function TrimString(str) {
str = str.replace(/^[ ]+(.*)$/, '$1'); // Trims leading spaces
str = str.replace(/^(.*)[ ]+$/, '$1'); // Trims trailing spaces
return str;
}

function checkParticipant_en(WhatCheck) {
    if(TrimString(document.getElementById('first_name').value) == '' ){
        alert("Fill in the 'First name' field");
        window.document.getElementById('first_name').focus();
        window.document.getElementById('first_name').select();
        return false;
    }
    if(TrimString(document.getElementById('last_name').value) == '' ){
        alert("Fill in the 'Last Name' field");
        window.document.getElementById('last_name').focus();
        window.document.getElementById('last_name').select();
        return false;
    }
    if(WhatCheck=='Part' && TrimString(document.getElementById('company').value) == '' ){
        alert("Fill in the 'Company' field");
        window.document.getElementById('company').focus();
        window.document.getElementById('company').select();
        return false;
    }
    if(TrimString(document.getElementById('email').value) == '' ){
        alert("Fill in the 'Email' field");
        window.document.getElementById('email').focus();
        window.document.getElementById('email').select();
        return false;
    }
    if(WhatCheck=='Part' && TrimString(document.getElementById('website').value) == '' ){
        alert("Fill in the 'WebSite' field");
        window.document.getElementById('website').focus();
        window.document.getElementById('website').select();
        return false;
    }
    if(!is_valid_email(document.getElementById('email').value) ){
        window.document.getElementById('email').focus();
        window.document.getElementById('email').select();
        return false;
    }
    if(WhatCheck=='Part' && TrimString(document.getElementById('position').value) == '' ){
        alert("Fill in the 'Position' field");
        window.document.getElementById('position').focus();
        window.document.getElementById('position').select();
        return false;
    }
    return true;
}

function checkParticipant_ru(WhatCheck) {
    if(TrimString(document.getElementById('first_name').value) == '' ){
        alert("Заполните поле 'Имя'");
        window.document.getElementById('first_name').focus();
        window.document.getElementById('first_name').select();
        return false;
    }
    if(TrimString(document.getElementById('last_name').value) == '' ){
        alert("Заполните поле 'Фамилия'");
        window.document.getElementById('last_name').focus();
        window.document.getElementById('last_name').select();
        return false;
    }
    if(WhatCheck=='Part' && TrimString(document.getElementById('company').value) == '' ){
        alert("Заполните поле 'Название компании'");
        window.document.getElementById('company').focus();
        window.document.getElementById('company').select();
        return false;
    }
    if(TrimString(document.getElementById('email').value) == '' ){
        alert("Заполните поле 'Email'");
        window.document.getElementById('email').focus();
        window.document.getElementById('email').select();
        return false;
    }
    if(WhatCheck=='Part'  && TrimString(document.getElementById('website').value) == '' ){
        alert("Заполните поле 'адрес Вашего WWW сайта'");
        window.document.getElementById('website').focus();
        window.document.getElementById('website').select();
        return false;
    }
    if(!is_valid_email(document.getElementById('email').value) ){
        window.document.getElementById('email').focus();
        window.document.getElementById('email').select();
        return false;
    }
    if(WhatCheck=='Part' && TrimString(document.getElementById('position').value) == '' ){
        alert("Заполните поле 'Должность");
        window.document.getElementById('position').focus();
        window.document.getElementById('position').select();
        return false;
    }
    return true;
}

function is_valid_email(mail)
{
    var re = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
    if (re.test(mail)) {
        return true;
    } else {
        alert('The email address is invalid.');
        return false;
    }
    
}



function selectGroup(groupId)
{
	var group = document.getElementById(groupId + "all");
	var count = 0;
	
	while(count < 100)
	{
		var item = document.getElementById(groupId + (count+1));
		
		if(item==null)	break;
		
		item.checked = group.checked;
		
		count++;
	}
}

function selectElement(groupId)
{
	var group = document.getElementById(groupId + "all");
	var count = 0;
	var selected = 0;
	
	while(count < 100)
	{
		var item = document.getElementById(groupId + (count+1));
		
		if(item==null)	break;
		
		if(item.checked)	selected++;
		
		count++;
	}
	
	if(selected < count)
	{
		group.checked = false;
	}
	else
	{
		group.checked = true;
	}
}

function checkElement(elementId)
{
	var element = document.getElementById(elementId);
	element.checked = !element.checked;
}




function movePhoto()
{
	var photo = document.getElementById("photo");
	
	var screenHeight = document.body.clientHeight - 50;
	var photoHeight = photo.height;
	
	var margin = Math.floor(screenHeight / 2 - photoHeight / 2);
	
	photo.style.margin = margin+"px 0 0 0";
}

