﻿function StringFromForm (aeItem)
{
	var sRequest = 'ajax=1', i, eItem;

	for (i = aeItem.length; i-- ;)
	{
		eItem = aeItem[i];
		if (eItem.nodeName == 'INPUT')
		{
			if (eItem.type == 'text' || eItem.type == 'hidden')
				sRequest += '&' + eItem.name + '=' + encodeURIComponent(eItem.value);
			if (eItem.type == 'checkbox' && eItem.checked)
				sRequest += '&' + eItem.name + '=' + encodeURIComponent(eItem.value);
		}
		if (eItem.nodeName == 'TEXTAREA' || eItem.nodeName == 'SELECT')
			sRequest += '&' + eItem.name + '=' + encodeURIComponent(eItem.value);
	}

	return sRequest;
}

function getHTTPRequestObject()
{
	var xmlHttpRequest;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (exception1) {
		try {
			xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (exception2) {
			xmlHttpRequest = false;
		}
	}
	@else
		xmlhttpRequest = false;
	@end @*/
 
	if (!xmlHttpRequest && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlHttpRequest = new XMLHttpRequest();
		} catch (exception) {
			xmlHttpRequest = false;
		}
	}
	return xmlHttpRequest;
}
 
var httpRequester = getHTTPRequestObject();

var couldProcess = false;

function SendDemiseForm (eForm)
{
	if (!httpRequester)
		return true;

	if (!couldProcess && (eForm.demise_contact.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '') != ''))
	{
		var sParam = StringFromForm(eForm)
		httpRequester.open("POST", '/demise.php', true);
		httpRequester.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpRequester.setRequestHeader("Content-length", sParam.length);
		httpRequester.setRequestHeader("Connection", "close");
		httpRequester.onreadystatechange = function ()
		{
			if (httpRequester.readyState == 4 && httpRequester.responseText.indexOf('OK') != -1)
			{
				document.getElementById('demise_help').style.display = 'none';
				document.getElementById('demise_thanks').style.display = 'block';
				couldProcess = false;
			}
		}
		couldProcess = true;
		httpRequester.send(sParam);
	}

	return false;
}

function GetReviews (sId)
{
	if (!httpRequester)
		return true;

	if (!couldProcess)
	{
		httpRequester.open("GET", '?ajax=1', true);
		httpRequester.onreadystatechange = function ()
		{
			if (httpRequester.readyState == 4 && httpRequester.status == '200')
			{
				document.getElementById('client_reviews').innerHTML = httpRequester.responseText;
				couldProcess = false;
			}
		}
		couldProcess = true;
		setTimeout(function () {couldProcess = false}, 10000);
		httpRequester.send(null);
	}

	return false;
}

function CheckPromo (eItem)
{
	if (!httpRequester)
		return true;

	httpRequester.open("GET", '/check_promo.php?code=' + encodeURIComponent(eItem.value), true);
	httpRequester.onreadystatechange = function ()
	{
		if (httpRequester.readyState == 4 && httpRequester.status == '200')
		{
			var str = httpRequester.responseText;
			if ((str == '') ? false : !isNaN(str))
			{
				eItem.parentNode.className = 'good';
				eItem.parentNode.getElementsByTagName('SMALL')[0].innerHTML = 'Скидка активирована! Цена для вас&nbsp;— ' + str + ' <span class="hyphen"></span><span class="ruble">p</span><span class="dot">уб.</span>';
			}
			else
			{
				eItem.parentNode.className = 'bad';
				eItem.parentNode.getElementsByTagName('SMALL')[0].innerHTML = 'Ошибка :-(';
			}
		}
	}
	httpRequester.send(null);

	return false;
}

function validate_email (email)
{
	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	return email.match(re);
}

/*
The rest of the file is based on source: http://www.tigir.com/js/opacity.js
*/

function setElementOpacity(oElem, nOpacity)
{
	var p = getOpacityProperty();
	(setElementOpacity = p=="filter"?new Function('oElem', 'nOpacity', 'nOpacity *= 100;	var oAlpha = oElem.filters["DXImageTransform.Microsoft.alpha"] || oElem.filters.alpha;	if (oAlpha) oAlpha.opacity = nOpacity; else oElem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";'):p?new Function('oElem', 'nOpacity', 'oElem.style.'+p+' = nOpacity;'):new Function)(oElem, nOpacity);
}

function getOpacityProperty()
{
	var p;
	if (typeof document.body.style.opacity == 'string') p = 'opacity';
	else if (typeof document.body.style.MozOpacity == 'string') p =	'MozOpacity';
	else if (typeof document.body.style.KhtmlOpacity == 'string') p =	'KhtmlOpacity';
	else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) p =	'filter';
	
	return (getOpacityProperty = new Function("return '"+p+"';"))();
}

function fadeOpacity(sElemId, sRuleName, bBackward)
{
	var elem = document.getElementById(sElemId);
	if (!elem || !getOpacityProperty() || !fadeOpacity.aRules[sRuleName]) return false;
	
	var rule = fadeOpacity.aRules[sRuleName];
	var nOpacity = rule.nStartOpacity;
	
	if (fadeOpacity.aProc[sElemId]) {clearInterval(fadeOpacity.aProc[sElemId].tId); nOpacity = fadeOpacity.aProc[sElemId].nOpacity;}
	if ((nOpacity==rule.nStartOpacity && bBackward) || (nOpacity==rule.nFinishOpacity && !bBackward)) return false;

	fadeOpacity.aProc[sElemId] = {'nOpacity':nOpacity, 'tId':setInterval('fadeOpacity.run("'+sElemId+'")', fadeOpacity.aRules[sRuleName].nDalay), 'sRuleName':sRuleName, 'bBackward':Boolean(bBackward)};

	return true;
}

fadeOpacity.addRule = function(sRuleName, nStartOpacity, nFinishOpacity, nDalay){fadeOpacity.aRules[sRuleName]={'nStartOpacity':nStartOpacity, 'nFinishOpacity':nFinishOpacity, 'nDalay':(nDalay || 30),'nDSign':(nFinishOpacity-nStartOpacity > 0?1:-1)};};

fadeOpacity.back = function(sElemId){fadeOpacity(sElemId,fadeOpacity.aProc[sElemId].sRuleName,true);};

fadeOpacity.run = function(sElemId)
{
	var proc = fadeOpacity.aProc[sElemId];
	var rule = fadeOpacity.aRules[proc.sRuleName];
	
	proc.nOpacity = Math.round(( proc.nOpacity + .1*rule.nDSign*(proc.bBackward?-1:1) )*10)/10;
	setElementOpacity(document.getElementById(sElemId), proc.nOpacity);
	
	if (proc.nOpacity==rule.nStartOpacity || proc.nOpacity==rule.nFinishOpacity) clearInterval(fadeOpacity.aProc[sElemId].tId);
}
fadeOpacity.aProc = {};
fadeOpacity.aRules = {};
