function getHttpObject()
{
	var xmlHttp;

	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try 
	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e) {
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlHttp = false;
		}
	}
	@else
		xmlHttp = false;
	@end @*/

	if (!xmlHttp && typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}

	return xmlHttp;
}

var http = getHttpObject();

function sendRequest(fileName, method, handler)
{
	fileName += "&rand=" + Math.random();
	http.open(method, fileName);
	http.onreadystatechange = handler;
	http.send(null);
}

function handleHttpResponse()
{
	if (http.readyState == 4)
	{
		if (http.status == 200)
		{
			// ok
		}
		else
		{
			// not ok
		}
    }
}



function getSubgroup(value, lang)
{
	var src = "";
	src += "/data/ajax/subgroup.php";
	src += '?value='+value;
	src += '&lang='+lang;

	sendRequest(src, "GET", handleSubgroup)
}

function handleSubgroup()
{
	if (http.readyState == 4)
	{
		if (http.status == 200)
		{
			document.getElementById("subgroupb").innerHTML = http.responseText;
		}
    }
}



function show(nr, id)
{
	for (var i = 1; i <= nr; i++)
	{
		document.getElementById("m"+i).style.background = "url('/images/main_meniu.jpg') no-repeat";
	}
	
	var arr = getElementsByClass('submeniu', null, null);
	for (var j = 0; j < arr.length; j++)
	{
		arr[j].style.display = 'none';
	}

	document.getElementById("submeniu_"+id).style.display = 'block';
	
	document.getElementById(id).style.background = "url('/images/main_meniu_selected.jpg') no-repeat";
	
	return;
}

function getElementsByClass(searchClass, node, tag) 
{
	var classElements = new Array();
	if (node==null) node = document;
	if (tag==null) tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++)
    {
		if (pattern.test(els[i].className))
        {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}



var color_norm = "#000000";
var color_fail = "#FA220B";

var last_fail = '';
var error = 'error';

function validLoginForm()
{
	var state = false;    
	error = 'login_error';
	state = fieldsCheck('password', 'username');
    if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function validEmail()
{
	var state = true;
	if (emailCheck(document.getElementById('email').value) == false)
	{
		document.getElementById('email').focus();
		state = false;
		document.getElementById(error).innerHTML = err_email;
		fadeIn(error, 0);
	}
    return state;
}

function validSubscription()
{
	var state = false;
	
	if (document.getElementById('group').value == '')
	{
		document.getElementById(error).innerHTML = err_group;
		fadeIn(error, 0);
	}
	else
	{
		state = true;
		if (emailCheck(document.getElementById('email').value) == false)
		{
			document.getElementById('email').focus();
			state = false;
			document.getElementById(error).innerHTML = err_email;
			fadeIn(error, 0);
		}
	}
    return state;
}

function validRegisterForm()
{
	var state = false;
	
	if (document.getElementById('name').value == '' && document.getElementById('company').value == '')
	{
		document.getElementById('name').focus();
		document.getElementById(error).innerHTML = err_oneoftwo;
		document.getElementById('name_l').style.color = color_fail;
		document.getElementById('company_l').style.color = color_fail;
		fadeIn(error, 0);
	}
	else
	{
		state = fieldsCheck('pass2', 'pass', 'user', 'email', 'phone', 'city');
		if (document.getElementById(last_fail))
		{
			document.getElementById(last_fail).focus();
		}
	}
	return state;
}

function validProfileForm()
{
	var state = false;
	
	if (document.getElementById('name').value == '' && document.getElementById('company').value == '')
	{
		document.getElementById('name').focus();
		document.getElementById(error).innerHTML = err_oneoftwo;
		document.getElementById('name_l').style.color = color_fail;
		document.getElementById('company_l').style.color = color_fail;
		fadeIn(error, 0);
	}
	else
	{
		state = fieldsCheck('email', 'phone', 'city');
		if (document.getElementById(last_fail))
		{
			document.getElementById(last_fail).focus();
		}
	}
	return state;
}

function validAddForm()
{
	var state = false;
	
	if (document.getElementById('group').value == '')
	{
		document.getElementById('group').focus();
		document.getElementById(error).innerHTML = err_group;
		fadeIn(error, 0);
	}
	else
	{
		if (document.getElementById('subgroup').value == '')
		{
			document.getElementById('subgroup').focus();
			document.getElementById(error).innerHTML = err_subgroup;
			fadeIn(error, 0);
		}
		else
		{
			state = fieldsCheck('code', 'city', 'phone', 'name', 'price', 'title');
			if (document.getElementById(last_fail))
			{
				document.getElementById(last_fail).focus();
			}
		}
	}
    return state;
}

function validEditForm()
{
	var state = false;
	
	state = fieldsCheck('city', 'phone', 'name', 'price', 'title');
	if (document.getElementById(last_fail))
	{
		document.getElementById(last_fail).focus();
	}
    return state;
}

function fieldsCheck()
{
	var state = true;
	document.getElementById(error).style.display = 'block';
    for (var n = 0; n < arguments.length; n++)
	{
		document.getElementById(arguments[n] + '_l').style.color = color_norm;
	}
    for (var n = 0; n < arguments.length; n++)
	{
		if (arguments[n] == 'email')
		{
			if (emailCheck(document.getElementById('email').value) == false)
			{
				last_fail = arguments[n];
				document.getElementById(arguments[n] + '_l').style.color = color_fail;
				state = false;
				document.getElementById(error).innerHTML = err_email;
				fadeIn(error, 0);
			}
		}
		else if (arguments[n] == 'code')
		{
			var code = document.getElementById('code').value;
			if (code == "" || code != document.getElementById('codeh').value)
			{
				last_fail = arguments[n];
				document.getElementById(arguments[n] + '_l').style.color = color_fail;
				state = false;
				document.getElementById(error).innerHTML = err_fields;
				fadeIn(error, 0);
			}	
		}
		else if (arguments[n] == 'pass2')
		{
			document.getElementById('pass_l').style.color = color_norm;
			document.getElementById('pass2_l').style.color = color_norm;
            if (document.getElementById('pass').value != "" && document.getElementById('pass2').value != "")
            {
				if (document.getElementById('pass').value != document.getElementById('pass2').value)
				{
					last_fail = 'pass';
					document.getElementById('pass_l').style.color = color_fail;
					document.getElementById('pass2_l').style.color = color_fail;
					state = false;
					document.getElementById(error).innerHTML = err_pass2;
					fadeIn(error, 0);
				}
			}
			else
			{
				last_fail = 'pass';
				document.getElementById('pass_l').style.color = color_fail;
				document.getElementById('pass2_l').style.color = color_fail;
				state = false;
				document.getElementById(error).innerHTML = err_fields;
				fadeIn(error, 0);
            }
        }
		else if (arguments[n] == 'price')
		{
			var price = document.getElementById('price').value;
			var pat = /[0-9,.]$/;
			var matchArr = price.match(pat);
			if (matchArr == null)
			{
				last_fail = 'price';
				document.getElementById('price_l').style.color = color_fail;
				state = false;
				document.getElementById(error).innerHTML = err_price;
				fadeIn(error, 0);
			}
		}
		else if (document.getElementById(arguments[n]).value == 0)
		{
			last_fail = arguments[n];
			document.getElementById(arguments[n] + '_l').style.color = color_fail;
			state = false;
			document.getElementById(error).innerHTML = err_fields;
			fadeIn(error, 0);
		}
	}
	return state; 
}

function emailCheck(emailStr)
{
	if (emailStr == "")
	{
		return false;
	}
    var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|lt|ru|ro|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);

	if (matchArray == null)
	{
		return false;
	}

	var user = matchArray[1];
	var domain = matchArray[2];

	for (i = 0; i < user.length; i++)
	{
		if (user.charCodeAt(i) > 127)
		{
			return false;
		}
	}

	for (i = 0; i < domain.length; i++)
	{
		if (domain.charCodeAt(i) > 127)
		{
			return false;
		}
	}

	if (user.match(userPat) == null)
	{
		return false;
	}

	var IPArray = domain.match(ipDomainPat);
	if (IPArray != null)
    {
		for (i = 1; i <= 4; i++)
		{
			if (IPArray[i] > 255)
			{
				return false;
			}
		}
		return true;
    }

	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	for (i = 0; i < len; i++)
	{
		if (domArr[i].search(atomPat) == -1)
		{
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length != 2 && domArr[domArr.length-1].search(knownDomsPat) == -1)
	{
		return false;
	}

	if (len < 2)
	{
		return false;
	}
}

function fadeIn(objId, opacity) 
{
    if (document.getElementById)
	{
		if (opacity <= 100)
        {
			document.getElementById(objId).style.MozOpacity = opacity / 100;
			document.getElementById(objId).style.filter = "alpha(opacity:" + opacity + ")";
			opacity += 10;
			window.setTimeout("fadeIn('" + objId + "'," + opacity + ")", 100);
        }
	}
}

function fadeOut(objId, opacity)
{
	if (document.getElementById)
	{
		if (opacity >= 0)
        {
			document.getElementById(objId).style.MozOpacity = opacity / 100;
			document.getElementById(objId).style.filter = "alpha(opacity:" + opacity + ")";
			opacity -= 10;
			window.setTimeout("fadeOut('" + objId + "'," + opacity + ")", 100);
		}
	}
}



function openPopup(url, width, height)
{
	var rnd = (Math.round((Math.random()*999)+1));
	var top = Math.round((screen.height-height)/2);
	var left = Math.round((screen.width-width)/2);
	this.open(url, rnd, "top="+top+",left="+left+",width="+width+",height="+height+",buttons=no,scrollbars=yes,location=no,menubar=no,resizable=yes,status=no,directories=no,toolbar=no");
}



function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			if (oldonload)
			{
				oldonload();
			}
			func();
		}
	}
}

function externalLinks()
{
	if (!document.getElementsByTagName) 
		return;

	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";                                               
	}
}

addLoadEvent (externalLinks);


function euro()
{
	var price = parseFloat(document.getElementById('price').value);
	if (isNaN(price))
	{
		document.getElementById('europrice').value = '';
	}
	else
	{
		document.getElementById('europrice').value = Math.round(price / 3.4528);
	}
}
