var xmlHttp = createXmlHttpRequestObject (); 

function changeInput (item, string)
{
	if (item.value == "")
	{
		item.value = string;
		item.style.color = "#737272";
	}
	else if (item.value == string)
	{
		item.value = "";
		item.style.color = "#000";
	}
}

function needSupport (param)
{
	if (param == 'no')
	{
		if (document.getElementById ('nosupport').checked)
		{
			support = document.getElementsByName ('support[]');
			for (i = 0; i < support.length; i ++)
				support[i].checked = "";
			document.getElementById ('nosupport').checked = true;
		}
	}
	else if (param == 'domain')
	{
		if (document.getElementById ('domain').checked)
		{
			document.getElementById ('info').checked = true;
			document.getElementById ('nosupport').checked = false;
		}
	}
	else if (param == 'hosting')
	{
		if (document.getElementById ('hosting').checked)
		{
			document.getElementById ('info').checked = true;
			document.getElementById ('nosupport').checked = false;
		}
	}
	else if (param == 'info')
	{
		if (document.getElementById ('info').checked)
		{
			document.getElementById ('nosupport').checked = false;
		}
		else
		{
			if (document.getElementById ('hosting').checked || document.getElementById ('domain').checked)
				document.getElementById ('info').checked = true;
		}
	}
}

function checkOrder ()
{
	valueName = document.getElementsByName ("name").item (0).value;
	valueContact_person = document.getElementsByName ("contact_person").item (0).value;
	valueTelephone = document.getElementsByName ("telephone").item (0).value;
	valueEmail = document.getElementsByName ("email").item (0).value;
	valueStructure = document.getElementsByName ("structure").item (0).value;
	valueLanguages = document.getElementsByName ("languages").item (0).value;
	valueSupport = document.getElementsByName ("support[]").item (0).value;

	labelName = document.getElementById ("name");
	labelContact_person = document.getElementById ("contact_person");
	labelTelephone = document.getElementById ("telephone");
	labelEmail = document.getElementById ("email");
	labelStructure = document.getElementById ("structure");
	labelLanguages = document.getElementById ("languages");
	labelSupport = document.getElementById ("support");
	labelError = document.getElementById ("error");

	fix = 0;

	var scrolled = false;
	if (valueName.length < 3 || valueName == "" || valueName == "Введите название Вашего предприятия или Ваше ФИО")
	{
		labelName.style.color = "#ff0000";
		labelName.style.fontWeight = "bold";
		if (!scrolled)
			scrolled = 'name';
	}
	else
	{
		labelName.style.color = "";
		labelName.style.fontWeight = "";
	}
	if (valueContact_person.length < 2 || valueContact_person == "" || valueContact_person == "Введите имя представителя компании")
	{
		labelContact_person.style.color = "#ff0000";
		labelContact_person.style.fontWeight = "bold";
		if (!scrolled)
			scrolled = 'contact_person';
	}
	else
	{
		labelContact_person.style.color = "";
		labelContact_person.style.fontWeight = "";
	}
	if (valueTelephone.length < 6 || valueTelephone == "" || valueTelephone == "Введите контактные телефоны (с кодом города/оператора)")
	{
		labelTelephone.style.color = "#ff0000";
		labelTelephone.style.fontWeight = "bold";
		if (!scrolled)
			scrolled = 'telephone';
	}
	else
	{
		labelTelephone.style.color = "";
		labelTelephone.style.fontWeight = "";
	}
	if (!valueEmail.match (/[0-9a-zA-Z._-]+@[0-9a-zA-Z._-]+\.[a-zA-Z]{2,5}/))
	{
		labelEmail.style.color = "#ff0000";
		labelEmail.style.fontWeight = "bold";
		if (!scrolled)
			scrolled = 'email';
	}
	else
	{
		labelEmail.style.color = "";
		labelEmail.style.fontWeight = "";
	}
	if (valueStructure.length < 6 || valueStructure == "" || valueStructure == "Например: Главная, О нас, Каталог (Кухни, Мягкая мебель, Спальни), Контакты")
	{
		labelStructure.style.color = "#ff0000";
		labelStructure.style.fontWeight = "bold";
		if (!scrolled)
			scrolled = 'structure';
	}
	else
	{
		labelStructure.style.color = "";
		labelStructure.style.fontWeight = "";
	}
	if (valueLanguages == "" || valueLanguages == "Укажите языки или их количество")
	{
		labelLanguages.style.color = "#ff0000";
		labelLanguages.style.fontWeight = "bold";
		if (!scrolled)
			scrolled = 'languages';
	}
	else
	{
		labelLanguages.style.color = "";
		labelLanguages.style.fontWeight = "";
	}
	supportChecked = 0;
	support = document.getElementsByName ('support[]');
	for (i = 0; i < support.length; i ++)
		if (support[i].checked)
			supportChecked = 1;
	if (!supportChecked)
	{
		labelSupport.style.color = "#ff0000";
		labelSupport.style.fontWeight = "bold";
		fix = 1;
	}
	else
	{
		labelSupport.style.color = "";
		labelSupport.style.fontWeight = "";
	}

	if (scrolled)
	{
		$("html").animate({ scrollTop: $("#" + scrolled).offset().top - 72}, 500);
		if ($("*[name='" + scrolled + "']").is('input') || $("*[name='" + scrolled + "']").is('textarea'))
			$("*[name='" + scrolled + "']").focus ();
		return false;
	}
	else
	{
		labelName.style.color = "";
		labelName.style.fontWeight = "";
		labelContact_person.style.color = "";
		labelContact_person.style.fontWeight = "";
		labelTelephone.style.color = "";
		labelTelephone.style.fontWeight = "";
		labelEmail.style.color = "";
		labelEmail.style.fontWeight = "";
		labelStructure.style.color = "";
		labelStructure.style.fontWeight = "";
		labelLanguages.style.color = "";
		labelLanguages.style.fontWeight = "";
		labelSupport.style.color = "";
		labelSupport.style.fontWeight = "";
		labelError.innerHTML = "";
		return true;
	}
}

function show_description (id)
{
	document.getElementById ('workscreenshot').style.display = 'none';
	maindiv = document.getElementById ('workdescription').childNodes[0];
	if (document.getElementById ('workdescription').childNodes.length > 1)
		maindiv = document.getElementById ('workdescription').childNodes[1];
	maindiv.innerHTML = "";
	document.getElementById ('workdescription').style.display = 'block';

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open ("GET", "/inc/work_description.php?id=" + id, true);
		xmlHttp.onreadystatechange = show_description_response;
		xmlHttp.send (null);
	}

	return false;
}

function show_description_response ()
{
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
	{
		xmlResponse = xmlHttp.responseXML.documentElement;
		result = xmlResponse.getElementsByTagName ("result").item(0).firstChild.data;
		if (result == "OK")
		{
			id = xmlResponse.getElementsByTagName ("id").item(0).firstChild.data;
			field = xmlResponse.getElementsByTagName ("field").item(0).firstChild.data;
			name = xmlResponse.getElementsByTagName ("name").item(0).firstChild.data;
			img = xmlResponse.getElementsByTagName ("img");
			site = xmlResponse.getElementsByTagName ("site").item(0) ? xmlResponse.getElementsByTagName ("site").item(0).firstChild.data : "";
			text = xmlResponse.getElementsByTagName ("text").item(0).firstChild.data;
			features = xmlResponse.getElementsByTagName ("features").item(0) ? xmlResponse.getElementsByTagName ("features").item(0).firstChild.data : "";
			technics = xmlResponse.getElementsByTagName ("technics").item(0) ? xmlResponse.getElementsByTagName ("technics").item(0).firstChild.data : "";
			type = xmlResponse.getElementsByTagName ("type").item(0).firstChild.data;
			time = xmlResponse.getElementsByTagName ("time").item(0).firstChild.data;

			maindiv = document.getElementById ('workdescription').childNodes[0];
			if (document.getElementById ('workdescription').childNodes.length > 1)
				maindiv = document.getElementById ('workdescription').childNodes[1];

			header = document.createElement ('h6');
			header.innerHTML = name + " (";
			if (field == "site")
				header.innerHTML += "web-дизайн)";
			else if (field == "print")
				header.innerHTML += "полиграфия)";
			else if (field == "photo")
				header.innerHTML += "фотоманипуляции)";
			maindiv.appendChild (header);
			if (img.length)
			{
				imgdiv = document.createElement ('div');
				newAttr = document.createAttribute ("class"); newAttr.nodeValue = "screen_shots"; imgdiv.setAttributeNode (newAttr);
				for (i = 0; i < Math.min (3, img.length); i ++)
				{
					newimg = new Image;
					newimg.src = img[i].childNodes[0].firstChild.data + "?" + Math.floor (Math.random () * 500);
					if (img[i].childNodes[1].firstChild.data == 1)
						imgdiv.innerHTML += "<a href=\"\" " + (i == Math.min (3, img.length) - 1 ? "id=\"lasta\" " : "") + "onclick=\"return show_screenshots (" + id + ", " + i + ")\"><img src=\"" + newimg.src + "\" alt=\"Изображение работы\" /></a>";
					else
						imgdiv.innerHTML += "<span " + (i == Math.min (3, img.length) - 1 ? "id=\"lasta\" " : "") + "><img src=\"" + newimg.src + "\" alt=\"Изображение работы\" /></span>";
				}
				maindiv.appendChild (imgdiv);
			}
			maindiv.innerHTML += "<p><span>Наименование</span>: " + name + "</p>\n";
			if (site != "")
				maindiv.innerHTML += "<p><span>URL</span>: <a href=\"" + site + "\" class=\"url\">" + site + "</a></p>\n";
			maindiv.innerHTML += "<p><span>Описание:</span><br />" + text + "</p>\n";
			if (features.length > 1)
				maindiv.innerHTML += "<p><span>Технические особенности</span>:<br />" + features + "</p>";
			if (technics.length > 1)
				maindiv.innerHTML += "<p><span>Сайт разработан с использованием</span>: " + technics + "</p>";
// Выборка в портфолио по типу работы
			if (site == "site")
				maindiv.innerHTML += "<p><span>Тип сайта</span>: <a href=\"" + site + "\">" + type + "</a><br />";
			else
				maindiv.innerHTML += "<p><span>Тип сайта</span>: " + type + "<br />";
			if (time != "" && time != "/на стадии разработки/")
				maindiv.innerHTML += "<span>Срок выполнения</span>: " + time + "<br /></p>\n";
			else
				maindiv.innerHTML += time + "<br /></p>\n";
			maindiv.innerHTML += "<img src=\"/img/logo_min.jpg\" class=\"logo_min\" alt=\"Студия web-дизайна Stoff в Донецке. Создание сайтов.\" />\n";
			maindiv.innerHTML += "<a href=\"\" onclick=\"document.getElementById ('workdescription').style.display='none'; document.getElementById ('workscreenshot').style.display='none'; return false;\" class=\"close\" title=\"Закрыть окно\"></a>\n";
		}
	}
}

function show_screenshots (id, i)
{
	window.scroll (0, 0);
	maindiv = document.getElementById ('workscreenshot').childNodes[0];
	if (document.getElementById ('workscreenshot').childNodes.length > 1)
		maindiv = document.getElementById ('workscreenshot').childNodes[1];
	maindiv.innerHTML = "";
	document.getElementById ('workscreenshot').style.display='block';

	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		xmlHttp.open ("GET", "/inc/work_screenshots.php?id=" + id + "&i=" + i, true);
		xmlHttp.onreadystatechange = show_screenshots_response;
		xmlHttp.send (null);
	}

	return false;
}

function show_screenshots_response ()
{
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
	{
		xmlResponse = xmlHttp.responseXML.documentElement;
		result = xmlResponse.getElementsByTagName ("result").item(0).firstChild.data;
		if (result == "OK")
		{
			i = xmlResponse.getElementsByTagName ("i").item(0).firstChild.data;
			name = xmlResponse.getElementsByTagName ("name").item(0).firstChild.data;
			field = xmlResponse.getElementsByTagName ("field").item(0).firstChild.data;
			img = xmlResponse.getElementsByTagName ("img");

			maindiv = document.getElementById ('workscreenshot').childNodes[0];
			if (document.getElementById ('workscreenshot').childNodes.length > 1)
				maindiv = document.getElementById ('workscreenshot').childNodes[1];

			header = document.createElement ('h6');
			header.innerHTML = name + " (";
			if (field == "site")
				header.innerHTML += "web-дизайн)";
			else if (field == "print")
				header.innerHTML += "полиграфия)";
			else if (field == "photo")
				header.innerHTML += "фотоманипуляции)";
			maindiv.appendChild (header);
			if (img.length)
			{
				thisimg = i;
				if (img.length > 1)
				{
					maindiv.innerHTML += "<a href=\"\" class=\"left\" id=\"left\" onclick=\"imgchange ('-'); return false;\"><img src=\"/img/point_red_left.gif\" alt=\"Предыдущая страница\" /> предыдущая страница</a>\n";
					maindiv.innerHTML += "<a href=\"\" class=\"right\" id=\"right\" onclick=\"imgchange ('+'); return false;\">следующая страница <img src=\"/img/point_red.gif\" alt=\"Cледующая страница\" /></a>\n";
				}
				imgdiv = document.createElement ('div');
				newAttr = document.createAttribute ("id"); newAttr.nodeValue = "big_img"; imgdiv.setAttributeNode (newAttr);
				newAttr = document.createAttribute ("class"); newAttr.nodeValue = "screen_shots_big"; imgdiv.setAttributeNode (newAttr);
				newimg = new Image;
				newimg.src = img[thisimg].childNodes[0].firstChild.data + "?" + Math.floor (Math.random () * 500);
				imgdiv.innerHTML = "<img src=\"" + newimg.src + "\" alt=\"Изображение работы\" />";
			}
			maindiv.appendChild (imgdiv);
			maindiv.innerHTML += "<img src=\"/img/logo_min.jpg\" class=\"logo_min\" alt=\"Студия web-дизайна Stoff в Донецке. Создание сайтов.\" />\n";
			maindiv.innerHTML += "<a href=\"\" onclick=\"document.getElementById ('workscreenshot').style.display='none'; return false;\" class=\"close\" title=\"Закрыть окно\"></a>\n";
		}
	}
}

function imgchange (side)
{
	if (side == "-")
	{
		if (thisimg == 0)
			thisimg = img.length - 1;
		else
			thisimg --;
	}
	else
	{
		if (thisimg == img.length - 1)
			thisimg = 0;
		else
			thisimg ++;
	}
	imgdiv = document.getElementById ('big_img');
	newimg = new Image;
	newimg.src = img[thisimg].childNodes[0].firstChild.data + "?" + Math.floor (Math.random () * 500);
	imgdiv.innerHTML = "<img src=\"" + newimg.src + "\" alt=\"Изображение работы\" />";
}

function createXmlHttpRequestObject ()
{
	var xmlHttp;
	if (window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject ("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	else
	{
		try
		{
			xmlHttp = new XMLHttpRequest ();
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	if (xmlHttp)
		return xmlHttp;
}

