var ajax = new Array();

function getModels()
{
	var vendors = document.getElementById('vendors');
	var models = document.getElementById('models');
	var subModels = document.getElementById('subModels');
	var index = ajax.length;
	
	if (document.getElementById('modelCount') != null)
		document.getElementById('modelCount').innerHTML = '';
	if (document.getElementById('vendorCount') != null)
		document.getElementById('vendorCount').innerHTML = '';
	
	models.innerHTML = '';
	subModels.innerHTML = '';
	
	var ind = vendors.selectedIndex;
	var vendorId = vendors[ind].value; 
	
	ajax[index] = new sack();
	ajax[index].requestFile = '?action=showVendorModelsAjax&vendorId='+vendorId;
	ajax[index].onCompletion = function(){ var obj = models;eval(ajax[index].response); };
	ajax[index].runAJAX();	
}
function getSubModels()
{
	var models = document.getElementById('models');
	var subModels = document.getElementById('subModels');
	
	var index = ajax.length;
	
	subModels.innerHTML = '';
	
	var ind = models.selectedIndex;
	var modelId = models[ind].value; 
	//alert(modelId);
	ajax[index] = new sack();
	ajax[index].requestFile = '?action=showModelSubModelsAjax&modelId='+modelId;
	ajax[index].onCompletion = function(){ var obj = subModels;eval(ajax[index].response); };
	ajax[index].runAJAX();	
}
function getSubModelsCount()
{
	var subModels = document.getElementById('subModels');
	
	var index = ajax.length;
	
	var ind = subModels.selectedIndex;
	var subModelId = subModels[ind].value; 
	//alert(subModelId);
	ajax[index] = new sack();
	ajax[index].requestFile = '?action=getModelSubModelsCountAjax&subModelId='+subModelId;
	ajax[index].onCompletion = function(){ 	document.getElementById('subModelCount').innerHTML = ajax[index].response; };
	ajax[index].runAJAX();	
}
function selectType(type)
{
	var index = ajax.length;
	
	document.getElementById('htt').value = type;
	
	ajax[index] = new sack();
	ajax[index].requestFile = '?action=showHelpfulAdd&type='+type;
	ajax[index].onCompletion = function(){ document.getElementById('mainCont').innerHTML = ajax[index].response;tinyInit(); };
	ajax[index].runAJAX();	
}
function checkAndSubmit()
{
	var models = document.getElementById('models');
	
	var ind = models.selectedIndex;
	var modelId = models[ind].value;
	if (ind != 0)
		document.getElementById('carForm').submit();
	else
		alert('you must select a car model');
}
//stexic
function g(id)
{
	return document.getElementById(id);
}
function checkSubmit()
{
	var nickname = g('userName');
	var email = g('userEmail');
	var p1 = g('userPassword');
	var p2 = g('userPasswordRetype');
	if(!checkUsername(nickname.value))
	{
		alert('неправельное имя пользователя');
		return false;
	}
	if(!checkEmail(email.value))
	{
		alert('неправельный майл');
		return false;
	}
	if(!checkUsername(p1.value))
	{
		alert('неправельный пароль');
		return false;
	}
	if (p1.value != p2.value)
	{
		alert('пароли не совподают');
		return false;
	}
	document.getElementById('regForm').submit();
}
function checkSubmit1()
{
	var email = g('userEmail');
	if(!checkEmail(email.value))
	{
		alert('неправельный майл');
		return false;
	}
	document.getElementById('regForm').submit();
}
function checkSubmit2()
{
	var p1 = g('userPassword');
	if(!checkUsername(p1.value))
	{
		alert('неправельный пароль');
		return false;
	}
	document.getElementById('regForm').submit();
}
function checkSubmitEdit()
{
	var nickname = g('userName');
	var p1 = g('userPassword');
	var p2 = g('userPasswordRetype');
	if(!checkUsername(nickname.value))
	{
		alert('error username/неправельное имя пользователя');
		return false;
	}
	if(!checkUsername(p1.value))
	{
		alert('error password/неправельный пароль');
		return false;
	}
	if(checkPass(p1.value))
	{
		alert('Password cannot contain only numbers. It is unsecure./Пароль не должен содержать только цифры, это небезопасно.');
		return false;
	}
	if (p1.value != p2.value)
	{
		alert('passwords don\'t match/пароли не совподают');
		return false;
	}
	document.getElementById('regForm').submit();
}
function checkEmail(value)
{
	var patt = /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i;
	return patt.test(value);
}
function checkPass(value)
{
	var patt= /^\d+$/;
	return patt.test(value);	
}
function checkUsername(value)
{
	var patt= /^[a-z0-9 .~!@#$%^&*()_+-]+$/i;
	return patt.test(value);
}